samples/bpf/tracex3.bpf.c
Source file repositories/reference/linux-study-clean/samples/bpf/tracex3.bpf.c
File Facts
- System
- Linux kernel
- Corpus path
samples/bpf/tracex3.bpf.c- Extension
.c- Size
- 2274 bytes
- Lines
- 101
- Domain
- Support Tooling And Documentation
- Bucket
- samples
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
vmlinux.hlinux/version.hbpf/bpf_helpers.hbpf/bpf_tracing.h
Detected Declarations
struct start_keyfunction bpf_prog1function log2lfunction bpf_prog2
Annotated Snippet
struct start_key {
dev_t dev;
u32 _pad;
sector_t sector;
};
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, long);
__type(value, u64);
__uint(max_entries, 4096);
} my_map SEC(".maps");
/* from /sys/kernel/tracing/events/block/block_io_start/format */
SEC("tracepoint/block/block_io_start")
int bpf_prog1(struct trace_event_raw_block_rq *ctx)
{
u64 val = bpf_ktime_get_ns();
struct start_key key = {
.dev = ctx->dev,
.sector = ctx->sector
};
bpf_map_update_elem(&my_map, &key, &val, BPF_ANY);
return 0;
}
static unsigned int log2l(unsigned long long n)
{
#define S(k) if (n >= (1ull << k)) { i += k; n >>= k; }
int i = -(n == 0);
S(32); S(16); S(8); S(4); S(2); S(1);
return i;
#undef S
}
#define SLOTS 100
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__uint(key_size, sizeof(u32));
__uint(value_size, sizeof(u64));
__uint(max_entries, SLOTS);
} lat_map SEC(".maps");
/* from /sys/kernel/tracing/events/block/block_io_done/format */
SEC("tracepoint/block/block_io_done")
int bpf_prog2(struct trace_event_raw_block_rq *ctx)
{
struct start_key key = {
.dev = ctx->dev,
.sector = ctx->sector
};
u64 *value, l, base;
u32 index;
value = bpf_map_lookup_elem(&my_map, &key);
if (!value)
return 0;
u64 cur_time = bpf_ktime_get_ns();
u64 delta = cur_time - *value;
bpf_map_delete_elem(&my_map, &key);
/* the lines below are computing index = log10(delta)*10
* using integer arithmetic
* index = 29 ~ 1 usec
* index = 59 ~ 1 msec
* index = 89 ~ 1 sec
* index = 99 ~ 10sec or more
* log10(x)*10 = log2(x)*10/log2(10) = log2(x)*3
*/
l = log2l(delta);
base = 1ll << l;
index = (l * 64 + (delta - base) * 64 / base) * 3 / 64;
if (index >= SLOTS)
index = SLOTS - 1;
value = bpf_map_lookup_elem(&lat_map, &index);
if (value)
*value += 1;
return 0;
}
char _license[] SEC("license") = "GPL";
u32 _version SEC("version") = LINUX_VERSION_CODE;
Annotation
- Immediate include surface: `vmlinux.h`, `linux/version.h`, `bpf/bpf_helpers.h`, `bpf/bpf_tracing.h`.
- Detected declarations: `struct start_key`, `function bpf_prog1`, `function log2l`, `function bpf_prog2`.
- Atlas domain: Support Tooling And Documentation / samples.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.