OSDN Git Service

Merge "simpleperf: notify user for unsupported modifiers."
[android-x86/system-extras.git] / simpleperf / cmd_stat_test.cpp
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 "command.h"
24 #include "get_test_data.h"
25 #include "test_util.h"
26
27 static std::unique_ptr<Command> StatCmd() {
28   return CreateCommandInstance("stat");
29 }
30
31 TEST(stat_cmd, no_options) { ASSERT_TRUE(StatCmd()->Run({"sleep", "1"})); }
32
33 TEST(stat_cmd, event_option) {
34   ASSERT_TRUE(StatCmd()->Run({"-e", "cpu-clock,task-clock", "sleep", "1"}));
35 }
36
37 TEST(stat_cmd, system_wide_option) {
38   TEST_IN_ROOT(ASSERT_TRUE(StatCmd()->Run({"-a", "sleep", "1"})));
39 }
40
41 TEST(stat_cmd, verbose_option) {
42   ASSERT_TRUE(StatCmd()->Run({"--verbose", "sleep", "1"}));
43 }
44
45 TEST(stat_cmd, tracepoint_event) {
46   TEST_IN_ROOT(ASSERT_TRUE(
47       StatCmd()->Run({"-a", "-e", "sched:sched_switch", "sleep", "1"})));
48 }
49
50 TEST(stat_cmd, event_modifier) {
51   ASSERT_TRUE(
52       StatCmd()->Run({"-e", "cpu-cycles:u,cpu-cycles:k", "sleep", "1"}));
53 }
54
55 void CreateProcesses(size_t count,
56                      std::vector<std::unique_ptr<Workload>>* workloads) {
57   workloads->clear();
58   for (size_t i = 0; i < count; ++i) {
59     // Create a workload runs longer than profiling time.
60     auto workload = Workload::CreateWorkload({"sleep", "1000"});
61     ASSERT_TRUE(workload != nullptr);
62     ASSERT_TRUE(workload->Start());
63     workloads->push_back(std::move(workload));
64   }
65 }
66
67 TEST(stat_cmd, existing_processes) {
68   std::vector<std::unique_ptr<Workload>> workloads;
69   CreateProcesses(2, &workloads);
70   std::string pid_list = android::base::StringPrintf(
71       "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
72   ASSERT_TRUE(StatCmd()->Run({"-p", pid_list, "sleep", "1"}));
73 }
74
75 TEST(stat_cmd, existing_threads) {
76   std::vector<std::unique_ptr<Workload>> workloads;
77   CreateProcesses(2, &workloads);
78   // Process id can be used as thread id in linux.
79   std::string tid_list = android::base::StringPrintf(
80       "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
81   ASSERT_TRUE(StatCmd()->Run({"-t", tid_list, "sleep", "1"}));
82 }
83
84 TEST(stat_cmd, no_monitored_threads) { ASSERT_FALSE(StatCmd()->Run({""})); }
85
86 TEST(stat_cmd, group_option) {
87   ASSERT_TRUE(
88       StatCmd()->Run({"--group", "cpu-cycles,cpu-clock", "sleep", "1"}));
89   ASSERT_TRUE(StatCmd()->Run({"--group", "cpu-cycles,instructions", "--group",
90                               "cpu-cycles:u,instructions:u", "--group",
91                               "cpu-cycles:k,instructions:k", "sleep", "1"}));
92 }
93
94 TEST(stat_cmd, auto_generated_summary) {
95   TemporaryFile tmp_file;
96   ASSERT_TRUE(StatCmd()->Run({"--group", "instructions:u,instructions:k", "-o",
97                               tmp_file.path, "sleep", "1"}));
98   std::string s;
99   ASSERT_TRUE(android::base::ReadFileToString(tmp_file.path, &s));
100   size_t pos = s.find("instructions:u");
101   ASSERT_NE(s.npos, pos);
102   pos = s.find("instructions:k", pos);
103   ASSERT_NE(s.npos, pos);
104   pos += strlen("instructions:k");
105   // Check if the summary of instructions is generated.
106   ASSERT_NE(s.npos, s.find("instructions", pos));
107 }
108
109 TEST(stat_cmd, duration_option) {
110   ASSERT_TRUE(
111       StatCmd()->Run({"--duration", "1.2", "-p", std::to_string(getpid())}));
112   ASSERT_TRUE(StatCmd()->Run({"--duration", "1", "sleep", "2"}));
113 }
114
115 TEST(stat_cmd, no_modifier_for_clock_events) {
116   for (const std::string& e : {"cpu-clock", "task-clock"}) {
117     for (const std::string& m : {"u", "k"}) {
118       ASSERT_FALSE(StatCmd()->Run({"-e", e + ":" + m, "sleep", "0.1"}))
119           << "event " << e << ":" << m;
120     }
121   }
122 }