Fixed format and improved readibility of timer.c functions. Also formalized timer.h header.
This commit is contained in:
parent
3e402adeb7
commit
ff385f98e9
12
src/iperf.h
12
src/iperf.h
@ -125,12 +125,12 @@ struct iperf_test
|
|||||||
|
|
||||||
int (*accept) (struct iperf_test *);
|
int (*accept) (struct iperf_test *);
|
||||||
struct iperf_stream *(*new_stream) (struct iperf_test *);
|
struct iperf_stream *(*new_stream) (struct iperf_test *);
|
||||||
int stats_interval; /* time interval to gather stats (-i) */
|
|
||||||
void *(*stats_callback) (struct iperf_test *); /* callback function
|
int stats_interval;
|
||||||
* pointer for stats */
|
int reporter_interval;
|
||||||
int reporter_interval;/* time interval for reporter */
|
void (*stats_callback) (struct iperf_test *);
|
||||||
void (*reporter_callback) (struct iperf_test *); /* callback function
|
void (*reporter_callback) (struct iperf_test *);
|
||||||
* pointer for reporter */
|
|
||||||
int reporter_fd; /* file descriptor for reporter */
|
int reporter_fd; /* file descriptor for reporter */
|
||||||
int num_streams; /* total streams in the test (-P) */
|
int num_streams; /* total streams in the test (-P) */
|
||||||
int tcp_info; /* display getsockopt(TCP_INFO) results */
|
int tcp_info; /* display getsockopt(TCP_INFO) results */
|
||||||
|
@ -466,16 +466,14 @@ iperf_free_test(struct iperf_test * test)
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
void *
|
void
|
||||||
iperf_stats_callback(struct iperf_test * test)
|
iperf_stats_callback(struct iperf_test * test)
|
||||||
{
|
{
|
||||||
struct iperf_stream *sp = test->streams;
|
struct iperf_stream *sp;
|
||||||
struct iperf_stream_result *rp = NULL;
|
struct iperf_stream_result *rp = NULL;
|
||||||
struct iperf_interval_results *ip = NULL, temp;
|
struct iperf_interval_results *ip = NULL, temp;
|
||||||
|
|
||||||
//printf("in stats_callback: num_streams = %d role = %c\n", test->num_streams, test->role);
|
for (sp = test->streams; sp != NULL; sp = sp->next) {
|
||||||
|
|
||||||
while (sp != NULL) {
|
|
||||||
rp = sp->result;
|
rp = sp->result;
|
||||||
|
|
||||||
if (test->role == 'c')
|
if (test->role == 'c')
|
||||||
@ -500,12 +498,8 @@ iperf_stats_callback(struct iperf_test * test)
|
|||||||
add_to_interval_list(rp, &temp);
|
add_to_interval_list(rp, &temp);
|
||||||
rp->bytes_sent_this_interval = rp->bytes_received_this_interval = 0;
|
rp->bytes_sent_this_interval = rp->bytes_received_this_interval = 0;
|
||||||
|
|
||||||
/* for debugging */
|
}
|
||||||
//display_interval_list(rp, test->tcp_info);
|
|
||||||
sp = sp->next;
|
|
||||||
} /* for each stream */
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
|
@ -61,17 +61,13 @@ void Display(struct iperf_test * test);
|
|||||||
/**
|
/**
|
||||||
* iperf_stats_callback -- handles the statistic gathering
|
* iperf_stats_callback -- handles the statistic gathering
|
||||||
*
|
*
|
||||||
*returns void *
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
void *iperf_stats_callback(struct iperf_test * test);
|
void iperf_stats_callback(struct iperf_test * test);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* iperf_reporter_callback -- handles the report printing
|
* iperf_reporter_callback -- handles the report printing
|
||||||
*
|
*
|
||||||
*returns report
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
void iperf_reporter_callback(struct iperf_test * test);
|
void iperf_reporter_callback(struct iperf_test * test);
|
||||||
|
|
||||||
|
75
src/timer.c
75
src/timer.c
@ -9,12 +9,10 @@
|
|||||||
|
|
||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
double
|
double
|
||||||
timeval_to_double(struct timeval * tv)
|
timeval_to_double(struct timeval * tv)
|
||||||
{
|
{
|
||||||
double d;
|
double d;
|
||||||
|
|
||||||
d = tv->tv_sec + tv->tv_usec / 1000000;
|
d = tv->tv_sec + tv->tv_usec / 1000000;
|
||||||
|
|
||||||
@ -24,46 +22,41 @@ timeval_to_double(struct timeval * tv)
|
|||||||
double
|
double
|
||||||
timeval_diff(struct timeval * tv0, struct timeval * tv1)
|
timeval_diff(struct timeval * tv0, struct timeval * tv1)
|
||||||
{
|
{
|
||||||
return ((tv1->tv_sec - tv0->tv_sec) + (abs(tv1->tv_usec - tv0->tv_usec) / 1000000.0));
|
return ((abs(tv1->tv_sec - tv0->tv_sec)) + (abs(tv1->tv_usec - tv0->tv_usec) / 1000000.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
timer_expired(struct timer * tp)
|
timer_expired(struct timer * tp)
|
||||||
{
|
{
|
||||||
/* for timer with zero time */
|
/* for timer with zero time */
|
||||||
if (tp->end.tv_sec == tp->begin.tv_sec && tp->end.tv_usec == tp->begin.tv_usec)
|
if (tp->end.tv_sec == tp->begin.tv_sec && tp->end.tv_usec == tp->begin.tv_usec) {
|
||||||
{
|
return 0;
|
||||||
//printf(" timer_expired: begining and end times are equal \n");
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct timeval now;
|
struct timeval now;
|
||||||
int64_t end = 0, current = 0, diff = 0;
|
int64_t end = 0, current = 0;
|
||||||
|
|
||||||
//printf("checking if timer has expired \n");
|
if (gettimeofday(&now, NULL) < 0) {
|
||||||
if (gettimeofday(&now, NULL) < 0)
|
perror("gettimeofday");
|
||||||
{
|
return -1;
|
||||||
perror("gettimeofday");
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
end += tp->end.tv_sec * 1000000;
|
end += tp->end.tv_sec * 1000000;
|
||||||
end += tp->end.tv_usec;
|
end += tp->end.tv_usec;
|
||||||
|
|
||||||
current += now.tv_sec * 1000000;
|
current += now.tv_sec * 1000000;
|
||||||
current += now.tv_usec;
|
current += now.tv_usec;
|
||||||
|
|
||||||
diff = end - current;
|
return current > end;
|
||||||
return diff <= 0;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
update_timer(struct timer * tp, time_t sec, suseconds_t usec)
|
update_timer(struct timer * tp, time_t sec, suseconds_t usec)
|
||||||
{
|
{
|
||||||
if (gettimeofday(&tp->begin, NULL) < 0)
|
if (gettimeofday(&tp->begin, NULL) < 0) {
|
||||||
{
|
perror("gettimeofday");
|
||||||
perror("gettimeofday");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tp->end.tv_sec = tp->begin.tv_sec + (time_t) sec;
|
tp->end.tv_sec = tp->begin.tv_sec + (time_t) sec;
|
||||||
tp->end.tv_usec = tp->begin.tv_usec + (time_t) usec;
|
tp->end.tv_usec = tp->begin.tv_usec + (time_t) usec;
|
||||||
|
|
||||||
@ -75,16 +68,14 @@ new_timer(time_t sec, suseconds_t usec)
|
|||||||
{
|
{
|
||||||
struct timer *tp = NULL;
|
struct timer *tp = NULL;
|
||||||
tp = (struct timer *) calloc(1, sizeof(struct timer));
|
tp = (struct timer *) calloc(1, sizeof(struct timer));
|
||||||
if (tp == NULL)
|
if (tp == NULL) {
|
||||||
{
|
perror("malloc");
|
||||||
perror("malloc");
|
return NULL;
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (gettimeofday(&tp->begin, NULL) < 0)
|
if (gettimeofday(&tp->begin, NULL) < 0) {
|
||||||
{
|
perror("gettimeofday");
|
||||||
perror("gettimeofday");
|
return NULL;
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tp->end.tv_sec = tp->begin.tv_sec + (time_t) sec;
|
tp->end.tv_sec = tp->begin.tv_sec + (time_t) sec;
|
||||||
@ -108,19 +99,18 @@ delay(int64_t ns)
|
|||||||
|
|
||||||
req.tv_sec = 0;
|
req.tv_sec = 0;
|
||||||
|
|
||||||
while (ns >= 1000000000L)
|
while (ns >= 1000000000L) {
|
||||||
{
|
ns -= 1000000000L;
|
||||||
ns -= 1000000000L;
|
req.tv_sec += 1;
|
||||||
req.tv_sec += 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
req.tv_nsec = ns;
|
req.tv_nsec = ns;
|
||||||
|
|
||||||
while (nanosleep(&req, &rem) == -1)
|
while (nanosleep(&req, &rem) == -1)
|
||||||
if (EINTR == errno)
|
if (EINTR == errno)
|
||||||
memcpy(&req, &rem, sizeof rem);
|
memcpy(&req, &rem, sizeof rem);
|
||||||
else
|
else
|
||||||
return -1;
|
return -1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,11 +132,12 @@ timer_remaining(struct timer * tp)
|
|||||||
{
|
{
|
||||||
struct timeval now;
|
struct timeval now;
|
||||||
long int end_time = 0, current_time = 0, diff = 0;
|
long int end_time = 0, current_time = 0, diff = 0;
|
||||||
if (gettimeofday(&now, NULL) < 0)
|
|
||||||
{
|
if (gettimeofday(&now, NULL) < 0) {
|
||||||
perror("gettimeofday");
|
perror("gettimeofday");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
end_time += tp->end.tv_sec * 1000000;
|
end_time += tp->end.tv_sec * 1000000;
|
||||||
end_time += tp->end.tv_usec;
|
end_time += tp->end.tv_usec;
|
||||||
|
|
||||||
@ -155,7 +146,7 @@ timer_remaining(struct timer * tp)
|
|||||||
|
|
||||||
diff = end_time - current_time;
|
diff = end_time - current_time;
|
||||||
if (diff > 0)
|
if (diff > 0)
|
||||||
return diff;
|
return diff;
|
||||||
else
|
else
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,9 @@
|
|||||||
|
#ifndef __TIMER_H
|
||||||
|
#define __TIMER_H
|
||||||
|
|
||||||
|
#include <time.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
|
||||||
struct timer {
|
struct timer {
|
||||||
struct timeval begin;
|
struct timeval begin;
|
||||||
struct timeval end;
|
struct timeval end;
|
||||||
@ -18,3 +24,4 @@ int64_t timer_remaining(struct timer *tp);
|
|||||||
|
|
||||||
void free_timer(struct timer *tp);
|
void free_timer(struct timer *tp);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user