Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libbpf-tools/klockstat: Search for correct stack offset when printing #5203

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tohojo
Copy link

@tohojo tohojo commented Jan 30, 2025

The klockstat BPF code contains a hard-coded offset (of 4) for the
captured stack entries, which is supposed to get rid of the topmost
entries that are just locking functions. However, when running the
klockstat tool on RHEL, this offset is not correct, leading to output
like the following:

                           Caller  Avg Wait    Count   Max Wait   Total Wait
                   mutex_lock+0x5    1.1 us        3     1.8 us       3.2 us
                   mutex_lock+0x5    825 ns        5     1.6 us       4.1 us
                   mutex_lock+0x5    1.1 us        4     1.5 us       4.3 us

                           Caller  Avg Hold    Count   Max Hold   Total Hold
                   mutex_lock+0x5   18.0 ms        3    23.0 ms      54.0 ms
                   mutex_lock+0x5    1.7 ms       15     2.2 ms      25.1 ms
                   mutex_lock+0x5    1.7 ms       15     2.2 ms      25.1 ms

To fix this, add logic in the userspace component of the utility to
walk the stack frames, skipping any entries that correspond to any of
the kprobe hooks that the tool attaches to. This fixes the output
on RHEL, without affecting other systems where the offset works
correctly.

Signed-off-by: Toke Høiland-Jørgensen [email protected]

libbpf-tools/klockstat.c Outdated Show resolved Hide resolved

for (j = 0; j < ARRAY_SIZE(internal_ksym_names); j++)
if (!strcmp(ksym->name, internal_ksym_names[j]))
ret = i + 1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we break out if 'ret = i + 1' is not triggered?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, no, I don't think so; the loop is trying to find the top-most instance of one of the names in the list, so to do that we'll keep matching while walking the whole stack. I.e., I'm not assuming that it's definitely an off-by-one thing here...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tohojo Could you show an stack example before and after the change for the above case? It will give a visible example so we can decide what is the proper way for the stack trace.

The klockstat BPF code contains a hard-coded offset (of 4) for the
captured stack entries, which is supposed to get rid of the topmost
entries that are just locking functions. However, when running the
klockstat tool on RHEL, this offset is not correct, leading to output
like the following:

                               Caller  Avg Wait    Count   Max Wait   Total Wait
                       mutex_lock+0x5    1.1 us        3     1.8 us       3.2 us
                       mutex_lock+0x5    825 ns        5     1.6 us       4.1 us
                       mutex_lock+0x5    1.1 us        4     1.5 us       4.3 us

                               Caller  Avg Hold    Count   Max Hold   Total Hold
                       mutex_lock+0x5   18.0 ms        3    23.0 ms      54.0 ms
                       mutex_lock+0x5    1.7 ms       15     2.2 ms      25.1 ms
                       mutex_lock+0x5    1.7 ms       15     2.2 ms      25.1 ms

To fix this, add logic in the userspace component of the utility to
walk the stack frames, skipping any entries that correspond to any of
the kprobe hooks that the tool attaches to. This fixes the output
on RHEL, without affecting other systems where the offset works
correctly.

Signed-off-by: Toke Høiland-Jørgensen <[email protected]>
@tohojo tohojo force-pushed the klockstat-stack-fix branch from 447cd67 to de3cde1 Compare February 3, 2025 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants