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/profile: Batch lookup support for hash map #4990

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions libbpf-tools/profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "profile.h"
#include "profile.skel.h"
#include "trace_helpers.h"
#include "map_helpers.h"

#define OPT_PERF_MAX_STACK_DEPTH 1 /* --perf-max-stack-depth */
#define OPT_STACK_STORAGE_SIZE 2 /* --stack-storage-size */
Expand Down Expand Up @@ -270,26 +271,29 @@ static int cmp_counts(const void *a, const void *b)

static int read_counts_map(int fd, struct key_ext_t *items, __u32 *count)
{
struct key_t keys[*count];
__u64 vals[*count];
__u32 key_size = sizeof(keys[0]);
__u32 value_size = sizeof(__u64);
struct key_t empty = {};
struct key_t *lookup_key = ∅
int i = 0;
int err;
__u32 n = *count;

while (bpf_map_get_next_key(fd, lookup_key, &items[i].k) == 0) {
err = bpf_map_lookup_elem(fd, &items[i].k, &items[i].v);
if (err < 0) {
fprintf(stderr, "failed to lookup counts: %d\n", err);
return -err;
}

if (items[i].v == 0)
continue;
err = dump_hash(fd, keys, key_size, vals, value_size, &n, &empty);
if (err)
return err;

lookup_key = &items[i].k;
i++;
for (i = 0; i < n; i++) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In previous patches, maps were read directly into the "items" variable, so this copy would not work unless it was in a batch. Now we use a little more memory and CPU. However, this change is good for eliminating duplicate code.

items[i].k.pid = keys[i].pid;
items[i].k.user_stack_id = keys[i].user_stack_id;
items[i].k.kern_stack_id = keys[i].kern_stack_id;
strncpy(items[i].k.name, keys[i].name, TASK_COMM_LEN);
items[i].v = vals[i];
}

*count = i;
*count = n;

return 0;
}

Expand Down
Loading