]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/tools/netmap/ctrs.h
Remove $FreeBSD$: one-line .c comment pattern
[FreeBSD/FreeBSD.git] / tools / tools / netmap / ctrs.h
1 #ifndef CTRS_H_
2 #define CTRS_H_
3
4
5 #include <sys/time.h>
6
7 /* counters to accumulate statistics */
8 struct my_ctrs {
9         uint64_t pkts, bytes, events;
10         uint64_t drop, drop_bytes;
11         uint64_t min_space;
12         struct timeval t;
13         uint32_t oq_n; /* number of elements in overflow queue (used in lb) */
14 };
15
16 /* very crude code to print a number in normalized form.
17  * Caller has to make sure that the buffer is large enough.
18  */
19 static const char *
20 norm2(char *buf, double val, const char *fmt, int normalize)
21 {
22         const char *units[] = { "", "K", "M", "G", "T" };
23         u_int i;
24         if (normalize)
25                 for (i = 0; val >=1000 && i < sizeof(units)/sizeof(const char *) - 1; i++)
26                         val /= 1000;
27         else
28                 i=0;
29         sprintf(buf, fmt, val, units[i]);
30         return buf;
31 }
32
33 static __inline const char *
34 norm(char *buf, double val, int normalize)
35 {
36         if (normalize)
37                 return norm2(buf, val, "%.3f %s", normalize);
38         else
39                 return norm2(buf, val, "%.0f %s", normalize);
40 }
41
42 static __inline int
43 timespec_ge(const struct timespec *a, const struct timespec *b)
44 {
45
46         if (a->tv_sec > b->tv_sec)
47                 return (1);
48         if (a->tv_sec < b->tv_sec)
49                 return (0);
50         if (a->tv_nsec >= b->tv_nsec)
51                 return (1);
52         return (0);
53 }
54
55 static __inline struct timespec
56 timeval2spec(const struct timeval *a)
57 {
58         struct timespec ts = {
59                 .tv_sec = a->tv_sec,
60                 .tv_nsec = a->tv_usec * 1000
61         };
62         return ts;
63 }
64
65 static __inline struct timeval
66 timespec2val(const struct timespec *a)
67 {
68         struct timeval tv = {
69                 .tv_sec = a->tv_sec,
70                 .tv_usec = a->tv_nsec / 1000
71         };
72         return tv;
73 }
74
75
76 static __inline struct timespec
77 timespec_add(struct timespec a, struct timespec b)
78 {
79         struct timespec ret = { a.tv_sec + b.tv_sec, a.tv_nsec + b.tv_nsec };
80         if (ret.tv_nsec >= 1000000000) {
81                 ret.tv_sec++;
82                 ret.tv_nsec -= 1000000000;
83         }
84         return ret;
85 }
86
87 static __inline struct timespec
88 timespec_sub(struct timespec a, struct timespec b)
89 {
90         struct timespec ret = { a.tv_sec - b.tv_sec, a.tv_nsec - b.tv_nsec };
91         if (ret.tv_nsec < 0) {
92                 ret.tv_sec--;
93                 ret.tv_nsec += 1000000000;
94         }
95         return ret;
96 }
97
98 static __inline uint64_t
99 wait_for_next_report(struct timeval *prev, struct timeval *cur,
100                 int report_interval)
101 {
102         struct timeval delta;
103
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) {
107                 perror("select");
108                 abort();
109         }
110         gettimeofday(cur, NULL);
111         timersub(cur, prev, &delta);
112         return delta.tv_sec* 1000000 + delta.tv_usec;
113 }
114 #endif /* CTRS_H_ */
115