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