| 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 <android-base/macros.h> |
| 18 | #include <gmock/gmock.h> |
| 19 | #include <gtest/gtest.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include "protos/graphicsstats.pb.h" |
| 27 | #include "service/GraphicsStatsService.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; |
| 38 | fprintf(stderr, "Failed to read from /proc/self/exe; r=%zd, err=%d (%s)\n", r, err, |
| 39 | strerror(err)); |
| 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) { |
| 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())); |
| 61 | } |
| 62 | |
| 63 | TEST(GraphicsStats, saveLoad) { |
| 64 | std::string path = findRootPath() + "/test_saveLoad"; |
| 65 | std::string packageName = "com.test.saveLoad"; |
| 66 | MockProfileData mockData; |
| 67 | mockData.editJankFrameCount() = 20; |
| 68 | mockData.editTotalFrameCount() = 100; |
| 69 | mockData.editStatStartTime() = 10000; |
| 70 | // Fill with patterned data we can recognize but which won't map to a |
| 71 | // memset or basic for iteration count |
| 72 | for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) { |
| 73 | mockData.editFrameCounts()[i] = ((i % 10) + 1) * 2; |
| 74 | } |
| 75 | for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) { |
| 76 | mockData.editSlowFrameCounts()[i] = (i % 5) + 1; |
| 77 | } |
| 78 | GraphicsStatsService::saveBuffer(path, packageName, 5, 3000, 7000, &mockData); |
| 79 | protos::GraphicsStatsProto loadedProto; |
| 80 | EXPECT_TRUE(GraphicsStatsService::parseFromFile(path, &loadedProto)); |
| 81 | // Clean up the file |
| 82 | unlink(path.c_str()); |
| 83 | |
| 84 | EXPECT_EQ(packageName, loadedProto.package_name()); |
| 85 | EXPECT_EQ(5, loadedProto.version_code()); |
| 86 | EXPECT_EQ(3000, loadedProto.stats_start()); |
| 87 | EXPECT_EQ(7000, loadedProto.stats_end()); |
| 88 | // ASSERT here so we don't continue with a nullptr deref crash if this is false |
| 89 | ASSERT_TRUE(loadedProto.has_summary()); |
| 90 | EXPECT_EQ(20, loadedProto.summary().janky_frames()); |
| 91 | EXPECT_EQ(100, loadedProto.summary().total_frames()); |
| 92 | EXPECT_EQ(mockData.editFrameCounts().size() + mockData.editSlowFrameCounts().size(), |
| 93 | (size_t)loadedProto.histogram_size()); |
| 94 | for (size_t i = 0; i < (size_t)loadedProto.histogram_size(); i++) { |
| 95 | int expectedCount, expectedBucket; |
| 96 | if (i < mockData.editFrameCounts().size()) { |
| 97 | expectedCount = ((i % 10) + 1) * 2; |
| 98 | expectedBucket = ProfileData::frameTimeForFrameCountIndex(i); |
| 99 | } else { |
| 100 | int temp = i - mockData.editFrameCounts().size(); |
| 101 | expectedCount = (temp % 5) + 1; |
| 102 | expectedBucket = ProfileData::frameTimeForSlowFrameCountIndex(temp); |
| 103 | } |
| 104 | EXPECT_EQ(expectedCount, loadedProto.histogram().Get(i).frame_count()); |
| 105 | EXPECT_EQ(expectedBucket, loadedProto.histogram().Get(i).render_millis()); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | TEST(GraphicsStats, merge) { |
| 110 | std::string path = findRootPath() + "/test_merge"; |
| 111 | std::string packageName = "com.test.merge"; |
| 112 | MockProfileData mockData; |
| 113 | mockData.editJankFrameCount() = 20; |
| 114 | mockData.editTotalFrameCount() = 100; |
| 115 | mockData.editStatStartTime() = 10000; |
| 116 | // Fill with patterned data we can recognize but which won't map to a |
| 117 | // memset or basic for iteration count |
| 118 | for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) { |
| 119 | mockData.editFrameCounts()[i] = ((i % 10) + 1) * 2; |
| 120 | } |
| 121 | for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) { |
| 122 | mockData.editSlowFrameCounts()[i] = (i % 5) + 1; |
| 123 | } |
| 124 | GraphicsStatsService::saveBuffer(path, packageName, 5, 3000, 7000, &mockData); |
| 125 | mockData.editJankFrameCount() = 50; |
| 126 | mockData.editTotalFrameCount() = 500; |
| 127 | for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) { |
| 128 | mockData.editFrameCounts()[i] = (i % 5) + 1; |
| 129 | } |
| 130 | for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) { |
| 131 | mockData.editSlowFrameCounts()[i] = ((i % 10) + 1) * 2; |
| 132 | } |
| 133 | GraphicsStatsService::saveBuffer(path, packageName, 5, 7050, 10000, &mockData); |
| 134 | |
| 135 | protos::GraphicsStatsProto loadedProto; |
| 136 | EXPECT_TRUE(GraphicsStatsService::parseFromFile(path, &loadedProto)); |
| 137 | // Clean up the file |
| 138 | unlink(path.c_str()); |
| 139 | |
| 140 | EXPECT_EQ(packageName, loadedProto.package_name()); |
| 141 | EXPECT_EQ(5, loadedProto.version_code()); |
| 142 | EXPECT_EQ(3000, loadedProto.stats_start()); |
| 143 | EXPECT_EQ(10000, loadedProto.stats_end()); |
| 144 | // ASSERT here so we don't continue with a nullptr deref crash if this is false |
| 145 | ASSERT_TRUE(loadedProto.has_summary()); |
| 146 | EXPECT_EQ(20 + 50, loadedProto.summary().janky_frames()); |
| 147 | EXPECT_EQ(100 + 500, loadedProto.summary().total_frames()); |
| 148 | EXPECT_EQ(mockData.editFrameCounts().size() + mockData.editSlowFrameCounts().size(), |
| 149 | (size_t)loadedProto.histogram_size()); |
| 150 | for (size_t i = 0; i < (size_t)loadedProto.histogram_size(); i++) { |
| 151 | int expectedCount, expectedBucket; |
| 152 | if (i < mockData.editFrameCounts().size()) { |
| 153 | expectedCount = ((i % 10) + 1) * 2; |
| 154 | expectedCount += (i % 5) + 1; |
| 155 | expectedBucket = ProfileData::frameTimeForFrameCountIndex(i); |
| 156 | } else { |
| 157 | int temp = i - mockData.editFrameCounts().size(); |
| 158 | expectedCount = (temp % 5) + 1; |
| 159 | expectedCount += ((temp % 10) + 1) * 2; |
| 160 | expectedBucket = ProfileData::frameTimeForSlowFrameCountIndex(temp); |
| 161 | } |
| 162 | EXPECT_EQ(expectedCount, loadedProto.histogram().Get(i).frame_count()); |
| 163 | EXPECT_EQ(expectedBucket, loadedProto.histogram().Get(i).render_millis()); |
| 164 | } |
| 165 | } |