latencytop.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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/slab.h>
  58. #include <linux/stacktrace.h>
  59. static DEFINE_SPINLOCK(latency_lock);
  60. #define MAXLR 128
  61. static struct latency_record latency_record[MAXLR];
  62. int latencytop_enabled;
  63. void clear_all_latency_tracing(struct task_struct *p)
  64. {
  65. unsigned long flags;
  66. if (!latencytop_enabled)
  67. return;
  68. spin_lock_irqsave(&latency_lock, flags);
  69. memset(&p->latency_record, 0, sizeof(p->latency_record));
  70. p->latency_record_count = 0;
  71. spin_unlock_irqrestore(&latency_lock, flags);
  72. }
  73. static void clear_global_latency_tracing(void)
  74. {
  75. unsigned long flags;
  76. spin_lock_irqsave(&latency_lock, flags);
  77. memset(&latency_record, 0, sizeof(latency_record));
  78. spin_unlock_irqrestore(&latency_lock, flags);
  79. }
  80. static void __sched
  81. account_global_scheduler_latency(struct task_struct *tsk, struct latency_record *lat)
  82. {
  83. int firstnonnull = MAXLR + 1;
  84. int i;
  85. if (!latencytop_enabled)
  86. return;
  87. /* skip kernel threads for now */
  88. if (!tsk->mm)
  89. return;
  90. for (i = 0; i < MAXLR; i++) {
  91. int q, same = 1;
  92. /* Nothing stored: */
  93. if (!latency_record[i].backtrace[0]) {
  94. if (firstnonnull > i)
  95. firstnonnull = i;
  96. continue;
  97. }
  98. for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
  99. unsigned long record = lat->backtrace[q];
  100. if (latency_record[i].backtrace[q] != record) {
  101. same = 0;
  102. break;
  103. }
  104. /* 0 and ULONG_MAX entries mean end of backtrace: */
  105. if (record == 0 || record == ULONG_MAX)
  106. break;
  107. }
  108. if (same) {
  109. latency_record[i].count++;
  110. latency_record[i].time += lat->time;
  111. if (lat->time > latency_record[i].max)
  112. latency_record[i].max = lat->time;
  113. return;
  114. }
  115. }
  116. i = firstnonnull;
  117. if (i >= MAXLR - 1)
  118. return;
  119. /* Allocted a new one: */
  120. memcpy(&latency_record[i], lat, sizeof(struct latency_record));
  121. }
  122. /*
  123. * Iterator to store a backtrace into a latency record entry
  124. */
  125. static inline void store_stacktrace(struct task_struct *tsk,
  126. struct latency_record *lat)
  127. {
  128. struct stack_trace trace;
  129. memset(&trace, 0, sizeof(trace));
  130. trace.max_entries = LT_BACKTRACEDEPTH;
  131. trace.entries = &lat->backtrace[0];
  132. save_stack_trace_tsk(tsk, &trace);
  133. }
  134. /**
  135. * __account_scheduler_latency - record an occured latency
  136. * @tsk - the task struct of the task hitting the latency
  137. * @usecs - the duration of the latency in microseconds
  138. * @inter - 1 if the sleep was interruptible, 0 if uninterruptible
  139. *
  140. * This function is the main entry point for recording latency entries
  141. * as called by the scheduler.
  142. *
  143. * This function has a few special cases to deal with normal 'non-latency'
  144. * sleeps: specifically, interruptible sleep longer than 5 msec is skipped
  145. * since this usually is caused by waiting for events via select() and co.
  146. *
  147. * Negative latencies (caused by time going backwards) are also explicitly
  148. * skipped.
  149. */
  150. void __sched
  151. __account_scheduler_latency(struct task_struct *tsk, int usecs, int inter)
  152. {
  153. unsigned long flags;
  154. int i, q;
  155. struct latency_record lat;
  156. /* Long interruptible waits are generally user requested... */
  157. if (inter && usecs > 5000)
  158. return;
  159. /* Negative sleeps are time going backwards */
  160. /* Zero-time sleeps are non-interesting */
  161. if (usecs <= 0)
  162. return;
  163. memset(&lat, 0, sizeof(lat));
  164. lat.count = 1;
  165. lat.time = usecs;
  166. lat.max = usecs;
  167. store_stacktrace(tsk, &lat);
  168. spin_lock_irqsave(&latency_lock, flags);
  169. account_global_scheduler_latency(tsk, &lat);
  170. /*
  171. * short term hack; if we're > 32 we stop; future we recycle:
  172. */
  173. tsk->latency_record_count++;
  174. if (tsk->latency_record_count >= LT_SAVECOUNT)
  175. goto out_unlock;
  176. for (i = 0; i < LT_SAVECOUNT; i++) {
  177. struct latency_record *mylat;
  178. int same = 1;
  179. mylat = &tsk->latency_record[i];
  180. for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
  181. unsigned long record = lat.backtrace[q];
  182. if (mylat->backtrace[q] != record) {
  183. same = 0;
  184. break;
  185. }
  186. /* 0 and ULONG_MAX entries mean end of backtrace: */
  187. if (record == 0 || record == ULONG_MAX)
  188. break;
  189. }
  190. if (same) {
  191. mylat->count++;
  192. mylat->time += lat.time;
  193. if (lat.time > mylat->max)
  194. mylat->max = lat.time;
  195. goto out_unlock;
  196. }
  197. }
  198. /* Allocated a new one: */
  199. i = tsk->latency_record_count;
  200. memcpy(&tsk->latency_record[i], &lat, sizeof(struct latency_record));
  201. out_unlock:
  202. spin_unlock_irqrestore(&latency_lock, flags);
  203. }
  204. static int lstats_show(struct seq_file *m, void *v)
  205. {
  206. int i;
  207. seq_puts(m, "Latency Top version : v0.1\n");
  208. for (i = 0; i < MAXLR; i++) {
  209. if (latency_record[i].backtrace[0]) {
  210. int q;
  211. seq_printf(m, "%i %lu %lu ",
  212. latency_record[i].count,
  213. latency_record[i].time,
  214. latency_record[i].max);
  215. for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
  216. char sym[KSYM_SYMBOL_LEN];
  217. char *c;
  218. if (!latency_record[i].backtrace[q])
  219. break;
  220. if (latency_record[i].backtrace[q] == ULONG_MAX)
  221. break;
  222. sprint_symbol(sym, latency_record[i].backtrace[q]);
  223. c = strchr(sym, '+');
  224. if (c)
  225. *c = 0;
  226. seq_printf(m, "%s ", sym);
  227. }
  228. seq_printf(m, "\n");
  229. }
  230. }
  231. return 0;
  232. }
  233. static ssize_t
  234. lstats_write(struct file *file, const char __user *buf, size_t count,
  235. loff_t *offs)
  236. {
  237. clear_global_latency_tracing();
  238. return count;
  239. }
  240. static int lstats_open(struct inode *inode, struct file *filp)
  241. {
  242. return single_open(filp, lstats_show, NULL);
  243. }
  244. static const struct file_operations lstats_fops = {
  245. .open = lstats_open,
  246. .read = seq_read,
  247. .write = lstats_write,
  248. .llseek = seq_lseek,
  249. .release = single_release,
  250. };
  251. static int __init init_lstats_procfs(void)
  252. {
  253. proc_create("latency_stats", 0644, NULL, &lstats_fops);
  254. return 0;
  255. }
  256. device_initcall(init_lstats_procfs);