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 
TEST(monitor_cmd,no_options)62 TEST(monitor_cmd, no_options) {
63   std::string output;
64   ASSERT_FALSE(RunMonitorCmd({}, output));
65 }
66 
TEST(monitor_cmd,no_event)67 TEST(monitor_cmd, no_event) {
68   ASSERT_FALSE(MonitorCmd()->Run({"-a", "--duration", "1"}));
69 }
70 
TEST(monitor_cmd,global)71 TEST(monitor_cmd, global) {
72   TEST_REQUIRE_ROOT();
73   std::string output;
74   ASSERT_TRUE(RunMonitorCmd({"-a"}, output));
75   ASSERT_GT(output.size(), 0);
76 }
77 
TEST(monitor_cmd,no_perf)78 TEST(monitor_cmd, no_perf) {
79   TEST_REQUIRE_ROOT();
80   std::string output;
81   ASSERT_TRUE(RunMonitorCmd({"-a", "--exclude-perf"}, output));
82   ASSERT_GT(output.size(), 0);
83 }
84 
TEST(monitor_cmd,with_callchain)85 TEST(monitor_cmd, with_callchain) {
86   TEST_REQUIRE_ROOT();
87   std::string output;
88   ASSERT_TRUE(RunMonitorCmd({"-a", "-g"}, output));
89   ASSERT_GT(output.size(), 0);
90 }
91 
TEST(monitor_cmd,with_callchain_fp)92 TEST(monitor_cmd, with_callchain_fp) {
93   TEST_REQUIRE_ROOT();
94   std::string output;
95   ASSERT_TRUE(RunMonitorCmd({"-a", "--call-graph", "fp"}, output));
96   ASSERT_GT(output.size(), 0);
97 }
98 
TEST(monitor_cmd,with_callchain_dwarf)99 TEST(monitor_cmd, with_callchain_dwarf) {
100   TEST_REQUIRE_ROOT();
101   std::string output;
102   ASSERT_TRUE(RunMonitorCmd({"-a", "--call-graph", "dwarf,512"}, output));
103   ASSERT_GT(output.size(), 0);
104 }
105 
TEST(monitor_cmd,frequency)106 TEST(monitor_cmd, frequency) {
107   TEST_REQUIRE_ROOT();
108   std::string output;
109   ASSERT_TRUE(RunMonitorCmd({"-a", "-f", "1"}, output));
110 }
111 
TEST(monitor_cmd,count)112 TEST(monitor_cmd, count) {
113   TEST_REQUIRE_ROOT();
114   std::string output;
115   ASSERT_TRUE(RunMonitorCmd({"-a", "-c", "10000000"}, output));
116 }
117 
TEST(monitor_cmd,cpu_percent)118 TEST(monitor_cmd, cpu_percent) {
119   TEST_REQUIRE_ROOT();
120   std::string output;
121   ASSERT_TRUE(RunMonitorCmd({"-a", "--cpu-percent", "1"}, output));
122   ASSERT_GT(output.size(), 0);
123   ASSERT_FALSE(RunMonitorCmd({"-a", "--cpu-percent", "-1"}, output));
124   ASSERT_FALSE(RunMonitorCmd({"-a", "--cpu-percent", "101"}, output));
125 }
126 
TEST(monitor_cmd,record_filter_options)127 TEST(monitor_cmd, record_filter_options) {
128   TEST_REQUIRE_ROOT();
129   std::string output;
130   ASSERT_TRUE(
131       RunMonitorCmd({"-a", "--exclude-pid", "1,2", "--exclude-tid", "3,4", "--exclude-process-name",
132                      "processA", "--exclude-thread-name", "threadA", "--exclude-uid", "5,6"},
133                     output));
134   ASSERT_TRUE(
135       RunMonitorCmd({"-a", "--include-pid", "1,2", "--include-tid", "3,4", "--include-process-name",
136                      "processB", "--include-thread-name", "threadB", "--include-uid", "5,6"},
137                     output));
138 }
139