Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jiri Olsa | 6742434 | 2015-10-25 15:51:28 +0100 | [diff] [blame] | 2 | #include <linux/compiler.h> |
| 3 | #include "event.h" |
| 4 | #include "tests.h" |
| 5 | #include "stat.h" |
Jiri Olsa | 5796f8f | 2015-10-25 15:51:31 +0100 | [diff] [blame] | 6 | #include "counts.h" |
Jiri Olsa | 6742434 | 2015-10-25 15:51:28 +0100 | [diff] [blame] | 7 | #include "debug.h" |
Arnaldo Carvalho de Melo | ea49e01 | 2019-09-18 11:36:13 -0300 | [diff] [blame] | 8 | #include "util/synthetic-events.h" |
Jiri Olsa | 6742434 | 2015-10-25 15:51:28 +0100 | [diff] [blame] | 9 | |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 10 | static bool has_term(struct perf_record_stat_config *config, |
Jiri Olsa | 6742434 | 2015-10-25 15:51:28 +0100 | [diff] [blame] | 11 | u64 tag, u64 val) |
| 12 | { |
| 13 | unsigned i; |
| 14 | |
| 15 | for (i = 0; i < config->nr; i++) { |
| 16 | if ((config->data[i].tag == tag) && |
| 17 | (config->data[i].val == val)) |
| 18 | return true; |
| 19 | } |
| 20 | |
| 21 | return false; |
| 22 | } |
| 23 | |
Jiri Olsa | 5796f8f | 2015-10-25 15:51:31 +0100 | [diff] [blame] | 24 | static int process_stat_config_event(struct perf_tool *tool __maybe_unused, |
| 25 | union perf_event *event, |
| 26 | struct perf_sample *sample __maybe_unused, |
| 27 | struct machine *machine __maybe_unused) |
Jiri Olsa | 6742434 | 2015-10-25 15:51:28 +0100 | [diff] [blame] | 28 | { |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 29 | struct perf_record_stat_config *config = &event->stat_config; |
Jiri Olsa | 8e38159 | 2015-10-25 15:51:29 +0100 | [diff] [blame] | 30 | struct perf_stat_config stat_config; |
Jiri Olsa | 6742434 | 2015-10-25 15:51:28 +0100 | [diff] [blame] | 31 | |
| 32 | #define HAS(term, val) \ |
| 33 | has_term(config, PERF_STAT_CONFIG_TERM__##term, val) |
| 34 | |
| 35 | TEST_ASSERT_VAL("wrong nr", config->nr == PERF_STAT_CONFIG_TERM__MAX); |
| 36 | TEST_ASSERT_VAL("wrong aggr_mode", HAS(AGGR_MODE, AGGR_CORE)); |
| 37 | TEST_ASSERT_VAL("wrong scale", HAS(SCALE, 1)); |
| 38 | TEST_ASSERT_VAL("wrong interval", HAS(INTERVAL, 1)); |
| 39 | |
| 40 | #undef HAS |
| 41 | |
Jiri Olsa | 8e38159 | 2015-10-25 15:51:29 +0100 | [diff] [blame] | 42 | perf_event__read_stat_config(&stat_config, config); |
| 43 | |
| 44 | TEST_ASSERT_VAL("wrong aggr_mode", stat_config.aggr_mode == AGGR_CORE); |
| 45 | TEST_ASSERT_VAL("wrong scale", stat_config.scale == 1); |
| 46 | TEST_ASSERT_VAL("wrong interval", stat_config.interval == 1); |
Jiri Olsa | 6742434 | 2015-10-25 15:51:28 +0100 | [diff] [blame] | 47 | return 0; |
| 48 | } |
| 49 | |
Ian Rogers | 33f44bf | 2021-11-03 23:41:51 -0700 | [diff] [blame] | 50 | static int test__synthesize_stat_config(struct test_suite *test __maybe_unused, |
Ian Rogers | d68f036 | 2021-11-03 23:41:50 -0700 | [diff] [blame] | 51 | int subtest __maybe_unused) |
Jiri Olsa | 6742434 | 2015-10-25 15:51:28 +0100 | [diff] [blame] | 52 | { |
| 53 | struct perf_stat_config stat_config = { |
| 54 | .aggr_mode = AGGR_CORE, |
| 55 | .scale = 1, |
| 56 | .interval = 1, |
| 57 | }; |
| 58 | |
| 59 | TEST_ASSERT_VAL("failed to synthesize stat_config", |
Jiri Olsa | 5796f8f | 2015-10-25 15:51:31 +0100 | [diff] [blame] | 60 | !perf_event__synthesize_stat_config(NULL, &stat_config, process_stat_config_event, NULL)); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int process_stat_event(struct perf_tool *tool __maybe_unused, |
| 66 | union perf_event *event, |
| 67 | struct perf_sample *sample __maybe_unused, |
| 68 | struct machine *machine __maybe_unused) |
| 69 | { |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 70 | struct perf_record_stat *st = &event->stat; |
Jiri Olsa | 5796f8f | 2015-10-25 15:51:31 +0100 | [diff] [blame] | 71 | |
| 72 | TEST_ASSERT_VAL("wrong cpu", st->cpu == 1); |
| 73 | TEST_ASSERT_VAL("wrong thread", st->thread == 2); |
| 74 | TEST_ASSERT_VAL("wrong id", st->id == 3); |
| 75 | TEST_ASSERT_VAL("wrong val", st->val == 100); |
| 76 | TEST_ASSERT_VAL("wrong run", st->ena == 200); |
| 77 | TEST_ASSERT_VAL("wrong ena", st->run == 300); |
| 78 | return 0; |
| 79 | } |
| 80 | |
Ian Rogers | 33f44bf | 2021-11-03 23:41:51 -0700 | [diff] [blame] | 81 | static int test__synthesize_stat(struct test_suite *test __maybe_unused, int subtest __maybe_unused) |
Jiri Olsa | 5796f8f | 2015-10-25 15:51:31 +0100 | [diff] [blame] | 82 | { |
| 83 | struct perf_counts_values count; |
| 84 | |
| 85 | count.val = 100; |
| 86 | count.ena = 200; |
| 87 | count.run = 300; |
| 88 | |
| 89 | TEST_ASSERT_VAL("failed to synthesize stat_config", |
| 90 | !perf_event__synthesize_stat(NULL, 1, 2, 3, &count, process_stat_event, NULL)); |
Jiri Olsa | 6742434 | 2015-10-25 15:51:28 +0100 | [diff] [blame] | 91 | |
| 92 | return 0; |
| 93 | } |
Jiri Olsa | d4c2259 | 2015-10-25 15:51:34 +0100 | [diff] [blame] | 94 | |
| 95 | static int process_stat_round_event(struct perf_tool *tool __maybe_unused, |
| 96 | union perf_event *event, |
| 97 | struct perf_sample *sample __maybe_unused, |
| 98 | struct machine *machine __maybe_unused) |
| 99 | { |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 100 | struct perf_record_stat_round *stat_round = &event->stat_round; |
Jiri Olsa | d4c2259 | 2015-10-25 15:51:34 +0100 | [diff] [blame] | 101 | |
| 102 | TEST_ASSERT_VAL("wrong time", stat_round->time == 0xdeadbeef); |
| 103 | TEST_ASSERT_VAL("wrong type", stat_round->type == PERF_STAT_ROUND_TYPE__INTERVAL); |
| 104 | return 0; |
| 105 | } |
| 106 | |
Ian Rogers | 33f44bf | 2021-11-03 23:41:51 -0700 | [diff] [blame] | 107 | static int test__synthesize_stat_round(struct test_suite *test __maybe_unused, int subtest __maybe_unused) |
Jiri Olsa | d4c2259 | 2015-10-25 15:51:34 +0100 | [diff] [blame] | 108 | { |
| 109 | TEST_ASSERT_VAL("failed to synthesize stat_config", |
| 110 | !perf_event__synthesize_stat_round(NULL, 0xdeadbeef, PERF_STAT_ROUND_TYPE__INTERVAL, |
| 111 | process_stat_round_event, NULL)); |
| 112 | |
| 113 | return 0; |
| 114 | } |
Ian Rogers | d68f036 | 2021-11-03 23:41:50 -0700 | [diff] [blame] | 115 | |
| 116 | DEFINE_SUITE("Synthesize stat config", synthesize_stat_config); |
| 117 | DEFINE_SUITE("Synthesize stat", synthesize_stat); |
| 118 | DEFINE_SUITE("Synthesize stat round", synthesize_stat_round); |