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/math64.h>
12 #include <asm/timex.h>
13 #include <asm/system.h>
15 #define NR_LOOPS 20000
19 static void do_testbaseline(void)
23 cycles_t time1
, time2
, time
;
26 local_irq_save(flags
);
29 for (i
= 0; i
< NR_LOOPS
; i
++) {
33 local_irq_restore(flags
);
37 printk(KERN_ALERT
"test results: time for baseline\n");
38 printk(KERN_ALERT
"number of loops: %d\n", NR_LOOPS
);
39 printk(KERN_ALERT
"total time: %llu\n", time
);
40 time
= div_u64_rem(time
, NR_LOOPS
, &rem
);
41 printk(KERN_ALERT
"-> baseline takes %llu cycles\n", time
);
42 printk(KERN_ALERT
"test end\n");
45 static void do_test_sync_cmpxchg(void)
50 cycles_t time1
, time2
, time
;
53 local_irq_save(flags
);
56 for (i
= 0; i
< NR_LOOPS
; i
++) {
58 ret
= sync_cmpxchg(&test_val
, 0, 0);
60 ret
= cmpxchg(&test_val
, 0, 0);
64 local_irq_restore(flags
);
68 printk(KERN_ALERT
"test results: time for locked cmpxchg\n");
69 printk(KERN_ALERT
"number of loops: %d\n", NR_LOOPS
);
70 printk(KERN_ALERT
"total time: %llu\n", time
);
71 time
= div_u64_rem(time
, NR_LOOPS
, &rem
);
72 printk(KERN_ALERT
"-> locked cmpxchg takes %llu cycles\n", time
);
73 printk(KERN_ALERT
"test end\n");
76 static void do_test_cmpxchg(void)
81 cycles_t time1
, time2
, time
;
84 local_irq_save(flags
);
87 for (i
= 0; i
< NR_LOOPS
; i
++) {
88 ret
= cmpxchg_local(&test_val
, 0, 0);
91 local_irq_restore(flags
);
95 printk(KERN_ALERT
"test results: time for non locked cmpxchg\n");
96 printk(KERN_ALERT
"number of loops: %d\n", NR_LOOPS
);
97 printk(KERN_ALERT
"total time: %llu\n", time
);
98 time
= div_u64_rem(time
, NR_LOOPS
, &rem
);
99 printk(KERN_ALERT
"-> non locked cmpxchg takes %llu cycles\n", time
);
100 printk(KERN_ALERT
"test end\n");
102 static void do_test_sync_inc(void)
107 cycles_t time1
, time2
, time
;
111 local_irq_save(flags
);
113 time1
= get_cycles();
114 for (i
= 0; i
< NR_LOOPS
; i
++) {
115 ret
= atomic_add_return(10, &val
);
117 time2
= get_cycles();
118 local_irq_restore(flags
);
120 time
= time2
- time1
;
122 printk(KERN_ALERT
"test results: time for locked add return\n");
123 printk(KERN_ALERT
"number of loops: %d\n", NR_LOOPS
);
124 printk(KERN_ALERT
"total time: %llu\n", time
);
125 time
= div_u64_rem(time
, NR_LOOPS
, &rem
);
126 printk(KERN_ALERT
"-> locked add return takes %llu cycles\n", time
);
127 printk(KERN_ALERT
"test end\n");
131 static void do_test_inc(void)
136 cycles_t time1
, time2
, time
;
140 local_irq_save(flags
);
142 time1
= get_cycles();
143 for (i
= 0; i
< NR_LOOPS
; i
++) {
144 ret
= local_add_return(10, &loc_val
);
146 time2
= get_cycles();
147 local_irq_restore(flags
);
149 time
= time2
- time1
;
151 printk(KERN_ALERT
"test results: time for non locked add return\n");
152 printk(KERN_ALERT
"number of loops: %d\n", NR_LOOPS
);
153 printk(KERN_ALERT
"total time: %llu\n", time
);
154 time
= div_u64_rem(time
, NR_LOOPS
, &rem
);
155 printk(KERN_ALERT
"-> non locked add return takes %llu cycles\n", time
);
156 printk(KERN_ALERT
"test end\n");
162 * This test will have a higher standard deviation due to incoming interrupts.
164 static void do_test_enable_int(void)
168 cycles_t time1
, time2
, time
;
171 local_irq_save(flags
);
173 time1
= get_cycles();
174 for (i
= 0; i
< NR_LOOPS
; i
++) {
175 local_irq_restore(flags
);
177 time2
= get_cycles();
178 local_irq_restore(flags
);
180 time
= time2
- time1
;
182 printk(KERN_ALERT
"test results: time for enabling interrupts (STI)\n");
183 printk(KERN_ALERT
"number of loops: %d\n", NR_LOOPS
);
184 printk(KERN_ALERT
"total time: %llu\n", time
);
185 time
= div_u64_rem(time
, NR_LOOPS
, &rem
);
186 printk(KERN_ALERT
"-> enabling interrupts (STI) takes %llu cycles\n",
188 printk(KERN_ALERT
"test end\n");
191 static void do_test_disable_int(void)
193 unsigned long flags
, flags2
;
195 cycles_t time1
, time2
, time
;
198 local_irq_save(flags
);
200 time1
= get_cycles();
201 for ( i
= 0; i
< NR_LOOPS
; i
++) {
202 local_irq_save(flags2
);
204 time2
= get_cycles();
205 local_irq_restore(flags
);
207 time
= time2
- time1
;
209 printk(KERN_ALERT
"test results: time for disabling interrupts (CLI)\n");
210 printk(KERN_ALERT
"number of loops: %d\n", NR_LOOPS
);
211 printk(KERN_ALERT
"total time: %llu\n", time
);
212 time
= div_u64_rem(time
, NR_LOOPS
, &rem
);
213 printk(KERN_ALERT
"-> disabling interrupts (CLI) takes %llu cycles\n",
215 printk(KERN_ALERT
"test end\n");
218 static void do_test_int(void)
222 cycles_t time1
, time2
, time
;
225 local_irq_save(flags
);
227 time1
= get_cycles();
228 for (i
= 0; i
< NR_LOOPS
; i
++) {
229 local_irq_restore(flags
);
230 local_irq_save(flags
);
232 time2
= get_cycles();
233 local_irq_restore(flags
);
235 time
= time2
- time1
;
237 printk(KERN_ALERT
"test results: time for disabling/enabling interrupts (STI/CLI)\n");
238 printk(KERN_ALERT
"number of loops: %d\n", NR_LOOPS
);
239 printk(KERN_ALERT
"total time: %llu\n", time
);
240 time
= div_u64_rem(time
, NR_LOOPS
, &rem
);
241 printk(KERN_ALERT
"-> enabling/disabling interrupts (STI/CLI) takes %llu cycles\n",
243 printk(KERN_ALERT
"test end\n");
248 static int ltt_test_init(void)
250 printk(KERN_ALERT
"test init\n");
253 do_test_sync_cmpxchg();
257 do_test_enable_int();
258 do_test_disable_int();
260 return -EAGAIN
; /* Fail will directly unload the module */
263 static void ltt_test_exit(void)
265 printk(KERN_ALERT
"test exit\n");
268 module_init(ltt_test_init
)
269 module_exit(ltt_test_exit
)
271 MODULE_LICENSE("GPL");
272 MODULE_AUTHOR("Mathieu Desnoyers");
273 MODULE_DESCRIPTION("Cmpxchg vs int Test");
This page took 0.037862 seconds and 4 git commands to generate.