latencytop.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * latencytop.c: Latency display infrastructure
  3. *
  4. * (C) Copyright 2008 Intel Corporation
  5. * Author: Arjan van de Ven <arjan@linux.intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; version 2
  10. * of the License.
  11. */
  12. #include <linux/latencytop.h>
  13. #include <linux/kallsyms.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/notifier.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/proc_fs.h>
  18. #include <linux/module.h>
  19. #include <linux/sched.h>
  20. #include <linux/list.h>
  21. #include <linux/slab.h>
  22. #include <linux/stacktrace.h>
  23. static DEFINE_SPINLOCK(latency_lock);
  24. #define MAXLR 128
  25. static struct latency_record latency_record[MAXLR];
  26. int latencytop_enabled;
  27. void clear_all_latency_tracing(struct task_struct *p)
  28. {
  29. unsigned long flags;
  30. if (!latencytop_enabled)
  31. return;
  32. spin_lock_irqsave(&latency_lock, flags);
  33. memset(&p->latency_record, 0, sizeof(p->latency_record));
  34. p->latency_record_count = 0;
  35. spin_unlock_irqrestore(&latency_lock, flags);
  36. }
  37. static void clear_global_latency_tracing(void)
  38. {
  39. unsigned long flags;
  40. spin_lock_irqsave(&latency_lock, flags);
  41. memset(&latency_record, 0, sizeof(latency_record));
  42. spin_unlock_irqrestore(&latency_lock, flags);
  43. }
  44. static void __sched
  45. account_global_scheduler_latency(struct task_struct *tsk, struct latency_record *lat)
  46. {
  47. int firstnonnull = MAXLR + 1;
  48. int i;
  49. if (!latencytop_enabled)
  50. return;
  51. /* skip kernel threads for now */
  52. if (!tsk->mm)
  53. return;
  54. for (i = 0; i < MAXLR; i++) {
  55. int q, same = 1;
  56. /* Nothing stored: */
  57. if (!latency_record[i].backtrace[0]) {
  58. if (firstnonnull > i)
  59. firstnonnull = i;
  60. continue;
  61. }
  62. for (q = 0 ; q < LT_BACKTRACEDEPTH ; q++) {
  63. unsigned long record = lat->backtrace[q];
  64. if (latency_record[i].backtrace[q] != record) {
  65. same = 0;
  66. break;
  67. }
  68. /* 0 and ULONG_MAX entries mean end of backtrace: */
  69. if (record == 0 || record == ULONG_MAX)
  70. break;
  71. }
  72. if (same) {
  73. latency_record[i].count++;
  74. latency_record[i].time += lat->time;
  75. if (lat->time > latency_record[i].max)
  76. latency_record[i].max = lat->time;
  77. return;
  78. }
  79. }
  80. i = firstnonnull;
  81. if (i >= MAXLR - 1)
  82. return;
  83. /* Allocted a new one: */
  84. memcpy(&latency_record[i], lat, sizeof(struct latency_record));
  85. }
  86. static inline void store_stacktrace(struct task_struct *tsk, struct latency_record *lat)
  87. {
  88. struct stack_trace trace;
  89. memset(&trace, 0, sizeof(trace));
  90. trace.max_entries = LT_BACKTRACEDEPTH;
  91. trace.entries = &lat->backtrace[0];
  92. trace.skip = 0;
  93. save_stack_trace_tsk(tsk, &trace);
  94. }
  95. void __sched
  96. account_scheduler_latency(struct task_struct *tsk, int usecs, int inter)
  97. {
  98. unsigned long flags;
  99. int i, q;
  100. struct latency_record lat;
  101. if (!latencytop_enabled)
  102. return;
  103. /* Long interruptible waits are generally user requested... */
  104. if (inter && usecs > 5000)
  105. return;
  106. memset(&lat, 0, sizeof(lat));
  107. lat.count = 1;
  108. lat.time = usecs;
  109. lat.max = usecs;
  110. store_stacktrace(tsk, &lat);
  111. spin_lock_irqsave(&latency_lock, flags);
  112. account_global_scheduler_latency(tsk, &lat);
  113. /*
  114. * short term hack; if we're > 32 we stop; future we recycle:
  115. */
  116. tsk->latency_record_count++;
  117. if (tsk->latency_record_count >= LT_SAVECOUNT)
  118. goto out_unlock;
  119. for (i = 0; i < LT_SAVECOUNT ; i++) {
  120. struct latency_record *mylat;
  121. int same = 1;
  122. mylat = &tsk->latency_record[i];
  123. for (q = 0 ; q < LT_BACKTRACEDEPTH ; q++) {
  124. unsigned long record = lat.backtrace[q];
  125. if (mylat->backtrace[q] != record) {
  126. same = 0;
  127. break;
  128. }
  129. /* 0 and ULONG_MAX entries mean end of backtrace: */
  130. if (record == 0 || record == ULONG_MAX)
  131. break;
  132. }
  133. if (same) {
  134. mylat->count++;
  135. mylat->time += lat.time;
  136. if (lat.time > mylat->max)
  137. mylat->max = lat.time;
  138. goto out_unlock;
  139. }
  140. }
  141. /* Allocated a new one: */
  142. i = tsk->latency_record_count;
  143. memcpy(&tsk->latency_record[i], &lat, sizeof(struct latency_record));
  144. out_unlock:
  145. spin_unlock_irqrestore(&latency_lock, flags);
  146. }
  147. static int lstats_show(struct seq_file *m, void *v)
  148. {
  149. int i;
  150. seq_puts(m, "Latency Top version : v0.1\n");
  151. for (i = 0; i < MAXLR; i++) {
  152. if (latency_record[i].backtrace[0]) {
  153. int q;
  154. seq_printf(m, "%i %li %li ",
  155. latency_record[i].count,
  156. latency_record[i].time,
  157. latency_record[i].max);
  158. for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
  159. char sym[KSYM_SYMBOL_LEN];
  160. char *c;
  161. if (!latency_record[i].backtrace[q])
  162. break;
  163. if (latency_record[i].backtrace[q] == ULONG_MAX)
  164. break;
  165. sprint_symbol(sym, latency_record[i].backtrace[q]);
  166. c = strchr(sym, '+');
  167. if (c)
  168. *c = 0;
  169. seq_printf(m, "%s ", sym);
  170. }
  171. seq_printf(m, "\n");
  172. }
  173. }
  174. return 0;
  175. }
  176. static ssize_t
  177. lstats_write(struct file *file, const char __user *buf, size_t count,
  178. loff_t *offs)
  179. {
  180. clear_global_latency_tracing();
  181. return count;
  182. }
  183. static int lstats_open(struct inode *inode, struct file *filp)
  184. {
  185. return single_open(filp, lstats_show, NULL);
  186. }
  187. static struct file_operations lstats_fops = {
  188. .open = lstats_open,
  189. .read = seq_read,
  190. .write = lstats_write,
  191. .llseek = seq_lseek,
  192. .release = single_release,
  193. };
  194. static int __init init_lstats_procfs(void)
  195. {
  196. proc_create("latency_stats", 0644, NULL, &lstats_fops);
  197. return 0;
  198. }
  199. __initcall(init_lstats_procfs);