latencytop.c 7.2 KB

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