-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathschedstat.py
More file actions
executable file
·284 lines (244 loc) · 7.12 KB
/
schedstat.py
File metadata and controls
executable file
·284 lines (244 loc) · 7.12 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env python3
import sys
import time
# Domain fields by schedstat version
# read kernel/sched/stats.c:show_schedstat() to find the definitions
DOMAIN_FIELDS_V15 = [
# CPU_IDLE
"lb_count_idle",
"lb_balance_idle",
"lb_failed_idle",
"lb_imbalance_idle",
"lb_gained_idle",
"lb_hot_gained_idle",
"lb_nobusyq_idle",
"lb_nobusyg_idle",
# CPU_NOT_IDLE
"lb_count_not_idle",
"lb_balance_not_idle",
"lb_failed_not_idle",
"lb_imbalance_not_idle",
"lb_gained_not_idle",
"lb_hot_gained_not_idle",
"lb_nobusyq_not_idle",
"lb_nobusyg_not_idle",
# CPU_NEWLY_IDLE
"lb_count_newly_idle",
"lb_balance_newly_idle",
"lb_failed_newly_idle",
"lb_imbalance_newly_idle",
"lb_gained_newly_idle",
"lb_hot_gained_newly_idle",
"lb_nobusyq_newly_idle",
"lb_nobusyg_newly_idle",
"alb_count",
"alb_failed",
"alb_pushed",
"sbe_cnt",
"sbe_balanced",
"sbe_pushed",
"sbf_cnt",
"sbf_balanced",
"sbf_pushed",
"ttwu_wake_remote",
"ttwu_move_affine",
"ttwu_move_balance"
]
# this is the same as v15, but the order of the idle type enum
# is different
DOMAIN_FIELDS_V16 = [
# __CPU_NOT_IDLE
"lb_count_not_idle",
"lb_balance_not_idle",
"lb_failed_not_idle",
"lb_imbalance_not_idle",
"lb_gained_not_idle",
"lb_hot_gained_not_idle",
"lb_nobusyq_not_idle",
"lb_nobusyg_not_idle",
# CPU_IDLE
"lb_count_idle",
"lb_balance_idle",
"lb_failed_idle",
"lb_imbalance_idle",
"lb_gained_idle",
"lb_hot_gained_idle",
"lb_nobusyq_idle",
"lb_nobusyg_idle",
# CPU_NEWLY_IDLE
"lb_count_newly_idle",
"lb_balance_newly_idle",
"lb_failed_newly_idle",
"lb_imbalance_newly_idle",
"lb_gained_newly_idle",
"lb_hot_gained_newly_idle",
"lb_nobusyq_newly_idle",
"lb_nobusyg_newly_idle",
"alb_count",
"alb_failed",
"alb_pushed",
"sbe_cnt",
"sbe_balanced",
"sbe_pushed",
"sbf_cnt",
"sbf_balanced",
"sbf_pushed",
"ttwu_wake_remote",
"ttwu_move_affine",
"ttwu_move_balance"
]
DOMAIN_FIELDS_V17 = [
# __CPU_NOT_IDLE
"lb_count_not_idle",
"lb_balance_not_idle",
"lb_failed_not_idle",
"lb_imbalance_load_not_idle",
"lb_imbalance_util_not_idle",
"lb_imbalance_task_not_idle",
"lb_imbalance_misfit_not_idle",
"lb_gained_not_idle",
"lb_hot_gained_not_idle",
"lb_nobusyq_not_idle",
"lb_nobusyg_not_idle",
# CPU_IDLE
"lb_count_idle",
"lb_balance_idle",
"lb_failed_idle",
"lb_imbalance_load_idle",
"lb_imbalance_util_idle",
"lb_imbalance_task_idle",
"lb_imbalance_misfit_idle",
"lb_gained_idle",
"lb_hot_gained_idle",
"lb_nobusyq_idle",
"lb_nobusyg_idle",
# CPU_NEWLY_IDLE
"lb_count_newly_idle",
"lb_balance_newly_idle",
"lb_failed_newly_idle",
"lb_imbalance_load_newly_idle",
"lb_imbalance_util_newly_idle",
"lb_imbalance_task_newly_idle",
"lb_imbalance_misfit_newly_idle",
"lb_gained_newly_idle",
"lb_hot_gained_newly_idle",
"lb_nobusyq_newly_idle",
"lb_nobusyg_newly_idle",
"alb_count",
"alb_failed",
"alb_pushed",
"sbe_cnt",
"sbe_balanced",
"sbe_pushed",
"sbf_cnt",
"sbf_balanced",
"sbf_pushed",
"ttwu_wake_remote",
"ttwu_move_affine",
"ttwu_move_balance"
]
# Common CPU fields (adjust if your kernel differs)
# these are the same in 15 and 17
CPU_FIELDS = [
"yld_count", "sched_count", "sched_goidle", "ttwu_count",
"ttwu_local", "rq_cpu_time", "rq_run_delay", "rq_pcount"
]
def detect_schedstat_version():
with open("/proc/schedstat", "r") as f:
for line in f:
if line.startswith("version"):
return int(line.strip().split()[1])
return 15 # Default to v15 if not specified
def parse_domains(lines, version):
domains = []
for line in lines:
if line.startswith("domain"):
parts = line.split()
if version == 17:
values = list(map(int, parts[3:]))
fields = DOMAIN_FIELDS_V17
elif version == 16:
values = list(map(int, parts[2:]))
fields = DOMAIN_FIELDS_V16
elif version == 15:
values = list(map(int, parts[2:]))
fields = DOMAIN_FIELDS_V15
else:
sys.stderr.write("Unsupported schedstat version %d" % version)
sys.exit(1)
domain = dict(zip(fields, values))
domains.append(domain)
return domains
def parse_cpus(lines):
cpus = {}
for line in lines:
if line.startswith("cpu") and not line.startswith("cpufreq"):
parts = line.split()
cpu_id = parts[0]
values = list(map(int, parts[1:]))
cpus[cpu_id] = values
return cpus
def read_schedstat():
version = detect_schedstat_version()
with open("/proc/schedstat", "r") as f:
lines = [line.strip() for line in f if line.strip() and
not line.startswith("version")]
domains = parse_domains(lines, version)
cpus = parse_cpus(lines)
return version, domains, cpus
def sum_domains(domains):
if not domains:
return {}
summed = {}
for field in domains[0].keys():
summed[field] = sum(domain.get(field, 0) for domain in domains)
return summed
def print_delta(delta, label):
print(f"\nSystem-wide domain counter deltas ({label}):")
for field, value in delta.items():
print(f"{field}: {value}")
def cpu_delta(start_cpus, end_cpus):
if not start_cpus or not end_cpus:
return []
num_fields = min(len(list(start_cpus.values())[0]),
len(list(end_cpus.values())[0]))
combined = [0] * num_fields
for cpu in start_cpus:
if cpu in end_cpus:
start_vals = start_cpus[cpu]
end_vals = end_cpus[cpu]
for i in range(num_fields):
combined[i] += end_vals[i] - start_vals[i]
return combined
def print_cpu_deltas(deltas, field_names=None):
print("\nCombined CPU field deltas (all CPUs):")
if field_names is None:
for idx, val in enumerate(deltas):
print(f"field_{idx}: {val}")
else:
for name, val in zip(field_names, deltas):
print(f"{name}: {val}")
def main():
if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} <interval_seconds>")
sys.exit(1)
try:
interval = float(sys.argv[1])
except Exception:
print("Interval must be a number (seconds).")
sys.exit(1)
version, start_domains, start_cpus = read_schedstat()
start_sum = sum_domains(start_domains)
time.sleep(interval)
_, end_domains, end_cpus = read_schedstat()
end_sum = sum_domains(end_domains)
# Print domain deltas
delta = {field: end_sum.get(field, 0) - start_sum.get(field, 0)
for field in start_sum.keys()}
print_delta(delta, f"interval {interval}s")
# Print combined CPU field deltas
cpu_field_names = CPU_FIELDS
deltas = cpu_delta(start_cpus, end_cpus)
print_cpu_deltas(deltas, cpu_field_names)
if __name__ == "__main__":
main()