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