Skip to content

Commit

Permalink
runqlen/cpuunclaimed: Fix runnable_weight issue after Linux 5.7
Browse files Browse the repository at this point in the history
runqlen / cpuunclaimed add check_runnable_weight_field() as workaround to
check runnable_weight presents in struct cfs_rq in kernel/sched/sched.h by
trying to access runnable_weight field of struct
sched_entity in include/linux/sched.h. Please check more details in PR iovisor#1510
and iovisor#2164.

Unfortunately, the runnable_weight field of struct cfs_rq is removed, but the
runnable_weight field of struct sched_entity is remained by following patchset
series from Linux version 5.7.0.

- https://yhbt.net/lore/all/[email protected]/
- https://yhbt.net/lore/all/[email protected]/

Please also check the source of Linux below.

- include/linux/sched.h
    - https://elixir.bootlin.com/linux/v5.7/source/include/linux/sched.h#L475
- kernel/sched/sched.h
    - https://elixir.bootlin.com/linux/v5.7/source/kernel/sched/sched.h#L502

This PR checks runnable_weight field exists by using kernel_struct_has_field()
if target system with BTF enabled, otherwise fallback to legacy on-the-fly
compiling check. In the meantime, add Linux version 5.7.0 check in
structure definition cfs_rq_partial to prevent issue if target system
w/o BTF enabled and Linux version > 5.7.

Please check more details in iovisor#4602.

Signed-off-by: Ism Hong <[email protected]>
  • Loading branch information
Ism Hong authored and yonghong-song committed May 19, 2023
1 parent 3e2a251 commit 46a125d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 7 additions & 1 deletion tools/cpuunclaimed.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,12 @@ def check_runnable_weight_field():
// Declare enough of cfs_rq to find nr_running, since we can't #import the
// header. This will need maintenance. It is from kernel/sched/sched.h:
// The runnable_weight field is removed from Linux 5.7.0
struct cfs_rq_partial {
struct load_weight load;
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 7, 0)
RUNNABLE_WEIGHT_FIELD
#endif
unsigned int nr_running, h_nr_running;
};
Expand Down Expand Up @@ -216,7 +219,10 @@ def check_runnable_weight_field():
}
"""

if check_runnable_weight_field():
# If target has BTF enabled, use BTF to check runnable_weight field exists in
# cfs_rq first, otherwise fallback to use check_runnable_weight_field().
if BPF.kernel_struct_has_field(b'cfs_rq', b'runnable_weight') == 1 \
or check_runnable_weight_field():
bpf_text = bpf_text.replace('RUNNABLE_WEIGHT_FIELD', 'unsigned long runnable_weight;')
else:
bpf_text = bpf_text.replace('RUNNABLE_WEIGHT_FIELD', '')
Expand Down
10 changes: 8 additions & 2 deletions tools/runqlen.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def check_runnable_weight_field():

# Get a temporary file name
tmp_file = NamedTemporaryFile(delete=False)
tmp_file.close();
tmp_file.close()

# Duplicate and close stderr (fd = 2)
old_stderr = dup(2)
Expand Down Expand Up @@ -122,9 +122,12 @@ def check_runnable_weight_field():
// Declare enough of cfs_rq to find nr_running, since we can't #import the
// header. This will need maintenance. It is from kernel/sched/sched.h:
// The runnable_weight field is removed from Linux 5.7.0
struct cfs_rq_partial {
struct load_weight load;
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 7, 0)
RUNNABLE_WEIGHT_FIELD
#endif
unsigned int nr_running, h_nr_running;
};
Expand Down Expand Up @@ -172,7 +175,10 @@ def check_runnable_weight_field():
'BPF_HISTOGRAM(dist, unsigned int);')
bpf_text = bpf_text.replace('STORE', 'dist.atomic_increment(len);')

if check_runnable_weight_field():
# If target has BTF enabled, use BTF to check runnable_weight field exists in
# cfs_rq first, otherwise fallback to use check_runnable_weight_field().
if BPF.kernel_struct_has_field(b'cfs_rq', b'runnable_weight') == 1 \
or check_runnable_weight_field():
bpf_text = bpf_text.replace('RUNNABLE_WEIGHT_FIELD', 'unsigned long runnable_weight;')
else:
bpf_text = bpf_text.replace('RUNNABLE_WEIGHT_FIELD', '')
Expand Down

0 comments on commit 46a125d

Please sign in to comment.