b64661e6 |
1 | /* test-cmpxchg.c |
2 | * |
3 | * Test time spent in a LTTng instrumentation probe. |
4 | */ |
5 | |
6 | |
7 | #include <linux/config.h> |
8 | #include <linux/jiffies.h> |
9 | #include <linux/compiler.h> |
10 | #include <linux/init.h> |
11 | #include <linux/module.h> |
12 | |
13 | |
14 | #define cmpxchg_up(ptr,o,n)\ |
15 | ((__typeof__(*(ptr)))__cmpxchg_up((ptr),(unsigned long)(o),\ |
16 | (unsigned long)(n),sizeof(*(ptr)))) |
17 | static inline unsigned long __cmpxchg_up(volatile void *ptr, unsigned long old, |
18 | unsigned long new, int size) |
19 | { |
20 | unsigned long prev; |
21 | switch (size) { |
22 | case 1: |
23 | __asm__ __volatile__("cmpxchgb %b1,%2" |
24 | : "=a"(prev) |
25 | : "q"(new), "m"(*__xg(ptr)), "0"(old) |
26 | : "memory"); |
27 | return prev; |
28 | case 2: |
29 | __asm__ __volatile__("cmpxchgw %w1,%2" |
30 | : "=a"(prev) |
31 | : "r"(new), "m"(*__xg(ptr)), "0"(old) |
32 | : "memory"); |
33 | return prev; |
34 | case 4: |
35 | __asm__ __volatile__("cmpxchgl %1,%2" |
36 | : "=a"(prev) |
37 | : "r"(new), "m"(*__xg(ptr)), "0"(old) |
38 | : "memory"); |
39 | return prev; |
40 | } |
41 | return old; |
42 | } |
43 | |
44 | |
45 | #define NR_LOOPS 20000 |
46 | |
47 | volatile int test_val = 100; |
48 | |
49 | |
50 | static inline void do_test(void) |
51 | { |
52 | int val, ret; |
53 | |
54 | val = test_val; |
55 | |
56 | ret = cmpxchg_up(&test_val, val, val+1); |
57 | } |
58 | |
59 | static int ltt_test_init(void) |
60 | { |
61 | unsigned int i; |
62 | cycles_t time1, time2, time; |
63 | cycles_t max_time = 0, min_time = 18446744073709551615ULL; /* (2^64)-1 */ |
64 | cycles_t tot_time = 0; |
65 | unsigned long flags; |
66 | printk(KERN_ALERT "test init\n"); |
67 | |
68 | local_irq_save(flags); |
69 | time1 = get_cycles(); |
70 | for(i=0; i<NR_LOOPS; i++) { |
71 | //for(int j=0; j<10; j++) { |
72 | do_test(); |
73 | //} |
74 | //max_time = max(max_time, time); |
75 | //min_time = min(min_time, time); |
76 | //printk("val : %d\n", test_val); |
77 | } |
78 | time2 = get_cycles(); |
79 | local_irq_restore(flags); |
80 | time = time2 - time1; |
81 | tot_time += time; |
82 | |
83 | printk(KERN_ALERT "test results : time non locked cmpxchg\n"); |
84 | printk(KERN_ALERT "number of loops : %d\n", NR_LOOPS); |
85 | printk(KERN_ALERT "total time : %llu\n", tot_time); |
86 | //printk(KERN_ALERT "min : %llu\n", min_time); |
87 | //printk(KERN_ALERT "max : %llu\n", max_time); |
88 | |
89 | printk(KERN_ALERT "test end\n"); |
90 | |
91 | return -EAGAIN; /* Fail will directly unload the module */ |
92 | } |
93 | |
94 | static void ltt_test_exit(void) |
95 | { |
96 | printk(KERN_ALERT "test exit\n"); |
97 | } |
98 | |
99 | module_init(ltt_test_init) |
100 | module_exit(ltt_test_exit) |
101 | |
102 | MODULE_LICENSE("GPL"); |
103 | MODULE_AUTHOR("Mathieu Desnoyers"); |
104 | MODULE_DESCRIPTION("Linux Trace Toolkit Test"); |
105 | |