Skip to content

Commit b1d29ba

Browse files
hnaztorvalds
authored andcommitted
delayacct: track delays from thrashing cache pages
Delay accounting already measures the time a task spends in direct reclaim and waiting for swapin, but in low memory situations tasks spend can spend a significant amount of their time waiting on thrashing page cache. This isn't tracked right now. To know the full impact of memory contention on an individual task, measure the delay when waiting for a recently evicted active cache page to read back into memory. Also update tools/accounting/getdelays.c: [hannes@computer accounting]$ sudo ./getdelays -d -p 1 print delayacct stats ON PID 1 CPU count real total virtual total delay total delay average 50318 745000000 847346785 400533713 0.008ms IO count delay total delay average 435 122601218 0ms SWAP count delay total delay average 0 0 0ms RECLAIM count delay total delay average 0 0 0ms THRASHING count delay total delay average 19 12621439 0ms Link: http://lkml.kernel.org/r/20180828172258.3185-4-hannes@cmpxchg.org Signed-off-by: Johannes Weiner <hannes@cmpxchg.org> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Tested-by: Daniel Drake <drake@endlessm.com> Tested-by: Suren Baghdasaryan <surenb@google.com> Cc: Christopher Lameter <cl@linux.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Johannes Weiner <jweiner@fb.com> Cc: Mike Galbraith <efault@gmx.de> Cc: Peter Enderborg <peter.enderborg@sony.com> Cc: Randy Dunlap <rdunlap@infradead.org> Cc: Shakeel Butt <shakeelb@google.com> Cc: Tejun Heo <tj@kernel.org> Cc: Vinayak Menon <vinmenon@codeaurora.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 1899ad1 commit b1d29ba

5 files changed

Lines changed: 61 additions & 2 deletions

File tree

‎include/linux/delayacct.h‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ struct task_delay_info {
5757

5858
u64 freepages_start;
5959
u64 freepages_delay; /* wait for memory reclaim */
60+
61+
u64 thrashing_start;
62+
u64 thrashing_delay; /* wait for thrashing page */
63+
6064
u32 freepages_count; /* total count of memory reclaim */
65+
u32 thrashing_count; /* total count of thrash waits */
6166
};
6267
#endif
6368

@@ -76,6 +81,8 @@ extern int __delayacct_add_tsk(struct taskstats *, struct task_struct *);
7681
extern __u64 __delayacct_blkio_ticks(struct task_struct *);
7782
extern void __delayacct_freepages_start(void);
7883
extern void __delayacct_freepages_end(void);
84+
extern void __delayacct_thrashing_start(void);
85+
extern void __delayacct_thrashing_end(void);
7986

8087
static inline int delayacct_is_task_waiting_on_io(struct task_struct *p)
8188
{
@@ -156,6 +163,18 @@ static inline void delayacct_freepages_end(void)
156163
__delayacct_freepages_end();
157164
}
158165

166+
static inline void delayacct_thrashing_start(void)
167+
{
168+
if (current->delays)
169+
__delayacct_thrashing_start();
170+
}
171+
172+
static inline void delayacct_thrashing_end(void)
173+
{
174+
if (current->delays)
175+
__delayacct_thrashing_end();
176+
}
177+
159178
#else
160179
static inline void delayacct_set_flag(int flag)
161180
{}
@@ -182,6 +201,10 @@ static inline void delayacct_freepages_start(void)
182201
{}
183202
static inline void delayacct_freepages_end(void)
184203
{}
204+
static inline void delayacct_thrashing_start(void)
205+
{}
206+
static inline void delayacct_thrashing_end(void)
207+
{}
185208

186209
#endif /* CONFIG_TASK_DELAY_ACCT */
187210

‎include/uapi/linux/taskstats.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*/
3535

3636

37-
#define TASKSTATS_VERSION 8
37+
#define TASKSTATS_VERSION 9
3838
#define TS_COMM_LEN 32 /* should be >= TASK_COMM_LEN
3939
* in linux/sched.h */
4040

@@ -164,6 +164,10 @@ struct taskstats {
164164
/* Delay waiting for memory reclaim */
165165
__u64 freepages_count;
166166
__u64 freepages_delay_total;
167+
168+
/* Delay waiting for thrashing page */
169+
__u64 thrashing_count;
170+
__u64 thrashing_delay_total;
167171
};
168172

169173

‎kernel/delayacct.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,12 @@ int __delayacct_add_tsk(struct taskstats *d, struct task_struct *tsk)
135135
d->swapin_delay_total = (tmp < d->swapin_delay_total) ? 0 : tmp;
136136
tmp = d->freepages_delay_total + tsk->delays->freepages_delay;
137137
d->freepages_delay_total = (tmp < d->freepages_delay_total) ? 0 : tmp;
138+
tmp = d->thrashing_delay_total + tsk->delays->thrashing_delay;
139+
d->thrashing_delay_total = (tmp < d->thrashing_delay_total) ? 0 : tmp;
138140
d->blkio_count += tsk->delays->blkio_count;
139141
d->swapin_count += tsk->delays->swapin_count;
140142
d->freepages_count += tsk->delays->freepages_count;
143+
d->thrashing_count += tsk->delays->thrashing_count;
141144
raw_spin_unlock_irqrestore(&tsk->delays->lock, flags);
142145

143146
return 0;
@@ -169,3 +172,15 @@ void __delayacct_freepages_end(void)
169172
&current->delays->freepages_count);
170173
}
171174

175+
void __delayacct_thrashing_start(void)
176+
{
177+
current->delays->thrashing_start = ktime_get_ns();
178+
}
179+
180+
void __delayacct_thrashing_end(void)
181+
{
182+
delayacct_end(&current->delays->lock,
183+
&current->delays->thrashing_start,
184+
&current->delays->thrashing_delay,
185+
&current->delays->thrashing_count);
186+
}

‎mm/filemap.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <linux/cleancache.h>
3737
#include <linux/shmem_fs.h>
3838
#include <linux/rmap.h>
39+
#include <linux/delayacct.h>
3940
#include "internal.h"
4041

4142
#define CREATE_TRACE_POINTS
@@ -1073,8 +1074,15 @@ static inline int wait_on_page_bit_common(wait_queue_head_t *q,
10731074
{
10741075
struct wait_page_queue wait_page;
10751076
wait_queue_entry_t *wait = &wait_page.wait;
1077+
bool thrashing = false;
10761078
int ret = 0;
10771079

1080+
if (bit_nr == PG_locked && !PageSwapBacked(page) &&
1081+
!PageUptodate(page) && PageWorkingset(page)) {
1082+
delayacct_thrashing_start();
1083+
thrashing = true;
1084+
}
1085+
10781086
init_wait(wait);
10791087
wait->flags = lock ? WQ_FLAG_EXCLUSIVE : 0;
10801088
wait->func = wake_page_function;
@@ -1113,6 +1121,9 @@ static inline int wait_on_page_bit_common(wait_queue_head_t *q,
11131121

11141122
finish_wait(q, wait);
11151123

1124+
if (thrashing)
1125+
delayacct_thrashing_end();
1126+
11161127
/*
11171128
* A signal could leave PageWaiters set. Clearing it here if
11181129
* !waitqueue_active would be possible (by open-coding finish_wait),

‎tools/accounting/getdelays.c‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ static void print_delayacct(struct taskstats *t)
203203
"SWAP %15s%15s%15s\n"
204204
" %15llu%15llu%15llums\n"
205205
"RECLAIM %12s%15s%15s\n"
206+
" %15llu%15llu%15llums\n"
207+
"THRASHING%12s%15s%15s\n"
206208
" %15llu%15llu%15llums\n",
207209
"count", "real total", "virtual total",
208210
"delay total", "delay average",
@@ -222,7 +224,11 @@ static void print_delayacct(struct taskstats *t)
222224
"count", "delay total", "delay average",
223225
(unsigned long long)t->freepages_count,
224226
(unsigned long long)t->freepages_delay_total,
225-
average_ms(t->freepages_delay_total, t->freepages_count));
227+
average_ms(t->freepages_delay_total, t->freepages_count),
228+
"count", "delay total", "delay average",
229+
(unsigned long long)t->thrashing_count,
230+
(unsigned long long)t->thrashing_delay_total,
231+
average_ms(t->thrashing_delay_total, t->thrashing_count));
226232
}
227233

228234
static void task_context_switch_counts(struct taskstats *t)

0 commit comments

Comments
 (0)