xref: /aosp_15_r20/system/extras/simpleperf/cmd_monitor_test.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1 /*
2  * Copyright (C) 2020 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 <android-base/strings.h>
20 #if defined(__ANDROID__)
21 #include <android-base/properties.h>
22 #endif
23 
24 #include <vector>
25 
26 #include "command.h"
27 #include "test_util.h"
28 
29 using namespace simpleperf;
30 
MonitorCmd()31 static std::unique_ptr<Command> MonitorCmd() {
32   return CreateCommandInstance("monitor");
33 }
34 
GetDefaultEvent()35 static const char* GetDefaultEvent() {
36   return HasHardwareCounter() ? "cpu-cycles" : "task-clock";
37 }
38 
RunMonitorCmd(std::vector<std::string> v,std::string & output)39 static ::testing::AssertionResult RunMonitorCmd(std::vector<std::string> v, std::string& output) {
40   bool has_event = false;
41   for (auto& arg : v) {
42     if (arg == "-e") {
43       has_event = true;
44       break;
45     }
46   }
47   if (!has_event) {
48     v.insert(v.end(), {"-e", GetDefaultEvent()});
49   }
50 
51   v.insert(v.end(), {"--duration", SLEEP_SEC});
52 
53   CaptureStdout capture;
54   if (!capture.Start()) {
55     return ::testing::AssertionFailure() << "Unable to capture stdout";
56   }
57   auto result = MonitorCmd()->Run(v);
58   output.append(capture.Finish());
59   return (result ? ::testing::AssertionSuccess() : ::testing::AssertionFailure());
60 }
61 
62 // @CddTest = 6.1/C-0-2
TEST(monitor_cmd,no_options)63 TEST(monitor_cmd, no_options) {
64   std::string output;
65   ASSERT_FALSE(RunMonitorCmd({}, output));
66 }
67 
68 // @CddTest = 6.1/C-0-2
TEST(monitor_cmd,no_event)69 TEST(monitor_cmd, no_event) {
70   ASSERT_FALSE(MonitorCmd()->Run({"-a", "--duration", "1"}));
71 }
72 
73 // @CddTest = 6.1/C-0-2
TEST(monitor_cmd,global)74 TEST(monitor_cmd, global) {
75   TEST_REQUIRE_ROOT();
76   std::string output;
77   ASSERT_TRUE(RunMonitorCmd({"-a"}, output));
78   ASSERT_GT(output.size(), 0);
79 }
80 
81 // @CddTest = 6.1/C-0-2
TEST(monitor_cmd,no_perf)82 TEST(monitor_cmd, no_perf) {
83   TEST_REQUIRE_ROOT();
84   std::string output;
85   ASSERT_TRUE(RunMonitorCmd({"-a", "--exclude-perf"}, output));
86   ASSERT_GT(output.size(), 0);
87 }
88 
89 // @CddTest = 6.1/C-0-2
TEST(monitor_cmd,with_callchain)90 TEST(monitor_cmd, with_callchain) {
91   TEST_REQUIRE_ROOT();
92   std::string output;
93   ASSERT_TRUE(RunMonitorCmd({"-a", "-g"}, output));
94   ASSERT_GT(output.size(), 0);
95 }
96 
97 // @CddTest = 6.1/C-0-2
TEST(monitor_cmd,with_callchain_fp)98 TEST(monitor_cmd, with_callchain_fp) {
99   TEST_REQUIRE_ROOT();
100   std::string output;
101   ASSERT_TRUE(RunMonitorCmd({"-a", "--call-graph", "fp"}, output));
102   ASSERT_GT(output.size(), 0);
103 }
104 
105 // @CddTest = 6.1/C-0-2
TEST(monitor_cmd,with_callchain_dwarf)106 TEST(monitor_cmd, with_callchain_dwarf) {
107   TEST_REQUIRE_ROOT();
108   std::string output;
109   ASSERT_TRUE(RunMonitorCmd({"-a", "--call-graph", "dwarf,512"}, output));
110   ASSERT_GT(output.size(), 0);
111 }
112 
113 // @CddTest = 6.1/C-0-2
TEST(monitor_cmd,frequency)114 TEST(monitor_cmd, frequency) {
115   TEST_REQUIRE_ROOT();
116   std::string output;
117   ASSERT_TRUE(RunMonitorCmd({"-a", "-f", "1"}, output));
118 }
119 
120 // @CddTest = 6.1/C-0-2
TEST(monitor_cmd,count)121 TEST(monitor_cmd, count) {
122   TEST_REQUIRE_ROOT();
123   std::string output;
124   ASSERT_TRUE(RunMonitorCmd({"-a", "-c", "10000000"}, output));
125 }
126 
127 // @CddTest = 6.1/C-0-2
TEST(monitor_cmd,cpu_percent)128 TEST(monitor_cmd, cpu_percent) {
129   TEST_REQUIRE_ROOT();
130   std::string output;
131   ASSERT_TRUE(RunMonitorCmd({"-a", "--cpu-percent", "1"}, output));
132   ASSERT_GT(output.size(), 0);
133   ASSERT_FALSE(RunMonitorCmd({"-a", "--cpu-percent", "-1"}, output));
134   ASSERT_FALSE(RunMonitorCmd({"-a", "--cpu-percent", "101"}, output));
135 }
136 
137 // @CddTest = 6.1/C-0-2
TEST(monitor_cmd,record_filter_options)138 TEST(monitor_cmd, record_filter_options) {
139   TEST_REQUIRE_ROOT();
140   std::string output;
141   ASSERT_TRUE(
142       RunMonitorCmd({"-a", "--exclude-pid", "1,2", "--exclude-tid", "3,4", "--exclude-process-name",
143                      "processA", "--exclude-thread-name", "threadA", "--exclude-uid", "5,6"},
144                     output));
145   ASSERT_TRUE(
146       RunMonitorCmd({"-a", "--include-pid", "1,2", "--include-tid", "3,4", "--include-process-name",
147                      "processB", "--include-thread-name", "threadB", "--include-uid", "5,6"},
148                     output));
149 }
150