Debian Codegraph @

regressions/ck_pr/benchmark/benchmark.h

raw
1 #include <errno.h>
2 #include <inttypes.h>
3 #include <pthread.h>
4 #include <math.h>
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <strings.h>
10 #include <unistd.h>
11 #include <sys/time.h>
12
13 #include <ck_pr.h>
14
15 #include "../../common.h"
16
17 /* 8! = 40320, evenly divide 1 .. 8 processor workload. */
18 #define WORKLOAD (40320 * 2056)
19
20 struct block {
21 unsigned int tid;
22 };
23
24 static struct affinity a;
25 static unsigned int ready;
26 static uint64_t *count;
27 static uint64_t nthr;
28
29 static uint64_t object[2] CK_CC_CACHELINE;
30
31 static void *
32 fairness(void *null)
33 {
34 struct block *context = null;
35 unsigned int i = context->tid;
36
37 if (aff_iterate(&a)) {
38 perror("ERROR: Could not affine thread");
39 exit(EXIT_FAILURE);
40 }
41
42 while (ck_pr_load_uint(&ready) == 0);
43 while (ck_pr_load_uint(&ready)) {
44 ATOMIC;
45 ATOMIC;
46 ATOMIC;
47 ATOMIC;
48 ck_pr_store_64(count + i, count[i] + 1);
49 }
50
51 return (NULL);
52 }
53
54 int
55 main(int argc, char *argv[])
56 {
57 uint64_t v, d;
58 unsigned int i;
59 pthread_t *threads;
60 struct block *context;
61
62 if (argc != 3) {
63 ck_error("Usage: " ATOMIC_STRING " <number of threads> <affinity delta>\n");
64 exit(EXIT_FAILURE);
65 }
66
67 nthr = atoi(argv[1]);
68 if (nthr <= 0) {
69 ck_error("ERROR: Number of threads must be greater than 0\n");
70 exit(EXIT_FAILURE);
71 }
72
73 threads = malloc(sizeof(pthread_t) * nthr);
74 if (threads == NULL) {
75 ck_error("ERROR: Could not allocate thread structures\n");
76 exit(EXIT_FAILURE);
77 }
78
79 context = malloc(sizeof(struct block) * nthr);
80 if (context == NULL) {
81 ck_error("ERROR: Could not allocate thread contexts\n");
82 exit(EXIT_FAILURE);
83 }
84
85 a.delta = atoi(argv[2]);
86 a.request = 0;
87
88 count = malloc(sizeof(uint64_t) * nthr);
89 if (count == NULL) {
90 ck_error("ERROR: Could not create acquisition buffer\n");
91 exit(EXIT_FAILURE);
92 }
93 memset(count, 0, sizeof(uint64_t) * nthr);
94
95 fprintf(stderr, "Creating threads (fairness)...");
96 for (i = 0; i < nthr; i++) {
97 context[i].tid = i;
98 if (pthread_create(&threads[i], NULL, fairness, context + i)) {
99 ck_error("ERROR: Could not create thread %d\n", i);
100 exit(EXIT_FAILURE);
101 }
102 }
103 fprintf(stderr, "done\n");
104
105 ck_pr_store_uint(&ready, 1);
106 common_sleep(10);
107 ck_pr_store_uint(&ready, 0);
108
109 fprintf(stderr, "Waiting for threads to finish acquisition regression...");
110 for (i = 0; i < nthr; i++)
111 pthread_join(threads[i], NULL);
112 fprintf(stderr, "done\n\n");
113
114 for (i = 0, v = 0; i < nthr; i++) {
115 printf("%d %15" PRIu64 "\n", i, count[i]);
116 v += count[i];
117 }
118
119 printf("\n# total : %15" PRIu64 "\n", v);
120 printf("# throughput : %15" PRIu64 " a/s\n", (v /= nthr) / 10);
121
122 for (i = 0, d = 0; i < nthr; i++)
123 d += (count[i] - v) * (count[i] - v);
124
125 printf("# average : %15" PRIu64 "\n", v);
126 printf("# deviation : %.2f (%.2f%%)\n\n", sqrt(d / nthr), (sqrt(d / nthr) / v) * 100.00);
127
128 return (0);
129 }
130