• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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/file.h>
20 #include <android-base/stringprintf.h>
21 #include <android-base/test_utils.h>
22 
23 #include <thread>
24 
25 #include "command.h"
26 #include "environment.h"
27 #include "get_test_data.h"
28 #include "test_util.h"
29 
StatCmd()30 static std::unique_ptr<Command> StatCmd() {
31   return CreateCommandInstance("stat");
32 }
33 
TEST(stat_cmd,no_options)34 TEST(stat_cmd, no_options) { ASSERT_TRUE(StatCmd()->Run({"sleep", "1"})); }
35 
TEST(stat_cmd,event_option)36 TEST(stat_cmd, event_option) {
37   ASSERT_TRUE(StatCmd()->Run({"-e", "cpu-clock,task-clock", "sleep", "1"}));
38 }
39 
TEST(stat_cmd,system_wide_option)40 TEST(stat_cmd, system_wide_option) {
41   TEST_IN_ROOT(ASSERT_TRUE(StatCmd()->Run({"-a", "sleep", "1"})));
42 }
43 
TEST(stat_cmd,verbose_option)44 TEST(stat_cmd, verbose_option) {
45   ASSERT_TRUE(StatCmd()->Run({"--verbose", "sleep", "1"}));
46 }
47 
TEST(stat_cmd,tracepoint_event)48 TEST(stat_cmd, tracepoint_event) {
49   TEST_IN_ROOT(ASSERT_TRUE(
50       StatCmd()->Run({"-a", "-e", "sched:sched_switch", "sleep", "1"})));
51 }
52 
TEST(stat_cmd,event_modifier)53 TEST(stat_cmd, event_modifier) {
54   ASSERT_TRUE(
55       StatCmd()->Run({"-e", "cpu-cycles:u,cpu-cycles:k", "sleep", "1"}));
56 }
57 
RunWorkloadFunction()58 void RunWorkloadFunction() {
59   while (true) {
60     for (volatile int i = 0; i < 10000; ++i);
61     usleep(1);
62   }
63 }
64 
CreateProcesses(size_t count,std::vector<std::unique_ptr<Workload>> * workloads)65 void CreateProcesses(size_t count,
66                      std::vector<std::unique_ptr<Workload>>* workloads) {
67   workloads->clear();
68   // Create workloads run longer than profiling time.
69   for (size_t i = 0; i < count; ++i) {
70     std::unique_ptr<Workload> workload;
71     if (GetDefaultAppPackageName().empty()) {
72       workload = Workload::CreateWorkload(RunWorkloadFunction);
73     } else {
74       workload = Workload::CreateWorkload({"run-as", GetDefaultAppPackageName(), "./workload"});
75       workload->SetKillFunction([](pid_t pid) {
76         Workload::RunCmd({"run-as", GetDefaultAppPackageName(), "kill", std::to_string(pid)});
77       });
78     }
79     ASSERT_TRUE(workload != nullptr);
80     ASSERT_TRUE(workload->Start());
81     workloads->push_back(std::move(workload));
82   }
83 }
84 
TEST(stat_cmd,existing_processes)85 TEST(stat_cmd, existing_processes) {
86   std::vector<std::unique_ptr<Workload>> workloads;
87   CreateProcesses(2, &workloads);
88   std::string pid_list = android::base::StringPrintf(
89       "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
90   ASSERT_TRUE(StatCmd()->Run({"-p", pid_list, "sleep", "1"}));
91 }
92 
TEST(stat_cmd,existing_threads)93 TEST(stat_cmd, existing_threads) {
94   std::vector<std::unique_ptr<Workload>> workloads;
95   CreateProcesses(2, &workloads);
96   // Process id can be used as thread id in linux.
97   std::string tid_list = android::base::StringPrintf(
98       "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
99   ASSERT_TRUE(StatCmd()->Run({"-t", tid_list, "sleep", "1"}));
100 }
101 
TEST(stat_cmd,no_monitored_threads)102 TEST(stat_cmd, no_monitored_threads) { ASSERT_FALSE(StatCmd()->Run({""})); }
103 
TEST(stat_cmd,group_option)104 TEST(stat_cmd, group_option) {
105   ASSERT_TRUE(
106       StatCmd()->Run({"--group", "cpu-clock,page-faults", "sleep", "1"}));
107   ASSERT_TRUE(StatCmd()->Run({"--group", "cpu-cycles,instructions", "--group",
108                               "cpu-cycles:u,instructions:u", "--group",
109                               "cpu-cycles:k,instructions:k", "sleep", "1"}));
110 }
111 
TEST(stat_cmd,auto_generated_summary)112 TEST(stat_cmd, auto_generated_summary) {
113   TemporaryFile tmp_file;
114   ASSERT_TRUE(StatCmd()->Run({"--group", "instructions:u,instructions:k", "-o",
115                               tmp_file.path, "sleep", "1"}));
116   std::string s;
117   ASSERT_TRUE(android::base::ReadFileToString(tmp_file.path, &s));
118   size_t pos = s.find("instructions:u");
119   ASSERT_NE(s.npos, pos);
120   pos = s.find("instructions:k", pos);
121   ASSERT_NE(s.npos, pos);
122   pos += strlen("instructions:k");
123   // Check if the summary of instructions is generated.
124   ASSERT_NE(s.npos, s.find("instructions", pos));
125 }
126 
TEST(stat_cmd,duration_option)127 TEST(stat_cmd, duration_option) {
128   ASSERT_TRUE(
129       StatCmd()->Run({"--duration", "1.2", "-p", std::to_string(getpid()), "--in-app"}));
130   ASSERT_TRUE(StatCmd()->Run({"--duration", "1", "sleep", "2"}));
131 }
132 
TEST(stat_cmd,interval_option)133 TEST(stat_cmd, interval_option) {
134   TemporaryFile tmp_file;
135   ASSERT_TRUE(
136     StatCmd()->Run({"--interval", "500.0", "--duration", "1.2", "-o",
137           tmp_file.path, "sleep", "2"}));
138   std::string s;
139   ASSERT_TRUE(android::base::ReadFileToString(tmp_file.path, &s));
140   size_t count = 0;
141   size_t pos = 0;
142   std::string subs = "statistics:";
143   while((pos = s.find(subs, pos)) != s.npos) {
144     pos += subs.size();
145     ++count ;
146   }
147   ASSERT_EQ(count, 3UL);
148 }
149 
TEST(stat_cmd,no_modifier_for_clock_events)150 TEST(stat_cmd, no_modifier_for_clock_events) {
151   for (const std::string& e : {"cpu-clock", "task-clock"}) {
152     for (const std::string& m : {"u", "k"}) {
153       ASSERT_FALSE(StatCmd()->Run({"-e", e + ":" + m, "sleep", "0.1"}))
154           << "event " << e << ":" << m;
155     }
156   }
157 }
158 
TEST(stat_cmd,handle_SIGHUP)159 TEST(stat_cmd, handle_SIGHUP) {
160   std::thread thread([]() {
161     sleep(1);
162     kill(getpid(), SIGHUP);
163   });
164   thread.detach();
165   ASSERT_TRUE(StatCmd()->Run({"sleep", "1000000"}));
166 }
167 
TEST(stat_cmd,stop_when_no_more_targets)168 TEST(stat_cmd, stop_when_no_more_targets) {
169   std::atomic<int> tid(0);
170   std::thread thread([&]() {
171     tid = gettid();
172     sleep(1);
173   });
174   thread.detach();
175   while (tid == 0);
176   ASSERT_TRUE(StatCmd()->Run({"-t", std::to_string(tid), "--in-app"}));
177 }
178