1 /*
2 * Copyright (C) 2016 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/macros.h>
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25
26 #include "protos/graphicsstats.pb.h"
27 #include "service/GraphicsStatsService.h"
28
29 using namespace android;
30 using namespace android::uirenderer;
31
findRootPath()32 std::string findRootPath() {
33 char path[1024];
34 ssize_t r = readlink("/proc/self/exe", path, 1024);
35 // < 1023 because we need room for the null terminator
36 if (r <= 0 || r > 1023) {
37 int err = errno;
38 fprintf(stderr, "Failed to read from /proc/self/exe; r=%zd, err=%d (%s)\n", r, err,
39 strerror(err));
40 exit(EXIT_FAILURE);
41 }
42 while (--r > 0) {
43 if (path[r] == '/') {
44 path[r] = '\0';
45 return std::string(path);
46 }
47 }
48 return std::string();
49 }
50
51 // No code left untested
TEST(GraphicsStats,findRootPath)52 TEST(GraphicsStats, findRootPath) {
53 // Different tools/infrastructure seem to push this to different locations. It shouldn't really
54 // matter where the binary is, so add new locations here as needed. This test still seems good
55 // as it's nice to understand the possibility space, and ensure findRootPath continues working
56 // as expected.
57 std::string acceptableLocations[] = {"/data/nativetest/hwui_unit_tests",
58 "/data/nativetest64/hwui_unit_tests",
59 "/data/local/tmp/nativetest/hwui_unit_tests/" ABI_STRING};
60 EXPECT_THAT(acceptableLocations, ::testing::Contains(findRootPath()));
61 }
62
TEST(GraphicsStats,saveLoad)63 TEST(GraphicsStats, saveLoad) {
64 std::string path = findRootPath() + "/test_saveLoad";
65 uid_t uid = 123;
66 std::string packageName = "com.test.saveLoad";
67 MockProfileData mockData;
68 mockData.editJankFrameCount() = 20;
69 mockData.editTotalFrameCount() = 100;
70 mockData.editStatStartTime() = 10000;
71 // Fill with patterned data we can recognize but which won't map to a
72 // memset or basic for iteration count
73 for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) {
74 mockData.editFrameCounts()[i] = ((i % 10) + 1) * 2;
75 }
76 for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) {
77 mockData.editSlowFrameCounts()[i] = (i % 5) + 1;
78 }
79 GraphicsStatsService::saveBuffer(path, uid, packageName, 5, 3000, 7000, &mockData);
80 protos::GraphicsStatsProto loadedProto;
81 EXPECT_TRUE(GraphicsStatsService::parseFromFile(path, &loadedProto));
82 // Clean up the file
83 unlink(path.c_str());
84
85 EXPECT_EQ(uid, loadedProto.uid());
86 EXPECT_EQ(packageName, loadedProto.package_name());
87 EXPECT_EQ(5, loadedProto.version_code());
88 EXPECT_EQ(3000, loadedProto.stats_start());
89 EXPECT_EQ(7000, loadedProto.stats_end());
90 // ASSERT here so we don't continue with a nullptr deref crash if this is false
91 ASSERT_TRUE(loadedProto.has_summary());
92 EXPECT_EQ(20, loadedProto.summary().janky_frames());
93 EXPECT_EQ(100, loadedProto.summary().total_frames());
94 EXPECT_EQ(mockData.editFrameCounts().size() + mockData.editSlowFrameCounts().size(),
95 (size_t)loadedProto.histogram_size());
96 for (size_t i = 0; i < (size_t)loadedProto.histogram_size(); i++) {
97 int expectedCount, expectedBucket;
98 if (i < mockData.editFrameCounts().size()) {
99 expectedCount = ((i % 10) + 1) * 2;
100 expectedBucket = ProfileData::frameTimeForFrameCountIndex(i);
101 } else {
102 int temp = i - mockData.editFrameCounts().size();
103 expectedCount = (temp % 5) + 1;
104 expectedBucket = ProfileData::frameTimeForSlowFrameCountIndex(temp);
105 }
106 EXPECT_EQ(expectedCount, loadedProto.histogram().Get(i).frame_count());
107 EXPECT_EQ(expectedBucket, loadedProto.histogram().Get(i).render_millis());
108 }
109 }
110
TEST(GraphicsStats,merge)111 TEST(GraphicsStats, merge) {
112 std::string path = findRootPath() + "/test_merge";
113 std::string packageName = "com.test.merge";
114 uid_t uid = 123;
115 MockProfileData mockData;
116 mockData.editJankFrameCount() = 20;
117 mockData.editTotalFrameCount() = 100;
118 mockData.editStatStartTime() = 10000;
119 // Fill with patterned data we can recognize but which won't map to a
120 // memset or basic for iteration count
121 for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) {
122 mockData.editFrameCounts()[i] = ((i % 10) + 1) * 2;
123 }
124 for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) {
125 mockData.editSlowFrameCounts()[i] = (i % 5) + 1;
126 }
127 GraphicsStatsService::saveBuffer(path, uid, packageName, 5, 3000, 7000, &mockData);
128 mockData.editJankFrameCount() = 50;
129 mockData.editTotalFrameCount() = 500;
130 for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) {
131 mockData.editFrameCounts()[i] = (i % 5) + 1;
132 }
133 for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) {
134 mockData.editSlowFrameCounts()[i] = ((i % 10) + 1) * 2;
135 }
136
137 GraphicsStatsService::saveBuffer(path, uid, packageName, 5, 7050, 10000, &mockData);
138
139 protos::GraphicsStatsProto loadedProto;
140 EXPECT_TRUE(GraphicsStatsService::parseFromFile(path, &loadedProto));
141 // Clean up the file
142 unlink(path.c_str());
143
144 EXPECT_EQ(uid, loadedProto.uid());
145 EXPECT_EQ(packageName, loadedProto.package_name());
146 EXPECT_EQ(5, loadedProto.version_code());
147 EXPECT_EQ(3000, loadedProto.stats_start());
148 EXPECT_EQ(10000, loadedProto.stats_end());
149 // ASSERT here so we don't continue with a nullptr deref crash if this is false
150 ASSERT_TRUE(loadedProto.has_summary());
151 EXPECT_EQ(20 + 50, loadedProto.summary().janky_frames());
152 EXPECT_EQ(100 + 500, loadedProto.summary().total_frames());
153 EXPECT_EQ(mockData.editFrameCounts().size() + mockData.editSlowFrameCounts().size(),
154 (size_t)loadedProto.histogram_size());
155 for (size_t i = 0; i < (size_t)loadedProto.histogram_size(); i++) {
156 int expectedCount, expectedBucket;
157 if (i < mockData.editFrameCounts().size()) {
158 expectedCount = ((i % 10) + 1) * 2;
159 expectedCount += (i % 5) + 1;
160 expectedBucket = ProfileData::frameTimeForFrameCountIndex(i);
161 } else {
162 int temp = i - mockData.editFrameCounts().size();
163 expectedCount = (temp % 5) + 1;
164 expectedCount += ((temp % 10) + 1) * 2;
165 expectedBucket = ProfileData::frameTimeForSlowFrameCountIndex(temp);
166 }
167 EXPECT_EQ(expectedCount, loadedProto.histogram().Get(i).frame_count());
168 EXPECT_EQ(expectedBucket, loadedProto.histogram().Get(i).render_millis());
169 }
170 }
171