Repository navigation
Expand file tree
/
Copy pathcfs_bandwidth01.c
More file actions
191 lines (154 loc) · 4.65 KB
/
Copy pathcfs_bandwidth01.c
File metadata and controls
191 lines (154 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2021 SUSE LLC <rpalethorpe@suse.com>
* Copyright (c) Linux Test Project, 2021-2023
*/
/*\
* Creates a multi-level CGroup hierarchy with the cpu controller
* enabled. The leaf groups are populated with "busy" processes which
* simulate intermittent cpu load. They spin for some time then sleep
* then repeat.
*
* Both the trunk and leaf groups are set cpu bandwidth limits. The
* busy processes will intermittently exceed these limits. Causing
* them to be throttled. When they begin sleeping this will then cause
* them to be unthrottle.
*
* The test is known to reproduce an issue with an update to
* SLE-15-SP1 (kernel 4.12.14-197.64,
* https://bugzilla.suse.com/show_bug.cgi?id=1179093).
*
* Also as an reproducer for another bug:
*
* commit fdaba61ef8a268d4136d0a113d153f7a89eb9984
* Author: Rik van Riel <riel@surriel.com>
* Date: Mon Jun 21 19:43:30 2021 +0200
*
* sched/fair: Ensure that the CFS parent is added after unthrottling
*/
#include <stdlib.h>
#include "tst_test.h"
#include "tst_timer.h"
static struct tst_cg_group *cg_level2, *cg_level3a, *cg_level3b;
static struct tst_cg_group *cg_workers[3];
static int may_have_waiters;
static void set_cpu_quota(const struct tst_cg_group *const cg,
const float quota_percent)
{
const unsigned int period_us = 10000;
const unsigned int quota_us = (quota_percent / 100) * (float)period_us;
if (!TST_CG_VER_IS_V1(cg, "cpu")) {
SAFE_CG_PRINTF(cg, "cpu.max",
"%u %u", quota_us, period_us);
} else {
SAFE_CG_PRINTF(cg, "cpu.cfs_period_us",
"%u", period_us);
SAFE_CG_PRINTF(cg, "cpu.max",
"%u", quota_us);
}
tst_res(TINFO, "Set '%s/cpu.max' = '%d %d'",
tst_cg_group_name(cg), quota_us, period_us);
}
static void mk_cpu_cgroup(struct tst_cg_group **cg,
const struct tst_cg_group *const cg_parent,
const char *const cg_child_name,
const float quota_percent)
{
*cg = tst_cg_group_mk(cg_parent, "%s", cg_child_name);
set_cpu_quota(*cg, quota_percent);
}
static void busy_loop(const unsigned int sleep_ms)
{
for (;;) {
tst_timer_start(CLOCK_MONOTONIC_RAW);
while (!tst_timer_expired_ms(20))
;
const int ret = tst_checkpoint_wait(0, sleep_ms);
if (!ret)
exit(0);
if (errno != ETIMEDOUT)
tst_brk(TBROK | TERRNO, "tst_checkpoint_wait");
}
}
static void fork_busy_procs_in_cgroup(const struct tst_cg_group *const cg)
{
const unsigned int sleeps_ms[] = {3000, 1000, 10};
const pid_t worker_pid = SAFE_FORK();
size_t i;
if (worker_pid)
return;
for (i = 0; i < ARRAY_SIZE(sleeps_ms); i++) {
const pid_t busy_pid = SAFE_FORK();
if (!busy_pid)
busy_loop(sleeps_ms[i]);
SAFE_CG_PRINTF(cg, "cgroup.procs", "%d", busy_pid);
}
tst_reap_children();
exit(0);
}
static void do_test(void)
{
size_t i;
may_have_waiters = 1;
for (i = 0; i < ARRAY_SIZE(cg_workers); i++)
fork_busy_procs_in_cgroup(cg_workers[i]);
tst_res(TPASS, "Scheduled bandwidth constrained workers");
sleep(1);
set_cpu_quota(cg_level2, 50);
sleep(2);
TST_CHECKPOINT_WAKE2(0, 3 * 3);
tst_reap_children();
may_have_waiters = 0;
tst_res(TPASS, "Workers exited");
}
static void setup(void)
{
cg_level2 = tst_cg_group_mk(tst_cg, "level2");
cg_level3a = tst_cg_group_mk(cg_level2, "level3a");
mk_cpu_cgroup(&cg_workers[0], cg_level3a, "worker1", 30);
mk_cpu_cgroup(&cg_workers[1], cg_level3a, "worker2", 20);
cg_level3b = tst_cg_group_mk(cg_level2, "level3b");
mk_cpu_cgroup(&cg_workers[2], cg_level3b, "worker3", 30);
}
static void cleanup(void)
{
size_t i;
if (may_have_waiters) {
TST_CHECKPOINT_WAKE2(0, 3 * 3);
tst_reap_children();
may_have_waiters = 0;
}
for (i = 0; i < ARRAY_SIZE(cg_workers); i++) {
if (cg_workers[i])
cg_workers[i] = tst_cg_group_rm(cg_workers[i]);
}
if (cg_level3a)
cg_level3a = tst_cg_group_rm(cg_level3a);
if (cg_level3b)
cg_level3b = tst_cg_group_rm(cg_level3b);
if (cg_level2)
cg_level2 = tst_cg_group_rm(cg_level2);
}
static struct tst_test test = {
.test_all = do_test,
.setup = setup,
.cleanup = cleanup,
.forks_child = 1,
.needs_checkpoints = 1,
.timeout = 20,
.taint_check = TST_TAINT_W | TST_TAINT_D,
.needs_kconfigs = (const char *[]) {
"CONFIG_CFS_BANDWIDTH",
NULL
},
.needs_cgroup_ctrls = (const char *const []){"cpu", NULL},
.tags = (const struct tst_tag[]) {
{"linux-git", "39f23ce07b9355d05a64ae303ce20d1c4b92b957"},
{"linux-git", "b34cb07dde7c2346dec73d053ce926aeaa087303"},
{"linux-git", "fe61468b2cbc2b7ce5f8d3bf32ae5001d4c434e9"},
{"linux-git", "5ab297bab984310267734dfbcc8104566658ebef"},
{"linux-git", "6d4d22468dae3d8757af9f8b81b848a76ef4409d"},
{"linux-git", "fdaba61ef8a268d4136d0a113d153f7a89eb9984"},
{ }
}
};