Blame view

kernel/time/timer_stats.c 9.91 KB
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  /*
   * kernel/time/timer_stats.c
   *
   * Collect timer usage statistics.
   *
   * Copyright(C) 2006, Red Hat, Inc., Ingo Molnar
   * Copyright(C) 2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
   *
   * timer_stats is based on timer_top, a similar functionality which was part of
   * Con Kolivas dyntick patch set. It was developed by Daniel Petrini at the
   * Instituto Nokia de Tecnologia - INdT - Manaus. timer_top's design was based
   * on dynamic allocation of the statistics entries and linear search based
   * lookup combined with a global lock, rather than the static array, hash
   * and per-CPU locking which is used by timer_stats. It was written for the
   * pre hrtimer kernel code and therefore did not take hrtimers into account.
   * Nevertheless it provided the base for the timer_stats implementation and
   * was a helpful source of inspiration. Kudos to Daniel and the Nokia folks
   * for this effort.
   *
   * timer_top.c is
   *	Copyright (C) 2005 Instituto Nokia de Tecnologia - INdT - Manaus
   *	Written by Daniel Petrini <d.pensator@gmail.com>
   *	timer_top.c was released under the GNU General Public License version 2
   *
   * We export the addresses and counting of timer functions being called,
   * the pid and cmdline from the owner process if applicable.
   *
   * Start/stop data collection:
b10db7f0d   Pavel Machek   time: more timer ...
29
   * # echo [1|0] >/proc/timer_stats
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
   *
   * Display the information collected so far:
   * # cat /proc/timer_stats
   *
   * 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>
  
  /*
   * This is our basic unit of interest: a timer expiry event identified
   * by the timer, its start/expire functions and the PID of the task that
   * started the timer. We count the number of times an event happens:
   */
  struct entry {
  	/*
  	 * Hash list:
  	 */
  	struct entry		*next;
  
  	/*
  	 * Hash keys:
  	 */
  	void			*timer;
  	void			*start_func;
  	void			*expire_func;
  	pid_t			pid;
  
  	/*
  	 * Number of timeout events:
  	 */
  	unsigned long		count;
c5c061b8f   Venki Pallipadi   Add a flag to ind...
71
  	unsigned int		timer_flag;
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
72
73
74
75
76
77
78
79
80
81
82
83
  
  	/*
  	 * We save the command-line string to preserve
  	 * this information past task exit:
  	 */
  	char			comm[TASK_COMM_LEN + 1];
  
  } ____cacheline_aligned_in_smp;
  
  /*
   * Spinlock protecting the tables - not taken during lookup:
   */
2737c49f2   Thomas Gleixner   locking, timer_st...
84
  static DEFINE_RAW_SPINLOCK(table_lock);
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
85
86
87
88
  
  /*
   * Per-CPU lookup locks for fast hash lookup:
   */
ecb49d1a6   Thomas Gleixner   hrtimers: Convert...
89
  static DEFINE_PER_CPU(raw_spinlock_t, tstats_lookup_lock);
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
90
91
92
93
94
95
96
97
98
  
  /*
   * Mutex to serialize state changes with show-stats activities:
   */
  static DEFINE_MUTEX(show_mutex);
  
  /*
   * Collection status, active/inactive:
   */
507e12315   Heiko Carstens   timer stats: Opti...
99
  int __read_mostly timer_stats_active;
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  
  /*
   * Beginning/end timestamps of measurement:
   */
  static ktime_t time_start, time_stop;
  
  /*
   * tstat entry structs only get allocated while collection is
   * active and never freed during that time - this simplifies
   * things quite a bit.
   *
   * They get freed when a new collection period is started.
   */
  #define MAX_ENTRIES_BITS	10
  #define MAX_ENTRIES		(1UL << MAX_ENTRIES_BITS)
  
  static unsigned long nr_entries;
  static struct entry entries[MAX_ENTRIES];
  
  static atomic_t overflow_count;
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
  /*
   * The entries are in a hash-table, for fast lookup:
   */
  #define TSTAT_HASH_BITS		(MAX_ENTRIES_BITS - 1)
  #define TSTAT_HASH_SIZE		(1UL << TSTAT_HASH_BITS)
  #define TSTAT_HASH_MASK		(TSTAT_HASH_SIZE - 1)
  
  #define __tstat_hashfn(entry)						\
  	(((unsigned long)(entry)->timer       ^				\
  	  (unsigned long)(entry)->start_func  ^				\
  	  (unsigned long)(entry)->expire_func ^				\
  	  (unsigned long)(entry)->pid		) & TSTAT_HASH_MASK)
  
  #define tstat_hashentry(entry)	(tstat_hash_table + __tstat_hashfn(entry))
  
  static struct entry *tstat_hash_table[TSTAT_HASH_SIZE] __read_mostly;
9fcc15ec3   Björn Steinbrink   timer statistics:...
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  static void reset_entries(void)
  {
  	nr_entries = 0;
  	memset(entries, 0, sizeof(entries));
  	memset(tstat_hash_table, 0, sizeof(tstat_hash_table));
  	atomic_set(&overflow_count, 0);
  }
  
  static struct entry *alloc_entry(void)
  {
  	if (nr_entries >= MAX_ENTRIES)
  		return NULL;
  
  	return entries + nr_entries++;
  }
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
  static int match_entries(struct entry *entry1, struct entry *entry2)
  {
  	return entry1->timer       == entry2->timer	  &&
  	       entry1->start_func  == entry2->start_func  &&
  	       entry1->expire_func == entry2->expire_func &&
  	       entry1->pid	   == entry2->pid;
  }
  
  /*
   * Look up whether an entry matching this item is present
   * in the hash already. Must be called with irqs off and the
   * lookup lock held:
   */
  static struct entry *tstat_lookup(struct entry *entry, char *comm)
  {
  	struct entry **head, *curr, *prev;
  
  	head = tstat_hashentry(entry);
  	curr = *head;
  
  	/*
  	 * The fastpath is when the entry is already hashed,
  	 * we do this with the lookup lock held, but with the
  	 * table lock not held:
  	 */
  	while (curr) {
  		if (match_entries(curr, entry))
  			return curr;
  
  		curr = curr->next;
  	}
  	/*
  	 * Slowpath: allocate, set up and link a new hash entry:
  	 */
  	prev = NULL;
  	curr = *head;
2737c49f2   Thomas Gleixner   locking, timer_st...
187
  	raw_spin_lock(&table_lock);
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
  	/*
  	 * Make sure we have not raced with another CPU:
  	 */
  	while (curr) {
  		if (match_entries(curr, entry))
  			goto out_unlock;
  
  		prev = curr;
  		curr = curr->next;
  	}
  
  	curr = alloc_entry();
  	if (curr) {
  		*curr = *entry;
  		curr->count = 0;
9fcc15ec3   Björn Steinbrink   timer statistics:...
203
  		curr->next = NULL;
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
204
  		memcpy(curr->comm, comm, TASK_COMM_LEN);
9fcc15ec3   Björn Steinbrink   timer statistics:...
205
206
  
  		smp_mb(); /* Ensure that curr is initialized before insert */
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
207
208
209
210
  		if (prev)
  			prev->next = curr;
  		else
  			*head = curr;
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
211
212
  	}
   out_unlock:
2737c49f2   Thomas Gleixner   locking, timer_st...
213
  	raw_spin_unlock(&table_lock);
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  
  	return curr;
  }
  
  /**
   * timer_stats_update_stats - Update the statistics for a timer.
   * @timer:	pointer to either a timer_list or a hrtimer
   * @pid:	the pid of the task which set up the timer
   * @startf:	pointer to the function which did the timer setup
   * @timerf:	pointer to the timer callback function of the timer
   * @comm:	name of the process which set up the timer
   *
   * When the timer is already registered, then the event counter is
   * incremented. Otherwise the timer is registered in a free slot.
   */
  void timer_stats_update_stats(void *timer, pid_t pid, void *startf,
c5c061b8f   Venki Pallipadi   Add a flag to ind...
230
231
  			      void *timerf, char *comm,
  			      unsigned int timer_flag)
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
232
233
  {
  	/*
25985edce   Lucas De Marchi   Fix common misspe...
234
  	 * It doesn't matter which lock we take:
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
235
  	 */
ecb49d1a6   Thomas Gleixner   hrtimers: Convert...
236
  	raw_spinlock_t *lock;
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
237
238
  	struct entry *entry, input;
  	unsigned long flags;
507e12315   Heiko Carstens   timer stats: Opti...
239
  	if (likely(!timer_stats_active))
c1a834dc7   Ingo Molnar   timer stats: spee...
240
  		return;
1871e52c7   Tejun Heo   percpu: make perc...
241
  	lock = &per_cpu(tstats_lookup_lock, raw_smp_processor_id());
c1a834dc7   Ingo Molnar   timer stats: spee...
242

82f67cd9f   Ingo Molnar   [PATCH] Add debug...
243
244
245
246
  	input.timer = timer;
  	input.start_func = startf;
  	input.expire_func = timerf;
  	input.pid = pid;
c5c061b8f   Venki Pallipadi   Add a flag to ind...
247
  	input.timer_flag = timer_flag;
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
248

ecb49d1a6   Thomas Gleixner   hrtimers: Convert...
249
  	raw_spin_lock_irqsave(lock, flags);
507e12315   Heiko Carstens   timer stats: Opti...
250
  	if (!timer_stats_active)
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
251
252
253
254
255
256
257
258
259
  		goto out_unlock;
  
  	entry = tstat_lookup(&input, comm);
  	if (likely(entry))
  		entry->count++;
  	else
  		atomic_inc(&overflow_count);
  
   out_unlock:
ecb49d1a6   Thomas Gleixner   hrtimers: Convert...
260
  	raw_spin_unlock_irqrestore(lock, flags);
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
261
262
263
264
  }
  
  static void print_name_offset(struct seq_file *m, unsigned long addr)
  {
9281acea6   Tejun Heo   kallsyms: make KS...
265
  	char symname[KSYM_NAME_LEN];
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
266

9d65cb4a1   Alexey Dobriyan   Fix race between ...
267
  	if (lookup_symbol_name(addr, symname) < 0)
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
268
  		seq_printf(m, "<%p>", (void *)addr);
9d65cb4a1   Alexey Dobriyan   Fix race between ...
269
270
  	else
  		seq_printf(m, "%s", symname);
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
  }
  
  static int tstats_show(struct seq_file *m, void *v)
  {
  	struct timespec period;
  	struct entry *entry;
  	unsigned long ms;
  	long events = 0;
  	ktime_t time;
  	int i;
  
  	mutex_lock(&show_mutex);
  	/*
  	 * If still active then calculate up to now:
  	 */
507e12315   Heiko Carstens   timer stats: Opti...
286
  	if (timer_stats_active)
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
287
288
289
290
291
292
  		time_stop = ktime_get();
  
  	time = ktime_sub(time_stop, time_start);
  
  	period = ktime_to_timespec(time);
  	ms = period.tv_nsec / 1000000;
c5c061b8f   Venki Pallipadi   Add a flag to ind...
293
294
  	seq_puts(m, "Timer Stats Version: v0.2
  ");
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
295
296
297
298
299
300
301
302
303
  	seq_printf(m, "Sample period: %ld.%03ld s
  ", period.tv_sec, ms);
  	if (atomic_read(&overflow_count))
  		seq_printf(m, "Overflow: %d entries
  ",
  			atomic_read(&overflow_count));
  
  	for (i = 0; i < nr_entries; i++) {
  		entry = entries + i;
c5c061b8f   Venki Pallipadi   Add a flag to ind...
304
305
   		if (entry->timer_flag & TIMER_STATS_FLAG_DEFERRABLE) {
  			seq_printf(m, "%4luD, %5d %-16s ",
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
306
  				entry->count, entry->pid, entry->comm);
c5c061b8f   Venki Pallipadi   Add a flag to ind...
307
308
309
310
  		} else {
  			seq_printf(m, " %4lu, %5d %-16s ",
  				entry->count, entry->pid, entry->comm);
  		}
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
  
  		print_name_offset(m, (unsigned long)entry->start_func);
  		seq_puts(m, " (");
  		print_name_offset(m, (unsigned long)entry->expire_func);
  		seq_puts(m, ")
  ");
  
  		events += entry->count;
  	}
  
  	ms += period.tv_sec * 1000;
  	if (!ms)
  		ms = 1;
  
  	if (events && period.tv_sec)
74922be14   Anton Blanchard   Fix timer_stats p...
326
327
328
329
  		seq_printf(m, "%ld total events, %ld.%03ld events/sec
  ",
  			   events, events * 1000 / ms,
  			   (events * 1000000 / ms) % 1000);
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
  	else
  		seq_printf(m, "%ld total events
  ", events);
  
  	mutex_unlock(&show_mutex);
  
  	return 0;
  }
  
  /*
   * After a state change, make sure all concurrent lookup/update
   * activities have stopped:
   */
  static void sync_access(void)
  {
  	unsigned long flags;
  	int cpu;
  
  	for_each_online_cpu(cpu) {
ecb49d1a6   Thomas Gleixner   hrtimers: Convert...
349
350
351
  		raw_spinlock_t *lock = &per_cpu(tstats_lookup_lock, cpu);
  
  		raw_spin_lock_irqsave(lock, flags);
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
352
  		/* nothing */
ecb49d1a6   Thomas Gleixner   hrtimers: Convert...
353
  		raw_spin_unlock_irqrestore(lock, flags);
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
  	}
  }
  
  static ssize_t tstats_write(struct file *file, const char __user *buf,
  			    size_t count, loff_t *offs)
  {
  	char ctl[2];
  
  	if (count != 2 || *offs)
  		return -EINVAL;
  
  	if (copy_from_user(ctl, buf, count))
  		return -EFAULT;
  
  	mutex_lock(&show_mutex);
  	switch (ctl[0]) {
  	case '0':
507e12315   Heiko Carstens   timer stats: Opti...
371
372
  		if (timer_stats_active) {
  			timer_stats_active = 0;
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
373
374
375
376
377
  			time_stop = ktime_get();
  			sync_access();
  		}
  		break;
  	case '1':
507e12315   Heiko Carstens   timer stats: Opti...
378
  		if (!timer_stats_active) {
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
379
380
  			reset_entries();
  			time_start = ktime_get();
9fcc15ec3   Björn Steinbrink   timer statistics:...
381
  			smp_mb();
507e12315   Heiko Carstens   timer stats: Opti...
382
  			timer_stats_active = 1;
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
383
384
385
386
387
388
389
390
391
392
393
394
395
396
  		}
  		break;
  	default:
  		count = -EINVAL;
  	}
  	mutex_unlock(&show_mutex);
  
  	return count;
  }
  
  static int tstats_open(struct inode *inode, struct file *filp)
  {
  	return single_open(filp, tstats_show, NULL);
  }
828c09509   Alexey Dobriyan   const: constify r...
397
  static const struct file_operations tstats_fops = {
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
398
399
400
401
  	.open		= tstats_open,
  	.read		= seq_read,
  	.write		= tstats_write,
  	.llseek		= seq_lseek,
5ea473a1d   Alexey Dobriyan   Fix leaks on /pro...
402
  	.release	= single_release,
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
403
404
405
406
407
408
409
  };
  
  void __init init_timer_stats(void)
  {
  	int cpu;
  
  	for_each_possible_cpu(cpu)
ecb49d1a6   Thomas Gleixner   hrtimers: Convert...
410
  		raw_spin_lock_init(&per_cpu(tstats_lookup_lock, cpu));
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
411
412
413
414
415
  }
  
  static int __init init_tstats_procfs(void)
  {
  	struct proc_dir_entry *pe;
c33fff0af   Denis V. Lunev   kernel: use non-r...
416
  	pe = proc_create("timer_stats", 0644, NULL, &tstats_fops);
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
417
418
  	if (!pe)
  		return -ENOMEM;
82f67cd9f   Ingo Molnar   [PATCH] Add debug...
419
420
421
  	return 0;
  }
  __initcall(init_tstats_procfs);