blob: 7b67a9fb37885ad2c2385cfefe8ace876780098a [file] [log] [blame]
Ingo Molnar289f4802007-02-16 01:28:15 -08001/*
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
13#include <linux/proc_fs.h>
14#include <linux/module.h>
15#include <linux/spinlock.h>
16#include <linux/sched.h>
17#include <linux/seq_file.h>
18#include <linux/kallsyms.h>
Tom Hromatka00c001f2017-01-04 15:28:04 -070019#include <linux/nmi.h>
Ingo Molnar289f4802007-02-16 01:28:15 -080020
21#include <asm/uaccess.h>
22
Thomas Gleixnerc1797ba2015-03-25 13:07:37 +010023#include "tick-internal.h"
Nathan Zimmerb3956a82013-03-26 19:56:30 -050024
25struct timer_list_iter {
26 int cpu;
27 bool second_pass;
28 u64 now;
29};
30
Ingo Molnar289f4802007-02-16 01:28:15 -080031typedef void (*print_fn_t)(struct seq_file *m, unsigned int *classes);
32
Ingo Molnar289f4802007-02-16 01:28:15 -080033/*
34 * This allows printing both to /proc/timer_list and
35 * to the console (on SysRq-Q):
36 */
Joe Perches7de4e742015-04-17 11:39:18 -070037__printf(2, 3)
38static void SEQ_printf(struct seq_file *m, const char *fmt, ...)
39{
40 va_list args;
41
42 va_start(args, fmt);
43
44 if (m)
45 seq_vprintf(m, fmt, args);
46 else
47 vprintk(fmt, args);
48
49 va_end(args);
50}
Ingo Molnar289f4802007-02-16 01:28:15 -080051
52static void print_name_offset(struct seq_file *m, void *sym)
53{
Tejun Heo9281ace2007-07-17 04:03:51 -070054 char symname[KSYM_NAME_LEN];
Ingo Molnar289f4802007-02-16 01:28:15 -080055
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -070056 if (lookup_symbol_name((unsigned long)sym, symname) < 0)
Kees Cookf5903082011-02-11 19:21:25 -080057 SEQ_printf(m, "<%pK>", sym);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -070058 else
59 SEQ_printf(m, "%s", symname);
Ingo Molnar289f4802007-02-16 01:28:15 -080060}
61
62static void
Thomas Gleixnere67ef252008-09-25 23:50:23 +020063print_timer(struct seq_file *m, struct hrtimer *taddr, struct hrtimer *timer,
64 int idx, u64 now)
Ingo Molnar289f4802007-02-16 01:28:15 -080065{
Ingo Molnar289f4802007-02-16 01:28:15 -080066 SEQ_printf(m, " #%d: ", idx);
Thomas Gleixnere67ef252008-09-25 23:50:23 +020067 print_name_offset(m, taddr);
Ingo Molnar289f4802007-02-16 01:28:15 -080068 SEQ_printf(m, ", ");
69 print_name_offset(m, timer->function);
Thomas Gleixner203cbf72016-01-14 16:54:46 +000070 SEQ_printf(m, ", S:%02x", timer->state);
Ingo Molnar289f4802007-02-16 01:28:15 -080071 SEQ_printf(m, "\n");
Arjan van de Ven704af522008-09-07 16:10:20 -070072 SEQ_printf(m, " # expires at %Lu-%Lu nsecs [in %Ld to %Ld nsecs]\n",
73 (unsigned long long)ktime_to_ns(hrtimer_get_softexpires(timer)),
Arjan van de Vencc584b22008-09-01 15:02:30 -070074 (unsigned long long)ktime_to_ns(hrtimer_get_expires(timer)),
Arjan van de Ven704af522008-09-07 16:10:20 -070075 (long long)(ktime_to_ns(hrtimer_get_softexpires(timer)) - now),
Arjan van de Vencc584b22008-09-01 15:02:30 -070076 (long long)(ktime_to_ns(hrtimer_get_expires(timer)) - now));
Ingo Molnar289f4802007-02-16 01:28:15 -080077}
78
79static void
80print_active_timers(struct seq_file *m, struct hrtimer_clock_base *base,
81 u64 now)
82{
83 struct hrtimer *timer, tmp;
84 unsigned long next = 0, i;
John Stultz998adc32010-09-20 19:19:17 -070085 struct timerqueue_node *curr;
Ingo Molnar289f4802007-02-16 01:28:15 -080086 unsigned long flags;
87
88next_one:
89 i = 0;
Tom Hromatka00c001f2017-01-04 15:28:04 -070090
91 touch_nmi_watchdog();
92
Thomas Gleixnerecb49d12009-11-17 16:36:54 +010093 raw_spin_lock_irqsave(&base->cpu_base->lock, flags);
Ingo Molnar289f4802007-02-16 01:28:15 -080094
John Stultz998adc32010-09-20 19:19:17 -070095 curr = timerqueue_getnext(&base->active);
Ingo Molnar289f4802007-02-16 01:28:15 -080096 /*
97 * Crude but we have to do this O(N*N) thing, because
98 * we have to unlock the base when printing:
99 */
100 while (curr && i < next) {
John Stultz998adc32010-09-20 19:19:17 -0700101 curr = timerqueue_iterate_next(curr);
Ingo Molnar289f4802007-02-16 01:28:15 -0800102 i++;
103 }
104
105 if (curr) {
106
John Stultz998adc32010-09-20 19:19:17 -0700107 timer = container_of(curr, struct hrtimer, node);
Ingo Molnar289f4802007-02-16 01:28:15 -0800108 tmp = *timer;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100109 raw_spin_unlock_irqrestore(&base->cpu_base->lock, flags);
Ingo Molnar289f4802007-02-16 01:28:15 -0800110
Thomas Gleixnere67ef252008-09-25 23:50:23 +0200111 print_timer(m, timer, &tmp, i, now);
Ingo Molnar289f4802007-02-16 01:28:15 -0800112 next++;
113 goto next_one;
114 }
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100115 raw_spin_unlock_irqrestore(&base->cpu_base->lock, flags);
Ingo Molnar289f4802007-02-16 01:28:15 -0800116}
117
118static void
119print_base(struct seq_file *m, struct hrtimer_clock_base *base, u64 now)
120{
Kees Cookf5903082011-02-11 19:21:25 -0800121 SEQ_printf(m, " .base: %pK\n", base);
Thomas Gleixner398ca172015-04-14 21:08:27 +0000122 SEQ_printf(m, " .index: %d\n", base->index);
123
124 SEQ_printf(m, " .resolution: %u nsecs\n", (unsigned) hrtimer_resolution);
125
Ingo Molnar289f4802007-02-16 01:28:15 -0800126 SEQ_printf(m, " .get_time: ");
127 print_name_offset(m, base->get_time);
128 SEQ_printf(m, "\n");
129#ifdef CONFIG_HIGH_RES_TIMERS
David Miller9b04bd22007-05-09 02:33:43 -0700130 SEQ_printf(m, " .offset: %Lu nsecs\n",
131 (unsigned long long) ktime_to_ns(base->offset));
Ingo Molnar289f4802007-02-16 01:28:15 -0800132#endif
133 SEQ_printf(m, "active timers:\n");
John Stultz38bf9852015-05-27 16:44:48 -0700134 print_active_timers(m, base, now + ktime_to_ns(base->offset));
Ingo Molnar289f4802007-02-16 01:28:15 -0800135}
136
137static void print_cpu(struct seq_file *m, int cpu, u64 now)
138{
139 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
140 int i;
141
Vegard Nossum129f1d22007-10-11 08:23:34 +0200142 SEQ_printf(m, "cpu: %d\n", cpu);
Ingo Molnar289f4802007-02-16 01:28:15 -0800143 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
144 SEQ_printf(m, " clock %d:\n", i);
145 print_base(m, cpu_base->clock_base + i, now);
146 }
147#define P(x) \
David Miller9b04bd22007-05-09 02:33:43 -0700148 SEQ_printf(m, " .%-15s: %Lu\n", #x, \
149 (unsigned long long)(cpu_base->x))
Ingo Molnar289f4802007-02-16 01:28:15 -0800150#define P_ns(x) \
David Miller9b04bd22007-05-09 02:33:43 -0700151 SEQ_printf(m, " .%-15s: %Lu nsecs\n", #x, \
152 (unsigned long long)(ktime_to_ns(cpu_base->x)))
Ingo Molnar289f4802007-02-16 01:28:15 -0800153
154#ifdef CONFIG_HIGH_RES_TIMERS
155 P_ns(expires_next);
156 P(hres_active);
157 P(nr_events);
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100158 P(nr_retries);
159 P(nr_hangs);
Thomas Gleixnera6ffebc2015-04-14 21:08:34 +0000160 P(max_hang_time);
Ingo Molnar289f4802007-02-16 01:28:15 -0800161#endif
162#undef P
163#undef P_ns
164
165#ifdef CONFIG_TICK_ONESHOT
166# define P(x) \
David Miller9b04bd22007-05-09 02:33:43 -0700167 SEQ_printf(m, " .%-15s: %Lu\n", #x, \
168 (unsigned long long)(ts->x))
Ingo Molnar289f4802007-02-16 01:28:15 -0800169# define P_ns(x) \
David Miller9b04bd22007-05-09 02:33:43 -0700170 SEQ_printf(m, " .%-15s: %Lu nsecs\n", #x, \
171 (unsigned long long)(ktime_to_ns(ts->x)))
Ingo Molnar289f4802007-02-16 01:28:15 -0800172 {
173 struct tick_sched *ts = tick_get_tick_sched(cpu);
174 P(nohz_mode);
Frederic Weisbeckerf5d411c2011-07-31 17:44:12 +0200175 P_ns(last_tick);
Ingo Molnar289f4802007-02-16 01:28:15 -0800176 P(tick_stopped);
177 P(idle_jiffies);
178 P(idle_calls);
179 P(idle_sleeps);
180 P_ns(idle_entrytime);
Thomas Gleixner5df7fa12008-02-01 17:45:14 +0100181 P_ns(idle_waketime);
182 P_ns(idle_exittime);
Ingo Molnar289f4802007-02-16 01:28:15 -0800183 P_ns(idle_sleeptime);
Arjan van de Ven0224cf42010-05-09 08:25:23 -0700184 P_ns(iowait_sleeptime);
Ingo Molnar289f4802007-02-16 01:28:15 -0800185 P(last_jiffies);
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000186 P(next_timer);
Ingo Molnar289f4802007-02-16 01:28:15 -0800187 P_ns(idle_expires);
David Miller9b04bd22007-05-09 02:33:43 -0700188 SEQ_printf(m, "jiffies: %Lu\n",
189 (unsigned long long)jiffies);
Ingo Molnar289f4802007-02-16 01:28:15 -0800190 }
191#endif
192
193#undef P
194#undef P_ns
Nathan Zimmer60cf7ea2013-03-26 19:56:29 -0500195 SEQ_printf(m, "\n");
Ingo Molnar289f4802007-02-16 01:28:15 -0800196}
197
198#ifdef CONFIG_GENERIC_CLOCKEVENTS
199static void
Thomas Gleixnerc5b77a32008-09-29 17:31:41 +0200200print_tickdevice(struct seq_file *m, struct tick_device *td, int cpu)
Ingo Molnar289f4802007-02-16 01:28:15 -0800201{
202 struct clock_event_device *dev = td->evtdev;
203
Tom Hromatka00c001f2017-01-04 15:28:04 -0700204 touch_nmi_watchdog();
205
Vegard Nossum129f1d22007-10-11 08:23:34 +0200206 SEQ_printf(m, "Tick Device: mode: %d\n", td->mode);
Thomas Gleixnerc5b77a32008-09-29 17:31:41 +0200207 if (cpu < 0)
208 SEQ_printf(m, "Broadcast device\n");
209 else
210 SEQ_printf(m, "Per CPU device: %d\n", cpu);
Ingo Molnar289f4802007-02-16 01:28:15 -0800211
212 SEQ_printf(m, "Clock Event Device: ");
213 if (!dev) {
214 SEQ_printf(m, "<NULL>\n");
215 return;
216 }
217 SEQ_printf(m, "%s\n", dev->name);
Jon Hunter97813f22009-08-18 12:45:11 -0500218 SEQ_printf(m, " max_delta_ns: %llu\n",
219 (unsigned long long) dev->max_delta_ns);
220 SEQ_printf(m, " min_delta_ns: %llu\n",
221 (unsigned long long) dev->min_delta_ns);
Thomas Gleixner23af3682009-11-11 14:05:25 +0000222 SEQ_printf(m, " mult: %u\n", dev->mult);
223 SEQ_printf(m, " shift: %u\n", dev->shift);
Viresh Kumareef76352015-09-11 09:34:26 +0530224 SEQ_printf(m, " mode: %d\n", clockevent_get_state(dev));
Ingo Molnar289f4802007-02-16 01:28:15 -0800225 SEQ_printf(m, " next_event: %Ld nsecs\n",
226 (unsigned long long) ktime_to_ns(dev->next_event));
227
228 SEQ_printf(m, " set_next_event: ");
229 print_name_offset(m, dev->set_next_event);
230 SEQ_printf(m, "\n");
231
Viresh Kumareef76352015-09-11 09:34:26 +0530232 if (dev->set_state_shutdown) {
233 SEQ_printf(m, " shutdown: ");
234 print_name_offset(m, dev->set_state_shutdown);
Viresh Kumarbd624d72015-02-13 08:54:56 +0800235 SEQ_printf(m, "\n");
Viresh Kumareef76352015-09-11 09:34:26 +0530236 }
Viresh Kumarbd624d72015-02-13 08:54:56 +0800237
Viresh Kumareef76352015-09-11 09:34:26 +0530238 if (dev->set_state_periodic) {
239 SEQ_printf(m, " periodic: ");
240 print_name_offset(m, dev->set_state_periodic);
241 SEQ_printf(m, "\n");
242 }
Viresh Kumarbd624d72015-02-13 08:54:56 +0800243
Viresh Kumareef76352015-09-11 09:34:26 +0530244 if (dev->set_state_oneshot) {
245 SEQ_printf(m, " oneshot: ");
246 print_name_offset(m, dev->set_state_oneshot);
247 SEQ_printf(m, "\n");
248 }
Viresh Kumarbd624d72015-02-13 08:54:56 +0800249
Viresh Kumareef76352015-09-11 09:34:26 +0530250 if (dev->set_state_oneshot_stopped) {
251 SEQ_printf(m, " oneshot stopped: ");
252 print_name_offset(m, dev->set_state_oneshot_stopped);
253 SEQ_printf(m, "\n");
254 }
Viresh Kumar8fff52f2015-04-03 09:04:04 +0530255
Viresh Kumareef76352015-09-11 09:34:26 +0530256 if (dev->tick_resume) {
257 SEQ_printf(m, " resume: ");
258 print_name_offset(m, dev->tick_resume);
259 SEQ_printf(m, "\n");
Viresh Kumarbd624d72015-02-13 08:54:56 +0800260 }
Ingo Molnar289f4802007-02-16 01:28:15 -0800261
262 SEQ_printf(m, " event_handler: ");
263 print_name_offset(m, dev->event_handler);
264 SEQ_printf(m, "\n");
Thomas Gleixner80a05b92010-03-12 17:34:14 +0100265 SEQ_printf(m, " retries: %lu\n", dev->retries);
Nathan Zimmer60cf7ea2013-03-26 19:56:29 -0500266 SEQ_printf(m, "\n");
Ingo Molnar289f4802007-02-16 01:28:15 -0800267}
268
Nathan Zimmer60cf7ea2013-03-26 19:56:29 -0500269static void timer_list_show_tickdevices_header(struct seq_file *m)
Ingo Molnar289f4802007-02-16 01:28:15 -0800270{
Ingo Molnar289f4802007-02-16 01:28:15 -0800271#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
Thomas Gleixnerc5b77a32008-09-29 17:31:41 +0200272 print_tickdevice(m, tick_get_broadcast_device(), -1);
Preeti U Murthy1ef09cd2015-04-28 14:15:20 +0530273 SEQ_printf(m, "tick_broadcast_mask: %*pb\n",
274 cpumask_pr_args(tick_get_broadcast_mask()));
Ingo Molnar289f4802007-02-16 01:28:15 -0800275#ifdef CONFIG_TICK_ONESHOT
Preeti U Murthy1ef09cd2015-04-28 14:15:20 +0530276 SEQ_printf(m, "tick_broadcast_oneshot_mask: %*pb\n",
277 cpumask_pr_args(tick_get_broadcast_oneshot_mask()));
Ingo Molnar289f4802007-02-16 01:28:15 -0800278#endif
279 SEQ_printf(m, "\n");
280#endif
Ingo Molnar289f4802007-02-16 01:28:15 -0800281}
Ingo Molnar289f4802007-02-16 01:28:15 -0800282#endif
283
Nathan Zimmerb3956a82013-03-26 19:56:30 -0500284static inline void timer_list_header(struct seq_file *m, u64 now)
Ingo Molnar289f4802007-02-16 01:28:15 -0800285{
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000286 SEQ_printf(m, "Timer List Version: v0.8\n");
Ingo Molnar289f4802007-02-16 01:28:15 -0800287 SEQ_printf(m, "HRTIMER_MAX_CLOCK_BASES: %d\n", HRTIMER_MAX_CLOCK_BASES);
288 SEQ_printf(m, "now at %Ld nsecs\n", (unsigned long long)now);
Nathan Zimmer60cf7ea2013-03-26 19:56:29 -0500289 SEQ_printf(m, "\n");
Nathan Zimmerb3956a82013-03-26 19:56:30 -0500290}
Ingo Molnar289f4802007-02-16 01:28:15 -0800291
Ingo Molnar289f4802007-02-16 01:28:15 -0800292void sysrq_timer_list_show(void)
293{
Nathan Zimmerb3956a82013-03-26 19:56:30 -0500294 u64 now = ktime_to_ns(ktime_get());
295 int cpu;
296
297 timer_list_header(NULL, now);
298
299 for_each_online_cpu(cpu)
300 print_cpu(NULL, cpu, now);
301
302#ifdef CONFIG_GENERIC_CLOCKEVENTS
303 timer_list_show_tickdevices_header(NULL);
304 for_each_online_cpu(cpu)
305 print_tickdevice(NULL, tick_get_device(cpu), cpu);
306#endif
307 return;
Ingo Molnar289f4802007-02-16 01:28:15 -0800308}
309
Nathan Huckleberry8cc6df32019-06-14 11:16:04 -0700310#ifdef CONFIG_PROC_FS
311static int timer_list_show(struct seq_file *m, void *v)
312{
313 struct timer_list_iter *iter = v;
314
315 if (iter->cpu == -1 && !iter->second_pass)
316 timer_list_header(m, iter->now);
317 else if (!iter->second_pass)
318 print_cpu(m, iter->cpu, iter->now);
319#ifdef CONFIG_GENERIC_CLOCKEVENTS
320 else if (iter->cpu == -1 && iter->second_pass)
321 timer_list_show_tickdevices_header(m);
322 else
323 print_tickdevice(m, tick_get_device(iter->cpu), iter->cpu);
324#endif
325 return 0;
326}
327
Nathan Zimmer84a78a62013-08-28 16:35:14 -0700328static void *move_iter(struct timer_list_iter *iter, loff_t offset)
329{
330 for (; offset; offset--) {
331 iter->cpu = cpumask_next(iter->cpu, cpu_online_mask);
332 if (iter->cpu >= nr_cpu_ids) {
333#ifdef CONFIG_GENERIC_CLOCKEVENTS
334 if (!iter->second_pass) {
335 iter->cpu = -1;
336 iter->second_pass = true;
337 } else
338 return NULL;
339#else
340 return NULL;
341#endif
342 }
343 }
344 return iter;
345}
346
Nathan Zimmerb3956a82013-03-26 19:56:30 -0500347static void *timer_list_start(struct seq_file *file, loff_t *offset)
348{
349 struct timer_list_iter *iter = file->private;
350
Nathan Zimmer84a78a62013-08-28 16:35:14 -0700351 if (!*offset)
Nathan Zimmerb3956a82013-03-26 19:56:30 -0500352 iter->now = ktime_to_ns(ktime_get());
Nathan Zimmer84a78a62013-08-28 16:35:14 -0700353 iter->cpu = -1;
354 iter->second_pass = false;
355 return move_iter(iter, *offset);
Nathan Zimmerb3956a82013-03-26 19:56:30 -0500356}
357
358static void *timer_list_next(struct seq_file *file, void *v, loff_t *offset)
359{
360 struct timer_list_iter *iter = file->private;
Nathan Zimmerb3956a82013-03-26 19:56:30 -0500361 ++*offset;
Nathan Zimmer84a78a62013-08-28 16:35:14 -0700362 return move_iter(iter, 1);
Nathan Zimmerb3956a82013-03-26 19:56:30 -0500363}
364
365static void timer_list_stop(struct seq_file *seq, void *v)
366{
367}
368
369static const struct seq_operations timer_list_sops = {
370 .start = timer_list_start,
371 .next = timer_list_next,
372 .stop = timer_list_stop,
373 .show = timer_list_show,
374};
375
Ingo Molnar289f4802007-02-16 01:28:15 -0800376static int timer_list_open(struct inode *inode, struct file *filp)
377{
Nathan Zimmerb3956a82013-03-26 19:56:30 -0500378 return seq_open_private(filp, &timer_list_sops,
379 sizeof(struct timer_list_iter));
Ingo Molnar289f4802007-02-16 01:28:15 -0800380}
381
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700382static const struct file_operations timer_list_fops = {
Ingo Molnar289f4802007-02-16 01:28:15 -0800383 .open = timer_list_open,
384 .read = seq_read,
385 .llseek = seq_lseek,
Nathan Zimmerb3956a82013-03-26 19:56:30 -0500386 .release = seq_release_private,
Ingo Molnar289f4802007-02-16 01:28:15 -0800387};
388
389static int __init init_timer_list_procfs(void)
390{
391 struct proc_dir_entry *pe;
392
Ingo Molnar1098aad2017-11-13 07:15:41 +0100393 pe = proc_create("timer_list", 0400, NULL, &timer_list_fops);
Ingo Molnar289f4802007-02-16 01:28:15 -0800394 if (!pe)
395 return -ENOMEM;
Ingo Molnar289f4802007-02-16 01:28:15 -0800396 return 0;
397}
398__initcall(init_timer_list_procfs);
Nathan Huckleberry8cc6df32019-06-14 11:16:04 -0700399#endif