2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
19 #include <android-base/file.h>
20 #include <android-base/strings.h>
21 #include <android-base/test_utils.h>
26 #include "get_test_data.h"
28 #include "record_file.h"
29 #include "test_util.h"
31 static std::unique_ptr<Command> KmemCmd() {
32 return CreateCommandInstance("kmem");
38 std::vector<std::string> lines;
41 static void KmemReportRawFile(const std::string& perf_data,
42 const std::vector<std::string>& additional_args,
43 ReportResult* result) {
44 result->success = false;
45 TemporaryFile tmp_file;
46 std::vector<std::string> args = {"report", "-i", perf_data, "-o",
48 args.insert(args.end(), additional_args.begin(), additional_args.end());
49 ASSERT_TRUE(KmemCmd()->Run(args));
50 ASSERT_TRUE(android::base::ReadFileToString(tmp_file.path, &result->content));
51 ASSERT_TRUE(!result->content.empty());
52 std::vector<std::string> raw_lines =
53 android::base::Split(result->content, "\n");
54 result->lines.clear();
55 for (const auto& line : raw_lines) {
56 std::string s = android::base::Trim(line);
58 result->lines.push_back(s);
61 ASSERT_GE(result->lines.size(), 2u);
62 result->success = true;
65 static void KmemReportFile(const std::string& perf_data,
66 const std::vector<std::string>& additional_args,
67 ReportResult* result) {
68 KmemReportRawFile(GetTestData(perf_data), additional_args, result);
71 #if defined(__linux__)
72 #include "environment.h"
74 static bool RunKmemRecordCmd(std::vector<std::string> v,
75 const char* output_file = nullptr) {
76 std::unique_ptr<TemporaryFile> tmpfile;
78 if (output_file != nullptr) {
79 out_file = output_file;
81 tmpfile.reset(new TemporaryFile);
82 out_file = tmpfile->path;
84 v.insert(v.begin(), "record");
85 v.insert(v.end(), {"-o", out_file, "sleep", SLEEP_SEC});
86 return KmemCmd()->Run(v);
89 TEST(kmem_cmd, record_slab) {
90 TEST_IN_ROOT(ASSERT_TRUE(RunKmemRecordCmd({"--slab"})));
93 TEST(kmem_cmd, record_fp_callchain_sampling) {
94 TEST_IN_ROOT(ASSERT_TRUE(RunKmemRecordCmd({"--slab", "-g"})));
95 TEST_IN_ROOT(ASSERT_TRUE(RunKmemRecordCmd({"--slab", "--call-graph", "fp"})));
98 TEST(kmem_cmd, record_and_report) {
99 TemporaryFile tmp_file;
101 ASSERT_TRUE(RunKmemRecordCmd({"--slab"}, tmp_file.path));
103 KmemReportRawFile(tmp_file.path, {}, &result);
104 ASSERT_TRUE(result.success);
108 TEST(kmem_cmd, record_and_report_callgraph) {
109 TemporaryFile tmp_file;
111 ASSERT_TRUE(RunKmemRecordCmd({"--slab", "-g"}, tmp_file.path));
113 KmemReportRawFile(tmp_file.path, {"-g"}, &result);
114 ASSERT_TRUE(result.success);
120 TEST(kmem_cmd, report) {
122 KmemReportFile(PERF_DATA_WITH_KMEM_SLAB_CALLGRAPH_RECORD, {}, &result);
123 ASSERT_TRUE(result.success);
124 ASSERT_NE(result.content.find("kmem:kmalloc"), std::string::npos);
125 ASSERT_NE(result.content.find("__alloc_skb"), std::string::npos);
128 TEST(kmem_cmd, report_all_sort_options) {
131 PERF_DATA_WITH_KMEM_SLAB_CALLGRAPH_RECORD,
133 "hit,caller,ptr,bytes_req,bytes_alloc,fragment,gfp_flags,pingpong"},
135 ASSERT_TRUE(result.success);
136 ASSERT_NE(result.content.find("Ptr"), std::string::npos);
137 ASSERT_NE(result.content.find("GfpFlags"), std::string::npos);
140 TEST(kmem_cmd, report_callgraph) {
142 KmemReportFile(PERF_DATA_WITH_KMEM_SLAB_CALLGRAPH_RECORD, {"-g"}, &result);
143 ASSERT_TRUE(result.success);
144 ASSERT_NE(result.content.find("kmem:kmalloc"), std::string::npos);
145 ASSERT_NE(result.content.find("__alloc_skb"), std::string::npos);
146 ASSERT_NE(result.content.find("system_call_fastpath"), std::string::npos);