tests: benchmark: use cpu-bound workload, calculate average and std.dev.
[lttng-ust.git] / tests / benchmark / bench.c
1 /*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 *
4 * Copyright 2010 Douglas Santos <douglas.santos@polymtl.ca>
5 *
6 * LTTng Userspace Tracer (UST) - benchmark tool
7 */
8
9 #include <stdio.h>
10 #include <pthread.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <sched.h>
14 #include <time.h>
15 #include <urcu/compiler.h>
16
17 #ifdef TRACING
18 #define TRACEPOINT_DEFINE
19 #include "ust_tests_benchmark.h"
20 #endif
21
22 static int nr_cpus;
23 static unsigned long nr_events;
24
25 void do_stuff(void)
26 {
27 int i;
28 #ifdef TRACING
29 int v = 50;
30 #endif
31
32 for (i = 0; i < 100; i++)
33 cmm_barrier();
34 #ifdef TRACING
35 tracepoint(ust_tests_benchmark, tpbench, v);
36 #endif
37 }
38
39 void *function(void *arg)
40 {
41 unsigned long i;
42
43 for (i = 0; i < nr_events; i++) {
44 do_stuff();
45 }
46 return NULL;
47 }
48
49 void usage(char **argv) {
50 printf("Usage: %s nr_cpus nr_events\n", argv[0]);
51 }
52
53
54 int main(int argc, char **argv)
55 {
56 void *retval;
57 int i;
58
59 if (argc < 3) {
60 usage(argv);
61 exit(1);
62 }
63
64 nr_cpus = atoi(argv[1]);
65 printf("using %d processor(s)\n", nr_cpus);
66
67 nr_events = atol(argv[2]);
68 printf("using %ld events per cpu\n", nr_events);
69
70 pthread_t thread[nr_cpus];
71 for (i = 0; i < nr_cpus; i++) {
72 if (pthread_create(&thread[i], NULL, function, NULL)) {
73 fprintf(stderr, "thread create %d failed\n", i);
74 exit(1);
75 }
76 }
77
78 for (i = 0; i < nr_cpus; i++) {
79 if (pthread_join(thread[i], &retval)) {
80 fprintf(stderr, "thread join %d failed\n", i);
81 exit(1);
82 }
83 }
84 return 0;
85 }
This page took 0.031738 seconds and 5 git commands to generate.