8 /* counters to accumulate statistics */
10 uint64_t pkts, bytes, events, drop;
15 /* very crude code to print a number in normalized form.
16 * Caller has to make sure that the buffer is large enough.
19 norm2(char *buf, double val, char *fmt)
21 char *units[] = { "", "K", "M", "G", "T" };
24 for (i = 0; val >=1000 && i < sizeof(units)/sizeof(char *) - 1; i++)
26 sprintf(buf, fmt, val, units[i]);
30 static __inline const char *
31 norm(char *buf, double val)
33 return norm2(buf, val, "%.3f %s");
37 timespec_ge(const struct timespec *a, const struct timespec *b)
40 if (a->tv_sec > b->tv_sec)
42 if (a->tv_sec < b->tv_sec)
44 if (a->tv_nsec >= b->tv_nsec)
49 static __inline struct timespec
50 timeval2spec(const struct timeval *a)
52 struct timespec ts = {
54 .tv_nsec = a->tv_usec * 1000
59 static __inline struct timeval
60 timespec2val(const struct timespec *a)
64 .tv_usec = a->tv_nsec / 1000
70 static __inline struct timespec
71 timespec_add(struct timespec a, struct timespec b)
73 struct timespec ret = { a.tv_sec + b.tv_sec, a.tv_nsec + b.tv_nsec };
74 if (ret.tv_nsec >= 1000000000) {
76 ret.tv_nsec -= 1000000000;
81 static __inline struct timespec
82 timespec_sub(struct timespec a, struct timespec b)
84 struct timespec ret = { a.tv_sec - b.tv_sec, a.tv_nsec - b.tv_nsec };
85 if (ret.tv_nsec < 0) {
87 ret.tv_nsec += 1000000000;
93 wait_for_next_report(struct timeval *prev, struct timeval *cur,
98 delta.tv_sec = report_interval/1000;
99 delta.tv_usec = (report_interval%1000)*1000;
100 if (select(0, NULL, NULL, NULL, &delta) < 0 && errno != EINTR) {
104 gettimeofday(cur, NULL);
105 timersub(cur, prev, &delta);
106 return delta.tv_sec* 1000000 + delta.tv_usec;