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