cpustat_user.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <errno.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <signal.h>
  7. #include <sched.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11. #include <locale.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <sys/time.h>
  15. #include <sys/wait.h>
  16. #include <bpf/bpf.h>
  17. #include <bpf/libbpf.h>
  18. static int cstate_map_fd, pstate_map_fd;
  19. #define MAX_CPU 8
  20. #define MAX_PSTATE_ENTRIES 5
  21. #define MAX_CSTATE_ENTRIES 3
  22. #define MAX_STARS 40
  23. #define CPUFREQ_MAX_SYSFS_PATH "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq"
  24. #define CPUFREQ_LOWEST_FREQ "208000"
  25. #define CPUFREQ_HIGHEST_FREQ "12000000"
  26. struct cpu_stat_data {
  27. unsigned long cstate[MAX_CSTATE_ENTRIES];
  28. unsigned long pstate[MAX_PSTATE_ENTRIES];
  29. };
  30. static struct cpu_stat_data stat_data[MAX_CPU];
  31. static void cpu_stat_print(void)
  32. {
  33. int i, j;
  34. char state_str[sizeof("cstate-9")];
  35. struct cpu_stat_data *data;
  36. /* Clear screen */
  37. printf("\033[2J");
  38. /* Header */
  39. printf("\nCPU states statistics:\n");
  40. printf("%-10s ", "state(ms)");
  41. for (i = 0; i < MAX_CSTATE_ENTRIES; i++) {
  42. sprintf(state_str, "cstate-%d", i);
  43. printf("%-11s ", state_str);
  44. }
  45. for (i = 0; i < MAX_PSTATE_ENTRIES; i++) {
  46. sprintf(state_str, "pstate-%d", i);
  47. printf("%-11s ", state_str);
  48. }
  49. printf("\n");
  50. for (j = 0; j < MAX_CPU; j++) {
  51. data = &stat_data[j];
  52. printf("CPU-%-6d ", j);
  53. for (i = 0; i < MAX_CSTATE_ENTRIES; i++)
  54. printf("%-11ld ", data->cstate[i] / 1000000);
  55. for (i = 0; i < MAX_PSTATE_ENTRIES; i++)
  56. printf("%-11ld ", data->pstate[i] / 1000000);
  57. printf("\n");
  58. }
  59. }
  60. static void cpu_stat_update(int cstate_fd, int pstate_fd)
  61. {
  62. unsigned long key, value;
  63. int c, i;
  64. for (c = 0; c < MAX_CPU; c++) {
  65. for (i = 0; i < MAX_CSTATE_ENTRIES; i++) {
  66. key = c * MAX_CSTATE_ENTRIES + i;
  67. bpf_map_lookup_elem(cstate_fd, &key, &value);
  68. stat_data[c].cstate[i] = value;
  69. }
  70. for (i = 0; i < MAX_PSTATE_ENTRIES; i++) {
  71. key = c * MAX_PSTATE_ENTRIES + i;
  72. bpf_map_lookup_elem(pstate_fd, &key, &value);
  73. stat_data[c].pstate[i] = value;
  74. }
  75. }
  76. }
  77. /*
  78. * This function is copied from 'idlestat' tool function
  79. * idlestat_wake_all() in idlestate.c.
  80. *
  81. * It sets the self running task affinity to cpus one by one so can wake up
  82. * the specific CPU to handle scheduling; this results in all cpus can be
  83. * waken up once and produce ftrace event 'trace_cpu_idle'.
  84. */
  85. static int cpu_stat_inject_cpu_idle_event(void)
  86. {
  87. int rcpu, i, ret;
  88. cpu_set_t cpumask;
  89. cpu_set_t original_cpumask;
  90. ret = sysconf(_SC_NPROCESSORS_CONF);
  91. if (ret < 0)
  92. return -1;
  93. rcpu = sched_getcpu();
  94. if (rcpu < 0)
  95. return -1;
  96. /* Keep track of the CPUs we will run on */
  97. sched_getaffinity(0, sizeof(original_cpumask), &original_cpumask);
  98. for (i = 0; i < ret; i++) {
  99. /* Pointless to wake up ourself */
  100. if (i == rcpu)
  101. continue;
  102. /* Pointless to wake CPUs we will not run on */
  103. if (!CPU_ISSET(i, &original_cpumask))
  104. continue;
  105. CPU_ZERO(&cpumask);
  106. CPU_SET(i, &cpumask);
  107. sched_setaffinity(0, sizeof(cpumask), &cpumask);
  108. }
  109. /* Enable all the CPUs of the original mask */
  110. sched_setaffinity(0, sizeof(original_cpumask), &original_cpumask);
  111. return 0;
  112. }
  113. /*
  114. * It's possible to have no any frequency change for long time and cannot
  115. * get ftrace event 'trace_cpu_frequency' for long period, this introduces
  116. * big deviation for pstate statistics.
  117. *
  118. * To solve this issue, below code forces to set 'scaling_max_freq' to 208MHz
  119. * for triggering ftrace event 'trace_cpu_frequency' and then recovery back to
  120. * the maximum frequency value 1.2GHz.
  121. */
  122. static int cpu_stat_inject_cpu_frequency_event(void)
  123. {
  124. int len, fd;
  125. fd = open(CPUFREQ_MAX_SYSFS_PATH, O_WRONLY);
  126. if (fd < 0) {
  127. printf("failed to open scaling_max_freq, errno=%d\n", errno);
  128. return fd;
  129. }
  130. len = write(fd, CPUFREQ_LOWEST_FREQ, strlen(CPUFREQ_LOWEST_FREQ));
  131. if (len < 0) {
  132. printf("failed to open scaling_max_freq, errno=%d\n", errno);
  133. goto err;
  134. }
  135. len = write(fd, CPUFREQ_HIGHEST_FREQ, strlen(CPUFREQ_HIGHEST_FREQ));
  136. if (len < 0) {
  137. printf("failed to open scaling_max_freq, errno=%d\n", errno);
  138. goto err;
  139. }
  140. err:
  141. close(fd);
  142. return len;
  143. }
  144. static void int_exit(int sig)
  145. {
  146. cpu_stat_inject_cpu_idle_event();
  147. cpu_stat_inject_cpu_frequency_event();
  148. cpu_stat_update(cstate_map_fd, pstate_map_fd);
  149. cpu_stat_print();
  150. exit(0);
  151. }
  152. int main(int argc, char **argv)
  153. {
  154. struct bpf_link *link = NULL;
  155. struct bpf_program *prog;
  156. struct bpf_object *obj;
  157. char filename[256];
  158. int ret;
  159. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  160. obj = bpf_object__open_file(filename, NULL);
  161. if (libbpf_get_error(obj)) {
  162. fprintf(stderr, "ERROR: opening BPF object file failed\n");
  163. return 0;
  164. }
  165. prog = bpf_object__find_program_by_name(obj, "bpf_prog1");
  166. if (!prog) {
  167. printf("finding a prog in obj file failed\n");
  168. goto cleanup;
  169. }
  170. /* load BPF program */
  171. if (bpf_object__load(obj)) {
  172. fprintf(stderr, "ERROR: loading BPF object file failed\n");
  173. goto cleanup;
  174. }
  175. cstate_map_fd = bpf_object__find_map_fd_by_name(obj, "cstate_duration");
  176. pstate_map_fd = bpf_object__find_map_fd_by_name(obj, "pstate_duration");
  177. if (cstate_map_fd < 0 || pstate_map_fd < 0) {
  178. fprintf(stderr, "ERROR: finding a map in obj file failed\n");
  179. goto cleanup;
  180. }
  181. link = bpf_program__attach(prog);
  182. if (libbpf_get_error(link)) {
  183. fprintf(stderr, "ERROR: bpf_program__attach failed\n");
  184. link = NULL;
  185. goto cleanup;
  186. }
  187. ret = cpu_stat_inject_cpu_idle_event();
  188. if (ret < 0)
  189. return 1;
  190. ret = cpu_stat_inject_cpu_frequency_event();
  191. if (ret < 0)
  192. return 1;
  193. signal(SIGINT, int_exit);
  194. signal(SIGTERM, int_exit);
  195. while (1) {
  196. cpu_stat_update(cstate_map_fd, pstate_map_fd);
  197. cpu_stat_print();
  198. sleep(5);
  199. }
  200. cleanup:
  201. bpf_link__destroy(link);
  202. bpf_object__close(obj);
  203. return 0;
  204. }