open-syscall-all-cpus.c 2.7 KB

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