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> |
Nolan Scobie | 1b576de | 2023-01-09 18:21:40 -0500 | [diff] [blame] | 18 | #include <gmock/gmock.h> |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 19 | #include <gtest/gtest.h> |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | |
Nolan Scobie | 24921a0 | 2022-11-14 19:35:58 -0500 | [diff] [blame] | 26 | #include "protos/graphicsstats.pb.h" |
| 27 | #include "service/GraphicsStatsService.h" |
| 28 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 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) { |
Nolan Scobie | 1b576de | 2023-01-09 18:21:40 -0500 | [diff] [blame] | 53 | // 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 Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | TEST(GraphicsStats, saveLoad) { |
| 64 | std::string path = findRootPath() + "/test_saveLoad"; |
Alec Mouri | 04b0d81 | 2024-09-20 16:15:55 +0000 | [diff] [blame] | 65 | uid_t uid = 123; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 66 | std::string packageName = "com.test.saveLoad"; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 67 | MockProfileData mockData; |
| 68 | mockData.editJankFrameCount() = 20; |
| 69 | mockData.editTotalFrameCount() = 100; |
| 70 | mockData.editStatStartTime() = 10000; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 71 | // Fill with patterned data we can recognize but which won't map to a |
| 72 | // memset or basic for iteration count |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 73 | for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) { |
| 74 | mockData.editFrameCounts()[i] = ((i % 10) + 1) * 2; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 75 | } |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 76 | for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) { |
| 77 | mockData.editSlowFrameCounts()[i] = (i % 5) + 1; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 78 | } |
Alec Mouri | 04b0d81 | 2024-09-20 16:15:55 +0000 | [diff] [blame] | 79 | GraphicsStatsService::saveBuffer(path, uid, packageName, 5, 3000, 7000, &mockData); |
Kweku Adams | 1856a4c | 2018-04-03 16:31:10 -0700 | [diff] [blame] | 80 | protos::GraphicsStatsProto loadedProto; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 81 | EXPECT_TRUE(GraphicsStatsService::parseFromFile(path, &loadedProto)); |
| 82 | // Clean up the file |
| 83 | unlink(path.c_str()); |
| 84 | |
Alec Mouri | 04b0d81 | 2024-09-20 16:15:55 +0000 | [diff] [blame] | 85 | EXPECT_EQ(uid, loadedProto.uid()); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 86 | 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 Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 94 | EXPECT_EQ(mockData.editFrameCounts().size() + mockData.editSlowFrameCounts().size(), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 95 | (size_t)loadedProto.histogram_size()); |
| 96 | for (size_t i = 0; i < (size_t)loadedProto.histogram_size(); i++) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 97 | int expectedCount, expectedBucket; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 98 | if (i < mockData.editFrameCounts().size()) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 99 | expectedCount = ((i % 10) + 1) * 2; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 100 | expectedBucket = ProfileData::frameTimeForFrameCountIndex(i); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 101 | } else { |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 102 | int temp = i - mockData.editFrameCounts().size(); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 103 | expectedCount = (temp % 5) + 1; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 104 | expectedBucket = ProfileData::frameTimeForSlowFrameCountIndex(temp); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 105 | } |
| 106 | EXPECT_EQ(expectedCount, loadedProto.histogram().Get(i).frame_count()); |
| 107 | EXPECT_EQ(expectedBucket, loadedProto.histogram().Get(i).render_millis()); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | TEST(GraphicsStats, merge) { |
| 112 | std::string path = findRootPath() + "/test_merge"; |
| 113 | std::string packageName = "com.test.merge"; |
Alec Mouri | 04b0d81 | 2024-09-20 16:15:55 +0000 | [diff] [blame] | 114 | uid_t uid = 123; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 115 | MockProfileData mockData; |
| 116 | mockData.editJankFrameCount() = 20; |
| 117 | mockData.editTotalFrameCount() = 100; |
| 118 | mockData.editStatStartTime() = 10000; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 119 | // Fill with patterned data we can recognize but which won't map to a |
| 120 | // memset or basic for iteration count |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 121 | for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) { |
| 122 | mockData.editFrameCounts()[i] = ((i % 10) + 1) * 2; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 123 | } |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 124 | for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) { |
| 125 | mockData.editSlowFrameCounts()[i] = (i % 5) + 1; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 126 | } |
Alec Mouri | 04b0d81 | 2024-09-20 16:15:55 +0000 | [diff] [blame] | 127 | GraphicsStatsService::saveBuffer(path, uid, packageName, 5, 3000, 7000, &mockData); |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 128 | 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 Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 132 | } |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 133 | for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) { |
| 134 | mockData.editSlowFrameCounts()[i] = ((i % 10) + 1) * 2; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 135 | } |
Alec Mouri | 04b0d81 | 2024-09-20 16:15:55 +0000 | [diff] [blame] | 136 | |
| 137 | GraphicsStatsService::saveBuffer(path, uid, packageName, 5, 7050, 10000, &mockData); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 138 | |
Kweku Adams | 1856a4c | 2018-04-03 16:31:10 -0700 | [diff] [blame] | 139 | protos::GraphicsStatsProto loadedProto; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 140 | EXPECT_TRUE(GraphicsStatsService::parseFromFile(path, &loadedProto)); |
| 141 | // Clean up the file |
| 142 | unlink(path.c_str()); |
| 143 | |
Alec Mouri | 04b0d81 | 2024-09-20 16:15:55 +0000 | [diff] [blame] | 144 | EXPECT_EQ(uid, loadedProto.uid()); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 145 | 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 Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 153 | EXPECT_EQ(mockData.editFrameCounts().size() + mockData.editSlowFrameCounts().size(), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 154 | (size_t)loadedProto.histogram_size()); |
| 155 | for (size_t i = 0; i < (size_t)loadedProto.histogram_size(); i++) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 156 | int expectedCount, expectedBucket; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 157 | if (i < mockData.editFrameCounts().size()) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 158 | expectedCount = ((i % 10) + 1) * 2; |
| 159 | expectedCount += (i % 5) + 1; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 160 | expectedBucket = ProfileData::frameTimeForFrameCountIndex(i); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 161 | } else { |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 162 | int temp = i - mockData.editFrameCounts().size(); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 163 | expectedCount = (temp % 5) + 1; |
| 164 | expectedCount += ((temp % 10) + 1) * 2; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 165 | expectedBucket = ProfileData::frameTimeForSlowFrameCountIndex(temp); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 166 | } |
| 167 | EXPECT_EQ(expectedCount, loadedProto.histogram().Get(i).frame_count()); |
| 168 | EXPECT_EQ(expectedBucket, loadedProto.histogram().Get(i).render_millis()); |
| 169 | } |
Colin Cross | 99c9bf6 | 2017-05-04 09:58:59 -0700 | [diff] [blame] | 170 | } |