xref: /aosp_15_r20/external/bcc/libbpf-tools/javagc.bpf.c (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 /* Copyright (c) 2022 Chen Tao */
3 #include <vmlinux.h>
4 #include <bpf/bpf_helpers.h>
5 #include <bpf/bpf_core_read.h>
6 #include <bpf/usdt.bpf.h>
7 #include "javagc.h"
8 
9 struct {
10 	__uint(type, BPF_MAP_TYPE_HASH);
11 	__uint(max_entries, 100);
12 	__type(key, uint32_t);
13 	__type(value, struct data_t);
14 } data_map SEC(".maps");
15 
16 struct {
17 	__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
18 	__type(key, int);
19 	__type(value, int);
20 } perf_map SEC(".maps");
21 
22 __u32 time;
23 
gc_start(struct pt_regs * ctx)24 static int gc_start(struct pt_regs *ctx)
25 {
26 	struct data_t data = {};
27 
28 	data.cpu = bpf_get_smp_processor_id();
29 	data.pid = bpf_get_current_pid_tgid() >> 32;
30 	data.ts = bpf_ktime_get_ns();
31 	bpf_map_update_elem(&data_map, &data.pid, &data, 0);
32 	return 0;
33 }
34 
gc_end(struct pt_regs * ctx)35 static int gc_end(struct pt_regs *ctx)
36 {
37 	struct data_t data = {};
38 	struct data_t *p;
39 	__u32 val;
40 
41 	data.cpu = bpf_get_smp_processor_id();
42 	data.pid = bpf_get_current_pid_tgid() >> 32;
43 	data.ts = bpf_ktime_get_ns();
44 	p = bpf_map_lookup_elem(&data_map, &data.pid);
45 	if (!p)
46 		return 0;
47 
48 	val = data.ts - p->ts;
49 	if (val > time) {
50 		data.ts = val;
51 		bpf_perf_event_output(ctx, &perf_map, BPF_F_CURRENT_CPU, &data, sizeof(data));
52 	}
53 	bpf_map_delete_elem(&data_map, &data.pid);
54 	return 0;
55 }
56 
57 SEC("usdt")
handle_gc_start(struct pt_regs * ctx)58 int handle_gc_start(struct pt_regs *ctx)
59 {
60 	return gc_start(ctx);
61 }
62 
63 SEC("usdt")
handle_gc_end(struct pt_regs * ctx)64 int handle_gc_end(struct pt_regs *ctx)
65 {
66 	return gc_end(ctx);
67 }
68 
69 SEC("usdt")
handle_mem_pool_gc_start(struct pt_regs * ctx)70 int handle_mem_pool_gc_start(struct pt_regs *ctx)
71 {
72 	return gc_start(ctx);
73 }
74 
75 SEC("usdt")
handle_mem_pool_gc_end(struct pt_regs * ctx)76 int handle_mem_pool_gc_end(struct pt_regs *ctx)
77 {
78 	return gc_end(ctx);
79 }
80 
81 char LICENSE[] SEC("license") = "Dual BSD/GPL";
82