bcc/Dose bcc tools support read/write global variables from bpf program?
- Dominant language
- C
- Stars
- 22.7k
- Forks
- 4.1k
- Avg merge
- 5d 13h
- Merged PRs (30d)
- 3
Description
(It seems that BCC Tools does not support it. If supported please ignore the following)
Hi I found that we could use global variables instead of `bpf-map` (`BPF_MAP_TYPE_ARRAY`) to store small arrays. (with libbpf-tools)
For `bpf-map` we awalys do like this `val = bpf_map_lookup_elem(&array, &key); if (val) (*val)++;`.
For global variables we can do like this `array[key]++`.
(The overhead of reading from user space is not taken into account, as user space typically does not read data very frequently)
In theory, the second approach should have less overhead.
But in practice, overhead is basically the same.
Here's my test.
Use `bpf-map`
```
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 1024);
__type(key, u32);
__type(value, u32);
} count SEC(".maps");
SEC("kprobe/vfs_read")
int BPF_KPROBE(vfs_read) {
static int key = 0;
u32 *val;
key++;
if (key >= 1024)
key = 0;
val = bpf_map_lookup_elem(&count, &key);
if (val)
(*val)++;
}
```
Use global variables
```
__u32 count[1024] = {};
SEC("kprobe/vfs_read")
int BPF_KPROBE(vfs_read) {
static int key = 0;
key++;
if (key >= 1024) {
key = 0;
count[key]++;
}
return 0;
}
```
test with `dd if=/dev/zero of=/dev/null bs=1 count=5242880`
The final throughput is similar.
(Maybe the `dd` tool is not suitable as a testing tool for this issue.)
**Should bcc tools support read/write global variable from bpf program?**
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by examining how BCC tools represent and expose BPF global variables, comparing that behavior with the libbpf-tools approach described in the issue. Reproduce the supplied BPF-map and global-variable examples with the dd workload, then establish whether read/write support is feasible and what successful support should include.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, linux
- Domain
- observability, operating-systems, tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100