1 // SPDX-License-Identifier: GPL-2.0
2 #ifdef HAVE_BPF_SKEL
3 #define _GNU_SOURCE
4 #include "timerlat.h"
5 #include "timerlat_bpf.h"
6 #include "timerlat.skel.h"
7
8 static struct timerlat_bpf *bpf;
9
10 /*
11 * timerlat_bpf_init - load and initialize BPF program to collect timerlat data
12 */
timerlat_bpf_init(struct timerlat_params * params)13 int timerlat_bpf_init(struct timerlat_params *params)
14 {
15 int err;
16
17 debug_msg("Loading BPF program\n");
18
19 bpf = timerlat_bpf__open();
20 if (!bpf)
21 return 1;
22
23 /* Pass common options */
24 bpf->rodata->output_divisor = params->output_divisor;
25 bpf->rodata->entries = params->entries;
26 bpf->rodata->irq_threshold = params->stop_us;
27 bpf->rodata->thread_threshold = params->stop_total_us;
28 bpf->rodata->aa_only = params->aa_only;
29
30 if (params->entries != 0) {
31 /* Pass histogram options */
32 bpf->rodata->bucket_size = params->bucket_size;
33
34 /* Set histogram array sizes */
35 bpf_map__set_max_entries(bpf->maps.hist_irq, params->entries);
36 bpf_map__set_max_entries(bpf->maps.hist_thread, params->entries);
37 bpf_map__set_max_entries(bpf->maps.hist_user, params->entries);
38 } else {
39 /* No entries, disable histogram */
40 bpf_map__set_autocreate(bpf->maps.hist_irq, false);
41 bpf_map__set_autocreate(bpf->maps.hist_thread, false);
42 bpf_map__set_autocreate(bpf->maps.hist_user, false);
43 }
44
45 if (params->aa_only) {
46 /* Auto-analysis only, disable summary */
47 bpf_map__set_autocreate(bpf->maps.summary_irq, false);
48 bpf_map__set_autocreate(bpf->maps.summary_thread, false);
49 bpf_map__set_autocreate(bpf->maps.summary_user, false);
50 }
51
52 /* Load and verify BPF program */
53 err = timerlat_bpf__load(bpf);
54 if (err) {
55 timerlat_bpf__destroy(bpf);
56 return err;
57 }
58
59 return 0;
60 }
61
62 /*
63 * timerlat_bpf_attach - attach BPF program to collect timerlat data
64 */
timerlat_bpf_attach(void)65 int timerlat_bpf_attach(void)
66 {
67 debug_msg("Attaching BPF program\n");
68
69 return timerlat_bpf__attach(bpf);
70 }
71
72 /*
73 * timerlat_bpf_detach - detach BPF program to collect timerlat data
74 */
timerlat_bpf_detach(void)75 void timerlat_bpf_detach(void)
76 {
77 timerlat_bpf__detach(bpf);
78 }
79
80 /*
81 * timerlat_bpf_detach - destroy BPF program to collect timerlat data
82 */
timerlat_bpf_destroy(void)83 void timerlat_bpf_destroy(void)
84 {
85 timerlat_bpf__destroy(bpf);
86 }
87
handle_rb_event(void * ctx,void * data,size_t data_sz)88 static int handle_rb_event(void *ctx, void *data, size_t data_sz)
89 {
90 return 0;
91 }
92
93 /*
94 * timerlat_bpf_wait - wait until tracing is stopped or signal
95 */
timerlat_bpf_wait(int timeout)96 int timerlat_bpf_wait(int timeout)
97 {
98 struct ring_buffer *rb;
99 int retval;
100
101 rb = ring_buffer__new(bpf_map__fd(bpf->maps.signal_stop_tracing),
102 handle_rb_event, NULL, NULL);
103 retval = ring_buffer__poll(rb, timeout * 1000);
104 ring_buffer__free(rb);
105
106 return retval;
107 }
108
get_value(struct bpf_map * map_irq,struct bpf_map * map_thread,struct bpf_map * map_user,int key,long long * value_irq,long long * value_thread,long long * value_user,int cpus)109 static int get_value(struct bpf_map *map_irq,
110 struct bpf_map *map_thread,
111 struct bpf_map *map_user,
112 int key,
113 long long *value_irq,
114 long long *value_thread,
115 long long *value_user,
116 int cpus)
117 {
118 int err;
119
120 err = bpf_map__lookup_elem(map_irq, &key,
121 sizeof(unsigned int), value_irq,
122 sizeof(long long) * cpus, 0);
123 if (err)
124 return err;
125 err = bpf_map__lookup_elem(map_thread, &key,
126 sizeof(unsigned int), value_thread,
127 sizeof(long long) * cpus, 0);
128 if (err)
129 return err;
130 err = bpf_map__lookup_elem(map_user, &key,
131 sizeof(unsigned int), value_user,
132 sizeof(long long) * cpus, 0);
133 if (err)
134 return err;
135 return 0;
136 }
137
138 /*
139 * timerlat_bpf_get_hist_value - get value from BPF hist map
140 */
timerlat_bpf_get_hist_value(int key,long long * value_irq,long long * value_thread,long long * value_user,int cpus)141 int timerlat_bpf_get_hist_value(int key,
142 long long *value_irq,
143 long long *value_thread,
144 long long *value_user,
145 int cpus)
146 {
147 return get_value(bpf->maps.hist_irq,
148 bpf->maps.hist_thread,
149 bpf->maps.hist_user,
150 key, value_irq, value_thread, value_user, cpus);
151 }
152
153 /*
154 * timerlat_bpf_get_summary_value - get value from BPF summary map
155 */
timerlat_bpf_get_summary_value(enum summary_field key,long long * value_irq,long long * value_thread,long long * value_user,int cpus)156 int timerlat_bpf_get_summary_value(enum summary_field key,
157 long long *value_irq,
158 long long *value_thread,
159 long long *value_user,
160 int cpus)
161 {
162 return get_value(bpf->maps.summary_irq,
163 bpf->maps.summary_thread,
164 bpf->maps.summary_user,
165 key, value_irq, value_thread, value_user, cpus);
166 }
167 #endif /* HAVE_BPF_SKEL */
168