5 #include <linux/module.h>
6 #include <linux/proc_fs.h>
7 #include <linux/sched.h>
8 #include <linux/timex.h>
9 #include <linux/fair-rwlock.h>
10 #include <linux/kthread.h>
11 #include <linux/delay.h>
12 #include <linux/hardirq.h>
13 #include <linux/module.h>
14 #include <linux/percpu.h>
15 #include <asm/ptrace.h>
17 #if (NR_CPUS > 64 && (BITS_PER_LONG == 32 || NR_CPUS > 32768))
18 #error "fair rwlock needs more bits per long to deal with that many CPUs"
21 /* Test duration, in seconds */
22 #define TEST_DURATION 60
24 #define THREAD_ROFFSET 1UL
25 #define THREAD_RMASK ((NR_CPUS - 1) * THREAD_ROFFSET)
26 #define SOFTIRQ_ROFFSET (THREAD_RMASK + 1)
27 #define SOFTIRQ_RMASK ((NR_CPUS - 1) * SOFTIRQ_ROFFSET)
28 #define HARDIRQ_ROFFSET ((SOFTIRQ_RMASK | THREAD_RMASK) + 1)
29 #define HARDIRQ_RMASK ((NR_CPUS - 1) * HARDIRQ_ROFFSET)
31 #define SUBSCRIBERS_WOFFSET \
32 ((HARDIRQ_RMASK | SOFTIRQ_RMASK | THREAD_RMASK) + 1)
33 #define SUBSCRIBERS_WMASK \
34 ((NR_CPUS - 1) * SUBSCRIBERS_WOFFSET)
35 #define WRITER_MUTEX \
36 ((SUBSCRIBERS_WMASK | HARDIRQ_RMASK | SOFTIRQ_RMASK | THREAD_RMASK) + 1)
37 #define SOFTIRQ_WMASK (WRITER_MUTEX << 1)
38 #define SOFTIRQ_WOFFSET SOFTIRQ_WMASK
39 #define HARDIRQ_WMASK (SOFTIRQ_WMASK << 1)
40 #define HARDIRQ_WOFFSET HARDIRQ_WMASK
44 #define NR_TRYLOCK_WRITERS 2
46 #define NR_TRYLOCK_READERS 2
47 #define NR_INTERRUPT_READERS 1
48 #define NR_TRYLOCK_INTERRUPT_READERS 1
50 /* Writer iteration delay, in ms. 0 for busy loop. */
51 #define WRITER_DELAY 1
53 static int var
[NR_VARS
];
54 static struct task_struct
*reader_threads
[NR_READERS
];
55 static struct task_struct
*trylock_reader_threads
[NR_TRYLOCK_READERS
];
56 static struct task_struct
*writer_threads
[NR_WRITERS
];
57 static struct task_struct
*trylock_writer_threads
[NR_TRYLOCK_WRITERS
];
58 static struct task_struct
*interrupt_reader
;
59 static struct task_struct
*trylock_interrupt_reader
;
61 static struct fair_rwlock frwlock
= {
62 .value
= ATOMIC_LONG_INIT(0),
65 struct proc_dir_entry
*pentry
= NULL
;
67 static int reader_thread(void *data
)
71 unsigned long iter
= 0;
72 cycles_t time1
, time2
, delaymax
= 0;
74 printk("reader_thread/%lu runnning\n", (unsigned long)data
);
77 preempt_disable(); /* for get_cycles accuracy */
79 fair_read_lock(&frwlock
);
81 delaymax
= max(delaymax
, time2
- time1
);
83 for (i
= 1; i
< NR_VARS
; i
++) {
87 "Unequal cur %d/prev %d at i %d, iter %lu "
88 "in thread\n", cur
, prev
, i
, iter
);
90 fair_read_unlock(&frwlock
);
91 preempt_enable(); /* for get_cycles accuracy */
93 } while (!kthread_should_stop());
94 printk("reader_thread/%lu iterations : %lu, "
95 "max contention %llu cycles\n",
96 (unsigned long)data
, iter
, delaymax
);
100 static int trylock_reader_thread(void *data
)
104 unsigned long iter
= 0, success_iter
= 0;
106 printk("trylock_reader_thread/%lu runnning\n", (unsigned long)data
);
108 while (!fair_read_trylock(&frwlock
))
112 for (i
= 1; i
< NR_VARS
; i
++) {
116 "Unequal cur %d/prev %d at i %d, iter %lu "
117 "in thread\n", cur
, prev
, i
, iter
);
119 fair_read_unlock(&frwlock
);
121 } while (!kthread_should_stop());
122 printk("trylock_reader_thread/%lu iterations : %lu, "
123 "successful iterations : %lu\n",
124 (unsigned long)data
, iter
, success_iter
);
128 DEFINE_PER_CPU(cycles_t
, int_delaymax
);
130 static void interrupt_reader_ipi(void *data
)
134 cycles_t time1
, time2
;
138 * Skip the ipi caller, not in irq context.
143 delaymax
= &per_cpu(int_delaymax
, smp_processor_id());
144 time1
= get_cycles();
145 fair_read_lock(&frwlock
);
146 time2
= get_cycles();
147 *delaymax
= max(*delaymax
, time2
- time1
);
149 for (i
= 1; i
< NR_VARS
; i
++) {
153 "Unequal cur %d/prev %d at i %d in interrupt\n",
156 fair_read_unlock(&frwlock
);
159 DEFINE_PER_CPU(unsigned long, trylock_int_iter
);
160 DEFINE_PER_CPU(unsigned long, trylock_int_success
);
162 static void trylock_interrupt_reader_ipi(void *data
)
168 * Skip the ipi caller, not in irq context.
173 per_cpu(trylock_int_iter
, smp_processor_id())++;
174 while (!fair_read_trylock(&frwlock
))
175 per_cpu(trylock_int_iter
, smp_processor_id())++;
176 per_cpu(trylock_int_success
, smp_processor_id())++;
178 for (i
= 1; i
< NR_VARS
; i
++) {
182 "Unequal cur %d/prev %d at i %d in interrupt\n",
185 fair_read_unlock(&frwlock
);
189 static int interrupt_reader_thread(void *data
)
191 unsigned long iter
= 0;
196 on_each_cpu(interrupt_reader_ipi
, NULL
, 0);
198 } while (!kthread_should_stop());
199 printk("interrupt_reader_thread/%lu iterations : %lu\n",
200 (unsigned long)data
, iter
);
201 for_each_online_cpu(i
) {
202 printk("interrupt readers on CPU %i, "
203 "max contention : %llu cycles\n",
204 i
, per_cpu(int_delaymax
, i
));
209 static int trylock_interrupt_reader_thread(void *data
)
211 unsigned long iter
= 0;
216 on_each_cpu(trylock_interrupt_reader_ipi
, NULL
, 0);
218 } while (!kthread_should_stop());
219 printk("trylock_interrupt_reader_thread/%lu iterations : %lu\n",
220 (unsigned long)data
, iter
);
221 for_each_online_cpu(i
) {
222 printk("trylock interrupt readers on CPU %i, "
224 "successful iterations : %lu\n",
225 i
, per_cpu(trylock_int_iter
, i
),
226 per_cpu(trylock_int_success
, i
));
231 static int writer_thread(void *data
)
235 unsigned long iter
= 0;
236 cycles_t time1
, time2
, delaymax
= 0;
238 printk("writer_thread/%lu runnning\n", (unsigned long)data
);
241 preempt_disable(); /* for get_cycles accuracy */
242 time1
= get_cycles();
243 fair_write_lock_irq(&frwlock
);
244 //fair_write_lock(&frwlock);
245 time2
= get_cycles();
246 delaymax
= max(delaymax
, time2
- time1
);
247 new = (int)get_cycles();
248 for (i
= 0; i
< NR_VARS
; i
++) {
251 //fair_write_unlock(&frwlock);
252 fair_write_unlock_irq(&frwlock
);
253 preempt_enable(); /* for get_cycles accuracy */
254 if (WRITER_DELAY
> 0)
255 msleep(WRITER_DELAY
);
256 } while (!kthread_should_stop());
257 printk("writer_thread/%lu iterations : %lu, "
258 "max contention %llu cycles\n",
259 (unsigned long)data
, iter
, delaymax
);
263 static int trylock_writer_thread(void *data
)
267 unsigned long iter
= 0, success
= 0;
269 printk("trylock_writer_thread/%lu runnning\n", (unsigned long)data
);
271 fair_write_subscribe(&frwlock
);
272 while (!fair_write_trylock_subscribed_irq(&frwlock
)) {
277 //fair_write_lock(&frwlock);
278 new = (int)get_cycles();
279 for (i
= 0; i
< NR_VARS
; i
++) {
282 //fair_write_unlock(&frwlock);
283 fair_write_unlock_irq(&frwlock
);
284 if (WRITER_DELAY
> 0)
285 msleep(WRITER_DELAY
);
286 } while (!kthread_should_stop());
287 printk("trylock_writer_thread/%lu iterations : %lu, "
288 "successful iterations : %lu\n",
289 (unsigned long)data
, iter
, success
);
293 static void fair_rwlock_create(void)
297 for (i
= 0; i
< NR_READERS
; i
++) {
298 printk("starting reader thread %lu\n", i
);
299 reader_threads
[i
] = kthread_run(reader_thread
, (void *)i
,
301 BUG_ON(!reader_threads
[i
]);
304 for (i
= 0; i
< NR_TRYLOCK_READERS
; i
++) {
305 printk("starting trylock reader thread %lu\n", i
);
306 trylock_reader_threads
[i
] = kthread_run(trylock_reader_thread
,
307 (void *)i
, "frwlock_trylock_reader");
308 BUG_ON(!trylock_reader_threads
[i
]);
312 printk("starting interrupt reader %lu\n", i
);
313 interrupt_reader
= kthread_run(interrupt_reader_thread
, NULL
,
314 "frwlock_interrupt_reader");
315 printk("starting trylock interrupt reader %lu\n", i
);
316 trylock_interrupt_reader
= kthread_run(trylock_interrupt_reader_thread
,
317 NULL
, "frwlock_trylock_interrupt_reader");
319 for (i
= 0; i
< NR_WRITERS
; i
++) {
320 printk("starting writer thread %lu\n", i
);
321 writer_threads
[i
] = kthread_run(writer_thread
, (void *)i
,
323 BUG_ON(!writer_threads
[i
]);
325 for (i
= 0; i
< NR_TRYLOCK_WRITERS
; i
++) {
326 printk("starting trylock writer thread %lu\n", i
);
327 trylock_writer_threads
[i
] = kthread_run(trylock_writer_thread
,
328 (void *)i
, "frwlock_trylock_writer");
329 BUG_ON(!trylock_writer_threads
[i
]);
334 static void fair_rwlock_stop(void)
338 for (i
= 0; i
< NR_WRITERS
; i
++)
339 kthread_stop(writer_threads
[i
]);
340 for (i
= 0; i
< NR_TRYLOCK_WRITERS
; i
++)
341 kthread_stop(trylock_writer_threads
[i
]);
342 for (i
= 0; i
< NR_READERS
; i
++)
343 kthread_stop(reader_threads
[i
]);
344 for (i
= 0; i
< NR_TRYLOCK_READERS
; i
++)
345 kthread_stop(trylock_reader_threads
[i
]);
346 kthread_stop(interrupt_reader
);
347 kthread_stop(trylock_interrupt_reader
);
351 static void perform_test(const char *name
, void (*callback
)(void))
353 printk("%s\n", name
);
357 static int my_open(struct inode
*inode
, struct file
*file
)
359 perform_test("fair-rwlock-create", fair_rwlock_create
);
360 ssleep(TEST_DURATION
);
361 perform_test("fair-rwlock-stop", fair_rwlock_stop
);
367 static struct file_operations my_operations
= {
371 int init_module(void)
373 pentry
= create_proc_entry("testfrwlock", 0444, NULL
);
375 pentry
->proc_fops
= &my_operations
;
377 printk("NR_CPUS : %d\n", NR_CPUS
);
378 printk("THREAD_ROFFSET : %lX\n", THREAD_ROFFSET
);
379 printk("THREAD_RMASK : %lX\n", THREAD_RMASK
);
380 printk("SOFTIRQ_ROFFSET : %lX\n", SOFTIRQ_ROFFSET
);
381 printk("SOFTIRQ_RMASK : %lX\n", SOFTIRQ_RMASK
);
382 printk("HARDIRQ_ROFFSET : %lX\n", HARDIRQ_ROFFSET
);
383 printk("HARDIRQ_RMASK : %lX\n", HARDIRQ_RMASK
);
384 printk("SUBSCRIBERS_WOFFSET : %lX\n", SUBSCRIBERS_WOFFSET
);
385 printk("SUBSCRIBERS_WMASK : %lX\n", SUBSCRIBERS_WMASK
);
386 printk("WRITER_MUTEX : %lX\n", WRITER_MUTEX
);
387 printk("SOFTIRQ_WMASK : %lX\n", SOFTIRQ_WMASK
);
388 printk("HARDIRQ_WMASK : %lX\n", HARDIRQ_WMASK
);
393 void cleanup_module(void)
395 remove_proc_entry("testfrwlock", NULL
);
398 MODULE_LICENSE("GPL");
399 MODULE_AUTHOR("Mathieu Desnoyers");
400 MODULE_DESCRIPTION("Fair rwlock test");