1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker * Copyright (C) 2023 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker *
4*288bf522SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker *
8*288bf522SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker *
10*288bf522SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker */
16*288bf522SAndroid Build Coastguard Worker
17*288bf522SAndroid Build Coastguard Worker #include <gtest/gtest.h>
18*288bf522SAndroid Build Coastguard Worker
19*288bf522SAndroid Build Coastguard Worker #include "event_selection_set.h"
20*288bf522SAndroid Build Coastguard Worker
21*288bf522SAndroid Build Coastguard Worker using namespace simpleperf;
22*288bf522SAndroid Build Coastguard Worker
23*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(EventSelectionSet,set_sample_rate_for_new_events)24*288bf522SAndroid Build Coastguard Worker TEST(EventSelectionSet, set_sample_rate_for_new_events) {
25*288bf522SAndroid Build Coastguard Worker EventSelectionSet event_selection_set(false);
26*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(event_selection_set.AddEventType("cpu-clock:u"));
27*288bf522SAndroid Build Coastguard Worker event_selection_set.SetSampleRateForNewEvents(SampleRate(100, 0));
28*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(event_selection_set.AddEventType("page-faults:u"));
29*288bf522SAndroid Build Coastguard Worker event_selection_set.SetSampleRateForNewEvents(SampleRate(200, 0));
30*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(event_selection_set.AddEventGroup({"context-switches:u", "task-clock:u"}));
31*288bf522SAndroid Build Coastguard Worker EventAttrIds attrs = event_selection_set.GetEventAttrWithId();
32*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(attrs.size(), 4);
33*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(GetEventNameByAttr(attrs[0].attr), "cpu-clock:u");
34*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(attrs[0].attr.freq, 1);
35*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(attrs[0].attr.sample_freq, 100);
36*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(GetEventNameByAttr(attrs[1].attr), "page-faults:u");
37*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(attrs[1].attr.freq, 1);
38*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(attrs[1].attr.sample_freq, 100);
39*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(GetEventNameByAttr(attrs[2].attr), "context-switches:u");
40*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(attrs[2].attr.freq, 1);
41*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(attrs[2].attr.sample_freq, 200);
42*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(GetEventNameByAttr(attrs[3].attr), "task-clock:u");
43*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(attrs[3].attr.freq, 1);
44*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(attrs[3].attr.sample_freq, 200);
45*288bf522SAndroid Build Coastguard Worker }
46*288bf522SAndroid Build Coastguard Worker
47*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(EventSelectionSet,add_event_with_sample_rate)48*288bf522SAndroid Build Coastguard Worker TEST(EventSelectionSet, add_event_with_sample_rate) {
49*288bf522SAndroid Build Coastguard Worker EventSelectionSet event_selection_set(false);
50*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(event_selection_set.AddEventType("cpu-clock:u"));
51*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(event_selection_set.AddEventType("context-switches", SampleRate(0, 1)));
52*288bf522SAndroid Build Coastguard Worker EventAttrIds attrs = event_selection_set.GetEventAttrWithId();
53*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(attrs.size(), 2);
54*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(GetEventNameByAttr(attrs[0].attr), "cpu-clock:u");
55*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(attrs[0].attr.freq, 1);
56*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(attrs[0].attr.sample_freq, 4000);
57*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(GetEventNameByAttr(attrs[1].attr), "context-switches");
58*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(attrs[1].attr.freq, 0);
59*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(attrs[1].attr.sample_period, 1);
60*288bf522SAndroid Build Coastguard Worker }
61*288bf522SAndroid Build Coastguard Worker
62*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(EventSelectionSet,set_cpus_for_new_events)63*288bf522SAndroid Build Coastguard Worker TEST(EventSelectionSet, set_cpus_for_new_events) {
64*288bf522SAndroid Build Coastguard Worker EventSelectionSet event_selection_set(false);
65*288bf522SAndroid Build Coastguard Worker std::vector<int> online_cpus = GetOnlineCpus();
66*288bf522SAndroid Build Coastguard Worker ASSERT_FALSE(online_cpus.empty());
67*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(event_selection_set.AddEventType("cpu-clock:u"));
68*288bf522SAndroid Build Coastguard Worker event_selection_set.SetCpusForNewEvents({online_cpus[0]});
69*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(event_selection_set.AddEventType("page-faults:u"));
70*288bf522SAndroid Build Coastguard Worker event_selection_set.SetCpusForNewEvents({online_cpus.back()});
71*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(event_selection_set.AddEventGroup({"context-switches:u", "task-clock:u"}));
72*288bf522SAndroid Build Coastguard Worker event_selection_set.AddMonitoredThreads({gettid()});
73*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(event_selection_set.OpenEventFiles());
74*288bf522SAndroid Build Coastguard Worker
75*288bf522SAndroid Build Coastguard Worker std::unordered_map<uint64_t, int> id_to_cpu = event_selection_set.GetCpusById();
76*288bf522SAndroid Build Coastguard Worker auto get_cpu = [&](int id) {
77*288bf522SAndroid Build Coastguard Worker if (auto it = id_to_cpu.find(id); it != id_to_cpu.end()) {
78*288bf522SAndroid Build Coastguard Worker return it->second;
79*288bf522SAndroid Build Coastguard Worker }
80*288bf522SAndroid Build Coastguard Worker return -2;
81*288bf522SAndroid Build Coastguard Worker };
82*288bf522SAndroid Build Coastguard Worker
83*288bf522SAndroid Build Coastguard Worker EventAttrIds attrs = event_selection_set.GetEventAttrWithId();
84*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(attrs.size(), 4);
85*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(GetEventNameByAttr(attrs[0].attr), "cpu-clock:u");
86*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(attrs[0].ids.size(), 1);
87*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(get_cpu(attrs[0].ids[0]), online_cpus[0]);
88*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(GetEventNameByAttr(attrs[1].attr), "page-faults:u");
89*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(attrs[1].ids.size(), 1);
90*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(get_cpu(attrs[1].ids[0]), online_cpus[0]);
91*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(GetEventNameByAttr(attrs[2].attr), "context-switches:u");
92*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(attrs[2].ids.size(), 1);
93*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(get_cpu(attrs[2].ids[0]), online_cpus.back());
94*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(GetEventNameByAttr(attrs[3].attr), "task-clock:u");
95*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(attrs[3].ids.size(), 1);
96*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(get_cpu(attrs[3].ids[0]), online_cpus.back());
97*288bf522SAndroid Build Coastguard Worker }
98