attr.c 5.1 KB

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