Skip to content

Commit

Permalink
iterate on implementation, add some comments to explain code
Browse files Browse the repository at this point in the history
  • Loading branch information
manedurphy committed May 21, 2024
1 parent fb52bba commit 8f56ce2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/iperf_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -892,15 +892,15 @@ void
iperf_on_test_start(struct iperf_test *test)
{
if (test->json_output) {
cJSON_AddItemToObject(test->json_start, "test_start", iperf_json_printf("protocol: %s num_streams: %d blksize: %d omit: %d duration: %d bytes: %d blocks: %d reverse: %d tos: %d target_bitrate: %d bidir: %d fqrate: %d interval: %f", test->protocol->name, (int64_t) test->num_streams, (int64_t) test->settings->blksize, (int64_t) test->omit, (int64_t) test->duration, (int64_t) test->settings->bytes, (int64_t) test->settings->blocks, test->reverse?(int64_t)1:(int64_t)0, (int64_t) test->settings->tos, (int64_t) test->settings->rate, (int64_t) test->bidirectional, (uint64_t) test->settings->fqrate, test->stats_interval));
cJSON_AddItemToObject(test->json_start, "test_start", iperf_json_printf("protocol: %s num_streams: %d blksize: %d omit: %d duration: %d server_duration: %d bytes: %d blocks: %d reverse: %d tos: %d target_bitrate: %d bidir: %d fqrate: %d interval: %f", test->protocol->name, (int64_t) test->num_streams, (int64_t) test->settings->blksize, (int64_t) test->omit, (int64_t) test->duration, (int64_t) test->settings->bytes, (int64_t) test->settings->blocks, test->reverse?(int64_t)1:(int64_t)0, (int64_t) test->settings->tos, (int64_t) test->settings->rate, (int64_t) test->bidirectional, (uint64_t) test->settings->fqrate, test->stats_interval));
} else {
if (test->verbose) {
if (test->settings->bytes)
iperf_printf(test, test_start_bytes, test->protocol->name, test->num_streams, test->settings->blksize, test->omit, test->settings->bytes, test->settings->tos);
else if (test->settings->blocks)
iperf_printf(test, test_start_blocks, test->protocol->name, test->num_streams, test->settings->blksize, test->omit, test->settings->blocks, test->settings->tos);
else
iperf_printf(test, test_start_time, test->protocol->name, test->num_streams, test->settings->blksize, test->omit, test->duration, test->settings->tos);
iperf_printf(test, test_start_time, test->protocol->name, test->num_streams, test->settings->blksize, test->omit, test->server_duration > test->duration ? test->server_duration : test->duration, test->settings->tos);
}
}
if (test->json_stream) {
Expand Down Expand Up @@ -2938,7 +2938,7 @@ iperf_defaults(struct iperf_test *testp)

testp->omit = OMIT;
testp->duration = DURATION;
testp->server_duration = -1;
testp->server_duration = 0;
testp->diskfile_name = (char*) 0;
testp->affinity = -1;
testp->server_affinity = -1;
Expand Down
2 changes: 2 additions & 0 deletions src/iperf_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,8 @@ enum {
IEPTHREADJOIN=152, // Unable to join thread (check perror)
IEPTHREADATTRINIT=153, // Unable to initialize thread attribute (check perror)
IEPTHREADATTRDESTROY=154, // Unable to destroy thread attribute (check perror)
IESENDSERVERDURATION=155, // Unable to send the server's maximum measurement duration
IERECVSERVERDURATION=156, // Unable to receive the server's maximum measurement duration
/* Stream errors */
IECREATESTREAM = 200, // Unable to create a new stream (check herror/perror)
IEINITSTREAM = 201, // Unable to initialize stream (check herror/perror)
Expand Down
32 changes: 28 additions & 4 deletions src/iperf_client_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,26 @@ create_client_timers(struct iperf_test * test)
}
cd.p = test;
test->timer = test->stats_timer = test->reporter_timer = NULL;
if (test->duration != 0) {
test->done = 0;
test->timer = tmr_create(&now, test_timer_proc, cd, ( test->duration + test->omit ) * SEC_TO_US, 0);

int duration = test->duration;
if (duration != 0) {
/**
* The duration of the measurement should only be overridden if it exceeds the duration set by the server or
* of no duration is set on the server.
*
*/
if (test->server_duration > 0 && test->server_duration < duration) {
duration = test->server_duration;
}

test->done = 0;
test->timer = tmr_create(&now, test_timer_proc, cd, (duration + test->omit) * SEC_TO_US, 0);
if (test->timer == NULL) {
i_errno = IEINITTEST;
return -1;
}
}
}

if (test->stats_interval != 0) {
test->stats_timer = tmr_create(&now, client_stats_timer_proc, cd, test->stats_interval * SEC_TO_US, 1);
if (test->stats_timer == NULL) {
Expand Down Expand Up @@ -426,6 +438,18 @@ iperf_connect(struct iperf_test *test)
return -1;
}

/**
* If the server has a value set for the --server-time flag, the client will read it here and adjust its measurement time accordingly.
* If the value is 0, then the measurement will run for the duration that the client has set.
*/
char server_duration_str[15] = "";
if (Nread(test->ctrl_sck, server_duration_str, sizeof(server_duration_str), Ptcp) != sizeof(server_duration_str))
{
i_errno = IERECVSERVERDURATION;
return -1;
}
test->server_duration = atoi(server_duration_str);

FD_SET(test->ctrl_sck, &test->read_set);
if (test->ctrl_sck > test->max_fd) test->max_fd = test->ctrl_sck;

Expand Down
32 changes: 27 additions & 5 deletions src/iperf_server_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,21 @@ iperf_accept(struct iperf_test *test)
i_errno = IERECVCOOKIE;
return -1;
}

/**
* If the server has a value set for the --server-time flag, send it to the client
* so that it can adjust its measurement time accordingly. If the value was not set
* via the flag, then the client will see a value of 0, which means that the server
* does not have a maximum measurement time.
*/
char server_duration_str[15] = "";
sprintf(server_duration_str, "%d", test->server_duration);

if (Nwrite(test->ctrl_sck, server_duration_str, sizeof(server_duration_str), Ptcp) < 0) {
i_errno = IESENDSERVERDURATION;
return -1;
}

FD_SET(test->ctrl_sck, &test->read_set);
if (test->ctrl_sck > test->max_fd) test->max_fd = test->ctrl_sck;

Expand Down Expand Up @@ -337,13 +352,20 @@ create_server_timers(struct iperf_test * test)
}
cd.p = test;
test->timer = test->stats_timer = test->reporter_timer = NULL;
if (test->duration != 0 ) {
if (test->server_duration > 0 && test->server_duration < test->duration) {
test->duration = test->server_duration;
grace_period = 0;

int duration = test->duration;
if (duration != 0 ) {
/**
* The duration of the measurement should only be overridden if it exceeds the duration set by the server or
* of no duration is set on the server.
*
*/
if (test->server_duration > 0 && test->server_duration < duration) {
duration = test->server_duration;
}

test->done = 0;
test->timer = tmr_create(&now, server_timer_proc, cd, (test->duration + test->omit + grace_period) * SEC_TO_US, 0);
test->timer = tmr_create(&now, server_timer_proc, cd, (duration + test->omit + grace_period) * SEC_TO_US, 0);
if (test->timer == NULL) {
i_errno = IEINITTEST;
return -1;
Expand Down

0 comments on commit 8f56ce2

Please sign in to comment.