latencytop.c 7.9 KB

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