1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker * Copyright (C) 2019 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 <android-base/file.h>
18*288bf522SAndroid Build Coastguard Worker #include <android-base/test_utils.h>
19*288bf522SAndroid Build Coastguard Worker #include <gtest/gtest.h>
20*288bf522SAndroid Build Coastguard Worker
21*288bf522SAndroid Build Coastguard Worker #include "command.h"
22*288bf522SAndroid Build Coastguard Worker #include "get_test_data.h"
23*288bf522SAndroid Build Coastguard Worker #include "test_util.h"
24*288bf522SAndroid Build Coastguard Worker #include "utils.h"
25*288bf522SAndroid Build Coastguard Worker
26*288bf522SAndroid Build Coastguard Worker using namespace simpleperf;
27*288bf522SAndroid Build Coastguard Worker
InjectCmd()28*288bf522SAndroid Build Coastguard Worker static std::unique_ptr<Command> InjectCmd() {
29*288bf522SAndroid Build Coastguard Worker return CreateCommandInstance("inject");
30*288bf522SAndroid Build Coastguard Worker }
31*288bf522SAndroid Build Coastguard Worker
RunInjectCmd(std::vector<std::string> && args)32*288bf522SAndroid Build Coastguard Worker static bool RunInjectCmd(std::vector<std::string>&& args) {
33*288bf522SAndroid Build Coastguard Worker bool has_input = std::find(args.begin(), args.end(), "-i") != args.end();
34*288bf522SAndroid Build Coastguard Worker if (!has_input) {
35*288bf522SAndroid Build Coastguard Worker args.insert(args.end(), {"-i", GetTestData(PERF_DATA_ETM_TEST_LOOP)});
36*288bf522SAndroid Build Coastguard Worker }
37*288bf522SAndroid Build Coastguard Worker args.insert(args.end(), {"--symdir", GetTestDataDir() + "etm"});
38*288bf522SAndroid Build Coastguard Worker return InjectCmd()->Run(args);
39*288bf522SAndroid Build Coastguard Worker }
40*288bf522SAndroid Build Coastguard Worker
RunInjectCmd(std::vector<std::string> && args,std::string * output)41*288bf522SAndroid Build Coastguard Worker static bool RunInjectCmd(std::vector<std::string>&& args, std::string* output) {
42*288bf522SAndroid Build Coastguard Worker TemporaryFile tmpfile;
43*288bf522SAndroid Build Coastguard Worker close(tmpfile.release());
44*288bf522SAndroid Build Coastguard Worker args.insert(args.end(), {"-o", tmpfile.path});
45*288bf522SAndroid Build Coastguard Worker if (!RunInjectCmd(std::move(args))) {
46*288bf522SAndroid Build Coastguard Worker return false;
47*288bf522SAndroid Build Coastguard Worker }
48*288bf522SAndroid Build Coastguard Worker if (output != nullptr) {
49*288bf522SAndroid Build Coastguard Worker return android::base::ReadFileToString(tmpfile.path, output);
50*288bf522SAndroid Build Coastguard Worker }
51*288bf522SAndroid Build Coastguard Worker return true;
52*288bf522SAndroid Build Coastguard Worker }
53*288bf522SAndroid Build Coastguard Worker
CheckMatchingExpectedData(const std::string & name,std::string & data)54*288bf522SAndroid Build Coastguard Worker static void CheckMatchingExpectedData(const std::string& name, std::string& data) {
55*288bf522SAndroid Build Coastguard Worker std::string expected_data;
56*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(android::base::ReadFileToString(
57*288bf522SAndroid Build Coastguard Worker GetTestData(std::string("etm") + OS_PATH_SEPARATOR + name), &expected_data));
58*288bf522SAndroid Build Coastguard Worker data.erase(std::remove(data.begin(), data.end(), '\r'), data.end());
59*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(data, expected_data);
60*288bf522SAndroid Build Coastguard Worker }
61*288bf522SAndroid Build Coastguard Worker
62*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(cmd_inject,smoke)63*288bf522SAndroid Build Coastguard Worker TEST(cmd_inject, smoke) {
64*288bf522SAndroid Build Coastguard Worker std::string data;
65*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({}, &data));
66*288bf522SAndroid Build Coastguard Worker // Test that we can find instr range in etm_test_loop binary.
67*288bf522SAndroid Build Coastguard Worker ASSERT_NE(data.find("etm_test_loop"), std::string::npos);
68*288bf522SAndroid Build Coastguard Worker CheckMatchingExpectedData("perf_inject.data", data);
69*288bf522SAndroid Build Coastguard Worker }
70*288bf522SAndroid Build Coastguard Worker
71*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(cmd_inject,binary_option)72*288bf522SAndroid Build Coastguard Worker TEST(cmd_inject, binary_option) {
73*288bf522SAndroid Build Coastguard Worker // Test that data for etm_test_loop is generated when selected by --binary.
74*288bf522SAndroid Build Coastguard Worker std::string data;
75*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"--binary", "etm_test_loop"}, &data));
76*288bf522SAndroid Build Coastguard Worker ASSERT_NE(data.find("etm_test_loop"), std::string::npos);
77*288bf522SAndroid Build Coastguard Worker
78*288bf522SAndroid Build Coastguard Worker // Test that data for etm_test_loop is generated when selected by regex.
79*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"--binary", "etm_t.*_loop"}, &data));
80*288bf522SAndroid Build Coastguard Worker ASSERT_NE(data.find("etm_test_loop"), std::string::npos);
81*288bf522SAndroid Build Coastguard Worker
82*288bf522SAndroid Build Coastguard Worker // Test that data for etm_test_loop isn't generated when not selected by --binary.
83*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"--binary", "no_etm_test_loop"}, &data));
84*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(data.find("etm_test_loop"), std::string::npos);
85*288bf522SAndroid Build Coastguard Worker
86*288bf522SAndroid Build Coastguard Worker // Test that data for etm_test_loop isn't generated when not selected by regex.
87*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"--binary", "no_etm_test_.*"}, &data));
88*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(data.find("etm_test_loop"), std::string::npos);
89*288bf522SAndroid Build Coastguard Worker }
90*288bf522SAndroid Build Coastguard Worker
91*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(cmd_inject,exclude_perf_option)92*288bf522SAndroid Build Coastguard Worker TEST(cmd_inject, exclude_perf_option) {
93*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"--exclude-perf"}, nullptr));
94*288bf522SAndroid Build Coastguard Worker }
95*288bf522SAndroid Build Coastguard Worker
96*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(cmd_inject,output_option)97*288bf522SAndroid Build Coastguard Worker TEST(cmd_inject, output_option) {
98*288bf522SAndroid Build Coastguard Worker TemporaryFile tmpfile;
99*288bf522SAndroid Build Coastguard Worker close(tmpfile.release());
100*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"--output", "autofdo", "-o", tmpfile.path}));
101*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"--output", "branch-list", "-o", tmpfile.path}));
102*288bf522SAndroid Build Coastguard Worker std::string autofdo_data;
103*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"-i", tmpfile.path, "--output", "autofdo"}, &autofdo_data));
104*288bf522SAndroid Build Coastguard Worker CheckMatchingExpectedData("perf_inject.data", autofdo_data);
105*288bf522SAndroid Build Coastguard Worker std::string bolt_data;
106*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"-i", tmpfile.path, "--output", "bolt"}, &bolt_data));
107*288bf522SAndroid Build Coastguard Worker CheckMatchingExpectedData("perf_inject_bolt.data", bolt_data);
108*288bf522SAndroid Build Coastguard Worker }
109*288bf522SAndroid Build Coastguard Worker
110*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(cmd_inject,compress_option)111*288bf522SAndroid Build Coastguard Worker TEST(cmd_inject, compress_option) {
112*288bf522SAndroid Build Coastguard Worker TemporaryFile tmpfile;
113*288bf522SAndroid Build Coastguard Worker close(tmpfile.release());
114*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"--output", "branch-list", "-z", "-o", tmpfile.path}));
115*288bf522SAndroid Build Coastguard Worker std::string autofdo_data;
116*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"-i", tmpfile.path, "--output", "autofdo"}, &autofdo_data));
117*288bf522SAndroid Build Coastguard Worker CheckMatchingExpectedData("perf_inject.data", autofdo_data);
118*288bf522SAndroid Build Coastguard Worker }
119*288bf522SAndroid Build Coastguard Worker
120*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(cmd_inject,skip_empty_output_file)121*288bf522SAndroid Build Coastguard Worker TEST(cmd_inject, skip_empty_output_file) {
122*288bf522SAndroid Build Coastguard Worker TemporaryFile tmpfile;
123*288bf522SAndroid Build Coastguard Worker close(tmpfile.release());
124*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd(
125*288bf522SAndroid Build Coastguard Worker {"--binary", "not_exist_binary", "--output", "branch-list", "-o", tmpfile.path}));
126*288bf522SAndroid Build Coastguard Worker // The empty output file should not be produced.
127*288bf522SAndroid Build Coastguard Worker ASSERT_FALSE(IsRegularFile(tmpfile.path));
128*288bf522SAndroid Build Coastguard Worker tmpfile.DoNotRemove();
129*288bf522SAndroid Build Coastguard Worker }
130*288bf522SAndroid Build Coastguard Worker
131*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(cmd_inject,inject_kernel_data)132*288bf522SAndroid Build Coastguard Worker TEST(cmd_inject, inject_kernel_data) {
133*288bf522SAndroid Build Coastguard Worker const std::string recording_file =
134*288bf522SAndroid Build Coastguard Worker GetTestData(std::string("etm") + OS_PATH_SEPARATOR + "perf_kernel.data");
135*288bf522SAndroid Build Coastguard Worker
136*288bf522SAndroid Build Coastguard Worker // Inject directly to autofdo format.
137*288bf522SAndroid Build Coastguard Worker TemporaryFile tmpfile;
138*288bf522SAndroid Build Coastguard Worker close(tmpfile.release());
139*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"-i", recording_file, "-o", tmpfile.path}));
140*288bf522SAndroid Build Coastguard Worker std::string autofdo_output;
141*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(android::base::ReadFileToString(tmpfile.path, &autofdo_output));
142*288bf522SAndroid Build Coastguard Worker ASSERT_NE(autofdo_output.find("rq_stats.ko"), std::string::npos);
143*288bf522SAndroid Build Coastguard Worker
144*288bf522SAndroid Build Coastguard Worker // Inject through etm branch list.
145*288bf522SAndroid Build Coastguard Worker TemporaryFile tmpfile2;
146*288bf522SAndroid Build Coastguard Worker close(tmpfile2.release());
147*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"-i", recording_file, "-o", tmpfile.path, "--output", "branch-list"}));
148*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"-i", tmpfile.path, "-o", tmpfile2.path}));
149*288bf522SAndroid Build Coastguard Worker std::string output;
150*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(android::base::ReadFileToString(tmpfile2.path, &output));
151*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(output, autofdo_output);
152*288bf522SAndroid Build Coastguard Worker }
153*288bf522SAndroid Build Coastguard Worker
154*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(cmd_inject,unformatted_trace)155*288bf522SAndroid Build Coastguard Worker TEST(cmd_inject, unformatted_trace) {
156*288bf522SAndroid Build Coastguard Worker std::string data;
157*288bf522SAndroid Build Coastguard Worker std::string perf_with_unformatted_trace =
158*288bf522SAndroid Build Coastguard Worker GetTestData(std::string("etm") + OS_PATH_SEPARATOR + "perf_with_unformatted_trace.data");
159*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"-i", perf_with_unformatted_trace}, &data));
160*288bf522SAndroid Build Coastguard Worker // Test that we can find instr range in etm_test_loop binary.
161*288bf522SAndroid Build Coastguard Worker ASSERT_NE(data.find("etm_test_loop"), std::string::npos);
162*288bf522SAndroid Build Coastguard Worker CheckMatchingExpectedData("perf_inject.data", data);
163*288bf522SAndroid Build Coastguard Worker }
164*288bf522SAndroid Build Coastguard Worker
165*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(cmd_inject,multiple_input_files)166*288bf522SAndroid Build Coastguard Worker TEST(cmd_inject, multiple_input_files) {
167*288bf522SAndroid Build Coastguard Worker std::string data;
168*288bf522SAndroid Build Coastguard Worker std::string perf_data = GetTestData(PERF_DATA_ETM_TEST_LOOP);
169*288bf522SAndroid Build Coastguard Worker std::string perf_with_unformatted_trace =
170*288bf522SAndroid Build Coastguard Worker GetTestData(std::string("etm") + OS_PATH_SEPARATOR + "perf_with_unformatted_trace.data");
171*288bf522SAndroid Build Coastguard Worker
172*288bf522SAndroid Build Coastguard Worker // Test input files separated by comma.
173*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"-i", perf_with_unformatted_trace + "," + perf_data}, &data));
174*288bf522SAndroid Build Coastguard Worker ASSERT_NE(data.find("106c->1074:200"), std::string::npos);
175*288bf522SAndroid Build Coastguard Worker
176*288bf522SAndroid Build Coastguard Worker // Test input files from different -i options.
177*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"-i", perf_with_unformatted_trace, "-i", perf_data}, &data));
178*288bf522SAndroid Build Coastguard Worker ASSERT_NE(data.find("106c->1074:200"), std::string::npos);
179*288bf522SAndroid Build Coastguard Worker
180*288bf522SAndroid Build Coastguard Worker // Test input files provided by input_file_list.
181*288bf522SAndroid Build Coastguard Worker TemporaryFile tmpfile;
182*288bf522SAndroid Build Coastguard Worker std::string input_file_list = perf_data + "\n" + perf_with_unformatted_trace + "\n";
183*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(android::base::WriteStringToFd(input_file_list, tmpfile.fd));
184*288bf522SAndroid Build Coastguard Worker close(tmpfile.release());
185*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"-i", std::string("@") + tmpfile.path}, &data));
186*288bf522SAndroid Build Coastguard Worker ASSERT_NE(data.find("106c->1074:200"), std::string::npos);
187*288bf522SAndroid Build Coastguard Worker }
188*288bf522SAndroid Build Coastguard Worker
189*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(cmd_inject,merge_branch_list_files)190*288bf522SAndroid Build Coastguard Worker TEST(cmd_inject, merge_branch_list_files) {
191*288bf522SAndroid Build Coastguard Worker TemporaryFile tmpfile;
192*288bf522SAndroid Build Coastguard Worker close(tmpfile.release());
193*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"--output", "branch-list", "-o", tmpfile.path}));
194*288bf522SAndroid Build Coastguard Worker TemporaryFile tmpfile2;
195*288bf522SAndroid Build Coastguard Worker close(tmpfile2.release());
196*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"-i", std::string(tmpfile.path) + "," + tmpfile.path, "--output",
197*288bf522SAndroid Build Coastguard Worker "branch-list", "-o", tmpfile2.path}));
198*288bf522SAndroid Build Coastguard Worker std::string autofdo_data;
199*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"-i", tmpfile2.path, "--output", "autofdo"}, &autofdo_data));
200*288bf522SAndroid Build Coastguard Worker ASSERT_NE(autofdo_data.find("106c->1074:200"), std::string::npos);
201*288bf522SAndroid Build Coastguard Worker
202*288bf522SAndroid Build Coastguard Worker // Accept invalid branch list files.
203*288bf522SAndroid Build Coastguard Worker TemporaryFile tmpfile3;
204*288bf522SAndroid Build Coastguard Worker close(tmpfile3.release());
205*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(android::base::WriteStringToFile("bad content", tmpfile3.path));
206*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"-i", std::string(tmpfile.path) + "," + tmpfile3.path, "--output",
207*288bf522SAndroid Build Coastguard Worker "branch-list", "-o", tmpfile2.path}));
208*288bf522SAndroid Build Coastguard Worker }
209*288bf522SAndroid Build Coastguard Worker
210*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(cmd_inject,report_warning_when_overflow)211*288bf522SAndroid Build Coastguard Worker TEST(cmd_inject, report_warning_when_overflow) {
212*288bf522SAndroid Build Coastguard Worker CapturedStderr capture;
213*288bf522SAndroid Build Coastguard Worker std::vector<std::unique_ptr<TemporaryFile>> branch_list_files;
214*288bf522SAndroid Build Coastguard Worker std::vector<std::unique_ptr<TemporaryFile>> input_files;
215*288bf522SAndroid Build Coastguard Worker
216*288bf522SAndroid Build Coastguard Worker branch_list_files.emplace_back(new TemporaryFile);
217*288bf522SAndroid Build Coastguard Worker close(branch_list_files.back()->release());
218*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"--output", "branch-list", "-o", branch_list_files.back()->path}));
219*288bf522SAndroid Build Coastguard Worker for (size_t i = 1; i <= 7; i++) {
220*288bf522SAndroid Build Coastguard Worker // Create input file list, repeating branch list file for 1000 times.
221*288bf522SAndroid Build Coastguard Worker std::string s;
222*288bf522SAndroid Build Coastguard Worker for (size_t j = 0; j < 1000; j++) {
223*288bf522SAndroid Build Coastguard Worker s += std::string(branch_list_files.back()->path) + "\n";
224*288bf522SAndroid Build Coastguard Worker }
225*288bf522SAndroid Build Coastguard Worker input_files.emplace_back(new TemporaryFile);
226*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(android::base::WriteStringToFd(s, input_files.back()->fd));
227*288bf522SAndroid Build Coastguard Worker close(input_files.back()->release());
228*288bf522SAndroid Build Coastguard Worker
229*288bf522SAndroid Build Coastguard Worker // Merge branch list files.
230*288bf522SAndroid Build Coastguard Worker branch_list_files.emplace_back(new TemporaryFile);
231*288bf522SAndroid Build Coastguard Worker close(branch_list_files.back()->release());
232*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(
233*288bf522SAndroid Build Coastguard Worker RunInjectCmd({"--output", "branch-list", "-i", std::string("@") + input_files.back()->path,
234*288bf522SAndroid Build Coastguard Worker "-o", branch_list_files.back()->path}));
235*288bf522SAndroid Build Coastguard Worker }
236*288bf522SAndroid Build Coastguard Worker capture.Stop();
237*288bf522SAndroid Build Coastguard Worker const std::string WARNING_MSG = "Branch count overflow happened.";
238*288bf522SAndroid Build Coastguard Worker ASSERT_NE(capture.str().find(WARNING_MSG), std::string::npos);
239*288bf522SAndroid Build Coastguard Worker
240*288bf522SAndroid Build Coastguard Worker // Warning also happens when converting branch lists to AutoFDO format.
241*288bf522SAndroid Build Coastguard Worker capture.Reset();
242*288bf522SAndroid Build Coastguard Worker capture.Start();
243*288bf522SAndroid Build Coastguard Worker std::string autofdo_data;
244*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"-i", branch_list_files.back()->path}, &autofdo_data));
245*288bf522SAndroid Build Coastguard Worker capture.Stop();
246*288bf522SAndroid Build Coastguard Worker ASSERT_NE(capture.str().find(WARNING_MSG), std::string::npos);
247*288bf522SAndroid Build Coastguard Worker ASSERT_NE(autofdo_data.find("106c->1074:18446744073709551615"), std::string::npos);
248*288bf522SAndroid Build Coastguard Worker }
249*288bf522SAndroid Build Coastguard Worker
250*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(cmd_inject,accept_missing_aux_data)251*288bf522SAndroid Build Coastguard Worker TEST(cmd_inject, accept_missing_aux_data) {
252*288bf522SAndroid Build Coastguard Worker // Recorded with "-e cs-etm:u --user-buffer-size 64k sleep 1".
253*288bf522SAndroid Build Coastguard Worker std::string perf_data = GetTestData("etm/perf_with_missing_aux_data.data");
254*288bf522SAndroid Build Coastguard Worker TemporaryFile tmpfile;
255*288bf522SAndroid Build Coastguard Worker close(tmpfile.release());
256*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"--output", "branch-list", "-i", perf_data, "-o", tmpfile.path}));
257*288bf522SAndroid Build Coastguard Worker }
258*288bf522SAndroid Build Coastguard Worker
259*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(cmd_inject,read_lbr_data)260*288bf522SAndroid Build Coastguard Worker TEST(cmd_inject, read_lbr_data) {
261*288bf522SAndroid Build Coastguard Worker // Convert perf.data to AutoFDO text format.
262*288bf522SAndroid Build Coastguard Worker auto get_autofdo_data = [&](std::vector<std::string>&& args, std::string* data) {
263*288bf522SAndroid Build Coastguard Worker args.insert(args.end(), {"--symdir", GetTestDataDir() + "lbr", "--allow-mismatched-build-id"});
264*288bf522SAndroid Build Coastguard Worker return RunInjectCmd(std::move(args), data);
265*288bf522SAndroid Build Coastguard Worker };
266*288bf522SAndroid Build Coastguard Worker
267*288bf522SAndroid Build Coastguard Worker const std::string perf_data_path = GetTestData("lbr/perf_lbr.data");
268*288bf522SAndroid Build Coastguard Worker std::string data;
269*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(get_autofdo_data({"-i", perf_data_path}, &data));
270*288bf522SAndroid Build Coastguard Worker data.erase(std::remove(data.begin(), data.end(), '\r'), data.end());
271*288bf522SAndroid Build Coastguard Worker
272*288bf522SAndroid Build Coastguard Worker std::string expected_data;
273*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(android::base::ReadFileToString(
274*288bf522SAndroid Build Coastguard Worker GetTestData(std::string("lbr") + OS_PATH_SEPARATOR + "inject_lbr.data"), &expected_data));
275*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(data, expected_data);
276*288bf522SAndroid Build Coastguard Worker
277*288bf522SAndroid Build Coastguard Worker // Convert perf.data to branch_list.proto format.
278*288bf522SAndroid Build Coastguard Worker // Then convert branch_list.proto format to AutoFDO text format.
279*288bf522SAndroid Build Coastguard Worker TemporaryFile branch_list_file;
280*288bf522SAndroid Build Coastguard Worker close(branch_list_file.release());
281*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(
282*288bf522SAndroid Build Coastguard Worker RunInjectCmd({"-i", perf_data_path, "--output", "branch-list", "-o", branch_list_file.path}));
283*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(get_autofdo_data({"-i", branch_list_file.path}, &data));
284*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(data, expected_data);
285*288bf522SAndroid Build Coastguard Worker
286*288bf522SAndroid Build Coastguard Worker // Test binary filter on LBR data.
287*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(get_autofdo_data({"-i", perf_data_path, "--binary", "no_lbr_test_loop"}, &data));
288*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(data.find("lbr_test_loop"), data.npos);
289*288bf522SAndroid Build Coastguard Worker
290*288bf522SAndroid Build Coastguard Worker // Test binary filter on branch list file.
291*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(
292*288bf522SAndroid Build Coastguard Worker get_autofdo_data({"-i", branch_list_file.path, "--binary", "no_lbr_test_loop"}, &data));
293*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(data.find("lbr_test_loop"), data.npos);
294*288bf522SAndroid Build Coastguard Worker
295*288bf522SAndroid Build Coastguard Worker // Test multiple input files.
296*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(get_autofdo_data(
297*288bf522SAndroid Build Coastguard Worker {
298*288bf522SAndroid Build Coastguard Worker "-i",
299*288bf522SAndroid Build Coastguard Worker std::string(branch_list_file.path) + "," + branch_list_file.path,
300*288bf522SAndroid Build Coastguard Worker },
301*288bf522SAndroid Build Coastguard Worker &data));
302*288bf522SAndroid Build Coastguard Worker ASSERT_NE(data.find("94d->940:706"), data.npos);
303*288bf522SAndroid Build Coastguard Worker }
304*288bf522SAndroid Build Coastguard Worker
305*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(cmd_inject,inject_small_binary)306*288bf522SAndroid Build Coastguard Worker TEST(cmd_inject, inject_small_binary) {
307*288bf522SAndroid Build Coastguard Worker // etm_test_loop_small, a binary compiled with
308*288bf522SAndroid Build Coastguard Worker // "-Wl,-z,noseparate-code", where the file is smaller than its text
309*288bf522SAndroid Build Coastguard Worker // section mapped into memory.
310*288bf522SAndroid Build Coastguard Worker std::string data;
311*288bf522SAndroid Build Coastguard Worker std::string perf_data = GetTestData("etm/perf_for_small_binary.data");
312*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"-i", perf_data}, &data));
313*288bf522SAndroid Build Coastguard Worker CheckMatchingExpectedData("perf_inject_small.data", data);
314*288bf522SAndroid Build Coastguard Worker
315*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"-i", perf_data, "--output", "bolt"}, &data));
316*288bf522SAndroid Build Coastguard Worker CheckMatchingExpectedData("perf_inject_small_bolt.data", data);
317*288bf522SAndroid Build Coastguard Worker }
318*288bf522SAndroid Build Coastguard Worker
319*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(cmd_inject,j_option)320*288bf522SAndroid Build Coastguard Worker TEST(cmd_inject, j_option) {
321*288bf522SAndroid Build Coastguard Worker TemporaryFile tmpfile;
322*288bf522SAndroid Build Coastguard Worker close(tmpfile.release());
323*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"--output", "branch-list", "-o", tmpfile.path}));
324*288bf522SAndroid Build Coastguard Worker std::string autofdo_data;
325*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd(
326*288bf522SAndroid Build Coastguard Worker {"-i", std::string(tmpfile.path) + "," + tmpfile.path, "--output", "autofdo", "-j", "1"},
327*288bf522SAndroid Build Coastguard Worker &autofdo_data));
328*288bf522SAndroid Build Coastguard Worker ASSERT_NE(autofdo_data.find("106c->1074:200"), std::string::npos);
329*288bf522SAndroid Build Coastguard Worker
330*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd(
331*288bf522SAndroid Build Coastguard Worker {"-i", std::string(tmpfile.path) + "," + tmpfile.path, "--output", "autofdo", "-j", "2"},
332*288bf522SAndroid Build Coastguard Worker &autofdo_data));
333*288bf522SAndroid Build Coastguard Worker ASSERT_NE(autofdo_data.find("106c->1074:200"), std::string::npos);
334*288bf522SAndroid Build Coastguard Worker
335*288bf522SAndroid Build Coastguard Worker // Invalid job count.
336*288bf522SAndroid Build Coastguard Worker ASSERT_FALSE(RunInjectCmd(
337*288bf522SAndroid Build Coastguard Worker {"-i", std::string(tmpfile.path) + "," + tmpfile.path, "--output", "autofdo", "-j", "0"},
338*288bf522SAndroid Build Coastguard Worker &autofdo_data));
339*288bf522SAndroid Build Coastguard Worker }
340*288bf522SAndroid Build Coastguard Worker
341*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(cmd_inject,dump_option)342*288bf522SAndroid Build Coastguard Worker TEST(cmd_inject, dump_option) {
343*288bf522SAndroid Build Coastguard Worker TemporaryFile tmpfile;
344*288bf522SAndroid Build Coastguard Worker close(tmpfile.release());
345*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd({"--output", "branch-list", "-o", tmpfile.path}));
346*288bf522SAndroid Build Coastguard Worker
347*288bf522SAndroid Build Coastguard Worker CaptureStdout capture;
348*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(capture.Start());
349*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(InjectCmd()->Run({"--dump", tmpfile.path}));
350*288bf522SAndroid Build Coastguard Worker std::string data = capture.Finish();
351*288bf522SAndroid Build Coastguard Worker ASSERT_NE(data.find("binary[0].build_id: 0x0c9a20bf9c009d0e4e8bbf9fad0300ae00000000"),
352*288bf522SAndroid Build Coastguard Worker std::string::npos);
353*288bf522SAndroid Build Coastguard Worker
354*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd(
355*288bf522SAndroid Build Coastguard Worker {"--output", "branch-list", "-o", tmpfile.path, "-i", GetTestData("lbr/perf_lbr.data")}));
356*288bf522SAndroid Build Coastguard Worker
357*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(capture.Start());
358*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(InjectCmd()->Run({"--dump", tmpfile.path}));
359*288bf522SAndroid Build Coastguard Worker data = capture.Finish();
360*288bf522SAndroid Build Coastguard Worker ASSERT_NE(data.find("binary[0].path: /home/yabinc/lbr_test_loop"), std::string::npos);
361*288bf522SAndroid Build Coastguard Worker }
362*288bf522SAndroid Build Coastguard Worker
363*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(cmd_inject,exclude_process_name_option)364*288bf522SAndroid Build Coastguard Worker TEST(cmd_inject, exclude_process_name_option) {
365*288bf522SAndroid Build Coastguard Worker TemporaryFile tmpfile;
366*288bf522SAndroid Build Coastguard Worker close(tmpfile.release());
367*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(RunInjectCmd(
368*288bf522SAndroid Build Coastguard Worker {"--output", "branch-list", "--exclude-process-name", "etm_test_loop", "-o", tmpfile.path}));
369*288bf522SAndroid Build Coastguard Worker struct stat st;
370*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(stat(tmpfile.path, &st), -1);
371*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(errno, ENOENT);
372*288bf522SAndroid Build Coastguard Worker }
373