pmu.c 3.7 KB

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