2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
19 #include <android-base/file.h>
20 #include <android-base/stringprintf.h>
21 #include <android-base/test_utils.h>
24 #include "get_test_data.h"
25 #include "test_util.h"
27 static std::unique_ptr<Command> StatCmd() {
28 return CreateCommandInstance("stat");
31 TEST(stat_cmd, no_options) { ASSERT_TRUE(StatCmd()->Run({"sleep", "1"})); }
33 TEST(stat_cmd, event_option) {
34 ASSERT_TRUE(StatCmd()->Run({"-e", "cpu-clock,task-clock", "sleep", "1"}));
37 TEST(stat_cmd, system_wide_option) {
38 TEST_IN_ROOT(ASSERT_TRUE(StatCmd()->Run({"-a", "sleep", "1"})));
41 TEST(stat_cmd, verbose_option) {
42 ASSERT_TRUE(StatCmd()->Run({"--verbose", "sleep", "1"}));
45 TEST(stat_cmd, tracepoint_event) {
46 TEST_IN_ROOT(ASSERT_TRUE(
47 StatCmd()->Run({"-a", "-e", "sched:sched_switch", "sleep", "1"})));
50 TEST(stat_cmd, event_modifier) {
52 StatCmd()->Run({"-e", "cpu-cycles:u,cpu-cycles:k", "sleep", "1"}));
55 void CreateProcesses(size_t count,
56 std::vector<std::unique_ptr<Workload>>* workloads) {
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));
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"}));
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"}));
84 TEST(stat_cmd, no_monitored_threads) { ASSERT_FALSE(StatCmd()->Run({""})); }
86 TEST(stat_cmd, group_option) {
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"}));
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"}));
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));
109 TEST(stat_cmd, duration_option) {
111 StatCmd()->Run({"--duration", "1.2", "-p", std::to_string(getpid())}));
112 ASSERT_TRUE(StatCmd()->Run({"--duration", "1", "sleep", "2"}));
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;