| 1 | /* |
| 2 | * bench.c |
| 3 | * |
| 4 | * LTTng Userspace Tracer (UST) - benchmark tool |
| 5 | * |
| 6 | * Copyright 2010 - Douglas Santos <douglas.santos@polymtl.ca> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | #define _GNU_SOURCE |
| 24 | #include <stdio.h> |
| 25 | #include <pthread.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <unistd.h> |
| 28 | #include <sched.h> |
| 29 | #include <time.h> |
| 30 | |
| 31 | #ifdef TRACING |
| 32 | #define TRACEPOINT_DEFINE |
| 33 | #include "ust_tests_benchmark.h" |
| 34 | #endif |
| 35 | |
| 36 | static int nr_cpus; |
| 37 | static unsigned long nr_events; |
| 38 | |
| 39 | void do_stuff(void) |
| 40 | { |
| 41 | int v; |
| 42 | FILE *file; |
| 43 | |
| 44 | v = 1; |
| 45 | |
| 46 | file = fopen("/dev/null", "a"); |
| 47 | fprintf(file, "%d", v); |
| 48 | fclose(file); |
| 49 | time(NULL); |
| 50 | |
| 51 | #ifdef TRACING |
| 52 | tracepoint(ust_tests_benchmark, tpbench, v); |
| 53 | #endif |
| 54 | |
| 55 | } |
| 56 | |
| 57 | |
| 58 | void *function(void *arg) |
| 59 | { |
| 60 | unsigned long i; |
| 61 | |
| 62 | for(i = 0; i < nr_events; i++) { |
| 63 | do_stuff(); |
| 64 | } |
| 65 | return NULL; |
| 66 | } |
| 67 | |
| 68 | void usage(char **argv) { |
| 69 | printf("Usage: %s nr_cpus nr_events\n", argv[0]); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | int main(int argc, char **argv) |
| 74 | { |
| 75 | void *retval; |
| 76 | int i; |
| 77 | |
| 78 | if (argc < 3) { |
| 79 | usage(argv); |
| 80 | exit(1); |
| 81 | } |
| 82 | |
| 83 | nr_cpus = atoi(argv[1]); |
| 84 | printf("using %d processor(s)\n", nr_cpus); |
| 85 | |
| 86 | nr_events = atol(argv[2]); |
| 87 | printf("using %ld events per cpu\n", nr_events); |
| 88 | |
| 89 | pthread_t thread[nr_cpus]; |
| 90 | for (i = 0; i < nr_cpus; i++) { |
| 91 | if (pthread_create(&thread[i], NULL, function, NULL)) { |
| 92 | fprintf(stderr, "thread create %d failed\n", i); |
| 93 | exit(1); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | for (i = 0; i < nr_cpus; i++) { |
| 98 | if (pthread_join(thread[i], &retval)) { |
| 99 | fprintf(stderr, "thread join %d failed\n", i); |
| 100 | exit(1); |
| 101 | } |
| 102 | } |
| 103 | return 0; |
| 104 | } |