1 /*
2  * Copyright (C) 2016 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/file.h>
20 
21 #include "command.h"
22 #include "get_test_data.h"
23 
24 using namespace simpleperf;
25 
ReportSampleCmd()26 static std::unique_ptr<Command> ReportSampleCmd() {
27   return CreateCommandInstance("report-sample");
28 }
29 
TEST(cmd_report_sample,text)30 TEST(cmd_report_sample, text) {
31   ASSERT_TRUE(
32       ReportSampleCmd()->Run({"-i", GetTestData(PERF_DATA_WITH_SYMBOLS)}));
33 }
34 
TEST(cmd_report_sample,output_option)35 TEST(cmd_report_sample, output_option) {
36   TemporaryFile tmpfile;
37   ASSERT_TRUE(ReportSampleCmd()->Run(
38       {"-i", GetTestData(PERF_DATA_WITH_SYMBOLS), "-o", tmpfile.path}));
39 }
40 
TEST(cmd_report_sample,show_callchain_option)41 TEST(cmd_report_sample, show_callchain_option) {
42   TemporaryFile tmpfile;
43   ASSERT_TRUE(ReportSampleCmd()->Run({"-i", GetTestData(CALLGRAPH_FP_PERF_DATA),
44                                       "-o", tmpfile.path, "--show-callchain"}));
45 }
46 
GetProtobufReport(const std::string & test_data_file,std::string * protobuf_report,const std::vector<std::string> & extra_args={})47 static void GetProtobufReport(const std::string& test_data_file, std::string* protobuf_report,
48                               const std::vector<std::string>& extra_args = {}) {
49   TemporaryFile tmpfile;
50   TemporaryFile tmpfile2;
51   std::vector<std::string> args = {"-i", GetTestData(test_data_file), "-o", tmpfile.path,
52                                    "--protobuf"};
53   args.insert(args.end(), extra_args.begin(), extra_args.end());
54   ASSERT_TRUE(ReportSampleCmd()->Run(args));
55   ASSERT_TRUE(ReportSampleCmd()->Run({"--dump-protobuf-report", tmpfile.path,
56                                       "-o", tmpfile2.path}));
57   ASSERT_TRUE(android::base::ReadFileToString(tmpfile2.path, protobuf_report));
58 }
59 
TEST(cmd_report_sample,protobuf_option)60 TEST(cmd_report_sample, protobuf_option) {
61   std::string data;
62   GetProtobufReport(PERF_DATA_WITH_SYMBOLS, &data);
63   ASSERT_NE(data.find("magic: SIMPLEPERF"), std::string::npos);
64   ASSERT_NE(data.find("version: 1"), std::string::npos);
65   ASSERT_NE(data.find("file:"), std::string::npos);
66 }
67 
TEST(cmd_report_sample,no_skipped_file_id)68 TEST(cmd_report_sample, no_skipped_file_id) {
69   std::string data;
70   GetProtobufReport(PERF_DATA_WITH_WRONG_IP_IN_CALLCHAIN, &data);
71   // If wrong ips in callchain are omitted, "unknown" file path will not be generated.
72   ASSERT_EQ(data.find("unknown"), std::string::npos);
73 }
74 
TEST(cmd_report_sample,sample_has_event_count)75 TEST(cmd_report_sample, sample_has_event_count) {
76   std::string data;
77   GetProtobufReport(PERF_DATA_WITH_SYMBOLS, &data);
78   ASSERT_NE(data.find("event_count:"), std::string::npos);
79 }
80 
TEST(cmd_report_sample,has_thread_record)81 TEST(cmd_report_sample, has_thread_record) {
82   std::string data;
83   GetProtobufReport(PERF_DATA_WITH_SYMBOLS, &data);
84   ASSERT_NE(data.find("thread:"), std::string::npos);
85   ASSERT_NE(data.find("thread_name: t2"), std::string::npos);
86 }
87 
TEST(cmd_report_sample,trace_offcpu)88 TEST(cmd_report_sample, trace_offcpu) {
89   std::string data;
90   GetProtobufReport(PERF_DATA_WITH_TRACE_OFFCPU, &data);
91   ASSERT_NE(data.find("event_type: sched:sched_switch"), std::string::npos);
92 }
93 
TEST(cmd_report_sample,have_clear_callchain_end_in_protobuf_output)94 TEST(cmd_report_sample, have_clear_callchain_end_in_protobuf_output) {
95   std::string data;
96   GetProtobufReport(PERF_DATA_WITH_TRACE_OFFCPU, &data, {"--show-callchain"});
97   ASSERT_NE(data.find("__libc_init"), std::string::npos);
98   ASSERT_EQ(data.find("_start_main"), std::string::npos);
99 }
100 
TEST(cmd_report_sample,app_package_name_in_meta_info)101 TEST(cmd_report_sample, app_package_name_in_meta_info) {
102   std::string data;
103   GetProtobufReport(PERF_DATA_WITH_APP_PACKAGE_NAME, &data);
104   ASSERT_NE(data.find("app_package_name: com.google.sample.tunnel"), std::string::npos);
105 }
106 
TEST(cmd_report_sample,remove_unknown_kernel_symbols)107 TEST(cmd_report_sample, remove_unknown_kernel_symbols) {
108   std::string data;
109   // Test --remove-unknown-kernel-symbols on perf.data with kernel_symbols_available=false.
110   GetProtobufReport(PERF_DATA_WITH_KERNEL_SYMBOLS_AVAILABLE_FALSE, &data,
111                     {"--show-callchain"});
112   ASSERT_NE(data.find("time: 1368182962424044"), std::string::npos);
113   ASSERT_NE(data.find("path: [kernel.kallsyms]"), std::string::npos);
114   ASSERT_NE(data.find("path: /system/lib64/libc.so"), std::string::npos);
115   GetProtobufReport(PERF_DATA_WITH_KERNEL_SYMBOLS_AVAILABLE_FALSE, &data,
116                     {"--show-callchain", "--remove-unknown-kernel-symbols"});
117   // The sample dumped at time 1368182962424044 shouldn't be removed. Because it has user space
118   // callchains.
119   ASSERT_NE(data.find("time: 1368182962424044"), std::string::npos);
120   // Kernel callchains shouldn't be removed.
121   ASSERT_EQ(data.find("path: [kernel.kallsyms]"), std::string::npos);
122   // User space callchains still exist.
123   ASSERT_NE(data.find("path: /system/lib64/libc.so"), std::string::npos);
124 
125   // Test --remove-unknown-kernel-symbols on perf.data with kernel_symbols_available=true.
126   GetProtobufReport(PERF_DATA_WITH_KERNEL_SYMBOLS_AVAILABLE_TRUE, &data,
127                     {"--show-callchain"});
128   ASSERT_NE(data.find("time: 1368297633794862"), std::string::npos);
129   ASSERT_NE(data.find("path: [kernel.kallsyms]"), std::string::npos);
130   ASSERT_NE(data.find("symbol: binder_ioctl_write_read"), std::string::npos);
131   ASSERT_NE(data.find("path: /system/lib64/libc.so"), std::string::npos);
132   GetProtobufReport(PERF_DATA_WITH_KERNEL_SYMBOLS_AVAILABLE_TRUE, &data,
133                     {"--show-callchain", "--remove-unknown-kernel-symbols"});
134   ASSERT_NE(data.find("time: 1368297633794862"), std::string::npos);
135   ASSERT_NE(data.find("path: [kernel.kallsyms]"), std::string::npos);
136   ASSERT_NE(data.find("symbol: binder_ioctl_write_read"), std::string::npos);
137   ASSERT_NE(data.find("path: /system/lib64/libc.so"), std::string::npos);
138 }
139 
TEST(cmd_report_sample,show_art_frames_option)140 TEST(cmd_report_sample, show_art_frames_option) {
141   std::string data;
142   GetProtobufReport(PERF_DATA_WITH_INTERPRETER_FRAMES, &data, {"--show-callchain"});
143   ASSERT_EQ(data.find("artMterpAsmInstructionStart"), std::string::npos);
144   GetProtobufReport(PERF_DATA_WITH_INTERPRETER_FRAMES, &data,
145                     {"--show-callchain", "--show-art-frames"});
146   ASSERT_NE(data.find("artMterpAsmInstructionStart"), std::string::npos);
147 }
148 
TEST(cmd_report_sample,show_symbols_before_and_after_demangle)149 TEST(cmd_report_sample, show_symbols_before_and_after_demangle) {
150   std::string data;
151   GetProtobufReport(PERF_DATA_WITH_INTERPRETER_FRAMES, &data, {"--show-callchain"});
152   ASSERT_NE(data.find("symbol: android::hardware::IPCThreadState::talkWithDriver(bool)"),
153             std::string::npos);
154   ASSERT_NE(data.find("mangled_symbol: _ZN7android8hardware14IPCThreadState14talkWithDriverEb"),
155             std::string::npos);
156 }
157 
TEST(cmd_report_sample,symdir_option)158 TEST(cmd_report_sample, symdir_option) {
159   std::string data;
160   GetProtobufReport(PERF_DATA_FOR_BUILD_ID_CHECK, &data);
161   ASSERT_EQ(data.find("symbol: main"), std::string::npos);
162   GetProtobufReport(PERF_DATA_FOR_BUILD_ID_CHECK, &data,
163                     {"--symdir", GetTestDataDir() + CORRECT_SYMFS_FOR_BUILD_ID_CHECK});
164   ASSERT_NE(data.find("symbol: main"), std::string::npos);
165 }
166