1 // SPDX-License-Identifier: GPL-2.0
13 #include <sys/types.h>
16 #include <sys/resource.h>
20 #include <bpf/libbpf.h>
22 static int cstate_map_fd
, pstate_map_fd
;
25 #define MAX_PSTATE_ENTRIES 5
26 #define MAX_CSTATE_ENTRIES 3
29 #define CPUFREQ_MAX_SYSFS_PATH "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq"
30 #define CPUFREQ_LOWEST_FREQ "208000"
31 #define CPUFREQ_HIGHEST_FREQ "12000000"
33 struct cpu_stat_data
{
34 unsigned long cstate
[MAX_CSTATE_ENTRIES
];
35 unsigned long pstate
[MAX_PSTATE_ENTRIES
];
38 static struct cpu_stat_data stat_data
[MAX_CPU
];
40 static void cpu_stat_print(void)
43 char state_str
[sizeof("cstate-9")];
44 struct cpu_stat_data
*data
;
50 printf("\nCPU states statistics:\n");
51 printf("%-10s ", "state(ms)");
53 for (i
= 0; i
< MAX_CSTATE_ENTRIES
; i
++) {
54 sprintf(state_str
, "cstate-%d", i
);
55 printf("%-11s ", state_str
);
58 for (i
= 0; i
< MAX_PSTATE_ENTRIES
; i
++) {
59 sprintf(state_str
, "pstate-%d", i
);
60 printf("%-11s ", state_str
);
65 for (j
= 0; j
< MAX_CPU
; j
++) {
68 printf("CPU-%-6d ", j
);
69 for (i
= 0; i
< MAX_CSTATE_ENTRIES
; i
++)
70 printf("%-11ld ", data
->cstate
[i
] / 1000000);
72 for (i
= 0; i
< MAX_PSTATE_ENTRIES
; i
++)
73 printf("%-11ld ", data
->pstate
[i
] / 1000000);
79 static void cpu_stat_update(int cstate_fd
, int pstate_fd
)
81 unsigned long key
, value
;
84 for (c
= 0; c
< MAX_CPU
; c
++) {
85 for (i
= 0; i
< MAX_CSTATE_ENTRIES
; i
++) {
86 key
= c
* MAX_CSTATE_ENTRIES
+ i
;
87 bpf_map_lookup_elem(cstate_fd
, &key
, &value
);
88 stat_data
[c
].cstate
[i
] = value
;
91 for (i
= 0; i
< MAX_PSTATE_ENTRIES
; i
++) {
92 key
= c
* MAX_PSTATE_ENTRIES
+ i
;
93 bpf_map_lookup_elem(pstate_fd
, &key
, &value
);
94 stat_data
[c
].pstate
[i
] = value
;
100 * This function is copied from 'idlestat' tool function
101 * idlestat_wake_all() in idlestate.c.
103 * It sets the self running task affinity to cpus one by one so can wake up
104 * the specific CPU to handle scheduling; this results in all cpus can be
105 * waken up once and produce ftrace event 'trace_cpu_idle'.
107 static int cpu_stat_inject_cpu_idle_event(void)
111 cpu_set_t original_cpumask
;
113 ret
= sysconf(_SC_NPROCESSORS_CONF
);
117 rcpu
= sched_getcpu();
121 /* Keep track of the CPUs we will run on */
122 sched_getaffinity(0, sizeof(original_cpumask
), &original_cpumask
);
124 for (i
= 0; i
< ret
; i
++) {
126 /* Pointless to wake up ourself */
130 /* Pointless to wake CPUs we will not run on */
131 if (!CPU_ISSET(i
, &original_cpumask
))
135 CPU_SET(i
, &cpumask
);
137 sched_setaffinity(0, sizeof(cpumask
), &cpumask
);
140 /* Enable all the CPUs of the original mask */
141 sched_setaffinity(0, sizeof(original_cpumask
), &original_cpumask
);
146 * It's possible to have no any frequency change for long time and cannot
147 * get ftrace event 'trace_cpu_frequency' for long period, this introduces
148 * big deviation for pstate statistics.
150 * To solve this issue, below code forces to set 'scaling_max_freq' to 208MHz
151 * for triggering ftrace event 'trace_cpu_frequency' and then recovery back to
152 * the maximum frequency value 1.2GHz.
154 static int cpu_stat_inject_cpu_frequency_event(void)
158 fd
= open(CPUFREQ_MAX_SYSFS_PATH
, O_WRONLY
);
160 printf("failed to open scaling_max_freq, errno=%d\n", errno
);
164 len
= write(fd
, CPUFREQ_LOWEST_FREQ
, strlen(CPUFREQ_LOWEST_FREQ
));
166 printf("failed to open scaling_max_freq, errno=%d\n", errno
);
170 len
= write(fd
, CPUFREQ_HIGHEST_FREQ
, strlen(CPUFREQ_HIGHEST_FREQ
));
172 printf("failed to open scaling_max_freq, errno=%d\n", errno
);
181 static void int_exit(int sig
)
183 cpu_stat_inject_cpu_idle_event();
184 cpu_stat_inject_cpu_frequency_event();
185 cpu_stat_update(cstate_map_fd
, pstate_map_fd
);
190 int main(int argc
, char **argv
)
192 struct bpf_link
*link
= NULL
;
193 struct bpf_program
*prog
;
194 struct bpf_object
*obj
;
198 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
199 obj
= bpf_object__open_file(filename
, NULL
);
200 if (libbpf_get_error(obj
)) {
201 fprintf(stderr
, "ERROR: opening BPF object file failed\n");
205 prog
= bpf_object__find_program_by_name(obj
, "bpf_prog1");
207 printf("finding a prog in obj file failed\n");
211 /* load BPF program */
212 if (bpf_object__load(obj
)) {
213 fprintf(stderr
, "ERROR: loading BPF object file failed\n");
217 cstate_map_fd
= bpf_object__find_map_fd_by_name(obj
, "cstate_duration");
218 pstate_map_fd
= bpf_object__find_map_fd_by_name(obj
, "pstate_duration");
219 if (cstate_map_fd
< 0 || pstate_map_fd
< 0) {
220 fprintf(stderr
, "ERROR: finding a map in obj file failed\n");
224 link
= bpf_program__attach(prog
);
225 if (libbpf_get_error(link
)) {
226 fprintf(stderr
, "ERROR: bpf_program__attach failed\n");
231 ret
= cpu_stat_inject_cpu_idle_event();
235 ret
= cpu_stat_inject_cpu_frequency_event();
239 signal(SIGINT
, int_exit
);
240 signal(SIGTERM
, int_exit
);
243 cpu_stat_update(cstate_map_fd
, pstate_map_fd
);
249 bpf_link__destroy(link
);
250 bpf_object__close(obj
);