| 1 | /* |
| 2 | * bench.c |
| 3 | * |
| 4 | * LTTng Userspace Tracer (UST) - benchmark tool |
| 5 | */ |
| 6 | |
| 7 | #define _GNU_SOURCE |
| 8 | #include <stdio.h> |
| 9 | #include <pthread.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <unistd.h> |
| 12 | #include <sched.h> |
| 13 | #include <ust/marker.h> |
| 14 | |
| 15 | static int nr_cpus; |
| 16 | static unsigned long nr_events; |
| 17 | pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
| 18 | |
| 19 | void do_stuff(void) |
| 20 | { |
| 21 | int v; |
| 22 | FILE *file; |
| 23 | int lock; |
| 24 | |
| 25 | v = 1; |
| 26 | |
| 27 | lock = pthread_mutex_lock(&mutex); |
| 28 | file = fopen("/tmp/bench.txt", "a"); |
| 29 | fprintf(file, "%d", v); |
| 30 | fclose(file); |
| 31 | lock = pthread_mutex_unlock(&mutex); |
| 32 | |
| 33 | #ifdef MARKER |
| 34 | trace_mark(ust, event, "event %d", v); |
| 35 | #endif |
| 36 | |
| 37 | } |
| 38 | |
| 39 | |
| 40 | void *function(void *arg) |
| 41 | { |
| 42 | unsigned long i; |
| 43 | |
| 44 | for(i = 0; i < nr_events; i++) { |
| 45 | do_stuff(); |
| 46 | } |
| 47 | return NULL; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | void usage(char **argv) { |
| 52 | printf("Usage: %s nr_cpus nr_events\n", argv[0]); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | int main(int argc, char **argv) |
| 57 | { |
| 58 | void *retval; |
| 59 | int i; |
| 60 | |
| 61 | if (argc < 3) { |
| 62 | usage(argv); |
| 63 | exit(1); |
| 64 | } |
| 65 | |
| 66 | nr_cpus = atoi(argv[1]); |
| 67 | printf("using %d processor(s)\n", nr_cpus); |
| 68 | |
| 69 | nr_events = atol(argv[2]); |
| 70 | printf("using %ld events per cpu\n", nr_events); |
| 71 | |
| 72 | pthread_t thread[nr_cpus]; |
| 73 | for (i = 0; i < nr_cpus; i++) { |
| 74 | if (pthread_create(&thread[i], NULL, function, NULL)) { |
| 75 | fprintf(stderr, "thread create %d failed\n", i); |
| 76 | exit(1); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | for (i = 0; i < nr_cpus; i++) { |
| 81 | if (pthread_join(thread[i], &retval)) { |
| 82 | fprintf(stderr, "thread join %d failed\n", i); |
| 83 | exit(1); |
| 84 | } |
| 85 | } |
| 86 | return 0; |
| 87 | } |