cpustat_user.c 5.7 KB

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