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 #include <android-base/test_utils.h> 21 22 #include "command.h" 23 #include "get_test_data.h" 24 25 static std::unique_ptr<Command> ReportSampleCmd() { 26 return CreateCommandInstance("report-sample"); 27 } 28 29 TEST(cmd_report_sample, text) { 30 ASSERT_TRUE( 31 ReportSampleCmd()->Run({"-i", GetTestData(PERF_DATA_WITH_SYMBOLS)})); 32 } 33 34 TEST(cmd_report_sample, output_option) { 35 TemporaryFile tmpfile; 36 ASSERT_TRUE(ReportSampleCmd()->Run( 37 {"-i", GetTestData(PERF_DATA_WITH_SYMBOLS), "-o", tmpfile.path})); 38 } 39 40 TEST(cmd_report_sample, show_callchain_option) { 41 TemporaryFile tmpfile; 42 ASSERT_TRUE(ReportSampleCmd()->Run({"-i", GetTestData(CALLGRAPH_FP_PERF_DATA), 43 "-o", tmpfile.path, "--show-callchain"})); 44 } 45 46 TEST(cmd_report_sample, protobuf_option) { 47 TemporaryFile tmpfile; 48 TemporaryFile tmpfile2; 49 ASSERT_TRUE(ReportSampleCmd()->Run({"-i", GetTestData(PERF_DATA_WITH_SYMBOLS), 50 "-o", tmpfile.path, "--protobuf"})); 51 ASSERT_TRUE(ReportSampleCmd()->Run( 52 {"--dump-protobuf-report", tmpfile.path, "-o", tmpfile2.path})); 53 std::string data; 54 ASSERT_TRUE(android::base::ReadFileToString(tmpfile2.path, &data)); 55 ASSERT_NE(data.find("file:"), std::string::npos); 56 } 57 58 TEST(cmd_report_sample, no_skipped_file_id) { 59 TemporaryFile tmpfile; 60 TemporaryFile tmpfile2; 61 ASSERT_TRUE(ReportSampleCmd()->Run({"-i", GetTestData(PERF_DATA_WITH_WRONG_IP_IN_CALLCHAIN), 62 "-o", tmpfile.path, "--protobuf"})); 63 ASSERT_TRUE(ReportSampleCmd()->Run({"--dump-protobuf-report", tmpfile.path, "-o", 64 tmpfile2.path})); 65 // If wrong ips in callchain are omitted, "unknown" file path will not be generated. 66 std::string data; 67 ASSERT_TRUE(android::base::ReadFileToString(tmpfile2.path, &data)); 68 ASSERT_EQ(data.find("unknown"), std::string::npos); 69 } 70 71 TEST(cmd_report_sample, sample_has_event_count) { 72 TemporaryFile tmpfile; 73 TemporaryFile tmpfile2; 74 ASSERT_TRUE(ReportSampleCmd()->Run({"-i", GetTestData(PERF_DATA_WITH_SYMBOLS), 75 "-o", tmpfile.path, "--protobuf"})); 76 ASSERT_TRUE(ReportSampleCmd()->Run( 77 {"--dump-protobuf-report", tmpfile.path, "-o", tmpfile2.path})); 78 std::string data; 79 ASSERT_TRUE(android::base::ReadFileToString(tmpfile2.path, &data)); 80 ASSERT_NE(data.find("event_count:"), std::string::npos); 81 } 82 83 TEST(cmd_report_sample, has_thread_record) { 84 TemporaryFile tmpfile; 85 TemporaryFile tmpfile2; 86 ASSERT_TRUE(ReportSampleCmd()->Run({"-i", GetTestData(PERF_DATA_WITH_SYMBOLS), 87 "-o", tmpfile.path, "--protobuf"})); 88 ASSERT_TRUE(ReportSampleCmd()->Run( 89 {"--dump-protobuf-report", tmpfile.path, "-o", tmpfile2.path})); 90 std::string data; 91 ASSERT_TRUE(android::base::ReadFileToString(tmpfile2.path, &data)); 92 ASSERT_NE(data.find("thread:"), std::string::npos); 93 } 94 95 TEST(cmd_report_sample, trace_offcpu) { 96 TemporaryFile tmpfile; 97 TemporaryFile tmpfile2; 98 ASSERT_TRUE(ReportSampleCmd()->Run({"-i", GetTestData(PERF_DATA_WITH_TRACE_OFFCPU), 99 "-o", tmpfile.path, "--protobuf"})); 100 ASSERT_TRUE(ReportSampleCmd()->Run( 101 {"--dump-protobuf-report", tmpfile.path, "-o", tmpfile2.path})); 102 std::string data; 103 ASSERT_TRUE(android::base::ReadFileToString(tmpfile2.path, &data)); 104 ASSERT_NE(data.find("event_type: sched:sched_switch"), std::string::npos); 105 } 106 107 TEST(cmd_report_sample, have_clear_callchain_end_in_protobuf_output) { 108 TemporaryFile tmpfile; 109 TemporaryFile tmpfile2; 110 ASSERT_TRUE(ReportSampleCmd()->Run({"-i", GetTestData(PERF_DATA_WITH_TRACE_OFFCPU), 111 "--show-callchain", "-o", tmpfile.path, "--protobuf"})); 112 ASSERT_TRUE(ReportSampleCmd()->Run( 113 {"--dump-protobuf-report", tmpfile.path, "-o", tmpfile2.path})); 114 std::string data; 115 ASSERT_TRUE(android::base::ReadFileToString(tmpfile2.path, &data)); 116 ASSERT_NE(data.find("__libc_init"), std::string::npos); 117 ASSERT_EQ(data.find("_start_main"), std::string::npos); 118 } 119