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