ext4: fix some nonstandard indentation in extents.c
[linux/fpc-iii.git] / samples / bpf / cpustat_kern.c
blob68c84da065b1515a5fbe8141011fc37c1f55193c
1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/version.h>
4 #include <linux/ptrace.h>
5 #include <uapi/linux/bpf.h>
6 #include "bpf_helpers.h"
8 /*
9 * The CPU number, cstate number and pstate number are based
10 * on 96boards Hikey with octa CA53 CPUs.
12 * Every CPU have three idle states for cstate:
13 * WFI, CPU_OFF, CLUSTER_OFF
15 * Every CPU have 5 operating points:
16 * 208MHz, 432MHz, 729MHz, 960MHz, 1200MHz
18 * This code is based on these assumption and other platforms
19 * need to adjust these definitions.
21 #define MAX_CPU 8
22 #define MAX_PSTATE_ENTRIES 5
23 #define MAX_CSTATE_ENTRIES 3
25 static int cpu_opps[] = { 208000, 432000, 729000, 960000, 1200000 };
28 * my_map structure is used to record cstate and pstate index and
29 * timestamp (Idx, Ts), when new event incoming we need to update
30 * combination for new state index and timestamp (Idx`, Ts`).
32 * Based on (Idx, Ts) and (Idx`, Ts`) we can calculate the time
33 * interval for the previous state: Duration(Idx) = Ts` - Ts.
35 * Every CPU has one below array for recording state index and
36 * timestamp, and record for cstate and pstate saperately:
38 * +--------------------------+
39 * | cstate timestamp |
40 * +--------------------------+
41 * | cstate index |
42 * +--------------------------+
43 * | pstate timestamp |
44 * +--------------------------+
45 * | pstate index |
46 * +--------------------------+
48 #define MAP_OFF_CSTATE_TIME 0
49 #define MAP_OFF_CSTATE_IDX 1
50 #define MAP_OFF_PSTATE_TIME 2
51 #define MAP_OFF_PSTATE_IDX 3
52 #define MAP_OFF_NUM 4
54 struct bpf_map_def SEC("maps") my_map = {
55 .type = BPF_MAP_TYPE_ARRAY,
56 .key_size = sizeof(u32),
57 .value_size = sizeof(u64),
58 .max_entries = MAX_CPU * MAP_OFF_NUM,
61 /* cstate_duration records duration time for every idle state per CPU */
62 struct bpf_map_def SEC("maps") cstate_duration = {
63 .type = BPF_MAP_TYPE_ARRAY,
64 .key_size = sizeof(u32),
65 .value_size = sizeof(u64),
66 .max_entries = MAX_CPU * MAX_CSTATE_ENTRIES,
69 /* pstate_duration records duration time for every operating point per CPU */
70 struct bpf_map_def SEC("maps") pstate_duration = {
71 .type = BPF_MAP_TYPE_ARRAY,
72 .key_size = sizeof(u32),
73 .value_size = sizeof(u64),
74 .max_entries = MAX_CPU * MAX_PSTATE_ENTRIES,
78 * The trace events for cpu_idle and cpu_frequency are taken from:
79 * /sys/kernel/debug/tracing/events/power/cpu_idle/format
80 * /sys/kernel/debug/tracing/events/power/cpu_frequency/format
82 * These two events have same format, so define one common structure.
84 struct cpu_args {
85 u64 pad;
86 u32 state;
87 u32 cpu_id;
90 /* calculate pstate index, returns MAX_PSTATE_ENTRIES for failure */
91 static u32 find_cpu_pstate_idx(u32 frequency)
93 u32 i;
95 for (i = 0; i < sizeof(cpu_opps) / sizeof(u32); i++) {
96 if (frequency == cpu_opps[i])
97 return i;
100 return i;
103 SEC("tracepoint/power/cpu_idle")
104 int bpf_prog1(struct cpu_args *ctx)
106 u64 *cts, *pts, *cstate, *pstate, prev_state, cur_ts, delta;
107 u32 key, cpu, pstate_idx;
108 u64 *val;
110 if (ctx->cpu_id > MAX_CPU)
111 return 0;
113 cpu = ctx->cpu_id;
115 key = cpu * MAP_OFF_NUM + MAP_OFF_CSTATE_TIME;
116 cts = bpf_map_lookup_elem(&my_map, &key);
117 if (!cts)
118 return 0;
120 key = cpu * MAP_OFF_NUM + MAP_OFF_CSTATE_IDX;
121 cstate = bpf_map_lookup_elem(&my_map, &key);
122 if (!cstate)
123 return 0;
125 key = cpu * MAP_OFF_NUM + MAP_OFF_PSTATE_TIME;
126 pts = bpf_map_lookup_elem(&my_map, &key);
127 if (!pts)
128 return 0;
130 key = cpu * MAP_OFF_NUM + MAP_OFF_PSTATE_IDX;
131 pstate = bpf_map_lookup_elem(&my_map, &key);
132 if (!pstate)
133 return 0;
135 prev_state = *cstate;
136 *cstate = ctx->state;
138 if (!*cts) {
139 *cts = bpf_ktime_get_ns();
140 return 0;
143 cur_ts = bpf_ktime_get_ns();
144 delta = cur_ts - *cts;
145 *cts = cur_ts;
148 * When state doesn't equal to (u32)-1, the cpu will enter
149 * one idle state; for this case we need to record interval
150 * for the pstate.
152 * OPP2
153 * +---------------------+
154 * OPP1 | |
155 * ---------+ |
156 * | Idle state
157 * +---------------
159 * |<- pstate duration ->|
160 * ^ ^
161 * pts cur_ts
163 if (ctx->state != (u32)-1) {
165 /* record pstate after have first cpu_frequency event */
166 if (!*pts)
167 return 0;
169 delta = cur_ts - *pts;
171 pstate_idx = find_cpu_pstate_idx(*pstate);
172 if (pstate_idx >= MAX_PSTATE_ENTRIES)
173 return 0;
175 key = cpu * MAX_PSTATE_ENTRIES + pstate_idx;
176 val = bpf_map_lookup_elem(&pstate_duration, &key);
177 if (val)
178 __sync_fetch_and_add((long *)val, delta);
181 * When state equal to (u32)-1, the cpu just exits from one
182 * specific idle state; for this case we need to record
183 * interval for the pstate.
185 * OPP2
186 * -----------+
187 * | OPP1
188 * | +-----------
189 * | Idle state |
190 * +---------------------+
192 * |<- cstate duration ->|
193 * ^ ^
194 * cts cur_ts
196 } else {
198 key = cpu * MAX_CSTATE_ENTRIES + prev_state;
199 val = bpf_map_lookup_elem(&cstate_duration, &key);
200 if (val)
201 __sync_fetch_and_add((long *)val, delta);
204 /* Update timestamp for pstate as new start time */
205 if (*pts)
206 *pts = cur_ts;
208 return 0;
211 SEC("tracepoint/power/cpu_frequency")
212 int bpf_prog2(struct cpu_args *ctx)
214 u64 *pts, *cstate, *pstate, prev_state, cur_ts, delta;
215 u32 key, cpu, pstate_idx;
216 u64 *val;
218 cpu = ctx->cpu_id;
220 key = cpu * MAP_OFF_NUM + MAP_OFF_PSTATE_TIME;
221 pts = bpf_map_lookup_elem(&my_map, &key);
222 if (!pts)
223 return 0;
225 key = cpu * MAP_OFF_NUM + MAP_OFF_PSTATE_IDX;
226 pstate = bpf_map_lookup_elem(&my_map, &key);
227 if (!pstate)
228 return 0;
230 key = cpu * MAP_OFF_NUM + MAP_OFF_CSTATE_IDX;
231 cstate = bpf_map_lookup_elem(&my_map, &key);
232 if (!cstate)
233 return 0;
235 prev_state = *pstate;
236 *pstate = ctx->state;
238 if (!*pts) {
239 *pts = bpf_ktime_get_ns();
240 return 0;
243 cur_ts = bpf_ktime_get_ns();
244 delta = cur_ts - *pts;
245 *pts = cur_ts;
247 /* When CPU is in idle, bail out to skip pstate statistics */
248 if (*cstate != (u32)(-1))
249 return 0;
252 * The cpu changes to another different OPP (in below diagram
253 * change frequency from OPP3 to OPP1), need recording interval
254 * for previous frequency OPP3 and update timestamp as start
255 * time for new frequency OPP1.
257 * OPP3
258 * +---------------------+
259 * OPP2 | |
260 * ---------+ |
261 * | OPP1
262 * +---------------
264 * |<- pstate duration ->|
265 * ^ ^
266 * pts cur_ts
268 pstate_idx = find_cpu_pstate_idx(*pstate);
269 if (pstate_idx >= MAX_PSTATE_ENTRIES)
270 return 0;
272 key = cpu * MAX_PSTATE_ENTRIES + pstate_idx;
273 val = bpf_map_lookup_elem(&pstate_duration, &key);
274 if (val)
275 __sync_fetch_and_add((long *)val, delta);
277 return 0;
280 char _license[] SEC("license") = "GPL";
281 u32 _version SEC("version") = LINUX_VERSION_CODE;