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 auto workload = Workload::CreateWorkload({"sleep", "1"});
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 =
71 android::base::StringPrintf("%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
72 ASSERT_TRUE(StatCmd()->Run({"-p", pid_list}));
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 =
80 android::base::StringPrintf("%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
81 ASSERT_TRUE(StatCmd()->Run({"-t", tid_list}));
84 TEST(stat_cmd, no_monitored_threads) {
85 ASSERT_FALSE(StatCmd()->Run({""}));
88 TEST(stat_cmd, cpu_option) {
89 ASSERT_TRUE(StatCmd()->Run({"--cpu", "0", "sleep", "1"}));
91 ASSERT_TRUE(StatCmd()->Run({"--cpu", "0", "-a", "sleep", "1"}));