xref: /openbmc/qemu/tests/bench/atomic_add-bench.c (revision 3b472e71)
1*3b472e71SThomas Huth #include "qemu/osdep.h"
2*3b472e71SThomas Huth #include "qemu/thread.h"
3*3b472e71SThomas Huth #include "qemu/host-utils.h"
4*3b472e71SThomas Huth #include "qemu/processor.h"
5*3b472e71SThomas Huth 
6*3b472e71SThomas Huth struct thread_info {
7*3b472e71SThomas Huth     uint64_t r;
8*3b472e71SThomas Huth } QEMU_ALIGNED(64);
9*3b472e71SThomas Huth 
10*3b472e71SThomas Huth struct count {
11*3b472e71SThomas Huth     QemuMutex lock;
12*3b472e71SThomas Huth     unsigned long val;
13*3b472e71SThomas Huth } QEMU_ALIGNED(64);
14*3b472e71SThomas Huth 
15*3b472e71SThomas Huth static QemuThread *threads;
16*3b472e71SThomas Huth static struct thread_info *th_info;
17*3b472e71SThomas Huth static unsigned int n_threads = 1;
18*3b472e71SThomas Huth static unsigned int n_ready_threads;
19*3b472e71SThomas Huth static struct count *counts;
20*3b472e71SThomas Huth static unsigned int duration = 1;
21*3b472e71SThomas Huth static unsigned int range = 1024;
22*3b472e71SThomas Huth static bool use_mutex;
23*3b472e71SThomas Huth static bool test_start;
24*3b472e71SThomas Huth static bool test_stop;
25*3b472e71SThomas Huth 
26*3b472e71SThomas Huth static const char commands_string[] =
27*3b472e71SThomas Huth     " -n = number of threads\n"
28*3b472e71SThomas Huth     " -m = use mutexes instead of atomic increments\n"
29*3b472e71SThomas Huth     " -p = enable sync profiler\n"
30*3b472e71SThomas Huth     " -d = duration in seconds\n"
31*3b472e71SThomas Huth     " -r = range (will be rounded up to pow2)";
32*3b472e71SThomas Huth 
33*3b472e71SThomas Huth static void usage_complete(char *argv[])
34*3b472e71SThomas Huth {
35*3b472e71SThomas Huth     fprintf(stderr, "Usage: %s [options]\n", argv[0]);
36*3b472e71SThomas Huth     fprintf(stderr, "options:\n%s\n", commands_string);
37*3b472e71SThomas Huth }
38*3b472e71SThomas Huth 
39*3b472e71SThomas Huth /*
40*3b472e71SThomas Huth  * From: https://en.wikipedia.org/wiki/Xorshift
41*3b472e71SThomas Huth  * This is faster than rand_r(), and gives us a wider range (RAND_MAX is only
42*3b472e71SThomas Huth  * guaranteed to be >= INT_MAX).
43*3b472e71SThomas Huth  */
44*3b472e71SThomas Huth static uint64_t xorshift64star(uint64_t x)
45*3b472e71SThomas Huth {
46*3b472e71SThomas Huth     x ^= x >> 12; /* a */
47*3b472e71SThomas Huth     x ^= x << 25; /* b */
48*3b472e71SThomas Huth     x ^= x >> 27; /* c */
49*3b472e71SThomas Huth     return x * UINT64_C(2685821657736338717);
50*3b472e71SThomas Huth }
51*3b472e71SThomas Huth 
52*3b472e71SThomas Huth static void *thread_func(void *arg)
53*3b472e71SThomas Huth {
54*3b472e71SThomas Huth     struct thread_info *info = arg;
55*3b472e71SThomas Huth 
56*3b472e71SThomas Huth     qatomic_inc(&n_ready_threads);
57*3b472e71SThomas Huth     while (!qatomic_read(&test_start)) {
58*3b472e71SThomas Huth         cpu_relax();
59*3b472e71SThomas Huth     }
60*3b472e71SThomas Huth 
61*3b472e71SThomas Huth     while (!qatomic_read(&test_stop)) {
62*3b472e71SThomas Huth         unsigned int index;
63*3b472e71SThomas Huth 
64*3b472e71SThomas Huth         info->r = xorshift64star(info->r);
65*3b472e71SThomas Huth         index = info->r & (range - 1);
66*3b472e71SThomas Huth         if (use_mutex) {
67*3b472e71SThomas Huth             qemu_mutex_lock(&counts[index].lock);
68*3b472e71SThomas Huth             counts[index].val += 1;
69*3b472e71SThomas Huth             qemu_mutex_unlock(&counts[index].lock);
70*3b472e71SThomas Huth         } else {
71*3b472e71SThomas Huth             qatomic_inc(&counts[index].val);
72*3b472e71SThomas Huth         }
73*3b472e71SThomas Huth     }
74*3b472e71SThomas Huth     return NULL;
75*3b472e71SThomas Huth }
76*3b472e71SThomas Huth 
77*3b472e71SThomas Huth static void run_test(void)
78*3b472e71SThomas Huth {
79*3b472e71SThomas Huth     unsigned int i;
80*3b472e71SThomas Huth 
81*3b472e71SThomas Huth     while (qatomic_read(&n_ready_threads) != n_threads) {
82*3b472e71SThomas Huth         cpu_relax();
83*3b472e71SThomas Huth     }
84*3b472e71SThomas Huth 
85*3b472e71SThomas Huth     qatomic_set(&test_start, true);
86*3b472e71SThomas Huth     g_usleep(duration * G_USEC_PER_SEC);
87*3b472e71SThomas Huth     qatomic_set(&test_stop, true);
88*3b472e71SThomas Huth 
89*3b472e71SThomas Huth     for (i = 0; i < n_threads; i++) {
90*3b472e71SThomas Huth         qemu_thread_join(&threads[i]);
91*3b472e71SThomas Huth     }
92*3b472e71SThomas Huth }
93*3b472e71SThomas Huth 
94*3b472e71SThomas Huth static void create_threads(void)
95*3b472e71SThomas Huth {
96*3b472e71SThomas Huth     unsigned int i;
97*3b472e71SThomas Huth 
98*3b472e71SThomas Huth     threads = g_new(QemuThread, n_threads);
99*3b472e71SThomas Huth     th_info = g_new(struct thread_info, n_threads);
100*3b472e71SThomas Huth     counts = qemu_memalign(64, sizeof(*counts) * range);
101*3b472e71SThomas Huth     memset(counts, 0, sizeof(*counts) * range);
102*3b472e71SThomas Huth     for (i = 0; i < range; i++) {
103*3b472e71SThomas Huth         qemu_mutex_init(&counts[i].lock);
104*3b472e71SThomas Huth     }
105*3b472e71SThomas Huth 
106*3b472e71SThomas Huth     for (i = 0; i < n_threads; i++) {
107*3b472e71SThomas Huth         struct thread_info *info = &th_info[i];
108*3b472e71SThomas Huth 
109*3b472e71SThomas Huth         info->r = (i + 1) ^ time(NULL);
110*3b472e71SThomas Huth         qemu_thread_create(&threads[i], NULL, thread_func, info,
111*3b472e71SThomas Huth                            QEMU_THREAD_JOINABLE);
112*3b472e71SThomas Huth     }
113*3b472e71SThomas Huth }
114*3b472e71SThomas Huth 
115*3b472e71SThomas Huth static void pr_params(void)
116*3b472e71SThomas Huth {
117*3b472e71SThomas Huth     printf("Parameters:\n");
118*3b472e71SThomas Huth     printf(" # of threads:      %u\n", n_threads);
119*3b472e71SThomas Huth     printf(" duration:          %u\n", duration);
120*3b472e71SThomas Huth     printf(" ops' range:        %u\n", range);
121*3b472e71SThomas Huth }
122*3b472e71SThomas Huth 
123*3b472e71SThomas Huth static void pr_stats(void)
124*3b472e71SThomas Huth {
125*3b472e71SThomas Huth     unsigned long long val = 0;
126*3b472e71SThomas Huth     unsigned int i;
127*3b472e71SThomas Huth     double tx;
128*3b472e71SThomas Huth 
129*3b472e71SThomas Huth     for (i = 0; i < range; i++) {
130*3b472e71SThomas Huth         val += counts[i].val;
131*3b472e71SThomas Huth     }
132*3b472e71SThomas Huth     tx = val / duration / 1e6;
133*3b472e71SThomas Huth 
134*3b472e71SThomas Huth     printf("Results:\n");
135*3b472e71SThomas Huth     printf("Duration:            %u s\n", duration);
136*3b472e71SThomas Huth     printf(" Throughput:         %.2f Mops/s\n", tx);
137*3b472e71SThomas Huth     printf(" Throughput/thread:  %.2f Mops/s/thread\n", tx / n_threads);
138*3b472e71SThomas Huth }
139*3b472e71SThomas Huth 
140*3b472e71SThomas Huth static void parse_args(int argc, char *argv[])
141*3b472e71SThomas Huth {
142*3b472e71SThomas Huth     int c;
143*3b472e71SThomas Huth 
144*3b472e71SThomas Huth     for (;;) {
145*3b472e71SThomas Huth         c = getopt(argc, argv, "hd:n:mpr:");
146*3b472e71SThomas Huth         if (c < 0) {
147*3b472e71SThomas Huth             break;
148*3b472e71SThomas Huth         }
149*3b472e71SThomas Huth         switch (c) {
150*3b472e71SThomas Huth         case 'h':
151*3b472e71SThomas Huth             usage_complete(argv);
152*3b472e71SThomas Huth             exit(0);
153*3b472e71SThomas Huth         case 'd':
154*3b472e71SThomas Huth             duration = atoi(optarg);
155*3b472e71SThomas Huth             break;
156*3b472e71SThomas Huth         case 'n':
157*3b472e71SThomas Huth             n_threads = atoi(optarg);
158*3b472e71SThomas Huth             break;
159*3b472e71SThomas Huth         case 'm':
160*3b472e71SThomas Huth             use_mutex = true;
161*3b472e71SThomas Huth             break;
162*3b472e71SThomas Huth         case 'p':
163*3b472e71SThomas Huth             qsp_enable();
164*3b472e71SThomas Huth             break;
165*3b472e71SThomas Huth         case 'r':
166*3b472e71SThomas Huth             range = pow2ceil(atoi(optarg));
167*3b472e71SThomas Huth             break;
168*3b472e71SThomas Huth         }
169*3b472e71SThomas Huth     }
170*3b472e71SThomas Huth }
171*3b472e71SThomas Huth 
172*3b472e71SThomas Huth int main(int argc, char *argv[])
173*3b472e71SThomas Huth {
174*3b472e71SThomas Huth     parse_args(argc, argv);
175*3b472e71SThomas Huth     pr_params();
176*3b472e71SThomas Huth     create_threads();
177*3b472e71SThomas Huth     run_test();
178*3b472e71SThomas Huth     pr_stats();
179*3b472e71SThomas Huth     return 0;
180*3b472e71SThomas Huth }
181