xref: /aosp_15_r20/system/extras/simpleperf/record_lib_test.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1 /*
2  * Copyright (C) 2017 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 "simpleperf.h"
18 
19 #include <gtest/gtest.h>
20 
21 #include <memory>
22 
23 using namespace simpleperf;
24 
25 // @CddTest = 6.1/C-0-2
TEST(get_all_events,smoke)26 TEST(get_all_events, smoke) {
27   std::vector<std::string> events = GetAllEvents();
28   ASSERT_GT(events.size(), 0u);
29   ASSERT_NE(std::find(events.begin(), events.end(), "cpu-cycles"), events.end());
30   ASSERT_TRUE(IsEventSupported("cpu-cycles"));
31   ASSERT_TRUE(IsEventSupported("cpu-cycles:u"));
32   ASSERT_TRUE(IsEventSupported("cpu-cycles:k"));
33 }
34 
DoSomeWork()35 static void DoSomeWork() {
36   for (volatile int i = 0; i < 100000000; ++i) {
37   }
38 }
39 
40 // @CddTest = 6.1/C-0-2
TEST(counter,add_event)41 TEST(counter, add_event) {
42   std::unique_ptr<PerfEventSet> perf(
43       PerfEventSet::CreateInstance(PerfEventSet::Type::kPerfForCounting));
44   ASSERT_TRUE(perf);
45   ASSERT_TRUE(perf->AddEvent("cpu-cycles"));
46   ASSERT_TRUE(perf->AddEvent("cpu-cycles:u"));
47   ASSERT_TRUE(perf->AddEvent("cpu-cycles:k"));
48   ASSERT_TRUE(perf->MonitorCurrentProcess());
49   ASSERT_TRUE(perf->StartCounters());
50   DoSomeWork();
51   ASSERT_TRUE(perf->StopCounters());
52   std::vector<Counter> counters;
53   ASSERT_TRUE(perf->ReadCounters(&counters));
54   ASSERT_EQ(counters.size(), 3u);
55   ASSERT_EQ(counters[0].event, "cpu-cycles");
56   ASSERT_EQ(counters[1].event, "cpu-cycles:u");
57   ASSERT_EQ(counters[2].event, "cpu-cycles:k");
58   for (auto& counter : counters) {
59     ASSERT_GE(counter.value, 0u);
60     ASSERT_GE(counter.time_enabled_in_ns, 0u);
61     ASSERT_GE(counter.time_running_in_ns, 0u);
62     ASSERT_LE(counter.time_running_in_ns, counter.time_enabled_in_ns);
63   }
64 }
65 
66 // @CddTest = 6.1/C-0-2
TEST(counter,different_targets)67 TEST(counter, different_targets) {
68   auto test_function = [](std::function<void(PerfEventSet*)> set_target_func) {
69     std::unique_ptr<PerfEventSet> perf(
70         PerfEventSet::CreateInstance(PerfEventSet::Type::kPerfForCounting));
71     ASSERT_TRUE(perf);
72     ASSERT_TRUE(perf->AddEvent("cpu-cycles"));
73     set_target_func(perf.get());
74     ASSERT_TRUE(perf->MonitorCurrentProcess());
75     ASSERT_TRUE(perf->StartCounters());
76     DoSomeWork();
77     ASSERT_TRUE(perf->StopCounters());
78     std::vector<Counter> counters;
79     ASSERT_TRUE(perf->ReadCounters(&counters));
80     ASSERT_EQ(counters.size(), 1u);
81     ASSERT_EQ(counters[0].event, "cpu-cycles");
82     ASSERT_GT(counters[0].value, 0u);
83     ASSERT_GT(counters[0].time_enabled_in_ns, 0u);
84     ASSERT_GT(counters[0].time_running_in_ns, 0u);
85     ASSERT_LE(counters[0].time_running_in_ns, counters[0].time_enabled_in_ns);
86   };
87   test_function([](PerfEventSet* perf) { ASSERT_TRUE(perf->MonitorCurrentProcess()); });
88   test_function([](PerfEventSet* perf) { ASSERT_TRUE(perf->MonitorCurrentThread()); });
89   test_function(
90       [](PerfEventSet* perf) { ASSERT_TRUE(perf->MonitorThreadsInCurrentProcess({getpid()})); });
91 }
92 
93 // @CddTest = 6.1/C-0-2
TEST(counter,start_stop_multiple_times)94 TEST(counter, start_stop_multiple_times) {
95   const size_t TEST_COUNT = 10;
96   std::unique_ptr<PerfEventSet> perf(
97       PerfEventSet::CreateInstance(PerfEventSet::Type::kPerfForCounting));
98   ASSERT_TRUE(perf);
99   ASSERT_TRUE(perf->AddEvent("cpu-cycles"));
100   ASSERT_TRUE(perf->MonitorCurrentProcess());
101   Counter prev_counter;
102   for (size_t i = 0; i < TEST_COUNT; ++i) {
103     ASSERT_TRUE(perf->StartCounters());
104     DoSomeWork();
105     ASSERT_TRUE(perf->StopCounters());
106     std::vector<Counter> counters;
107     ASSERT_TRUE(perf->ReadCounters(&counters));
108     ASSERT_EQ(counters.size(), 1u);
109     ASSERT_EQ(counters[0].event, "cpu-cycles");
110     ASSERT_GT(counters[0].value, 0u);
111     ASSERT_GT(counters[0].time_enabled_in_ns, 0u);
112     ASSERT_GT(counters[0].time_running_in_ns, 0u);
113     ASSERT_LE(counters[0].time_running_in_ns, counters[0].time_enabled_in_ns);
114     if (i > 0u) {
115       ASSERT_GT(counters[0].value, prev_counter.value);
116       ASSERT_GT(counters[0].time_enabled_in_ns, prev_counter.time_enabled_in_ns);
117       ASSERT_GT(counters[0].time_running_in_ns, prev_counter.time_running_in_ns);
118     }
119     prev_counter = counters[0];
120   }
121 }
122 
123 // @CddTest = 6.1/C-0-2
TEST(counter,no_change_after_stop)124 TEST(counter, no_change_after_stop) {
125   std::unique_ptr<PerfEventSet> perf(
126       PerfEventSet::CreateInstance(PerfEventSet::Type::kPerfForCounting));
127   ASSERT_TRUE(perf);
128   ASSERT_TRUE(perf->AddEvent("cpu-cycles"));
129   ASSERT_TRUE(perf->MonitorCurrentProcess());
130   ASSERT_TRUE(perf->StartCounters());
131   DoSomeWork();
132   ASSERT_TRUE(perf->StopCounters());
133   std::vector<Counter> counters;
134   ASSERT_TRUE(perf->ReadCounters(&counters));
135   ASSERT_EQ(counters.size(), 1u);
136   ASSERT_EQ(counters[0].event, "cpu-cycles");
137   ASSERT_GT(counters[0].value, 0u);
138   ASSERT_GT(counters[0].time_enabled_in_ns, 0u);
139   ASSERT_GT(counters[0].time_running_in_ns, 0u);
140   ASSERT_LE(counters[0].time_running_in_ns, counters[0].time_enabled_in_ns);
141   Counter prev_counter = counters[0];
142   DoSomeWork();
143   ASSERT_TRUE(perf->ReadCounters(&counters));
144   ASSERT_EQ(counters.size(), 1u);
145   ASSERT_EQ(counters[0].value, prev_counter.value);
146   ASSERT_EQ(counters[0].time_enabled_in_ns, prev_counter.time_enabled_in_ns);
147   ASSERT_EQ(counters[0].time_running_in_ns, prev_counter.time_running_in_ns);
148 }
149