1 /* test-cmpxchg-nolock.c
3 * Compare local cmpxchg with irq disable / enable.
7 #include <linux/jiffies.h>
8 #include <linux/compiler.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/calc64.h>
12 #include <asm/timex.h>
13 #include <asm/system.h>
15 #define NR_LOOPS 20000
19 static void do_testbaseline(void)
24 cycles_t time1
, time2
, time
;
27 local_irq_save(flags
);
30 for (i
= 0; i
< NR_LOOPS
; i
++) {
34 local_irq_restore(flags
);
38 printk(KERN_ALERT
"test results: time for baseline\n");
39 printk(KERN_ALERT
"number of loops: %d\n", NR_LOOPS
);
40 printk(KERN_ALERT
"total time: %llu\n", time
);
41 time
= div_long_long_rem(time
, NR_LOOPS
, &rem
);
42 printk(KERN_ALERT
"-> baseline takes %llu cycles\n", time
);
43 printk(KERN_ALERT
"test end\n");
46 static void do_test_sync_cmpxchg(void)
51 cycles_t time1
, time2
, time
;
54 local_irq_save(flags
);
57 for (i
= 0; i
< NR_LOOPS
; i
++) {
59 ret
= sync_cmpxchg(&test_val
, 0, 0);
61 ret
= cmpxchg(&test_val
, 0, 0);
65 local_irq_restore(flags
);
69 printk(KERN_ALERT
"test results: time for locked cmpxchg\n");
70 printk(KERN_ALERT
"number of loops: %d\n", NR_LOOPS
);
71 printk(KERN_ALERT
"total time: %llu\n", time
);
72 time
= div_long_long_rem(time
, NR_LOOPS
, &rem
);
73 printk(KERN_ALERT
"-> locked cmpxchg takes %llu cycles\n", time
);
74 printk(KERN_ALERT
"test end\n");
77 static void do_test_cmpxchg(void)
82 cycles_t time1
, time2
, time
;
85 local_irq_save(flags
);
88 for (i
= 0; i
< NR_LOOPS
; i
++) {
89 ret
= cmpxchg_local(&test_val
, 0, 0);
92 local_irq_restore(flags
);
96 printk(KERN_ALERT
"test results: time for non locked cmpxchg\n");
97 printk(KERN_ALERT
"number of loops: %d\n", NR_LOOPS
);
98 printk(KERN_ALERT
"total time: %llu\n", time
);
99 time
= div_long_long_rem(time
, NR_LOOPS
, &rem
);
100 printk(KERN_ALERT
"-> non locked cmpxchg takes %llu cycles\n", time
);
101 printk(KERN_ALERT
"test end\n");
103 static void do_test_sync_inc(void)
108 cycles_t time1
, time2
, time
;
112 local_irq_save(flags
);
114 time1
= get_cycles();
115 for (i
= 0; i
< NR_LOOPS
; i
++) {
116 ret
= atomic_add_return(10, &val
);
118 time2
= get_cycles();
119 local_irq_restore(flags
);
121 time
= time2
- time1
;
123 printk(KERN_ALERT
"test results: time for locked add return\n");
124 printk(KERN_ALERT
"number of loops: %d\n", NR_LOOPS
);
125 printk(KERN_ALERT
"total time: %llu\n", time
);
126 time
= div_long_long_rem(time
, NR_LOOPS
, &rem
);
127 printk(KERN_ALERT
"-> locked add return takes %llu cycles\n", time
);
128 printk(KERN_ALERT
"test end\n");
132 static void do_test_inc(void)
137 cycles_t time1
, time2
, time
;
141 local_irq_save(flags
);
143 time1
= get_cycles();
144 for (i
= 0; i
< NR_LOOPS
; i
++) {
145 ret
= local_add_return(10, &loc_val
);
147 time2
= get_cycles();
148 local_irq_restore(flags
);
150 time
= time2
- time1
;
152 printk(KERN_ALERT
"test results: time for non locked add return\n");
153 printk(KERN_ALERT
"number of loops: %d\n", NR_LOOPS
);
154 printk(KERN_ALERT
"total time: %llu\n", time
);
155 time
= div_long_long_rem(time
, NR_LOOPS
, &rem
);
156 printk(KERN_ALERT
"-> non locked add return takes %llu cycles\n", time
);
157 printk(KERN_ALERT
"test end\n");
163 * This test will have a higher standard deviation due to incoming interrupts.
165 static void do_test_enable_int(void)
169 cycles_t time1
, time2
, time
;
172 local_irq_save(flags
);
174 time1
= get_cycles();
175 for (i
= 0; i
< NR_LOOPS
; i
++) {
176 local_irq_restore(flags
);
178 time2
= get_cycles();
179 local_irq_restore(flags
);
181 time
= time2
- time1
;
183 printk(KERN_ALERT
"test results: time for enabling interrupts (STI)\n");
184 printk(KERN_ALERT
"number of loops: %d\n", NR_LOOPS
);
185 printk(KERN_ALERT
"total time: %llu\n", time
);
186 time
= div_long_long_rem(time
, NR_LOOPS
, &rem
);
187 printk(KERN_ALERT
"-> enabling interrupts (STI) takes %llu cycles\n",
189 printk(KERN_ALERT
"test end\n");
192 static void do_test_disable_int(void)
194 unsigned long flags
, flags2
;
196 cycles_t time1
, time2
, time
;
199 local_irq_save(flags
);
201 time1
= get_cycles();
202 for ( i
= 0; i
< NR_LOOPS
; i
++) {
203 local_irq_save(flags2
);
205 time2
= get_cycles();
206 local_irq_restore(flags
);
208 time
= time2
- time1
;
210 printk(KERN_ALERT
"test results: time for disabling interrupts (CLI)\n");
211 printk(KERN_ALERT
"number of loops: %d\n", NR_LOOPS
);
212 printk(KERN_ALERT
"total time: %llu\n", time
);
213 time
= div_long_long_rem(time
, NR_LOOPS
, &rem
);
214 printk(KERN_ALERT
"-> disabling interrupts (CLI) takes %llu cycles\n",
216 printk(KERN_ALERT
"test end\n");
219 static void do_test_int(void)
223 cycles_t time1
, time2
, time
;
226 local_irq_save(flags
);
228 time1
= get_cycles();
229 for (i
= 0; i
< NR_LOOPS
; i
++) {
230 local_irq_restore(flags
);
231 local_irq_save(flags
);
233 time2
= get_cycles();
234 local_irq_restore(flags
);
236 time
= time2
- time1
;
238 printk(KERN_ALERT
"test results: time for disabling/enabling interrupts (STI/CLI)\n");
239 printk(KERN_ALERT
"number of loops: %d\n", NR_LOOPS
);
240 printk(KERN_ALERT
"total time: %llu\n", time
);
241 time
= div_long_long_rem(time
, NR_LOOPS
, &rem
);
242 printk(KERN_ALERT
"-> enabling/disabling interrupts (STI/CLI) takes %llu cycles\n",
244 printk(KERN_ALERT
"test end\n");
249 static int ltt_test_init(void)
251 printk(KERN_ALERT
"test init\n");
254 do_test_sync_cmpxchg();
258 do_test_enable_int();
259 do_test_disable_int();
261 return -EAGAIN
; /* Fail will directly unload the module */
264 static void ltt_test_exit(void)
266 printk(KERN_ALERT
"test exit\n");
269 module_init(ltt_test_init
)
270 module_exit(ltt_test_exit
)
272 MODULE_LICENSE("GPL");
273 MODULE_AUTHOR("Mathieu Desnoyers");
274 MODULE_DESCRIPTION("Cmpxchg vs int Test");
This page took 0.034419 seconds and 4 git commands to generate.