timer_list.c 8.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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 <linux/tick.h>

#include <asm/uaccess.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 38 39 40 41 42 43 44 45 46 47
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):
 */
#define SEQ_printf(m, x...)			\
 do {						\
	if (m)					\
		seq_printf(m, x);		\
	else					\
		printk(x);			\
 } while (0)

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

50
	if (lookup_symbol_name((unsigned long)sym, symname) < 0)
51
		SEQ_printf(m, "<%pK>", sym);
52 53
	else
		SEQ_printf(m, "%s", symname);
54 55 56
}

static void
57 58
print_timer(struct seq_file *m, struct hrtimer *taddr, struct hrtimer *timer,
	    int idx, u64 now)
59 60 61 62 63
{
#ifdef CONFIG_TIMER_STATS
	char tmp[TASK_COMM_LEN + 1];
#endif
	SEQ_printf(m, " #%d: ", idx);
64
	print_name_offset(m, taddr);
65 66 67 68 69 70 71 72 73 74 75
	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");
76 77
	SEQ_printf(m, " # expires at %Lu-%Lu nsecs [in %Ld to %Ld nsecs]\n",
		(unsigned long long)ktime_to_ns(hrtimer_get_softexpires(timer)),
78
		(unsigned long long)ktime_to_ns(hrtimer_get_expires(timer)),
79
		(long long)(ktime_to_ns(hrtimer_get_softexpires(timer)) - now),
80
		(long long)(ktime_to_ns(hrtimer_get_expires(timer)) - now));
81 82 83 84 85 86 87 88
}

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;
89
	struct timerqueue_node *curr;
90 91 92 93
	unsigned long flags;

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

96
	curr = timerqueue_getnext(&base->active);
97 98 99 100 101
	/*
	 * Crude but we have to do this O(N*N) thing, because
	 * we have to unlock the base when printing:
	 */
	while (curr && i < next) {
102
		curr = timerqueue_iterate_next(curr);
103 104 105 106 107
		i++;
	}

	if (curr) {

108
		timer = container_of(curr, struct hrtimer, node);
109
		tmp = *timer;
110
		raw_spin_unlock_irqrestore(&base->cpu_base->lock, flags);
111

112
		print_timer(m, timer, &tmp, i, now);
113 114 115
		next++;
		goto next_one;
	}
116
	raw_spin_unlock_irqrestore(&base->cpu_base->lock, flags);
117 118 119 120 121
}

static void
print_base(struct seq_file *m, struct hrtimer_clock_base *base, u64 now)
{
122
	SEQ_printf(m, "  .base:       %pK\n", base);
123 124
	SEQ_printf(m, "  .index:      %d\n",
			base->index);
125
	SEQ_printf(m, "  .resolution: %Lu nsecs\n",
126 127 128 129 130
			(unsigned long long)ktime_to_ns(base->resolution));
	SEQ_printf(m,   "  .get_time:   ");
	print_name_offset(m, base->get_time);
	SEQ_printf(m,   "\n");
#ifdef CONFIG_HIGH_RES_TIMERS
131 132
	SEQ_printf(m, "  .offset:     %Lu nsecs\n",
		   (unsigned long long) ktime_to_ns(base->offset));
133 134 135 136 137 138 139 140 141 142
#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;

143
	SEQ_printf(m, "cpu: %d\n", cpu);
144 145 146 147 148
	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) \
149 150
	SEQ_printf(m, "  .%-15s: %Lu\n", #x, \
		   (unsigned long long)(cpu_base->x))
151
#define P_ns(x) \
152 153
	SEQ_printf(m, "  .%-15s: %Lu nsecs\n", #x, \
		   (unsigned long long)(ktime_to_ns(cpu_base->x)))
154 155 156 157 158

#ifdef CONFIG_HIGH_RES_TIMERS
	P_ns(expires_next);
	P(hres_active);
	P(nr_events);
159 160 161
	P(nr_retries);
	P(nr_hangs);
	P_ns(max_hang_time);
162 163 164 165 166 167
#endif
#undef P
#undef P_ns

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

#undef P
#undef P_ns
196
	SEQ_printf(m, "\n");
197 198 199 200
}

#ifdef CONFIG_GENERIC_CLOCKEVENTS
static void
201
print_tickdevice(struct seq_file *m, struct tick_device *td, int cpu)
202 203 204
{
	struct clock_event_device *dev = td->evtdev;

205
	SEQ_printf(m, "Tick Device: mode:     %d\n", td->mode);
206 207 208 209
	if (cpu < 0)
		SEQ_printf(m, "Broadcast device\n");
	else
		SEQ_printf(m, "Per CPU device: %d\n", cpu);
210 211 212 213 214 215 216

	SEQ_printf(m, "Clock Event Device: ");
	if (!dev) {
		SEQ_printf(m, "<NULL>\n");
		return;
	}
	SEQ_printf(m, "%s\n", dev->name);
217 218 219 220
	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);
221 222
	SEQ_printf(m, " mult:           %u\n", dev->mult);
	SEQ_printf(m, " shift:          %u\n", dev->shift);
223 224 225 226 227 228 229 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");

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

	SEQ_printf(m, " event_handler:  ");
	print_name_offset(m, dev->event_handler);
	SEQ_printf(m, "\n");
238
	SEQ_printf(m, " retries:        %lu\n", dev->retries);
239
	SEQ_printf(m, "\n");
240 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: %08lx\n",
247
		   cpumask_bits(tick_get_broadcast_mask())[0]);
248 249
#ifdef CONFIG_TICK_ONESHOT
	SEQ_printf(m, "tick_broadcast_oneshot_mask: %08lx\n",
250
		   cpumask_bits(tick_get_broadcast_oneshot_mask())[0]);
251 252 253 254 255 256
#endif
	SEQ_printf(m, "\n");
#endif
}
#endif

257
static inline void timer_list_header(struct seq_file *m, u64 now)
258
{
259
	SEQ_printf(m, "Timer List Version: v0.7\n");
260 261
	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);
262
	SEQ_printf(m, "\n");
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
}

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

	if (iter->cpu == -1 && !iter->second_pass)
		timer_list_header(m, now);
	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);
289 290

	for_each_online_cpu(cpu)
291
		print_cpu(NULL, cpu, now);
292

293
#ifdef CONFIG_GENERIC_CLOCKEVENTS
294
	timer_list_show_tickdevices_header(NULL);
295
	for_each_online_cpu(cpu)
296
		print_tickdevice(NULL, tick_get_device(cpu), cpu);
297
#endif
298 299
	return;
}
300

301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
static void *timer_list_start(struct seq_file *file, loff_t *offset)
{
	struct timer_list_iter *iter = file->private;

	if (!*offset) {
		iter->cpu = -1;
		iter->now = ktime_to_ns(ktime_get());
	} else if (iter->cpu >= nr_cpu_ids) {
#ifdef CONFIG_GENERIC_CLOCKEVENTS
		if (!iter->second_pass) {
			iter->cpu = -1;
			iter->second_pass = true;
		} else
			return NULL;
#else
		return NULL;
#endif
	}
	return iter;
320 321
}

322
static void *timer_list_next(struct seq_file *file, void *v, loff_t *offset)
323
{
324 325 326 327
	struct timer_list_iter *iter = file->private;
	iter->cpu = cpumask_next(iter->cpu, cpu_online_mask);
	++*offset;
	return timer_list_start(file, offset);
328 329
}

330 331 332 333 334 335 336 337 338 339 340
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,
};

341 342
static int timer_list_open(struct inode *inode, struct file *filp)
{
343 344
	return seq_open_private(filp, &timer_list_sops,
			sizeof(struct timer_list_iter));
345 346
}

347
static const struct file_operations timer_list_fops = {
348 349 350
	.open		= timer_list_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
351
	.release	= seq_release_private,
352 353 354 355 356 357
};

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

358
	pe = proc_create("timer_list", 0444, NULL, &timer_list_fops);
359 360 361 362 363
	if (!pe)
		return -ENOMEM;
	return 0;
}
__initcall(init_timer_list_procfs);