X-Git-Url: http://git.lttng.org./?a=blobdiff_plain;f=tests%2Fbenchmark%2Fbench.c;h=d15896cccd38dad1f5fdb89cc03920c2a95cf58c;hb=fd46f5c79a6a7178e35ce4f8ea21caa30da45cc4;hp=ec08130d764e8d5001a7836ab45ec470fa5b1fd1;hpb=e6af533d5eb211b3b01834621a1c022e07b124de;p=lttng-ust.git diff --git a/tests/benchmark/bench.c b/tests/benchmark/bench.c index ec08130d..d15896cc 100644 --- a/tests/benchmark/bench.c +++ b/tests/benchmark/bench.c @@ -2,6 +2,23 @@ * bench.c * * LTTng Userspace Tracer (UST) - benchmark tool + * + * Copyright 2010 - Douglas Santos + * Copyright 2021 - Mathieu Desnoyers + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #define _GNU_SOURCE @@ -10,51 +27,74 @@ #include #include #include -#include +#include +#include -static int nr_cpus; -static unsigned long nr_events; -pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +#ifdef TRACING +#define TRACEPOINT_DEFINE +#include "ust_tests_benchmark.h" +#endif -void do_stuff(void) -{ - int v; - FILE *file; - int lock; +#define printf_verbose(fmt, args...) \ + do { \ + if (verbose_mode) \ + printf(fmt, ## args); \ + } while (0) - v = 1; +static int verbose_mode; - lock = pthread_mutex_lock(&mutex); - file = fopen("/tmp/bench.txt", "a"); - fprintf(file, "%d", v); - fclose(file); - lock = pthread_mutex_unlock(&mutex); +struct thread_counter { + unsigned long long nr_loops; +}; -#ifdef MARKER - trace_mark(ust, event, "event %d", v); +static int nr_threads; +static unsigned long duration; + +static volatile int test_go, test_stop; + +void do_stuff(void) +{ + int i; +#ifdef TRACING + int v = 50; #endif + for (i = 0; i < 100; i++) + cmm_barrier(); +#ifdef TRACING + tracepoint(ust_tests_benchmark, tpbench, v); +#endif } - void *function(void *arg) { - unsigned long i; + unsigned long long nr_loops = 0; + struct thread_counter *thread_counter = arg; + + while (!test_go) + cmm_barrier(); - for(i = 0; i < nr_events; i++) { + for (;;) { do_stuff(); + nr_loops++; + if (test_stop) + break; } + thread_counter->nr_loops = nr_loops; return NULL; } - void usage(char **argv) { - printf("Usage: %s nr_cpus nr_events\n", argv[0]); + printf("Usage: %s nr_threads duration(s) \n", argv[0]); + printf("OPTIONS:\n"); + printf(" [-v] (verbose output)\n"); + printf("\n"); } - int main(int argc, char **argv) { + unsigned long long total_loops = 0; + unsigned long i_thr; void *retval; int i; @@ -63,25 +103,53 @@ int main(int argc, char **argv) exit(1); } - nr_cpus = atoi(argv[1]); - printf("using %d processor(s)\n", nr_cpus); + nr_threads = atoi(argv[1]); + duration = atol(argv[2]); + + for (i = 3; i < argc; i++) { + if (argv[i][0] != '-') + continue; + switch (argv[i][1]) { + case 'v': + verbose_mode = 1; + break; + } + } - nr_events = atol(argv[2]); - printf("using %ld events per cpu\n", nr_events); + printf_verbose("using %d thread(s)\n", nr_threads); + printf_verbose("for a duration of %lds\n", duration); - pthread_t thread[nr_cpus]; - for (i = 0; i < nr_cpus; i++) { - if (pthread_create(&thread[i], NULL, function, NULL)) { + pthread_t thread[nr_threads]; + struct thread_counter thread_counter[nr_threads]; + + for (i = 0; i < nr_threads; i++) { + thread_counter[i].nr_loops = 0; + if (pthread_create(&thread[i], NULL, function, &thread_counter[i])) { fprintf(stderr, "thread create %d failed\n", i); exit(1); } } - for (i = 0; i < nr_cpus; i++) { + test_go = 1; + + for (i_thr = 0; i_thr < duration; i_thr++) { + sleep(1); + if (verbose_mode) { + fwrite(".", sizeof(char), 1, stdout); + fflush(stdout); + } + } + printf_verbose("\n"); + + test_stop = 1; + + for (i = 0; i < nr_threads; i++) { if (pthread_join(thread[i], &retval)) { fprintf(stderr, "thread join %d failed\n", i); exit(1); } + total_loops += thread_counter[i].nr_loops; } + printf("Number of loops: %llu\n", total_loops); return 0; }