OSDN Git Service

extras: remove su, we have our own
[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/stringprintf.h>
20
21 #include "command.h"
22 #include "get_test_data.h"
23 #include "test_util.h"
24
25 static std::unique_ptr<Command> StatCmd() {
26   return CreateCommandInstance("stat");
27 }
28
29 TEST(stat_cmd, no_options) {
30   ASSERT_TRUE(StatCmd()->Run({"sleep", "1"}));
31 }
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   if (IsRoot()) {
39     ASSERT_TRUE(StatCmd()->Run({"-a", "sleep", "1"}));
40   }
41 }
42
43 TEST(stat_cmd, verbose_option) {
44   ASSERT_TRUE(StatCmd()->Run({"--verbose", "sleep", "1"}));
45 }
46
47 TEST(stat_cmd, tracepoint_event) {
48   if (IsRoot()) {
49     ASSERT_TRUE(StatCmd()->Run({"-a", "-e", "sched:sched_switch", "sleep", "1"}));
50   }
51 }
52
53 TEST(stat_cmd, event_modifier) {
54   ASSERT_TRUE(StatCmd()->Run({"-e", "cpu-cycles:u,cpu-cycles:k", "sleep", "1"}));
55 }
56
57 void CreateProcesses(size_t count, std::vector<std::unique_ptr<Workload>>* workloads) {
58   workloads->clear();
59   for (size_t i = 0; i < count; ++i) {
60     // Create a workload runs longer than profiling time.
61     auto workload = Workload::CreateWorkload({"sleep", "1000"});
62     ASSERT_TRUE(workload != nullptr);
63     ASSERT_TRUE(workload->Start());
64     workloads->push_back(std::move(workload));
65   }
66 }
67
68 TEST(stat_cmd, existing_processes) {
69   std::vector<std::unique_ptr<Workload>> workloads;
70   CreateProcesses(2, &workloads);
71   std::string pid_list =
72       android::base::StringPrintf("%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
73   ASSERT_TRUE(StatCmd()->Run({"-p", pid_list, "sleep", "1"}));
74 }
75
76 TEST(stat_cmd, existing_threads) {
77   std::vector<std::unique_ptr<Workload>> workloads;
78   CreateProcesses(2, &workloads);
79   // Process id can be used as thread id in linux.
80   std::string tid_list =
81       android::base::StringPrintf("%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
82   ASSERT_TRUE(StatCmd()->Run({"-t", tid_list, "sleep", "1"}));
83 }
84
85 TEST(stat_cmd, no_monitored_threads) {
86   ASSERT_FALSE(StatCmd()->Run({""}));
87 }
88
89 TEST(stat_cmd, cpu_option) {
90   ASSERT_TRUE(StatCmd()->Run({"--cpu", "0", "sleep", "1"}));
91   if (IsRoot()) {
92     ASSERT_TRUE(StatCmd()->Run({"--cpu", "0", "-a", "sleep", "1"}));
93   }
94 }