]>
Commit | Line | Data |
---|---|---|
aef4a13f DL |
1 | /* |
2 | * testing log message generator | |
3 | * Copyright (C) 2019-2020 David Lamparter for NetDEF, Inc. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License as published by the Free | |
7 | * Software Foundation; either version 2 of the License, or (at your option) | |
8 | * any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; see the file COPYING; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | */ | |
19 | ||
20 | #include <zebra.h> | |
21 | ||
22 | #include "vty.h" | |
23 | #include "command.h" | |
24 | #include "prefix.h" | |
25 | #include "nexthop.h" | |
26 | #include "log.h" | |
27 | #include "thread.h" | |
28 | #include "vrf.h" | |
29 | #include "zclient.h" | |
30 | #include "frr_pthread.h" | |
31 | ||
32 | #include "sharpd/sharp_vty.h" | |
33 | ||
34 | /* this is quite hacky, but then again it's a test tool and it does its job. */ | |
35 | static struct frr_pthread *lpt; | |
36 | ||
37 | static unsigned long lp_duration; | |
38 | static unsigned lp_frequency; | |
39 | static unsigned lp_burst; | |
40 | static size_t lp_ctr, lp_expect; | |
41 | static struct rusage lp_rusage; | |
42 | static struct vty *lp_vty; | |
43 | ||
44 | extern struct thread_master *master; | |
45 | ||
46 | static int logpump_done(struct thread *thread) | |
47 | { | |
48 | double x; | |
49 | ||
50 | vty_out(lp_vty, "\nlogpump done\n"); | |
51 | vty_out(lp_vty, "%9zu messages written\n", lp_ctr); | |
52 | x = (double)lp_ctr / (double)lp_expect * 100.; | |
53 | vty_out(lp_vty, "%9zu messages targeted = %5.1lf%%\n", lp_expect, x); | |
54 | ||
55 | x = lp_rusage.ru_utime.tv_sec * 1000000 + lp_rusage.ru_utime.tv_usec; | |
56 | x /= (double)lp_ctr; | |
57 | vty_out(lp_vty, "%6llu.%06u usr %9.1lfns/msg\n", | |
58 | (unsigned long long)lp_rusage.ru_utime.tv_sec, | |
59 | (unsigned)lp_rusage.ru_utime.tv_usec, x * 1000.); | |
60 | ||
61 | x = lp_rusage.ru_stime.tv_sec * 1000000 + lp_rusage.ru_stime.tv_usec; | |
62 | x /= (double)lp_ctr; | |
63 | vty_out(lp_vty, "%6llu.%06u sys %9.1lfns/msg\n", | |
64 | (unsigned long long)lp_rusage.ru_stime.tv_sec, | |
65 | (unsigned)lp_rusage.ru_stime.tv_usec, x * 1000.); | |
66 | ||
67 | frr_pthread_stop(lpt, NULL); | |
68 | frr_pthread_destroy(lpt); | |
69 | lpt = NULL; | |
70 | return 0; | |
71 | } | |
72 | ||
73 | static void *logpump_run(void *arg) | |
74 | { | |
75 | struct timespec start, next, now; | |
76 | unsigned long delta, period; | |
77 | ||
78 | period = 1000000000L / lp_frequency; | |
79 | ||
0bdeb5e5 DL |
80 | zlog_tls_buffer_init(); |
81 | ||
aef4a13f DL |
82 | clock_gettime(CLOCK_MONOTONIC, &start); |
83 | next = start; | |
84 | do { | |
85 | for (size_t inburst = 0; inburst < lp_burst; inburst++) | |
86 | zlog_debug("log pump: %zu (burst %zu)", | |
87 | lp_ctr++, inburst); | |
88 | ||
89 | clock_gettime(CLOCK_MONOTONIC, &now); | |
90 | delta = (now.tv_sec - start.tv_sec) * 1000000000L | |
91 | + (now.tv_nsec - start.tv_nsec); | |
92 | ||
93 | next.tv_nsec += period; | |
94 | if (next.tv_nsec > 1000000000L) { | |
95 | next.tv_sec++; | |
96 | next.tv_nsec -= 1000000000L; | |
97 | } | |
98 | #ifdef HAVE_CLOCK_NANOSLEEP | |
99 | clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next, NULL); | |
100 | #else | |
101 | struct timespec slpdur; | |
102 | ||
103 | slpdur.tv_sec = next.tv_sec - now.tv_sec; | |
104 | slpdur.tv_nsec = next.tv_nsec - now.tv_nsec; | |
105 | if (slpdur.tv_nsec < 0) { | |
106 | slpdur.tv_sec--; | |
107 | slpdur.tv_nsec += 1000000000L; | |
108 | } | |
109 | ||
110 | nanosleep(&slpdur, NULL); | |
111 | #endif | |
112 | } while (delta < lp_duration); | |
113 | ||
0bdeb5e5 DL |
114 | zlog_tls_buffer_fini(); |
115 | ||
aef4a13f DL |
116 | #ifdef RUSAGE_THREAD |
117 | getrusage(RUSAGE_THREAD, &lp_rusage); | |
118 | #else | |
119 | getrusage(RUSAGE_SELF, &lp_rusage); | |
120 | #endif | |
121 | ||
122 | thread_add_timer_msec(master, logpump_done, NULL, 0, NULL); | |
123 | return NULL; | |
124 | } | |
125 | ||
126 | static int logpump_halt(struct frr_pthread *fpt, void **res) | |
127 | { | |
128 | return 0; | |
129 | } | |
130 | ||
131 | /* default frr_pthread attributes */ | |
132 | static const struct frr_pthread_attr attr = { | |
133 | .start = logpump_run, | |
134 | .stop = logpump_halt, | |
135 | }; | |
136 | ||
137 | void sharp_logpump_run(struct vty *vty, unsigned duration, unsigned frequency, | |
138 | unsigned burst) | |
139 | { | |
140 | if (lpt != NULL) { | |
141 | vty_out(vty, "logpump already running\n"); | |
142 | return; | |
143 | } | |
144 | ||
145 | vty_out(vty, "starting logpump...\n"); | |
146 | vty_out(vty, "keep this VTY open and press Enter to see results\n"); | |
147 | ||
148 | lp_vty = vty; | |
149 | lp_duration = duration * 1000000000UL; | |
150 | lp_frequency = frequency; | |
151 | lp_burst = burst; | |
152 | lp_expect = duration * frequency * burst; | |
153 | lp_ctr = 0; | |
154 | ||
155 | lpt = frr_pthread_new(&attr, "logpump", "logpump"); | |
156 | frr_pthread_run(lpt, NULL); | |
157 | } |