Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/tools/sched_ext/scx_flatcg.c
26285 views
1
/* SPDX-License-Identifier: GPL-2.0 */
2
/*
3
* Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
4
* Copyright (c) 2023 Tejun Heo <[email protected]>
5
* Copyright (c) 2023 David Vernet <[email protected]>
6
*/
7
#include <stdio.h>
8
#include <signal.h>
9
#include <unistd.h>
10
#include <libgen.h>
11
#include <limits.h>
12
#include <inttypes.h>
13
#include <fcntl.h>
14
#include <time.h>
15
#include <bpf/bpf.h>
16
#include <scx/common.h>
17
#include "scx_flatcg.h"
18
#include "scx_flatcg.bpf.skel.h"
19
20
#ifndef FILEID_KERNFS
21
#define FILEID_KERNFS 0xfe
22
#endif
23
24
const char help_fmt[] =
25
"A flattened cgroup hierarchy sched_ext scheduler.\n"
26
"\n"
27
"See the top-level comment in .bpf.c for more details.\n"
28
"\n"
29
"Usage: %s [-s SLICE_US] [-i INTERVAL] [-f] [-v]\n"
30
"\n"
31
" -s SLICE_US Override slice duration\n"
32
" -i INTERVAL Report interval\n"
33
" -f Use FIFO scheduling instead of weighted vtime scheduling\n"
34
" -v Print libbpf debug messages\n"
35
" -h Display this help and exit\n";
36
37
static bool verbose;
38
static volatile int exit_req;
39
40
static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
41
{
42
if (level == LIBBPF_DEBUG && !verbose)
43
return 0;
44
return vfprintf(stderr, format, args);
45
}
46
47
static void sigint_handler(int dummy)
48
{
49
exit_req = 1;
50
}
51
52
static float read_cpu_util(__u64 *last_sum, __u64 *last_idle)
53
{
54
FILE *fp;
55
char buf[4096];
56
char *line, *cur = NULL, *tok;
57
__u64 sum = 0, idle = 0;
58
__u64 delta_sum, delta_idle;
59
int idx;
60
61
fp = fopen("/proc/stat", "r");
62
if (!fp) {
63
perror("fopen(\"/proc/stat\")");
64
return 0.0;
65
}
66
67
if (!fgets(buf, sizeof(buf), fp)) {
68
perror("fgets(\"/proc/stat\")");
69
fclose(fp);
70
return 0.0;
71
}
72
fclose(fp);
73
74
line = buf;
75
for (idx = 0; (tok = strtok_r(line, " \n", &cur)); idx++) {
76
char *endp = NULL;
77
__u64 v;
78
79
if (idx == 0) {
80
line = NULL;
81
continue;
82
}
83
v = strtoull(tok, &endp, 0);
84
if (!endp || *endp != '\0') {
85
fprintf(stderr, "failed to parse %dth field of /proc/stat (\"%s\")\n",
86
idx, tok);
87
continue;
88
}
89
sum += v;
90
if (idx == 4)
91
idle = v;
92
}
93
94
delta_sum = sum - *last_sum;
95
delta_idle = idle - *last_idle;
96
*last_sum = sum;
97
*last_idle = idle;
98
99
return delta_sum ? (float)(delta_sum - delta_idle) / delta_sum : 0.0;
100
}
101
102
static void fcg_read_stats(struct scx_flatcg *skel, __u64 *stats)
103
{
104
__u64 cnts[FCG_NR_STATS][skel->rodata->nr_cpus];
105
__u32 idx;
106
107
memset(stats, 0, sizeof(stats[0]) * FCG_NR_STATS);
108
109
for (idx = 0; idx < FCG_NR_STATS; idx++) {
110
int ret, cpu;
111
112
ret = bpf_map_lookup_elem(bpf_map__fd(skel->maps.stats),
113
&idx, cnts[idx]);
114
if (ret < 0)
115
continue;
116
for (cpu = 0; cpu < skel->rodata->nr_cpus; cpu++)
117
stats[idx] += cnts[idx][cpu];
118
}
119
}
120
121
int main(int argc, char **argv)
122
{
123
struct scx_flatcg *skel;
124
struct bpf_link *link;
125
struct timespec intv_ts = { .tv_sec = 2, .tv_nsec = 0 };
126
bool dump_cgrps = false;
127
__u64 last_cpu_sum = 0, last_cpu_idle = 0;
128
__u64 last_stats[FCG_NR_STATS] = {};
129
unsigned long seq = 0;
130
__s32 opt;
131
__u64 ecode;
132
133
libbpf_set_print(libbpf_print_fn);
134
signal(SIGINT, sigint_handler);
135
signal(SIGTERM, sigint_handler);
136
restart:
137
skel = SCX_OPS_OPEN(flatcg_ops, scx_flatcg);
138
139
skel->rodata->nr_cpus = libbpf_num_possible_cpus();
140
skel->rodata->cgrp_slice_ns = __COMPAT_ENUM_OR_ZERO("scx_public_consts", "SCX_SLICE_DFL");
141
142
while ((opt = getopt(argc, argv, "s:i:dfvh")) != -1) {
143
double v;
144
145
switch (opt) {
146
case 's':
147
v = strtod(optarg, NULL);
148
skel->rodata->cgrp_slice_ns = v * 1000;
149
break;
150
case 'i':
151
v = strtod(optarg, NULL);
152
intv_ts.tv_sec = v;
153
intv_ts.tv_nsec = (v - (float)intv_ts.tv_sec) * 1000000000;
154
break;
155
case 'd':
156
dump_cgrps = true;
157
break;
158
case 'f':
159
skel->rodata->fifo_sched = true;
160
break;
161
case 'v':
162
verbose = true;
163
break;
164
case 'h':
165
default:
166
fprintf(stderr, help_fmt, basename(argv[0]));
167
return opt != 'h';
168
}
169
}
170
171
printf("slice=%.1lfms intv=%.1lfs dump_cgrps=%d",
172
(double)skel->rodata->cgrp_slice_ns / 1000000.0,
173
(double)intv_ts.tv_sec + (double)intv_ts.tv_nsec / 1000000000.0,
174
dump_cgrps);
175
176
SCX_OPS_LOAD(skel, flatcg_ops, scx_flatcg, uei);
177
link = SCX_OPS_ATTACH(skel, flatcg_ops, scx_flatcg);
178
179
while (!exit_req && !UEI_EXITED(skel, uei)) {
180
__u64 acc_stats[FCG_NR_STATS];
181
__u64 stats[FCG_NR_STATS];
182
float cpu_util;
183
int i;
184
185
cpu_util = read_cpu_util(&last_cpu_sum, &last_cpu_idle);
186
187
fcg_read_stats(skel, acc_stats);
188
for (i = 0; i < FCG_NR_STATS; i++)
189
stats[i] = acc_stats[i] - last_stats[i];
190
191
memcpy(last_stats, acc_stats, sizeof(acc_stats));
192
193
printf("\n[SEQ %6lu cpu=%5.1lf hweight_gen=%" PRIu64 "]\n",
194
seq++, cpu_util * 100.0, skel->data->hweight_gen);
195
printf(" act:%6llu deact:%6llu global:%6llu local:%6llu\n",
196
stats[FCG_STAT_ACT],
197
stats[FCG_STAT_DEACT],
198
stats[FCG_STAT_GLOBAL],
199
stats[FCG_STAT_LOCAL]);
200
printf("HWT cache:%6llu update:%6llu skip:%6llu race:%6llu\n",
201
stats[FCG_STAT_HWT_CACHE],
202
stats[FCG_STAT_HWT_UPDATES],
203
stats[FCG_STAT_HWT_SKIP],
204
stats[FCG_STAT_HWT_RACE]);
205
printf("ENQ skip:%6llu race:%6llu\n",
206
stats[FCG_STAT_ENQ_SKIP],
207
stats[FCG_STAT_ENQ_RACE]);
208
printf("CNS keep:%6llu expire:%6llu empty:%6llu gone:%6llu\n",
209
stats[FCG_STAT_CNS_KEEP],
210
stats[FCG_STAT_CNS_EXPIRE],
211
stats[FCG_STAT_CNS_EMPTY],
212
stats[FCG_STAT_CNS_GONE]);
213
printf("PNC next:%6llu empty:%6llu nocgrp:%6llu gone:%6llu race:%6llu fail:%6llu\n",
214
stats[FCG_STAT_PNC_NEXT],
215
stats[FCG_STAT_PNC_EMPTY],
216
stats[FCG_STAT_PNC_NO_CGRP],
217
stats[FCG_STAT_PNC_GONE],
218
stats[FCG_STAT_PNC_RACE],
219
stats[FCG_STAT_PNC_FAIL]);
220
printf("BAD remove:%6llu\n",
221
acc_stats[FCG_STAT_BAD_REMOVAL]);
222
fflush(stdout);
223
224
nanosleep(&intv_ts, NULL);
225
}
226
227
bpf_link__destroy(link);
228
ecode = UEI_REPORT(skel, uei);
229
scx_flatcg__destroy(skel);
230
231
if (UEI_ECODE_RESTART(ecode))
232
goto restart;
233
return 0;
234
}
235
236