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/strings.h> 21 22 #include <memory> 23 24 #include "command.h" 25 #include "get_test_data.h" 26 #include "record.h" 27 #include "record_file.h" 28 #include "test_util.h" 29 30 static std::unique_ptr<Command> KmemCmd() { 31 return CreateCommandInstance("kmem"); 32 } 33 34 struct ReportResult { 35 bool success; 36 std::string content; 37 std::vector<std::string> lines; 38 }; 39 40 static void KmemReportRawFile(const std::string& perf_data, 41 const std::vector<std::string>& additional_args, 42 ReportResult* result) { 43 result->success = false; 44 TemporaryFile tmp_file; 45 std::vector<std::string> args = {"report", "-i", perf_data, "-o", 46 tmp_file.path}; 47 args.insert(args.end(), additional_args.begin(), additional_args.end()); 48 ASSERT_TRUE(KmemCmd()->Run(args)); 49 ASSERT_TRUE(android::base::ReadFileToString(tmp_file.path, &result->content)); 50 ASSERT_TRUE(!result->content.empty()); 51 std::vector<std::string> raw_lines = 52 android::base::Split(result->content, "\n"); 53 result->lines.clear(); 54 for (const auto& line : raw_lines) { 55 std::string s = android::base::Trim(line); 56 if (!s.empty()) { 57 result->lines.push_back(s); 58 } 59 } 60 ASSERT_GE(result->lines.size(), 2u); 61 result->success = true; 62 } 63 64 static void KmemReportFile(const std::string& perf_data, 65 const std::vector<std::string>& additional_args, 66 ReportResult* result) { 67 KmemReportRawFile(GetTestData(perf_data), additional_args, result); 68 } 69 70 #if defined(__linux__) 71 #include "environment.h" 72 73 static bool RunKmemRecordCmd(std::vector<std::string> v, 74 const char* output_file = nullptr) { 75 std::unique_ptr<TemporaryFile> tmpfile; 76 std::string out_file; 77 if (output_file != nullptr) { 78 out_file = output_file; 79 } else { 80 tmpfile.reset(new TemporaryFile); 81 out_file = tmpfile->path; 82 } 83 v.insert(v.begin(), "record"); 84 v.insert(v.end(), {"-o", out_file, "sleep", SLEEP_SEC}); 85 return KmemCmd()->Run(v); 86 } 87 88 TEST(kmem_cmd, record_slab) { 89 TEST_IN_ROOT(ASSERT_TRUE(RunKmemRecordCmd({"--slab"}))); 90 } 91 92 TEST(kmem_cmd, record_fp_callchain_sampling) { 93 TEST_IN_ROOT(ASSERT_TRUE(RunKmemRecordCmd({"--slab", "-g"}))); 94 TEST_IN_ROOT(ASSERT_TRUE(RunKmemRecordCmd({"--slab", "--call-graph", "fp"}))); 95 } 96 97 TEST(kmem_cmd, record_and_report) { 98 TemporaryFile tmp_file; 99 TEST_IN_ROOT({ 100 ASSERT_TRUE(RunKmemRecordCmd({"--slab"}, tmp_file.path)); 101 ReportResult result; 102 KmemReportRawFile(tmp_file.path, {}, &result); 103 ASSERT_TRUE(result.success); 104 }); 105 } 106 107 TEST(kmem_cmd, record_and_report_callgraph) { 108 TemporaryFile tmp_file; 109 TEST_IN_ROOT({ 110 ASSERT_TRUE(RunKmemRecordCmd({"--slab", "-g"}, tmp_file.path)); 111 ReportResult result; 112 KmemReportRawFile(tmp_file.path, {"-g"}, &result); 113 ASSERT_TRUE(result.success); 114 }); 115 } 116 117 #endif 118 119 TEST(kmem_cmd, report) { 120 ReportResult result; 121 KmemReportFile(PERF_DATA_WITH_KMEM_SLAB_CALLGRAPH_RECORD, {}, &result); 122 ASSERT_TRUE(result.success); 123 ASSERT_NE(result.content.find("kmem:kmalloc"), std::string::npos); 124 ASSERT_NE(result.content.find("__alloc_skb"), std::string::npos); 125 } 126 127 TEST(kmem_cmd, report_all_sort_options) { 128 ReportResult result; 129 KmemReportFile( 130 PERF_DATA_WITH_KMEM_SLAB_CALLGRAPH_RECORD, 131 {"--slab-sort", 132 "hit,caller,ptr,bytes_req,bytes_alloc,fragment,gfp_flags,pingpong"}, 133 &result); 134 ASSERT_TRUE(result.success); 135 ASSERT_NE(result.content.find("Ptr"), std::string::npos); 136 ASSERT_NE(result.content.find("GfpFlags"), std::string::npos); 137 } 138 139 TEST(kmem_cmd, report_callgraph) { 140 ReportResult result; 141 KmemReportFile(PERF_DATA_WITH_KMEM_SLAB_CALLGRAPH_RECORD, {"-g"}, &result); 142 ASSERT_TRUE(result.success); 143 ASSERT_NE(result.content.find("kmem:kmalloc"), std::string::npos); 144 ASSERT_NE(result.content.find("__alloc_skb"), std::string::npos); 145 ASSERT_NE(result.content.find("system_call_fastpath"), std::string::npos); 146 } 147