timer_list.c 9.7 KB

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