attr.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #include "pmus.h"
  37. #define ENV "PERF_TEST_ATTR"
  38. static char *dir;
  39. static bool ready;
  40. void test_attr__init(void)
  41. {
  42. dir = getenv(ENV);
  43. test_attr__enabled = (dir != NULL);
  44. }
  45. #define BUFSIZE 1024
  46. #define __WRITE_ASS(str, fmt, data) \
  47. do { \
  48. char buf[BUFSIZE]; \
  49. size_t size; \
  50. \
  51. size = snprintf(buf, BUFSIZE, #str "=%"fmt "\n", data); \
  52. if (1 != fwrite(buf, size, 1, file)) { \
  53. perror("test attr - failed to write event file"); \
  54. fclose(file); \
  55. return -1; \
  56. } \
  57. \
  58. } while (0)
  59. #define WRITE_ASS(field, fmt) __WRITE_ASS(field, fmt, attr->field)
  60. static int store_event(struct perf_event_attr *attr, pid_t pid, struct perf_cpu cpu,
  61. int fd, int group_fd, unsigned long flags)
  62. {
  63. FILE *file;
  64. char path[PATH_MAX];
  65. if (!ready)
  66. return 0;
  67. snprintf(path, PATH_MAX, "%s/event-%d-%llu-%d", dir,
  68. attr->type, attr->config, fd);
  69. file = fopen(path, "w+");
  70. if (!file) {
  71. perror("test attr - failed to open event file");
  72. return -1;
  73. }
  74. if (fprintf(file, "[event-%d-%llu-%d]\n",
  75. attr->type, attr->config, fd) < 0) {
  76. perror("test attr - failed to write event file");
  77. fclose(file);
  78. return -1;
  79. }
  80. /* syscall arguments */
  81. __WRITE_ASS(fd, "d", fd);
  82. __WRITE_ASS(group_fd, "d", group_fd);
  83. __WRITE_ASS(cpu, "d", cpu.cpu);
  84. __WRITE_ASS(pid, "d", pid);
  85. __WRITE_ASS(flags, "lu", flags);
  86. /* struct perf_event_attr */
  87. WRITE_ASS(type, PRIu32);
  88. WRITE_ASS(size, PRIu32);
  89. WRITE_ASS(config, "llu");
  90. WRITE_ASS(sample_period, "llu");
  91. WRITE_ASS(sample_type, "llu");
  92. WRITE_ASS(read_format, "llu");
  93. WRITE_ASS(disabled, "d");
  94. WRITE_ASS(inherit, "d");
  95. WRITE_ASS(pinned, "d");
  96. WRITE_ASS(exclusive, "d");
  97. WRITE_ASS(exclude_user, "d");
  98. WRITE_ASS(exclude_kernel, "d");
  99. WRITE_ASS(exclude_hv, "d");
  100. WRITE_ASS(exclude_idle, "d");
  101. WRITE_ASS(mmap, "d");
  102. WRITE_ASS(comm, "d");
  103. WRITE_ASS(freq, "d");
  104. WRITE_ASS(inherit_stat, "d");
  105. WRITE_ASS(enable_on_exec, "d");
  106. WRITE_ASS(task, "d");
  107. WRITE_ASS(watermark, "d");
  108. WRITE_ASS(precise_ip, "d");
  109. WRITE_ASS(mmap_data, "d");
  110. WRITE_ASS(sample_id_all, "d");
  111. WRITE_ASS(exclude_host, "d");
  112. WRITE_ASS(exclude_guest, "d");
  113. WRITE_ASS(exclude_callchain_kernel, "d");
  114. WRITE_ASS(exclude_callchain_user, "d");
  115. WRITE_ASS(mmap2, "d");
  116. WRITE_ASS(comm_exec, "d");
  117. WRITE_ASS(context_switch, "d");
  118. WRITE_ASS(write_backward, "d");
  119. WRITE_ASS(namespaces, "d");
  120. WRITE_ASS(use_clockid, "d");
  121. WRITE_ASS(wakeup_events, PRIu32);
  122. WRITE_ASS(bp_type, PRIu32);
  123. WRITE_ASS(config1, "llu");
  124. WRITE_ASS(config2, "llu");
  125. WRITE_ASS(branch_sample_type, "llu");
  126. WRITE_ASS(sample_regs_user, "llu");
  127. WRITE_ASS(sample_stack_user, PRIu32);
  128. fclose(file);
  129. return 0;
  130. }
  131. void test_attr__open(struct perf_event_attr *attr, pid_t pid, struct perf_cpu cpu,
  132. int fd, int group_fd, unsigned long flags)
  133. {
  134. int errno_saved = errno;
  135. if ((fd != -1) && store_event(attr, pid, cpu, fd, group_fd, flags)) {
  136. pr_err("test attr FAILED");
  137. exit(128);
  138. }
  139. errno = errno_saved;
  140. }
  141. void test_attr__ready(void)
  142. {
  143. if (unlikely(test_attr__enabled) && !ready)
  144. ready = true;
  145. }
  146. static int run_dir(const char *d, const char *perf)
  147. {
  148. char v[] = "-vvvvv";
  149. int vcnt = min(verbose, (int) sizeof(v) - 1);
  150. char cmd[3*PATH_MAX];
  151. if (verbose > 0)
  152. vcnt++;
  153. scnprintf(cmd, 3*PATH_MAX, PYTHON " %s/attr.py -d %s/attr/ -p %s %.*s",
  154. d, d, perf, vcnt, v);
  155. return system(cmd) ? TEST_FAIL : TEST_OK;
  156. }
  157. static int test__attr(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
  158. {
  159. struct stat st;
  160. char path_perf[PATH_MAX];
  161. char path_dir[PATH_MAX];
  162. char *exec_path;
  163. if (perf_pmus__num_core_pmus() > 1) {
  164. /*
  165. * TODO: Attribute tests hard code the PMU type. If there are >1
  166. * core PMU then each PMU will have a different type which
  167. * requires additional support.
  168. */
  169. pr_debug("Skip test on hybrid systems");
  170. return TEST_SKIP;
  171. }
  172. /* First try development tree tests. */
  173. if (!lstat("./tests", &st))
  174. return run_dir("./tests", "./perf");
  175. exec_path = get_argv_exec_path();
  176. if (exec_path == NULL)
  177. return -1;
  178. /* Then installed path. */
  179. snprintf(path_dir, PATH_MAX, "%s/tests", exec_path);
  180. snprintf(path_perf, PATH_MAX, "%s/perf", BINDIR);
  181. free(exec_path);
  182. if (!lstat(path_dir, &st) &&
  183. !lstat(path_perf, &st))
  184. return run_dir(path_dir, path_perf);
  185. return TEST_SKIP;
  186. }
  187. DEFINE_SUITE("Setup struct perf_event_attr", attr);