openat-syscall-all-cpus.c 2.9 KB

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