John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 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 <gtest/gtest.h> |
| 18 | |
| 19 | #include "service/GraphicsStatsService.h" |
| 20 | |
| 21 | #include <frameworks/base/core/proto/android/service/graphicsstats.pb.h> |
| 22 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | |
| 29 | using namespace android; |
| 30 | using namespace android::uirenderer; |
| 31 | |
| 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; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 38 | fprintf(stderr, "Failed to read from /proc/self/exe; r=%zd, err=%d (%s)\n", r, err, |
| 39 | strerror(err)); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 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 |
| 52 | TEST(GraphicsStats, findRootPath) { |
Colin Cross | 99c9bf6 | 2017-05-04 09:58:59 -0700 | [diff] [blame] | 53 | #ifdef __LP64__ |
| 54 | std::string expected = "/data/nativetest64/hwui_unit_tests"; |
| 55 | #else |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 56 | std::string expected = "/data/nativetest/hwui_unit_tests"; |
Colin Cross | 99c9bf6 | 2017-05-04 09:58:59 -0700 | [diff] [blame] | 57 | #endif |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 58 | EXPECT_EQ(expected, findRootPath()); |
| 59 | } |
| 60 | |
| 61 | TEST(GraphicsStats, saveLoad) { |
| 62 | std::string path = findRootPath() + "/test_saveLoad"; |
| 63 | std::string packageName = "com.test.saveLoad"; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 64 | MockProfileData mockData; |
| 65 | mockData.editJankFrameCount() = 20; |
| 66 | mockData.editTotalFrameCount() = 100; |
| 67 | mockData.editStatStartTime() = 10000; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 68 | // Fill with patterned data we can recognize but which won't map to a |
| 69 | // memset or basic for iteration count |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 70 | for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) { |
| 71 | mockData.editFrameCounts()[i] = ((i % 10) + 1) * 2; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 72 | } |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 73 | for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) { |
| 74 | mockData.editSlowFrameCounts()[i] = (i % 5) + 1; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 75 | } |
| 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 Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 90 | EXPECT_EQ(mockData.editFrameCounts().size() + mockData.editSlowFrameCounts().size(), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 91 | (size_t)loadedProto.histogram_size()); |
| 92 | for (size_t i = 0; i < (size_t)loadedProto.histogram_size(); i++) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 93 | int expectedCount, expectedBucket; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 94 | if (i < mockData.editFrameCounts().size()) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 95 | expectedCount = ((i % 10) + 1) * 2; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 96 | expectedBucket = ProfileData::frameTimeForFrameCountIndex(i); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 97 | } else { |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 98 | int temp = i - mockData.editFrameCounts().size(); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 99 | expectedCount = (temp % 5) + 1; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 100 | expectedBucket = ProfileData::frameTimeForSlowFrameCountIndex(temp); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 101 | } |
| 102 | EXPECT_EQ(expectedCount, loadedProto.histogram().Get(i).frame_count()); |
| 103 | EXPECT_EQ(expectedBucket, loadedProto.histogram().Get(i).render_millis()); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | TEST(GraphicsStats, merge) { |
| 108 | std::string path = findRootPath() + "/test_merge"; |
| 109 | std::string packageName = "com.test.merge"; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 110 | MockProfileData mockData; |
| 111 | mockData.editJankFrameCount() = 20; |
| 112 | mockData.editTotalFrameCount() = 100; |
| 113 | mockData.editStatStartTime() = 10000; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 114 | // Fill with patterned data we can recognize but which won't map to a |
| 115 | // memset or basic for iteration count |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 116 | for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) { |
| 117 | mockData.editFrameCounts()[i] = ((i % 10) + 1) * 2; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 118 | } |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 119 | for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) { |
| 120 | mockData.editSlowFrameCounts()[i] = (i % 5) + 1; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 121 | } |
| 122 | GraphicsStatsService::saveBuffer(path, packageName, 5, 3000, 7000, &mockData); |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 123 | 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 Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 127 | } |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 128 | for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) { |
| 129 | mockData.editSlowFrameCounts()[i] = ((i % 10) + 1) * 2; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 130 | } |
| 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 Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 146 | EXPECT_EQ(mockData.editFrameCounts().size() + mockData.editSlowFrameCounts().size(), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 147 | (size_t)loadedProto.histogram_size()); |
| 148 | for (size_t i = 0; i < (size_t)loadedProto.histogram_size(); i++) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 149 | int expectedCount, expectedBucket; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 150 | if (i < mockData.editFrameCounts().size()) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 151 | expectedCount = ((i % 10) + 1) * 2; |
| 152 | expectedCount += (i % 5) + 1; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 153 | expectedBucket = ProfileData::frameTimeForFrameCountIndex(i); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 154 | } else { |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 155 | int temp = i - mockData.editFrameCounts().size(); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 156 | expectedCount = (temp % 5) + 1; |
| 157 | expectedCount += ((temp % 10) + 1) * 2; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 158 | expectedBucket = ProfileData::frameTimeForSlowFrameCountIndex(temp); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 159 | } |
| 160 | EXPECT_EQ(expectedCount, loadedProto.histogram().Get(i).frame_count()); |
| 161 | EXPECT_EQ(expectedBucket, loadedProto.histogram().Get(i).render_millis()); |
| 162 | } |
Colin Cross | 99c9bf6 | 2017-05-04 09:58:59 -0700 | [diff] [blame] | 163 | } |