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

Fix: for loops with per-cpu aggregations #3664

Merged
merged 1 commit into from
Dec 30, 2024

Conversation

jordalgo
Copy link
Contributor

@jordalgo jordalgo commented Dec 29, 2024

For a map that had multiple keys or a single key
that was inBpfMemory and contained a per-cpu
aggregation as a value (e.g. count or sum)
then retrieving the value from the $kv tuple was broken e.g.

@map[1, 2] = count();
for ($kv : @map) {
  print($kv.1); // this would always be 0
}

The fix is to just use the callback arg ptr
instead of allocating a new value (as the key)
and trying to store something in it.

Previously, for values inBpfMemory (like tuples) the
key in the line b_.CreateStore(key, key_ptr); was
populated with a pointer instead of the actual value
so key_ptr was storing a pointer and then we were
taking a pointer to a pointer for the map lookup,
which was wrong.

Checklist
  • Language changes are updated in man/adoc/bpftrace.adoc
  • User-visible and non-trivial changes updated in CHANGELOG.md
  • The new behaviour is covered by tests

@jordalgo
Copy link
Contributor Author

jordalgo commented Dec 29, 2024

This might need to go into the release branch. Will check tomorrow.

Edit: Yes, going to cherry-pick this on to the new release branch after it gets merged.

@@ -72,3 +72,11 @@ EXPECT_REGEX ^abc\ndef\ndef$
NAME variable context multiple
PROG BEGIN { @map[0] = 0; $var1 = 123; $var2 = "abc"; for ($kv : @map) { print(($var1, $var2)); } exit(); }
EXPECT (123, abc)

NAME map two keys with a per cpu aggregation
Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, both of these tests don't pass on master 😞

@jordalgo jordalgo force-pushed the fix_for_loop_per_cpu_agg branch from f2d35a9 to 3a42669 Compare December 29, 2024 12:51
Copy link
Member

Choose a reason for hiding this comment

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

why does this fix work? looking at this codegen change, seems like the PR is a noop.

(although avoiding a stack allocation is still better, but that's different)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll add a codegen test too but basically for values inBpfMemory (like tuples) the key in the line b_.CreateStore(key, key_ptr); was populated with a pointer instead of the actual value so key_ptr was storing a pointer and then we were taking a pointer to a pointer for the map lookup (see line 4557)

Copy link
Member

Choose a reason for hiding this comment

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

Ah ok. This detail would be good to include in PR and commit message

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@danobi Updated.

@jordalgo jordalgo force-pushed the fix_for_loop_per_cpu_agg branch from 3a42669 to 47e9a7b Compare December 29, 2024 21:41
For a map that had multiple keys or a single key
that was `inBpfMemory` and contained a per-cpu
aggregation as a value (e.g. `count` or `sum`)
then retrieving the value from the `$kv` tuple was
broken e.g.
```
@Map[1, 2] = count();
for ($kv : @Map) {
  print($kv.1); // this would always be 0
}
```

The fix is to just use the callback arg ptr
instead of allocating a new value (as the key)
and trying to store something in it.

Previously, for values inBpfMemory (like tuples) the
key in the line `b_.CreateStore(key, key_ptr);` was
populated with a pointer instead of the actual value
so key_ptr was storing a pointer and then we were
taking a pointer to a pointer for the map lookup,
which was wrong.
@jordalgo jordalgo force-pushed the fix_for_loop_per_cpu_agg branch from 47e9a7b to fc91148 Compare December 29, 2024 21:44
@danobi danobi merged commit 3518622 into bpftrace:master Dec 30, 2024
18 checks passed
@danobi
Copy link
Member

danobi commented Dec 30, 2024

Does this need a fixes entry in changelog? Or was buggy behavior not released yet?

@jordalgo
Copy link
Contributor Author

@danobi I'm going to make a PR for the release branch with the changelog entry and then another PR for master with the changelog entry under the new release branch section. 🏋️

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