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

Support clear() and delete() on a count()-based map without key #1639

Merged
merged 3 commits into from
Jan 13, 2021

Conversation

mmisono
Copy link
Collaborator

@mmisono mmisono commented Nov 19, 2020

439a6bc changes so that count()-based map with no arguments uses
BPF_MAP_TYPE_PERCPU_ARRAY for the performance. The problem is crear()
uses bpf_map_delete_elem(), but ARRAY-typed maps cannot be deleted.
To solve this, suggested by fbs in #1350 (comment),
use zero() internally in clear() if a map type is array.

Like clear(), delete() also does not work for count()-based map (please
see the previous commit message for the details.) To fix this, instead
calling bpf_map_delete_elem() when delete(), call bpf_mpa_updat_elem()
and update the map value with zero.

Closes #1350

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

@mmisono mmisono force-pushed the clear_count_maps branch 2 times, most recently from 0acedd8 to 051b6ee Compare November 19, 2020 21:20
@mmisono
Copy link
Collaborator Author

mmisono commented Nov 19, 2020

So the difference between before and after 439a6bc is a count()-based map is an array, so it'll be printed out even if we clear() or delete() it. For example:

sudo ./src/bpftrace -e 'BEGIN { @ = 1; clear(@); exit(); }'
Attaching 1 probe...

// no output

but

% sudo ./src/bpftrace -e 'BEGIN { @ = count(); clear(@); exit(); }' probe...


@: 0

But I guess most people want to know the final value of the count.

Copy link
Member

@danobi danobi left a comment

Choose a reason for hiding this comment

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

LGTM, small nit

src/imap.h Outdated
@@ -27,6 +27,11 @@ class IMap
return map_type_ == BPF_MAP_TYPE_PERCPU_HASH ||
map_type_ == BPF_MAP_TYPE_PERCPU_ARRAY;
}
bool is_not_clearable() const
Copy link
Member

Choose a reason for hiding this comment

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

Can we name this is_clearable()? Would be confusing to read !is_not_clearable() later on (the double negation).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That makes sense. will fix.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done.

@mmisono mmisono force-pushed the clear_count_maps branch 4 times, most recently from bbe53ce to 6efc09f Compare November 24, 2020 19:25
@mmisono mmisono requested a review from danobi December 7, 2020 01:23
@danobi
Copy link
Member

danobi commented Jan 5, 2021

Needs a rebase

439a6bc changes so that count()-based map with no arguments uses
BPF_MAP_TYPE_PERCPU_ARRAY for the performance. The problem is crear()
uses bpf_map_delete_elem(), but ARRAY-typed maps cannot be deleted.
To solve this, suggested by fbs in bpftrace#1350 (comment),
use zero() internally in clear() if a map type is array.

Suggested-by: bas smit <[email protected]>
Signed-off-by: Masanori Misono <[email protected]>
Like clear(), delete() also does not work for count()-based map (please
see the previous commit message for the details.) To fix this, instead
calling bpf_map_delete_elem() when delete(), call bpf_mpa_updat_elem()
and update the map value with zero.
@mmisono mmisono merged commit 6e9c830 into bpftrace:master Jan 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clear() on count()-based map fails with a key error
2 participants