Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/compiler.h>
0003 #include "event.h"
0004 #include "tests.h"
0005 #include "stat.h"
0006 #include "counts.h"
0007 #include "debug.h"
0008 #include "util/synthetic-events.h"
0009 
0010 static bool has_term(struct perf_record_stat_config *config,
0011              u64 tag, u64 val)
0012 {
0013     unsigned i;
0014 
0015     for (i = 0; i < config->nr; i++) {
0016         if ((config->data[i].tag == tag) &&
0017             (config->data[i].val == val))
0018             return true;
0019     }
0020 
0021     return false;
0022 }
0023 
0024 static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
0025                      union perf_event *event,
0026                      struct perf_sample *sample __maybe_unused,
0027                      struct machine *machine __maybe_unused)
0028 {
0029     struct perf_record_stat_config *config = &event->stat_config;
0030     struct perf_stat_config stat_config;
0031 
0032 #define HAS(term, val) \
0033     has_term(config, PERF_STAT_CONFIG_TERM__##term, val)
0034 
0035     TEST_ASSERT_VAL("wrong nr",        config->nr == PERF_STAT_CONFIG_TERM__MAX);
0036     TEST_ASSERT_VAL("wrong aggr_mode", HAS(AGGR_MODE, AGGR_CORE));
0037     TEST_ASSERT_VAL("wrong scale",     HAS(SCALE, 1));
0038     TEST_ASSERT_VAL("wrong interval",  HAS(INTERVAL, 1));
0039 
0040 #undef HAS
0041 
0042     perf_event__read_stat_config(&stat_config, config);
0043 
0044     TEST_ASSERT_VAL("wrong aggr_mode", stat_config.aggr_mode == AGGR_CORE);
0045     TEST_ASSERT_VAL("wrong scale",     stat_config.scale == 1);
0046     TEST_ASSERT_VAL("wrong interval",  stat_config.interval == 1);
0047     return 0;
0048 }
0049 
0050 static int test__synthesize_stat_config(struct test_suite *test __maybe_unused,
0051                     int subtest __maybe_unused)
0052 {
0053     struct perf_stat_config stat_config = {
0054         .aggr_mode  = AGGR_CORE,
0055         .scale      = 1,
0056         .interval   = 1,
0057     };
0058 
0059     TEST_ASSERT_VAL("failed to synthesize stat_config",
0060         !perf_event__synthesize_stat_config(NULL, &stat_config, process_stat_config_event, NULL));
0061 
0062     return 0;
0063 }
0064 
0065 static int process_stat_event(struct perf_tool *tool __maybe_unused,
0066                   union perf_event *event,
0067                   struct perf_sample *sample __maybe_unused,
0068                   struct machine *machine __maybe_unused)
0069 {
0070     struct perf_record_stat *st = &event->stat;
0071 
0072     TEST_ASSERT_VAL("wrong cpu",    st->cpu    == 1);
0073     TEST_ASSERT_VAL("wrong thread", st->thread == 2);
0074     TEST_ASSERT_VAL("wrong id",     st->id     == 3);
0075     TEST_ASSERT_VAL("wrong val",    st->val    == 100);
0076     TEST_ASSERT_VAL("wrong run",    st->ena    == 200);
0077     TEST_ASSERT_VAL("wrong ena",    st->run    == 300);
0078     return 0;
0079 }
0080 
0081 static int test__synthesize_stat(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0082 {
0083     struct perf_counts_values count;
0084 
0085     count.val = 100;
0086     count.ena = 200;
0087     count.run = 300;
0088 
0089     TEST_ASSERT_VAL("failed to synthesize stat_config",
0090             !perf_event__synthesize_stat(NULL, (struct perf_cpu){.cpu = 1}, 2, 3,
0091                              &count, process_stat_event, NULL));
0092 
0093     return 0;
0094 }
0095 
0096 static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
0097                     union perf_event *event,
0098                     struct perf_sample *sample __maybe_unused,
0099                     struct machine *machine __maybe_unused)
0100 {
0101     struct perf_record_stat_round *stat_round = &event->stat_round;
0102 
0103     TEST_ASSERT_VAL("wrong time", stat_round->time == 0xdeadbeef);
0104     TEST_ASSERT_VAL("wrong type", stat_round->type == PERF_STAT_ROUND_TYPE__INTERVAL);
0105     return 0;
0106 }
0107 
0108 static int test__synthesize_stat_round(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0109 {
0110     TEST_ASSERT_VAL("failed to synthesize stat_config",
0111         !perf_event__synthesize_stat_round(NULL, 0xdeadbeef, PERF_STAT_ROUND_TYPE__INTERVAL,
0112                            process_stat_round_event, NULL));
0113 
0114     return 0;
0115 }
0116 
0117 DEFINE_SUITE("Synthesize stat config", synthesize_stat_config);
0118 DEFINE_SUITE("Synthesize stat", synthesize_stat);
0119 DEFINE_SUITE("Synthesize stat round", synthesize_stat_round);