3343768e |
1 | /* test-cmpxchg-nolock.c |
2 | * |
3 | * Compare local cmpxchg with irq disable / enable. |
4 | */ |
5 | |
6 | |
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> |
8593c315 |
12 | #include <linux/spinlock.h> |
13 | #include <linux/seqlock.h> |
3343768e |
14 | #include <asm/timex.h> |
15 | #include <asm/system.h> |
16 | |
17 | #define NR_LOOPS 20000 |
18 | |
19 | int test_val; |
20 | |
21 | static void do_testbaseline(void) |
22 | { |
3343768e |
23 | long flags; |
24 | unsigned int i; |
25 | cycles_t time1, time2, time; |
26 | long rem; |
27 | |
28 | local_irq_save(flags); |
29 | preempt_disable(); |
30 | time1 = get_cycles(); |
31 | for (i = 0; i < NR_LOOPS; i++) { |
32 | asm volatile (""); |
33 | } |
34 | time2 = get_cycles(); |
35 | local_irq_restore(flags); |
36 | preempt_enable(); |
37 | time = time2 - time1; |
38 | |
39 | printk(KERN_ALERT "test results: time for baseline\n"); |
40 | printk(KERN_ALERT "number of loops: %d\n", NR_LOOPS); |
41 | printk(KERN_ALERT "total time: %llu\n", time); |
42 | time = div_long_long_rem(time, NR_LOOPS, &rem); |
43 | printk(KERN_ALERT "-> baseline takes %llu cycles\n", time); |
44 | printk(KERN_ALERT "test end\n"); |
45 | } |
46 | |
47 | static void do_test_spinlock(void) |
48 | { |
8593c315 |
49 | static DEFINE_SPINLOCK(mylock); |
3343768e |
50 | long flags; |
51 | unsigned int i; |
52 | cycles_t time1, time2, time; |
53 | long rem; |
54 | |
55 | preempt_disable(); |
8593c315 |
56 | spin_lock_irqsave(&mylock, flags); |
3343768e |
57 | time1 = get_cycles(); |
58 | for (i = 0; i < NR_LOOPS; i++) { |
0c12e051 |
59 | spin_unlock(&mylock); |
60 | spin_lock(&mylock); |
3343768e |
61 | } |
62 | time2 = get_cycles(); |
8593c315 |
63 | spin_unlock_irqrestore(&mylock, flags); |
3343768e |
64 | preempt_enable(); |
65 | time = time2 - time1; |
66 | |
67 | printk(KERN_ALERT "test results: time for spinlock\n"); |
68 | printk(KERN_ALERT "number of loops: %d\n", NR_LOOPS); |
69 | printk(KERN_ALERT "total time: %llu\n", time); |
70 | time = div_long_long_rem(time, NR_LOOPS, &rem); |
71 | printk(KERN_ALERT "-> spinlock takes %llu cycles\n", time); |
72 | printk(KERN_ALERT "test end\n"); |
73 | } |
74 | |
75 | static void do_test_seqlock(void) |
76 | { |
77 | static seqlock_t test_lock; |
8593c315 |
78 | unsigned long seq; |
3343768e |
79 | long flags; |
80 | unsigned int i; |
81 | cycles_t time1, time2, time; |
82 | long rem; |
83 | |
472e3e15 |
84 | local_irq_save(flags); |
3343768e |
85 | preempt_disable(); |
86 | time1 = get_cycles(); |
87 | for (i = 0; i < NR_LOOPS; i++) { |
88 | do { |
472e3e15 |
89 | seq = read_seqbegin(&test_lock); |
90 | } while (read_seqretry(&test_lock, seq)); |
3343768e |
91 | } |
92 | time2 = get_cycles(); |
93 | preempt_enable(); |
94 | time = time2 - time1; |
472e3e15 |
95 | local_irq_restore(flags); |
3343768e |
96 | |
97 | printk(KERN_ALERT "test results: time for seqlock\n"); |
98 | printk(KERN_ALERT "number of loops: %d\n", NR_LOOPS); |
99 | printk(KERN_ALERT "total time: %llu\n", time); |
100 | time = div_long_long_rem(time, NR_LOOPS, &rem); |
101 | printk(KERN_ALERT "-> seqlock takes %llu cycles\n", time); |
102 | printk(KERN_ALERT "test end\n"); |
103 | } |
104 | |
105 | /* |
106 | * This test will have a higher standard deviation due to incoming interrupts. |
107 | */ |
108 | static void do_test_preempt(void) |
109 | { |
110 | long flags; |
111 | unsigned int i; |
112 | cycles_t time1, time2, time; |
113 | long rem; |
114 | |
115 | local_irq_save(flags); |
116 | preempt_disable(); |
117 | time1 = get_cycles(); |
118 | for (i = 0; i < NR_LOOPS; i++) { |
3343768e |
119 | preempt_disable(); |
54e7c224 |
120 | preempt_enable(); |
3343768e |
121 | } |
122 | time2 = get_cycles(); |
123 | preempt_enable(); |
124 | time = time2 - time1; |
125 | local_irq_restore(flags); |
126 | |
127 | printk(KERN_ALERT "test results: time for preempt disable/enable pairs\n"); |
128 | printk(KERN_ALERT "number of loops: %d\n", NR_LOOPS); |
129 | printk(KERN_ALERT "total time: %llu\n", time); |
130 | time = div_long_long_rem(time, NR_LOOPS, &rem); |
131 | printk(KERN_ALERT "-> preempt disable/enable pair takes %llu cycles\n", |
132 | time); |
133 | printk(KERN_ALERT "test end\n"); |
134 | } |
135 | |
136 | static int ltt_test_init(void) |
137 | { |
138 | printk(KERN_ALERT "test init\n"); |
139 | |
140 | do_testbaseline(); |
141 | do_test_spinlock(); |
142 | do_test_seqlock(); |
143 | do_test_preempt(); |
144 | return -EAGAIN; /* Fail will directly unload the module */ |
145 | } |
146 | |
147 | static void ltt_test_exit(void) |
148 | { |
149 | printk(KERN_ALERT "test exit\n"); |
150 | } |
151 | |
152 | module_init(ltt_test_init) |
153 | module_exit(ltt_test_exit) |
154 | |
155 | MODULE_LICENSE("GPL"); |
156 | MODULE_AUTHOR("Mathieu Desnoyers"); |
157 | MODULE_DESCRIPTION("Cmpxchg vs int Test"); |
158 | |