attr.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 <stdlib.h>
  21. #include <stdio.h>
  22. #include <inttypes.h>
  23. #include <linux/types.h>
  24. #include <linux/kernel.h>
  25. #include "../perf.h"
  26. #include "util.h"
  27. #include "exec_cmd.h"
  28. #define ENV "PERF_TEST_ATTR"
  29. extern int verbose;
  30. bool test_attr__enabled;
  31. static char *dir;
  32. void test_attr__init(void)
  33. {
  34. dir = getenv(ENV);
  35. test_attr__enabled = (dir != NULL);
  36. }
  37. #define BUFSIZE 1024
  38. #define WRITE_ASS(str, fmt, data) \
  39. do { \
  40. char buf[BUFSIZE]; \
  41. size_t size; \
  42. \
  43. size = snprintf(buf, BUFSIZE, #str "=%"fmt "\n", data); \
  44. if (1 != fwrite(buf, size, 1, file)) { \
  45. perror("test attr - failed to write event file"); \
  46. fclose(file); \
  47. return -1; \
  48. } \
  49. \
  50. } while (0)
  51. static int store_event(struct perf_event_attr *attr, pid_t pid, int cpu,
  52. int fd, int group_fd, unsigned long flags)
  53. {
  54. FILE *file;
  55. char path[PATH_MAX];
  56. snprintf(path, PATH_MAX, "%s/event-%d-%llu-%d", dir,
  57. attr->type, attr->config, fd);
  58. file = fopen(path, "w+");
  59. if (!file) {
  60. perror("test attr - failed to open event file");
  61. return -1;
  62. }
  63. if (fprintf(file, "[event-%d-%llu-%d]\n",
  64. attr->type, attr->config, fd) < 0) {
  65. perror("test attr - failed to write event file");
  66. fclose(file);
  67. return -1;
  68. }
  69. /* syscall arguments */
  70. WRITE_ASS(fd, "d", fd);
  71. WRITE_ASS(group_fd, "d", group_fd);
  72. WRITE_ASS(cpu, "d", cpu);
  73. WRITE_ASS(pid, "d", pid);
  74. WRITE_ASS(flags, "lu", flags);
  75. /* struct perf_event_attr */
  76. WRITE_ASS(type, PRIu32, attr->type);
  77. WRITE_ASS(size, PRIu32, attr->size);
  78. WRITE_ASS(config, "llu", attr->config);
  79. WRITE_ASS(sample_period, "llu", attr->sample_period);
  80. WRITE_ASS(sample_type, "llu", attr->sample_type);
  81. WRITE_ASS(read_format, "llu", attr->read_format);
  82. WRITE_ASS(disabled, "d", attr->disabled);
  83. WRITE_ASS(inherit, "d", attr->inherit);
  84. WRITE_ASS(pinned, "d", attr->pinned);
  85. WRITE_ASS(exclusive, "d", attr->exclusive);
  86. WRITE_ASS(exclude_user, "d", attr->exclude_user);
  87. WRITE_ASS(exclude_kernel, "d", attr->exclude_kernel);
  88. WRITE_ASS(exclude_hv, "d", attr->exclude_hv);
  89. WRITE_ASS(exclude_idle, "d", attr->exclude_idle);
  90. WRITE_ASS(mmap, "d", attr->mmap);
  91. WRITE_ASS(comm, "d", attr->comm);
  92. WRITE_ASS(freq, "d", attr->freq);
  93. WRITE_ASS(inherit_stat, "d", attr->inherit_stat);
  94. WRITE_ASS(enable_on_exec, "d", attr->enable_on_exec);
  95. WRITE_ASS(task, "d", attr->task);
  96. WRITE_ASS(watermask, "d", attr->watermark);
  97. WRITE_ASS(precise_ip, "d", attr->precise_ip);
  98. WRITE_ASS(mmap_data, "d", attr->mmap_data);
  99. WRITE_ASS(sample_id_all, "d", attr->sample_id_all);
  100. WRITE_ASS(exclude_host, "d", attr->exclude_host);
  101. WRITE_ASS(exclude_guest, "d", attr->exclude_guest);
  102. WRITE_ASS(exclude_callchain_kernel, "d",
  103. attr->exclude_callchain_kernel);
  104. WRITE_ASS(exclude_callchain_user, "d",
  105. attr->exclude_callchain_user);
  106. WRITE_ASS(wakeup_events, PRIu32, attr->wakeup_events);
  107. WRITE_ASS(bp_type, PRIu32, attr->bp_type);
  108. WRITE_ASS(config1, "llu", attr->config1);
  109. WRITE_ASS(config2, "llu", attr->config2);
  110. WRITE_ASS(branch_sample_type, "llu", attr->branch_sample_type);
  111. WRITE_ASS(sample_regs_user, "llu", attr->sample_regs_user);
  112. WRITE_ASS(sample_stack_user, PRIu32, attr->sample_stack_user);
  113. WRITE_ASS(optional, "d", 0);
  114. fclose(file);
  115. return 0;
  116. }
  117. void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
  118. int fd, int group_fd, unsigned long flags)
  119. {
  120. int errno_saved = errno;
  121. if (store_event(attr, pid, cpu, fd, group_fd, flags))
  122. die("test attr FAILED");
  123. errno = errno_saved;
  124. }
  125. static int run_dir(const char *d, const char *perf)
  126. {
  127. char cmd[3*PATH_MAX];
  128. snprintf(cmd, 3*PATH_MAX, "python %s/attr.py -d %s/attr/ -p %s %s",
  129. d, d, perf, verbose ? "-v" : "");
  130. return system(cmd);
  131. }
  132. int test_attr__run(void)
  133. {
  134. struct stat st;
  135. char path_perf[PATH_MAX];
  136. char path_dir[PATH_MAX];
  137. /* First try developement tree tests. */
  138. if (!lstat("./tests", &st))
  139. return run_dir("./tests", "./perf");
  140. /* Then installed path. */
  141. snprintf(path_dir, PATH_MAX, "%s/tests", perf_exec_path());
  142. snprintf(path_perf, PATH_MAX, "%s/perf", BINDIR);
  143. if (!lstat(path_dir, &st) &&
  144. !lstat(path_perf, &st))
  145. return run_dir(path_dir, path_perf);
  146. fprintf(stderr, " (ommitted)");
  147. return 0;
  148. }