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/stringprintf.h>
22 #include "get_test_data.h"
23 #include "test_util.h"
25 static std::unique_ptr<Command> StatCmd() {
26 return CreateCommandInstance("stat");
29 TEST(stat_cmd, no_options) {
30 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) {
39 ASSERT_TRUE(StatCmd()->Run({"-a", "sleep", "1"}));
43 TEST(stat_cmd, verbose_option) {
44 ASSERT_TRUE(StatCmd()->Run({"--verbose", "sleep", "1"}));
47 TEST(stat_cmd, tracepoint_event) {
49 ASSERT_TRUE(StatCmd()->Run({"-a", "-e", "sched:sched_switch", "sleep", "1"}));
53 TEST(stat_cmd, event_modifier) {
54 ASSERT_TRUE(StatCmd()->Run({"-e", "cpu-cycles:u,cpu-cycles:k", "sleep", "1"}));
57 void CreateProcesses(size_t count, std::vector<std::unique_ptr<Workload>>* workloads) {
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));
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"}));
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"}));
85 TEST(stat_cmd, no_monitored_threads) {
86 ASSERT_FALSE(StatCmd()->Run({""}));
89 TEST(stat_cmd, cpu_option) {
90 ASSERT_TRUE(StatCmd()->Run({"--cpu", "0", "sleep", "1"}));
92 ASSERT_TRUE(StatCmd()->Run({"--cpu", "0", "-a", "sleep", "1"}));