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 | |
Kweku Adams | 1856a4c | 2018-04-03 16:31:10 -0700 | [diff] [blame] | 19 | #include "protos/graphicsstats.pb.h" |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 20 | #include "service/GraphicsStatsService.h" |
| 21 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | |
| 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) { |
Colin Cross | 99c9bf6 | 2017-05-04 09:58:59 -0700 | [diff] [blame] | 52 | #ifdef __LP64__ |
| 53 | std::string expected = "/data/nativetest64/hwui_unit_tests"; |
| 54 | #else |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 55 | std::string expected = "/data/nativetest/hwui_unit_tests"; |
Colin Cross | 99c9bf6 | 2017-05-04 09:58:59 -0700 | [diff] [blame] | 56 | #endif |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 57 | EXPECT_EQ(expected, findRootPath()); |
| 58 | } |
| 59 | |
| 60 | TEST(GraphicsStats, saveLoad) { |
| 61 | std::string path = findRootPath() + "/test_saveLoad"; |
| 62 | std::string packageName = "com.test.saveLoad"; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 63 | MockProfileData mockData; |
| 64 | mockData.editJankFrameCount() = 20; |
| 65 | mockData.editTotalFrameCount() = 100; |
| 66 | mockData.editStatStartTime() = 10000; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 67 | // Fill with patterned data we can recognize but which won't map to a |
| 68 | // memset or basic for iteration count |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 69 | for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) { |
| 70 | mockData.editFrameCounts()[i] = ((i % 10) + 1) * 2; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 71 | } |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 72 | for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) { |
| 73 | mockData.editSlowFrameCounts()[i] = (i % 5) + 1; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 74 | } |
| 75 | GraphicsStatsService::saveBuffer(path, packageName, 5, 3000, 7000, &mockData); |
Kweku Adams | 1856a4c | 2018-04-03 16:31:10 -0700 | [diff] [blame] | 76 | protos::GraphicsStatsProto loadedProto; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 77 | EXPECT_TRUE(GraphicsStatsService::parseFromFile(path, &loadedProto)); |
| 78 | // Clean up the file |
| 79 | unlink(path.c_str()); |
| 80 | |
| 81 | EXPECT_EQ(packageName, loadedProto.package_name()); |
| 82 | EXPECT_EQ(5, loadedProto.version_code()); |
| 83 | EXPECT_EQ(3000, loadedProto.stats_start()); |
| 84 | EXPECT_EQ(7000, loadedProto.stats_end()); |
| 85 | // ASSERT here so we don't continue with a nullptr deref crash if this is false |
| 86 | ASSERT_TRUE(loadedProto.has_summary()); |
| 87 | EXPECT_EQ(20, loadedProto.summary().janky_frames()); |
| 88 | EXPECT_EQ(100, loadedProto.summary().total_frames()); |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 89 | EXPECT_EQ(mockData.editFrameCounts().size() + mockData.editSlowFrameCounts().size(), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 90 | (size_t)loadedProto.histogram_size()); |
| 91 | for (size_t i = 0; i < (size_t)loadedProto.histogram_size(); i++) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 92 | int expectedCount, expectedBucket; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 93 | if (i < mockData.editFrameCounts().size()) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 94 | expectedCount = ((i % 10) + 1) * 2; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 95 | expectedBucket = ProfileData::frameTimeForFrameCountIndex(i); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 96 | } else { |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 97 | int temp = i - mockData.editFrameCounts().size(); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 98 | expectedCount = (temp % 5) + 1; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 99 | expectedBucket = ProfileData::frameTimeForSlowFrameCountIndex(temp); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 100 | } |
| 101 | EXPECT_EQ(expectedCount, loadedProto.histogram().Get(i).frame_count()); |
| 102 | EXPECT_EQ(expectedBucket, loadedProto.histogram().Get(i).render_millis()); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | TEST(GraphicsStats, merge) { |
| 107 | std::string path = findRootPath() + "/test_merge"; |
| 108 | std::string packageName = "com.test.merge"; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 109 | MockProfileData mockData; |
| 110 | mockData.editJankFrameCount() = 20; |
| 111 | mockData.editTotalFrameCount() = 100; |
| 112 | mockData.editStatStartTime() = 10000; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 113 | // Fill with patterned data we can recognize but which won't map to a |
| 114 | // memset or basic for iteration count |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 115 | for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) { |
| 116 | mockData.editFrameCounts()[i] = ((i % 10) + 1) * 2; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 117 | } |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 118 | for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) { |
| 119 | mockData.editSlowFrameCounts()[i] = (i % 5) + 1; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 120 | } |
| 121 | GraphicsStatsService::saveBuffer(path, packageName, 5, 3000, 7000, &mockData); |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 122 | mockData.editJankFrameCount() = 50; |
| 123 | mockData.editTotalFrameCount() = 500; |
| 124 | for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) { |
| 125 | mockData.editFrameCounts()[i] = (i % 5) + 1; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 126 | } |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 127 | for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) { |
| 128 | mockData.editSlowFrameCounts()[i] = ((i % 10) + 1) * 2; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 129 | } |
| 130 | GraphicsStatsService::saveBuffer(path, packageName, 5, 7050, 10000, &mockData); |
| 131 | |
Kweku Adams | 1856a4c | 2018-04-03 16:31:10 -0700 | [diff] [blame] | 132 | protos::GraphicsStatsProto loadedProto; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 133 | EXPECT_TRUE(GraphicsStatsService::parseFromFile(path, &loadedProto)); |
| 134 | // Clean up the file |
| 135 | unlink(path.c_str()); |
| 136 | |
| 137 | EXPECT_EQ(packageName, loadedProto.package_name()); |
| 138 | EXPECT_EQ(5, loadedProto.version_code()); |
| 139 | EXPECT_EQ(3000, loadedProto.stats_start()); |
| 140 | EXPECT_EQ(10000, loadedProto.stats_end()); |
| 141 | // ASSERT here so we don't continue with a nullptr deref crash if this is false |
| 142 | ASSERT_TRUE(loadedProto.has_summary()); |
| 143 | EXPECT_EQ(20 + 50, loadedProto.summary().janky_frames()); |
| 144 | EXPECT_EQ(100 + 500, loadedProto.summary().total_frames()); |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 145 | EXPECT_EQ(mockData.editFrameCounts().size() + mockData.editSlowFrameCounts().size(), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 146 | (size_t)loadedProto.histogram_size()); |
| 147 | for (size_t i = 0; i < (size_t)loadedProto.histogram_size(); i++) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 148 | int expectedCount, expectedBucket; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 149 | if (i < mockData.editFrameCounts().size()) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 150 | expectedCount = ((i % 10) + 1) * 2; |
| 151 | expectedCount += (i % 5) + 1; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 152 | expectedBucket = ProfileData::frameTimeForFrameCountIndex(i); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 153 | } else { |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 154 | int temp = i - mockData.editFrameCounts().size(); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 155 | expectedCount = (temp % 5) + 1; |
| 156 | expectedCount += ((temp % 10) + 1) * 2; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 157 | expectedBucket = ProfileData::frameTimeForSlowFrameCountIndex(temp); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 158 | } |
| 159 | EXPECT_EQ(expectedCount, loadedProto.histogram().Get(i).frame_count()); |
| 160 | EXPECT_EQ(expectedBucket, loadedProto.histogram().Get(i).render_millis()); |
| 161 | } |
Colin Cross | 99c9bf6 | 2017-05-04 09:58:59 -0700 | [diff] [blame] | 162 | } |