xref: /aosp_15_r20/system/extras/simpleperf/cmd_kmem_test.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker  *
4*288bf522SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker  *
8*288bf522SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker  *
10*288bf522SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker  * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker  */
16*288bf522SAndroid Build Coastguard Worker 
17*288bf522SAndroid Build Coastguard Worker #include <gtest/gtest.h>
18*288bf522SAndroid Build Coastguard Worker 
19*288bf522SAndroid Build Coastguard Worker #include <android-base/file.h>
20*288bf522SAndroid Build Coastguard Worker #include <android-base/strings.h>
21*288bf522SAndroid Build Coastguard Worker 
22*288bf522SAndroid Build Coastguard Worker #include <memory>
23*288bf522SAndroid Build Coastguard Worker 
24*288bf522SAndroid Build Coastguard Worker #include "command.h"
25*288bf522SAndroid Build Coastguard Worker #include "get_test_data.h"
26*288bf522SAndroid Build Coastguard Worker #include "record.h"
27*288bf522SAndroid Build Coastguard Worker #include "record_file.h"
28*288bf522SAndroid Build Coastguard Worker #include "test_util.h"
29*288bf522SAndroid Build Coastguard Worker 
30*288bf522SAndroid Build Coastguard Worker using namespace simpleperf;
31*288bf522SAndroid Build Coastguard Worker 
KmemCmd()32*288bf522SAndroid Build Coastguard Worker static std::unique_ptr<Command> KmemCmd() {
33*288bf522SAndroid Build Coastguard Worker   return CreateCommandInstance("kmem");
34*288bf522SAndroid Build Coastguard Worker }
35*288bf522SAndroid Build Coastguard Worker 
36*288bf522SAndroid Build Coastguard Worker struct ReportResult {
37*288bf522SAndroid Build Coastguard Worker   bool success;
38*288bf522SAndroid Build Coastguard Worker   std::string content;
39*288bf522SAndroid Build Coastguard Worker   std::vector<std::string> lines;
40*288bf522SAndroid Build Coastguard Worker };
41*288bf522SAndroid Build Coastguard Worker 
KmemReportRawFile(const std::string & perf_data,const std::vector<std::string> & additional_args,ReportResult * result)42*288bf522SAndroid Build Coastguard Worker static void KmemReportRawFile(const std::string& perf_data,
43*288bf522SAndroid Build Coastguard Worker                               const std::vector<std::string>& additional_args,
44*288bf522SAndroid Build Coastguard Worker                               ReportResult* result) {
45*288bf522SAndroid Build Coastguard Worker   result->success = false;
46*288bf522SAndroid Build Coastguard Worker   TemporaryFile tmp_file;
47*288bf522SAndroid Build Coastguard Worker   close(tmp_file.release());
48*288bf522SAndroid Build Coastguard Worker   std::vector<std::string> args = {"report", "-i", perf_data, "-o", tmp_file.path};
49*288bf522SAndroid Build Coastguard Worker   args.insert(args.end(), additional_args.begin(), additional_args.end());
50*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(KmemCmd()->Run(args));
51*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(android::base::ReadFileToString(tmp_file.path, &result->content));
52*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(!result->content.empty());
53*288bf522SAndroid Build Coastguard Worker   std::vector<std::string> raw_lines = android::base::Split(result->content, "\n");
54*288bf522SAndroid Build Coastguard Worker   result->lines.clear();
55*288bf522SAndroid Build Coastguard Worker   for (const auto& line : raw_lines) {
56*288bf522SAndroid Build Coastguard Worker     std::string s = android::base::Trim(line);
57*288bf522SAndroid Build Coastguard Worker     if (!s.empty()) {
58*288bf522SAndroid Build Coastguard Worker       result->lines.push_back(s);
59*288bf522SAndroid Build Coastguard Worker     }
60*288bf522SAndroid Build Coastguard Worker   }
61*288bf522SAndroid Build Coastguard Worker   ASSERT_GE(result->lines.size(), 2u);
62*288bf522SAndroid Build Coastguard Worker   result->success = true;
63*288bf522SAndroid Build Coastguard Worker }
64*288bf522SAndroid Build Coastguard Worker 
KmemReportFile(const std::string & perf_data,const std::vector<std::string> & additional_args,ReportResult * result)65*288bf522SAndroid Build Coastguard Worker static void KmemReportFile(const std::string& perf_data,
66*288bf522SAndroid Build Coastguard Worker                            const std::vector<std::string>& additional_args, ReportResult* result) {
67*288bf522SAndroid Build Coastguard Worker   KmemReportRawFile(GetTestData(perf_data), additional_args, result);
68*288bf522SAndroid Build Coastguard Worker }
69*288bf522SAndroid Build Coastguard Worker 
70*288bf522SAndroid Build Coastguard Worker #if defined(__linux__)
71*288bf522SAndroid Build Coastguard Worker #include "environment.h"
72*288bf522SAndroid Build Coastguard Worker 
RunKmemRecordCmd(std::vector<std::string> v,const char * output_file=nullptr)73*288bf522SAndroid Build Coastguard Worker static bool RunKmemRecordCmd(std::vector<std::string> v, const char* output_file = nullptr) {
74*288bf522SAndroid Build Coastguard Worker   std::unique_ptr<TemporaryFile> tmpfile;
75*288bf522SAndroid Build Coastguard Worker   std::string out_file;
76*288bf522SAndroid Build Coastguard Worker   if (output_file != nullptr) {
77*288bf522SAndroid Build Coastguard Worker     out_file = output_file;
78*288bf522SAndroid Build Coastguard Worker   } else {
79*288bf522SAndroid Build Coastguard Worker     tmpfile.reset(new TemporaryFile);
80*288bf522SAndroid Build Coastguard Worker     close(tmpfile->release());
81*288bf522SAndroid Build Coastguard Worker     out_file = tmpfile->path;
82*288bf522SAndroid Build Coastguard Worker   }
83*288bf522SAndroid Build Coastguard Worker   v.insert(v.begin(), "record");
84*288bf522SAndroid Build Coastguard Worker   v.insert(v.end(), {"-o", out_file, "sleep", SLEEP_SEC});
85*288bf522SAndroid Build Coastguard Worker   return KmemCmd()->Run(v);
86*288bf522SAndroid Build Coastguard Worker }
87*288bf522SAndroid Build Coastguard Worker 
88*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(kmem_cmd,record_slab)89*288bf522SAndroid Build Coastguard Worker TEST(kmem_cmd, record_slab) {
90*288bf522SAndroid Build Coastguard Worker   TEST_IN_ROOT(ASSERT_TRUE(RunKmemRecordCmd({"--slab"})));
91*288bf522SAndroid Build Coastguard Worker }
92*288bf522SAndroid Build Coastguard Worker 
93*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(kmem_cmd,record_fp_callchain_sampling)94*288bf522SAndroid Build Coastguard Worker TEST(kmem_cmd, record_fp_callchain_sampling) {
95*288bf522SAndroid Build Coastguard Worker   TEST_IN_ROOT(ASSERT_TRUE(RunKmemRecordCmd({"--slab", "-g"})));
96*288bf522SAndroid Build Coastguard Worker   TEST_IN_ROOT(ASSERT_TRUE(RunKmemRecordCmd({"--slab", "--call-graph", "fp"})));
97*288bf522SAndroid Build Coastguard Worker }
98*288bf522SAndroid Build Coastguard Worker 
99*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(kmem_cmd,record_and_report)100*288bf522SAndroid Build Coastguard Worker TEST(kmem_cmd, record_and_report) {
101*288bf522SAndroid Build Coastguard Worker   TemporaryFile tmp_file;
102*288bf522SAndroid Build Coastguard Worker   TEST_IN_ROOT({
103*288bf522SAndroid Build Coastguard Worker     ASSERT_TRUE(RunKmemRecordCmd({"--slab"}, tmp_file.path));
104*288bf522SAndroid Build Coastguard Worker     ReportResult result;
105*288bf522SAndroid Build Coastguard Worker     KmemReportRawFile(tmp_file.path, {}, &result);
106*288bf522SAndroid Build Coastguard Worker     ASSERT_TRUE(result.success);
107*288bf522SAndroid Build Coastguard Worker   });
108*288bf522SAndroid Build Coastguard Worker }
109*288bf522SAndroid Build Coastguard Worker 
110*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(kmem_cmd,record_and_report_callgraph)111*288bf522SAndroid Build Coastguard Worker TEST(kmem_cmd, record_and_report_callgraph) {
112*288bf522SAndroid Build Coastguard Worker   TemporaryFile tmp_file;
113*288bf522SAndroid Build Coastguard Worker   TEST_IN_ROOT({
114*288bf522SAndroid Build Coastguard Worker     ASSERT_TRUE(RunKmemRecordCmd({"--slab", "-g"}, tmp_file.path));
115*288bf522SAndroid Build Coastguard Worker     ReportResult result;
116*288bf522SAndroid Build Coastguard Worker     KmemReportRawFile(tmp_file.path, {"-g"}, &result);
117*288bf522SAndroid Build Coastguard Worker     ASSERT_TRUE(result.success);
118*288bf522SAndroid Build Coastguard Worker   });
119*288bf522SAndroid Build Coastguard Worker }
120*288bf522SAndroid Build Coastguard Worker 
121*288bf522SAndroid Build Coastguard Worker #endif
122*288bf522SAndroid Build Coastguard Worker 
123*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(kmem_cmd,report)124*288bf522SAndroid Build Coastguard Worker TEST(kmem_cmd, report) {
125*288bf522SAndroid Build Coastguard Worker   ReportResult result;
126*288bf522SAndroid Build Coastguard Worker   KmemReportFile(PERF_DATA_WITH_KMEM_SLAB_CALLGRAPH_RECORD, {}, &result);
127*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(result.success);
128*288bf522SAndroid Build Coastguard Worker   ASSERT_NE(result.content.find("kmem:kmalloc"), std::string::npos);
129*288bf522SAndroid Build Coastguard Worker   ASSERT_NE(result.content.find("__alloc_skb"), std::string::npos);
130*288bf522SAndroid Build Coastguard Worker }
131*288bf522SAndroid Build Coastguard Worker 
132*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(kmem_cmd,report_all_sort_options)133*288bf522SAndroid Build Coastguard Worker TEST(kmem_cmd, report_all_sort_options) {
134*288bf522SAndroid Build Coastguard Worker   ReportResult result;
135*288bf522SAndroid Build Coastguard Worker   KmemReportFile(
136*288bf522SAndroid Build Coastguard Worker       PERF_DATA_WITH_KMEM_SLAB_CALLGRAPH_RECORD,
137*288bf522SAndroid Build Coastguard Worker       {"--slab-sort", "hit,caller,ptr,bytes_req,bytes_alloc,fragment,gfp_flags,pingpong"}, &result);
138*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(result.success);
139*288bf522SAndroid Build Coastguard Worker   ASSERT_NE(result.content.find("Ptr"), std::string::npos);
140*288bf522SAndroid Build Coastguard Worker   ASSERT_NE(result.content.find("GfpFlags"), std::string::npos);
141*288bf522SAndroid Build Coastguard Worker }
142*288bf522SAndroid Build Coastguard Worker 
143*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(kmem_cmd,report_callgraph)144*288bf522SAndroid Build Coastguard Worker TEST(kmem_cmd, report_callgraph) {
145*288bf522SAndroid Build Coastguard Worker   ReportResult result;
146*288bf522SAndroid Build Coastguard Worker   KmemReportFile(PERF_DATA_WITH_KMEM_SLAB_CALLGRAPH_RECORD, {"-g"}, &result);
147*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(result.success);
148*288bf522SAndroid Build Coastguard Worker   ASSERT_NE(result.content.find("kmem:kmalloc"), std::string::npos);
149*288bf522SAndroid Build Coastguard Worker   ASSERT_NE(result.content.find("__alloc_skb"), std::string::npos);
150*288bf522SAndroid Build Coastguard Worker   ASSERT_NE(result.content.find("system_call_fastpath"), std::string::npos);
151*288bf522SAndroid Build Coastguard Worker }
152