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