2 * kernel/time/timer_stats.c
4 * Collect timer usage statistics.
6 * Copyright(C) 2006, Red Hat, Inc., Ingo Molnar
7 * Copyright(C) 2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
9 * timer_stats is based on timer_top, a similar functionality which was part of
10 * Con Kolivas dyntick patch set. It was developed by Daniel Petrini at the
11 * Instituto Nokia de Tecnologia - INdT - Manaus. timer_top's design was based
12 * on dynamic allocation of the statistics entries and linear search based
13 * lookup combined with a global lock, rather than the static array, hash
14 * and per-CPU locking which is used by timer_stats. It was written for the
15 * pre hrtimer kernel code and therefore did not take hrtimers into account.
16 * Nevertheless it provided the base for the timer_stats implementation and
17 * was a helpful source of inspiration. Kudos to Daniel and the Nokia folks
21 * Copyright (C) 2005 Instituto Nokia de Tecnologia - INdT - Manaus
22 * Written by Daniel Petrini <d.pensator@gmail.com>
23 * timer_top.c was released under the GNU General Public License version 2
25 * We export the addresses and counting of timer functions being called,
26 * the pid and cmdline from the owner process if applicable.
28 * Start/stop data collection:
29 * # echo 1[0] >/proc/timer_stats
31 * Display the information collected so far:
32 * # cat /proc/timer_stats
34 * This program is free software; you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License version 2 as
36 * published by the Free Software Foundation.
39 #include <linux/proc_fs.h>
40 #include <linux/module.h>
41 #include <linux/spinlock.h>
42 #include <linux/sched.h>
43 #include <linux/seq_file.h>
44 #include <linux/kallsyms.h>
46 #include <asm/uaccess.h>
49 * This is our basic unit of interest: a timer expiry event identified
50 * by the timer, its start/expire functions and the PID of the task that
51 * started the timer. We count the number of times an event happens:
68 * Number of timeout events:
71 unsigned int timer_flag;
74 * We save the command-line string to preserve
75 * this information past task exit:
77 char comm[TASK_COMM_LEN + 1];
79 } ____cacheline_aligned_in_smp;
82 * Spinlock protecting the tables - not taken during lookup:
84 static DEFINE_SPINLOCK(table_lock);
87 * Per-CPU lookup locks for fast hash lookup:
89 static DEFINE_PER_CPU(spinlock_t, lookup_lock);
92 * Mutex to serialize state changes with show-stats activities:
94 static DEFINE_MUTEX(show_mutex);
97 * Collection status, active/inactive:
99 static int __read_mostly active;
102 * Beginning/end timestamps of measurement:
104 static ktime_t time_start, time_stop;
107 * tstat entry structs only get allocated while collection is
108 * active and never freed during that time - this simplifies
109 * things quite a bit.
111 * They get freed when a new collection period is started.
113 #define MAX_ENTRIES_BITS 10
114 #define MAX_ENTRIES (1UL << MAX_ENTRIES_BITS)
116 static unsigned long nr_entries;
117 static struct entry entries[MAX_ENTRIES];
119 static atomic_t overflow_count;
122 * The entries are in a hash-table, for fast lookup:
124 #define TSTAT_HASH_BITS (MAX_ENTRIES_BITS - 1)
125 #define TSTAT_HASH_SIZE (1UL << TSTAT_HASH_BITS)
126 #define TSTAT_HASH_MASK (TSTAT_HASH_SIZE - 1)
128 #define __tstat_hashfn(entry) \
129 (((unsigned long)(entry)->timer ^ \
130 (unsigned long)(entry)->start_func ^ \
131 (unsigned long)(entry)->expire_func ^ \
132 (unsigned long)(entry)->pid ) & TSTAT_HASH_MASK)
134 #define tstat_hashentry(entry) (tstat_hash_table + __tstat_hashfn(entry))
136 static struct entry *tstat_hash_table[TSTAT_HASH_SIZE] __read_mostly;
138 static void reset_entries(void)
141 memset(entries, 0, sizeof(entries));
142 memset(tstat_hash_table, 0, sizeof(tstat_hash_table));
143 atomic_set(&overflow_count, 0);
146 static struct entry *alloc_entry(void)
148 if (nr_entries >= MAX_ENTRIES)
151 return entries + nr_entries++;
154 static int match_entries(struct entry *entry1, struct entry *entry2)
156 return entry1->timer == entry2->timer &&
157 entry1->start_func == entry2->start_func &&
158 entry1->expire_func == entry2->expire_func &&
159 entry1->pid == entry2->pid;
163 * Look up whether an entry matching this item is present
164 * in the hash already. Must be called with irqs off and the
167 static struct entry *tstat_lookup(struct entry *entry, char *comm)
169 struct entry **head, *curr, *prev;
171 head = tstat_hashentry(entry);
175 * The fastpath is when the entry is already hashed,
176 * we do this with the lookup lock held, but with the
177 * table lock not held:
180 if (match_entries(curr, entry))
186 * Slowpath: allocate, set up and link a new hash entry:
191 spin_lock(&table_lock);
193 * Make sure we have not raced with another CPU:
196 if (match_entries(curr, entry))
203 curr = alloc_entry();
208 memcpy(curr->comm, comm, TASK_COMM_LEN);
210 smp_mb(); /* Ensure that curr is initialized before insert */
218 spin_unlock(&table_lock);
224 * timer_stats_update_stats - Update the statistics for a timer.
225 * @timer: pointer to either a timer_list or a hrtimer
226 * @pid: the pid of the task which set up the timer
227 * @startf: pointer to the function which did the timer setup
228 * @timerf: pointer to the timer callback function of the timer
229 * @comm: name of the process which set up the timer
231 * When the timer is already registered, then the event counter is
232 * incremented. Otherwise the timer is registered in a free slot.
234 void timer_stats_update_stats(void *timer, pid_t pid, void *startf,
235 void *timerf, char *comm,
236 unsigned int timer_flag)
239 * It doesnt matter which lock we take:
242 struct entry *entry, input;
248 lock = &per_cpu(lookup_lock, raw_smp_processor_id());
251 input.start_func = startf;
252 input.expire_func = timerf;
254 input.timer_flag = timer_flag;
256 spin_lock_irqsave(lock, flags);
260 entry = tstat_lookup(&input, comm);
264 atomic_inc(&overflow_count);
267 spin_unlock_irqrestore(lock, flags);
270 static void print_name_offset(struct seq_file *m, unsigned long addr)
272 char symname[KSYM_NAME_LEN];
274 if (lookup_symbol_name(addr, symname) < 0)
275 seq_printf(m, "<%p>", (void *)addr);
277 seq_printf(m, "%s", symname);
280 static int tstats_show(struct seq_file *m, void *v)
282 struct timespec period;
289 mutex_lock(&show_mutex);
291 * If still active then calculate up to now:
294 time_stop = ktime_get();
296 time = ktime_sub(time_stop, time_start);
298 period = ktime_to_timespec(time);
299 ms = period.tv_nsec / 1000000;
301 seq_puts(m, "Timer Stats Version: v0.2\n");
302 seq_printf(m, "Sample period: %ld.%03ld s\n", period.tv_sec, ms);
303 if (atomic_read(&overflow_count))
304 seq_printf(m, "Overflow: %d entries\n",
305 atomic_read(&overflow_count));
307 for (i = 0; i < nr_entries; i++) {
309 if (entry->timer_flag & TIMER_STATS_FLAG_DEFERRABLE) {
310 seq_printf(m, "%4luD, %5d %-16s ",
311 entry->count, entry->pid, entry->comm);
313 seq_printf(m, " %4lu, %5d %-16s ",
314 entry->count, entry->pid, entry->comm);
317 print_name_offset(m, (unsigned long)entry->start_func);
319 print_name_offset(m, (unsigned long)entry->expire_func);
322 events += entry->count;
325 ms += period.tv_sec * 1000;
329 if (events && period.tv_sec)
330 seq_printf(m, "%ld total events, %ld.%03ld events/sec\n",
331 events, events * 1000 / ms,
332 (events * 1000000 / ms) % 1000);
334 seq_printf(m, "%ld total events\n", events);
336 mutex_unlock(&show_mutex);
342 * After a state change, make sure all concurrent lookup/update
343 * activities have stopped:
345 static void sync_access(void)
350 for_each_online_cpu(cpu) {
351 spin_lock_irqsave(&per_cpu(lookup_lock, cpu), flags);
353 spin_unlock_irqrestore(&per_cpu(lookup_lock, cpu), flags);
357 static ssize_t tstats_write(struct file *file, const char __user *buf,
358 size_t count, loff_t *offs)
362 if (count != 2 || *offs)
365 if (copy_from_user(ctl, buf, count))
368 mutex_lock(&show_mutex);
373 time_stop = ktime_get();
380 time_start = ktime_get();
388 mutex_unlock(&show_mutex);
393 static int tstats_open(struct inode *inode, struct file *filp)
395 return single_open(filp, tstats_show, NULL);
398 static struct file_operations tstats_fops = {
401 .write = tstats_write,
403 .release = single_release,
406 void __init init_timer_stats(void)
410 for_each_possible_cpu(cpu)
411 spin_lock_init(&per_cpu(lookup_lock, cpu));
414 static int __init init_tstats_procfs(void)
416 struct proc_dir_entry *pe;
418 pe = create_proc_entry("timer_stats", 0644, NULL);
422 pe->proc_fops = &tstats_fops;
426 __initcall(init_tstats_procfs);