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