7 /* counters to accumulate statistics */
9 uint64_t pkts, bytes, events;
10 uint64_t drop, drop_bytes;
13 uint32_t oq_n; /* number of elements in overflow queue (used in lb) */
16 /* very crude code to print a number in normalized form.
17 * Caller has to make sure that the buffer is large enough.
20 norm2(char *buf, double val, const char *fmt, int normalize)
22 const char *units[] = { "", "K", "M", "G", "T" };
25 for (i = 0; val >=1000 && i < sizeof(units)/sizeof(const char *) - 1; i++)
29 sprintf(buf, fmt, val, units[i]);
33 static __inline const char *
34 norm(char *buf, double val, int normalize)
37 return norm2(buf, val, "%.3f %s", normalize);
39 return norm2(buf, val, "%.0f %s", normalize);
43 timespec_ge(const struct timespec *a, const struct timespec *b)
46 if (a->tv_sec > b->tv_sec)
48 if (a->tv_sec < b->tv_sec)
50 if (a->tv_nsec >= b->tv_nsec)
55 static __inline struct timespec
56 timeval2spec(const struct timeval *a)
58 struct timespec ts = {
60 .tv_nsec = a->tv_usec * 1000
65 static __inline struct timeval
66 timespec2val(const struct timespec *a)
70 .tv_usec = a->tv_nsec / 1000
76 static __inline struct timespec
77 timespec_add(struct timespec a, struct timespec b)
79 struct timespec ret = { a.tv_sec + b.tv_sec, a.tv_nsec + b.tv_nsec };
80 if (ret.tv_nsec >= 1000000000) {
82 ret.tv_nsec -= 1000000000;
87 static __inline struct timespec
88 timespec_sub(struct timespec a, struct timespec b)
90 struct timespec ret = { a.tv_sec - b.tv_sec, a.tv_nsec - b.tv_nsec };
91 if (ret.tv_nsec < 0) {
93 ret.tv_nsec += 1000000000;
98 static __inline uint64_t
99 wait_for_next_report(struct timeval *prev, struct timeval *cur,
102 struct timeval delta;
104 delta.tv_sec = report_interval/1000;
105 delta.tv_usec = (report_interval%1000)*1000;
106 if (select(0, NULL, NULL, NULL, &delta) < 0 && errno != EINTR) {
110 gettimeofday(cur, NULL);
111 timersub(cur, prev, &delta);
112 return delta.tv_sec* 1000000 + delta.tv_usec;