1 /*
2 * Copyright (C) 2020 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 <stdio.h>
20
21 #include "command.h"
22 #include "get_test_data.h"
23 #include "record_file.h"
24 #include "test_util.h"
25 #include "utils.h"
26
27 #if defined(__ANDROID__)
28
WaitUntilAppExit(const std::string & package_name)29 static bool WaitUntilAppExit(const std::string& package_name) {
30 while (true) {
31 std::unique_ptr<FILE, decltype(&pclose)> fp(popen("ps -e", "re"), pclose);
32 if (!fp) {
33 return false;
34 }
35 std::string s;
36 if (!android::base::ReadFdToString(fileno(fp.get()), &s)) {
37 return false;
38 }
39 if (s.find(package_name) == std::string::npos) {
40 break;
41 }
42 sleep(1);
43 }
44 return true;
45 }
46
CheckPerfDataFile(const std::string & filename)47 static void CheckPerfDataFile(const std::string& filename) {
48 auto reader = RecordFileReader::CreateInstance(filename);
49 ASSERT_TRUE(reader);
50 bool has_sample = false;
51 ASSERT_TRUE(reader->ReadDataSection([&](std::unique_ptr<Record> r) {
52 if (r->type() == PERF_RECORD_SAMPLE) {
53 has_sample = true;
54 }
55 return true;
56 }));
57 ASSERT_TRUE(has_sample);
58 }
59
RecordApp(const std::string & package_name,const std::string & apk_path)60 static void RecordApp(const std::string& package_name, const std::string& apk_path) {
61 // 1. Prepare recording.
62 ASSERT_TRUE(CreateCommandInstance("api-prepare")->Run({}));
63
64 // 2. Install apk and start the app.
65 AppHelper app_helper;
66 ASSERT_TRUE(app_helper.InstallApk(apk_path, package_name));
67 ASSERT_TRUE(app_helper.StartApp("am start " + package_name + "/.MainActivity"));
68
69 // 3. Wait until the app stops.
70 sleep(3);
71 ASSERT_TRUE(WaitUntilAppExit(package_name));
72
73 // 4. Collect perf.data.
74 SetRunInAppToolForTesting(true, true);
75 TemporaryFile tmpfile;
76 ASSERT_TRUE(
77 CreateCommandInstance("api-collect")->Run({"--app", package_name, "-o", tmpfile.path}));
78
79 // 5. Verify perf.data.
80 TemporaryDir tmpdir;
81 ASSERT_TRUE(Workload::RunCmd({"unzip", "-d", tmpdir.path, tmpfile.path}));
82 for (const std::string& filename : GetEntriesInDir(tmpdir.path)) {
83 CheckPerfDataFile(std::string(tmpdir.path) + "/" + filename);
84 }
85 }
86
87 #endif // defined(__ANDROID__)
88
TEST(cmd_api,java_app)89 TEST(cmd_api, java_app) {
90 #if defined(__ANDROID__)
91 RecordApp("simpleperf.demo.java_api", GetTestData("java_api.apk"));
92 #else
93 GTEST_LOG_(INFO) << "This test tests recording apps on Android.";
94 #endif
95 }
96
TEST(cmd_api,native_app)97 TEST(cmd_api, native_app) {
98 #if defined(__ANDROID__)
99 RecordApp("simpleperf.demo.cpp_api", GetTestData("cpp_api.apk"));
100 #else
101 GTEST_LOG_(INFO) << "This test tests recording apps on Android.";
102 #endif
103 }