Preempt-RCU: implementation
[linux-2.6.git] / kernel / rcupreempt_trace.c
1 /*
2  * Read-Copy Update tracing for realtime implementation
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * Copyright IBM Corporation, 2006
19  *
20  * Papers:  http://www.rdrop.com/users/paulmck/RCU
21  *
22  * For detailed explanation of Read-Copy Update mechanism see -
23  *              Documentation/RCU/ *.txt
24  *
25  */
26 #include <linux/types.h>
27 #include <linux/kernel.h>
28 #include <linux/init.h>
29 #include <linux/spinlock.h>
30 #include <linux/smp.h>
31 #include <linux/rcupdate.h>
32 #include <linux/interrupt.h>
33 #include <linux/sched.h>
34 #include <asm/atomic.h>
35 #include <linux/bitops.h>
36 #include <linux/module.h>
37 #include <linux/completion.h>
38 #include <linux/moduleparam.h>
39 #include <linux/percpu.h>
40 #include <linux/notifier.h>
41 #include <linux/rcupdate.h>
42 #include <linux/cpu.h>
43 #include <linux/mutex.h>
44 #include <linux/rcupreempt_trace.h>
45 #include <linux/debugfs.h>
46
47 static struct mutex rcupreempt_trace_mutex;
48 static char *rcupreempt_trace_buf;
49 #define RCUPREEMPT_TRACE_BUF_SIZE 4096
50
51 void rcupreempt_trace_move2done(struct rcupreempt_trace *trace)
52 {
53         trace->done_length += trace->wait_length;
54         trace->done_add += trace->wait_length;
55         trace->wait_length = 0;
56 }
57 void rcupreempt_trace_move2wait(struct rcupreempt_trace *trace)
58 {
59         trace->wait_length += trace->next_length;
60         trace->wait_add += trace->next_length;
61         trace->next_length = 0;
62 }
63 void rcupreempt_trace_try_flip_1(struct rcupreempt_trace *trace)
64 {
65         atomic_inc(&trace->rcu_try_flip_1);
66 }
67 void rcupreempt_trace_try_flip_e1(struct rcupreempt_trace *trace)
68 {
69         atomic_inc(&trace->rcu_try_flip_e1);
70 }
71 void rcupreempt_trace_try_flip_i1(struct rcupreempt_trace *trace)
72 {
73         trace->rcu_try_flip_i1++;
74 }
75 void rcupreempt_trace_try_flip_ie1(struct rcupreempt_trace *trace)
76 {
77         trace->rcu_try_flip_ie1++;
78 }
79 void rcupreempt_trace_try_flip_g1(struct rcupreempt_trace *trace)
80 {
81         trace->rcu_try_flip_g1++;
82 }
83 void rcupreempt_trace_try_flip_a1(struct rcupreempt_trace *trace)
84 {
85         trace->rcu_try_flip_a1++;
86 }
87 void rcupreempt_trace_try_flip_ae1(struct rcupreempt_trace *trace)
88 {
89         trace->rcu_try_flip_ae1++;
90 }
91 void rcupreempt_trace_try_flip_a2(struct rcupreempt_trace *trace)
92 {
93         trace->rcu_try_flip_a2++;
94 }
95 void rcupreempt_trace_try_flip_z1(struct rcupreempt_trace *trace)
96 {
97         trace->rcu_try_flip_z1++;
98 }
99 void rcupreempt_trace_try_flip_ze1(struct rcupreempt_trace *trace)
100 {
101         trace->rcu_try_flip_ze1++;
102 }
103 void rcupreempt_trace_try_flip_z2(struct rcupreempt_trace *trace)
104 {
105         trace->rcu_try_flip_z2++;
106 }
107 void rcupreempt_trace_try_flip_m1(struct rcupreempt_trace *trace)
108 {
109         trace->rcu_try_flip_m1++;
110 }
111 void rcupreempt_trace_try_flip_me1(struct rcupreempt_trace *trace)
112 {
113         trace->rcu_try_flip_me1++;
114 }
115 void rcupreempt_trace_try_flip_m2(struct rcupreempt_trace *trace)
116 {
117         trace->rcu_try_flip_m2++;
118 }
119 void rcupreempt_trace_check_callbacks(struct rcupreempt_trace *trace)
120 {
121         trace->rcu_check_callbacks++;
122 }
123 void rcupreempt_trace_done_remove(struct rcupreempt_trace *trace)
124 {
125         trace->done_remove += trace->done_length;
126         trace->done_length = 0;
127 }
128 void rcupreempt_trace_invoke(struct rcupreempt_trace *trace)
129 {
130         atomic_inc(&trace->done_invoked);
131 }
132 void rcupreempt_trace_next_add(struct rcupreempt_trace *trace)
133 {
134         trace->next_add++;
135         trace->next_length++;
136 }
137
138 static void rcupreempt_trace_sum(struct rcupreempt_trace *sp)
139 {
140         struct rcupreempt_trace *cp;
141         int cpu;
142
143         memset(sp, 0, sizeof(*sp));
144         for_each_possible_cpu(cpu) {
145                 cp = rcupreempt_trace_cpu(cpu);
146                 sp->next_length += cp->next_length;
147                 sp->next_add += cp->next_add;
148                 sp->wait_length += cp->wait_length;
149                 sp->wait_add += cp->wait_add;
150                 sp->done_length += cp->done_length;
151                 sp->done_add += cp->done_add;
152                 sp->done_remove += cp->done_remove;
153                 atomic_set(&sp->done_invoked, atomic_read(&cp->done_invoked));
154                 sp->rcu_check_callbacks += cp->rcu_check_callbacks;
155                 atomic_set(&sp->rcu_try_flip_1,
156                            atomic_read(&cp->rcu_try_flip_1));
157                 atomic_set(&sp->rcu_try_flip_e1,
158                            atomic_read(&cp->rcu_try_flip_e1));
159                 sp->rcu_try_flip_i1 += cp->rcu_try_flip_i1;
160                 sp->rcu_try_flip_ie1 += cp->rcu_try_flip_ie1;
161                 sp->rcu_try_flip_g1 += cp->rcu_try_flip_g1;
162                 sp->rcu_try_flip_a1 += cp->rcu_try_flip_a1;
163                 sp->rcu_try_flip_ae1 += cp->rcu_try_flip_ae1;
164                 sp->rcu_try_flip_a2 += cp->rcu_try_flip_a2;
165                 sp->rcu_try_flip_z1 += cp->rcu_try_flip_z1;
166                 sp->rcu_try_flip_ze1 += cp->rcu_try_flip_ze1;
167                 sp->rcu_try_flip_z2 += cp->rcu_try_flip_z2;
168                 sp->rcu_try_flip_m1 += cp->rcu_try_flip_m1;
169                 sp->rcu_try_flip_me1 += cp->rcu_try_flip_me1;
170                 sp->rcu_try_flip_m2 += cp->rcu_try_flip_m2;
171         }
172 }
173
174 static ssize_t rcustats_read(struct file *filp, char __user *buffer,
175                                 size_t count, loff_t *ppos)
176 {
177         struct rcupreempt_trace trace;
178         ssize_t bcount;
179         int cnt = 0;
180
181         rcupreempt_trace_sum(&trace);
182         mutex_lock(&rcupreempt_trace_mutex);
183         snprintf(&rcupreempt_trace_buf[cnt], RCUPREEMPT_TRACE_BUF_SIZE - cnt,
184                  "ggp=%ld rcc=%ld\n",
185                  rcu_batches_completed(),
186                  trace.rcu_check_callbacks);
187         snprintf(&rcupreempt_trace_buf[cnt], RCUPREEMPT_TRACE_BUF_SIZE - cnt,
188                  "na=%ld nl=%ld wa=%ld wl=%ld da=%ld dl=%ld dr=%ld di=%d\n"
189                  "1=%d e1=%d i1=%ld ie1=%ld g1=%ld a1=%ld ae1=%ld a2=%ld\n"
190                  "z1=%ld ze1=%ld z2=%ld m1=%ld me1=%ld m2=%ld\n",
191
192                  trace.next_add, trace.next_length,
193                  trace.wait_add, trace.wait_length,
194                  trace.done_add, trace.done_length,
195                  trace.done_remove, atomic_read(&trace.done_invoked),
196                  atomic_read(&trace.rcu_try_flip_1),
197                  atomic_read(&trace.rcu_try_flip_e1),
198                  trace.rcu_try_flip_i1, trace.rcu_try_flip_ie1,
199                  trace.rcu_try_flip_g1,
200                  trace.rcu_try_flip_a1, trace.rcu_try_flip_ae1,
201                          trace.rcu_try_flip_a2,
202                  trace.rcu_try_flip_z1, trace.rcu_try_flip_ze1,
203                          trace.rcu_try_flip_z2,
204                  trace.rcu_try_flip_m1, trace.rcu_try_flip_me1,
205                         trace.rcu_try_flip_m2);
206         bcount = simple_read_from_buffer(buffer, count, ppos,
207                         rcupreempt_trace_buf, strlen(rcupreempt_trace_buf));
208         mutex_unlock(&rcupreempt_trace_mutex);
209         return bcount;
210 }
211
212 static ssize_t rcugp_read(struct file *filp, char __user *buffer,
213                                 size_t count, loff_t *ppos)
214 {
215         long oldgp = rcu_batches_completed();
216         ssize_t bcount;
217
218         mutex_lock(&rcupreempt_trace_mutex);
219         synchronize_rcu();
220         snprintf(rcupreempt_trace_buf, RCUPREEMPT_TRACE_BUF_SIZE,
221                 "oldggp=%ld  newggp=%ld\n", oldgp, rcu_batches_completed());
222         bcount = simple_read_from_buffer(buffer, count, ppos,
223                         rcupreempt_trace_buf, strlen(rcupreempt_trace_buf));
224         mutex_unlock(&rcupreempt_trace_mutex);
225         return bcount;
226 }
227
228 static ssize_t rcuctrs_read(struct file *filp, char __user *buffer,
229                                 size_t count, loff_t *ppos)
230 {
231         int cnt = 0;
232         int cpu;
233         int f = rcu_batches_completed() & 0x1;
234         ssize_t bcount;
235
236         mutex_lock(&rcupreempt_trace_mutex);
237
238         cnt += snprintf(&rcupreempt_trace_buf[cnt], RCUPREEMPT_TRACE_BUF_SIZE,
239                                 "CPU last cur F M\n");
240         for_each_online_cpu(cpu) {
241                 long *flipctr = rcupreempt_flipctr(cpu);
242                 cnt += snprintf(&rcupreempt_trace_buf[cnt],
243                                 RCUPREEMPT_TRACE_BUF_SIZE - cnt,
244                                         "%3d %4ld %3ld %d %d\n",
245                                cpu,
246                                flipctr[!f],
247                                flipctr[f],
248                                rcupreempt_flip_flag(cpu),
249                                rcupreempt_mb_flag(cpu));
250         }
251         cnt += snprintf(&rcupreempt_trace_buf[cnt],
252                         RCUPREEMPT_TRACE_BUF_SIZE - cnt,
253                         "ggp = %ld, state = %s\n",
254                         rcu_batches_completed(),
255                         rcupreempt_try_flip_state_name());
256         cnt += snprintf(&rcupreempt_trace_buf[cnt],
257                         RCUPREEMPT_TRACE_BUF_SIZE - cnt,
258                         "\n");
259         bcount = simple_read_from_buffer(buffer, count, ppos,
260                         rcupreempt_trace_buf, strlen(rcupreempt_trace_buf));
261         mutex_unlock(&rcupreempt_trace_mutex);
262         return bcount;
263 }
264
265 static struct file_operations rcustats_fops = {
266         .owner = THIS_MODULE,
267         .read = rcustats_read,
268 };
269
270 static struct file_operations rcugp_fops = {
271         .owner = THIS_MODULE,
272         .read = rcugp_read,
273 };
274
275 static struct file_operations rcuctrs_fops = {
276         .owner = THIS_MODULE,
277         .read = rcuctrs_read,
278 };
279
280 static struct dentry *rcudir, *statdir, *ctrsdir, *gpdir;
281 static int rcupreempt_debugfs_init(void)
282 {
283         rcudir = debugfs_create_dir("rcu", NULL);
284         if (!rcudir)
285                 goto out;
286         statdir = debugfs_create_file("rcustats", 0444, rcudir,
287                                                 NULL, &rcustats_fops);
288         if (!statdir)
289                 goto free_out;
290
291         gpdir = debugfs_create_file("rcugp", 0444, rcudir, NULL, &rcugp_fops);
292         if (!gpdir)
293                 goto free_out;
294
295         ctrsdir = debugfs_create_file("rcuctrs", 0444, rcudir,
296                                                 NULL, &rcuctrs_fops);
297         if (!ctrsdir)
298                 goto free_out;
299         return 0;
300 free_out:
301         if (statdir)
302                 debugfs_remove(statdir);
303         if (gpdir)
304                 debugfs_remove(gpdir);
305         debugfs_remove(rcudir);
306 out:
307         return 1;
308 }
309
310 static int __init rcupreempt_trace_init(void)
311 {
312         mutex_init(&rcupreempt_trace_mutex);
313         rcupreempt_trace_buf = kmalloc(RCUPREEMPT_TRACE_BUF_SIZE, GFP_KERNEL);
314         if (!rcupreempt_trace_buf)
315                 return 1;
316         return rcupreempt_debugfs_init();
317 }
318
319 static void __exit rcupreempt_trace_cleanup(void)
320 {
321         debugfs_remove(statdir);
322         debugfs_remove(gpdir);
323         debugfs_remove(ctrsdir);
324         debugfs_remove(rcudir);
325         kfree(rcupreempt_trace_buf);
326 }
327
328
329 module_init(rcupreempt_trace_init);
330 module_exit(rcupreempt_trace_cleanup);