Debian Codegraph @

src/queuelat/queuelat.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2
3 /*
4 * Copyright (C) 2018 Marcelo Tosatti <[email protected]>
5 * Copyright (C) 2019 John Kacur <[email protected]>
6 * Copyright (C) 2019 Clark Williams <[email protected]>
7 */
8
9 #include <string.h>
10 #include <stdlib.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <ctype.h>
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <signal.h>
18 #include <time.h>
19 #include <getopt.h>
20
21 #include "rt-utils.h"
22
23 /* Program parameters:
24 * max_queue_len: maximum latency allowed, in nanoseconds (int).
25 * cycles_per_packet: number of cycles to process one packet (int).
26 * mpps(million-packet-per-sec): million packets per second (float).
27 * tsc_freq_mhz: TSC frequency in MHz, as measured by TSC PIT calibration
28 * (search for "Detected XXX MHz processor" in dmesg, and use the integer part).
29 *
30 * How it works
31 * ============
32 *
33 * The program in essence does:
34 *
35 * b = rdtsc();
36 * memmove(dest, src, n);
37 * a = rdtsc();
38 *
39 * delay = convert_to_ns(a - b);
40 *
41 * queue_size += packets_queued_in(delay);
42 * queue_size -= packets_processed;
43 *
44 * if (queue_size > max_queue_len)
45 * FAIL();
46 *
47 * packets_processed is fixed, and is estimated as follows:
48 * n is determined first, so that the stats bucket with highest count
49 * takes max_latency/2.
50 * for max_latency/2, we calculate how many packets can be drained
51 * in that time (using cycles_per_packet).
52 *
53 */
54
55 int maxlatency;
56 int cycles_per_packet;
57 float mpps;
58 int timeout_secs;
59 int min_queue_size_to_print;
60
61 /* Derived constants */
62
63 float cycles_to_ns;
64 int max_queue_len;
65
66 int default_n;
67 int nr_packets_drain_per_block;
68
69 /*
70 * Parameters for the stats collection buckets
71 */
72
73 #define LAST_VAL 70000
74 #define VALS_PER_BUCKET 100
75 #define NR_BUCKETS LAST_VAL/VALS_PER_BUCKET
76
77 unsigned long long buckets[NR_BUCKETS+1];
78 unsigned long long total_count;
79
80 #define OUTLIER_BUCKET NR_BUCKETS
81
82 static int val_to_bucket(unsigned long long val)
83 {
84 int bucket_nr = val / VALS_PER_BUCKET;
85 if (bucket_nr >= NR_BUCKETS)
86 return OUTLIER_BUCKET;
87 return bucket_nr;
88 }
89
90 static void account(unsigned long long val)
91 {
92 int bucket_nr = val_to_bucket(val);
93 buckets[bucket_nr]++;
94 total_count++;
95 }
96
97 static unsigned long long total_samples(void)
98 {
99 int i;
100 unsigned long long total = 0;
101
102 for (i = 0; i <= OUTLIER_BUCKET; i++)
103 total += buckets[i];
104
105 return total;
106 }
107
108 static void print_all_buckets(void)
109 {
110 int i, print_dotdotdot = 0;
111
112 for (i = 0; i <= OUTLIER_BUCKET; i++) {
113 int bucket_nr;
114 unsigned long long val = i*VALS_PER_BUCKET;
115
116 bucket_nr = val_to_bucket(val);
117
118 if (bucket_nr != OUTLIER_BUCKET) {
119 int n_bucketnr = bucket_nr+1;
120 if (buckets[bucket_nr] == buckets[n_bucketnr]) {
121 print_dotdotdot = 1;
122 continue;
123 }
124 if (print_dotdotdot) {
125 printf("...\n");
126 print_dotdotdot = 0;
127 }
128 printf("[%lld - %lld] = %lld\n", val,
129 val + VALS_PER_BUCKET-1,
130 buckets[bucket_nr]);
131 } else {
132 if (print_dotdotdot) {
133 printf("...\n");
134 print_dotdotdot = 0;
135 }
136 printf("[%lld - END] = %lld\n", val,
137 buckets[bucket_nr]);
138 }
139 }
140 }
141
142 static void print_max_bucketsec(void)
143 {
144 int i, bucket_nr;
145 unsigned long long highest_val = 0;
146
147 for (i = 0; i <= OUTLIER_BUCKET; i++) {
148 unsigned long long val = i*VALS_PER_BUCKET;
149
150 bucket_nr = val_to_bucket(val);
151
152 if (buckets[bucket_nr] != 0)
153 highest_val = val;
154 }
155
156 bucket_nr = val_to_bucket(highest_val);
157 printf("Max loop processing time: [%lld - %lld] = %lld\n", highest_val,
158 highest_val + VALS_PER_BUCKET-1,
159 buckets[bucket_nr]);
160
161 return;
162 }
163
164 static void print_min_bucketsec(void)
165 {
166 int i, bucket_nr;
167 unsigned long long min_val = 0;
168
169 for (i = 0; i <= OUTLIER_BUCKET; i++) {
170 unsigned long long val = i*VALS_PER_BUCKET;
171
172 bucket_nr = val_to_bucket(val);
173
174 if (buckets[bucket_nr] != 0) {
175 min_val = val;
176 break;
177 }
178 }
179
180 bucket_nr = val_to_bucket(min_val);
181 printf("Min loop processing time: [%lld - %lld] = %lld\n", min_val,
182 min_val + VALS_PER_BUCKET-1,
183 buckets[bucket_nr]);
184
185 return;
186 }
187
188 static void print_avg_bucketsec(void)
189 {
190 int i, bucket_nr;
191 unsigned long long total_sum = 0;
192 unsigned long long nr_hits = 0;
193
194 for (i = 0; i <= OUTLIER_BUCKET; i++) {
195 unsigned long long val = i*VALS_PER_BUCKET;
196 unsigned long long maxtime;
197
198 bucket_nr = val_to_bucket(val);
199
200 maxtime = val + VALS_PER_BUCKET-1;
201 total_sum = total_sum + maxtime*buckets[bucket_nr];
202
203 nr_hits = nr_hits + buckets[bucket_nr];
204 }
205
206 printf("Avg loop processing time: %lld\n", total_sum / nr_hits);
207 }
208
209 static void print_all_buckets_drainlength(void)
210 {
211 int i, print_dotdotdot = 0;
212
213 for (i = 0; i <= OUTLIER_BUCKET; i++) {
214 int bucket_nr;
215 unsigned long long val = i*VALS_PER_BUCKET;
216
217 bucket_nr = val_to_bucket(val);
218
219 if (bucket_nr != OUTLIER_BUCKET) {
220 unsigned long long mindelta, maxdelta;
221 int nr_packets_minfill, nr_packets_maxfill;
222 int n_bucketnr = bucket_nr+1;
223
224 if (buckets[bucket_nr] == buckets[n_bucketnr]) {
225 print_dotdotdot = 1;
226 continue;
227 }
228 if (print_dotdotdot) {
229 printf("...\n");
230 print_dotdotdot = 0;
231 }
232
233 mindelta = val;
234 maxdelta = val + VALS_PER_BUCKET-1;
235
236 nr_packets_minfill = mindelta * mpps * 1000000 / NSEC_PER_SEC;
237 nr_packets_maxfill = maxdelta * mpps * 1000000 / NSEC_PER_SEC;
238
239 printf("[%lld - %lld] = %lld packetfillrates=[%d - %d]\n", val,
240 val + VALS_PER_BUCKET-1,
241 buckets[bucket_nr],
242 nr_packets_minfill,
243 nr_packets_maxfill);
244 } else {
245 if (print_dotdotdot) {
246 printf("...\n");
247 print_dotdotdot = 0;
248 }
249 printf("[%lld - END] = %lld\n", val,
250 buckets[bucket_nr]);
251 }
252 }
253 }
254
255 typedef unsigned long long cycles_t;
256 typedef unsigned long long usecs_t;
257 typedef unsigned long long u64;
258
259 #if defined __x86_64__ || defined __i386__
260
261 #ifdef __x86_64__
262 #define DECLARE_ARGS(val, low, high) unsigned low, high
263 #define EAX_EDX_VAL(val, low, high) ((low) | ((u64)(high) << 32))
264 #define EAX_EDX_ARGS(val, low, high) "a" (low), "d" (high)
265 #define EAX_EDX_RET(val, low, high) "=a" (low), "=d" (high)
266 #else
267 #define DECLARE_ARGS(val, low, high) unsigned long long val
268 #define EAX_EDX_VAL(val, low, high) (val)
269 #define EAX_EDX_ARGS(val, low, high) "A" (val)
270 #define EAX_EDX_RET(val, low, high) "=A" (val)
271 #endif
272
273 static inline unsigned long long __rdtscll(void)
274 {
275 DECLARE_ARGS(val, low, high);
276
277 asm volatile("mfence; rdtsc" : EAX_EDX_RET(val, low, high));
278
279 return EAX_EDX_VAL(val, low, high);
280 }
281
282 #define gettick(val) do { (val) = __rdtscll(); } while (0)
283
284 #else
285
286 static inline unsigned long long __clock_gettime(void)
287 {
288 struct timespec now;
289 int ret;
290
291 ret = clock_gettime(CLOCK_MONOTONIC, &now);
292 if (ret < 0)
293 return 0;
294
295 // Combine seconds and nanoseconds into a single value in nanoseconds
296 return (unsigned long long)now.tv_sec * 1000000000ULL + now.tv_nsec;
297 }
298
299 #define gettick(val) do { (val) = __clock_gettime(); } while (0)
300
301 #endif
302
303 static void init_buckets(void)
304 {
305 int i;
306
307 for (i=0; i <= NR_BUCKETS; i++)
308 buckets[i] = 0;
309
310 total_count = 0;
311 }
312
313 static int find_highest_count_bucket(void)
314 {
315 int i;
316 int max_bucket = 0;
317 unsigned long long max_val = 0;
318
319 for (i=0; i <= NR_BUCKETS; i++) {
320 if (buckets[i] > max_val) {
321 max_bucket = i;
322 max_val = buckets[i];
323 }
324 }
325
326 return max_bucket;
327 }
328
329 int tracing_mark_fd;
330 static void trace_open(void)
331 {
332 int fd;
333
334 fd = open("/sys/kernel/tracing/trace_marker", O_RDWR);
335
336 if (fd == -1) {
337 perror("open");
338 exit(0);
339 }
340 tracing_mark_fd = fd;
341 }
342
343 static void trace_write(char *buf, int len)
344 {
345 int ret;
346
347 ret = write(tracing_mark_fd, buf, len);
348 if (ret == -1) {
349 perror("write");
350 exit(0);
351 }
352 }
353
354 static void run_n(int n)
355 {
356 u64 a, b, delta;
357 void *dest, *src;
358 int i, loops = 50000;
359
360 init_buckets();
361
362 dest = malloc(n);
363 if (dest == NULL) {
364 printf("failure to allocate %d bytes "
365 " for dest\n", n);
366 exit(0);
367 }
368 src = malloc(n);
369 if (src == NULL) {
370 printf("failure to allocate %d bytes "
371 " for src\n", n);
372 exit(0);
373 }
374
375 memset(src, 0, n);
376
377 memmove(dest, src, n);
378 for (i = 0; i < loops; i++) {
379 gettick(b);
380 memmove(dest, src, n);
381 gettick(a);
382 delta = (a - b) * cycles_to_ns;
383 account(delta);
384 }
385
386 free(dest);
387 free(src);
388
389 return;
390 }
391
392 /*
393 * Find the size of n such that the stats for the
394 * function call
395 *
396 * memmove(dest, src, n).
397 *
398 * Takes MaximumLat/2 in the bucket that has most
399 * entries.
400 *
401 */
402 static int measure_n(void)
403 {
404 int time, bucket_nr;
405 int n = 100000, delta = 0;
406
407 do {
408 if (delta > 0)
409 n = n+1000;
410 else if (delta < 0)
411 n = n-1000;
412
413 run_n(n);
414 bucket_nr = find_highest_count_bucket();
415
416 time = bucket_nr * VALS_PER_BUCKET;
417
418 delta = maxlatency/2 - time;
419 } while (abs(delta) > VALS_PER_BUCKET*2);
420
421 return n;
422 }
423
424 static void convert_to_ghz(double tsc_freq_mhz)
425 {
426 float tsc_freq_ghz = tsc_freq_mhz/1000;
427
428 cycles_to_ns = 1/tsc_freq_ghz;
429
430 printf("tsc_freq_ghz = %f, cycles_to_ns = %f\n", tsc_freq_ghz,
431 cycles_to_ns);
432 }
433
434
435 static void print_exit_info(void)
436 {
437 print_all_buckets();
438 printf("\n ---------------- \n");
439 print_min_bucketsec();
440 print_max_bucketsec();
441 print_avg_bucketsec();
442
443 }
444
445 void main_loop(void)
446 {
447 u64 a, b, delta;
448 void *dest, *src;
449 int queue_size = 0;
450
451 trace_open();
452
453 init_buckets();
454
455 dest = malloc(default_n);
456 if (dest == NULL) {
457 printf("failure to allocate %d bytes "
458 " for dest\n", default_n);
459 exit(0);
460 }
461 src = malloc(default_n);
462 if (src == NULL) {
463 printf("failure to allocate %d bytes "
464 " for src\n", default_n);
465 exit(0);
466 }
467
468 memset(src, 0, default_n);
469 memmove(dest, src, default_n);
470
471 while (1) {
472 char buf[500];
473 int ret;
474 int nr_packets_fill;
475
476 gettick(b);
477 memmove(dest, src, default_n);
478 gettick(a);
479 delta = (a - b) * cycles_to_ns;
480 account(delta);
481
482 /* fill up the queue by the amount of
483 * time that passed */
484 nr_packets_fill = delta * mpps * 1000000 / NSEC_PER_SEC;
485 queue_size += nr_packets_fill;
486
487 /* decrease the queue by the amount of packets
488 * processed in maxlatency/2 nanoseconds of
489 * full processing.
490 */
491
492 queue_size -= nr_packets_drain_per_block;
493
494 if (queue_size < 0)
495 queue_size = 0;
496
497 if (queue_size <= min_queue_size_to_print)
498 continue;
499
500 ret = sprintf(buf, "memmove block queue_size=%d queue_dec=%d"
501 " queue_inc=%d delta=%llu ns\n", queue_size,
502 nr_packets_drain_per_block,
503 nr_packets_fill, delta);
504 trace_write(buf, ret);
505
506 if (queue_size > max_queue_len) {
507 printf("queue length exceeded: "
508 " queue_size=%d max_queue_len=%d\n",
509 queue_size, max_queue_len);
510 ret = sprintf(buf, "queue length exceeded: "
511 "queue_size=%d max_queue_len=%d\n",
512 queue_size, max_queue_len);
513 trace_write(buf, ret);
514 print_exit_info();
515 exit(0);
516 }
517 }
518
519 free(dest);
520 free(src);
521 }
522
523 void sig_handler(int sig)
524 {
525 print_exit_info();
526 exit(0);
527 }
528
529 static void install_signals(void)
530 {
531 signal(SIGALRM, sig_handler);
532 signal(SIGINT, sig_handler);
533 }
534
535 int calculate_nr_packets_drain_per_block(void)
536 {
537 unsigned long long maxcount;
538 int i, time;
539 int found = 0;
540 int bucket_nr = find_highest_count_bucket();
541
542 maxcount = total_samples() / 40;
543
544 for (i = bucket_nr+1; i <= NR_BUCKETS; i++) {
545 if (buckets[i] < maxcount) {
546 found = 1;
547 break;
548 }
549 }
550
551 if (found == 0) {
552 printf("error, did not find right bucket with < 10%% of total\n");
553 exit(0);
554 }
555
556 time = i*VALS_PER_BUCKET + VALS_PER_BUCKET-1;
557 nr_packets_drain_per_block = time / (cycles_per_packet*cycles_to_ns);
558
559 return nr_packets_drain_per_block;
560 }
561
562 static void print_help(int error)
563 {
564 printf("queuelat V %1.2f\n", VERSION);
565 printf("Usage:\n"
566 "queuelat <options>\n\n"
567 "-c N --cycles N number of cycles to process one packet (int)\n"
568 "-f F --freq F TSC frequency in MHz (float)\n"
569 "-h --help show this help menu\n"
570 "-m LEN --max-len LEN maximum latency allowed, in nanoseconds (int)\n"
571 "-p F --packets F million packets per second (float)\n"
572 "-q N --queue-len N minimum queue len to print trace (int)\n"
573 "-t TIME --timeout TIME timeout, in seconds (int)\n"
574 );
575 exit(error);
576 }
577
578 int main(int argc, char **argv)
579 {
580 double tsc_freq_mhz;
581 float max_queue_len_f;
582 char *mvalue = NULL;
583 char *cvalue = NULL;
584 char *pvalue = NULL;
585 char *fvalue = NULL;
586 char *tvalue = NULL;
587 char *qvalue = NULL;
588
589 opterr = 0;
590
591 for (;;) {
592 static struct option options[] = {
593 {"cycles", required_argument, NULL, 'c'},
594 {"freq", required_argument, NULL, 'f'},
595 {"help", no_argument, NULL, 'h'},
596 {"max-len", required_argument, NULL, 'm'},
597 {"packets", required_argument, NULL, 'p'},
598 {"queue-len", required_argument, NULL, 'q'},
599 {"timeout", required_argument, NULL, 't'},
600 {NULL, 0, NULL, 0}
601 };
602 int c = getopt_long(argc, argv, "c:f:hm:p:q:t:", options, NULL);
603 if (c == -1)
604 break;
605 switch (c) {
606 case 'c':
607 cvalue = optarg;
608 break;
609 case 'f':
610 fvalue = optarg;
611 break;
612 case '?':
613 case 'h':
614 print_help(0);
615 break;
616 case 'm':
617 mvalue = optarg;
618 break;
619 case 'p':
620 pvalue = optarg;
621 break;
622 case 'q':
623 qvalue = optarg;
624 break;
625 case 't':
626 tvalue = optarg;
627 break;
628 default:
629 print_help(1);
630 break;
631 }
632 }
633
634 if (mvalue == NULL || cvalue == NULL || pvalue == NULL || fvalue == NULL) {
635 printf("options -m, -c, -p and -f are required\n");
636 exit(1);
637 }
638
639 install_signals();
640
641 maxlatency = atoi(mvalue);
642 cycles_per_packet = atoi(cvalue);
643 mpps = atof(pvalue);
644 tsc_freq_mhz = atof(fvalue);
645
646 if (tvalue) {
647 int alarm_secs;
648 alarm_secs = atoi(tvalue);
649 alarm(alarm_secs);
650 }
651
652 if (qvalue)
653 min_queue_size_to_print = atoi(qvalue);
654
655 convert_to_ghz(tsc_freq_mhz);
656
657 max_queue_len_f = maxlatency / (cycles_per_packet*cycles_to_ns);
658 max_queue_len = max_queue_len_f;
659
660 printf("max_queue_len = %d\n", max_queue_len);
661 default_n = measure_n();
662
663 nr_packets_drain_per_block = calculate_nr_packets_drain_per_block();
664 print_all_buckets_drainlength();
665
666 printf("default_n=%d nr_packets_drain_per_block=%d\n", default_n,
667 nr_packets_drain_per_block);
668
669 main_loop();
670
671 return 0;
672 }
673