Work on optimizing send(), to use fewer send function calls.

This commit is contained in:
Jan Vidar Krey 2009-06-21 14:21:34 +02:00
parent 7f24238ab5
commit ea3cd1bd90
4 changed files with 33 additions and 27 deletions

View File

@ -386,7 +386,7 @@ static int recv_client(struct ADC_client* client)
char* start = client->recvbuf; char* start = client->recvbuf;
char* pos; char* pos;
char* lastPos; char* lastPos = 0;
while ((pos = strchr(start, '\n'))) while ((pos = strchr(start, '\n')))
{ {
lastPos = pos; lastPos = pos;
@ -482,9 +482,16 @@ static int recv_client(struct ADC_client* client)
start = &pos[1]; start = &pos[1];
} }
if (lastPos)
{
client->r_offset = strlen(lastPos); client->r_offset = strlen(lastPos);
memmove(client->recvbuf, lastPos, strlen(lastPos)); memmove(client->recvbuf, lastPos, strlen(lastPos));
memset(&client->recvbuf[client->r_offset], 0, ADC_BUFSIZE-client->r_offset); memset(&client->recvbuf[client->r_offset], 0, ADC_BUFSIZE-client->r_offset);
}
else
{
// client->r_offset = size;
}
return 0; return 0;

View File

@ -20,7 +20,7 @@
#include "uhub.h" #include "uhub.h"
#include "hubio.h" #include "hubio.h"
#define SEND_CHUNKS 1 // #define SEND_CHUNKS 1
/* FIXME: This should not be needed! */ /* FIXME: This should not be needed! */
extern struct hub_info* g_hub; extern struct hub_info* g_hub;
@ -152,29 +152,27 @@ int hub_sendq_send(struct hub_sendq* q, hub_recvq_write w, void* data)
return bytes_sent; return bytes_sent;
#else #else
/* <======.....><.......><.......> */
int ret = 0; int ret = 0;
size_t sent = 0;
size_t bytes = 0; size_t bytes = 0;
size_t offset = q->offset; // offset into first message. size_t offset = q->offset; // offset into first message.
size_t remain = 0;
size_t length = 0; size_t length = 0;
size_t msgs = 0;
char* sbuf = g_hub->sendbuf; char* sbuf = g_hub->sendbuf;
size_t max_send_buf = 1000; // MAX_SEND_BUF; size_t max_send_buf = 4096;
/* Copy as many messages possible into global send queue */ /* Copy as many messages possible into global send queue */
struct adc_message* msg = list_get_first(q->queue); struct adc_message* msg = list_get_first(q->queue);
while (msg) while (msg)
{ {
msgs++;
length = MIN(msg->length - offset, (max_send_buf-1) - bytes); length = MIN(msg->length - offset, (max_send_buf-1) - bytes);
#ifdef DEBUG_SENDQ
printf("Queued: %d bytes (%d bytes)\n", (int) length, (int) msg->length);
#endif
memcpy(sbuf + bytes, msg->cache + offset, length); memcpy(sbuf + bytes, msg->cache + offset, length);
bytes += length; bytes += length;
if (length < msg->length - offset) if (length < (msg->length - offset))
break; break;
offset = 0; offset = 0;
msg = list_get_next(q->queue); msg = list_get_next(q->queue);
@ -182,7 +180,7 @@ int hub_sendq_send(struct hub_sendq* q, hub_recvq_write w, void* data)
msg = list_get_first(q->queue); msg = list_get_first(q->queue);
#ifdef DEBUG_SENDQ #ifdef DEBUG_SENDQ
printf("Queued up bytes: %d, in %d msgs (first=%d/%d)\n", (int) bytes, (int) msgs, (int) q->offset, (msg ? (int) msg->length : 0)); printf("Queued up bytes: %d (first=%d/%d)\n", (int) bytes, (int) q->offset, (msg ? (int) msg->length : 0));
#endif #endif
/* Send as much as possible */ /* Send as much as possible */
ret = w(data, sbuf, bytes); ret = w(data, sbuf, bytes);
@ -191,22 +189,24 @@ int hub_sendq_send(struct hub_sendq* q, hub_recvq_write w, void* data)
{ {
/* Remove messages sent */ /* Remove messages sent */
offset = q->offset; offset = q->offset;
msg = list_get_first(q->queue); remain = ret;
while (msg) while (msg)
{ {
sent += (msg->length - offset); length = msg->length - offset;
offset = 0; if (length >= remain)
{
if (sent >= bytes) q->offset += remain;
break; break;
}
#ifdef DEBUG_SENDQ #ifdef DEBUG_SENDQ
printf("removing msg %d [%p]\n", (int) msgs, msg); printf("removing msg %d [%p]\n", (int) msgs, msg);
#endif #endif
remain -= length;
hub_sendq_remove(q, msg); hub_sendq_remove(q, msg);
msgs--;
msg = list_get_next(q->queue); msg = list_get_next(q->queue);
offset = 0;
} }
q->offset = (bytes - sent);
} }
return ret; return ret;
#endif #endif

View File

@ -23,8 +23,6 @@
/* FIXME: This should not be needed! */ /* FIXME: This should not be needed! */
extern struct hub_info* g_hub; extern struct hub_info* g_hub;
/* #define DEBUG_SENDQ 1 */
int net_user_send(void* ptr, const void* buf, size_t len) int net_user_send(void* ptr, const void* buf, size_t len)
{ {
struct user* user = (struct user*) ptr; struct user* user = (struct user*) ptr;
@ -56,9 +54,7 @@ int net_user_recv(void* ptr, void* buf, size_t len)
{ {
struct user* user = (struct user*) ptr; struct user* user = (struct user*) ptr;
int ret = net_recv(user->net.sd, buf, len, 0); int ret = net_recv(user->net.sd, buf, len, 0);
/*
hub_log(log_trace, "net_user_recv: sd=%d, len=%d/%d", user->net.sd, ret, (int) len);
*/
if (ret > 0) if (ret > 0)
{ {
user->net.tm_last_read = time(NULL); user->net.tm_last_read = time(NULL);

View File

@ -23,6 +23,9 @@
/* Debugging */ /* Debugging */
/* #define NETWORK_DUMP_DEBUG */ /* #define NETWORK_DUMP_DEBUG */
/* #define MEMORY_DEBUG */ /* #define MEMORY_DEBUG */
/* #define DEBUG_SENDQ 1 */
#ifndef _GNU_SOURCE #ifndef _GNU_SOURCE
#define _GNU_SOURCE #define _GNU_SOURCE