attr.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * The struct perf_event_attr test support.
  3. *
  4. * This test is embedded inside into perf directly and is governed
  5. * by the PERF_TEST_ATTR environment variable and hook inside
  6. * sys_perf_event_open function.
  7. *
  8. * The general idea is to store 'struct perf_event_attr' details for
  9. * each event created within single perf command. Each event details
  10. * are stored into separate text file. Once perf command is finished
  11. * these files can be checked for values we expect for command.
  12. *
  13. * Besides 'struct perf_event_attr' values we also store 'fd' and
  14. * 'group_fd' values to allow checking for groups created.
  15. *
  16. * This all is triggered by setting PERF_TEST_ATTR environment variable.
  17. * It must contain name of existing directory with access and write
  18. * permissions. All the event text files are stored there.
  19. */
  20. #include <debug.h>
  21. #include <errno.h>
  22. #include <inttypes.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <linux/types.h>
  26. #include <linux/kernel.h>
  27. #include <sys/param.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <unistd.h>
  31. #include "../perf.h"
  32. #include <subcmd/exec-cmd.h>
  33. #include "tests.h"
  34. #define ENV "PERF_TEST_ATTR"
  35. static char *dir;
  36. void test_attr__init(void)
  37. {
  38. dir = getenv(ENV);
  39. test_attr__enabled = (dir != NULL);
  40. }
  41. #define BUFSIZE 1024
  42. #define __WRITE_ASS(str, fmt, data) \
  43. do { \
  44. char buf[BUFSIZE]; \
  45. size_t size; \
  46. \
  47. size = snprintf(buf, BUFSIZE, #str "=%"fmt "\n", data); \
  48. if (1 != fwrite(buf, size, 1, file)) { \
  49. perror("test attr - failed to write event file"); \
  50. fclose(file); \
  51. return -1; \
  52. } \
  53. \
  54. } while (0)
  55. #define WRITE_ASS(field, fmt) __WRITE_ASS(field, fmt, attr->field)
  56. static int store_event(struct perf_event_attr *attr, pid_t pid, int cpu,
  57. int fd, int group_fd, unsigned long flags)
  58. {
  59. FILE *file;
  60. char path[PATH_MAX];
  61. snprintf(path, PATH_MAX, "%s/event-%d-%llu-%d", dir,
  62. attr->type, attr->config, fd);
  63. file = fopen(path, "w+");
  64. if (!file) {
  65. perror("test attr - failed to open event file");
  66. return -1;
  67. }
  68. if (fprintf(file, "[event-%d-%llu-%d]\n",
  69. attr->type, attr->config, fd) < 0) {
  70. perror("test attr - failed to write event file");
  71. fclose(file);
  72. return -1;
  73. }
  74. /* syscall arguments */
  75. __WRITE_ASS(fd, "d", fd);
  76. __WRITE_ASS(group_fd, "d", group_fd);
  77. __WRITE_ASS(cpu, "d", cpu);
  78. __WRITE_ASS(pid, "d", pid);
  79. __WRITE_ASS(flags, "lu", flags);
  80. /* struct perf_event_attr */
  81. WRITE_ASS(type, PRIu32);
  82. WRITE_ASS(size, PRIu32);
  83. WRITE_ASS(config, "llu");
  84. WRITE_ASS(sample_period, "llu");
  85. WRITE_ASS(sample_type, "llu");
  86. WRITE_ASS(read_format, "llu");
  87. WRITE_ASS(disabled, "d");
  88. WRITE_ASS(inherit, "d");
  89. WRITE_ASS(pinned, "d");
  90. WRITE_ASS(exclusive, "d");
  91. WRITE_ASS(exclude_user, "d");
  92. WRITE_ASS(exclude_kernel, "d");
  93. WRITE_ASS(exclude_hv, "d");
  94. WRITE_ASS(exclude_idle, "d");
  95. WRITE_ASS(mmap, "d");
  96. WRITE_ASS(comm, "d");
  97. WRITE_ASS(freq, "d");
  98. WRITE_ASS(inherit_stat, "d");
  99. WRITE_ASS(enable_on_exec, "d");
  100. WRITE_ASS(task, "d");
  101. WRITE_ASS(watermark, "d");
  102. WRITE_ASS(precise_ip, "d");
  103. WRITE_ASS(mmap_data, "d");
  104. WRITE_ASS(sample_id_all, "d");
  105. WRITE_ASS(exclude_host, "d");
  106. WRITE_ASS(exclude_guest, "d");
  107. WRITE_ASS(exclude_callchain_kernel, "d");
  108. WRITE_ASS(exclude_callchain_user, "d");
  109. WRITE_ASS(wakeup_events, PRIu32);
  110. WRITE_ASS(bp_type, PRIu32);
  111. WRITE_ASS(config1, "llu");
  112. WRITE_ASS(config2, "llu");
  113. WRITE_ASS(branch_sample_type, "llu");
  114. WRITE_ASS(sample_regs_user, "llu");
  115. WRITE_ASS(sample_stack_user, PRIu32);
  116. fclose(file);
  117. return 0;
  118. }
  119. void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
  120. int fd, int group_fd, unsigned long flags)
  121. {
  122. int errno_saved = errno;
  123. if (store_event(attr, pid, cpu, fd, group_fd, flags)) {
  124. pr_err("test attr FAILED");
  125. exit(128);
  126. }
  127. errno = errno_saved;
  128. }
  129. static int run_dir(const char *d, const char *perf)
  130. {
  131. char v[] = "-vvvvv";
  132. int vcnt = min(verbose, (int) sizeof(v) - 1);
  133. char cmd[3*PATH_MAX];
  134. if (verbose > 0)
  135. vcnt++;
  136. snprintf(cmd, 3*PATH_MAX, PYTHON " %s/attr.py -d %s/attr/ -p %s %.*s",
  137. d, d, perf, vcnt, v);
  138. return system(cmd);
  139. }
  140. int test__attr(int subtest __maybe_unused)
  141. {
  142. struct stat st;
  143. char path_perf[PATH_MAX];
  144. char path_dir[PATH_MAX];
  145. /* First try developement tree tests. */
  146. if (!lstat("./tests", &st))
  147. return run_dir("./tests", "./perf");
  148. /* Then installed path. */
  149. snprintf(path_dir, PATH_MAX, "%s/tests", get_argv_exec_path());
  150. snprintf(path_perf, PATH_MAX, "%s/perf", BINDIR);
  151. if (!lstat(path_dir, &st) &&
  152. !lstat(path_perf, &st))
  153. return run_dir(path_dir, path_perf);
  154. return TEST_SKIP;
  155. }