Skip to content

Commit f04f34d

Browse files
committed
tests/atomic_add-bench: add -m option to use mutexes
This allows us to use atomic-add-bench as a microbenchmark for evaluating qemu_mutex_lock's performance. Signed-off-by: Emilio G. Cota <cota@braap.org>
1 parent 879e6ca commit f04f34d

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

tests/atomic_add-bench.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ struct thread_info {
88
} QEMU_ALIGNED(64);
99

1010
struct count {
11+
QemuMutex lock;
1112
unsigned long val;
1213
} QEMU_ALIGNED(64);
1314

@@ -18,11 +19,13 @@ static unsigned int n_ready_threads;
1819
static struct count *counts;
1920
static unsigned int duration = 1;
2021
static unsigned int range = 1024;
22+
static bool use_mutex;
2123
static bool test_start;
2224
static bool test_stop;
2325

2426
static const char commands_string[] =
2527
" -n = number of threads\n"
28+
" -m = use mutexes instead of atomic increments\n"
2629
" -d = duration in seconds\n"
2730
" -r = range (will be rounded up to pow2)";
2831

@@ -59,7 +62,13 @@ static void *thread_func(void *arg)
5962

6063
info->r = xorshift64star(info->r);
6164
index = info->r & (range - 1);
62-
atomic_inc(&counts[index].val);
65+
if (use_mutex) {
66+
qemu_mutex_lock(&counts[index].lock);
67+
counts[index].val += 1;
68+
qemu_mutex_unlock(&counts[index].lock);
69+
} else {
70+
atomic_inc(&counts[index].val);
71+
}
6372
}
6473
return NULL;
6574
}
@@ -91,6 +100,9 @@ static void create_threads(void)
91100
th_info = g_new(struct thread_info, n_threads);
92101
counts = qemu_memalign(64, sizeof(*counts) * range);
93102
memset(counts, 0, sizeof(*counts) * range);
103+
for (i = 0; i < range; i++) {
104+
qemu_mutex_init(&counts[i].lock);
105+
}
94106

95107
for (i = 0; i < n_threads; i++) {
96108
struct thread_info *info = &th_info[i];
@@ -131,7 +143,7 @@ static void parse_args(int argc, char *argv[])
131143
int c;
132144

133145
for (;;) {
134-
c = getopt(argc, argv, "hd:n:r:");
146+
c = getopt(argc, argv, "hd:n:mr:");
135147
if (c < 0) {
136148
break;
137149
}
@@ -145,6 +157,9 @@ static void parse_args(int argc, char *argv[])
145157
case 'n':
146158
n_threads = atoi(optarg);
147159
break;
160+
case 'm':
161+
use_mutex = true;
162+
break;
148163
case 'r':
149164
range = pow2ceil(atoi(optarg));
150165
break;

0 commit comments

Comments
 (0)