8 /* counters to accumulate statistics */
10 uint64_t pkts, bytes, events;
11 uint64_t drop, drop_bytes;
14 uint32_t oq_n; /* number of elements in overflow queue (used in lb) */
17 /* very crude code to print a number in normalized form.
18 * Caller has to make sure that the buffer is large enough.
21 norm2(char *buf, double val, char *fmt, int normalize)
23 char *units[] = { "", "K", "M", "G", "T" };
26 for (i = 0; val >=1000 && i < sizeof(units)/sizeof(char *) - 1; i++)
30 sprintf(buf, fmt, val, units[i]);
34 static __inline const char *
35 norm(char *buf, double val, int normalize)
38 return norm2(buf, val, "%.3f %s", normalize);
40 return norm2(buf, val, "%.0f %s", normalize);
44 timespec_ge(const struct timespec *a, const struct timespec *b)
47 if (a->tv_sec > b->tv_sec)
49 if (a->tv_sec < b->tv_sec)
51 if (a->tv_nsec >= b->tv_nsec)
56 static __inline struct timespec
57 timeval2spec(const struct timeval *a)
59 struct timespec ts = {
61 .tv_nsec = a->tv_usec * 1000
66 static __inline struct timeval
67 timespec2val(const struct timespec *a)
71 .tv_usec = a->tv_nsec / 1000
77 static __inline struct timespec
78 timespec_add(struct timespec a, struct timespec b)
80 struct timespec ret = { a.tv_sec + b.tv_sec, a.tv_nsec + b.tv_nsec };
81 if (ret.tv_nsec >= 1000000000) {
83 ret.tv_nsec -= 1000000000;
88 static __inline struct timespec
89 timespec_sub(struct timespec a, struct timespec b)
91 struct timespec ret = { a.tv_sec - b.tv_sec, a.tv_nsec - b.tv_nsec };
92 if (ret.tv_nsec < 0) {
94 ret.tv_nsec += 1000000000;
99 static __inline uint64_t
100 wait_for_next_report(struct timeval *prev, struct timeval *cur,
103 struct timeval delta;
105 delta.tv_sec = report_interval/1000;
106 delta.tv_usec = (report_interval%1000)*1000;
107 if (select(0, NULL, NULL, NULL, &delta) < 0 && errno != EINTR) {
111 gettimeofday(cur, NULL);
112 timersub(cur, prev, &delta);
113 return delta.tv_sec* 1000000 + delta.tv_usec;