record.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "evlist.h"
  3. #include "evsel.h"
  4. #include "cpumap.h"
  5. #include "parse-events.h"
  6. #include <errno.h>
  7. #include <api/fs/fs.h>
  8. #include <subcmd/parse-options.h>
  9. #include "util.h"
  10. #include "cloexec.h"
  11. typedef void (*setup_probe_fn_t)(struct perf_evsel *evsel);
  12. static int perf_do_probe_api(setup_probe_fn_t fn, int cpu, const char *str)
  13. {
  14. struct perf_evlist *evlist;
  15. struct perf_evsel *evsel;
  16. unsigned long flags = perf_event_open_cloexec_flag();
  17. int err = -EAGAIN, fd;
  18. static pid_t pid = -1;
  19. evlist = perf_evlist__new();
  20. if (!evlist)
  21. return -ENOMEM;
  22. if (parse_events(evlist, str, NULL))
  23. goto out_delete;
  24. evsel = perf_evlist__first(evlist);
  25. while (1) {
  26. fd = sys_perf_event_open(&evsel->attr, pid, cpu, -1, flags);
  27. if (fd < 0) {
  28. if (pid == -1 && errno == EACCES) {
  29. pid = 0;
  30. continue;
  31. }
  32. goto out_delete;
  33. }
  34. break;
  35. }
  36. close(fd);
  37. fn(evsel);
  38. fd = sys_perf_event_open(&evsel->attr, pid, cpu, -1, flags);
  39. if (fd < 0) {
  40. if (errno == EINVAL)
  41. err = -EINVAL;
  42. goto out_delete;
  43. }
  44. close(fd);
  45. err = 0;
  46. out_delete:
  47. perf_evlist__delete(evlist);
  48. return err;
  49. }
  50. static bool perf_probe_api(setup_probe_fn_t fn)
  51. {
  52. const char *try[] = {"cycles:u", "instructions:u", "cpu-clock:u", NULL};
  53. struct cpu_map *cpus;
  54. int cpu, ret, i = 0;
  55. cpus = cpu_map__new(NULL);
  56. if (!cpus)
  57. return false;
  58. cpu = cpus->map[0];
  59. cpu_map__put(cpus);
  60. do {
  61. ret = perf_do_probe_api(fn, cpu, try[i++]);
  62. if (!ret)
  63. return true;
  64. } while (ret == -EAGAIN && try[i]);
  65. return false;
  66. }
  67. static void perf_probe_sample_identifier(struct perf_evsel *evsel)
  68. {
  69. evsel->attr.sample_type |= PERF_SAMPLE_IDENTIFIER;
  70. }
  71. static void perf_probe_comm_exec(struct perf_evsel *evsel)
  72. {
  73. evsel->attr.comm_exec = 1;
  74. }
  75. static void perf_probe_context_switch(struct perf_evsel *evsel)
  76. {
  77. evsel->attr.context_switch = 1;
  78. }
  79. bool perf_can_sample_identifier(void)
  80. {
  81. return perf_probe_api(perf_probe_sample_identifier);
  82. }
  83. static bool perf_can_comm_exec(void)
  84. {
  85. return perf_probe_api(perf_probe_comm_exec);
  86. }
  87. bool perf_can_record_switch_events(void)
  88. {
  89. return perf_probe_api(perf_probe_context_switch);
  90. }
  91. bool perf_can_record_cpu_wide(void)
  92. {
  93. struct perf_event_attr attr = {
  94. .type = PERF_TYPE_SOFTWARE,
  95. .config = PERF_COUNT_SW_CPU_CLOCK,
  96. .exclude_kernel = 1,
  97. };
  98. struct cpu_map *cpus;
  99. int cpu, fd;
  100. cpus = cpu_map__new(NULL);
  101. if (!cpus)
  102. return false;
  103. cpu = cpus->map[0];
  104. cpu_map__put(cpus);
  105. fd = sys_perf_event_open(&attr, -1, cpu, -1, 0);
  106. if (fd < 0)
  107. return false;
  108. close(fd);
  109. return true;
  110. }
  111. void perf_evlist__config(struct perf_evlist *evlist, struct record_opts *opts,
  112. struct callchain_param *callchain)
  113. {
  114. struct perf_evsel *evsel;
  115. bool use_sample_identifier = false;
  116. bool use_comm_exec;
  117. bool sample_id = opts->sample_id;
  118. /*
  119. * Set the evsel leader links before we configure attributes,
  120. * since some might depend on this info.
  121. */
  122. if (opts->group)
  123. perf_evlist__set_leader(evlist);
  124. if (evlist->cpus->map[0] < 0)
  125. opts->no_inherit = true;
  126. use_comm_exec = perf_can_comm_exec();
  127. evlist__for_each_entry(evlist, evsel) {
  128. perf_evsel__config(evsel, opts, callchain);
  129. if (evsel->tracking && use_comm_exec)
  130. evsel->attr.comm_exec = 1;
  131. }
  132. if (opts->full_auxtrace) {
  133. /*
  134. * Need to be able to synthesize and parse selected events with
  135. * arbitrary sample types, which requires always being able to
  136. * match the id.
  137. */
  138. use_sample_identifier = perf_can_sample_identifier();
  139. sample_id = true;
  140. } else if (evlist->nr_entries > 1) {
  141. struct perf_evsel *first = perf_evlist__first(evlist);
  142. evlist__for_each_entry(evlist, evsel) {
  143. if (evsel->attr.sample_type == first->attr.sample_type)
  144. continue;
  145. use_sample_identifier = perf_can_sample_identifier();
  146. break;
  147. }
  148. sample_id = true;
  149. }
  150. if (sample_id) {
  151. evlist__for_each_entry(evlist, evsel)
  152. perf_evsel__set_sample_id(evsel, use_sample_identifier);
  153. }
  154. perf_evlist__set_id_pos(evlist);
  155. }
  156. static int get_max_rate(unsigned int *rate)
  157. {
  158. return sysctl__read_int("kernel/perf_event_max_sample_rate", (int *)rate);
  159. }
  160. static int record_opts__config_freq(struct record_opts *opts)
  161. {
  162. bool user_freq = opts->user_freq != UINT_MAX;
  163. unsigned int max_rate;
  164. if (opts->user_interval != ULLONG_MAX)
  165. opts->default_interval = opts->user_interval;
  166. if (user_freq)
  167. opts->freq = opts->user_freq;
  168. /*
  169. * User specified count overrides default frequency.
  170. */
  171. if (opts->default_interval)
  172. opts->freq = 0;
  173. else if (opts->freq) {
  174. opts->default_interval = opts->freq;
  175. } else {
  176. pr_err("frequency and count are zero, aborting\n");
  177. return -1;
  178. }
  179. if (get_max_rate(&max_rate))
  180. return 0;
  181. /*
  182. * User specified frequency is over current maximum.
  183. */
  184. if (user_freq && (max_rate < opts->freq)) {
  185. if (opts->strict_freq) {
  186. pr_err("error: Maximum frequency rate (%'u Hz) exceeded.\n"
  187. " Please use -F freq option with a lower value or consider\n"
  188. " tweaking /proc/sys/kernel/perf_event_max_sample_rate.\n",
  189. max_rate);
  190. return -1;
  191. } else {
  192. pr_warning("warning: Maximum frequency rate (%'u Hz) exceeded, throttling from %'u Hz to %'u Hz.\n"
  193. " The limit can be raised via /proc/sys/kernel/perf_event_max_sample_rate.\n"
  194. " The kernel will lower it when perf's interrupts take too long.\n"
  195. " Use --strict-freq to disable this throttling, refusing to record.\n",
  196. max_rate, opts->freq, max_rate);
  197. opts->freq = max_rate;
  198. }
  199. }
  200. /*
  201. * Default frequency is over current maximum.
  202. */
  203. if (max_rate < opts->freq) {
  204. pr_warning("Lowering default frequency rate to %u.\n"
  205. "Please consider tweaking "
  206. "/proc/sys/kernel/perf_event_max_sample_rate.\n",
  207. max_rate);
  208. opts->freq = max_rate;
  209. }
  210. return 0;
  211. }
  212. int record_opts__config(struct record_opts *opts)
  213. {
  214. return record_opts__config_freq(opts);
  215. }
  216. bool perf_evlist__can_select_event(struct perf_evlist *evlist, const char *str)
  217. {
  218. struct perf_evlist *temp_evlist;
  219. struct perf_evsel *evsel;
  220. int err, fd, cpu;
  221. bool ret = false;
  222. pid_t pid = -1;
  223. temp_evlist = perf_evlist__new();
  224. if (!temp_evlist)
  225. return false;
  226. err = parse_events(temp_evlist, str, NULL);
  227. if (err)
  228. goto out_delete;
  229. evsel = perf_evlist__last(temp_evlist);
  230. if (!evlist || cpu_map__empty(evlist->cpus)) {
  231. struct cpu_map *cpus = cpu_map__new(NULL);
  232. cpu = cpus ? cpus->map[0] : 0;
  233. cpu_map__put(cpus);
  234. } else {
  235. cpu = evlist->cpus->map[0];
  236. }
  237. while (1) {
  238. fd = sys_perf_event_open(&evsel->attr, pid, cpu, -1,
  239. perf_event_open_cloexec_flag());
  240. if (fd < 0) {
  241. if (pid == -1 && errno == EACCES) {
  242. pid = 0;
  243. continue;
  244. }
  245. goto out_delete;
  246. }
  247. break;
  248. }
  249. close(fd);
  250. ret = true;
  251. out_delete:
  252. perf_evlist__delete(temp_evlist);
  253. return ret;
  254. }
  255. int record__parse_freq(const struct option *opt, const char *str, int unset __maybe_unused)
  256. {
  257. unsigned int freq;
  258. struct record_opts *opts = opt->value;
  259. if (!str)
  260. return -EINVAL;
  261. if (strcasecmp(str, "max") == 0) {
  262. if (get_max_rate(&freq)) {
  263. pr_err("couldn't read /proc/sys/kernel/perf_event_max_sample_rate\n");
  264. return -1;
  265. }
  266. pr_info("info: Using a maximum frequency rate of %'d Hz\n", freq);
  267. } else {
  268. freq = atoi(str);
  269. }
  270. opts->user_freq = freq;
  271. return 0;
  272. }