72777946 |
1 | /* test-mark.c |
2 | * |
3 | */ |
4 | |
5 | #include <linux/module.h> |
6 | #include <linux/proc_fs.h> |
7 | #include <linux/sched.h> |
8 | #include <linux/timex.h> |
9 | #include <linux/marker.h> |
10 | #include <asm/ptrace.h> |
11 | #include <asm/system.h> |
12 | |
2e577577 |
13 | static void pmc_flush_cache(void) |
14 | { |
1993e033 |
15 | register int i; |
2e577577 |
16 | /* write back and invalidate cache (a serializing instruction) */ |
17 | |
18 | __asm__ __volatile__ ( "wbinvd" : : : "memory" ); |
19 | |
20 | /* The wbinvd instruction does not wait for the external caches |
21 | * to be flushed, but only requests that it be done. The loop |
22 | * is to be sure that enough time has elapsed, but the compiler |
23 | * might simplify or even remove it. The loop bound is for a |
24 | * 512 KB L2 cache. On a Pentium Pro/II/III, the loop uses |
25 | * 2 cycles per iteration. |
26 | * |
27 | * Does wbinvd also cause the TLB to be flushed? |
28 | * A comment in mtrr.c suggests that it does. |
29 | */ |
1993e033 |
30 | for (i = 0; i < 512*1024; i++) { |
31 | cpu_relax(); |
32 | } |
2e577577 |
33 | } |
34 | |
35 | |
72777946 |
36 | static void noinline test2(const struct marker *mdata, |
37 | void *call_private, ...) |
38 | { |
39 | unsigned char *ins = __builtin_return_address(0) - 5; |
40 | #if 0 |
41 | /* not called */ |
42 | printk("ip %p\n", __builtin_return_address(0)); |
43 | printk("prev_ins %hX %hX %hX %hX %hX\n", |
44 | ins[0], ins[1], ins[2], ins[3], ins[4]); |
45 | #endif //0 |
46 | ins[0] = 0x90; |
47 | ins[1] = 0x8d; |
48 | ins[2] = 0x74; |
49 | ins[3] = 0x26; |
50 | ins[4] = 0x00; |
51 | } |
52 | |
53 | /* |
54 | * Generic marker flavor always available. |
55 | * Note : the empty asm volatile with read constraint is used here instead of a |
56 | * "used" attribute to fix a gcc 4.1.x bug. |
57 | * Make sure the alignment of the structure in the __markers section will |
58 | * not add unwanted padding between the beginning of the section and the |
59 | * structure. Force alignment to the same alignment as the section start. |
60 | */ |
61 | #define __my_trace_mark(generic, name, call_private, format, args...) \ |
62 | do { \ |
63 | static const char __mstrtab_##name[] \ |
64 | __attribute__((section("__markers_strings"))) \ |
65 | = #name "\0" format; \ |
66 | static struct marker __mark_##name \ |
67 | __attribute__((section("__markers"), aligned(8))) = \ |
68 | { __mstrtab_##name, &__mstrtab_##name[sizeof(#name)], \ |
69 | 0, 0, marker_probe_cb, \ |
70 | { __mark_empty_function, NULL}, NULL }; \ |
71 | __mark_check_format(format, ## args); \ |
72 | if (!generic) { \ |
73 | if (unlikely(imv_read(__mark_##name.state))) \ |
74 | test2 \ |
75 | (&__mark_##name, call_private, \ |
76 | ## args); \ |
77 | } else { \ |
78 | if (unlikely(_imv_read(__mark_##name.state))) \ |
79 | test2 \ |
80 | (&__mark_##name, call_private, \ |
81 | ## args); \ |
82 | } \ |
83 | } while (0) |
84 | |
72777946 |
85 | struct proc_dir_entry *pentry = NULL; |
86 | |
87 | static inline void test(unsigned long arg, unsigned long arg2) |
88 | { |
1993e033 |
89 | volatile int temp[5]; |
72777946 |
90 | #ifdef CACHEFLUSH |
1a06f620 |
91 | clflush(¤t->pid); |
92 | //pmc_flush_cache(); |
72777946 |
93 | #endif |
f8a585e7 |
94 | temp[2] = (temp[0] + 60) << 10; |
95 | temp[3] = (temp[2] + 60) << 10; |
96 | temp[4] = (temp[3] + 60) << 10; |
97 | temp[0] = (temp[4] + 60) << 10; |
72777946 |
98 | //asm volatile (""); |
99 | //__my_trace_mark(1, kernel_debug_test, NULL, "%d %d %ld %ld", 2, current->pid, arg, arg2); |
f39e77c6 |
100 | barrier(); |
72777946 |
101 | test2(NULL, NULL, 2, 10, arg, arg2); |
f39e77c6 |
102 | barrier(); // make sure the compiler does not optimize marker conditions away! |
72777946 |
103 | //__my_trace_mark(0, kernel_debug_test, NULL, "%d %d %ld %ld", 2, current->pid, arg, arg2); |
104 | } |
105 | |
106 | static int my_open(struct inode *inode, struct file *file) |
107 | { |
108 | unsigned int i; |
109 | cycles_t cycles1, cycles2; |
110 | unsigned long flags; |
111 | |
112 | local_irq_save(flags); |
d3926140 |
113 | #ifdef CACHEFLUSH |
1a06f620 |
114 | //pmc_flush_cache(); /* initial write back, without cycle count */ |
115 | //msleep(20); /* wait for L2 flush */ |
d3926140 |
116 | #endif |
72777946 |
117 | rdtsc_barrier(); |
118 | cycles1 = get_cycles(); |
119 | rdtsc_barrier(); |
0ab0302a |
120 | for(i=0; i<2000; i++) { |
3a9fa119 |
121 | test(i, i); |
122 | test(i, i); |
123 | test(i, i); |
124 | test(i, i); |
125 | test(i, i); |
126 | test(i, i); |
127 | test(i, i); |
128 | test(i, i); |
129 | test(i, i); |
72777946 |
130 | test(i, i); |
131 | } |
132 | rdtsc_barrier(); |
133 | cycles2 = get_cycles(); |
134 | rdtsc_barrier(); |
135 | local_irq_restore(flags); |
37c58830 |
136 | printk("cycles : %llu\n", cycles2-cycles1); |
72777946 |
137 | return -EPERM; |
138 | } |
139 | |
140 | |
141 | static struct file_operations my_operations = { |
142 | .open = my_open, |
143 | }; |
144 | |
145 | int init_module(void) |
146 | { |
147 | pentry = create_proc_entry("testmark", 0444, NULL); |
148 | if (pentry) |
149 | pentry->proc_fops = &my_operations; |
150 | |
151 | return 0; |
152 | } |
153 | |
154 | void cleanup_module(void) |
155 | { |
156 | remove_proc_entry("testmark", NULL); |
157 | } |
158 | |
159 | MODULE_LICENSE("GPL"); |
160 | MODULE_AUTHOR("Mathieu Desnoyers"); |
161 | MODULE_DESCRIPTION("Marker Test"); |
162 | MODULE_VERSION("1.0"); |
163 | |