openat-syscall-all-cpus.c 3.0 KB

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