latencytop.c 7.6 KB

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