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