| 1 | /* |
| 2 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | * |
| 4 | * Copyright 2010 Douglas Santos <douglas.santos@polymtl.ca> |
| 5 | * Copyright 2021 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 6 | * |
| 7 | * LTTng Userspace Tracer (UST) - benchmark tool |
| 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
| 11 | #include <pthread.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <unistd.h> |
| 14 | #include <sched.h> |
| 15 | #include <time.h> |
| 16 | #include <urcu/compiler.h> |
| 17 | |
| 18 | #ifdef TRACING |
| 19 | #define LTTNG_UST_TRACEPOINT_DEFINE |
| 20 | #include "ust_tests_benchmark.h" |
| 21 | #endif |
| 22 | |
| 23 | #define printf_verbose(fmt, args...) \ |
| 24 | do { \ |
| 25 | if (verbose_mode) \ |
| 26 | printf(fmt, ## args); \ |
| 27 | } while (0) |
| 28 | |
| 29 | static int verbose_mode; |
| 30 | |
| 31 | struct thread_counter { |
| 32 | unsigned long long nr_loops; |
| 33 | }; |
| 34 | |
| 35 | static int nr_threads; |
| 36 | static unsigned long duration; |
| 37 | |
| 38 | static volatile int test_go, test_stop; |
| 39 | |
| 40 | static |
| 41 | void do_stuff(void) |
| 42 | { |
| 43 | int i; |
| 44 | #ifdef TRACING |
| 45 | int v = 50; |
| 46 | #endif |
| 47 | |
| 48 | for (i = 0; i < 100; i++) |
| 49 | cmm_barrier(); |
| 50 | #ifdef TRACING |
| 51 | lttng_ust_tracepoint(ust_tests_benchmark, tpbench, v); |
| 52 | #endif |
| 53 | } |
| 54 | |
| 55 | |
| 56 | static |
| 57 | void *function(void *arg __attribute__((unused))) |
| 58 | { |
| 59 | unsigned long long nr_loops = 0; |
| 60 | struct thread_counter *thread_counter = arg; |
| 61 | |
| 62 | while (!test_go) |
| 63 | cmm_barrier(); |
| 64 | |
| 65 | for (;;) { |
| 66 | do_stuff(); |
| 67 | nr_loops++; |
| 68 | if (test_stop) |
| 69 | break; |
| 70 | } |
| 71 | thread_counter->nr_loops = nr_loops; |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | static |
| 76 | void usage(char **argv) { |
| 77 | printf("Usage: %s nr_threads duration(s) <OPTIONS>\n", argv[0]); |
| 78 | printf("OPTIONS:\n"); |
| 79 | printf(" [-v] (verbose output)\n"); |
| 80 | printf("\n"); |
| 81 | } |
| 82 | |
| 83 | int main(int argc, char **argv) |
| 84 | { |
| 85 | unsigned long long total_loops = 0; |
| 86 | unsigned long i_thr; |
| 87 | void *retval; |
| 88 | int i; |
| 89 | |
| 90 | if (argc < 3) { |
| 91 | usage(argv); |
| 92 | exit(1); |
| 93 | } |
| 94 | |
| 95 | nr_threads = atoi(argv[1]); |
| 96 | duration = atol(argv[2]); |
| 97 | |
| 98 | for (i = 3; i < argc; i++) { |
| 99 | if (argv[i][0] != '-') |
| 100 | continue; |
| 101 | switch (argv[i][1]) { |
| 102 | case 'v': |
| 103 | verbose_mode = 1; |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | printf_verbose("using %d thread(s)\n", nr_threads); |
| 109 | printf_verbose("for a duration of %lds\n", duration); |
| 110 | |
| 111 | pthread_t thread[nr_threads]; |
| 112 | struct thread_counter thread_counter[nr_threads]; |
| 113 | |
| 114 | for (i = 0; i < nr_threads; i++) { |
| 115 | thread_counter[i].nr_loops = 0; |
| 116 | if (pthread_create(&thread[i], NULL, function, &thread_counter[i])) { |
| 117 | fprintf(stderr, "thread create %d failed\n", i); |
| 118 | exit(1); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | test_go = 1; |
| 123 | |
| 124 | for (i_thr = 0; i_thr < duration; i_thr++) { |
| 125 | sleep(1); |
| 126 | if (verbose_mode) { |
| 127 | fwrite(".", sizeof(char), 1, stdout); |
| 128 | fflush(stdout); |
| 129 | } |
| 130 | } |
| 131 | printf_verbose("\n"); |
| 132 | |
| 133 | test_stop = 1; |
| 134 | |
| 135 | for (i = 0; i < nr_threads; i++) { |
| 136 | if (pthread_join(thread[i], &retval)) { |
| 137 | fprintf(stderr, "thread join %d failed\n", i); |
| 138 | exit(1); |
| 139 | } |
| 140 | total_loops += thread_counter[i].nr_loops; |
| 141 | } |
| 142 | printf("Number of loops: %llu\n", total_loops); |
| 143 | return 0; |
| 144 | } |