2 * test-read-lock-speed.c
5 * - spin lock irqsave / spin unlock irqrestore (close to rwlocks when
7 * - using a sequence read lock (uncontended)
8 * - preempt disable/enable (RCU)
10 * Copyright 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
11 * Distributed under GPLv2
14 #include <linux/jiffies.h>
15 #include <linux/compiler.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/math64.h>
19 #include <linux/spinlock.h>
20 #include <linux/seqlock.h>
21 #include <linux/cpumask.h>
22 #include <asm/timex.h>
23 #include <asm/system.h>
25 #define NR_LOOPS 20000
27 #ifndef CONFIG_PREEMPT
28 #error "Your kernel should be built with preemption enabled"
31 #ifdef CONFIG_DEBUG_PREEMPT
32 #error "Please disable CONFIG_DEBUG_PREEMPT"
35 #ifdef CONFIG_DEBUG_SPINLOCK
36 #error "Please disable CONFIG_DEBUG_SPINLOCK"
40 #error "Please disable CONFIG_LOCKDEP"
45 static void do_testbaseline(void)
49 cycles_t time1
, time2
, time
;
52 local_irq_save(flags
);
55 for (i
= 0; i
< NR_LOOPS
; i
++) {
59 local_irq_restore(flags
);
63 printk(KERN_ALERT
"test results: time for baseline\n");
64 printk(KERN_ALERT
"number of loops: %d\n", NR_LOOPS
);
65 printk(KERN_ALERT
"total time: %llu\n", time
);
66 time
= div_u64_rem(time
, NR_LOOPS
, &rem
);
67 printk(KERN_ALERT
"-> baseline takes %llu cycles\n", time
);
68 printk(KERN_ALERT
"test end\n");
71 static void do_test_spinlock(void)
73 static DEFINE_SPINLOCK(mylock
);
76 cycles_t time1
, time2
, time
;
80 spin_lock_irqsave(&mylock
, flags
);
82 for (i
= 0; i
< NR_LOOPS
; i
++) {
87 spin_unlock_irqrestore(&mylock
, flags
);
91 printk(KERN_ALERT
"test results: time for spinlock\n");
92 printk(KERN_ALERT
"number of loops: %d\n", NR_LOOPS
);
93 printk(KERN_ALERT
"total time: %llu\n", time
);
94 time
= div_u64_rem(time
, NR_LOOPS
, &rem
);
95 printk(KERN_ALERT
"-> spinlock takes %llu cycles\n", time
);
96 printk(KERN_ALERT
"test end\n");
99 static void do_test_seqlock(void)
101 static seqlock_t test_lock
;
105 cycles_t time1
, time2
, time
;
108 local_irq_save(flags
);
109 time1
= get_cycles();
110 for (i
= 0; i
< NR_LOOPS
; i
++) {
112 seq
= read_seqbegin(&test_lock
);
113 } while (read_seqretry(&test_lock
, seq
));
115 time2
= get_cycles();
116 time
= time2
- time1
;
117 local_irq_restore(flags
);
119 printk(KERN_ALERT
"test results: time for seqlock\n");
120 printk(KERN_ALERT
"number of loops: %d\n", NR_LOOPS
);
121 printk(KERN_ALERT
"total time: %llu\n", time
);
122 time
= div_u64_rem(time
, NR_LOOPS
, &rem
);
123 printk(KERN_ALERT
"-> seqlock takes %llu cycles\n", time
);
124 printk(KERN_ALERT
"test end\n");
128 * Note : This test _should_ trigger lockdep errors due to preemption
129 * disabling/enabling within irq off section. Given we are only interested in
130 * having the most precise measurement for preemption disable/enable, we don't
133 static void do_test_preempt(void)
137 cycles_t time1
, time2
, time
;
140 local_irq_save(flags
);
142 time1
= get_cycles();
143 for (i
= 0; i
< NR_LOOPS
; i
++) {
147 time2
= get_cycles();
149 time
= time2
- time1
;
150 local_irq_restore(flags
);
153 "test results: time for preempt disable/enable pairs\n");
154 printk(KERN_ALERT
"number of loops: %d\n", NR_LOOPS
);
155 printk(KERN_ALERT
"total time: %llu\n", time
);
156 time
= div_u64_rem(time
, NR_LOOPS
, &rem
);
157 printk(KERN_ALERT
"-> preempt disable/enable pair takes %llu cycles\n",
159 printk(KERN_ALERT
"test end\n");
162 static int ltt_test_init(void)
164 printk(KERN_ALERT
"test init\n");
166 printk(KERN_ALERT
"Number of active CPUs : %d\n", num_online_cpus());
171 return -EAGAIN
; /* Fail will directly unload the module */
174 static void ltt_test_exit(void)
176 printk(KERN_ALERT
"test exit\n");
179 module_init(ltt_test_init
)
180 module_exit(ltt_test_exit
)
182 MODULE_LICENSE("GPL");
183 MODULE_AUTHOR("Mathieu Desnoyers");
184 MODULE_DESCRIPTION("Test read lock speed");
This page took 0.03321 seconds and 4 git commands to generate.