Skip to content

Commit ad0b0fd

Browse files
Arjan van de VenIngo Molnar
authored andcommitted
sched, latencytop: incorporate review feedback from Andrew Morton
Andrew had some suggestions for the latencytop file; this patch takes care of most of these: * Add documentation * Turn account_scheduler_latency into an inline function * Don't report negative values to userspace * Make the file operations struct const * Fix a few checkpatch.pl warnings Signed-off-by: Arjan van de Ven <arjan@linux.intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent f437e8b commit ad0b0fd

2 files changed

Lines changed: 80 additions & 13 deletions

File tree

‎include/linux/latencytop.h‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef _INCLUDE_GUARD_LATENCYTOP_H_
1010
#define _INCLUDE_GUARD_LATENCYTOP_H_
1111

12+
#include <linux/compiler.h>
1213
#ifdef CONFIG_LATENCYTOP
1314

1415
#define LT_SAVECOUNT 32
@@ -24,7 +25,14 @@ struct latency_record {
2425

2526
struct task_struct;
2627

27-
void account_scheduler_latency(struct task_struct *task, int usecs, int inter);
28+
extern int latencytop_enabled;
29+
void __account_scheduler_latency(struct task_struct *task, int usecs, int inter);
30+
static inline void
31+
account_scheduler_latency(struct task_struct *task, int usecs, int inter)
32+
{
33+
if (unlikely(latencytop_enabled))
34+
__account_scheduler_latency(task, usecs, inter);
35+
}
2836

2937
void clear_all_latency_tracing(struct task_struct *p);
3038

‎kernel/latencytop.c‎

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,44 @@
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+
*/
115172
void __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

Comments
 (0)