forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core-lock.c
506 lines (425 loc) · 10.6 KB
/
core-lock.c
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
* Copyright (C) 2022 Colin Ian King.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "stress-ng.h"
#include "core-pthread.h"
#if defined(HAVE_LINUX_FUTEX_H)
#include <linux/futex.h>
#endif
#if defined(HAVE_SEMAPHORE_H)
#include <semaphore.h>
#endif
#if defined(HAVE_SEM_SYSV)
#include <sys/sem.h>
#endif
#define STRESS_LOCK_MAGIC (0x387cb9e5)
#if defined(HAVE_LIB_PTHREAD) && \
defined(HAVE_LIB_PTHREAD_SPINLOCK) && \
!defined(__DragonFly__) && \
!defined(__OpenBSD__)
#define LOCK_METHOD_PTHREAD_SPINLOCK (0x0001)
#else
#define LOCK_METHOD_PTHREAD_SPINLOCK (0)
#endif
#if defined(HAVE_LIB_PTHREAD) && \
defined(HAVE_PTHREAD_MUTEX_T) && \
defined(HAVE_PTHREAD_MUTEX_DESTROY) && \
defined(HAVE_PTHREAD_MUTEX_INIT)
#define LOCK_METHOD_PTHREAD_MUTEX (0x0002)
#else
#define LOCK_METHOD_PTHREAD_MUTEX (0)
#endif
#if defined(HAVE_LINUX_FUTEX_H) && \
defined(__NR_futex) && \
defined(FUTEX_LOCK_PI) && \
defined(FUTEX_UNLOCK_PI) && \
defined(HAVE_SYSCALL)
#define LOCK_METHOD_FUTEX (0x0004)
#else
#define LOCK_METHOD_FUTEX (0)
#endif
#if defined(HAVE_ATOMIC_TEST_AND_SET)
#define LOCK_METHOD_ATOMIC_SPINLOCK (0x0008)
#else
#define LOCK_METHOD_ATOMIC_SPINLOCK (0)
#endif
#if defined(HAVE_SEMAPHORE_H) && \
defined(HAVE_LIB_PTHREAD) && \
defined(HAVE_SEM_POSIX)
#define LOCK_METHOD_SEM_POSIX (0x0010)
#else
#define LOCK_METHOD_SEM_POSIX (0)
#endif
#if defined(HAVE_SEM_SYSV) && \
defined(HAVE_KEY_T)
#define LOCK_METHOD_SEM_SYSV (0x0020)
#else
#define LOCK_METHOD_SEM_SYSV (0)
#endif
#define LOCK_METHOD_ALL \
(LOCK_METHOD_ATOMIC_SPINLOCK | \
LOCK_METHOD_PTHREAD_SPINLOCK | \
LOCK_METHOD_PTHREAD_MUTEX | \
LOCK_METHOD_FUTEX | \
LOCK_METHOD_SEM_POSIX | \
LOCK_METHOD_SEM_SYSV)
typedef struct stress_lock {
uint32_t magic; /* Lock magic struct pattern */
int method; /* Lock method */
char *type; /* User readable lock type */
union {
#if LOCK_METHOD_ATOMIC_SPINLOCK != 0
bool flag; /* atomic spinlock flag */
#endif
#if LOCK_METHOD_PTHREAD_SPINLOCK != 0
pthread_spinlock_t pthread_spinlock; /* spinlock */
#endif
#if LOCK_METHOD_PTHREAD_MUTEX != 0
pthread_mutex_t pthread_mutex; /* mutex */
#endif
#if LOCK_METHOD_FUTEX != 0
int futex; /* futex */
#endif
#if LOCK_METHOD_SEM_POSIX != 0
sem_t sem_posix; /* POSIX semaphore */
#endif
#if LOCK_METHOD_SEM_SYSV != 0
int sem_id; /* SYS V semaphore */
#endif
} u;
int (*init)(struct stress_lock *lock);
int (*deinit)(struct stress_lock *lock);
int (*acquire)(struct stress_lock *lock);
int (*release)(struct stress_lock *lock);
} stress_lock_t;
/*
* Locking via atomic spinlock
*/
#if LOCK_METHOD_ATOMIC_SPINLOCK != 0
static inline bool test_and_set(bool *addr)
{
return __atomic_test_and_set((void *)addr, __ATOMIC_ACQ_REL);
}
static int stress_atomic_lock_init(stress_lock_t *lock)
{
lock->u.flag = 0;
return 0;
}
static int stress_atomic_lock_deinit(stress_lock_t *lock)
{
(void)lock;
return 0;
}
static int stress_atomic_lock_acquire(stress_lock_t *lock)
{
if (lock) {
while (test_and_set(&lock->u.flag) == true) {
#if defined(HAVE_ASM_X86_PAUSE)
__asm__ __volatile__("pause;\n" ::: "memory");
#else
shim_sched_yield();
#endif
if (!keep_stressing_flag()) {
errno = EAGAIN;
return -1;
}
}
return 0;
}
errno = EINVAL;
return -1;
}
static int stress_atomic_lock_release(stress_lock_t *lock)
{
lock->u.flag = false;
return 0;
}
/*
* Locking via pthread spinlock
*/
#elif LOCK_METHOD_PTHREAD_SPINLOCK != 0
static int stress_pthread_spinlock_init(stress_lock_t *lock)
{
int ret;
ret = pthread_spin_init(&lock->u.pthread_spinlock, PTHREAD_PROCESS_SHARED);
if (ret == 0)
return 0;
errno = ret;
return -1;
}
static int stress_pthread_spinlock_deinit(stress_lock_t *lock)
{
(void)lock;
return 0;
}
static int stress_pthread_spinlock_acquire(stress_lock_t *lock)
{
int ret;
ret = pthread_spin_lock(&lock->u.pthread_spinlock);
if (ret == 0)
return 0;
errno = ret;
return -1;
}
static int stress_pthread_spinlock_release(stress_lock_t *lock)
{
int ret;
ret = pthread_spin_unlock(&lock->u.pthread_spinlock);
if (ret == 0)
return 0;
errno = ret;
return -1;
}
/*
* Locking via pthread mutex
*/
#elif LOCK_METHOD_PTHREAD_MUTEX != 0
static int stress_pthread_mutex_init(stress_lock_t *lock)
{
int ret;
ret = pthread_mutex_init(&lock->u.pthread_mutex, NULL);
if (ret == 0)
return 0;
errno = ret;
return -1;
}
static int stress_pthread_mutex_deinit(stress_lock_t *lock)
{
(void)lock;
return 0;
}
static int stress_pthread_mutex_acquire(stress_lock_t *lock)
{
int ret;
ret = pthread_mutex_lock(&lock->u.pthread_mutex);
if (ret == 0)
return 0;
errno = ret;
return -1;
}
static int stress_pthread_mutex_release(stress_lock_t *lock)
{
int ret;
ret = pthread_mutex_unlock(&lock->u.pthread_mutex);
if (ret == 0)
return 0;
errno = ret;
return -1;
}
/*
* Locking via Linux futex system call API
*/
#elif LOCK_METHOD_FUTEX != 0
static int stress_futex_init(stress_lock_t *lock)
{
lock->u.futex = 0;
return 0;
}
static int stress_futex_deinit(stress_lock_t *lock)
{
(void)lock;
return 0;
}
static int stress_futex_acquire(stress_lock_t *lock)
{
return (int)syscall(__NR_futex, &lock->u.futex, FUTEX_LOCK_PI, 0, 0, 0, 0);
}
static int stress_futex_release(stress_lock_t *lock)
{
return (int)syscall(__NR_futex, &lock->u.futex, FUTEX_UNLOCK_PI, 0, 0, 0, 0);
}
/*
* Locking via POSIX semaphore
*/
#elif LOCK_METHOD_SEM_POSIX != 0
static int stress_sem_posix_init(stress_lock_t *lock)
{
return sem_init(&lock->u.sem_posix, 0, 1);
}
static int stress_sem_posix_deinit(stress_lock_t *lock)
{
(void)lock;
return 0;
}
static int stress_sem_posix_acquire(stress_lock_t *lock)
{
return sem_wait(&lock->u.sem_posix);
}
static int stress_sem_posix_release(stress_lock_t *lock)
{
return sem_post(&lock->u.sem_posix);
}
/*
* Locking via SYSV semaphore
*/
#elif LOCK_METHOD_SEM_SYSV != 0
static int stress_sem_sysv_init(stress_lock_t *lock)
{
int i;
for (i = 0; i < 256; i++) {
const key_t key_id = (key_t)stress_mwc16();
const int sem_id = semget(key_id, 1, IPC_CREAT | S_IRUSR | S_IWUSR);
if (sem_id >= 0) {
union semun {
int val;
} arg;
arg.val = 1;
if (semctl(sem_id, 0, SETVAL, arg) == 0) {
lock->u.sem_id = sem_id;
return 0;
}
break;
}
}
errno = ENOENT;
return -1;
}
static int stress_sem_sysv_deinit(stress_lock_t *lock)
{
return semctl(lock->u.sem_id, 0, IPC_RMID);
}
static int stress_sem_sysv_acquire(stress_lock_t *lock)
{
struct sembuf sops[1];
sops[0].sem_num = 0;
sops[0].sem_op = -1;
sops[0].sem_flg = SEM_UNDO;
return semop(lock->u.sem_id, sops, 1);
}
static int stress_sem_sysv_release(stress_lock_t *lock)
{
struct sembuf sops[1];
sops[0].sem_num = 0;
sops[0].sem_op = 1;
sops[0].sem_flg = SEM_UNDO;
return semop(lock->u.sem_id, sops, 1);
}
#endif
static bool stress_lock_valid(stress_lock_t *lock)
{
return (lock && (lock->magic == STRESS_LOCK_MAGIC));
}
/*
* stress_lock_create()
* generic lock creation and initialization
*/
void *stress_lock_create(void)
{
stress_lock_t *lock;
errno = ENOMEM;
if (LOCK_METHOD_ALL == (0))
goto no_locks;
lock = (stress_lock_t *)mmap(NULL, sizeof(*lock), PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (lock == MAP_FAILED)
return NULL;
/*
* Select locking implementation, try to use fast atomic
* spinlock, then pthread spinlock, then pthread mutex
* and fall back on Linux futex
*/
#if LOCK_METHOD_ATOMIC_SPINLOCK != 0
lock->init = stress_atomic_lock_init;
lock->deinit = stress_atomic_lock_deinit;
lock->acquire = stress_atomic_lock_acquire;
lock->release = stress_atomic_lock_release;
lock->type = "atomic-spinlock";
#elif LOCK_METHOD_PTHREAD_SPINLOCK != 0
lock->init = stress_pthread_spinlock_init;
lock->deinit = stress_pthread_spinlock_deinit;
lock->acquire = stress_pthread_spinlock_acquire;
lock->release = stress_pthread_spinlock_release;
lock->type = "pthread-spinlock";
#elif LOCK_METHOD_PTHREAD_MUTEX != 0
lock->init = stress_pthread_mutex_init;
lock->deinit = stress_pthread_mutex_deinit;
lock->acquire = stress_pthread_mutex_acquire;
lock->release = stress_pthread_mutex_release;
lock->type = "pthread-mutex";
#elif LOCK_METHOD_FUTEX != 0
lock->init = stress_futex_init;
lock->deinit = stress_futex_deinit;
lock->acquire = stress_futex_acquire;
lock->release = stress_futex_release;
lock->type = "futex";
#elif LOCK_METHOD_SEM_POSIX != 0
lock->init = stress_sem_posix_init;
lock->deinit = stress_sem_posix_deinit;
lock->acquire = stress_sem_posix_acquire;
lock->release = stress_sem_posix_release;
lock->type = "sem-posix";
#elif LOCK_METHOD_SEM_SYSV != 0
lock->init = stress_sem_sysv_init;
lock->deinit = stress_sem_sysv_deinit;
lock->acquire = stress_sem_sysv_acquire;
lock->release = stress_sem_sysv_release;
lock->type = "sem-posix";
#else
(void)munmap((void *)lock, sizeof(*lock));
goto no_locks;
#endif
lock->magic = STRESS_LOCK_MAGIC;
if (lock->init(lock) == 0)
return lock;
VOID_RET(int, stress_lock_destroy(lock));
return NULL;
no_locks:
/* Critical, we need to be able to lock somehow! */
pr_err("core-lock: no locking primitives available\n");
return NULL;
}
/*
* stress_lock_destroy()
* generic lock destruction
*/
int stress_lock_destroy(void *lock_handle)
{
stress_lock_t *lock = (stress_lock_t *)lock_handle;
if (stress_lock_valid(lock)) {
(void)lock->deinit(lock);
(void)memset(lock, 0, sizeof(*lock));
(void)munmap((void *)lock, sizeof(*lock));
return 0;
}
errno = EINVAL;
return -1;
}
/*
* stress_lock_acquire()
* generic lock acquire (lock)
*/
int stress_lock_acquire(void *lock_handle)
{
stress_lock_t *lock = (stress_lock_t *)lock_handle;
if (stress_lock_valid(lock))
return lock->acquire(lock);
errno = EINVAL;
return -1;
}
/*
* stress_lock_release()
* generic lock release (unlock)
*/
int stress_lock_release(void *lock_handle)
{
stress_lock_t *lock = (stress_lock_t *)lock_handle;
if (stress_lock_valid(lock))
return lock->release(lock);
errno = EINVAL;
return -1;
}