lockstat: measure lock bouncing
[linux-2.6.git] / kernel / lockdep_proc.c
1 /*
2  * kernel/lockdep_proc.c
3  *
4  * Runtime locking correctness validator
5  *
6  * Started by Ingo Molnar:
7  *
8  *  Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9  *  Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
10  *
11  * Code for /proc/lockdep and /proc/lockdep_stats:
12  *
13  */
14 #include <linux/module.h>
15 #include <linux/proc_fs.h>
16 #include <linux/seq_file.h>
17 #include <linux/kallsyms.h>
18 #include <linux/debug_locks.h>
19 #include <linux/vmalloc.h>
20 #include <linux/sort.h>
21 #include <asm/uaccess.h>
22 #include <asm/div64.h>
23
24 #include "lockdep_internals.h"
25
26 static void *l_next(struct seq_file *m, void *v, loff_t *pos)
27 {
28         struct lock_class *class = v;
29
30         (*pos)++;
31
32         if (class->lock_entry.next != &all_lock_classes)
33                 class = list_entry(class->lock_entry.next, struct lock_class,
34                                   lock_entry);
35         else
36                 class = NULL;
37         m->private = class;
38
39         return class;
40 }
41
42 static void *l_start(struct seq_file *m, loff_t *pos)
43 {
44         struct lock_class *class = m->private;
45
46         if (&class->lock_entry == all_lock_classes.next)
47                 seq_printf(m, "all lock classes:\n");
48
49         return class;
50 }
51
52 static void l_stop(struct seq_file *m, void *v)
53 {
54 }
55
56 static unsigned long count_forward_deps(struct lock_class *class)
57 {
58         struct lock_list *entry;
59         unsigned long ret = 1;
60
61         /*
62          * Recurse this class's dependency list:
63          */
64         list_for_each_entry(entry, &class->locks_after, entry)
65                 ret += count_forward_deps(entry->class);
66
67         return ret;
68 }
69
70 static unsigned long count_backward_deps(struct lock_class *class)
71 {
72         struct lock_list *entry;
73         unsigned long ret = 1;
74
75         /*
76          * Recurse this class's dependency list:
77          */
78         list_for_each_entry(entry, &class->locks_before, entry)
79                 ret += count_backward_deps(entry->class);
80
81         return ret;
82 }
83
84 static void print_name(struct seq_file *m, struct lock_class *class)
85 {
86         char str[128];
87         const char *name = class->name;
88
89         if (!name) {
90                 name = __get_key_name(class->key, str);
91                 seq_printf(m, "%s", name);
92         } else{
93                 seq_printf(m, "%s", name);
94                 if (class->name_version > 1)
95                         seq_printf(m, "#%d", class->name_version);
96                 if (class->subclass)
97                         seq_printf(m, "/%d", class->subclass);
98         }
99 }
100
101 static int l_show(struct seq_file *m, void *v)
102 {
103         unsigned long nr_forward_deps, nr_backward_deps;
104         struct lock_class *class = m->private;
105         struct lock_list *entry;
106         char c1, c2, c3, c4;
107
108         seq_printf(m, "%p", class->key);
109 #ifdef CONFIG_DEBUG_LOCKDEP
110         seq_printf(m, " OPS:%8ld", class->ops);
111 #endif
112         nr_forward_deps = count_forward_deps(class);
113         seq_printf(m, " FD:%5ld", nr_forward_deps);
114
115         nr_backward_deps = count_backward_deps(class);
116         seq_printf(m, " BD:%5ld", nr_backward_deps);
117
118         get_usage_chars(class, &c1, &c2, &c3, &c4);
119         seq_printf(m, " %c%c%c%c", c1, c2, c3, c4);
120
121         seq_printf(m, ": ");
122         print_name(m, class);
123         seq_puts(m, "\n");
124
125         list_for_each_entry(entry, &class->locks_after, entry) {
126                 if (entry->distance == 1) {
127                         seq_printf(m, " -> [%p] ", entry->class);
128                         print_name(m, entry->class);
129                         seq_puts(m, "\n");
130                 }
131         }
132         seq_puts(m, "\n");
133
134         return 0;
135 }
136
137 static const struct seq_operations lockdep_ops = {
138         .start  = l_start,
139         .next   = l_next,
140         .stop   = l_stop,
141         .show   = l_show,
142 };
143
144 static int lockdep_open(struct inode *inode, struct file *file)
145 {
146         int res = seq_open(file, &lockdep_ops);
147         if (!res) {
148                 struct seq_file *m = file->private_data;
149
150                 if (!list_empty(&all_lock_classes))
151                         m->private = list_entry(all_lock_classes.next,
152                                         struct lock_class, lock_entry);
153                 else
154                         m->private = NULL;
155         }
156         return res;
157 }
158
159 static const struct file_operations proc_lockdep_operations = {
160         .open           = lockdep_open,
161         .read           = seq_read,
162         .llseek         = seq_lseek,
163         .release        = seq_release,
164 };
165
166 static void lockdep_stats_debug_show(struct seq_file *m)
167 {
168 #ifdef CONFIG_DEBUG_LOCKDEP
169         unsigned int hi1 = debug_atomic_read(&hardirqs_on_events),
170                      hi2 = debug_atomic_read(&hardirqs_off_events),
171                      hr1 = debug_atomic_read(&redundant_hardirqs_on),
172                      hr2 = debug_atomic_read(&redundant_hardirqs_off),
173                      si1 = debug_atomic_read(&softirqs_on_events),
174                      si2 = debug_atomic_read(&softirqs_off_events),
175                      sr1 = debug_atomic_read(&redundant_softirqs_on),
176                      sr2 = debug_atomic_read(&redundant_softirqs_off);
177
178         seq_printf(m, " chain lookup misses:           %11u\n",
179                 debug_atomic_read(&chain_lookup_misses));
180         seq_printf(m, " chain lookup hits:             %11u\n",
181                 debug_atomic_read(&chain_lookup_hits));
182         seq_printf(m, " cyclic checks:                 %11u\n",
183                 debug_atomic_read(&nr_cyclic_checks));
184         seq_printf(m, " cyclic-check recursions:       %11u\n",
185                 debug_atomic_read(&nr_cyclic_check_recursions));
186         seq_printf(m, " find-mask forwards checks:     %11u\n",
187                 debug_atomic_read(&nr_find_usage_forwards_checks));
188         seq_printf(m, " find-mask forwards recursions: %11u\n",
189                 debug_atomic_read(&nr_find_usage_forwards_recursions));
190         seq_printf(m, " find-mask backwards checks:    %11u\n",
191                 debug_atomic_read(&nr_find_usage_backwards_checks));
192         seq_printf(m, " find-mask backwards recursions:%11u\n",
193                 debug_atomic_read(&nr_find_usage_backwards_recursions));
194
195         seq_printf(m, " hardirq on events:             %11u\n", hi1);
196         seq_printf(m, " hardirq off events:            %11u\n", hi2);
197         seq_printf(m, " redundant hardirq ons:         %11u\n", hr1);
198         seq_printf(m, " redundant hardirq offs:        %11u\n", hr2);
199         seq_printf(m, " softirq on events:             %11u\n", si1);
200         seq_printf(m, " softirq off events:            %11u\n", si2);
201         seq_printf(m, " redundant softirq ons:         %11u\n", sr1);
202         seq_printf(m, " redundant softirq offs:        %11u\n", sr2);
203 #endif
204 }
205
206 static int lockdep_stats_show(struct seq_file *m, void *v)
207 {
208         struct lock_class *class;
209         unsigned long nr_unused = 0, nr_uncategorized = 0,
210                       nr_irq_safe = 0, nr_irq_unsafe = 0,
211                       nr_softirq_safe = 0, nr_softirq_unsafe = 0,
212                       nr_hardirq_safe = 0, nr_hardirq_unsafe = 0,
213                       nr_irq_read_safe = 0, nr_irq_read_unsafe = 0,
214                       nr_softirq_read_safe = 0, nr_softirq_read_unsafe = 0,
215                       nr_hardirq_read_safe = 0, nr_hardirq_read_unsafe = 0,
216                       sum_forward_deps = 0, factor = 0;
217
218         list_for_each_entry(class, &all_lock_classes, lock_entry) {
219
220                 if (class->usage_mask == 0)
221                         nr_unused++;
222                 if (class->usage_mask == LOCKF_USED)
223                         nr_uncategorized++;
224                 if (class->usage_mask & LOCKF_USED_IN_IRQ)
225                         nr_irq_safe++;
226                 if (class->usage_mask & LOCKF_ENABLED_IRQS)
227                         nr_irq_unsafe++;
228                 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
229                         nr_softirq_safe++;
230                 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS)
231                         nr_softirq_unsafe++;
232                 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
233                         nr_hardirq_safe++;
234                 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS)
235                         nr_hardirq_unsafe++;
236                 if (class->usage_mask & LOCKF_USED_IN_IRQ_READ)
237                         nr_irq_read_safe++;
238                 if (class->usage_mask & LOCKF_ENABLED_IRQS_READ)
239                         nr_irq_read_unsafe++;
240                 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ)
241                         nr_softirq_read_safe++;
242                 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
243                         nr_softirq_read_unsafe++;
244                 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ)
245                         nr_hardirq_read_safe++;
246                 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
247                         nr_hardirq_read_unsafe++;
248
249                 sum_forward_deps += count_forward_deps(class);
250         }
251 #ifdef CONFIG_DEBUG_LOCKDEP
252         DEBUG_LOCKS_WARN_ON(debug_atomic_read(&nr_unused_locks) != nr_unused);
253 #endif
254         seq_printf(m, " lock-classes:                  %11lu [max: %lu]\n",
255                         nr_lock_classes, MAX_LOCKDEP_KEYS);
256         seq_printf(m, " direct dependencies:           %11lu [max: %lu]\n",
257                         nr_list_entries, MAX_LOCKDEP_ENTRIES);
258         seq_printf(m, " indirect dependencies:         %11lu\n",
259                         sum_forward_deps);
260
261         /*
262          * Total number of dependencies:
263          *
264          * All irq-safe locks may nest inside irq-unsafe locks,
265          * plus all the other known dependencies:
266          */
267         seq_printf(m, " all direct dependencies:       %11lu\n",
268                         nr_irq_unsafe * nr_irq_safe +
269                         nr_hardirq_unsafe * nr_hardirq_safe +
270                         nr_list_entries);
271
272         /*
273          * Estimated factor between direct and indirect
274          * dependencies:
275          */
276         if (nr_list_entries)
277                 factor = sum_forward_deps / nr_list_entries;
278
279 #ifdef CONFIG_PROVE_LOCKING
280         seq_printf(m, " dependency chains:             %11lu [max: %lu]\n",
281                         nr_lock_chains, MAX_LOCKDEP_CHAINS);
282 #endif
283
284 #ifdef CONFIG_TRACE_IRQFLAGS
285         seq_printf(m, " in-hardirq chains:             %11u\n",
286                         nr_hardirq_chains);
287         seq_printf(m, " in-softirq chains:             %11u\n",
288                         nr_softirq_chains);
289 #endif
290         seq_printf(m, " in-process chains:             %11u\n",
291                         nr_process_chains);
292         seq_printf(m, " stack-trace entries:           %11lu [max: %lu]\n",
293                         nr_stack_trace_entries, MAX_STACK_TRACE_ENTRIES);
294         seq_printf(m, " combined max dependencies:     %11u\n",
295                         (nr_hardirq_chains + 1) *
296                         (nr_softirq_chains + 1) *
297                         (nr_process_chains + 1)
298         );
299         seq_printf(m, " hardirq-safe locks:            %11lu\n",
300                         nr_hardirq_safe);
301         seq_printf(m, " hardirq-unsafe locks:          %11lu\n",
302                         nr_hardirq_unsafe);
303         seq_printf(m, " softirq-safe locks:            %11lu\n",
304                         nr_softirq_safe);
305         seq_printf(m, " softirq-unsafe locks:          %11lu\n",
306                         nr_softirq_unsafe);
307         seq_printf(m, " irq-safe locks:                %11lu\n",
308                         nr_irq_safe);
309         seq_printf(m, " irq-unsafe locks:              %11lu\n",
310                         nr_irq_unsafe);
311
312         seq_printf(m, " hardirq-read-safe locks:       %11lu\n",
313                         nr_hardirq_read_safe);
314         seq_printf(m, " hardirq-read-unsafe locks:     %11lu\n",
315                         nr_hardirq_read_unsafe);
316         seq_printf(m, " softirq-read-safe locks:       %11lu\n",
317                         nr_softirq_read_safe);
318         seq_printf(m, " softirq-read-unsafe locks:     %11lu\n",
319                         nr_softirq_read_unsafe);
320         seq_printf(m, " irq-read-safe locks:           %11lu\n",
321                         nr_irq_read_safe);
322         seq_printf(m, " irq-read-unsafe locks:         %11lu\n",
323                         nr_irq_read_unsafe);
324
325         seq_printf(m, " uncategorized locks:           %11lu\n",
326                         nr_uncategorized);
327         seq_printf(m, " unused locks:                  %11lu\n",
328                         nr_unused);
329         seq_printf(m, " max locking depth:             %11u\n",
330                         max_lockdep_depth);
331         seq_printf(m, " max recursion depth:           %11u\n",
332                         max_recursion_depth);
333         lockdep_stats_debug_show(m);
334         seq_printf(m, " debug_locks:                   %11u\n",
335                         debug_locks);
336
337         return 0;
338 }
339
340 static int lockdep_stats_open(struct inode *inode, struct file *file)
341 {
342         return single_open(file, lockdep_stats_show, NULL);
343 }
344
345 static const struct file_operations proc_lockdep_stats_operations = {
346         .open           = lockdep_stats_open,
347         .read           = seq_read,
348         .llseek         = seq_lseek,
349         .release        = seq_release,
350 };
351
352 #ifdef CONFIG_LOCK_STAT
353
354 struct lock_stat_data {
355         struct lock_class *class;
356         struct lock_class_stats stats;
357 };
358
359 struct lock_stat_seq {
360         struct lock_stat_data *iter;
361         struct lock_stat_data *iter_end;
362         struct lock_stat_data stats[MAX_LOCKDEP_KEYS];
363 };
364
365 /*
366  * sort on absolute number of contentions
367  */
368 static int lock_stat_cmp(const void *l, const void *r)
369 {
370         const struct lock_stat_data *dl = l, *dr = r;
371         unsigned long nl, nr;
372
373         nl = dl->stats.read_waittime.nr + dl->stats.write_waittime.nr;
374         nr = dr->stats.read_waittime.nr + dr->stats.write_waittime.nr;
375
376         return nr - nl;
377 }
378
379 static void seq_line(struct seq_file *m, char c, int offset, int length)
380 {
381         int i;
382
383         for (i = 0; i < offset; i++)
384                 seq_puts(m, " ");
385         for (i = 0; i < length; i++)
386                 seq_printf(m, "%c", c);
387         seq_puts(m, "\n");
388 }
389
390 static void snprint_time(char *buf, size_t bufsiz, s64 nr)
391 {
392         unsigned long rem;
393
394         rem = do_div(nr, 1000); /* XXX: do_div_signed */
395         snprintf(buf, bufsiz, "%lld.%02d", (long long)nr, ((int)rem+5)/10);
396 }
397
398 static void seq_time(struct seq_file *m, s64 time)
399 {
400         char num[15];
401
402         snprint_time(num, sizeof(num), time);
403         seq_printf(m, " %14s", num);
404 }
405
406 static void seq_lock_time(struct seq_file *m, struct lock_time *lt)
407 {
408         seq_printf(m, "%14lu", lt->nr);
409         seq_time(m, lt->min);
410         seq_time(m, lt->max);
411         seq_time(m, lt->total);
412 }
413
414 static void seq_stats(struct seq_file *m, struct lock_stat_data *data)
415 {
416         char name[39];
417         struct lock_class *class;
418         struct lock_class_stats *stats;
419         int i, namelen;
420
421         class = data->class;
422         stats = &data->stats;
423
424         snprintf(name, 38, "%s", class->name);
425         namelen = strlen(name);
426
427         if (stats->write_holdtime.nr) {
428                 if (stats->read_holdtime.nr)
429                         seq_printf(m, "%38s-W:", name);
430                 else
431                         seq_printf(m, "%40s:", name);
432
433                 seq_printf(m, "%14lu ", stats->bounces[bounce_contended_write]);
434                 seq_lock_time(m, &stats->write_waittime);
435                 seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_write]);
436                 seq_lock_time(m, &stats->write_holdtime);
437                 seq_puts(m, "\n");
438         }
439
440         if (stats->read_holdtime.nr) {
441                 seq_printf(m, "%38s-R:", name);
442                 seq_printf(m, "%14lu ", stats->bounces[bounce_contended_read]);
443                 seq_lock_time(m, &stats->read_waittime);
444                 seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_read]);
445                 seq_lock_time(m, &stats->read_holdtime);
446                 seq_puts(m, "\n");
447         }
448
449         if (stats->read_waittime.nr + stats->write_waittime.nr == 0)
450                 return;
451
452         if (stats->read_holdtime.nr)
453                 namelen += 2;
454
455         for (i = 0; i < ARRAY_SIZE(class->contention_point); i++) {
456                 char sym[KSYM_SYMBOL_LEN];
457                 char ip[32];
458
459                 if (class->contention_point[i] == 0)
460                         break;
461
462                 if (!i)
463                         seq_line(m, '-', 40-namelen, namelen);
464
465                 sprint_symbol(sym, class->contention_point[i]);
466                 snprintf(ip, sizeof(ip), "[<%p>]",
467                                 (void *)class->contention_point[i]);
468                 seq_printf(m, "%40s %14lu %29s %s\n", name,
469                                 stats->contention_point[i],
470                                 ip, sym);
471         }
472         if (i) {
473                 seq_puts(m, "\n");
474                 seq_line(m, '.', 0, 40 + 1 + 10 * (14 + 1));
475                 seq_puts(m, "\n");
476         }
477 }
478
479 static void seq_header(struct seq_file *m)
480 {
481         seq_printf(m, "lock_stat version 0.2\n");
482         seq_line(m, '-', 0, 40 + 1 + 10 * (14 + 1));
483         seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s "
484                         "%14s %14s\n",
485                         "class name",
486                         "con-bounces",
487                         "contentions",
488                         "waittime-min",
489                         "waittime-max",
490                         "waittime-total",
491                         "acq-bounces",
492                         "acquisitions",
493                         "holdtime-min",
494                         "holdtime-max",
495                         "holdtime-total");
496         seq_line(m, '-', 0, 40 + 1 + 10 * (14 + 1));
497         seq_printf(m, "\n");
498 }
499
500 static void *ls_start(struct seq_file *m, loff_t *pos)
501 {
502         struct lock_stat_seq *data = m->private;
503
504         if (data->iter == data->stats)
505                 seq_header(m);
506
507         if (data->iter == data->iter_end)
508                 data->iter = NULL;
509
510         return data->iter;
511 }
512
513 static void *ls_next(struct seq_file *m, void *v, loff_t *pos)
514 {
515         struct lock_stat_seq *data = m->private;
516
517         (*pos)++;
518
519         data->iter = v;
520         data->iter++;
521         if (data->iter == data->iter_end)
522                 data->iter = NULL;
523
524         return data->iter;
525 }
526
527 static void ls_stop(struct seq_file *m, void *v)
528 {
529 }
530
531 static int ls_show(struct seq_file *m, void *v)
532 {
533         struct lock_stat_seq *data = m->private;
534
535         seq_stats(m, data->iter);
536         return 0;
537 }
538
539 static struct seq_operations lockstat_ops = {
540         .start  = ls_start,
541         .next   = ls_next,
542         .stop   = ls_stop,
543         .show   = ls_show,
544 };
545
546 static int lock_stat_open(struct inode *inode, struct file *file)
547 {
548         int res;
549         struct lock_class *class;
550         struct lock_stat_seq *data = vmalloc(sizeof(struct lock_stat_seq));
551
552         if (!data)
553                 return -ENOMEM;
554
555         res = seq_open(file, &lockstat_ops);
556         if (!res) {
557                 struct lock_stat_data *iter = data->stats;
558                 struct seq_file *m = file->private_data;
559
560                 data->iter = iter;
561                 list_for_each_entry(class, &all_lock_classes, lock_entry) {
562                         iter->class = class;
563                         iter->stats = lock_stats(class);
564                         iter++;
565                 }
566                 data->iter_end = iter;
567
568                 sort(data->stats, data->iter_end - data->iter,
569                                 sizeof(struct lock_stat_data),
570                                 lock_stat_cmp, NULL);
571
572                 m->private = data;
573         } else
574                 vfree(data);
575
576         return res;
577 }
578
579 static ssize_t lock_stat_write(struct file *file, const char __user *buf,
580                                size_t count, loff_t *ppos)
581 {
582         struct lock_class *class;
583         char c;
584
585         if (count) {
586                 if (get_user(c, buf))
587                         return -EFAULT;
588
589                 if (c != '0')
590                         return count;
591
592                 list_for_each_entry(class, &all_lock_classes, lock_entry)
593                         clear_lock_stats(class);
594         }
595         return count;
596 }
597
598 static int lock_stat_release(struct inode *inode, struct file *file)
599 {
600         struct seq_file *seq = file->private_data;
601
602         vfree(seq->private);
603         seq->private = NULL;
604         return seq_release(inode, file);
605 }
606
607 static const struct file_operations proc_lock_stat_operations = {
608         .open           = lock_stat_open,
609         .write          = lock_stat_write,
610         .read           = seq_read,
611         .llseek         = seq_lseek,
612         .release        = lock_stat_release,
613 };
614 #endif /* CONFIG_LOCK_STAT */
615
616 static int __init lockdep_proc_init(void)
617 {
618         struct proc_dir_entry *entry;
619
620         entry = create_proc_entry("lockdep", S_IRUSR, NULL);
621         if (entry)
622                 entry->proc_fops = &proc_lockdep_operations;
623
624         entry = create_proc_entry("lockdep_stats", S_IRUSR, NULL);
625         if (entry)
626                 entry->proc_fops = &proc_lockdep_stats_operations;
627
628 #ifdef CONFIG_LOCK_STAT
629         entry = create_proc_entry("lock_stat", S_IRUSR, NULL);
630         if (entry)
631                 entry->proc_fops = &proc_lock_stat_operations;
632 #endif
633
634         return 0;
635 }
636
637 __initcall(lockdep_proc_init);
638