blob: eb164f97c9a28a06de78646a623534bb1046e44e [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";
Alec Mouri04b0d812024-09-20 16:15:55 +000065 uid_t uid = 123;
John Reckdf1742e2017-01-19 15:56:21 -080066 std::string packageName = "com.test.saveLoad";
John Reck7075c792017-07-05 14:03:43 -070067 MockProfileData mockData;
68 mockData.editJankFrameCount() = 20;
69 mockData.editTotalFrameCount() = 100;
70 mockData.editStatStartTime() = 10000;
John Reckdf1742e2017-01-19 15:56:21 -080071 // Fill with patterned data we can recognize but which won't map to a
72 // memset or basic for iteration count
John Reck7075c792017-07-05 14:03:43 -070073 for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) {
74 mockData.editFrameCounts()[i] = ((i % 10) + 1) * 2;
John Reckdf1742e2017-01-19 15:56:21 -080075 }
John Reck7075c792017-07-05 14:03:43 -070076 for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) {
77 mockData.editSlowFrameCounts()[i] = (i % 5) + 1;
John Reckdf1742e2017-01-19 15:56:21 -080078 }
Alec Mouri04b0d812024-09-20 16:15:55 +000079 GraphicsStatsService::saveBuffer(path, uid, packageName, 5, 3000, 7000, &mockData);
Kweku Adams1856a4c2018-04-03 16:31:10 -070080 protos::GraphicsStatsProto loadedProto;
John Reckdf1742e2017-01-19 15:56:21 -080081 EXPECT_TRUE(GraphicsStatsService::parseFromFile(path, &loadedProto));
82 // Clean up the file
83 unlink(path.c_str());
84
Alec Mouri04b0d812024-09-20 16:15:55 +000085 EXPECT_EQ(uid, loadedProto.uid());
John Reckdf1742e2017-01-19 15:56:21 -080086 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());
John Reck7075c792017-07-05 14:03:43 -070094 EXPECT_EQ(mockData.editFrameCounts().size() + mockData.editSlowFrameCounts().size(),
John Reck1bcacfd2017-11-03 10:12:19 -070095 (size_t)loadedProto.histogram_size());
96 for (size_t i = 0; i < (size_t)loadedProto.histogram_size(); i++) {
John Reckdf1742e2017-01-19 15:56:21 -080097 int expectedCount, expectedBucket;
John Reck7075c792017-07-05 14:03:43 -070098 if (i < mockData.editFrameCounts().size()) {
John Reckdf1742e2017-01-19 15:56:21 -080099 expectedCount = ((i % 10) + 1) * 2;
John Reck7075c792017-07-05 14:03:43 -0700100 expectedBucket = ProfileData::frameTimeForFrameCountIndex(i);
John Reckdf1742e2017-01-19 15:56:21 -0800101 } else {
John Reck7075c792017-07-05 14:03:43 -0700102 int temp = i - mockData.editFrameCounts().size();
John Reckdf1742e2017-01-19 15:56:21 -0800103 expectedCount = (temp % 5) + 1;
John Reck7075c792017-07-05 14:03:43 -0700104 expectedBucket = ProfileData::frameTimeForSlowFrameCountIndex(temp);
John Reckdf1742e2017-01-19 15:56:21 -0800105 }
106 EXPECT_EQ(expectedCount, loadedProto.histogram().Get(i).frame_count());
107 EXPECT_EQ(expectedBucket, loadedProto.histogram().Get(i).render_millis());
108 }
109}
110
111TEST(GraphicsStats, merge) {
112 std::string path = findRootPath() + "/test_merge";
113 std::string packageName = "com.test.merge";
Alec Mouri04b0d812024-09-20 16:15:55 +0000114 uid_t uid = 123;
John Reck7075c792017-07-05 14:03:43 -0700115 MockProfileData mockData;
116 mockData.editJankFrameCount() = 20;
117 mockData.editTotalFrameCount() = 100;
118 mockData.editStatStartTime() = 10000;
John Reckdf1742e2017-01-19 15:56:21 -0800119 // Fill with patterned data we can recognize but which won't map to a
120 // memset or basic for iteration count
John Reck7075c792017-07-05 14:03:43 -0700121 for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) {
122 mockData.editFrameCounts()[i] = ((i % 10) + 1) * 2;
John Reckdf1742e2017-01-19 15:56:21 -0800123 }
John Reck7075c792017-07-05 14:03:43 -0700124 for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) {
125 mockData.editSlowFrameCounts()[i] = (i % 5) + 1;
John Reckdf1742e2017-01-19 15:56:21 -0800126 }
Alec Mouri04b0d812024-09-20 16:15:55 +0000127 GraphicsStatsService::saveBuffer(path, uid, packageName, 5, 3000, 7000, &mockData);
John Reck7075c792017-07-05 14:03:43 -0700128 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;
John Reckdf1742e2017-01-19 15:56:21 -0800132 }
John Reck7075c792017-07-05 14:03:43 -0700133 for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) {
134 mockData.editSlowFrameCounts()[i] = ((i % 10) + 1) * 2;
John Reckdf1742e2017-01-19 15:56:21 -0800135 }
Alec Mouri04b0d812024-09-20 16:15:55 +0000136
137 GraphicsStatsService::saveBuffer(path, uid, packageName, 5, 7050, 10000, &mockData);
John Reckdf1742e2017-01-19 15:56:21 -0800138
Kweku Adams1856a4c2018-04-03 16:31:10 -0700139 protos::GraphicsStatsProto loadedProto;
John Reckdf1742e2017-01-19 15:56:21 -0800140 EXPECT_TRUE(GraphicsStatsService::parseFromFile(path, &loadedProto));
141 // Clean up the file
142 unlink(path.c_str());
143
Alec Mouri04b0d812024-09-20 16:15:55 +0000144 EXPECT_EQ(uid, loadedProto.uid());
John Reckdf1742e2017-01-19 15:56:21 -0800145 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());
John Reck7075c792017-07-05 14:03:43 -0700153 EXPECT_EQ(mockData.editFrameCounts().size() + mockData.editSlowFrameCounts().size(),
John Reck1bcacfd2017-11-03 10:12:19 -0700154 (size_t)loadedProto.histogram_size());
155 for (size_t i = 0; i < (size_t)loadedProto.histogram_size(); i++) {
John Reckdf1742e2017-01-19 15:56:21 -0800156 int expectedCount, expectedBucket;
John Reck7075c792017-07-05 14:03:43 -0700157 if (i < mockData.editFrameCounts().size()) {
John Reckdf1742e2017-01-19 15:56:21 -0800158 expectedCount = ((i % 10) + 1) * 2;
159 expectedCount += (i % 5) + 1;
John Reck7075c792017-07-05 14:03:43 -0700160 expectedBucket = ProfileData::frameTimeForFrameCountIndex(i);
John Reckdf1742e2017-01-19 15:56:21 -0800161 } else {
John Reck7075c792017-07-05 14:03:43 -0700162 int temp = i - mockData.editFrameCounts().size();
John Reckdf1742e2017-01-19 15:56:21 -0800163 expectedCount = (temp % 5) + 1;
164 expectedCount += ((temp % 10) + 1) * 2;
John Reck7075c792017-07-05 14:03:43 -0700165 expectedBucket = ProfileData::frameTimeForSlowFrameCountIndex(temp);
John Reckdf1742e2017-01-19 15:56:21 -0800166 }
167 EXPECT_EQ(expectedCount, loadedProto.histogram().Get(i).frame_count());
168 EXPECT_EQ(expectedBucket, loadedProto.histogram().Get(i).render_millis());
169 }
Colin Cross99c9bf62017-05-04 09:58:59 -0700170}