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