From 7bcbb1f6534713a915ad4d1ac2e8ac0523937920 Mon Sep 17 00:00:00 2001 From: Jef Poskanzer Date: Fri, 15 Nov 2013 09:54:21 -0800 Subject: [PATCH] Performance improvements. --- src/iperf_api.c | 7 +++++-- src/iperf_client_api.c | 18 ++++++++++-------- src/iperf_server_api.c | 15 ++++++++++----- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/iperf_api.c b/src/iperf_api.c index f2ccfbf..48a8b37 100644 --- a/src/iperf_api.c +++ b/src/iperf_api.c @@ -772,10 +772,13 @@ iperf_check_throttle(struct iperf_stream *sp, struct timeval *nowP) seconds = timeval_diff(&sp->result->start_time, nowP); bits_per_second = sp->result->bytes_sent * 8 / seconds; - if (bits_per_second < sp->test->settings->rate) + if (bits_per_second < sp->test->settings->rate) { sp->green_light = 1; - else + FD_SET(sp->socket, &sp->test->write_set); + } else { sp->green_light = 0; + FD_CLR(sp->socket, &sp->test->write_set); + } } int diff --git a/src/iperf_client_api.c b/src/iperf_client_api.c index 2178a29..18cf8fa 100644 --- a/src/iperf_client_api.c +++ b/src/iperf_client_api.c @@ -319,9 +319,10 @@ iperf_run_client(struct iperf_test * test) int startup; #define CM_SELECT 1 #define CM_SIGALRM 2 - int result; + int result = 0; fd_set read_set, write_set; struct timeval now; + struct timeval* timeout = NULL; /* Termination signals. */ iperf_catch_sigend(sigend_handler); @@ -355,13 +356,14 @@ iperf_run_client(struct iperf_test * test) startup = 1; concurrency_model = CM_SELECT; /* always start in select mode */ - (void) gettimeofday(&now, NULL); while (test->state != IPERF_DONE) { if (concurrency_model == CM_SELECT) { memcpy(&read_set, &test->read_set, sizeof(fd_set)); memcpy(&write_set, &test->write_set, sizeof(fd_set)); - result = select(test->max_fd + 1, &read_set, &write_set, NULL, tmr_timeout(&now)); + (void) gettimeofday(&now, NULL); + timeout = tmr_timeout(&now); + result = select(test->max_fd + 1, &read_set, &write_set, NULL, timeout); if (result < 0 && errno != EINTR) { i_errno = IESELECT; return -1; @@ -402,17 +404,17 @@ iperf_run_client(struct iperf_test * test) if (test->reverse) { // Reverse mode. Client receives. - if (iperf_recv(test, &read_set) < 0) { + if (iperf_recv(test, &read_set) < 0) return -1; - } } else { // Regular mode. Client sends. - if (iperf_send(test, concurrency_model == CM_SIGALRM ? NULL : &write_set) < 0) { + if (iperf_send(test, concurrency_model == CM_SIGALRM ? NULL : &write_set) < 0) return -1; - } } - if (concurrency_model == CM_SELECT || + if ((concurrency_model == CM_SELECT && + (result == 0 || + (timeout != NULL && timeout->tv_sec == 0 && timeout->tv_usec == 0))) || (concurrency_model == CM_SIGALRM && sigalrm_triggered)) { /* Run the timers. */ (void) gettimeofday(&now, NULL); diff --git a/src/iperf_server_api.c b/src/iperf_server_api.c index 9fb52d1..d5ec537 100644 --- a/src/iperf_server_api.c +++ b/src/iperf_server_api.c @@ -417,6 +417,7 @@ iperf_run_server(struct iperf_test *test) fd_set read_set, write_set; struct iperf_stream *sp; struct timeval now; + struct timeval* timeout; /* Termination signals. */ iperf_catch_sigend(sigend_handler); @@ -452,13 +453,14 @@ iperf_run_server(struct iperf_test *test) test->state = IPERF_START; streams_accepted = 0; - (void) gettimeofday(&now, NULL); while (test->state != IPERF_DONE) { memcpy(&read_set, &test->read_set, sizeof(fd_set)); memcpy(&write_set, &test->write_set, sizeof(fd_set)); - result = select(test->max_fd + 1, &read_set, &write_set, NULL, tmr_timeout(&now)); + (void) gettimeofday(&now, NULL); + timeout = tmr_timeout(&now); + result = select(test->max_fd + 1, &read_set, &write_set, NULL, timeout); if (result < 0 && errno != EINTR) { cleanup_server(test); i_errno = IESELECT; @@ -570,9 +572,12 @@ iperf_run_server(struct iperf_test *test) } } - /* Run the timers. */ - (void) gettimeofday(&now, NULL); - tmr_run(&now); + if (result == 0 || + (timeout != NULL && timeout->tv_sec == 0 && timeout->tv_usec == 0)) { + /* Run the timers. */ + (void) gettimeofday(&now, NULL); + tmr_run(&now); + } } } }