blob: c2d23e6d1101cff39d18ff1698bfdc11ebee6591 [file] [log] [blame]
John Reckdf1742e2017-01-19 15:56:21 -08001/*
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
Nolan Scobie24921a02022-11-14 19:35:58 -050017#include <android-base/macros.h>
Nolan Scobie1b576de2023-01-09 18:21:40 -050018#include <gmock/gmock.h>
John Reckdf1742e2017-01-19 15:56:21 -080019#include <gtest/gtest.h>
John Reckdf1742e2017-01-19 15:56:21 -080020#include <stdio.h>
21#include <stdlib.h>
John Reck1bcacfd2017-11-03 10:12:19 -070022#include <sys/stat.h>
23#include <sys/types.h>
John Reckdf1742e2017-01-19 15:56:21 -080024#include <unistd.h>
25
Nolan Scobie24921a02022-11-14 19:35:58 -050026#include "protos/graphicsstats.pb.h"
27#include "service/GraphicsStatsService.h"
28
John Reckdf1742e2017-01-19 15:56:21 -080029using namespace android;
30using namespace android::uirenderer;
31
32std::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;
John Reck1bcacfd2017-11-03 10:12:19 -070038 fprintf(stderr, "Failed to read from /proc/self/exe; r=%zd, err=%d (%s)\n", r, err,
39 strerror(err));
John Reckdf1742e2017-01-19 15:56:21 -080040 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
52TEST(GraphicsStats, findRootPath) {
Nolan Scobie1b576de2023-01-09 18:21:40 -050053 // 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()));
John Reckdf1742e2017-01-19 15:56:21 -080061}
62
63TEST(GraphicsStats, saveLoad) {
64 std::string path = findRootPath() + "/test_saveLoad";
65 std::string packageName = "com.test.saveLoad";
John Reck7075c792017-07-05 14:03:43 -070066 MockProfileData mockData;
67 mockData.editJankFrameCount() = 20;
68 mockData.editTotalFrameCount() = 100;
69 mockData.editStatStartTime() = 10000;
John Reckdf1742e2017-01-19 15:56:21 -080070 // Fill with patterned data we can recognize but which won't map to a
71 // memset or basic for iteration count
John Reck7075c792017-07-05 14:03:43 -070072 for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) {
73 mockData.editFrameCounts()[i] = ((i % 10) + 1) * 2;
John Reckdf1742e2017-01-19 15:56:21 -080074 }
John Reck7075c792017-07-05 14:03:43 -070075 for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) {
76 mockData.editSlowFrameCounts()[i] = (i % 5) + 1;
John Reckdf1742e2017-01-19 15:56:21 -080077 }
78 GraphicsStatsService::saveBuffer(path, packageName, 5, 3000, 7000, &mockData);
Kweku Adams1856a4c2018-04-03 16:31:10 -070079 protos::GraphicsStatsProto loadedProto;
John Reckdf1742e2017-01-19 15:56:21 -080080 EXPECT_TRUE(GraphicsStatsService::parseFromFile(path, &loadedProto));
81 // Clean up the file
82 unlink(path.c_str());
83
84 EXPECT_EQ(packageName, loadedProto.package_name());
85 EXPECT_EQ(5, loadedProto.version_code());
86 EXPECT_EQ(3000, loadedProto.stats_start());
87 EXPECT_EQ(7000, loadedProto.stats_end());
88 // ASSERT here so we don't continue with a nullptr deref crash if this is false
89 ASSERT_TRUE(loadedProto.has_summary());
90 EXPECT_EQ(20, loadedProto.summary().janky_frames());
91 EXPECT_EQ(100, loadedProto.summary().total_frames());
John Reck7075c792017-07-05 14:03:43 -070092 EXPECT_EQ(mockData.editFrameCounts().size() + mockData.editSlowFrameCounts().size(),
John Reck1bcacfd2017-11-03 10:12:19 -070093 (size_t)loadedProto.histogram_size());
94 for (size_t i = 0; i < (size_t)loadedProto.histogram_size(); i++) {
John Reckdf1742e2017-01-19 15:56:21 -080095 int expectedCount, expectedBucket;
John Reck7075c792017-07-05 14:03:43 -070096 if (i < mockData.editFrameCounts().size()) {
John Reckdf1742e2017-01-19 15:56:21 -080097 expectedCount = ((i % 10) + 1) * 2;
John Reck7075c792017-07-05 14:03:43 -070098 expectedBucket = ProfileData::frameTimeForFrameCountIndex(i);
John Reckdf1742e2017-01-19 15:56:21 -080099 } else {
John Reck7075c792017-07-05 14:03:43 -0700100 int temp = i - mockData.editFrameCounts().size();
John Reckdf1742e2017-01-19 15:56:21 -0800101 expectedCount = (temp % 5) + 1;
John Reck7075c792017-07-05 14:03:43 -0700102 expectedBucket = ProfileData::frameTimeForSlowFrameCountIndex(temp);
John Reckdf1742e2017-01-19 15:56:21 -0800103 }
104 EXPECT_EQ(expectedCount, loadedProto.histogram().Get(i).frame_count());
105 EXPECT_EQ(expectedBucket, loadedProto.histogram().Get(i).render_millis());
106 }
107}
108
109TEST(GraphicsStats, merge) {
110 std::string path = findRootPath() + "/test_merge";
111 std::string packageName = "com.test.merge";
John Reck7075c792017-07-05 14:03:43 -0700112 MockProfileData mockData;
113 mockData.editJankFrameCount() = 20;
114 mockData.editTotalFrameCount() = 100;
115 mockData.editStatStartTime() = 10000;
John Reckdf1742e2017-01-19 15:56:21 -0800116 // Fill with patterned data we can recognize but which won't map to a
117 // memset or basic for iteration count
John Reck7075c792017-07-05 14:03:43 -0700118 for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) {
119 mockData.editFrameCounts()[i] = ((i % 10) + 1) * 2;
John Reckdf1742e2017-01-19 15:56:21 -0800120 }
John Reck7075c792017-07-05 14:03:43 -0700121 for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) {
122 mockData.editSlowFrameCounts()[i] = (i % 5) + 1;
John Reckdf1742e2017-01-19 15:56:21 -0800123 }
124 GraphicsStatsService::saveBuffer(path, packageName, 5, 3000, 7000, &mockData);
John Reck7075c792017-07-05 14:03:43 -0700125 mockData.editJankFrameCount() = 50;
126 mockData.editTotalFrameCount() = 500;
127 for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) {
128 mockData.editFrameCounts()[i] = (i % 5) + 1;
John Reckdf1742e2017-01-19 15:56:21 -0800129 }
John Reck7075c792017-07-05 14:03:43 -0700130 for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) {
131 mockData.editSlowFrameCounts()[i] = ((i % 10) + 1) * 2;
John Reckdf1742e2017-01-19 15:56:21 -0800132 }
133 GraphicsStatsService::saveBuffer(path, packageName, 5, 7050, 10000, &mockData);
134
Kweku Adams1856a4c2018-04-03 16:31:10 -0700135 protos::GraphicsStatsProto loadedProto;
John Reckdf1742e2017-01-19 15:56:21 -0800136 EXPECT_TRUE(GraphicsStatsService::parseFromFile(path, &loadedProto));
137 // Clean up the file
138 unlink(path.c_str());
139
140 EXPECT_EQ(packageName, loadedProto.package_name());
141 EXPECT_EQ(5, loadedProto.version_code());
142 EXPECT_EQ(3000, loadedProto.stats_start());
143 EXPECT_EQ(10000, loadedProto.stats_end());
144 // ASSERT here so we don't continue with a nullptr deref crash if this is false
145 ASSERT_TRUE(loadedProto.has_summary());
146 EXPECT_EQ(20 + 50, loadedProto.summary().janky_frames());
147 EXPECT_EQ(100 + 500, loadedProto.summary().total_frames());
John Reck7075c792017-07-05 14:03:43 -0700148 EXPECT_EQ(mockData.editFrameCounts().size() + mockData.editSlowFrameCounts().size(),
John Reck1bcacfd2017-11-03 10:12:19 -0700149 (size_t)loadedProto.histogram_size());
150 for (size_t i = 0; i < (size_t)loadedProto.histogram_size(); i++) {
John Reckdf1742e2017-01-19 15:56:21 -0800151 int expectedCount, expectedBucket;
John Reck7075c792017-07-05 14:03:43 -0700152 if (i < mockData.editFrameCounts().size()) {
John Reckdf1742e2017-01-19 15:56:21 -0800153 expectedCount = ((i % 10) + 1) * 2;
154 expectedCount += (i % 5) + 1;
John Reck7075c792017-07-05 14:03:43 -0700155 expectedBucket = ProfileData::frameTimeForFrameCountIndex(i);
John Reckdf1742e2017-01-19 15:56:21 -0800156 } else {
John Reck7075c792017-07-05 14:03:43 -0700157 int temp = i - mockData.editFrameCounts().size();
John Reckdf1742e2017-01-19 15:56:21 -0800158 expectedCount = (temp % 5) + 1;
159 expectedCount += ((temp % 10) + 1) * 2;
John Reck7075c792017-07-05 14:03:43 -0700160 expectedBucket = ProfileData::frameTimeForSlowFrameCountIndex(temp);
John Reckdf1742e2017-01-19 15:56:21 -0800161 }
162 EXPECT_EQ(expectedCount, loadedProto.histogram().Get(i).frame_count());
163 EXPECT_EQ(expectedBucket, loadedProto.histogram().Get(i).render_millis());
164 }
Colin Cross99c9bf62017-05-04 09:58:59 -0700165}