perf_api_probe.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include "perf-sys.h"
  3. #include "util/cloexec.h"
  4. #include "util/evlist.h"
  5. #include "util/evsel.h"
  6. #include "util/parse-events.h"
  7. #include "util/perf_api_probe.h"
  8. #include <perf/cpumap.h>
  9. #include <errno.h>
  10. typedef void (*setup_probe_fn_t)(struct evsel *evsel);
  11. static int perf_do_probe_api(setup_probe_fn_t fn, int cpu, const char *str)
  12. {
  13. struct evlist *evlist;
  14. struct evsel *evsel;
  15. unsigned long flags = perf_event_open_cloexec_flag();
  16. int err = -EAGAIN, fd;
  17. static pid_t pid = -1;
  18. evlist = evlist__new();
  19. if (!evlist)
  20. return -ENOMEM;
  21. if (parse_events(evlist, str, NULL))
  22. goto out_delete;
  23. evsel = evlist__first(evlist);
  24. while (1) {
  25. fd = sys_perf_event_open(&evsel->core.attr, pid, cpu, -1, flags);
  26. if (fd < 0) {
  27. if (pid == -1 && errno == EACCES) {
  28. pid = 0;
  29. continue;
  30. }
  31. goto out_delete;
  32. }
  33. break;
  34. }
  35. close(fd);
  36. fn(evsel);
  37. fd = sys_perf_event_open(&evsel->core.attr, pid, cpu, -1, flags);
  38. if (fd < 0) {
  39. if (errno == EINVAL)
  40. err = -EINVAL;
  41. goto out_delete;
  42. }
  43. close(fd);
  44. err = 0;
  45. out_delete:
  46. evlist__delete(evlist);
  47. return err;
  48. }
  49. static bool perf_probe_api(setup_probe_fn_t fn)
  50. {
  51. const char *try[] = {"cycles:u", "instructions:u", "cpu-clock:u", NULL};
  52. struct perf_cpu_map *cpus;
  53. int cpu, ret, i = 0;
  54. cpus = perf_cpu_map__new(NULL);
  55. if (!cpus)
  56. return false;
  57. cpu = cpus->map[0];
  58. perf_cpu_map__put(cpus);
  59. do {
  60. ret = perf_do_probe_api(fn, cpu, try[i++]);
  61. if (!ret)
  62. return true;
  63. } while (ret == -EAGAIN && try[i]);
  64. return false;
  65. }
  66. static void perf_probe_sample_identifier(struct evsel *evsel)
  67. {
  68. evsel->core.attr.sample_type |= PERF_SAMPLE_IDENTIFIER;
  69. }
  70. static void perf_probe_comm_exec(struct evsel *evsel)
  71. {
  72. evsel->core.attr.comm_exec = 1;
  73. }
  74. static void perf_probe_context_switch(struct evsel *evsel)
  75. {
  76. evsel->core.attr.context_switch = 1;
  77. }
  78. static void perf_probe_text_poke(struct evsel *evsel)
  79. {
  80. evsel->core.attr.text_poke = 1;
  81. }
  82. bool perf_can_sample_identifier(void)
  83. {
  84. return perf_probe_api(perf_probe_sample_identifier);
  85. }
  86. bool perf_can_comm_exec(void)
  87. {
  88. return perf_probe_api(perf_probe_comm_exec);
  89. }
  90. bool perf_can_record_switch_events(void)
  91. {
  92. return perf_probe_api(perf_probe_context_switch);
  93. }
  94. bool perf_can_record_text_poke_events(void)
  95. {
  96. return perf_probe_api(perf_probe_text_poke);
  97. }
  98. bool perf_can_record_cpu_wide(void)
  99. {
  100. struct perf_event_attr attr = {
  101. .type = PERF_TYPE_SOFTWARE,
  102. .config = PERF_COUNT_SW_CPU_CLOCK,
  103. .exclude_kernel = 1,
  104. };
  105. struct perf_cpu_map *cpus;
  106. int cpu, fd;
  107. cpus = perf_cpu_map__new(NULL);
  108. if (!cpus)
  109. return false;
  110. cpu = cpus->map[0];
  111. perf_cpu_map__put(cpus);
  112. fd = sys_perf_event_open(&attr, -1, cpu, -1, 0);
  113. if (fd < 0)
  114. return false;
  115. close(fd);
  116. return true;
  117. }
  118. /*
  119. * Architectures are expected to know if AUX area sampling is supported by the
  120. * hardware. Here we check for kernel support.
  121. */
  122. bool perf_can_aux_sample(void)
  123. {
  124. struct perf_event_attr attr = {
  125. .size = sizeof(struct perf_event_attr),
  126. .exclude_kernel = 1,
  127. /*
  128. * Non-zero value causes the kernel to calculate the effective
  129. * attribute size up to that byte.
  130. */
  131. .aux_sample_size = 1,
  132. };
  133. int fd;
  134. fd = sys_perf_event_open(&attr, -1, 0, -1, 0);
  135. /*
  136. * If the kernel attribute is big enough to contain aux_sample_size
  137. * then we assume that it is supported. We are relying on the kernel to
  138. * validate the attribute size before anything else that could be wrong.
  139. */
  140. if (fd < 0 && errno == E2BIG)
  141. return false;
  142. if (fd >= 0)
  143. close(fd);
  144. return true;
  145. }