From ad426c6fd431c945877b4487b2e1c417b710cae8 Mon Sep 17 00:00:00 2001 From: sethdelliott Date: Wed, 2 Mar 2011 21:32:04 +0000 Subject: [PATCH] Added CPU utilization exchange and display (use verbose mode [-V]). --- src/iperf.h | 2 ++ src/iperf_api.c | 45 +++++++++++++++++++++++++++++++++++++++++++-- src/timer.c | 2 +- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/iperf.h b/src/iperf.h index c220299..43d955e 100644 --- a/src/iperf.h +++ b/src/iperf.h @@ -149,7 +149,9 @@ struct iperf_test struct timer *timer; struct timer *stats_timer; struct timer *reporter_timer; + double cpu_util; /* cpu utilization of the test */ + double remote_cpu_util; /* cpu utilization for the remote host/client */ int num_streams; /* total streams in the test (-P) */ diff --git a/src/iperf_api.c b/src/iperf_api.c index cb56623..11a9c70 100644 --- a/src/iperf_api.c +++ b/src/iperf_api.c @@ -529,7 +529,7 @@ package_parameters(struct iperf_test *test) snprintf(optbuf, sizeof(optbuf), "-P %d ", test->num_streams); strncat(pstring, optbuf, sizeof(pstring)); - + if (test->reverse) strncat(pstring, "-R ", sizeof(pstring)); @@ -734,6 +734,16 @@ iperf_exchange_results(struct iperf_test *test) /* Prepare results string and send to server */ results = NULL; size = 0; + + snprintf(buf, 128, "-C %f\n", test->cpu_util); + size += strlen(buf); + if ((results = malloc(size+1)) == NULL) { + i_errno = IEPACKAGERESULTS; + return (-1); + } + *results = '\0'; + strncat(results, buf, size+1); + SLIST_FOREACH(sp, &test->streams, streams) { bytes_transferred = (test->reverse ? sp->result->bytes_received : sp->result->bytes_sent); snprintf(buf, 128, "%d:%llu,%lf,%d,%d\n", sp->id, bytes_transferred,sp->jitter, @@ -743,8 +753,10 @@ iperf_exchange_results(struct iperf_test *test) i_errno = IEPACKAGERESULTS; return (-1); } +/* if (sp == SLIST_FIRST(&test->streams)) *results = '\0'; +*/ strncat(results, buf, size+1); } size++; @@ -807,6 +819,16 @@ iperf_exchange_results(struct iperf_test *test) /* Prepare results string and send to client */ results = NULL; size = 0; + + snprintf(buf, 128, "-C %f\n", test->cpu_util); + size += strlen(buf); + if ((results = malloc(size+1)) == NULL) { + i_errno = IEPACKAGERESULTS; + return (-1); + } + *results = '\0'; + strncat(results, buf, size+1); + SLIST_FOREACH(sp, &test->streams, streams) { bytes_transferred = (test->reverse ? sp->result->bytes_sent : sp->result->bytes_received); snprintf(buf, 128, "%d:%llu,%lf,%d,%d\n", sp->id, bytes_transferred, sp->jitter, @@ -816,8 +838,10 @@ iperf_exchange_results(struct iperf_test *test) i_errno = IEPACKAGERESULTS; return (-1); } +/* if (sp == SLIST_FIRST(&test->streams)) *results = '\0'; +*/ strncat(results, buf, size+1); } size++; @@ -845,10 +869,22 @@ parse_results(struct iperf_test *test, char *results) int sid, cerror, pcount; double jitter; char *strp; + char *tok; iperf_size_t bytes_transferred; struct iperf_stream *sp; - for (strp = results; *strp; strp = strchr(strp, '\n')+1) { + /* Isolate the first line */ + strp = strchr(results, '\n'); + *strp = '\0'; + strp++; + + for (tok = strtok(results, " "); tok; tok = strtok(NULL, " ")) { + if (strcmp(tok, "-C") == 0) { + test->remote_cpu_util = atof(strtok(NULL, " ")); + } + } + + for (strp; *strp; strp = strchr(strp, '\n')+1) { sscanf(strp, "%d:%llu,%lf,%d,%d\n", &sid, &bytes_transferred, &jitter, &cerror, &pcount); SLIST_FOREACH(sp, &test->streams, streams) @@ -1263,6 +1299,11 @@ iperf_reporter_callback(struct iperf_test * test) lost_packets, total_packets, (double) (100.0 * lost_packets / total_packets)); } } + if (test->verbose) { + printf("Host CPU Utilization: %.1f%%\n", test->cpu_util); + printf("Remote CPU Utilization: %.1f%%\n", test->remote_cpu_util); + } + break; } diff --git a/src/timer.c b/src/timer.c index a3c63ab..3fb5e94 100644 --- a/src/timer.c +++ b/src/timer.c @@ -176,6 +176,6 @@ cpu_util(double *pcpu) timediff = ((temp.tv_sec * 1000000.0 + temp.tv_usec) - (last.tv_sec * 1000000.0 + last.tv_usec)); - return ((ctemp - clast) / timediff); + *pcpu = ((ctemp - clast) / timediff) * 100; }