timer_list.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * List pending timers
  4. *
  5. * Copyright(C) 2006, Red Hat, Inc., Ingo Molnar
  6. */
  7. #include <linux/proc_fs.h>
  8. #include <linux/module.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/sched.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/kallsyms.h>
  13. #include <linux/nmi.h>
  14. #include <linux/uaccess.h>
  15. #include "tick-internal.h"
  16. struct timer_list_iter {
  17. int cpu;
  18. bool second_pass;
  19. u64 now;
  20. };
  21. /*
  22. * This allows printing both to /proc/timer_list and
  23. * to the console (on SysRq-Q):
  24. */
  25. __printf(2, 3)
  26. static void SEQ_printf(struct seq_file *m, const char *fmt, ...)
  27. {
  28. va_list args;
  29. va_start(args, fmt);
  30. if (m)
  31. seq_vprintf(m, fmt, args);
  32. else
  33. vprintk(fmt, args);
  34. va_end(args);
  35. }
  36. static void
  37. print_timer(struct seq_file *m, struct hrtimer *taddr, struct hrtimer *timer,
  38. int idx, u64 now)
  39. {
  40. SEQ_printf(m, " #%d: <%pK>, %ps", idx, taddr, timer->function);
  41. SEQ_printf(m, ", S:%02x", timer->state);
  42. SEQ_printf(m, "\n");
  43. SEQ_printf(m, " # expires at %Lu-%Lu nsecs [in %Ld to %Ld nsecs]\n",
  44. (unsigned long long)ktime_to_ns(hrtimer_get_softexpires(timer)),
  45. (unsigned long long)ktime_to_ns(hrtimer_get_expires(timer)),
  46. (long long)(ktime_to_ns(hrtimer_get_softexpires(timer)) - now),
  47. (long long)(ktime_to_ns(hrtimer_get_expires(timer)) - now));
  48. }
  49. static void
  50. print_active_timers(struct seq_file *m, struct hrtimer_clock_base *base,
  51. u64 now)
  52. {
  53. struct hrtimer *timer, tmp;
  54. unsigned long next = 0, i;
  55. struct timerqueue_node *curr;
  56. unsigned long flags;
  57. next_one:
  58. i = 0;
  59. touch_nmi_watchdog();
  60. raw_spin_lock_irqsave(&base->cpu_base->lock, flags);
  61. curr = timerqueue_getnext(&base->active);
  62. /*
  63. * Crude but we have to do this O(N*N) thing, because
  64. * we have to unlock the base when printing:
  65. */
  66. while (curr && i < next) {
  67. curr = timerqueue_iterate_next(curr);
  68. i++;
  69. }
  70. if (curr) {
  71. timer = container_of(curr, struct hrtimer, node);
  72. tmp = *timer;
  73. raw_spin_unlock_irqrestore(&base->cpu_base->lock, flags);
  74. print_timer(m, timer, &tmp, i, now);
  75. next++;
  76. goto next_one;
  77. }
  78. raw_spin_unlock_irqrestore(&base->cpu_base->lock, flags);
  79. }
  80. static void
  81. print_base(struct seq_file *m, struct hrtimer_clock_base *base, u64 now)
  82. {
  83. SEQ_printf(m, " .base: %pK\n", base);
  84. SEQ_printf(m, " .index: %d\n", base->index);
  85. SEQ_printf(m, " .resolution: %u nsecs\n", hrtimer_resolution);
  86. SEQ_printf(m, " .get_time: %ps\n", base->get_time);
  87. #ifdef CONFIG_HIGH_RES_TIMERS
  88. SEQ_printf(m, " .offset: %Lu nsecs\n",
  89. (unsigned long long) ktime_to_ns(base->offset));
  90. #endif
  91. SEQ_printf(m, "active timers:\n");
  92. print_active_timers(m, base, now + ktime_to_ns(base->offset));
  93. }
  94. static void print_cpu(struct seq_file *m, int cpu, u64 now)
  95. {
  96. struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
  97. int i;
  98. SEQ_printf(m, "cpu: %d\n", cpu);
  99. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  100. SEQ_printf(m, " clock %d:\n", i);
  101. print_base(m, cpu_base->clock_base + i, now);
  102. }
  103. #define P(x) \
  104. SEQ_printf(m, " .%-15s: %Lu\n", #x, \
  105. (unsigned long long)(cpu_base->x))
  106. #define P_ns(x) \
  107. SEQ_printf(m, " .%-15s: %Lu nsecs\n", #x, \
  108. (unsigned long long)(ktime_to_ns(cpu_base->x)))
  109. #ifdef CONFIG_HIGH_RES_TIMERS
  110. P_ns(expires_next);
  111. P(hres_active);
  112. P(nr_events);
  113. P(nr_retries);
  114. P(nr_hangs);
  115. P(max_hang_time);
  116. #endif
  117. #undef P
  118. #undef P_ns
  119. #ifdef CONFIG_TICK_ONESHOT
  120. # define P(x) \
  121. SEQ_printf(m, " .%-15s: %Lu\n", #x, \
  122. (unsigned long long)(ts->x))
  123. # define P_ns(x) \
  124. SEQ_printf(m, " .%-15s: %Lu nsecs\n", #x, \
  125. (unsigned long long)(ktime_to_ns(ts->x)))
  126. {
  127. struct tick_sched *ts = tick_get_tick_sched(cpu);
  128. P(nohz_mode);
  129. P_ns(last_tick);
  130. P(tick_stopped);
  131. P(idle_jiffies);
  132. P(idle_calls);
  133. P(idle_sleeps);
  134. P_ns(idle_entrytime);
  135. P_ns(idle_waketime);
  136. P_ns(idle_exittime);
  137. P_ns(idle_sleeptime);
  138. P_ns(iowait_sleeptime);
  139. P(last_jiffies);
  140. P(next_timer);
  141. P_ns(idle_expires);
  142. SEQ_printf(m, "jiffies: %Lu\n",
  143. (unsigned long long)jiffies);
  144. }
  145. #endif
  146. #undef P
  147. #undef P_ns
  148. SEQ_printf(m, "\n");
  149. }
  150. #ifdef CONFIG_GENERIC_CLOCKEVENTS
  151. static void
  152. print_tickdevice(struct seq_file *m, struct tick_device *td, int cpu)
  153. {
  154. struct clock_event_device *dev = td->evtdev;
  155. touch_nmi_watchdog();
  156. SEQ_printf(m, "Tick Device: mode: %d\n", td->mode);
  157. if (cpu < 0)
  158. SEQ_printf(m, "Broadcast device\n");
  159. else
  160. SEQ_printf(m, "Per CPU device: %d\n", cpu);
  161. SEQ_printf(m, "Clock Event Device: ");
  162. if (!dev) {
  163. SEQ_printf(m, "<NULL>\n");
  164. return;
  165. }
  166. SEQ_printf(m, "%s\n", dev->name);
  167. SEQ_printf(m, " max_delta_ns: %llu\n",
  168. (unsigned long long) dev->max_delta_ns);
  169. SEQ_printf(m, " min_delta_ns: %llu\n",
  170. (unsigned long long) dev->min_delta_ns);
  171. SEQ_printf(m, " mult: %u\n", dev->mult);
  172. SEQ_printf(m, " shift: %u\n", dev->shift);
  173. SEQ_printf(m, " mode: %d\n", clockevent_get_state(dev));
  174. SEQ_printf(m, " next_event: %Ld nsecs\n",
  175. (unsigned long long) ktime_to_ns(dev->next_event));
  176. SEQ_printf(m, " set_next_event: %ps\n", dev->set_next_event);
  177. if (dev->set_state_shutdown)
  178. SEQ_printf(m, " shutdown: %ps\n",
  179. dev->set_state_shutdown);
  180. if (dev->set_state_periodic)
  181. SEQ_printf(m, " periodic: %ps\n",
  182. dev->set_state_periodic);
  183. if (dev->set_state_oneshot)
  184. SEQ_printf(m, " oneshot: %ps\n",
  185. dev->set_state_oneshot);
  186. if (dev->set_state_oneshot_stopped)
  187. SEQ_printf(m, " oneshot stopped: %ps\n",
  188. dev->set_state_oneshot_stopped);
  189. if (dev->tick_resume)
  190. SEQ_printf(m, " resume: %ps\n",
  191. dev->tick_resume);
  192. SEQ_printf(m, " event_handler: %ps\n", dev->event_handler);
  193. SEQ_printf(m, "\n");
  194. SEQ_printf(m, " retries: %lu\n", dev->retries);
  195. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  196. if (cpu >= 0) {
  197. const struct clock_event_device *wd = tick_get_wakeup_device(cpu);
  198. SEQ_printf(m, "Wakeup Device: %s\n", wd ? wd->name : "<NULL>");
  199. }
  200. #endif
  201. SEQ_printf(m, "\n");
  202. }
  203. static void timer_list_show_tickdevices_header(struct seq_file *m)
  204. {
  205. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  206. print_tickdevice(m, tick_get_broadcast_device(), -1);
  207. SEQ_printf(m, "tick_broadcast_mask: %*pb\n",
  208. cpumask_pr_args(tick_get_broadcast_mask()));
  209. #ifdef CONFIG_TICK_ONESHOT
  210. SEQ_printf(m, "tick_broadcast_oneshot_mask: %*pb\n",
  211. cpumask_pr_args(tick_get_broadcast_oneshot_mask()));
  212. #endif
  213. SEQ_printf(m, "\n");
  214. #endif
  215. }
  216. #endif
  217. static inline void timer_list_header(struct seq_file *m, u64 now)
  218. {
  219. SEQ_printf(m, "Timer List Version: v0.9\n");
  220. SEQ_printf(m, "HRTIMER_MAX_CLOCK_BASES: %d\n", HRTIMER_MAX_CLOCK_BASES);
  221. SEQ_printf(m, "now at %Ld nsecs\n", (unsigned long long)now);
  222. SEQ_printf(m, "\n");
  223. }
  224. void sysrq_timer_list_show(void)
  225. {
  226. u64 now = ktime_to_ns(ktime_get());
  227. int cpu;
  228. timer_list_header(NULL, now);
  229. for_each_online_cpu(cpu)
  230. print_cpu(NULL, cpu, now);
  231. #ifdef CONFIG_GENERIC_CLOCKEVENTS
  232. timer_list_show_tickdevices_header(NULL);
  233. for_each_online_cpu(cpu)
  234. print_tickdevice(NULL, tick_get_device(cpu), cpu);
  235. #endif
  236. return;
  237. }
  238. #ifdef CONFIG_PROC_FS
  239. static int timer_list_show(struct seq_file *m, void *v)
  240. {
  241. struct timer_list_iter *iter = v;
  242. if (iter->cpu == -1 && !iter->second_pass)
  243. timer_list_header(m, iter->now);
  244. else if (!iter->second_pass)
  245. print_cpu(m, iter->cpu, iter->now);
  246. #ifdef CONFIG_GENERIC_CLOCKEVENTS
  247. else if (iter->cpu == -1 && iter->second_pass)
  248. timer_list_show_tickdevices_header(m);
  249. else
  250. print_tickdevice(m, tick_get_device(iter->cpu), iter->cpu);
  251. #endif
  252. return 0;
  253. }
  254. static void *move_iter(struct timer_list_iter *iter, loff_t offset)
  255. {
  256. for (; offset; offset--) {
  257. iter->cpu = cpumask_next(iter->cpu, cpu_online_mask);
  258. if (iter->cpu >= nr_cpu_ids) {
  259. #ifdef CONFIG_GENERIC_CLOCKEVENTS
  260. if (!iter->second_pass) {
  261. iter->cpu = -1;
  262. iter->second_pass = true;
  263. } else
  264. return NULL;
  265. #else
  266. return NULL;
  267. #endif
  268. }
  269. }
  270. return iter;
  271. }
  272. static void *timer_list_start(struct seq_file *file, loff_t *offset)
  273. {
  274. struct timer_list_iter *iter = file->private;
  275. if (!*offset)
  276. iter->now = ktime_to_ns(ktime_get());
  277. iter->cpu = -1;
  278. iter->second_pass = false;
  279. return move_iter(iter, *offset);
  280. }
  281. static void *timer_list_next(struct seq_file *file, void *v, loff_t *offset)
  282. {
  283. struct timer_list_iter *iter = file->private;
  284. ++*offset;
  285. return move_iter(iter, 1);
  286. }
  287. static void timer_list_stop(struct seq_file *seq, void *v)
  288. {
  289. }
  290. static const struct seq_operations timer_list_sops = {
  291. .start = timer_list_start,
  292. .next = timer_list_next,
  293. .stop = timer_list_stop,
  294. .show = timer_list_show,
  295. };
  296. static int __init init_timer_list_procfs(void)
  297. {
  298. struct proc_dir_entry *pe;
  299. pe = proc_create_seq_private("timer_list", 0400, NULL, &timer_list_sops,
  300. sizeof(struct timer_list_iter), NULL);
  301. if (!pe)
  302. return -ENOMEM;
  303. return 0;
  304. }
  305. __initcall(init_timer_list_procfs);
  306. #endif