diff --git a/sockets/latency/latency-count b/sockets/latency/latency-count new file mode 100755 index 0000000..70b6b8c Binary files /dev/null and b/sockets/latency/latency-count differ diff --git a/sockets/latency/latency-count.c b/sockets/latency/latency-count.c index a928b24..5cd3e5f 100644 --- a/sockets/latency/latency-count.c +++ b/sockets/latency/latency-count.c @@ -9,6 +9,7 @@ #include #include #include +#include "../../util/hist.h" /**************************************** Author: Chenghu He, Tim Wood, Anthony Korzan @@ -184,6 +185,8 @@ int client_count_tcp(char *server_ip, char *server_port, char *number_input) struct timeval tval_start, tval_end, sock_opts; long long int diff, min = -1LL, max = -1LL; long double avg = 0.0L; + // From 0 seconds to 2,000,000 microseconds with 10 buckets + struct histogram* hist = histogram_create(0, 2000000, 10); /* Get the number from string number_input and check the result */ number_try = atoi(number_input); @@ -277,6 +280,8 @@ int client_count_tcp(char *server_ip, char *server_port, char *number_input) if (min == -1LL || diff < min) min = diff; if (max == -1LL || diff > max) max = diff; avg = avg * (successes - 1) / successes + (long double)diff / successes ; + + histogram_inc(hist, diff); } /* Show the result */ @@ -287,6 +292,11 @@ int client_count_tcp(char *server_ip, char *server_port, char *number_input) printf(" The average time of latency : %Lf us\n", avg); printf(" Number of Packets Dropped : %d\n", dropped); + printf("\n"); + printf("Histogram -- number of packets per bracket of nanoseconds:\n"); + histogram_print(hist); + histogram_free(hist); + freeaddrinfo(server); close(sockfd); return 0; @@ -303,6 +313,8 @@ int client_count_udp(char *server_ip, char *server_port, char *number_input) struct timeval tval_start, tval_end, sock_opts; long long int diff, min = -1LL, max = -1LL; long double avg = 0.0L; + // From 0 seconds to 2,000,000 microseconds with 10 buckets + struct histogram* hist = histogram_create(0, 2000000, 10); /* Get the number from string number_input and check the result */ number_try = atoi(number_input); @@ -392,6 +404,8 @@ int client_count_udp(char *server_ip, char *server_port, char *number_input) if (min == -1LL || diff < min) min = diff; if (max == -1LL || diff > max) max = diff; avg = avg * (successes - 1) / successes + (long double)diff / successes ; + + histogram_inc(hist, diff); } /* Show the result */ @@ -402,6 +416,11 @@ int client_count_udp(char *server_ip, char *server_port, char *number_input) printf(" The average time of latency : %Lf us\n", avg); printf(" Number of Packets Dropped : %d\n", dropped); + printf("\n"); + printf("Histogram -- number of packets per bracket of nanoseconds:\n"); + histogram_print(hist); + histogram_free(hist); + freeaddrinfo(server); close(sockfd); return 0; diff --git a/util/hist.h b/util/hist.h new file mode 100644 index 0000000..04b7847 --- /dev/null +++ b/util/hist.h @@ -0,0 +1,80 @@ +struct histogram { + int min; + int max; + int num_buckets; + double bucket_size; // For memo-izing the division + unsigned int* buckets; +}; + +/* Creates and returns the histogram datastructure + */ +struct histogram* histogram_create(int min, int max, int num_buckets) +{ + int i; + struct histogram* hist = (struct histogram*) malloc(sizeof(struct histogram)); + + hist->num_buckets = num_buckets; + hist->min = min; + hist->max = max; + + hist->bucket_size = (double) max / (double) num_buckets; + + // Create and zero the histogram's array + hist->buckets = (unsigned int*) malloc(sizeof(unsigned int) * num_buckets); + for (i = 0; i < num_buckets; i++) { + hist->buckets[i] = 0; + } + + return hist; +} + +/* Increments the respective bucket in the histogram + */ +void histogram_inc(struct histogram* hist, int data_point) +{ + int i = (int) (((double) data_point - hist->min) / hist->bucket_size); + + // Deal with the edge case of when data_point is max + if (i == hist->num_buckets) { + hist->buckets[i - 1]++; + } + else { + hist->buckets[i]++; + } +} + +/* Prints the histogram to the console + */ +void histogram_print(struct histogram* hist) +{ + int i; + double value = (double) hist->min; + + // Figure out how much we should right justify the buckets + int diff = hist->max - hist->min; + + if (diff < 10000) { + for (i = 0; i < hist->num_buckets; i++) { + printf("%8.2f\t%d\n", value, hist->buckets[i]); + value += hist->bucket_size; + } + } else if (diff < 10000000) { + for (i = 0; i < hist->num_buckets; i++) { + printf("%11.2f\t%d\n", value, hist->buckets[i]); + value += hist->bucket_size; + } + } else { + for (i = 0; i < hist->num_buckets; i++) { + printf("%14.2f\t%d\n", value, hist->buckets[i]); + value += hist->bucket_size; + } + } +} + +/* Frees the histogram from memory + */ +void histogram_free(struct histogram* hist) +{ + free(hist->buckets); + free(hist); +} \ No newline at end of file