Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #8 Histogram Library and Ping Pong Latency functionality #88

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added sockets/latency/latency-count
Binary file not shown.
19 changes: 19 additions & 0 deletions sockets/latency/latency-count.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string.h>
#include <sys/time.h>
#include <sys/errno.h>
#include "../../util/hist.h"

/****************************************
Author: Chenghu He, Tim Wood, Anthony Korzan
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 */
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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 */
Expand All @@ -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;
Expand Down
80 changes: 80 additions & 0 deletions util/hist.h
Original file line number Diff line number Diff line change
@@ -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);
}