| Yabin Cui | 6965d42 | 2016-06-15 11:41:42 -0700 | [diff] [blame] | 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> |
| Yabin Cui | 6965d42 | 2016-06-15 11:41:42 -0700 | [diff] [blame] | 21 | |
| 22 | #include <memory> |
| 23 | |
| 24 | #include "command.h" |
| Yabin Cui | 6965d42 | 2016-06-15 11:41:42 -0700 | [diff] [blame] | 25 | #include "get_test_data.h" |
| 26 | #include "record.h" |
| 27 | #include "record_file.h" |
| 28 | #include "test_util.h" |
| 29 | |
| Yabin Cui | acbdb24 | 2020-07-07 15:56:34 -0700 | [diff] [blame] | 30 | using namespace simpleperf; |
| 31 | |
| Yabin Cui | 6965d42 | 2016-06-15 11:41:42 -0700 | [diff] [blame] | 32 | static std::unique_ptr<Command> KmemCmd() { |
| 33 | return CreateCommandInstance("kmem"); |
| 34 | } |
| 35 | |
| 36 | struct ReportResult { |
| 37 | bool success; |
| 38 | std::string content; |
| 39 | std::vector<std::string> lines; |
| 40 | }; |
| 41 | |
| 42 | static void KmemReportRawFile(const std::string& perf_data, |
| 43 | const std::vector<std::string>& additional_args, |
| 44 | ReportResult* result) { |
| 45 | result->success = false; |
| 46 | TemporaryFile tmp_file; |
| Yabin Cui | 01b38b8 | 2020-12-17 16:03:10 -0800 | [diff] [blame] | 47 | close(tmp_file.release()); |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 48 | std::vector<std::string> args = {"report", "-i", perf_data, "-o", tmp_file.path}; |
| Yabin Cui | 6965d42 | 2016-06-15 11:41:42 -0700 | [diff] [blame] | 49 | args.insert(args.end(), additional_args.begin(), additional_args.end()); |
| 50 | ASSERT_TRUE(KmemCmd()->Run(args)); |
| 51 | ASSERT_TRUE(android::base::ReadFileToString(tmp_file.path, &result->content)); |
| 52 | ASSERT_TRUE(!result->content.empty()); |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 53 | std::vector<std::string> raw_lines = android::base::Split(result->content, "\n"); |
| Yabin Cui | 6965d42 | 2016-06-15 11:41:42 -0700 | [diff] [blame] | 54 | result->lines.clear(); |
| 55 | for (const auto& line : raw_lines) { |
| 56 | std::string s = android::base::Trim(line); |
| 57 | if (!s.empty()) { |
| 58 | result->lines.push_back(s); |
| 59 | } |
| 60 | } |
| 61 | ASSERT_GE(result->lines.size(), 2u); |
| 62 | result->success = true; |
| 63 | } |
| 64 | |
| 65 | static void KmemReportFile(const std::string& perf_data, |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 66 | const std::vector<std::string>& additional_args, ReportResult* result) { |
| Yabin Cui | 6965d42 | 2016-06-15 11:41:42 -0700 | [diff] [blame] | 67 | KmemReportRawFile(GetTestData(perf_data), additional_args, result); |
| 68 | } |
| 69 | |
| 70 | #if defined(__linux__) |
| Yabin Cui | 2597ef0 | 2016-10-19 11:28:48 -0700 | [diff] [blame] | 71 | #include "environment.h" |
| Yabin Cui | 6965d42 | 2016-06-15 11:41:42 -0700 | [diff] [blame] | 72 | |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 73 | static bool RunKmemRecordCmd(std::vector<std::string> v, const char* output_file = nullptr) { |
| Yabin Cui | 6965d42 | 2016-06-15 11:41:42 -0700 | [diff] [blame] | 74 | std::unique_ptr<TemporaryFile> tmpfile; |
| 75 | std::string out_file; |
| 76 | if (output_file != nullptr) { |
| 77 | out_file = output_file; |
| 78 | } else { |
| 79 | tmpfile.reset(new TemporaryFile); |
| Yabin Cui | 01b38b8 | 2020-12-17 16:03:10 -0800 | [diff] [blame] | 80 | close(tmpfile->release()); |
| Yabin Cui | 6965d42 | 2016-06-15 11:41:42 -0700 | [diff] [blame] | 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 | |
| Yabin Cui | 648c3e4 | 2024-04-02 13:12:45 -0700 | [diff] [blame] | 88 | // @CddTest = 6.1/C-0-2 |
| Yabin Cui | 6965d42 | 2016-06-15 11:41:42 -0700 | [diff] [blame] | 89 | TEST(kmem_cmd, record_slab) { |
| 90 | TEST_IN_ROOT(ASSERT_TRUE(RunKmemRecordCmd({"--slab"}))); |
| 91 | } |
| 92 | |
| Yabin Cui | 648c3e4 | 2024-04-02 13:12:45 -0700 | [diff] [blame] | 93 | // @CddTest = 6.1/C-0-2 |
| Yabin Cui | 6965d42 | 2016-06-15 11:41:42 -0700 | [diff] [blame] | 94 | TEST(kmem_cmd, record_fp_callchain_sampling) { |
| 95 | TEST_IN_ROOT(ASSERT_TRUE(RunKmemRecordCmd({"--slab", "-g"}))); |
| 96 | TEST_IN_ROOT(ASSERT_TRUE(RunKmemRecordCmd({"--slab", "--call-graph", "fp"}))); |
| 97 | } |
| 98 | |
| Yabin Cui | 648c3e4 | 2024-04-02 13:12:45 -0700 | [diff] [blame] | 99 | // @CddTest = 6.1/C-0-2 |
| Yabin Cui | 6965d42 | 2016-06-15 11:41:42 -0700 | [diff] [blame] | 100 | TEST(kmem_cmd, record_and_report) { |
| 101 | TemporaryFile tmp_file; |
| 102 | TEST_IN_ROOT({ |
| 103 | ASSERT_TRUE(RunKmemRecordCmd({"--slab"}, tmp_file.path)); |
| 104 | ReportResult result; |
| 105 | KmemReportRawFile(tmp_file.path, {}, &result); |
| 106 | ASSERT_TRUE(result.success); |
| 107 | }); |
| 108 | } |
| 109 | |
| Yabin Cui | 648c3e4 | 2024-04-02 13:12:45 -0700 | [diff] [blame] | 110 | // @CddTest = 6.1/C-0-2 |
| Yabin Cui | 6965d42 | 2016-06-15 11:41:42 -0700 | [diff] [blame] | 111 | TEST(kmem_cmd, record_and_report_callgraph) { |
| 112 | TemporaryFile tmp_file; |
| 113 | TEST_IN_ROOT({ |
| 114 | ASSERT_TRUE(RunKmemRecordCmd({"--slab", "-g"}, tmp_file.path)); |
| 115 | ReportResult result; |
| 116 | KmemReportRawFile(tmp_file.path, {"-g"}, &result); |
| 117 | ASSERT_TRUE(result.success); |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | #endif |
| 122 | |
| Yabin Cui | 648c3e4 | 2024-04-02 13:12:45 -0700 | [diff] [blame] | 123 | // @CddTest = 6.1/C-0-2 |
| Yabin Cui | 6965d42 | 2016-06-15 11:41:42 -0700 | [diff] [blame] | 124 | TEST(kmem_cmd, report) { |
| 125 | ReportResult result; |
| 126 | KmemReportFile(PERF_DATA_WITH_KMEM_SLAB_CALLGRAPH_RECORD, {}, &result); |
| 127 | ASSERT_TRUE(result.success); |
| 128 | ASSERT_NE(result.content.find("kmem:kmalloc"), std::string::npos); |
| 129 | ASSERT_NE(result.content.find("__alloc_skb"), std::string::npos); |
| 130 | } |
| 131 | |
| Yabin Cui | 648c3e4 | 2024-04-02 13:12:45 -0700 | [diff] [blame] | 132 | // @CddTest = 6.1/C-0-2 |
| Yabin Cui | 6965d42 | 2016-06-15 11:41:42 -0700 | [diff] [blame] | 133 | TEST(kmem_cmd, report_all_sort_options) { |
| 134 | ReportResult result; |
| 135 | KmemReportFile( |
| 136 | PERF_DATA_WITH_KMEM_SLAB_CALLGRAPH_RECORD, |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 137 | {"--slab-sort", "hit,caller,ptr,bytes_req,bytes_alloc,fragment,gfp_flags,pingpong"}, &result); |
| Yabin Cui | 6965d42 | 2016-06-15 11:41:42 -0700 | [diff] [blame] | 138 | ASSERT_TRUE(result.success); |
| 139 | ASSERT_NE(result.content.find("Ptr"), std::string::npos); |
| 140 | ASSERT_NE(result.content.find("GfpFlags"), std::string::npos); |
| 141 | } |
| 142 | |
| Yabin Cui | 648c3e4 | 2024-04-02 13:12:45 -0700 | [diff] [blame] | 143 | // @CddTest = 6.1/C-0-2 |
| Yabin Cui | 6965d42 | 2016-06-15 11:41:42 -0700 | [diff] [blame] | 144 | TEST(kmem_cmd, report_callgraph) { |
| 145 | ReportResult result; |
| 146 | KmemReportFile(PERF_DATA_WITH_KMEM_SLAB_CALLGRAPH_RECORD, {"-g"}, &result); |
| 147 | ASSERT_TRUE(result.success); |
| 148 | ASSERT_NE(result.content.find("kmem:kmalloc"), std::string::npos); |
| 149 | ASSERT_NE(result.content.find("__alloc_skb"), std::string::npos); |
| 150 | ASSERT_NE(result.content.find("system_call_fastpath"), std::string::npos); |
| 151 | } |