xref: /linux/tools/perf/tests/stat.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
267424342SJiri Olsa #include <linux/compiler.h>
367424342SJiri Olsa #include "event.h"
467424342SJiri Olsa #include "tests.h"
567424342SJiri Olsa #include "stat.h"
65796f8f0SJiri Olsa #include "counts.h"
767424342SJiri Olsa #include "debug.h"
8ea49e01cSArnaldo Carvalho de Melo #include "util/synthetic-events.h"
967424342SJiri Olsa 
has_term(struct perf_record_stat_config * config,u64 tag,u64 val)1072932371SJiri Olsa static bool has_term(struct perf_record_stat_config *config,
1167424342SJiri Olsa 		     u64 tag, u64 val)
1267424342SJiri Olsa {
1367424342SJiri Olsa 	unsigned i;
1467424342SJiri Olsa 
1567424342SJiri Olsa 	for (i = 0; i < config->nr; i++) {
1667424342SJiri Olsa 		if ((config->data[i].tag == tag) &&
1767424342SJiri Olsa 		    (config->data[i].val == val))
1867424342SJiri Olsa 			return true;
1967424342SJiri Olsa 	}
2067424342SJiri Olsa 
2167424342SJiri Olsa 	return false;
2267424342SJiri Olsa }
2367424342SJiri Olsa 
process_stat_config_event(const struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample __maybe_unused,struct machine * machine __maybe_unused)24*30f29baeSIan Rogers static int process_stat_config_event(const struct perf_tool *tool __maybe_unused,
2567424342SJiri Olsa 				     union perf_event *event,
2667424342SJiri Olsa 				     struct perf_sample *sample __maybe_unused,
2767424342SJiri Olsa 				     struct machine *machine __maybe_unused)
2867424342SJiri Olsa {
2972932371SJiri Olsa 	struct perf_record_stat_config *config = &event->stat_config;
3091f88a0aSIan Rogers 	struct perf_stat_config stat_config = {};
3167424342SJiri Olsa 
3267424342SJiri Olsa #define HAS(term, val) \
3367424342SJiri Olsa 	has_term(config, PERF_STAT_CONFIG_TERM__##term, val)
3467424342SJiri Olsa 
3567424342SJiri Olsa 	TEST_ASSERT_VAL("wrong nr",        config->nr == PERF_STAT_CONFIG_TERM__MAX);
3667424342SJiri Olsa 	TEST_ASSERT_VAL("wrong aggr_mode", HAS(AGGR_MODE, AGGR_CORE));
3767424342SJiri Olsa 	TEST_ASSERT_VAL("wrong scale",     HAS(SCALE, 1));
3867424342SJiri Olsa 	TEST_ASSERT_VAL("wrong interval",  HAS(INTERVAL, 1));
3967424342SJiri Olsa 
4067424342SJiri Olsa #undef HAS
4167424342SJiri Olsa 
428e381596SJiri Olsa 	perf_event__read_stat_config(&stat_config, config);
438e381596SJiri Olsa 
448e381596SJiri Olsa 	TEST_ASSERT_VAL("wrong aggr_mode", stat_config.aggr_mode == AGGR_CORE);
458e381596SJiri Olsa 	TEST_ASSERT_VAL("wrong scale",     stat_config.scale == 1);
468e381596SJiri Olsa 	TEST_ASSERT_VAL("wrong interval",  stat_config.interval == 1);
4767424342SJiri Olsa 	return 0;
4867424342SJiri Olsa }
4967424342SJiri Olsa 
test__synthesize_stat_config(struct test_suite * test __maybe_unused,int subtest __maybe_unused)5033f44bfdSIan Rogers static int test__synthesize_stat_config(struct test_suite *test __maybe_unused,
51d68f0365SIan Rogers 					int subtest __maybe_unused)
5267424342SJiri Olsa {
5367424342SJiri Olsa 	struct perf_stat_config stat_config = {
5467424342SJiri Olsa 		.aggr_mode	= AGGR_CORE,
5567424342SJiri Olsa 		.scale		= 1,
5667424342SJiri Olsa 		.interval	= 1,
5767424342SJiri Olsa 	};
5867424342SJiri Olsa 
5967424342SJiri Olsa 	TEST_ASSERT_VAL("failed to synthesize stat_config",
605796f8f0SJiri Olsa 		!perf_event__synthesize_stat_config(NULL, &stat_config, process_stat_config_event, NULL));
615796f8f0SJiri Olsa 
625796f8f0SJiri Olsa 	return 0;
635796f8f0SJiri Olsa }
645796f8f0SJiri Olsa 
process_stat_event(const struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample __maybe_unused,struct machine * machine __maybe_unused)65*30f29baeSIan Rogers static int process_stat_event(const struct perf_tool *tool __maybe_unused,
665796f8f0SJiri Olsa 			      union perf_event *event,
675796f8f0SJiri Olsa 			      struct perf_sample *sample __maybe_unused,
685796f8f0SJiri Olsa 			      struct machine *machine __maybe_unused)
695796f8f0SJiri Olsa {
7072932371SJiri Olsa 	struct perf_record_stat *st = &event->stat;
715796f8f0SJiri Olsa 
725796f8f0SJiri Olsa 	TEST_ASSERT_VAL("wrong cpu",    st->cpu    == 1);
735796f8f0SJiri Olsa 	TEST_ASSERT_VAL("wrong thread", st->thread == 2);
745796f8f0SJiri Olsa 	TEST_ASSERT_VAL("wrong id",     st->id     == 3);
755796f8f0SJiri Olsa 	TEST_ASSERT_VAL("wrong val",    st->val    == 100);
765796f8f0SJiri Olsa 	TEST_ASSERT_VAL("wrong run",    st->ena    == 200);
775796f8f0SJiri Olsa 	TEST_ASSERT_VAL("wrong ena",    st->run    == 300);
785796f8f0SJiri Olsa 	return 0;
795796f8f0SJiri Olsa }
805796f8f0SJiri Olsa 
test__synthesize_stat(struct test_suite * test __maybe_unused,int subtest __maybe_unused)8133f44bfdSIan Rogers static int test__synthesize_stat(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
825796f8f0SJiri Olsa {
835796f8f0SJiri Olsa 	struct perf_counts_values count;
845796f8f0SJiri Olsa 
855796f8f0SJiri Olsa 	count.val = 100;
865796f8f0SJiri Olsa 	count.ena = 200;
875796f8f0SJiri Olsa 	count.run = 300;
885796f8f0SJiri Olsa 
895796f8f0SJiri Olsa 	TEST_ASSERT_VAL("failed to synthesize stat_config",
906d18804bSIan Rogers 			!perf_event__synthesize_stat(NULL, (struct perf_cpu){.cpu = 1}, 2, 3,
916d18804bSIan Rogers 						     &count, process_stat_event, NULL));
9267424342SJiri Olsa 
9367424342SJiri Olsa 	return 0;
9467424342SJiri Olsa }
95d4c22591SJiri Olsa 
process_stat_round_event(const struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample __maybe_unused,struct machine * machine __maybe_unused)96*30f29baeSIan Rogers static int process_stat_round_event(const struct perf_tool *tool __maybe_unused,
97d4c22591SJiri Olsa 				    union perf_event *event,
98d4c22591SJiri Olsa 				    struct perf_sample *sample __maybe_unused,
99d4c22591SJiri Olsa 				    struct machine *machine __maybe_unused)
100d4c22591SJiri Olsa {
10172932371SJiri Olsa 	struct perf_record_stat_round *stat_round = &event->stat_round;
102d4c22591SJiri Olsa 
103d4c22591SJiri Olsa 	TEST_ASSERT_VAL("wrong time", stat_round->time == 0xdeadbeef);
104d4c22591SJiri Olsa 	TEST_ASSERT_VAL("wrong type", stat_round->type == PERF_STAT_ROUND_TYPE__INTERVAL);
105d4c22591SJiri Olsa 	return 0;
106d4c22591SJiri Olsa }
107d4c22591SJiri Olsa 
test__synthesize_stat_round(struct test_suite * test __maybe_unused,int subtest __maybe_unused)10833f44bfdSIan Rogers static int test__synthesize_stat_round(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
109d4c22591SJiri Olsa {
110d4c22591SJiri Olsa 	TEST_ASSERT_VAL("failed to synthesize stat_config",
111d4c22591SJiri Olsa 		!perf_event__synthesize_stat_round(NULL, 0xdeadbeef, PERF_STAT_ROUND_TYPE__INTERVAL,
112d4c22591SJiri Olsa 						   process_stat_round_event, NULL));
113d4c22591SJiri Olsa 
114d4c22591SJiri Olsa 	return 0;
115d4c22591SJiri Olsa }
116d68f0365SIan Rogers 
117d68f0365SIan Rogers DEFINE_SUITE("Synthesize stat config", synthesize_stat_config);
118d68f0365SIan Rogers DEFINE_SUITE("Synthesize stat", synthesize_stat);
119d68f0365SIan Rogers DEFINE_SUITE("Synthesize stat round", synthesize_stat_round);
120