blob: 92fd8294a486a0efb16bbf698fc1764205cbbaca [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>
John Reckdf1742e2017-01-19 15:56:21 -080018#include <gtest/gtest.h>
John Reckdf1742e2017-01-19 15:56:21 -080019#include <stdio.h>
20#include <stdlib.h>
John Reck1bcacfd2017-11-03 10:12:19 -070021#include <sys/stat.h>
22#include <sys/types.h>
John Reckdf1742e2017-01-19 15:56:21 -080023#include <unistd.h>
24
Nolan Scobie24921a02022-11-14 19:35:58 -050025#include "protos/graphicsstats.pb.h"
26#include "service/GraphicsStatsService.h"
27
John Reckdf1742e2017-01-19 15:56:21 -080028using namespace android;
29using namespace android::uirenderer;
30
31std::string findRootPath() {
32 char path[1024];
33 ssize_t r = readlink("/proc/self/exe", path, 1024);
34 // < 1023 because we need room for the null terminator
35 if (r <= 0 || r > 1023) {
36 int err = errno;
John Reck1bcacfd2017-11-03 10:12:19 -070037 fprintf(stderr, "Failed to read from /proc/self/exe; r=%zd, err=%d (%s)\n", r, err,
38 strerror(err));
John Reckdf1742e2017-01-19 15:56:21 -080039 exit(EXIT_FAILURE);
40 }
41 while (--r > 0) {
42 if (path[r] == '/') {
43 path[r] = '\0';
44 return std::string(path);
45 }
46 }
47 return std::string();
48}
49
50// No code left untested
51TEST(GraphicsStats, findRootPath) {
Nolan Scobie24921a02022-11-14 19:35:58 -050052 std::string expected = "/data/local/tmp/nativetest/hwui_unit_tests/" ABI_STRING;
John Reckdf1742e2017-01-19 15:56:21 -080053 EXPECT_EQ(expected, findRootPath());
54}
55
56TEST(GraphicsStats, saveLoad) {
57 std::string path = findRootPath() + "/test_saveLoad";
58 std::string packageName = "com.test.saveLoad";
John Reck7075c792017-07-05 14:03:43 -070059 MockProfileData mockData;
60 mockData.editJankFrameCount() = 20;
61 mockData.editTotalFrameCount() = 100;
62 mockData.editStatStartTime() = 10000;
John Reckdf1742e2017-01-19 15:56:21 -080063 // Fill with patterned data we can recognize but which won't map to a
64 // memset or basic for iteration count
John Reck7075c792017-07-05 14:03:43 -070065 for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) {
66 mockData.editFrameCounts()[i] = ((i % 10) + 1) * 2;
John Reckdf1742e2017-01-19 15:56:21 -080067 }
John Reck7075c792017-07-05 14:03:43 -070068 for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) {
69 mockData.editSlowFrameCounts()[i] = (i % 5) + 1;
John Reckdf1742e2017-01-19 15:56:21 -080070 }
71 GraphicsStatsService::saveBuffer(path, packageName, 5, 3000, 7000, &mockData);
Kweku Adams1856a4c2018-04-03 16:31:10 -070072 protos::GraphicsStatsProto loadedProto;
John Reckdf1742e2017-01-19 15:56:21 -080073 EXPECT_TRUE(GraphicsStatsService::parseFromFile(path, &loadedProto));
74 // Clean up the file
75 unlink(path.c_str());
76
77 EXPECT_EQ(packageName, loadedProto.package_name());
78 EXPECT_EQ(5, loadedProto.version_code());
79 EXPECT_EQ(3000, loadedProto.stats_start());
80 EXPECT_EQ(7000, loadedProto.stats_end());
81 // ASSERT here so we don't continue with a nullptr deref crash if this is false
82 ASSERT_TRUE(loadedProto.has_summary());
83 EXPECT_EQ(20, loadedProto.summary().janky_frames());
84 EXPECT_EQ(100, loadedProto.summary().total_frames());
John Reck7075c792017-07-05 14:03:43 -070085 EXPECT_EQ(mockData.editFrameCounts().size() + mockData.editSlowFrameCounts().size(),
John Reck1bcacfd2017-11-03 10:12:19 -070086 (size_t)loadedProto.histogram_size());
87 for (size_t i = 0; i < (size_t)loadedProto.histogram_size(); i++) {
John Reckdf1742e2017-01-19 15:56:21 -080088 int expectedCount, expectedBucket;
John Reck7075c792017-07-05 14:03:43 -070089 if (i < mockData.editFrameCounts().size()) {
John Reckdf1742e2017-01-19 15:56:21 -080090 expectedCount = ((i % 10) + 1) * 2;
John Reck7075c792017-07-05 14:03:43 -070091 expectedBucket = ProfileData::frameTimeForFrameCountIndex(i);
John Reckdf1742e2017-01-19 15:56:21 -080092 } else {
John Reck7075c792017-07-05 14:03:43 -070093 int temp = i - mockData.editFrameCounts().size();
John Reckdf1742e2017-01-19 15:56:21 -080094 expectedCount = (temp % 5) + 1;
John Reck7075c792017-07-05 14:03:43 -070095 expectedBucket = ProfileData::frameTimeForSlowFrameCountIndex(temp);
John Reckdf1742e2017-01-19 15:56:21 -080096 }
97 EXPECT_EQ(expectedCount, loadedProto.histogram().Get(i).frame_count());
98 EXPECT_EQ(expectedBucket, loadedProto.histogram().Get(i).render_millis());
99 }
100}
101
102TEST(GraphicsStats, merge) {
103 std::string path = findRootPath() + "/test_merge";
104 std::string packageName = "com.test.merge";
John Reck7075c792017-07-05 14:03:43 -0700105 MockProfileData mockData;
106 mockData.editJankFrameCount() = 20;
107 mockData.editTotalFrameCount() = 100;
108 mockData.editStatStartTime() = 10000;
John Reckdf1742e2017-01-19 15:56:21 -0800109 // Fill with patterned data we can recognize but which won't map to a
110 // memset or basic for iteration count
John Reck7075c792017-07-05 14:03:43 -0700111 for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) {
112 mockData.editFrameCounts()[i] = ((i % 10) + 1) * 2;
John Reckdf1742e2017-01-19 15:56:21 -0800113 }
John Reck7075c792017-07-05 14:03:43 -0700114 for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) {
115 mockData.editSlowFrameCounts()[i] = (i % 5) + 1;
John Reckdf1742e2017-01-19 15:56:21 -0800116 }
117 GraphicsStatsService::saveBuffer(path, packageName, 5, 3000, 7000, &mockData);
John Reck7075c792017-07-05 14:03:43 -0700118 mockData.editJankFrameCount() = 50;
119 mockData.editTotalFrameCount() = 500;
120 for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) {
121 mockData.editFrameCounts()[i] = (i % 5) + 1;
John Reckdf1742e2017-01-19 15:56:21 -0800122 }
John Reck7075c792017-07-05 14:03:43 -0700123 for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) {
124 mockData.editSlowFrameCounts()[i] = ((i % 10) + 1) * 2;
John Reckdf1742e2017-01-19 15:56:21 -0800125 }
126 GraphicsStatsService::saveBuffer(path, packageName, 5, 7050, 10000, &mockData);
127
Kweku Adams1856a4c2018-04-03 16:31:10 -0700128 protos::GraphicsStatsProto loadedProto;
John Reckdf1742e2017-01-19 15:56:21 -0800129 EXPECT_TRUE(GraphicsStatsService::parseFromFile(path, &loadedProto));
130 // Clean up the file
131 unlink(path.c_str());
132
133 EXPECT_EQ(packageName, loadedProto.package_name());
134 EXPECT_EQ(5, loadedProto.version_code());
135 EXPECT_EQ(3000, loadedProto.stats_start());
136 EXPECT_EQ(10000, loadedProto.stats_end());
137 // ASSERT here so we don't continue with a nullptr deref crash if this is false
138 ASSERT_TRUE(loadedProto.has_summary());
139 EXPECT_EQ(20 + 50, loadedProto.summary().janky_frames());
140 EXPECT_EQ(100 + 500, loadedProto.summary().total_frames());
John Reck7075c792017-07-05 14:03:43 -0700141 EXPECT_EQ(mockData.editFrameCounts().size() + mockData.editSlowFrameCounts().size(),
John Reck1bcacfd2017-11-03 10:12:19 -0700142 (size_t)loadedProto.histogram_size());
143 for (size_t i = 0; i < (size_t)loadedProto.histogram_size(); i++) {
John Reckdf1742e2017-01-19 15:56:21 -0800144 int expectedCount, expectedBucket;
John Reck7075c792017-07-05 14:03:43 -0700145 if (i < mockData.editFrameCounts().size()) {
John Reckdf1742e2017-01-19 15:56:21 -0800146 expectedCount = ((i % 10) + 1) * 2;
147 expectedCount += (i % 5) + 1;
John Reck7075c792017-07-05 14:03:43 -0700148 expectedBucket = ProfileData::frameTimeForFrameCountIndex(i);
John Reckdf1742e2017-01-19 15:56:21 -0800149 } else {
John Reck7075c792017-07-05 14:03:43 -0700150 int temp = i - mockData.editFrameCounts().size();
John Reckdf1742e2017-01-19 15:56:21 -0800151 expectedCount = (temp % 5) + 1;
152 expectedCount += ((temp % 10) + 1) * 2;
John Reck7075c792017-07-05 14:03:43 -0700153 expectedBucket = ProfileData::frameTimeForSlowFrameCountIndex(temp);
John Reckdf1742e2017-01-19 15:56:21 -0800154 }
155 EXPECT_EQ(expectedCount, loadedProto.histogram().Get(i).frame_count());
156 EXPECT_EQ(expectedBucket, loadedProto.histogram().Get(i).render_millis());
157 }
Colin Cross99c9bf62017-05-04 09:58:59 -0700158}