timer_list.c 9.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * kernel/time/timer_list.c
 *
 * List pending timers
 *
 * Copyright(C) 2006, Red Hat, Inc., Ingo Molnar
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/proc_fs.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/sched.h>
#include <linux/seq_file.h>
#include <linux/kallsyms.h>

#include <asm/uaccess.h>

22
#include "tick-internal.h"
23 24 25 26 27 28 29

struct timer_list_iter {
	int cpu;
	bool second_pass;
	u64 now;
};

30 31 32 33 34 35 36 37
typedef void (*print_fn_t)(struct seq_file *m, unsigned int *classes);

DECLARE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases);

/*
 * This allows printing both to /proc/timer_list and
 * to the console (on SysRq-Q):
 */
38 39 40 41 42 43 44 45 46 47 48 49 50 51
__printf(2, 3)
static void SEQ_printf(struct seq_file *m, const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);

	if (m)
		seq_vprintf(m, fmt, args);
	else
		vprintk(fmt, args);

	va_end(args);
}
52 53 54

static void print_name_offset(struct seq_file *m, void *sym)
{
55
	char symname[KSYM_NAME_LEN];
56

57
	if (lookup_symbol_name((unsigned long)sym, symname) < 0)
58
		SEQ_printf(m, "<%pK>", sym);
59 60
	else
		SEQ_printf(m, "%s", symname);
61 62 63
}

static void
64 65
print_timer(struct seq_file *m, struct hrtimer *taddr, struct hrtimer *timer,
	    int idx, u64 now)
66 67 68 69 70
{
#ifdef CONFIG_TIMER_STATS
	char tmp[TASK_COMM_LEN + 1];
#endif
	SEQ_printf(m, " #%d: ", idx);
71
	print_name_offset(m, taddr);
72 73 74 75 76 77 78 79 80 81 82
	SEQ_printf(m, ", ");
	print_name_offset(m, timer->function);
	SEQ_printf(m, ", S:%02lx", timer->state);
#ifdef CONFIG_TIMER_STATS
	SEQ_printf(m, ", ");
	print_name_offset(m, timer->start_site);
	memcpy(tmp, timer->start_comm, TASK_COMM_LEN);
	tmp[TASK_COMM_LEN] = 0;
	SEQ_printf(m, ", %s/%d", tmp, timer->start_pid);
#endif
	SEQ_printf(m, "\n");
83 84
	SEQ_printf(m, " # expires at %Lu-%Lu nsecs [in %Ld to %Ld nsecs]\n",
		(unsigned long long)ktime_to_ns(hrtimer_get_softexpires(timer)),
85
		(unsigned long long)ktime_to_ns(hrtimer_get_expires(timer)),
86
		(long long)(ktime_to_ns(hrtimer_get_softexpires(timer)) - now),
87
		(long long)(ktime_to_ns(hrtimer_get_expires(timer)) - now));
88 89 90 91 92 93 94 95
}

static void
print_active_timers(struct seq_file *m, struct hrtimer_clock_base *base,
		    u64 now)
{
	struct hrtimer *timer, tmp;
	unsigned long next = 0, i;
96
	struct timerqueue_node *curr;
97 98 99 100
	unsigned long flags;

next_one:
	i = 0;
101
	raw_spin_lock_irqsave(&base->cpu_base->lock, flags);
102

103
	curr = timerqueue_getnext(&base->active);
104 105 106 107 108
	/*
	 * Crude but we have to do this O(N*N) thing, because
	 * we have to unlock the base when printing:
	 */
	while (curr && i < next) {
109
		curr = timerqueue_iterate_next(curr);
110 111 112 113 114
		i++;
	}

	if (curr) {

115
		timer = container_of(curr, struct hrtimer, node);
116
		tmp = *timer;
117
		raw_spin_unlock_irqrestore(&base->cpu_base->lock, flags);
118

119
		print_timer(m, timer, &tmp, i, now);
120 121 122
		next++;
		goto next_one;
	}
123
	raw_spin_unlock_irqrestore(&base->cpu_base->lock, flags);
124 125 126 127 128
}

static void
print_base(struct seq_file *m, struct hrtimer_clock_base *base, u64 now)
{
129
	SEQ_printf(m, "  .base:       %pK\n", base);
130 131 132 133
	SEQ_printf(m, "  .index:      %d\n", base->index);

	SEQ_printf(m, "  .resolution: %u nsecs\n", (unsigned) hrtimer_resolution);

134 135 136 137
	SEQ_printf(m,   "  .get_time:   ");
	print_name_offset(m, base->get_time);
	SEQ_printf(m,   "\n");
#ifdef CONFIG_HIGH_RES_TIMERS
138 139
	SEQ_printf(m, "  .offset:     %Lu nsecs\n",
		   (unsigned long long) ktime_to_ns(base->offset));
140 141 142 143 144 145 146 147 148 149
#endif
	SEQ_printf(m,   "active timers:\n");
	print_active_timers(m, base, now);
}

static void print_cpu(struct seq_file *m, int cpu, u64 now)
{
	struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
	int i;

150
	SEQ_printf(m, "cpu: %d\n", cpu);
151 152 153 154 155
	for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
		SEQ_printf(m, " clock %d:\n", i);
		print_base(m, cpu_base->clock_base + i, now);
	}
#define P(x) \
156 157
	SEQ_printf(m, "  .%-15s: %Lu\n", #x, \
		   (unsigned long long)(cpu_base->x))
158
#define P_ns(x) \
159 160
	SEQ_printf(m, "  .%-15s: %Lu nsecs\n", #x, \
		   (unsigned long long)(ktime_to_ns(cpu_base->x)))
161 162 163 164 165

#ifdef CONFIG_HIGH_RES_TIMERS
	P_ns(expires_next);
	P(hres_active);
	P(nr_events);
166 167
	P(nr_retries);
	P(nr_hangs);
168
	P(max_hang_time);
169 170 171 172 173 174
#endif
#undef P
#undef P_ns

#ifdef CONFIG_TICK_ONESHOT
# define P(x) \
175 176
	SEQ_printf(m, "  .%-15s: %Lu\n", #x, \
		   (unsigned long long)(ts->x))
177
# define P_ns(x) \
178 179
	SEQ_printf(m, "  .%-15s: %Lu nsecs\n", #x, \
		   (unsigned long long)(ktime_to_ns(ts->x)))
180 181 182
	{
		struct tick_sched *ts = tick_get_tick_sched(cpu);
		P(nohz_mode);
183
		P_ns(last_tick);
184 185 186 187 188
		P(tick_stopped);
		P(idle_jiffies);
		P(idle_calls);
		P(idle_sleeps);
		P_ns(idle_entrytime);
189 190
		P_ns(idle_waketime);
		P_ns(idle_exittime);
191
		P_ns(idle_sleeptime);
192
		P_ns(iowait_sleeptime);
193
		P(last_jiffies);
194
		P(next_timer);
195
		P_ns(idle_expires);
196 197
		SEQ_printf(m, "jiffies: %Lu\n",
			   (unsigned long long)jiffies);
198 199 200 201 202
	}
#endif

#undef P
#undef P_ns
203
	SEQ_printf(m, "\n");
204 205 206 207
}

#ifdef CONFIG_GENERIC_CLOCKEVENTS
static void
208
print_tickdevice(struct seq_file *m, struct tick_device *td, int cpu)
209 210 211
{
	struct clock_event_device *dev = td->evtdev;

212
	SEQ_printf(m, "Tick Device: mode:     %d\n", td->mode);
213 214 215 216
	if (cpu < 0)
		SEQ_printf(m, "Broadcast device\n");
	else
		SEQ_printf(m, "Per CPU device: %d\n", cpu);
217 218 219 220 221 222 223

	SEQ_printf(m, "Clock Event Device: ");
	if (!dev) {
		SEQ_printf(m, "<NULL>\n");
		return;
	}
	SEQ_printf(m, "%s\n", dev->name);
224 225 226 227
	SEQ_printf(m, " max_delta_ns:   %llu\n",
		   (unsigned long long) dev->max_delta_ns);
	SEQ_printf(m, " min_delta_ns:   %llu\n",
		   (unsigned long long) dev->min_delta_ns);
228 229
	SEQ_printf(m, " mult:           %u\n", dev->mult);
	SEQ_printf(m, " shift:          %u\n", dev->shift);
230 231 232 233 234 235 236 237
	SEQ_printf(m, " mode:           %d\n", dev->mode);
	SEQ_printf(m, " next_event:     %Ld nsecs\n",
		   (unsigned long long) ktime_to_ns(dev->next_event));

	SEQ_printf(m, " set_next_event: ");
	print_name_offset(m, dev->set_next_event);
	SEQ_printf(m, "\n");

238 239 240 241 242
	if (dev->set_mode) {
		SEQ_printf(m, " set_mode:       ");
		print_name_offset(m, dev->set_mode);
		SEQ_printf(m, "\n");
	} else {
243
		if (dev->set_state_shutdown) {
244
			SEQ_printf(m, " shutdown: ");
245
			print_name_offset(m, dev->set_state_shutdown);
246 247 248
			SEQ_printf(m, "\n");
		}

249
		if (dev->set_state_periodic) {
250
			SEQ_printf(m, " periodic: ");
251
			print_name_offset(m, dev->set_state_periodic);
252 253 254
			SEQ_printf(m, "\n");
		}

255
		if (dev->set_state_oneshot) {
256
			SEQ_printf(m, " oneshot:  ");
257
			print_name_offset(m, dev->set_state_oneshot);
258 259 260
			SEQ_printf(m, "\n");
		}

261
		if (dev->tick_resume) {
262
			SEQ_printf(m, " resume:   ");
263
			print_name_offset(m, dev->tick_resume);
264 265 266
			SEQ_printf(m, "\n");
		}
	}
267 268 269 270

	SEQ_printf(m, " event_handler:  ");
	print_name_offset(m, dev->event_handler);
	SEQ_printf(m, "\n");
271
	SEQ_printf(m, " retries:        %lu\n", dev->retries);
272
	SEQ_printf(m, "\n");
273 274
}

275
static void timer_list_show_tickdevices_header(struct seq_file *m)
276 277
{
#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
278
	print_tickdevice(m, tick_get_broadcast_device(), -1);
279
	SEQ_printf(m, "tick_broadcast_mask: %08lx\n",
280
		   cpumask_bits(tick_get_broadcast_mask())[0]);
281 282
#ifdef CONFIG_TICK_ONESHOT
	SEQ_printf(m, "tick_broadcast_oneshot_mask: %08lx\n",
283
		   cpumask_bits(tick_get_broadcast_oneshot_mask())[0]);
284 285 286 287 288 289
#endif
	SEQ_printf(m, "\n");
#endif
}
#endif

290
static inline void timer_list_header(struct seq_file *m, u64 now)
291
{
292
	SEQ_printf(m, "Timer List Version: v0.8\n");
293 294
	SEQ_printf(m, "HRTIMER_MAX_CLOCK_BASES: %d\n", HRTIMER_MAX_CLOCK_BASES);
	SEQ_printf(m, "now at %Ld nsecs\n", (unsigned long long)now);
295
	SEQ_printf(m, "\n");
296 297 298 299 300 301 302
}

static int timer_list_show(struct seq_file *m, void *v)
{
	struct timer_list_iter *iter = v;

	if (iter->cpu == -1 && !iter->second_pass)
303
		timer_list_header(m, iter->now);
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	else if (!iter->second_pass)
		print_cpu(m, iter->cpu, iter->now);
#ifdef CONFIG_GENERIC_CLOCKEVENTS
	else if (iter->cpu == -1 && iter->second_pass)
		timer_list_show_tickdevices_header(m);
	else
		print_tickdevice(m, tick_get_device(iter->cpu), iter->cpu);
#endif
	return 0;
}

void sysrq_timer_list_show(void)
{
	u64 now = ktime_to_ns(ktime_get());
	int cpu;

	timer_list_header(NULL, now);
321 322

	for_each_online_cpu(cpu)
323
		print_cpu(NULL, cpu, now);
324

325
#ifdef CONFIG_GENERIC_CLOCKEVENTS
326
	timer_list_show_tickdevices_header(NULL);
327
	for_each_online_cpu(cpu)
328
		print_tickdevice(NULL, tick_get_device(cpu), cpu);
329
#endif
330 331
	return;
}
332

333
static void *move_iter(struct timer_list_iter *iter, loff_t offset)
334
{
335 336 337
	for (; offset; offset--) {
		iter->cpu = cpumask_next(iter->cpu, cpu_online_mask);
		if (iter->cpu >= nr_cpu_ids) {
338
#ifdef CONFIG_GENERIC_CLOCKEVENTS
339 340 341 342 343
			if (!iter->second_pass) {
				iter->cpu = -1;
				iter->second_pass = true;
			} else
				return NULL;
344
#else
345
			return NULL;
346
#endif
347
		}
348 349
	}
	return iter;
350 351
}

352 353 354 355 356 357 358 359 360 361 362
static void *timer_list_start(struct seq_file *file, loff_t *offset)
{
	struct timer_list_iter *iter = file->private;

	if (!*offset)
		iter->now = ktime_to_ns(ktime_get());
	iter->cpu = -1;
	iter->second_pass = false;
	return move_iter(iter, *offset);
}

363
static void *timer_list_next(struct seq_file *file, void *v, loff_t *offset)
364
{
365 366
	struct timer_list_iter *iter = file->private;
	++*offset;
367
	return move_iter(iter, 1);
368 369
}

370 371 372 373 374 375 376 377 378 379 380
static void timer_list_stop(struct seq_file *seq, void *v)
{
}

static const struct seq_operations timer_list_sops = {
	.start = timer_list_start,
	.next = timer_list_next,
	.stop = timer_list_stop,
	.show = timer_list_show,
};

381 382
static int timer_list_open(struct inode *inode, struct file *filp)
{
383 384
	return seq_open_private(filp, &timer_list_sops,
			sizeof(struct timer_list_iter));
385 386
}

387
static const struct file_operations timer_list_fops = {
388 389 390
	.open		= timer_list_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
391
	.release	= seq_release_private,
392 393 394 395 396 397
};

static int __init init_timer_list_procfs(void)
{
	struct proc_dir_entry *pe;

398
	pe = proc_create("timer_list", 0444, NULL, &timer_list_fops);
399 400 401 402 403
	if (!pe)
		return -ENOMEM;
	return 0;
}
__initcall(init_timer_list_procfs);