forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slabratetop.bpf.c
53 lines (42 loc) · 1.29 KB
/
slabratetop.bpf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
/* Copyright (c) 2022 Rong Tao */
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
#include <bpf/bpf_tracing.h>
#include "slabratetop.h"
#define MAX_ENTRIES 10240
const volatile pid_t target_pid = 0;
static struct slabrate_info slab_zero_value = {};
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, MAX_ENTRIES);
__type(key, char *);
__type(value, struct slabrate_info);
} slab_entries SEC(".maps");
static int probe_entry(struct kmem_cache *cachep)
{
__u64 pid_tgid = bpf_get_current_pid_tgid();
__u32 pid = pid_tgid >> 32;
struct slabrate_info *valuep;
const char *name = BPF_CORE_READ(cachep, name);
if (target_pid && target_pid != pid)
return 0;
valuep = bpf_map_lookup_elem(&slab_entries, &name);
if (!valuep) {
bpf_map_update_elem(&slab_entries, &name, &slab_zero_value, BPF_ANY);
valuep = bpf_map_lookup_elem(&slab_entries, &name);
if (!valuep)
return 0;
bpf_probe_read_kernel(&valuep->name, sizeof(valuep->name), name);
}
valuep->count++;
valuep->size += BPF_CORE_READ(cachep, size);
return 0;
}
SEC("kprobe/kmem_cache_alloc")
int BPF_KPROBE(kmem_cache_alloc, struct kmem_cache *cachep)
{
return probe_entry(cachep);
}
char LICENSE[] SEC("license") = "Dual BSD/GPL";