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