1 /*
2 * Copyright (C) 2019 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 <android-base/file.h>
18 #include <android-base/test_utils.h>
19 #include <gtest/gtest.h>
20
21 #include "cmd_inject_impl.h"
22 #include "command.h"
23 #include "get_test_data.h"
24 #include "test_util.h"
25 #include "utils.h"
26
27 using namespace simpleperf;
28
InjectCmd()29 static std::unique_ptr<Command> InjectCmd() {
30 return CreateCommandInstance("inject");
31 }
32
RunInjectCmd(std::vector<std::string> && args)33 static bool RunInjectCmd(std::vector<std::string>&& args) {
34 bool has_input = std::find(args.begin(), args.end(), "-i") != args.end();
35 if (!has_input) {
36 args.insert(args.end(), {"-i", GetTestData(PERF_DATA_ETM_TEST_LOOP)});
37 }
38 args.insert(args.end(), {"--symdir", GetTestDataDir() + "etm"});
39 return InjectCmd()->Run(args);
40 }
41
RunInjectCmd(std::vector<std::string> && args,std::string * output)42 static bool RunInjectCmd(std::vector<std::string>&& args, std::string* output) {
43 TemporaryFile tmpfile;
44 close(tmpfile.release());
45 args.insert(args.end(), {"-o", tmpfile.path});
46 if (!RunInjectCmd(std::move(args))) {
47 return false;
48 }
49 if (output != nullptr) {
50 return android::base::ReadFileToString(tmpfile.path, output);
51 }
52 return true;
53 }
54
CheckMatchingExpectedData(std::string & data)55 static void CheckMatchingExpectedData(std::string& data) {
56 std::string expected_data;
57 ASSERT_TRUE(android::base::ReadFileToString(
58 GetTestData(std::string("etm") + OS_PATH_SEPARATOR + "perf_inject.data"), &expected_data));
59 data.erase(std::remove(data.begin(), data.end(), '\r'), data.end());
60 ASSERT_EQ(data, expected_data);
61 }
62
TEST(cmd_inject,smoke)63 TEST(cmd_inject, smoke) {
64 std::string data;
65 ASSERT_TRUE(RunInjectCmd({}, &data));
66 // Test that we can find instr range in etm_test_loop binary.
67 ASSERT_NE(data.find("etm_test_loop"), std::string::npos);
68 CheckMatchingExpectedData(data);
69 }
70
TEST(cmd_inject,binary_option)71 TEST(cmd_inject, binary_option) {
72 // Test that data for etm_test_loop is generated when selected by --binary.
73 std::string data;
74 ASSERT_TRUE(RunInjectCmd({"--binary", "etm_test_loop"}, &data));
75 ASSERT_NE(data.find("etm_test_loop"), std::string::npos);
76
77 // Test that data for etm_test_loop is generated when selected by regex.
78 ASSERT_TRUE(RunInjectCmd({"--binary", "etm_t.*_loop"}, &data));
79 ASSERT_NE(data.find("etm_test_loop"), std::string::npos);
80
81 // Test that data for etm_test_loop isn't generated when not selected by --binary.
82 ASSERT_TRUE(RunInjectCmd({"--binary", "no_etm_test_loop"}, &data));
83 ASSERT_EQ(data.find("etm_test_loop"), std::string::npos);
84
85 // Test that data for etm_test_loop isn't generated when not selected by regex.
86 ASSERT_TRUE(RunInjectCmd({"--binary", "no_etm_test_.*"}, &data));
87 ASSERT_EQ(data.find("etm_test_loop"), std::string::npos);
88 }
89
TEST(cmd_inject,exclude_perf_option)90 TEST(cmd_inject, exclude_perf_option) {
91 ASSERT_TRUE(RunInjectCmd({"--exclude-perf"}, nullptr));
92 }
93
TEST(cmd_inject,output_option)94 TEST(cmd_inject, output_option) {
95 TemporaryFile tmpfile;
96 close(tmpfile.release());
97 ASSERT_TRUE(RunInjectCmd({"--output", "autofdo", "-o", tmpfile.path}));
98 ASSERT_TRUE(RunInjectCmd({"--output", "branch-list", "-o", tmpfile.path}));
99 std::string autofdo_data;
100 ASSERT_TRUE(RunInjectCmd({"-i", tmpfile.path, "--output", "autofdo"}, &autofdo_data));
101 CheckMatchingExpectedData(autofdo_data);
102 }
103
TEST(cmd_inject,branch_to_proto_string)104 TEST(cmd_inject, branch_to_proto_string) {
105 std::vector<bool> branch;
106 for (size_t i = 0; i < 100; i++) {
107 branch.push_back(i % 2 == 0);
108 std::string s = BranchToProtoString(branch);
109 for (size_t j = 0; j <= i; j++) {
110 bool b = s[j >> 3] & (1 << (j & 7));
111 ASSERT_EQ(b, branch[j]);
112 }
113 std::vector<bool> branch2 = ProtoStringToBranch(s, branch.size());
114 ASSERT_EQ(branch, branch2);
115 }
116 }
117
TEST(cmd_inject,skip_empty_output_file)118 TEST(cmd_inject, skip_empty_output_file) {
119 TemporaryFile tmpfile;
120 close(tmpfile.release());
121 ASSERT_TRUE(RunInjectCmd(
122 {"--binary", "not_exist_binary", "--output", "branch-list", "-o", tmpfile.path}));
123 // The empty output file should not be produced.
124 ASSERT_FALSE(IsRegularFile(tmpfile.path));
125 tmpfile.DoNotRemove();
126 }
127
TEST(cmd_inject,inject_kernel_data)128 TEST(cmd_inject, inject_kernel_data) {
129 const std::string recording_file =
130 GetTestData(std::string("etm") + OS_PATH_SEPARATOR + "perf_kernel.data");
131
132 // Inject directly to autofdo format.
133 TemporaryFile tmpfile;
134 close(tmpfile.release());
135 ASSERT_TRUE(RunInjectCmd({"-i", recording_file, "-o", tmpfile.path}));
136 std::string autofdo_output;
137 ASSERT_TRUE(android::base::ReadFileToString(tmpfile.path, &autofdo_output));
138 ASSERT_NE(autofdo_output.find("rq_stats.ko"), std::string::npos);
139
140 // Inject through etm branch list.
141 TemporaryFile tmpfile2;
142 close(tmpfile2.release());
143 ASSERT_TRUE(RunInjectCmd({"-i", recording_file, "-o", tmpfile.path, "--output", "branch-list"}));
144 ASSERT_TRUE(RunInjectCmd({"-i", tmpfile.path, "-o", tmpfile2.path}));
145 std::string output;
146 ASSERT_TRUE(android::base::ReadFileToString(tmpfile2.path, &output));
147 ASSERT_EQ(output, autofdo_output);
148 }
149
TEST(cmd_inject,unformatted_trace)150 TEST(cmd_inject, unformatted_trace) {
151 std::string data;
152 std::string perf_with_unformatted_trace =
153 GetTestData(std::string("etm") + OS_PATH_SEPARATOR + "perf_with_unformatted_trace.data");
154 ASSERT_TRUE(RunInjectCmd({"-i", perf_with_unformatted_trace}, &data));
155 // Test that we can find instr range in etm_test_loop binary.
156 ASSERT_NE(data.find("etm_test_loop"), std::string::npos);
157 CheckMatchingExpectedData(data);
158 }
159
TEST(cmd_inject,multiple_input_files)160 TEST(cmd_inject, multiple_input_files) {
161 std::string data;
162 std::string perf_data = GetTestData(std::string("etm") + OS_PATH_SEPARATOR + "perf.data");
163 std::string perf_with_unformatted_trace =
164 GetTestData(std::string("etm") + OS_PATH_SEPARATOR + "perf_with_unformatted_trace.data");
165
166 // Test input files separated by comma.
167 ASSERT_TRUE(RunInjectCmd({"-i", perf_with_unformatted_trace + "," + perf_data}, &data));
168 ASSERT_NE(data.find("106c->1074:200"), std::string::npos);
169
170 // Test input files from different -i options.
171 ASSERT_TRUE(RunInjectCmd({"-i", perf_with_unformatted_trace, "-i", perf_data}, &data));
172 ASSERT_NE(data.find("106c->1074:200"), std::string::npos);
173
174 // Test input files provided by input_file_list.
175 TemporaryFile tmpfile;
176 std::string input_file_list = perf_data + "\n" + perf_with_unformatted_trace + "\n";
177 ASSERT_TRUE(android::base::WriteStringToFd(input_file_list, tmpfile.fd));
178 close(tmpfile.release());
179 ASSERT_TRUE(RunInjectCmd({"-i", std::string("@") + tmpfile.path}, &data));
180 ASSERT_NE(data.find("106c->1074:200"), std::string::npos);
181 }
182
TEST(cmd_inject,merge_branch_list_files)183 TEST(cmd_inject, merge_branch_list_files) {
184 TemporaryFile tmpfile;
185 close(tmpfile.release());
186 ASSERT_TRUE(RunInjectCmd({"--output", "branch-list", "-o", tmpfile.path}));
187 TemporaryFile tmpfile2;
188 close(tmpfile2.release());
189 ASSERT_TRUE(RunInjectCmd({"-i", std::string(tmpfile.path) + "," + tmpfile.path, "--output",
190 "branch-list", "-o", tmpfile2.path}));
191 std::string autofdo_data;
192 ASSERT_TRUE(RunInjectCmd({"-i", tmpfile2.path, "--output", "autofdo"}, &autofdo_data));
193 ASSERT_NE(autofdo_data.find("106c->1074:200"), std::string::npos);
194 }
195
TEST(cmd_inject,report_warning_when_overflow)196 TEST(cmd_inject, report_warning_when_overflow) {
197 CapturedStderr capture;
198 capture.Start();
199 std::vector<std::unique_ptr<TemporaryFile>> branch_list_files;
200 std::vector<std::unique_ptr<TemporaryFile>> input_files;
201
202 branch_list_files.emplace_back(new TemporaryFile);
203 close(branch_list_files.back()->release());
204 ASSERT_TRUE(RunInjectCmd({"--output", "branch-list", "-o", branch_list_files.back()->path}));
205 for (size_t i = 1; i <= 7; i++) {
206 // Create input file list, repeating branch list file for 1000 times.
207 std::string s;
208 for (size_t j = 0; j < 1000; j++) {
209 s += std::string(branch_list_files.back()->path) + "\n";
210 }
211 input_files.emplace_back(new TemporaryFile);
212 ASSERT_TRUE(android::base::WriteStringToFd(s, input_files.back()->fd));
213 close(input_files.back()->release());
214
215 // Merge branch list files.
216 branch_list_files.emplace_back(new TemporaryFile);
217 close(branch_list_files.back()->release());
218 ASSERT_TRUE(
219 RunInjectCmd({"--output", "branch-list", "-i", std::string("@") + input_files.back()->path,
220 "-o", branch_list_files.back()->path}));
221 }
222 capture.Stop();
223 const std::string WARNING_MSG = "Branch count overflow happened.";
224 ASSERT_NE(capture.str().find(WARNING_MSG), std::string::npos);
225
226 // Warning also happens when converting branch lists to AutoFDO format.
227 capture.Reset();
228 capture.Start();
229 std::string autofdo_data;
230 ASSERT_TRUE(RunInjectCmd({"-i", branch_list_files.back()->path}, &autofdo_data));
231 capture.Stop();
232 ASSERT_NE(capture.str().find(WARNING_MSG), std::string::npos);
233 ASSERT_NE(autofdo_data.find("106c->1074:18446744073709551615"), std::string::npos);
234 }
235