bpf_kwork.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * bpf_kwork.c
  4. *
  5. * Copyright (c) 2022 Huawei Inc, Yang Jihong <yangjihong1@huawei.com>
  6. */
  7. #include <time.h>
  8. #include <fcntl.h>
  9. #include <signal.h>
  10. #include <stdio.h>
  11. #include <unistd.h>
  12. #include <linux/time64.h>
  13. #include "util/debug.h"
  14. #include "util/evsel.h"
  15. #include "util/kwork.h"
  16. #include <bpf/bpf.h>
  17. #include <perf/cpumap.h>
  18. #include "util/bpf_skel/kwork_trace.skel.h"
  19. /*
  20. * This should be in sync with "util/kwork_trace.bpf.c"
  21. */
  22. #define MAX_KWORKNAME 128
  23. struct work_key {
  24. u32 type;
  25. u32 cpu;
  26. u64 id;
  27. };
  28. struct report_data {
  29. u64 nr;
  30. u64 total_time;
  31. u64 max_time;
  32. u64 max_time_start;
  33. u64 max_time_end;
  34. };
  35. struct kwork_class_bpf {
  36. struct kwork_class *class;
  37. void (*load_prepare)(struct perf_kwork *kwork);
  38. int (*get_work_name)(struct work_key *key, char **ret_name);
  39. };
  40. static struct kwork_trace_bpf *skel;
  41. static struct timespec ts_start;
  42. static struct timespec ts_end;
  43. void perf_kwork__trace_start(void)
  44. {
  45. clock_gettime(CLOCK_MONOTONIC, &ts_start);
  46. skel->bss->enabled = 1;
  47. }
  48. void perf_kwork__trace_finish(void)
  49. {
  50. clock_gettime(CLOCK_MONOTONIC, &ts_end);
  51. skel->bss->enabled = 0;
  52. }
  53. static int get_work_name_from_map(struct work_key *key, char **ret_name)
  54. {
  55. char name[MAX_KWORKNAME] = { 0 };
  56. int fd = bpf_map__fd(skel->maps.perf_kwork_names);
  57. *ret_name = NULL;
  58. if (fd < 0) {
  59. pr_debug("Invalid names map fd\n");
  60. return 0;
  61. }
  62. if ((bpf_map_lookup_elem(fd, key, name) == 0) && (strlen(name) != 0)) {
  63. *ret_name = strdup(name);
  64. if (*ret_name == NULL) {
  65. pr_err("Failed to copy work name\n");
  66. return -1;
  67. }
  68. }
  69. return 0;
  70. }
  71. static void irq_load_prepare(struct perf_kwork *kwork)
  72. {
  73. if (kwork->report == KWORK_REPORT_RUNTIME) {
  74. bpf_program__set_autoload(skel->progs.report_irq_handler_entry, true);
  75. bpf_program__set_autoload(skel->progs.report_irq_handler_exit, true);
  76. }
  77. }
  78. static struct kwork_class_bpf kwork_irq_bpf = {
  79. .load_prepare = irq_load_prepare,
  80. .get_work_name = get_work_name_from_map,
  81. };
  82. static void softirq_load_prepare(struct perf_kwork *kwork)
  83. {
  84. if (kwork->report == KWORK_REPORT_RUNTIME) {
  85. bpf_program__set_autoload(skel->progs.report_softirq_entry, true);
  86. bpf_program__set_autoload(skel->progs.report_softirq_exit, true);
  87. } else if (kwork->report == KWORK_REPORT_LATENCY) {
  88. bpf_program__set_autoload(skel->progs.latency_softirq_raise, true);
  89. bpf_program__set_autoload(skel->progs.latency_softirq_entry, true);
  90. }
  91. }
  92. static struct kwork_class_bpf kwork_softirq_bpf = {
  93. .load_prepare = softirq_load_prepare,
  94. .get_work_name = get_work_name_from_map,
  95. };
  96. static void workqueue_load_prepare(struct perf_kwork *kwork)
  97. {
  98. if (kwork->report == KWORK_REPORT_RUNTIME) {
  99. bpf_program__set_autoload(skel->progs.report_workqueue_execute_start, true);
  100. bpf_program__set_autoload(skel->progs.report_workqueue_execute_end, true);
  101. } else if (kwork->report == KWORK_REPORT_LATENCY) {
  102. bpf_program__set_autoload(skel->progs.latency_workqueue_activate_work, true);
  103. bpf_program__set_autoload(skel->progs.latency_workqueue_execute_start, true);
  104. }
  105. }
  106. static struct kwork_class_bpf kwork_workqueue_bpf = {
  107. .load_prepare = workqueue_load_prepare,
  108. .get_work_name = get_work_name_from_map,
  109. };
  110. static struct kwork_class_bpf *
  111. kwork_class_bpf_supported_list[KWORK_CLASS_MAX] = {
  112. [KWORK_CLASS_IRQ] = &kwork_irq_bpf,
  113. [KWORK_CLASS_SOFTIRQ] = &kwork_softirq_bpf,
  114. [KWORK_CLASS_WORKQUEUE] = &kwork_workqueue_bpf,
  115. };
  116. static bool valid_kwork_class_type(enum kwork_class_type type)
  117. {
  118. return type >= 0 && type < KWORK_CLASS_MAX ? true : false;
  119. }
  120. static int setup_filters(struct perf_kwork *kwork)
  121. {
  122. if (kwork->cpu_list != NULL) {
  123. int idx, nr_cpus;
  124. struct perf_cpu_map *map;
  125. struct perf_cpu cpu;
  126. int fd = bpf_map__fd(skel->maps.perf_kwork_cpu_filter);
  127. if (fd < 0) {
  128. pr_debug("Invalid cpu filter fd\n");
  129. return -1;
  130. }
  131. map = perf_cpu_map__new(kwork->cpu_list);
  132. if (map == NULL) {
  133. pr_debug("Invalid cpu_list\n");
  134. return -1;
  135. }
  136. nr_cpus = libbpf_num_possible_cpus();
  137. perf_cpu_map__for_each_cpu(cpu, idx, map) {
  138. u8 val = 1;
  139. if (cpu.cpu >= nr_cpus) {
  140. perf_cpu_map__put(map);
  141. pr_err("Requested cpu %d too large\n", cpu.cpu);
  142. return -1;
  143. }
  144. bpf_map_update_elem(fd, &cpu.cpu, &val, BPF_ANY);
  145. }
  146. perf_cpu_map__put(map);
  147. }
  148. if (kwork->profile_name != NULL) {
  149. int key, fd;
  150. if (strlen(kwork->profile_name) >= MAX_KWORKNAME) {
  151. pr_err("Requested name filter %s too large, limit to %d\n",
  152. kwork->profile_name, MAX_KWORKNAME - 1);
  153. return -1;
  154. }
  155. fd = bpf_map__fd(skel->maps.perf_kwork_name_filter);
  156. if (fd < 0) {
  157. pr_debug("Invalid name filter fd\n");
  158. return -1;
  159. }
  160. key = 0;
  161. bpf_map_update_elem(fd, &key, kwork->profile_name, BPF_ANY);
  162. }
  163. return 0;
  164. }
  165. int perf_kwork__trace_prepare_bpf(struct perf_kwork *kwork)
  166. {
  167. struct bpf_program *prog;
  168. struct kwork_class *class;
  169. struct kwork_class_bpf *class_bpf;
  170. enum kwork_class_type type;
  171. skel = kwork_trace_bpf__open();
  172. if (!skel) {
  173. pr_debug("Failed to open kwork trace skeleton\n");
  174. return -1;
  175. }
  176. /*
  177. * set all progs to non-autoload,
  178. * then set corresponding progs according to config
  179. */
  180. bpf_object__for_each_program(prog, skel->obj)
  181. bpf_program__set_autoload(prog, false);
  182. list_for_each_entry(class, &kwork->class_list, list) {
  183. type = class->type;
  184. if (!valid_kwork_class_type(type) ||
  185. (kwork_class_bpf_supported_list[type] == NULL)) {
  186. pr_err("Unsupported bpf trace class %s\n", class->name);
  187. goto out;
  188. }
  189. class_bpf = kwork_class_bpf_supported_list[type];
  190. class_bpf->class = class;
  191. if (class_bpf->load_prepare != NULL)
  192. class_bpf->load_prepare(kwork);
  193. }
  194. if (kwork->cpu_list != NULL)
  195. skel->rodata->has_cpu_filter = 1;
  196. if (kwork->profile_name != NULL)
  197. skel->rodata->has_name_filter = 1;
  198. if (kwork_trace_bpf__load(skel)) {
  199. pr_debug("Failed to load kwork trace skeleton\n");
  200. goto out;
  201. }
  202. if (setup_filters(kwork))
  203. goto out;
  204. if (kwork_trace_bpf__attach(skel)) {
  205. pr_debug("Failed to attach kwork trace skeleton\n");
  206. goto out;
  207. }
  208. return 0;
  209. out:
  210. kwork_trace_bpf__destroy(skel);
  211. return -1;
  212. }
  213. static int add_work(struct perf_kwork *kwork,
  214. struct work_key *key,
  215. struct report_data *data)
  216. {
  217. struct kwork_work *work;
  218. struct kwork_class_bpf *bpf_trace;
  219. struct kwork_work tmp = {
  220. .id = key->id,
  221. .name = NULL,
  222. .cpu = key->cpu,
  223. };
  224. enum kwork_class_type type = key->type;
  225. if (!valid_kwork_class_type(type)) {
  226. pr_debug("Invalid class type %d to add work\n", type);
  227. return -1;
  228. }
  229. bpf_trace = kwork_class_bpf_supported_list[type];
  230. tmp.class = bpf_trace->class;
  231. if ((bpf_trace->get_work_name != NULL) &&
  232. (bpf_trace->get_work_name(key, &tmp.name)))
  233. return -1;
  234. work = kwork->add_work(kwork, tmp.class, &tmp);
  235. if (work == NULL)
  236. return -1;
  237. if (kwork->report == KWORK_REPORT_RUNTIME) {
  238. work->nr_atoms = data->nr;
  239. work->total_runtime = data->total_time;
  240. work->max_runtime = data->max_time;
  241. work->max_runtime_start = data->max_time_start;
  242. work->max_runtime_end = data->max_time_end;
  243. } else if (kwork->report == KWORK_REPORT_LATENCY) {
  244. work->nr_atoms = data->nr;
  245. work->total_latency = data->total_time;
  246. work->max_latency = data->max_time;
  247. work->max_latency_start = data->max_time_start;
  248. work->max_latency_end = data->max_time_end;
  249. } else {
  250. pr_debug("Invalid bpf report type %d\n", kwork->report);
  251. return -1;
  252. }
  253. kwork->timestart = (u64)ts_start.tv_sec * NSEC_PER_SEC + ts_start.tv_nsec;
  254. kwork->timeend = (u64)ts_end.tv_sec * NSEC_PER_SEC + ts_end.tv_nsec;
  255. return 0;
  256. }
  257. int perf_kwork__report_read_bpf(struct perf_kwork *kwork)
  258. {
  259. struct report_data data;
  260. struct work_key key = {
  261. .type = 0,
  262. .cpu = 0,
  263. .id = 0,
  264. };
  265. struct work_key prev = {
  266. .type = 0,
  267. .cpu = 0,
  268. .id = 0,
  269. };
  270. int fd = bpf_map__fd(skel->maps.perf_kwork_report);
  271. if (fd < 0) {
  272. pr_debug("Invalid report fd\n");
  273. return -1;
  274. }
  275. while (!bpf_map_get_next_key(fd, &prev, &key)) {
  276. if ((bpf_map_lookup_elem(fd, &key, &data)) != 0) {
  277. pr_debug("Failed to lookup report elem\n");
  278. return -1;
  279. }
  280. if ((data.nr != 0) && (add_work(kwork, &key, &data) != 0))
  281. return -1;
  282. prev = key;
  283. }
  284. return 0;
  285. }
  286. void perf_kwork__report_cleanup_bpf(void)
  287. {
  288. kwork_trace_bpf__destroy(skel);
  289. }