attr.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #define ENV "PERF_TEST_ATTR"
  28. bool test_attr__enabled;
  29. static char *dir;
  30. void test_attr__init(void)
  31. {
  32. dir = getenv(ENV);
  33. test_attr__enabled = (dir != NULL);
  34. }
  35. #define BUFSIZE 1024
  36. #define WRITE_ASS(str, fmt, data) \
  37. do { \
  38. char buf[BUFSIZE]; \
  39. size_t size; \
  40. \
  41. size = snprintf(buf, BUFSIZE, #str "=%"fmt "\n", data); \
  42. if (1 != fwrite(buf, size, 1, file)) { \
  43. perror("test attr - failed to write event file"); \
  44. fclose(file); \
  45. return -1; \
  46. } \
  47. \
  48. } while (0)
  49. static int store_event(struct perf_event_attr *attr, pid_t pid, int cpu,
  50. int fd, int group_fd, unsigned long flags)
  51. {
  52. FILE *file;
  53. char path[PATH_MAX];
  54. snprintf(path, PATH_MAX, "%s/event-%d-%llu-%d", dir,
  55. attr->type, attr->config, fd);
  56. file = fopen(path, "w+");
  57. if (!file) {
  58. perror("test attr - failed to open event file");
  59. return -1;
  60. }
  61. if (fprintf(file, "[event-%d-%llu-%d]\n",
  62. attr->type, attr->config, fd) < 0) {
  63. perror("test attr - failed to write event file");
  64. fclose(file);
  65. return -1;
  66. }
  67. /* syscall arguments */
  68. WRITE_ASS(fd, "d", fd);
  69. WRITE_ASS(group_fd, "d", group_fd);
  70. WRITE_ASS(cpu, "d", cpu);
  71. WRITE_ASS(pid, "d", pid);
  72. WRITE_ASS(flags, "lu", flags);
  73. /* struct perf_event_attr */
  74. WRITE_ASS(type, PRIu32, attr->type);
  75. WRITE_ASS(size, PRIu32, attr->size);
  76. WRITE_ASS(config, "llu", attr->config);
  77. WRITE_ASS(sample_period, "llu", attr->sample_period);
  78. WRITE_ASS(sample_type, "llu", attr->sample_type);
  79. WRITE_ASS(read_format, "llu", attr->read_format);
  80. WRITE_ASS(disabled, "d", attr->disabled);
  81. WRITE_ASS(inherit, "d", attr->inherit);
  82. WRITE_ASS(pinned, "d", attr->pinned);
  83. WRITE_ASS(exclusive, "d", attr->exclusive);
  84. WRITE_ASS(exclude_user, "d", attr->exclude_user);
  85. WRITE_ASS(exclude_kernel, "d", attr->exclude_kernel);
  86. WRITE_ASS(exclude_hv, "d", attr->exclude_hv);
  87. WRITE_ASS(exclude_idle, "d", attr->exclude_idle);
  88. WRITE_ASS(mmap, "d", attr->mmap);
  89. WRITE_ASS(comm, "d", attr->comm);
  90. WRITE_ASS(freq, "d", attr->freq);
  91. WRITE_ASS(inherit_stat, "d", attr->inherit_stat);
  92. WRITE_ASS(enable_on_exec, "d", attr->enable_on_exec);
  93. WRITE_ASS(task, "d", attr->task);
  94. WRITE_ASS(watermask, "d", attr->watermark);
  95. WRITE_ASS(precise_ip, "d", attr->precise_ip);
  96. WRITE_ASS(mmap_data, "d", attr->mmap_data);
  97. WRITE_ASS(sample_id_all, "d", attr->sample_id_all);
  98. WRITE_ASS(exclude_host, "d", attr->exclude_host);
  99. WRITE_ASS(exclude_guest, "d", attr->exclude_guest);
  100. WRITE_ASS(exclude_callchain_kernel, "d",
  101. attr->exclude_callchain_kernel);
  102. WRITE_ASS(exclude_callchain_user, "d",
  103. attr->exclude_callchain_user);
  104. WRITE_ASS(wakeup_events, PRIu32, attr->wakeup_events);
  105. WRITE_ASS(bp_type, PRIu32, attr->bp_type);
  106. WRITE_ASS(config1, "llu", attr->config1);
  107. WRITE_ASS(config2, "llu", attr->config2);
  108. WRITE_ASS(branch_sample_type, "llu", attr->branch_sample_type);
  109. WRITE_ASS(sample_regs_user, "llu", attr->sample_regs_user);
  110. WRITE_ASS(sample_stack_user, PRIu32, attr->sample_stack_user);
  111. WRITE_ASS(optional, "d", 0);
  112. fclose(file);
  113. return 0;
  114. }
  115. void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
  116. int fd, int group_fd, unsigned long flags)
  117. {
  118. int errno_saved = errno;
  119. if (store_event(attr, pid, cpu, fd, group_fd, flags))
  120. die("test attr FAILED");
  121. errno = errno_saved;
  122. }