open-syscall-all-cpus.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "evsel.h"
  2. #include "tests.h"
  3. #include "thread_map.h"
  4. #include "cpumap.h"
  5. #include "debug.h"
  6. int test__open_syscall_event_on_all_cpus(void)
  7. {
  8. int err = -1, fd, cpu;
  9. struct thread_map *threads;
  10. struct cpu_map *cpus;
  11. struct perf_evsel *evsel;
  12. struct perf_event_attr attr;
  13. unsigned int nr_open_calls = 111, i;
  14. cpu_set_t cpu_set;
  15. int id = trace_event__id("sys_enter_open");
  16. if (id < 0) {
  17. pr_debug("is debugfs mounted on /sys/kernel/debug?\n");
  18. return -1;
  19. }
  20. threads = thread_map__new(-1, getpid(), UINT_MAX);
  21. if (threads == NULL) {
  22. pr_debug("thread_map__new\n");
  23. return -1;
  24. }
  25. cpus = cpu_map__new(NULL);
  26. if (cpus == NULL) {
  27. pr_debug("cpu_map__new\n");
  28. goto out_thread_map_delete;
  29. }
  30. CPU_ZERO(&cpu_set);
  31. memset(&attr, 0, sizeof(attr));
  32. attr.type = PERF_TYPE_TRACEPOINT;
  33. attr.config = id;
  34. evsel = perf_evsel__new(&attr, 0);
  35. if (evsel == NULL) {
  36. pr_debug("perf_evsel__new\n");
  37. goto out_thread_map_delete;
  38. }
  39. if (perf_evsel__open(evsel, cpus, threads) < 0) {
  40. pr_debug("failed to open counter: %s, "
  41. "tweak /proc/sys/kernel/perf_event_paranoid?\n",
  42. strerror(errno));
  43. goto out_evsel_delete;
  44. }
  45. for (cpu = 0; cpu < cpus->nr; ++cpu) {
  46. unsigned int ncalls = nr_open_calls + cpu;
  47. /*
  48. * XXX eventually lift this restriction in a way that
  49. * keeps perf building on older glibc installations
  50. * without CPU_ALLOC. 1024 cpus in 2010 still seems
  51. * a reasonable upper limit tho :-)
  52. */
  53. if (cpus->map[cpu] >= CPU_SETSIZE) {
  54. pr_debug("Ignoring CPU %d\n", cpus->map[cpu]);
  55. continue;
  56. }
  57. CPU_SET(cpus->map[cpu], &cpu_set);
  58. if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
  59. pr_debug("sched_setaffinity() failed on CPU %d: %s ",
  60. cpus->map[cpu],
  61. strerror(errno));
  62. goto out_close_fd;
  63. }
  64. for (i = 0; i < ncalls; ++i) {
  65. fd = open("/etc/passwd", O_RDONLY);
  66. close(fd);
  67. }
  68. CPU_CLR(cpus->map[cpu], &cpu_set);
  69. }
  70. /*
  71. * Here we need to explicitely preallocate the counts, as if
  72. * we use the auto allocation it will allocate just for 1 cpu,
  73. * as we start by cpu 0.
  74. */
  75. if (perf_evsel__alloc_counts(evsel, cpus->nr) < 0) {
  76. pr_debug("perf_evsel__alloc_counts(ncpus=%d)\n", cpus->nr);
  77. goto out_close_fd;
  78. }
  79. err = 0;
  80. for (cpu = 0; cpu < cpus->nr; ++cpu) {
  81. unsigned int expected;
  82. if (cpus->map[cpu] >= CPU_SETSIZE)
  83. continue;
  84. if (perf_evsel__read_on_cpu(evsel, cpu, 0) < 0) {
  85. pr_debug("perf_evsel__read_on_cpu\n");
  86. err = -1;
  87. break;
  88. }
  89. expected = nr_open_calls + cpu;
  90. if (evsel->counts->cpu[cpu].val != expected) {
  91. pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n",
  92. expected, cpus->map[cpu], evsel->counts->cpu[cpu].val);
  93. err = -1;
  94. }
  95. }
  96. out_close_fd:
  97. perf_evsel__close_fd(evsel, 1, threads->nr);
  98. out_evsel_delete:
  99. perf_evsel__delete(evsel);
  100. out_thread_map_delete:
  101. thread_map__delete(threads);
  102. return err;
  103. }