record.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "evlist.h"
  2. #include "evsel.h"
  3. #include "cpumap.h"
  4. #include "parse-events.h"
  5. typedef void (*setup_probe_fn_t)(struct perf_evsel *evsel);
  6. static int perf_do_probe_api(setup_probe_fn_t fn, int cpu, const char *str)
  7. {
  8. struct perf_evlist *evlist;
  9. struct perf_evsel *evsel;
  10. int err = -EAGAIN, fd;
  11. evlist = perf_evlist__new();
  12. if (!evlist)
  13. return -ENOMEM;
  14. if (parse_events(evlist, str))
  15. goto out_delete;
  16. evsel = perf_evlist__first(evlist);
  17. fd = sys_perf_event_open(&evsel->attr, -1, cpu, -1, 0);
  18. if (fd < 0)
  19. goto out_delete;
  20. close(fd);
  21. fn(evsel);
  22. fd = sys_perf_event_open(&evsel->attr, -1, cpu, -1, 0);
  23. if (fd < 0) {
  24. if (errno == EINVAL)
  25. err = -EINVAL;
  26. goto out_delete;
  27. }
  28. close(fd);
  29. err = 0;
  30. out_delete:
  31. perf_evlist__delete(evlist);
  32. return err;
  33. }
  34. static bool perf_probe_api(setup_probe_fn_t fn)
  35. {
  36. const char *try[] = {"cycles:u", "instructions:u", "cpu-clock", NULL};
  37. struct cpu_map *cpus;
  38. int cpu, ret, i = 0;
  39. cpus = cpu_map__new(NULL);
  40. if (!cpus)
  41. return false;
  42. cpu = cpus->map[0];
  43. cpu_map__delete(cpus);
  44. do {
  45. ret = perf_do_probe_api(fn, cpu, try[i++]);
  46. if (!ret)
  47. return true;
  48. } while (ret == -EAGAIN && try[i]);
  49. return false;
  50. }
  51. static void perf_probe_sample_identifier(struct perf_evsel *evsel)
  52. {
  53. evsel->attr.sample_type |= PERF_SAMPLE_IDENTIFIER;
  54. }
  55. bool perf_can_sample_identifier(void)
  56. {
  57. return perf_probe_api(perf_probe_sample_identifier);
  58. }
  59. void perf_evlist__config(struct perf_evlist *evlist,
  60. struct perf_record_opts *opts)
  61. {
  62. struct perf_evsel *evsel;
  63. bool use_sample_identifier = false;
  64. /*
  65. * Set the evsel leader links before we configure attributes,
  66. * since some might depend on this info.
  67. */
  68. if (opts->group)
  69. perf_evlist__set_leader(evlist);
  70. if (evlist->cpus->map[0] < 0)
  71. opts->no_inherit = true;
  72. list_for_each_entry(evsel, &evlist->entries, node)
  73. perf_evsel__config(evsel, opts);
  74. if (evlist->nr_entries > 1) {
  75. struct perf_evsel *first = perf_evlist__first(evlist);
  76. list_for_each_entry(evsel, &evlist->entries, node) {
  77. if (evsel->attr.sample_type == first->attr.sample_type)
  78. continue;
  79. use_sample_identifier = perf_can_sample_identifier();
  80. break;
  81. }
  82. list_for_each_entry(evsel, &evlist->entries, node)
  83. perf_evsel__set_sample_id(evsel, use_sample_identifier);
  84. }
  85. perf_evlist__set_id_pos(evlist);
  86. }