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

Implicit casts from per-CPU integers into regular integers #3126

Open
ajor opened this issue Apr 17, 2024 · 1 comment
Open

Implicit casts from per-CPU integers into regular integers #3126

ajor opened this issue Apr 17, 2024 · 1 comment
Assignees
Labels
difficulty: medium enhancement New feature or request, changes on existing features language Changes to the bpftrace language power-user priority: high

Comments

@ajor
Copy link
Member

ajor commented Apr 17, 2024

Example usage:

tracepoint:syscalls:sys_enter_read { @c = count(); }
interval:s:1 { if (@c > 100) { exit() } }

Current behaviour:

ERROR: Type mismatch for '>': comparing 'count' with 'int64'
interval:s:1 { if (@c > 100) { exit() } }
                  ~~~~~~~~~

Per-CPU maps help to improve the performance of writes, but it is difficult to read their values. Currently they can only be aggregated and read in userspace, so people often fall back to using regular, non-per-cpu maps instead.

It'd be useful if we provided a mechanism to read the aggregated values of per-cpu maps from within BPF.

The bpf_map_lookup_percpu_elem helper can be used to look up a Per-CPU map's value from a different CPU.

A cast from a per-CPU integer to a regular integer could be done with this pseudo-code:

@percpu = count();
$int = 0;
$int += bpf_map_lookup_percpu_elem(@percpu, 0, 0); // CPU 0
$int += bpf_map_lookup_percpu_elem(@percpu, 0, 1); // CPU 1
$int += bpf_map_lookup_percpu_elem(@percpu, 0, 2); // CPU 2
$int += bpf_map_lookup_percpu_elem(@percpu, 0, 3); // CPU 3
...
@ajor ajor added enhancement New feature or request, changes on existing features priority: high difficulty: medium labels Apr 17, 2024
@jordalgo
Copy link
Contributor

jordalgo commented Apr 17, 2024

We can use a const volatile variable for nr_cpus and update it from userspace before program load then use bpf_for(i, 0, nr_cpus) { to iterate.

Note: Open-coded iterators didn't make it in until kernel 6.4

@jordalgo jordalgo self-assigned this May 8, 2024
@ajor ajor added the language Changes to the bpftrace language label May 13, 2024
jordalgo pushed a commit to jordalgo/bpftrace that referenced this issue May 20, 2024
This utilizes 'map_lookup_percpu_elem' to iterate over
the per-cpu maps that 'sum' and 'count' use to aggregate
the values for each cpu so these maps can be used in kernel
space for conditionals and arithmetic.

Issue: bpftrace#3126

Note: We can do the same to support to 'avg', 'max', 'min'
but those require the same user-space logic written in llvm ir.
jordalgo pushed a commit to jordalgo/bpftrace that referenced this issue May 20, 2024
This utilizes 'map_lookup_percpu_elem' to iterate over
the per-cpu maps that 'sum' and 'count' use to aggregate
the values for each cpu so these maps can be used in kernel
space for conditionals and arithmetic.

Issue: bpftrace#3126

Note: We can do the same to support to 'avg', 'max', 'min'
but those require the same user-space logic written in llvm ir.
jordalgo pushed a commit to jordalgo/bpftrace that referenced this issue May 20, 2024
This utilizes 'map_lookup_percpu_elem' to iterate over
the per-cpu maps that 'sum' and 'count' use to aggregate
the values for each cpu so these maps can be used in kernel
space for conditionals and arithmetic.

Issue: bpftrace#3126

Note: We can do the same to support to 'avg', 'max', 'min'
but those require the same user-space logic written in llvm ir.
jordalgo pushed a commit to jordalgo/bpftrace that referenced this issue May 21, 2024
This utilizes 'map_lookup_percpu_elem' to iterate over
the per-cpu maps that 'sum' and 'count' use to aggregate
the values for each cpu so these maps can be used in kernel
space for conditionals and arithmetic.

Additionally, add explicit casting for printing or storing
just the value for these types of maps.

Issue: bpftrace#3126

Note: We can do the same to support to 'avg', 'max', 'min'
but those require the same user-space logic written in llvm ir.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: medium enhancement New feature or request, changes on existing features language Changes to the bpftrace language power-user priority: high
Projects
None yet
Development

No branches or pull requests

3 participants