forked from npiggin/lockstorm
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlockstorm.c
More file actions
380 lines (312 loc) · 8.65 KB
/
lockstorm.c
File metadata and controls
380 lines (312 loc) · 8.65 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* Copyright 2017-2020 Anton Blanchard, IBM Corporation <anton@linux.ibm.com>
*
* 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.
*/
#define pr_fmt(fmt) "lockstorm: " fmt
#include <linux/limits.h>
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/slab.h>
static long timeout = 10;
module_param(timeout, long, S_IRUGO);
MODULE_PARM_DESC(timeout, "Timeout in seconds (default = 10)");
static bool use_atomics = false;
module_param(use_atomics , bool, S_IRUGO);
MODULE_PARM_DESC(use_atomics, "Use atomic ops rather than spinlock (default false)");
static char cpulist_str[256];
module_param_string(cpulist, cpulist_str, sizeof(cpulist_str), 0644);
MODULE_PARM_DESC(cpulist, "List of CPUs (default all)");
static unsigned int num_cpus;
static atomic_t running;
static DECLARE_COMPLETION(lockstorm_done);
struct obj {
spinlock_t spinlock;
unsigned int lock;
unsigned int taken;
};
// static __cacheline_aligned struct obj obj;
static int gmax_starve[NR_CPUS];
static int gmin_starve[NR_CPUS];
static int gmax_sequential[NR_CPUS];
static u64 gmin_wait[NR_CPUS];
static u64 gmax_wait[NR_CPUS];
static u64 gnr_locks[NR_CPUS];
static __inline__ unsigned int incret(unsigned int *v, int eh)
{
unsigned int t;
__asm__ __volatile__(
"1: lwarx %0,0,%2,%3 # incret\n"
" addic %0,%0,1\n"
" stwcx. %0,0,%2\n"
" bne- 1b"
: "=&r" (t), "+m" (*v)
: "r" (v), "i" (eh)
: "cc", "xer");
return t;
}
static __inline__ bool tas_lock(unsigned int *v, int eh)
{
unsigned int t;
__asm__ __volatile__(
"1: lwarx %0,0,%2,%4 # incret\n"
" cmpw %0,0\n"
" bne- 2f\n"
" stwcx. %3,0,%2\n"
" bne- 1b\n"
" lwsync\n"
"2:"
: "=&r" (t), "+m" (*v)
: "r" (v), "r"(1), "i" (eh)
: "cc", "xer", "memory");
return t;
}
static __inline__ void clr_unlock(unsigned int *v)
{
smp_store_release(v, 0);
}
static void spin_lock2(struct obj *obj)
{
for (;;) {
if (tas_lock(&obj->lock, 1) == 0)
break;
while (obj->lock != 0)
cpu_relax();
}
}
static void spin_unlock2(struct obj *obj)
{
clr_unlock(&obj->lock);
}
static int lockstorm_thread(void *data)
{
struct obj *obj = data;
unsigned long t;
u64 iters = 0;
ktime_t start, end;
ktime_t lock, granted;
u64 max_wait = 0;
u64 min_wait = U64_MAX;
int prev_taken = -1;
int max_taken = 0;
int min_taken = INT_MAX;
int max_sequential = 0;
int cur_sequential = 0;
int taken, tdelta;
atomic_inc(&running);
while (atomic_read(&running) < num_cpus) {
if (need_resched())
schedule();
}
t = jiffies + timeout*HZ;
start = ktime_get();
iters = 0;
prev_taken = 0;
while (time_before(jiffies, t)) {
lock = ktime_get();
spin_lock(&obj->spinlock);
taken = obj->taken++;
if (iters == 0 || atomic_read(&running) < num_cpus)
goto next;
granted = ktime_get();
if (ktime_sub_ns(granted, lock) < min_wait)
min_wait = ktime_sub_ns(granted, lock);
if (ktime_sub_ns(granted, lock) > max_wait)
max_wait = ktime_sub_ns(granted, lock);
if (taken == prev_taken + 1)
cur_sequential++;
else
cur_sequential = 0;
if (cur_sequential > max_sequential)
max_sequential = cur_sequential;
tdelta = taken - prev_taken;
if (tdelta < min_taken)
min_taken = tdelta;
if (tdelta > max_taken)
max_taken = tdelta;
next:
spin_unlock(&obj->spinlock);
prev_taken = taken;
iters++;
if (atomic_read(&running) < num_cpus)
break;
}
end = ktime_get();
gmax_starve[smp_processor_id()] = max_taken;
gmin_starve[smp_processor_id()] = min_taken;
gmax_sequential[smp_processor_id()] = max_sequential;
gmax_wait[smp_processor_id()] = max_wait;
gmin_wait[smp_processor_id()] = min_wait;
gnr_locks[smp_processor_id()] = iters;
if (atomic_dec_and_test(&running))
complete(&lockstorm_done);
return 0;
}
static int atomicstorm_thread(void *data)
{
struct obj *obj = data;
unsigned long t;
u64 iters = 0;
ktime_t start, end;
ktime_t lock, granted;
u64 max_wait = 0;
u64 min_wait = U64_MAX;
int prev_taken = -1;
int max_taken = 0;
int min_taken = INT_MAX;
int max_sequential = 0;
int cur_sequential = 0;
int taken, tdelta;
atomic_inc(&running);
while (atomic_read(&running) < num_cpus) {
if (need_resched())
schedule();
}
t = jiffies + timeout*HZ;
start = ktime_get();
iters = 0;
prev_taken = 0;
while (time_before(jiffies, t)) {
lock = ktime_get();
taken = incret(&obj->taken, 0);
if (iters == 0 || atomic_read(&running) < num_cpus)
goto next;
granted = ktime_get();
if (ktime_sub_ns(granted, lock) < min_wait)
min_wait = ktime_sub_ns(granted, lock);
if (ktime_sub_ns(granted, lock) > max_wait)
max_wait = ktime_sub_ns(granted, lock);
if (taken == prev_taken + 1)
cur_sequential++;
else
cur_sequential = 0;
if (cur_sequential > max_sequential)
max_sequential = cur_sequential;
tdelta = taken - prev_taken;
if (tdelta < min_taken)
min_taken = tdelta;
if (tdelta > max_taken)
max_taken = tdelta;
next:
prev_taken = taken;
iters++;
if (atomic_read(&running) < num_cpus)
break;
}
end = ktime_get();
gmax_starve[smp_processor_id()] = max_taken;
gmin_starve[smp_processor_id()] = min_taken;
gmax_sequential[smp_processor_id()] = max_sequential;
gmax_wait[smp_processor_id()] = max_wait;
gmin_wait[smp_processor_id()] = min_wait;
gnr_locks[smp_processor_id()] = iters;
if (atomic_dec_and_test(&running))
complete(&lockstorm_done);
return 0;
}
static int __init lockstorm_init(void)
{
struct obj *obj;
unsigned long cpu;
cpumask_var_t mask;
int ret = 0;
obj = kmalloc_node(sizeof(struct obj), GFP_KERNEL, 0);
if (!obj)
return -ENOMEM;
spin_lock_init(&obj->spinlock);
obj->lock = 0;
obj->taken = 0;
init_completion(&lockstorm_done);
if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
kfree(obj);
return -ENOMEM;
}
if (cpulist_str[0]) {
ret = cpulist_parse(cpulist_str, mask);
if (ret)
goto out_free;
if (!cpumask_subset(mask, cpu_online_mask)) {
pr_err("Invalid CPU list: %s\n", cpulist_str);
ret = -EINVAL;
goto out_free;
}
} else {
cpumask_copy(mask, cpu_online_mask);
}
num_cpus = cpumask_weight(mask);
for_each_cpu(cpu, mask) {
struct task_struct *p;
if (!use_atomics)
p = kthread_create(lockstorm_thread, obj,
"lockstorm/%lu", cpu);
else
p = kthread_create(atomicstorm_thread, obj,
"lockstorm/%lu", cpu);
if (IS_ERR(p)) {
pr_err("kthread_create on CPU %lu failed\n", cpu);
atomic_inc(&running);
} else {
kthread_bind(p, cpu);
wake_up_process(p);
}
}
wait_for_completion(&lockstorm_done);
if (1) {
int max_max_starve = 0;
int min_max_starve = INT_MAX;
int max_min_starve = 0;
int min_min_starve = INT_MAX;
int max_max_sequential = 0;
int min_max_sequential = INT_MAX;
u64 max_max_wait = 0;
u64 min_max_wait = U64_MAX;
u64 max_min_wait = 0;
u64 min_min_wait = U64_MAX;
u64 max_nr_locks = 0;
u64 min_nr_locks = U64_MAX;
u64 nr_locks = 0;
for_each_cpu(cpu, mask) {
if (gmax_starve[cpu] > max_max_starve)
max_max_starve = gmax_starve[cpu];
if (gmax_starve[cpu] < min_max_starve)
min_max_starve = gmax_starve[cpu];
if (gmin_starve[cpu] > max_min_starve)
max_min_starve = gmin_starve[cpu];
if (gmin_starve[cpu] < min_min_starve)
min_min_starve = gmin_starve[cpu];
if (gmax_sequential[cpu] > max_max_sequential)
max_max_sequential = gmax_sequential[cpu];
if (gmax_sequential[cpu] < min_max_sequential)
min_max_sequential = gmax_sequential[cpu];
if (gmax_wait[cpu] > max_max_wait)
max_max_wait = gmax_wait[cpu];
if (gmax_wait[cpu] < min_max_wait)
min_max_wait = gmax_wait[cpu];
if (gmin_wait[cpu] > max_min_wait)
max_min_wait = gmin_wait[cpu];
if (gmin_wait[cpu] < min_min_wait)
min_min_wait = gmin_wait[cpu];
nr_locks += gnr_locks[cpu];
if (gnr_locks[cpu] > max_nr_locks)
max_nr_locks = gnr_locks[cpu];
if (gnr_locks[cpu] < min_nr_locks)
min_nr_locks = gnr_locks[cpu];
}
pr_notice("%s iterations:%llu (max:%llu min:%llu) max wait:%llu (min:%llu) min wait:%llu (max:%llu) max starve:%d (min:%d) min starve:%d (max:%d) max sequential:%d (min:%d)\n", use_atomics ? " atomic" : "spinlock", nr_locks, max_nr_locks, min_nr_locks, max_max_wait, min_max_wait, min_min_wait, max_min_wait, max_max_starve, min_max_starve, min_min_starve, max_min_starve, max_max_sequential, min_max_sequential);
}
out_free:
kfree(obj);
free_cpumask_var(mask);
return ret ? ret : -EAGAIN;
}
static void __exit lockstorm_exit(void)
{
}
module_init(lockstorm_init)
module_exit(lockstorm_exit)
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Anton Blanchard");
MODULE_DESCRIPTION("Lock testing");