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