blob: 3bc4473386bfc7657f453a436a436917c2360edd [file] [log] [blame]
Yabin Cui6965d422016-06-15 11:41:42 -07001/*
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#include <android-base/test_utils.h>
22
23#include <memory>
24
25#include "command.h"
Yabin Cui6965d422016-06-15 11:41:42 -070026#include "get_test_data.h"
27#include "record.h"
28#include "record_file.h"
29#include "test_util.h"
30
31static std::unique_ptr<Command> KmemCmd() {
32 return CreateCommandInstance("kmem");
33}
34
35struct ReportResult {
36 bool success;
37 std::string content;
38 std::vector<std::string> lines;
39};
40
41static 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",
47 tmp_file.path};
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);
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
65static 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);
69}
70
71#if defined(__linux__)
Yabin Cui2597ef02016-10-19 11:28:48 -070072#include "environment.h"
Yabin Cui6965d422016-06-15 11:41:42 -070073
74static bool RunKmemRecordCmd(std::vector<std::string> v,
75 const char* output_file = nullptr) {
76 std::unique_ptr<TemporaryFile> tmpfile;
77 std::string out_file;
78 if (output_file != nullptr) {
79 out_file = output_file;
80 } else {
81 tmpfile.reset(new TemporaryFile);
82 out_file = tmpfile->path;
83 }
84 v.insert(v.begin(), "record");
85 v.insert(v.end(), {"-o", out_file, "sleep", SLEEP_SEC});
86 return KmemCmd()->Run(v);
87}
88
89TEST(kmem_cmd, record_slab) {
90 TEST_IN_ROOT(ASSERT_TRUE(RunKmemRecordCmd({"--slab"})));
91}
92
93TEST(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"})));
96}
97
98TEST(kmem_cmd, record_and_report) {
99 TemporaryFile tmp_file;
100 TEST_IN_ROOT({
101 ASSERT_TRUE(RunKmemRecordCmd({"--slab"}, tmp_file.path));
102 ReportResult result;
103 KmemReportRawFile(tmp_file.path, {}, &result);
104 ASSERT_TRUE(result.success);
105 });
106}
107
108TEST(kmem_cmd, record_and_report_callgraph) {
109 TemporaryFile tmp_file;
110 TEST_IN_ROOT({
111 ASSERT_TRUE(RunKmemRecordCmd({"--slab", "-g"}, tmp_file.path));
112 ReportResult result;
113 KmemReportRawFile(tmp_file.path, {"-g"}, &result);
114 ASSERT_TRUE(result.success);
115 });
116}
117
118#endif
119
120TEST(kmem_cmd, report) {
121 ReportResult result;
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);
126}
127
128TEST(kmem_cmd, report_all_sort_options) {
129 ReportResult result;
130 KmemReportFile(
131 PERF_DATA_WITH_KMEM_SLAB_CALLGRAPH_RECORD,
132 {"--slab-sort",
133 "hit,caller,ptr,bytes_req,bytes_alloc,fragment,gfp_flags,pingpong"},
134 &result);
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);
138}
139
140TEST(kmem_cmd, report_callgraph) {
141 ReportResult result;
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);
147}