blob: 30c3b4ea5ab372bf07f98e3cd3931c1153def560 [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
17#include <gtest/gtest.h>
18
19#include "service/GraphicsStatsService.h"
20
21#include <frameworks/base/core/proto/android/service/graphicsstats.pb.h>
22
John Reckdf1742e2017-01-19 15:56:21 -080023#include <stdio.h>
24#include <stdlib.h>
John Reck1bcacfd2017-11-03 10:12:19 -070025#include <sys/stat.h>
26#include <sys/types.h>
John Reckdf1742e2017-01-19 15:56:21 -080027#include <unistd.h>
28
29using 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) {
Colin Cross99c9bf62017-05-04 09:58:59 -070053#ifdef __LP64__
54 std::string expected = "/data/nativetest64/hwui_unit_tests";
55#else
John Reckdf1742e2017-01-19 15:56:21 -080056 std::string expected = "/data/nativetest/hwui_unit_tests";
Colin Cross99c9bf62017-05-04 09:58:59 -070057#endif
John Reckdf1742e2017-01-19 15:56:21 -080058 EXPECT_EQ(expected, findRootPath());
59}
60
61TEST(GraphicsStats, saveLoad) {
62 std::string path = findRootPath() + "/test_saveLoad";
63 std::string packageName = "com.test.saveLoad";
John Reck7075c792017-07-05 14:03:43 -070064 MockProfileData mockData;
65 mockData.editJankFrameCount() = 20;
66 mockData.editTotalFrameCount() = 100;
67 mockData.editStatStartTime() = 10000;
John Reckdf1742e2017-01-19 15:56:21 -080068 // Fill with patterned data we can recognize but which won't map to a
69 // memset or basic for iteration count
John Reck7075c792017-07-05 14:03:43 -070070 for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) {
71 mockData.editFrameCounts()[i] = ((i % 10) + 1) * 2;
John Reckdf1742e2017-01-19 15:56:21 -080072 }
John Reck7075c792017-07-05 14:03:43 -070073 for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) {
74 mockData.editSlowFrameCounts()[i] = (i % 5) + 1;
John Reckdf1742e2017-01-19 15:56:21 -080075 }
76 GraphicsStatsService::saveBuffer(path, packageName, 5, 3000, 7000, &mockData);
77 service::GraphicsStatsProto loadedProto;
78 EXPECT_TRUE(GraphicsStatsService::parseFromFile(path, &loadedProto));
79 // Clean up the file
80 unlink(path.c_str());
81
82 EXPECT_EQ(packageName, loadedProto.package_name());
83 EXPECT_EQ(5, loadedProto.version_code());
84 EXPECT_EQ(3000, loadedProto.stats_start());
85 EXPECT_EQ(7000, loadedProto.stats_end());
86 // ASSERT here so we don't continue with a nullptr deref crash if this is false
87 ASSERT_TRUE(loadedProto.has_summary());
88 EXPECT_EQ(20, loadedProto.summary().janky_frames());
89 EXPECT_EQ(100, loadedProto.summary().total_frames());
John Reck7075c792017-07-05 14:03:43 -070090 EXPECT_EQ(mockData.editFrameCounts().size() + mockData.editSlowFrameCounts().size(),
John Reck1bcacfd2017-11-03 10:12:19 -070091 (size_t)loadedProto.histogram_size());
92 for (size_t i = 0; i < (size_t)loadedProto.histogram_size(); i++) {
John Reckdf1742e2017-01-19 15:56:21 -080093 int expectedCount, expectedBucket;
John Reck7075c792017-07-05 14:03:43 -070094 if (i < mockData.editFrameCounts().size()) {
John Reckdf1742e2017-01-19 15:56:21 -080095 expectedCount = ((i % 10) + 1) * 2;
John Reck7075c792017-07-05 14:03:43 -070096 expectedBucket = ProfileData::frameTimeForFrameCountIndex(i);
John Reckdf1742e2017-01-19 15:56:21 -080097 } else {
John Reck7075c792017-07-05 14:03:43 -070098 int temp = i - mockData.editFrameCounts().size();
John Reckdf1742e2017-01-19 15:56:21 -080099 expectedCount = (temp % 5) + 1;
John Reck7075c792017-07-05 14:03:43 -0700100 expectedBucket = ProfileData::frameTimeForSlowFrameCountIndex(temp);
John Reckdf1742e2017-01-19 15:56:21 -0800101 }
102 EXPECT_EQ(expectedCount, loadedProto.histogram().Get(i).frame_count());
103 EXPECT_EQ(expectedBucket, loadedProto.histogram().Get(i).render_millis());
104 }
105}
106
107TEST(GraphicsStats, merge) {
108 std::string path = findRootPath() + "/test_merge";
109 std::string packageName = "com.test.merge";
John Reck7075c792017-07-05 14:03:43 -0700110 MockProfileData mockData;
111 mockData.editJankFrameCount() = 20;
112 mockData.editTotalFrameCount() = 100;
113 mockData.editStatStartTime() = 10000;
John Reckdf1742e2017-01-19 15:56:21 -0800114 // Fill with patterned data we can recognize but which won't map to a
115 // memset or basic for iteration count
John Reck7075c792017-07-05 14:03:43 -0700116 for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) {
117 mockData.editFrameCounts()[i] = ((i % 10) + 1) * 2;
John Reckdf1742e2017-01-19 15:56:21 -0800118 }
John Reck7075c792017-07-05 14:03:43 -0700119 for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) {
120 mockData.editSlowFrameCounts()[i] = (i % 5) + 1;
John Reckdf1742e2017-01-19 15:56:21 -0800121 }
122 GraphicsStatsService::saveBuffer(path, packageName, 5, 3000, 7000, &mockData);
John Reck7075c792017-07-05 14:03:43 -0700123 mockData.editJankFrameCount() = 50;
124 mockData.editTotalFrameCount() = 500;
125 for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) {
126 mockData.editFrameCounts()[i] = (i % 5) + 1;
John Reckdf1742e2017-01-19 15:56:21 -0800127 }
John Reck7075c792017-07-05 14:03:43 -0700128 for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) {
129 mockData.editSlowFrameCounts()[i] = ((i % 10) + 1) * 2;
John Reckdf1742e2017-01-19 15:56:21 -0800130 }
131 GraphicsStatsService::saveBuffer(path, packageName, 5, 7050, 10000, &mockData);
132
133 service::GraphicsStatsProto loadedProto;
134 EXPECT_TRUE(GraphicsStatsService::parseFromFile(path, &loadedProto));
135 // Clean up the file
136 unlink(path.c_str());
137
138 EXPECT_EQ(packageName, loadedProto.package_name());
139 EXPECT_EQ(5, loadedProto.version_code());
140 EXPECT_EQ(3000, loadedProto.stats_start());
141 EXPECT_EQ(10000, loadedProto.stats_end());
142 // ASSERT here so we don't continue with a nullptr deref crash if this is false
143 ASSERT_TRUE(loadedProto.has_summary());
144 EXPECT_EQ(20 + 50, loadedProto.summary().janky_frames());
145 EXPECT_EQ(100 + 500, loadedProto.summary().total_frames());
John Reck7075c792017-07-05 14:03:43 -0700146 EXPECT_EQ(mockData.editFrameCounts().size() + mockData.editSlowFrameCounts().size(),
John Reck1bcacfd2017-11-03 10:12:19 -0700147 (size_t)loadedProto.histogram_size());
148 for (size_t i = 0; i < (size_t)loadedProto.histogram_size(); i++) {
John Reckdf1742e2017-01-19 15:56:21 -0800149 int expectedCount, expectedBucket;
John Reck7075c792017-07-05 14:03:43 -0700150 if (i < mockData.editFrameCounts().size()) {
John Reckdf1742e2017-01-19 15:56:21 -0800151 expectedCount = ((i % 10) + 1) * 2;
152 expectedCount += (i % 5) + 1;
John Reck7075c792017-07-05 14:03:43 -0700153 expectedBucket = ProfileData::frameTimeForFrameCountIndex(i);
John Reckdf1742e2017-01-19 15:56:21 -0800154 } else {
John Reck7075c792017-07-05 14:03:43 -0700155 int temp = i - mockData.editFrameCounts().size();
John Reckdf1742e2017-01-19 15:56:21 -0800156 expectedCount = (temp % 5) + 1;
157 expectedCount += ((temp % 10) + 1) * 2;
John Reck7075c792017-07-05 14:03:43 -0700158 expectedBucket = ProfileData::frameTimeForSlowFrameCountIndex(temp);
John Reckdf1742e2017-01-19 15:56:21 -0800159 }
160 EXPECT_EQ(expectedCount, loadedProto.histogram().Get(i).frame_count());
161 EXPECT_EQ(expectedBucket, loadedProto.histogram().Get(i).render_millis());
162 }
Colin Cross99c9bf62017-05-04 09:58:59 -0700163}