latencytop.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. /*
  13. * CONFIG_LATENCYTOP enables a kernel latency tracking infrastructure that is
  14. * used by the "latencytop" userspace tool. The latency that is tracked is not
  15. * the 'traditional' interrupt latency (which is primarily caused by something
  16. * else consuming CPU), but instead, it is the latency an application encounters
  17. * because the kernel sleeps on its behalf for various reasons.
  18. *
  19. * This code tracks 2 levels of statistics:
  20. * 1) System level latency
  21. * 2) Per process latency
  22. *
  23. * The latency is stored in fixed sized data structures in an accumulated form;
  24. * if the "same" latency cause is hit twice, this will be tracked as one entry
  25. * in the data structure. Both the count, total accumulated latency and maximum
  26. * latency are tracked in this data structure. When the fixed size structure is
  27. * full, no new causes are tracked until the buffer is flushed by writing to
  28. * the /proc file; the userspace tool does this on a regular basis.
  29. *
  30. * A latency cause is identified by a stringified backtrace at the point that
  31. * the scheduler gets invoked. The userland tool will use this string to
  32. * identify the cause of the latency in human readable form.
  33. *
  34. * The information is exported via /proc/latency_stats and /proc/<pid>/latency.
  35. * These files look like this:
  36. *
  37. * Latency Top version : v0.1
  38. * 70 59433 4897 i915_irq_wait drm_ioctl vfs_ioctl do_vfs_ioctl sys_ioctl
  39. * | | | |
  40. * | | | +----> the stringified backtrace
  41. * | | +---------> The maximum latency for this entry in microseconds
  42. * | +--------------> The accumulated latency for this entry (microseconds)
  43. * +-------------------> The number of times this entry is hit
  44. *
  45. * (note: the average latency is the accumulated latency divided by the number
  46. * of times)
  47. */
  48. #include <linux/latencytop.h>
  49. #include <linux/kallsyms.h>
  50. #include <linux/seq_file.h>
  51. #include <linux/notifier.h>
  52. #include <linux/spinlock.h>
  53. #include <linux/proc_fs.h>
  54. #include <linux/module.h>
  55. #include <linux/sched.h>
  56. #include <linux/list.h>
  57. #include <linux/stacktrace.h>
  58. static DEFINE_SPINLOCK(latency_lock);
  59. #define MAXLR 128
  60. static struct latency_record latency_record[MAXLR];
  61. int latencytop_enabled;
  62. void clear_all_latency_tracing(struct task_struct *p)
  63. {
  64. unsigned long flags;
  65. if (!latencytop_enabled)
  66. return;
  67. spin_lock_irqsave(&latency_lock, flags);
  68. memset(&p->latency_record, 0, sizeof(p->latency_record));
  69. p->latency_record_count = 0;
  70. spin_unlock_irqrestore(&latency_lock, flags);
  71. }
  72. static void clear_global_latency_tracing(void)
  73. {
  74. unsigned long flags;
  75. spin_lock_irqsave(&latency_lock, flags);
  76. memset(&latency_record, 0, sizeof(latency_record));
  77. spin_unlock_irqrestore(&latency_lock, flags);
  78. }
  79. static void __sched
  80. account_global_scheduler_latency(struct task_struct *tsk, struct latency_record *lat)
  81. {
  82. int firstnonnull = MAXLR + 1;
  83. int i;
  84. if (!latencytop_enabled)
  85. return;
  86. /* skip kernel threads for now */
  87. if (!tsk->mm)
  88. return;
  89. for (i = 0; i < MAXLR; i++) {
  90. int q, same = 1;
  91. /* Nothing stored: */
  92. if (!latency_record[i].backtrace[0]) {
  93. if (firstnonnull > i)
  94. firstnonnull = i;
  95. continue;
  96. }
  97. for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
  98. unsigned long record = lat->backtrace[q];
  99. if (latency_record[i].backtrace[q] != record) {
  100. same = 0;
  101. break;
  102. }
  103. /* 0 and ULONG_MAX entries mean end of backtrace: */
  104. if (record == 0 || record == ULONG_MAX)
  105. break;
  106. }
  107. if (same) {
  108. latency_record[i].count++;
  109. latency_record[i].time += lat->time;
  110. if (lat->time > latency_record[i].max)
  111. latency_record[i].max = lat->time;
  112. return;
  113. }
  114. }
  115. i = firstnonnull;
  116. if (i >= MAXLR - 1)
  117. return;
  118. /* Allocted a new one: */
  119. memcpy(&latency_record[i], lat, sizeof(struct latency_record));
  120. }
  121. /*
  122. * Iterator to store a backtrace into a latency record entry
  123. */
  124. static inline void store_stacktrace(struct task_struct *tsk,
  125. struct latency_record *lat)
  126. {
  127. struct stack_trace trace;
  128. memset(&trace, 0, sizeof(trace));
  129. trace.max_entries = LT_BACKTRACEDEPTH;
  130. trace.entries = &lat->backtrace[0];
  131. save_stack_trace_tsk(tsk, &trace);
  132. }
  133. /**
  134. * __account_scheduler_latency - record an occurred latency
  135. * @tsk - the task struct of the task hitting the latency
  136. * @usecs - the duration of the latency in microseconds
  137. * @inter - 1 if the sleep was interruptible, 0 if uninterruptible
  138. *
  139. * This function is the main entry point for recording latency entries
  140. * as called by the scheduler.
  141. *
  142. * This function has a few special cases to deal with normal 'non-latency'
  143. * sleeps: specifically, interruptible sleep longer than 5 msec is skipped
  144. * since this usually is caused by waiting for events via select() and co.
  145. *
  146. * Negative latencies (caused by time going backwards) are also explicitly
  147. * skipped.
  148. */
  149. void __sched
  150. __account_scheduler_latency(struct task_struct *tsk, int usecs, int inter)
  151. {
  152. unsigned long flags;
  153. int i, q;
  154. struct latency_record lat;
  155. /* Long interruptible waits are generally user requested... */
  156. if (inter && usecs > 5000)
  157. return;
  158. /* Negative sleeps are time going backwards */
  159. /* Zero-time sleeps are non-interesting */
  160. if (usecs <= 0)
  161. return;
  162. memset(&lat, 0, sizeof(lat));
  163. lat.count = 1;
  164. lat.time = usecs;
  165. lat.max = usecs;
  166. store_stacktrace(tsk, &lat);
  167. spin_lock_irqsave(&latency_lock, flags);
  168. account_global_scheduler_latency(tsk, &lat);
  169. for (i = 0; i < tsk->latency_record_count; i++) {
  170. struct latency_record *mylat;
  171. int same = 1;
  172. mylat = &tsk->latency_record[i];
  173. for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
  174. unsigned long record = lat.backtrace[q];
  175. if (mylat->backtrace[q] != record) {
  176. same = 0;
  177. break;
  178. }
  179. /* 0 and ULONG_MAX entries mean end of backtrace: */
  180. if (record == 0 || record == ULONG_MAX)
  181. break;
  182. }
  183. if (same) {
  184. mylat->count++;
  185. mylat->time += lat.time;
  186. if (lat.time > mylat->max)
  187. mylat->max = lat.time;
  188. goto out_unlock;
  189. }
  190. }
  191. /*
  192. * short term hack; if we're > 32 we stop; future we recycle:
  193. */
  194. if (tsk->latency_record_count >= LT_SAVECOUNT)
  195. goto out_unlock;
  196. /* Allocated a new one: */
  197. i = tsk->latency_record_count++;
  198. memcpy(&tsk->latency_record[i], &lat, sizeof(struct latency_record));
  199. out_unlock:
  200. spin_unlock_irqrestore(&latency_lock, flags);
  201. }
  202. static int lstats_show(struct seq_file *m, void *v)
  203. {
  204. int i;
  205. seq_puts(m, "Latency Top version : v0.1\n");
  206. for (i = 0; i < MAXLR; i++) {
  207. struct latency_record *lr = &latency_record[i];
  208. if (lr->backtrace[0]) {
  209. int q;
  210. seq_printf(m, "%i %lu %lu",
  211. lr->count, lr->time, lr->max);
  212. for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
  213. unsigned long bt = lr->backtrace[q];
  214. if (!bt)
  215. break;
  216. if (bt == ULONG_MAX)
  217. break;
  218. seq_printf(m, " %ps", (void *)bt);
  219. }
  220. seq_printf(m, "\n");
  221. }
  222. }
  223. return 0;
  224. }
  225. static ssize_t
  226. lstats_write(struct file *file, const char __user *buf, size_t count,
  227. loff_t *offs)
  228. {
  229. clear_global_latency_tracing();
  230. return count;
  231. }
  232. static int lstats_open(struct inode *inode, struct file *filp)
  233. {
  234. return single_open(filp, lstats_show, NULL);
  235. }
  236. static const struct file_operations lstats_fops = {
  237. .open = lstats_open,
  238. .read = seq_read,
  239. .write = lstats_write,
  240. .llseek = seq_lseek,
  241. .release = single_release,
  242. };
  243. static int __init init_lstats_procfs(void)
  244. {
  245. proc_create("latency_stats", 0644, NULL, &lstats_fops);
  246. return 0;
  247. }
  248. device_initcall(init_lstats_procfs);