latencytop.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 <[email protected]>
  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. #include <linux/sysctl.h>
  57. static DEFINE_RAW_SPINLOCK(latency_lock);
  58. #define MAXLR 128
  59. static struct latency_record latency_record[MAXLR];
  60. int latencytop_enabled;
  61. #ifdef CONFIG_SYSCTL
  62. static int sysctl_latencytop(struct ctl_table *table, int write, void *buffer,
  63. size_t *lenp, loff_t *ppos)
  64. {
  65. int err;
  66. err = proc_dointvec(table, write, buffer, lenp, ppos);
  67. if (latencytop_enabled)
  68. force_schedstat_enabled();
  69. return err;
  70. }
  71. static struct ctl_table latencytop_sysctl[] = {
  72. {
  73. .procname = "latencytop",
  74. .data = &latencytop_enabled,
  75. .maxlen = sizeof(int),
  76. .mode = 0644,
  77. .proc_handler = sysctl_latencytop,
  78. },
  79. {}
  80. };
  81. #endif
  82. void clear_tsk_latency_tracing(struct task_struct *p)
  83. {
  84. unsigned long flags;
  85. raw_spin_lock_irqsave(&latency_lock, flags);
  86. memset(&p->latency_record, 0, sizeof(p->latency_record));
  87. p->latency_record_count = 0;
  88. raw_spin_unlock_irqrestore(&latency_lock, flags);
  89. }
  90. static void clear_global_latency_tracing(void)
  91. {
  92. unsigned long flags;
  93. raw_spin_lock_irqsave(&latency_lock, flags);
  94. memset(&latency_record, 0, sizeof(latency_record));
  95. raw_spin_unlock_irqrestore(&latency_lock, flags);
  96. }
  97. static void __sched
  98. account_global_scheduler_latency(struct task_struct *tsk,
  99. struct latency_record *lat)
  100. {
  101. int firstnonnull = MAXLR;
  102. int i;
  103. /* skip kernel threads for now */
  104. if (!tsk->mm)
  105. return;
  106. for (i = 0; i < MAXLR; i++) {
  107. int q, same = 1;
  108. /* Nothing stored: */
  109. if (!latency_record[i].backtrace[0]) {
  110. if (firstnonnull > i)
  111. firstnonnull = i;
  112. continue;
  113. }
  114. for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
  115. unsigned long record = lat->backtrace[q];
  116. if (latency_record[i].backtrace[q] != record) {
  117. same = 0;
  118. break;
  119. }
  120. /* 0 entry marks end of backtrace: */
  121. if (!record)
  122. break;
  123. }
  124. if (same) {
  125. latency_record[i].count++;
  126. latency_record[i].time += lat->time;
  127. if (lat->time > latency_record[i].max)
  128. latency_record[i].max = lat->time;
  129. return;
  130. }
  131. }
  132. i = firstnonnull;
  133. if (i >= MAXLR)
  134. return;
  135. /* Allocted a new one: */
  136. memcpy(&latency_record[i], lat, sizeof(struct latency_record));
  137. }
  138. /**
  139. * __account_scheduler_latency - record an occurred latency
  140. * @tsk - the task struct of the task hitting the latency
  141. * @usecs - the duration of the latency in microseconds
  142. * @inter - 1 if the sleep was interruptible, 0 if uninterruptible
  143. *
  144. * This function is the main entry point for recording latency entries
  145. * as called by the scheduler.
  146. *
  147. * This function has a few special cases to deal with normal 'non-latency'
  148. * sleeps: specifically, interruptible sleep longer than 5 msec is skipped
  149. * since this usually is caused by waiting for events via select() and co.
  150. *
  151. * Negative latencies (caused by time going backwards) are also explicitly
  152. * skipped.
  153. */
  154. void __sched
  155. __account_scheduler_latency(struct task_struct *tsk, int usecs, int inter)
  156. {
  157. unsigned long flags;
  158. int i, q;
  159. struct latency_record lat;
  160. /* Long interruptible waits are generally user requested... */
  161. if (inter && usecs > 5000)
  162. return;
  163. /* Negative sleeps are time going backwards */
  164. /* Zero-time sleeps are non-interesting */
  165. if (usecs <= 0)
  166. return;
  167. memset(&lat, 0, sizeof(lat));
  168. lat.count = 1;
  169. lat.time = usecs;
  170. lat.max = usecs;
  171. stack_trace_save_tsk(tsk, lat.backtrace, LT_BACKTRACEDEPTH, 0);
  172. raw_spin_lock_irqsave(&latency_lock, flags);
  173. account_global_scheduler_latency(tsk, &lat);
  174. for (i = 0; i < tsk->latency_record_count; i++) {
  175. struct latency_record *mylat;
  176. int same = 1;
  177. mylat = &tsk->latency_record[i];
  178. for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
  179. unsigned long record = lat.backtrace[q];
  180. if (mylat->backtrace[q] != record) {
  181. same = 0;
  182. break;
  183. }
  184. /* 0 entry is end of backtrace */
  185. if (!record)
  186. break;
  187. }
  188. if (same) {
  189. mylat->count++;
  190. mylat->time += lat.time;
  191. if (lat.time > mylat->max)
  192. mylat->max = lat.time;
  193. goto out_unlock;
  194. }
  195. }
  196. /*
  197. * short term hack; if we're > 32 we stop; future we recycle:
  198. */
  199. if (tsk->latency_record_count >= LT_SAVECOUNT)
  200. goto out_unlock;
  201. /* Allocated a new one: */
  202. i = tsk->latency_record_count++;
  203. memcpy(&tsk->latency_record[i], &lat, sizeof(struct latency_record));
  204. out_unlock:
  205. raw_spin_unlock_irqrestore(&latency_lock, flags);
  206. }
  207. static int lstats_show(struct seq_file *m, void *v)
  208. {
  209. int i;
  210. seq_puts(m, "Latency Top version : v0.1\n");
  211. for (i = 0; i < MAXLR; i++) {
  212. struct latency_record *lr = &latency_record[i];
  213. if (lr->backtrace[0]) {
  214. int q;
  215. seq_printf(m, "%i %lu %lu",
  216. lr->count, lr->time, lr->max);
  217. for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
  218. unsigned long bt = lr->backtrace[q];
  219. if (!bt)
  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 proc_ops lstats_proc_ops = {
  240. .proc_open = lstats_open,
  241. .proc_read = seq_read,
  242. .proc_write = lstats_write,
  243. .proc_lseek = seq_lseek,
  244. .proc_release = single_release,
  245. };
  246. static int __init init_lstats_procfs(void)
  247. {
  248. proc_create("latency_stats", 0644, NULL, &lstats_proc_ops);
  249. #ifdef CONFIG_SYSCTL
  250. register_sysctl_init("kernel", latencytop_sysctl);
  251. #endif
  252. return 0;
  253. }
  254. device_initcall(init_lstats_procfs);