stat.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. static bool has_term(struct stat_config_event *config,
  9. u64 tag, u64 val)
  10. {
  11. unsigned i;
  12. for (i = 0; i < config->nr; i++) {
  13. if ((config->data[i].tag == tag) &&
  14. (config->data[i].val == val))
  15. return true;
  16. }
  17. return false;
  18. }
  19. static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
  20. union perf_event *event,
  21. struct perf_sample *sample __maybe_unused,
  22. struct machine *machine __maybe_unused)
  23. {
  24. struct stat_config_event *config = &event->stat_config;
  25. struct perf_stat_config stat_config;
  26. #define HAS(term, val) \
  27. has_term(config, PERF_STAT_CONFIG_TERM__##term, val)
  28. TEST_ASSERT_VAL("wrong nr", config->nr == PERF_STAT_CONFIG_TERM__MAX);
  29. TEST_ASSERT_VAL("wrong aggr_mode", HAS(AGGR_MODE, AGGR_CORE));
  30. TEST_ASSERT_VAL("wrong scale", HAS(SCALE, 1));
  31. TEST_ASSERT_VAL("wrong interval", HAS(INTERVAL, 1));
  32. #undef HAS
  33. perf_event__read_stat_config(&stat_config, config);
  34. TEST_ASSERT_VAL("wrong aggr_mode", stat_config.aggr_mode == AGGR_CORE);
  35. TEST_ASSERT_VAL("wrong scale", stat_config.scale == 1);
  36. TEST_ASSERT_VAL("wrong interval", stat_config.interval == 1);
  37. return 0;
  38. }
  39. int test__synthesize_stat_config(struct test *test __maybe_unused, int subtest __maybe_unused)
  40. {
  41. struct perf_stat_config stat_config = {
  42. .aggr_mode = AGGR_CORE,
  43. .scale = 1,
  44. .interval = 1,
  45. };
  46. TEST_ASSERT_VAL("failed to synthesize stat_config",
  47. !perf_event__synthesize_stat_config(NULL, &stat_config, process_stat_config_event, NULL));
  48. return 0;
  49. }
  50. static int process_stat_event(struct perf_tool *tool __maybe_unused,
  51. union perf_event *event,
  52. struct perf_sample *sample __maybe_unused,
  53. struct machine *machine __maybe_unused)
  54. {
  55. struct stat_event *st = &event->stat;
  56. TEST_ASSERT_VAL("wrong cpu", st->cpu == 1);
  57. TEST_ASSERT_VAL("wrong thread", st->thread == 2);
  58. TEST_ASSERT_VAL("wrong id", st->id == 3);
  59. TEST_ASSERT_VAL("wrong val", st->val == 100);
  60. TEST_ASSERT_VAL("wrong run", st->ena == 200);
  61. TEST_ASSERT_VAL("wrong ena", st->run == 300);
  62. return 0;
  63. }
  64. int test__synthesize_stat(struct test *test __maybe_unused, int subtest __maybe_unused)
  65. {
  66. struct perf_counts_values count;
  67. count.val = 100;
  68. count.ena = 200;
  69. count.run = 300;
  70. TEST_ASSERT_VAL("failed to synthesize stat_config",
  71. !perf_event__synthesize_stat(NULL, 1, 2, 3, &count, process_stat_event, NULL));
  72. return 0;
  73. }
  74. static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
  75. union perf_event *event,
  76. struct perf_sample *sample __maybe_unused,
  77. struct machine *machine __maybe_unused)
  78. {
  79. struct stat_round_event *stat_round = &event->stat_round;
  80. TEST_ASSERT_VAL("wrong time", stat_round->time == 0xdeadbeef);
  81. TEST_ASSERT_VAL("wrong type", stat_round->type == PERF_STAT_ROUND_TYPE__INTERVAL);
  82. return 0;
  83. }
  84. int test__synthesize_stat_round(struct test *test __maybe_unused, int subtest __maybe_unused)
  85. {
  86. TEST_ASSERT_VAL("failed to synthesize stat_config",
  87. !perf_event__synthesize_stat_round(NULL, 0xdeadbeef, PERF_STAT_ROUND_TYPE__INTERVAL,
  88. process_stat_round_event, NULL));
  89. return 0;
  90. }