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