99 * as published by the Free Software Foundation; version 2
1010 * of the License.
1111 */
12+
13+ /*
14+ * CONFIG_LATENCYTOP enables a kernel latency tracking infrastructure that is
15+ * used by the "latencytop" userspace tool. The latency that is tracked is not
16+ * the 'traditional' interrupt latency (which is primarily caused by something
17+ * else consuming CPU), but instead, it is the latency an application encounters
18+ * because the kernel sleeps on its behalf for various reasons.
19+ *
20+ * This code tracks 2 levels of statistics:
21+ * 1) System level latency
22+ * 2) Per process latency
23+ *
24+ * The latency is stored in fixed sized data structures in an accumulated form;
25+ * if the "same" latency cause is hit twice, this will be tracked as one entry
26+ * in the data structure. Both the count, total accumulated latency and maximum
27+ * latency are tracked in this data structure. When the fixed size structure is
28+ * full, no new causes are tracked until the buffer is flushed by writing to
29+ * the /proc file; the userspace tool does this on a regular basis.
30+ *
31+ * A latency cause is identified by a stringified backtrace at the point that
32+ * the scheduler gets invoked. The userland tool will use this string to
33+ * identify the cause of the latency in human readable form.
34+ *
35+ * The information is exported via /proc/latency_stats and /proc/<pid>/latency.
36+ * These files look like this:
37+ *
38+ * Latency Top version : v0.1
39+ * 70 59433 4897 i915_irq_wait drm_ioctl vfs_ioctl do_vfs_ioctl sys_ioctl
40+ * | | | |
41+ * | | | +----> the stringified backtrace
42+ * | | +---------> The maximum latency for this entry in microseconds
43+ * | +--------------> The accumulated latency for this entry (microseconds)
44+ * +-------------------> The number of times this entry is hit
45+ *
46+ * (note: the average latency is the accumulated latency divided by the number
47+ * of times)
48+ */
49+
1250#include <linux/latencytop.h>
1351#include <linux/kallsyms.h>
1452#include <linux/seq_file.h>
@@ -72,7 +110,7 @@ account_global_scheduler_latency(struct task_struct *tsk, struct latency_record
72110 firstnonnull = i ;
73111 continue ;
74112 }
75- for (q = 0 ; q < LT_BACKTRACEDEPTH ; q ++ ) {
113+ for (q = 0 ; q < LT_BACKTRACEDEPTH ; q ++ ) {
76114 unsigned long record = lat -> backtrace [q ];
77115
78116 if (latency_record [i ].backtrace [q ] != record ) {
@@ -101,31 +139,52 @@ account_global_scheduler_latency(struct task_struct *tsk, struct latency_record
101139 memcpy (& latency_record [i ], lat , sizeof (struct latency_record ));
102140}
103141
104- static inline void store_stacktrace (struct task_struct * tsk , struct latency_record * lat )
142+ /*
143+ * Iterator to store a backtrace into a latency record entry
144+ */
145+ static inline void store_stacktrace (struct task_struct * tsk ,
146+ struct latency_record * lat )
105147{
106148 struct stack_trace trace ;
107149
108150 memset (& trace , 0 , sizeof (trace ));
109151 trace .max_entries = LT_BACKTRACEDEPTH ;
110152 trace .entries = & lat -> backtrace [0 ];
111- trace .skip = 0 ;
112153 save_stack_trace_tsk (tsk , & trace );
113154}
114155
156+ /**
157+ * __account_scheduler_latency - record an occured latency
158+ * @tsk - the task struct of the task hitting the latency
159+ * @usecs - the duration of the latency in microseconds
160+ * @inter - 1 if the sleep was interruptible, 0 if uninterruptible
161+ *
162+ * This function is the main entry point for recording latency entries
163+ * as called by the scheduler.
164+ *
165+ * This function has a few special cases to deal with normal 'non-latency'
166+ * sleeps: specifically, interruptible sleep longer than 5 msec is skipped
167+ * since this usually is caused by waiting for events via select() and co.
168+ *
169+ * Negative latencies (caused by time going backwards) are also explicitly
170+ * skipped.
171+ */
115172void __sched
116- account_scheduler_latency (struct task_struct * tsk , int usecs , int inter )
173+ __account_scheduler_latency (struct task_struct * tsk , int usecs , int inter )
117174{
118175 unsigned long flags ;
119176 int i , q ;
120177 struct latency_record lat ;
121178
122- if (!latencytop_enabled )
123- return ;
124-
125179 /* Long interruptible waits are generally user requested... */
126180 if (inter && usecs > 5000 )
127181 return ;
128182
183+ /* Negative sleeps are time going backwards */
184+ /* Zero-time sleeps are non-interesting */
185+ if (usecs <= 0 )
186+ return ;
187+
129188 memset (& lat , 0 , sizeof (lat ));
130189 lat .count = 1 ;
131190 lat .time = usecs ;
@@ -143,12 +202,12 @@ account_scheduler_latency(struct task_struct *tsk, int usecs, int inter)
143202 if (tsk -> latency_record_count >= LT_SAVECOUNT )
144203 goto out_unlock ;
145204
146- for (i = 0 ; i < LT_SAVECOUNT ; i ++ ) {
205+ for (i = 0 ; i < LT_SAVECOUNT ; i ++ ) {
147206 struct latency_record * mylat ;
148207 int same = 1 ;
149208
150209 mylat = & tsk -> latency_record [i ];
151- for (q = 0 ; q < LT_BACKTRACEDEPTH ; q ++ ) {
210+ for (q = 0 ; q < LT_BACKTRACEDEPTH ; q ++ ) {
152211 unsigned long record = lat .backtrace [q ];
153212
154213 if (mylat -> backtrace [q ] != record ) {
@@ -186,7 +245,7 @@ static int lstats_show(struct seq_file *m, void *v)
186245 for (i = 0 ; i < MAXLR ; i ++ ) {
187246 if (latency_record [i ].backtrace [0 ]) {
188247 int q ;
189- seq_printf (m , "%i %li %li " ,
248+ seq_printf (m , "%i %lu %lu " ,
190249 latency_record [i ].count ,
191250 latency_record [i ].time ,
192251 latency_record [i ].max );
@@ -223,7 +282,7 @@ static int lstats_open(struct inode *inode, struct file *filp)
223282 return single_open (filp , lstats_show , NULL );
224283}
225284
226- static struct file_operations lstats_fops = {
285+ static const struct file_operations lstats_fops = {
227286 .open = lstats_open ,
228287 .read = seq_read ,
229288 .write = lstats_write ,
@@ -236,4 +295,4 @@ static int __init init_lstats_procfs(void)
236295 proc_create ("latency_stats" , 0644 , NULL , & lstats_fops );
237296 return 0 ;
238297}
239- __initcall (init_lstats_procfs );
298+ device_initcall (init_lstats_procfs );
0 commit comments