18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <linux/compiler.h>
38c2ecf20Sopenharmony_ci#include "event.h"
48c2ecf20Sopenharmony_ci#include "tests.h"
58c2ecf20Sopenharmony_ci#include "stat.h"
68c2ecf20Sopenharmony_ci#include "counts.h"
78c2ecf20Sopenharmony_ci#include "debug.h"
88c2ecf20Sopenharmony_ci#include "util/synthetic-events.h"
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_cistatic bool has_term(struct perf_record_stat_config *config,
118c2ecf20Sopenharmony_ci		     u64 tag, u64 val)
128c2ecf20Sopenharmony_ci{
138c2ecf20Sopenharmony_ci	unsigned i;
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci	for (i = 0; i < config->nr; i++) {
168c2ecf20Sopenharmony_ci		if ((config->data[i].tag == tag) &&
178c2ecf20Sopenharmony_ci		    (config->data[i].val == val))
188c2ecf20Sopenharmony_ci			return true;
198c2ecf20Sopenharmony_ci	}
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci	return false;
228c2ecf20Sopenharmony_ci}
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistatic int process_stat_config_event(struct perf_tool *tool __maybe_unused,
258c2ecf20Sopenharmony_ci				     union perf_event *event,
268c2ecf20Sopenharmony_ci				     struct perf_sample *sample __maybe_unused,
278c2ecf20Sopenharmony_ci				     struct machine *machine __maybe_unused)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	struct perf_record_stat_config *config = &event->stat_config;
308c2ecf20Sopenharmony_ci	struct perf_stat_config stat_config;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define HAS(term, val) \
338c2ecf20Sopenharmony_ci	has_term(config, PERF_STAT_CONFIG_TERM__##term, val)
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("wrong nr",        config->nr == PERF_STAT_CONFIG_TERM__MAX);
368c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("wrong aggr_mode", HAS(AGGR_MODE, AGGR_CORE));
378c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("wrong scale",     HAS(SCALE, 1));
388c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("wrong interval",  HAS(INTERVAL, 1));
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#undef HAS
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	perf_event__read_stat_config(&stat_config, config);
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("wrong aggr_mode", stat_config.aggr_mode == AGGR_CORE);
458c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("wrong scale",     stat_config.scale == 1);
468c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("wrong interval",  stat_config.interval == 1);
478c2ecf20Sopenharmony_ci	return 0;
488c2ecf20Sopenharmony_ci}
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ciint test__synthesize_stat_config(struct test *test __maybe_unused, int subtest __maybe_unused)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	struct perf_stat_config stat_config = {
538c2ecf20Sopenharmony_ci		.aggr_mode	= AGGR_CORE,
548c2ecf20Sopenharmony_ci		.scale		= 1,
558c2ecf20Sopenharmony_ci		.interval	= 1,
568c2ecf20Sopenharmony_ci	};
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("failed to synthesize stat_config",
598c2ecf20Sopenharmony_ci		!perf_event__synthesize_stat_config(NULL, &stat_config, process_stat_config_event, NULL));
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	return 0;
628c2ecf20Sopenharmony_ci}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic int process_stat_event(struct perf_tool *tool __maybe_unused,
658c2ecf20Sopenharmony_ci			      union perf_event *event,
668c2ecf20Sopenharmony_ci			      struct perf_sample *sample __maybe_unused,
678c2ecf20Sopenharmony_ci			      struct machine *machine __maybe_unused)
688c2ecf20Sopenharmony_ci{
698c2ecf20Sopenharmony_ci	struct perf_record_stat *st = &event->stat;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("wrong cpu",    st->cpu    == 1);
728c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("wrong thread", st->thread == 2);
738c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("wrong id",     st->id     == 3);
748c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("wrong val",    st->val    == 100);
758c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("wrong run",    st->ena    == 200);
768c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("wrong ena",    st->run    == 300);
778c2ecf20Sopenharmony_ci	return 0;
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ciint test__synthesize_stat(struct test *test __maybe_unused, int subtest __maybe_unused)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	struct perf_counts_values count;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	count.val = 100;
858c2ecf20Sopenharmony_ci	count.ena = 200;
868c2ecf20Sopenharmony_ci	count.run = 300;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("failed to synthesize stat_config",
898c2ecf20Sopenharmony_ci		!perf_event__synthesize_stat(NULL, 1, 2, 3, &count, process_stat_event, NULL));
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	return 0;
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic int process_stat_round_event(struct perf_tool *tool __maybe_unused,
958c2ecf20Sopenharmony_ci				    union perf_event *event,
968c2ecf20Sopenharmony_ci				    struct perf_sample *sample __maybe_unused,
978c2ecf20Sopenharmony_ci				    struct machine *machine __maybe_unused)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	struct perf_record_stat_round *stat_round = &event->stat_round;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("wrong time", stat_round->time == 0xdeadbeef);
1028c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("wrong type", stat_round->type == PERF_STAT_ROUND_TYPE__INTERVAL);
1038c2ecf20Sopenharmony_ci	return 0;
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ciint test__synthesize_stat_round(struct test *test __maybe_unused, int subtest __maybe_unused)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("failed to synthesize stat_config",
1098c2ecf20Sopenharmony_ci		!perf_event__synthesize_stat_round(NULL, 0xdeadbeef, PERF_STAT_ROUND_TYPE__INTERVAL,
1108c2ecf20Sopenharmony_ci						   process_stat_round_event, NULL));
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	return 0;
1138c2ecf20Sopenharmony_ci}
114