forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_lock.c
More file actions
361 lines (298 loc) · 9.42 KB
/
test_lock.c
File metadata and controls
361 lines (298 loc) · 9.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// C Extension module to test pycore_lock.h API
#include "parts.h"
#include "pycore_lock.h"
#include "clinic/test_lock.c.h"
#ifdef MS_WINDOWS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <unistd.h> // usleep()
#endif
/*[clinic input]
module _testinternalcapi
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=7bb583d8c9eb9a78]*/
static void
pysleep(int ms)
{
#ifdef MS_WINDOWS
Sleep(ms);
#else
usleep(ms * 1000);
#endif
}
static PyObject *
test_lock_basic(PyObject *self, PyObject *obj)
{
PyMutex m = (PyMutex){0};
// uncontended lock and unlock
PyMutex_Lock(&m);
assert(m.v == 1);
PyMutex_Unlock(&m);
assert(m.v == 0);
Py_RETURN_NONE;
}
struct test_lock2_data {
PyMutex m;
PyEvent done;
int started;
};
static void
lock_thread(void *arg)
{
struct test_lock2_data *test_data = arg;
PyMutex *m = &test_data->m;
_Py_atomic_store_int(&test_data->started, 1);
PyMutex_Lock(m);
assert(m->v == 1);
PyMutex_Unlock(m);
assert(m->v == 0);
_PyEvent_Notify(&test_data->done);
}
static PyObject *
test_lock_two_threads(PyObject *self, PyObject *obj)
{
// lock attempt by two threads
struct test_lock2_data test_data;
memset(&test_data, 0, sizeof(test_data));
PyMutex_Lock(&test_data.m);
assert(test_data.m.v == 1);
PyThread_start_new_thread(lock_thread, &test_data);
// wait up to two seconds for the lock_thread to attempt to lock "m"
int iters = 0;
uint8_t v;
do {
pysleep(10); // allow some time for the other thread to try to lock
v = _Py_atomic_load_uint8_relaxed(&test_data.m.v);
assert(v == 1 || v == 3);
iters++;
} while (v != 3 && iters < 200);
// both the "locked" and the "has parked" bits should be set
assert(test_data.m.v == 3);
PyMutex_Unlock(&test_data.m);
PyEvent_Wait(&test_data.done);
assert(test_data.m.v == 0);
Py_RETURN_NONE;
}
#define COUNTER_THREADS 5
#define COUNTER_ITERS 10000
struct test_data_counter {
PyMutex m;
Py_ssize_t counter;
};
struct thread_data_counter {
struct test_data_counter *test_data;
PyEvent done_event;
};
static void
counter_thread(void *arg)
{
struct thread_data_counter *thread_data = arg;
struct test_data_counter *test_data = thread_data->test_data;
for (Py_ssize_t i = 0; i < COUNTER_ITERS; i++) {
PyMutex_Lock(&test_data->m);
test_data->counter++;
PyMutex_Unlock(&test_data->m);
}
_PyEvent_Notify(&thread_data->done_event);
}
static PyObject *
test_lock_counter(PyObject *self, PyObject *obj)
{
// Test with rapidly locking and unlocking mutex
struct test_data_counter test_data;
memset(&test_data, 0, sizeof(test_data));
struct thread_data_counter thread_data[COUNTER_THREADS];
memset(&thread_data, 0, sizeof(thread_data));
for (Py_ssize_t i = 0; i < COUNTER_THREADS; i++) {
thread_data[i].test_data = &test_data;
PyThread_start_new_thread(counter_thread, &thread_data[i]);
}
for (Py_ssize_t i = 0; i < COUNTER_THREADS; i++) {
PyEvent_Wait(&thread_data[i].done_event);
}
assert(test_data.counter == COUNTER_THREADS * COUNTER_ITERS);
Py_RETURN_NONE;
}
#define SLOW_COUNTER_ITERS 100
static void
slow_counter_thread(void *arg)
{
struct thread_data_counter *thread_data = arg;
struct test_data_counter *test_data = thread_data->test_data;
for (Py_ssize_t i = 0; i < SLOW_COUNTER_ITERS; i++) {
PyMutex_Lock(&test_data->m);
if (i % 7 == 0) {
pysleep(2);
}
test_data->counter++;
PyMutex_Unlock(&test_data->m);
}
_PyEvent_Notify(&thread_data->done_event);
}
static PyObject *
test_lock_counter_slow(PyObject *self, PyObject *obj)
{
// Test lock/unlock with occasional "long" critical section, which will
// trigger handoff of the lock.
struct test_data_counter test_data;
memset(&test_data, 0, sizeof(test_data));
struct thread_data_counter thread_data[COUNTER_THREADS];
memset(&thread_data, 0, sizeof(thread_data));
for (Py_ssize_t i = 0; i < COUNTER_THREADS; i++) {
thread_data[i].test_data = &test_data;
PyThread_start_new_thread(slow_counter_thread, &thread_data[i]);
}
for (Py_ssize_t i = 0; i < COUNTER_THREADS; i++) {
PyEvent_Wait(&thread_data[i].done_event);
}
assert(test_data.counter == COUNTER_THREADS * SLOW_COUNTER_ITERS);
Py_RETURN_NONE;
}
struct bench_data_locks {
int stop;
int use_pymutex;
int critical_section_length;
char padding[200];
PyThread_type_lock lock;
PyMutex m;
double value;
Py_ssize_t total_iters;
};
struct bench_thread_data {
struct bench_data_locks *bench_data;
Py_ssize_t iters;
PyEvent done;
};
static void
thread_benchmark_locks(void *arg)
{
struct bench_thread_data *thread_data = arg;
struct bench_data_locks *bench_data = thread_data->bench_data;
int use_pymutex = bench_data->use_pymutex;
int critical_section_length = bench_data->critical_section_length;
double my_value = 1.0;
Py_ssize_t iters = 0;
while (!_Py_atomic_load_int_relaxed(&bench_data->stop)) {
if (use_pymutex) {
PyMutex_Lock(&bench_data->m);
for (int i = 0; i < critical_section_length; i++) {
bench_data->value += my_value;
my_value = bench_data->value;
}
PyMutex_Unlock(&bench_data->m);
}
else {
PyThread_acquire_lock(bench_data->lock, 1);
for (int i = 0; i < critical_section_length; i++) {
bench_data->value += my_value;
my_value = bench_data->value;
}
PyThread_release_lock(bench_data->lock);
}
iters++;
}
thread_data->iters = iters;
_Py_atomic_add_ssize(&bench_data->total_iters, iters);
_PyEvent_Notify(&thread_data->done);
}
/*[clinic input]
_testinternalcapi.benchmark_locks
num_threads: Py_ssize_t
use_pymutex: bool = True
critical_section_length: int = 1
time_ms: int = 1000
/
[clinic start generated code]*/
static PyObject *
_testinternalcapi_benchmark_locks_impl(PyObject *module,
Py_ssize_t num_threads,
int use_pymutex,
int critical_section_length,
int time_ms)
/*[clinic end generated code: output=381df8d7e9a74f18 input=f3aeaf688738c121]*/
{
// Run from Tools/lockbench/lockbench.py
// Based on the WebKit lock benchmarks:
// https://github.com/WebKit/WebKit/blob/main/Source/WTF/benchmarks/LockSpeedTest.cpp
// See also https://webkit.org/blog/6161/locking-in-webkit/
PyObject *thread_iters = NULL;
PyObject *res = NULL;
struct bench_data_locks bench_data;
memset(&bench_data, 0, sizeof(bench_data));
bench_data.use_pymutex = use_pymutex;
bench_data.critical_section_length = critical_section_length;
bench_data.lock = PyThread_allocate_lock();
if (bench_data.lock == NULL) {
return PyErr_NoMemory();
}
struct bench_thread_data *thread_data = NULL;
thread_data = PyMem_Calloc(num_threads, sizeof(*thread_data));
if (thread_data == NULL) {
PyErr_NoMemory();
goto exit;
}
thread_iters = PyList_New(num_threads);
if (thread_iters == NULL) {
goto exit;
}
_PyTime_t start = _PyTime_GetMonotonicClock();
for (Py_ssize_t i = 0; i < num_threads; i++) {
thread_data[i].bench_data = &bench_data;
PyThread_start_new_thread(thread_benchmark_locks, &thread_data[i]);
}
// Let the threads run for `time_ms` milliseconds
pysleep(time_ms);
_Py_atomic_store_int(&bench_data.stop, 1);
// Wait for the threads to finish
for (Py_ssize_t i = 0; i < num_threads; i++) {
PyEvent_Wait(&thread_data[i].done);
}
Py_ssize_t total_iters = bench_data.total_iters;
_PyTime_t end = _PyTime_GetMonotonicClock();
// Return the total number of acquisitions and the number of acquisitions
// for each thread.
for (Py_ssize_t i = 0; i < num_threads; i++) {
PyObject *iter = PyLong_FromSsize_t(thread_data[i].iters);
if (iter == NULL) {
goto exit;
}
PyList_SET_ITEM(thread_iters, i, iter);
}
double rate = total_iters * 1000000000.0 / (end - start);
res = Py_BuildValue("(dO)", rate, thread_iters);
exit:
PyThread_free_lock(bench_data.lock);
PyMem_Free(thread_data);
Py_XDECREF(thread_iters);
return res;
}
static PyObject *
test_lock_benchmark(PyObject *module, PyObject *obj)
{
// Just make sure the benchmark runs without crashing
PyObject *res = _testinternalcapi_benchmark_locks_impl(
module, 1, 1, 1, 100);
if (res == NULL) {
return NULL;
}
Py_DECREF(res);
Py_RETURN_NONE;
}
static PyMethodDef test_methods[] = {
{"test_lock_basic", test_lock_basic, METH_NOARGS},
{"test_lock_two_threads", test_lock_two_threads, METH_NOARGS},
{"test_lock_counter", test_lock_counter, METH_NOARGS},
{"test_lock_counter_slow", test_lock_counter_slow, METH_NOARGS},
_TESTINTERNALCAPI_BENCHMARK_LOCKS_METHODDEF
{"test_lock_benchmark", test_lock_benchmark, METH_NOARGS},
{NULL, NULL} /* sentinel */
};
int
_PyTestInternalCapi_Init_Lock(PyObject *mod)
{
if (PyModule_AddFunctions(mod, test_methods) < 0) {
return -1;
}
return 0;
}