1 /*
2  * Copyright (C) 2018 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 <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 
23 #include <android-base/file.h>
24 #include <android-base/stringprintf.h>
25 
26 #include <map>
27 #include <memory>
28 #include <thread>
29 
30 #include "command.h"
31 #include "environment.h"
32 #include "event_selection_set.h"
33 #include "get_test_data.h"
34 #include "record.h"
35 #include "record_file.h"
36 #include "test_util.h"
37 #include "thread_tree.h"
38 
39 using namespace PerfFileFormat;
40 
TraceSchedCmd()41 static std::unique_ptr<Command> TraceSchedCmd() {
42   return CreateCommandInstance("trace-sched");
43 }
44 
TEST(trace_sched_cmd,smoke)45 TEST(trace_sched_cmd, smoke) {
46   TEST_IN_ROOT({
47     ASSERT_TRUE(TraceSchedCmd()->Run({"--duration", "1"}));
48   });
49 }
50 
TEST(trace_sched_cmd,report_smoke)51 TEST(trace_sched_cmd, report_smoke) {
52   CaptureStdout capture;
53   ASSERT_TRUE(capture.Start());
54   ASSERT_TRUE(TraceSchedCmd()->Run({"--record-file", GetTestData(PERF_DATA_SCHED_STAT_RUNTIME),
55                                     "--show-threads"}));
56   std::string data = capture.Finish();
57   ASSERT_NE(data.find("Process  3845.961 ms  94.90%      8603  examplepurejava"),
58             std::string::npos);
59   ASSERT_NE(data.find("Thread   3845.961 ms  94.90%      8615  BusyThread"), std::string::npos);
60   ASSERT_NE(data.find("Detect 3 spin loops in process examplepurejava (8603) thread "
61                       "BusyThread (8615),\nmax rate at [326962.439095 s - 326963.442418 s], "
62                       "taken 997.813 ms / 1003.323 ms (99.45%)."), std::string::npos);
63 }
64