Rename "tsc" to "timestamp"
[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 18#ifdef TRACING
88c7c4ea 19#define LTTNG_UST_TRACEPOINT_DEFINE
a44af49d
ZT
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 39
4b4a1337 40static
e6af533d
DS
41void do_stuff(void)
42{
035d7688
MD
43 int i;
44#ifdef TRACING
45 int v = 50;
46#endif
e6af533d 47
035d7688
MD
48 for (i = 0; i < 100; i++)
49 cmm_barrier();
a44af49d 50#ifdef TRACING
cbc06a3b 51 lttng_ust_tracepoint(ust_tests_benchmark, tpbench, v);
e6af533d 52#endif
e6af533d
DS
53}
54
4b4a1337
MJ
55
56static
2208d8b5 57void *function(void *arg __attribute__((unused)))
e6af533d 58{
b670e9e8
MD
59 unsigned long long nr_loops = 0;
60 struct thread_counter *thread_counter = arg;
e6af533d 61
b670e9e8
MD
62 while (!test_go)
63 cmm_barrier();
64
65 for (;;) {
e6af533d 66 do_stuff();
b670e9e8
MD
67 nr_loops++;
68 if (test_stop)
69 break;
e6af533d 70 }
b670e9e8 71 thread_counter->nr_loops = nr_loops;
e6af533d
DS
72 return NULL;
73}
74
4b4a1337 75static
e6af533d 76void usage(char **argv) {
b670e9e8
MD
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");
e6af533d
DS
81}
82
e6af533d
DS
83int main(int argc, char **argv)
84{
b670e9e8
MD
85 unsigned long long total_loops = 0;
86 unsigned long i_thr;
e6af533d
DS
87 void *retval;
88 int i;
89
90 if (argc < 3) {
91 usage(argv);
92 exit(1);
93 }
94
b670e9e8
MD
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);
e6af533d 110
b670e9e8
MD
111 pthread_t thread[nr_threads];
112 struct thread_counter thread_counter[nr_threads];
e6af533d 113
b670e9e8
MD
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])) {
e6af533d
DS
117 fprintf(stderr, "thread create %d failed\n", i);
118 exit(1);
119 }
120 }
121
b670e9e8
MD
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++) {
e6af533d
DS
136 if (pthread_join(thread[i], &retval)) {
137 fprintf(stderr, "thread join %d failed\n", i);
138 exit(1);
139 }
b670e9e8 140 total_loops += thread_counter[i].nr_loops;
e6af533d 141 }
b670e9e8 142 printf("Number of loops: %llu\n", total_loops);
e6af533d
DS
143 return 0;
144}
This page took 0.043053 seconds and 4 git commands to generate.