Skip to content

Commit 0cd7c74

Browse files
author
Peter Zijlstra
committed
delayacct: Add sysctl to enable at runtime
Just like sched_schedstats, allow runtime enabling (and disabling) of delayacct. This is useful if one forgot to add the delayacct boot time option. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lkml.kernel.org/r/YJkhebGJAywaZowX@hirez.programming.kicks-ass.net
1 parent e4042ad commit 0cd7c74

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

Documentation/accounting/delay-accounting.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ To enable, add::
7474

7575
delayacct
7676

77-
to the kernel boot options. The rest of the instructions
78-
below assume this has been done.
77+
to the kernel boot options. The rest of the instructions below assume this has
78+
been done. Alternatively, use sysctl kernel.task_delayacct to switch the state
79+
at runtime. Note however that only tasks started after enabling it will have
80+
delayacct information.
7981

8082
After the system has booted up, use a utility
8183
similar to getdelays.c to access the delays

include/linux/delayacct.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ DECLARE_STATIC_KEY_FALSE(delayacct_key);
6565
extern int delayacct_on; /* Delay accounting turned on/off */
6666
extern struct kmem_cache *delayacct_cache;
6767
extern void delayacct_init(void);
68+
69+
extern int sysctl_delayacct(struct ctl_table *table, int write, void *buffer,
70+
size_t *lenp, loff_t *ppos);
71+
6872
extern void __delayacct_tsk_init(struct task_struct *);
6973
extern void __delayacct_tsk_exit(struct task_struct *);
7074
extern void __delayacct_blkio_start(void);

kernel/delayacct.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ DEFINE_STATIC_KEY_FALSE(delayacct_key);
1818
int delayacct_on __read_mostly; /* Delay accounting turned on/off */
1919
struct kmem_cache *delayacct_cache;
2020

21+
static void set_delayacct(bool enabled)
22+
{
23+
if (enabled) {
24+
static_branch_enable(&delayacct_key);
25+
delayacct_on = 1;
26+
} else {
27+
delayacct_on = 0;
28+
static_branch_disable(&delayacct_key);
29+
}
30+
}
31+
2132
static int __init delayacct_setup_enable(char *str)
2233
{
2334
delayacct_on = 1;
@@ -29,9 +40,30 @@ void delayacct_init(void)
2940
{
3041
delayacct_cache = KMEM_CACHE(task_delay_info, SLAB_PANIC|SLAB_ACCOUNT);
3142
delayacct_tsk_init(&init_task);
32-
if (delayacct_on)
33-
static_branch_enable(&delayacct_key);
43+
set_delayacct(delayacct_on);
44+
}
45+
46+
#ifdef CONFIG_PROC_SYSCTL
47+
int sysctl_delayacct(struct ctl_table *table, int write, void *buffer,
48+
size_t *lenp, loff_t *ppos)
49+
{
50+
int state = delayacct_on;
51+
struct ctl_table t;
52+
int err;
53+
54+
if (write && !capable(CAP_SYS_ADMIN))
55+
return -EPERM;
56+
57+
t = *table;
58+
t.data = &state;
59+
err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
60+
if (err < 0)
61+
return err;
62+
if (write)
63+
set_delayacct(state);
64+
return err;
3465
}
66+
#endif
3567

3668
void __delayacct_tsk_init(struct task_struct *tsk)
3769
{

kernel/sysctl.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
#include <linux/coredump.h>
7272
#include <linux/latencytop.h>
7373
#include <linux/pid.h>
74+
#include <linux/delayacct.h>
7475

7576
#include "../lib/kstrtox.h"
7677

@@ -1727,6 +1728,17 @@ static struct ctl_table kern_table[] = {
17271728
.extra2 = SYSCTL_ONE,
17281729
},
17291730
#endif /* CONFIG_SCHEDSTATS */
1731+
#ifdef CONFIG_TASK_DELAY_ACCT
1732+
{
1733+
.procname = "task_delayacct",
1734+
.data = NULL,
1735+
.maxlen = sizeof(unsigned int),
1736+
.mode = 0644,
1737+
.proc_handler = sysctl_delayacct,
1738+
.extra1 = SYSCTL_ZERO,
1739+
.extra2 = SYSCTL_ONE,
1740+
},
1741+
#endif /* CONFIG_TASK_DELAY_ACCT */
17301742
#ifdef CONFIG_NUMA_BALANCING
17311743
{
17321744
.procname = "numa_balancing",

0 commit comments

Comments
 (0)