stat.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/compiler.h>
  3. #include "event.h"
  4. #include "tests.h"
  5. #include "stat.h"
  6. #include "counts.h"
  7. #include "debug.h"
  8. #include "util/synthetic-events.h"
  9. static bool has_term(struct perf_record_stat_config *config,
  10. u64 tag, u64 val)
  11. {
  12. unsigned i;
  13. for (i = 0; i < config->nr; i++) {
  14. if ((config->data[i].tag == tag) &&
  15. (config->data[i].val == val))
  16. return true;
  17. }
  18. return false;
  19. }
  20. static int process_stat_config_event(const struct perf_tool *tool __maybe_unused,
  21. union perf_event *event,
  22. struct perf_sample *sample __maybe_unused,
  23. struct machine *machine __maybe_unused)
  24. {
  25. struct perf_record_stat_config *config = &event->stat_config;
  26. struct perf_stat_config test_stat_config = {};
  27. #define HAS(term, val) \
  28. has_term(config, PERF_STAT_CONFIG_TERM__##term, val)
  29. TEST_ASSERT_VAL("wrong nr", config->nr == PERF_STAT_CONFIG_TERM__MAX);
  30. TEST_ASSERT_VAL("wrong aggr_mode", HAS(AGGR_MODE, AGGR_CORE));
  31. TEST_ASSERT_VAL("wrong scale", HAS(SCALE, 1));
  32. TEST_ASSERT_VAL("wrong interval", HAS(INTERVAL, 1));
  33. #undef HAS
  34. perf_event__read_stat_config(&test_stat_config, config);
  35. TEST_ASSERT_VAL("wrong aggr_mode", test_stat_config.aggr_mode == AGGR_CORE);
  36. TEST_ASSERT_VAL("wrong scale", test_stat_config.scale == 1);
  37. TEST_ASSERT_VAL("wrong interval", test_stat_config.interval == 1);
  38. return 0;
  39. }
  40. static int test__synthesize_stat_config(struct test_suite *test __maybe_unused,
  41. int subtest __maybe_unused)
  42. {
  43. struct perf_stat_config test_stat_config = {
  44. .aggr_mode = AGGR_CORE,
  45. .scale = 1,
  46. .interval = 1,
  47. };
  48. TEST_ASSERT_VAL("failed to synthesize stat_config",
  49. !perf_event__synthesize_stat_config(NULL, &test_stat_config,
  50. process_stat_config_event,
  51. NULL));
  52. return 0;
  53. }
  54. static int process_stat_event(const struct perf_tool *tool __maybe_unused,
  55. union perf_event *event,
  56. struct perf_sample *sample __maybe_unused,
  57. struct machine *machine __maybe_unused)
  58. {
  59. struct perf_record_stat *st = &event->stat;
  60. TEST_ASSERT_VAL("wrong cpu", st->cpu == 1);
  61. TEST_ASSERT_VAL("wrong thread", st->thread == 2);
  62. TEST_ASSERT_VAL("wrong id", st->id == 3);
  63. TEST_ASSERT_VAL("wrong val", st->val == 100);
  64. TEST_ASSERT_VAL("wrong run", st->ena == 200);
  65. TEST_ASSERT_VAL("wrong ena", st->run == 300);
  66. return 0;
  67. }
  68. static int test__synthesize_stat(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
  69. {
  70. struct perf_counts_values count;
  71. count.val = 100;
  72. count.ena = 200;
  73. count.run = 300;
  74. TEST_ASSERT_VAL("failed to synthesize stat_config",
  75. !perf_event__synthesize_stat(NULL, (struct perf_cpu){.cpu = 1}, 2, 3,
  76. &count, process_stat_event, NULL));
  77. return 0;
  78. }
  79. static int process_stat_round_event(const struct perf_tool *tool __maybe_unused,
  80. union perf_event *event,
  81. struct perf_sample *sample __maybe_unused,
  82. struct machine *machine __maybe_unused)
  83. {
  84. struct perf_record_stat_round *stat_round = &event->stat_round;
  85. TEST_ASSERT_VAL("wrong time", stat_round->time == 0xdeadbeef);
  86. TEST_ASSERT_VAL("wrong type", stat_round->type == PERF_STAT_ROUND_TYPE__INTERVAL);
  87. return 0;
  88. }
  89. static int test__synthesize_stat_round(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
  90. {
  91. TEST_ASSERT_VAL("failed to synthesize stat_config",
  92. !perf_event__synthesize_stat_round(NULL, 0xdeadbeef, PERF_STAT_ROUND_TYPE__INTERVAL,
  93. process_stat_round_event, NULL));
  94. return 0;
  95. }
  96. DEFINE_SUITE("Synthesize stat config", synthesize_stat_config);
  97. DEFINE_SUITE("Synthesize stat", synthesize_stat);
  98. DEFINE_SUITE("Synthesize stat round", synthesize_stat_round);