timer_list.c 9.8 KB

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