pmu.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "parse-events.h"
  3. #include "pmu.h"
  4. #include "util.h"
  5. #include "tests.h"
  6. #include <errno.h>
  7. #include <linux/kernel.h>
  8. /* Simulated format definitions. */
  9. static struct test_format {
  10. const char *name;
  11. const char *value;
  12. } test_formats[] = {
  13. { "krava01", "config:0-1,62-63\n", },
  14. { "krava02", "config:10-17\n", },
  15. { "krava03", "config:5\n", },
  16. { "krava11", "config1:0,2,4,6,8,20-28\n", },
  17. { "krava12", "config1:63\n", },
  18. { "krava13", "config1:45-47\n", },
  19. { "krava21", "config2:0-3,10-13,20-23,30-33,40-43,50-53,60-63\n", },
  20. { "krava22", "config2:8,18,48,58\n", },
  21. { "krava23", "config2:28-29,38\n", },
  22. };
  23. /* Simulated users input. */
  24. static struct parse_events_term test_terms[] = {
  25. {
  26. .config = (char *) "krava01",
  27. .val.num = 15,
  28. .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
  29. .type_term = PARSE_EVENTS__TERM_TYPE_USER,
  30. },
  31. {
  32. .config = (char *) "krava02",
  33. .val.num = 170,
  34. .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
  35. .type_term = PARSE_EVENTS__TERM_TYPE_USER,
  36. },
  37. {
  38. .config = (char *) "krava03",
  39. .val.num = 1,
  40. .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
  41. .type_term = PARSE_EVENTS__TERM_TYPE_USER,
  42. },
  43. {
  44. .config = (char *) "krava11",
  45. .val.num = 27,
  46. .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
  47. .type_term = PARSE_EVENTS__TERM_TYPE_USER,
  48. },
  49. {
  50. .config = (char *) "krava12",
  51. .val.num = 1,
  52. .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
  53. .type_term = PARSE_EVENTS__TERM_TYPE_USER,
  54. },
  55. {
  56. .config = (char *) "krava13",
  57. .val.num = 2,
  58. .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
  59. .type_term = PARSE_EVENTS__TERM_TYPE_USER,
  60. },
  61. {
  62. .config = (char *) "krava21",
  63. .val.num = 119,
  64. .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
  65. .type_term = PARSE_EVENTS__TERM_TYPE_USER,
  66. },
  67. {
  68. .config = (char *) "krava22",
  69. .val.num = 11,
  70. .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
  71. .type_term = PARSE_EVENTS__TERM_TYPE_USER,
  72. },
  73. {
  74. .config = (char *) "krava23",
  75. .val.num = 2,
  76. .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
  77. .type_term = PARSE_EVENTS__TERM_TYPE_USER,
  78. },
  79. };
  80. /*
  81. * Prepare format directory data, exported by kernel
  82. * at /sys/bus/event_source/devices/<dev>/format.
  83. */
  84. static char *test_format_dir_get(void)
  85. {
  86. static char dir[PATH_MAX];
  87. unsigned int i;
  88. snprintf(dir, PATH_MAX, "/tmp/perf-pmu-test-format-XXXXXX");
  89. if (!mkdtemp(dir))
  90. return NULL;
  91. for (i = 0; i < ARRAY_SIZE(test_formats); i++) {
  92. static char name[PATH_MAX];
  93. struct test_format *format = &test_formats[i];
  94. FILE *file;
  95. scnprintf(name, PATH_MAX, "%s/%s", dir, format->name);
  96. file = fopen(name, "w");
  97. if (!file)
  98. return NULL;
  99. if (1 != fwrite(format->value, strlen(format->value), 1, file))
  100. break;
  101. fclose(file);
  102. }
  103. return dir;
  104. }
  105. /* Cleanup format directory. */
  106. static int test_format_dir_put(char *dir)
  107. {
  108. char buf[PATH_MAX];
  109. snprintf(buf, PATH_MAX, "rm -f %s/*\n", dir);
  110. if (system(buf))
  111. return -1;
  112. snprintf(buf, PATH_MAX, "rmdir %s\n", dir);
  113. return system(buf);
  114. }
  115. static struct list_head *test_terms_list(void)
  116. {
  117. static LIST_HEAD(terms);
  118. unsigned int i;
  119. for (i = 0; i < ARRAY_SIZE(test_terms); i++)
  120. list_add_tail(&test_terms[i].list, &terms);
  121. return &terms;
  122. }
  123. int test__pmu(struct test *test __maybe_unused, int subtest __maybe_unused)
  124. {
  125. char *format = test_format_dir_get();
  126. LIST_HEAD(formats);
  127. struct list_head *terms = test_terms_list();
  128. int ret;
  129. if (!format)
  130. return -EINVAL;
  131. do {
  132. struct perf_event_attr attr;
  133. memset(&attr, 0, sizeof(attr));
  134. ret = perf_pmu__format_parse(format, &formats);
  135. if (ret)
  136. break;
  137. ret = perf_pmu__config_terms(&formats, &attr, terms,
  138. false, NULL);
  139. if (ret)
  140. break;
  141. ret = -EINVAL;
  142. if (attr.config != 0xc00000000002a823)
  143. break;
  144. if (attr.config1 != 0x8000400000000145)
  145. break;
  146. if (attr.config2 != 0x0400000020041d07)
  147. break;
  148. ret = 0;
  149. } while (0);
  150. test_format_dir_put(format);
  151. return ret;
  152. }