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