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");
105 * This test will have a higher standard deviation due to incoming interrupts.
107 static void do_test_enable_int(void)
111 cycles_t time1
, time2
, time
;
114 local_irq_save(flags
);
116 time1
= get_cycles();
117 for (i
= 0; i
< NR_LOOPS
; i
++) {
118 local_irq_restore(flags
);
120 time2
= get_cycles();
121 local_irq_restore(flags
);
123 time
= time2
- time1
;
125 printk(KERN_ALERT
"test results: time for enabling interrupts (STI)\n");
126 printk(KERN_ALERT
"number of loops: %d\n", NR_LOOPS
);
127 printk(KERN_ALERT
"total time: %llu\n", time
);
128 time
= div_long_long_rem(time
, NR_LOOPS
, &rem
);
129 printk(KERN_ALERT
"-> enabling interrupts (STI) takes %llu cycles\n",
131 printk(KERN_ALERT
"test end\n");
134 static void do_test_disable_int(void)
136 unsigned long flags
, flags2
;
138 cycles_t time1
, time2
, time
;
141 local_irq_save(flags
);
143 time1
= get_cycles();
144 for ( i
= 0; i
< NR_LOOPS
; i
++) {
145 local_irq_save(flags2
);
147 time2
= get_cycles();
148 local_irq_restore(flags
);
150 time
= time2
- time1
;
152 printk(KERN_ALERT
"test results: time for disabling interrupts (CLI)\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
"-> disabling interrupts (CLI) takes %llu cycles\n",
158 printk(KERN_ALERT
"test end\n");
161 static void do_test_int(void)
165 cycles_t time1
, time2
, time
;
168 local_irq_save(flags
);
170 time1
= get_cycles();
171 for (i
= 0; i
< NR_LOOPS
; i
++) {
172 local_irq_restore(flags
);
173 local_irq_save(flags
);
175 time2
= get_cycles();
176 local_irq_restore(flags
);
178 time
= time2
- time1
;
180 printk(KERN_ALERT
"test results: time for disabling/enabling interrupts (STI/CLI)\n");
181 printk(KERN_ALERT
"number of loops: %d\n", NR_LOOPS
);
182 printk(KERN_ALERT
"total time: %llu\n", time
);
183 time
= div_long_long_rem(time
, NR_LOOPS
, &rem
);
184 printk(KERN_ALERT
"-> enabling/disabling interrupts (STI/CLI) takes %llu cycles\n",
186 printk(KERN_ALERT
"test end\n");
191 static int ltt_test_init(void)
193 printk(KERN_ALERT
"test init\n");
196 do_test_sync_cmpxchg();
198 do_test_enable_int();
199 do_test_disable_int();
201 return -EAGAIN
; /* Fail will directly unload the module */
204 static void ltt_test_exit(void)
206 printk(KERN_ALERT
"test exit\n");
209 module_init(ltt_test_init
)
210 module_exit(ltt_test_exit
)
212 MODULE_LICENSE("GPL");
213 MODULE_AUTHOR("Mathieu Desnoyers");
214 MODULE_DESCRIPTION("Cmpxchg vs int Test");
This page took 0.035605 seconds and 4 git commands to generate.