2 * latencytop.c: Latency display infrastructure
4 * (C) Copyright 2008 Intel Corporation
5 * Author: Arjan van de Ven <arjan@linux.intel.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; version 2
12 #include <linux/latencytop.h>
13 #include <linux/kallsyms.h>
14 #include <linux/seq_file.h>
15 #include <linux/notifier.h>
16 #include <linux/spinlock.h>
17 #include <linux/proc_fs.h>
18 #include <linux/module.h>
19 #include <linux/sched.h>
20 #include <linux/list.h>
21 #include <linux/slab.h>
22 #include <linux/stacktrace.h>
24 static DEFINE_SPINLOCK(latency_lock);
27 static struct latency_record latency_record[MAXLR];
29 int latencytop_enabled;
31 void clear_all_latency_tracing(struct task_struct *p)
35 if (!latencytop_enabled)
38 spin_lock_irqsave(&latency_lock, flags);
39 memset(&p->latency_record, 0, sizeof(p->latency_record));
40 p->latency_record_count = 0;
41 spin_unlock_irqrestore(&latency_lock, flags);
44 static void clear_global_latency_tracing(void)
48 spin_lock_irqsave(&latency_lock, flags);
49 memset(&latency_record, 0, sizeof(latency_record));
50 spin_unlock_irqrestore(&latency_lock, flags);
54 account_global_scheduler_latency(struct task_struct *tsk, struct latency_record *lat)
56 int firstnonnull = MAXLR + 1;
59 if (!latencytop_enabled)
62 /* skip kernel threads for now */
66 for (i = 0; i < MAXLR; i++) {
70 if (!latency_record[i].backtrace[0]) {
75 for (q = 0 ; q < LT_BACKTRACEDEPTH ; q++) {
76 unsigned long record = lat->backtrace[q];
78 if (latency_record[i].backtrace[q] != record) {
83 /* 0 and ULONG_MAX entries mean end of backtrace: */
84 if (record == 0 || record == ULONG_MAX)
88 latency_record[i].count++;
89 latency_record[i].time += lat->time;
90 if (lat->time > latency_record[i].max)
91 latency_record[i].max = lat->time;
100 /* Allocted a new one: */
101 memcpy(&latency_record[i], lat, sizeof(struct latency_record));
104 static inline void store_stacktrace(struct task_struct *tsk, struct latency_record *lat)
106 struct stack_trace trace;
108 memset(&trace, 0, sizeof(trace));
109 trace.max_entries = LT_BACKTRACEDEPTH;
110 trace.entries = &lat->backtrace[0];
112 save_stack_trace_tsk(tsk, &trace);
116 account_scheduler_latency(struct task_struct *tsk, int usecs, int inter)
120 struct latency_record lat;
122 if (!latencytop_enabled)
125 /* Long interruptible waits are generally user requested... */
126 if (inter && usecs > 5000)
129 memset(&lat, 0, sizeof(lat));
133 store_stacktrace(tsk, &lat);
135 spin_lock_irqsave(&latency_lock, flags);
137 account_global_scheduler_latency(tsk, &lat);
140 * short term hack; if we're > 32 we stop; future we recycle:
142 tsk->latency_record_count++;
143 if (tsk->latency_record_count >= LT_SAVECOUNT)
146 for (i = 0; i < LT_SAVECOUNT ; i++) {
147 struct latency_record *mylat;
150 mylat = &tsk->latency_record[i];
151 for (q = 0 ; q < LT_BACKTRACEDEPTH ; q++) {
152 unsigned long record = lat.backtrace[q];
154 if (mylat->backtrace[q] != record) {
159 /* 0 and ULONG_MAX entries mean end of backtrace: */
160 if (record == 0 || record == ULONG_MAX)
165 mylat->time += lat.time;
166 if (lat.time > mylat->max)
167 mylat->max = lat.time;
172 /* Allocated a new one: */
173 i = tsk->latency_record_count;
174 memcpy(&tsk->latency_record[i], &lat, sizeof(struct latency_record));
177 spin_unlock_irqrestore(&latency_lock, flags);
180 static int lstats_show(struct seq_file *m, void *v)
184 seq_puts(m, "Latency Top version : v0.1\n");
186 for (i = 0; i < MAXLR; i++) {
187 if (latency_record[i].backtrace[0]) {
189 seq_printf(m, "%i %li %li ",
190 latency_record[i].count,
191 latency_record[i].time,
192 latency_record[i].max);
193 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
194 char sym[KSYM_NAME_LEN];
196 if (!latency_record[i].backtrace[q])
198 if (latency_record[i].backtrace[q] == ULONG_MAX)
200 sprint_symbol(sym, latency_record[i].backtrace[q]);
201 c = strchr(sym, '+');
204 seq_printf(m, "%s ", sym);
213 lstats_write(struct file *file, const char __user *buf, size_t count,
216 clear_global_latency_tracing();
221 static int lstats_open(struct inode *inode, struct file *filp)
223 return single_open(filp, lstats_show, NULL);
226 static struct file_operations lstats_fops = {
229 .write = lstats_write,
231 .release = single_release,
234 static int __init init_lstats_procfs(void)
236 struct proc_dir_entry *pe;
238 pe = create_proc_entry("latency_stats", 0644, NULL);
242 pe->proc_fops = &lstats_fops;
246 __initcall(init_lstats_procfs);