John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 "GraphicsStatsService.h" |
| 18 | |
Dan Albert | 110e007 | 2017-10-11 12:41:26 -0700 | [diff] [blame] | 19 | #include <errno.h> |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 20 | #include <fcntl.h> |
Stan Iliev | 637ba5e | 2019-08-16 13:43:08 -0400 | [diff] [blame] | 21 | #include <google/protobuf/io/zero_copy_stream_impl_lite.h> |
Dan Albert | 110e007 | 2017-10-11 12:41:26 -0700 | [diff] [blame] | 22 | #include <inttypes.h> |
Stan Iliev | 637ba5e | 2019-08-16 13:43:08 -0400 | [diff] [blame] | 23 | #include <log/log.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 24 | #include <sys/mman.h> |
Dan Albert | 110e007 | 2017-10-11 12:41:26 -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 | |
Stan Iliev | 637ba5e | 2019-08-16 13:43:08 -0400 | [diff] [blame] | 29 | #include <algorithm> |
| 30 | #include <map> |
| 31 | #include <vector> |
| 32 | |
| 33 | #include "JankTracker.h" |
| 34 | #include "protos/graphicsstats.pb.h" |
| 35 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 36 | namespace android { |
| 37 | namespace uirenderer { |
| 38 | |
| 39 | using namespace google::protobuf; |
Stan Iliev | 637ba5e | 2019-08-16 13:43:08 -0400 | [diff] [blame] | 40 | using namespace uirenderer::protos; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 41 | |
| 42 | constexpr int32_t sCurrentFileVersion = 1; |
| 43 | constexpr int32_t sHeaderSize = 4; |
| 44 | static_assert(sizeof(sCurrentFileVersion) == sHeaderSize, "Header size is wrong"); |
| 45 | |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 46 | constexpr int sHistogramSize = ProfileData::HistogramSize(); |
Stan Iliev | 7203e1f | 2019-07-25 13:12:02 -0400 | [diff] [blame] | 47 | constexpr int sGPUHistogramSize = ProfileData::GPUHistogramSize(); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 48 | |
Stan Iliev | 637ba5e | 2019-08-16 13:43:08 -0400 | [diff] [blame] | 49 | static bool mergeProfileDataIntoProto(protos::GraphicsStatsProto* proto, const std::string& package, |
| 50 | int64_t versionCode, int64_t startTime, int64_t endTime, |
| 51 | const ProfileData* data); |
Kweku Adams | 1856a4c | 2018-04-03 16:31:10 -0700 | [diff] [blame] | 52 | static void dumpAsTextToFd(protos::GraphicsStatsProto* proto, int outFd); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 53 | |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 54 | class FileDescriptor { |
| 55 | public: |
Chih-Hung Hsieh | f21b0b6 | 2018-12-20 13:48:02 -0800 | [diff] [blame] | 56 | explicit FileDescriptor(int fd) : mFd(fd) {} |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 57 | ~FileDescriptor() { |
| 58 | if (mFd != -1) { |
| 59 | close(mFd); |
| 60 | mFd = -1; |
| 61 | } |
| 62 | } |
| 63 | bool valid() { return mFd != -1; } |
Stan Iliev | 637ba5e | 2019-08-16 13:43:08 -0400 | [diff] [blame] | 64 | operator int() { return mFd; } // NOLINT(google-explicit-constructor) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 65 | |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 66 | private: |
| 67 | int mFd; |
| 68 | }; |
| 69 | |
| 70 | class FileOutputStreamLite : public io::ZeroCopyOutputStream { |
| 71 | public: |
Chih-Hung Hsieh | f21b0b6 | 2018-12-20 13:48:02 -0800 | [diff] [blame] | 72 | explicit FileOutputStreamLite(int fd) : mCopyAdapter(fd), mImpl(&mCopyAdapter) {} |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 73 | virtual ~FileOutputStreamLite() {} |
| 74 | |
| 75 | int GetErrno() { return mCopyAdapter.mErrno; } |
| 76 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 77 | virtual bool Next(void** data, int* size) override { return mImpl.Next(data, size); } |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 78 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 79 | virtual void BackUp(int count) override { mImpl.BackUp(count); } |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 80 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 81 | virtual int64 ByteCount() const override { return mImpl.ByteCount(); } |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 82 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 83 | bool Flush() { return mImpl.Flush(); } |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 84 | |
| 85 | private: |
| 86 | struct FDAdapter : public io::CopyingOutputStream { |
| 87 | int mFd; |
| 88 | int mErrno = 0; |
| 89 | |
Chih-Hung Hsieh | f21b0b6 | 2018-12-20 13:48:02 -0800 | [diff] [blame] | 90 | explicit FDAdapter(int fd) : mFd(fd) {} |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 91 | virtual ~FDAdapter() {} |
| 92 | |
| 93 | virtual bool Write(const void* buffer, int size) override { |
| 94 | int ret; |
| 95 | while (size) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 96 | ret = TEMP_FAILURE_RETRY(write(mFd, buffer, size)); |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 97 | if (ret <= 0) { |
| 98 | mErrno = errno; |
| 99 | return false; |
| 100 | } |
| 101 | size -= ret; |
| 102 | } |
| 103 | return true; |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | FileOutputStreamLite::FDAdapter mCopyAdapter; |
| 108 | io::CopyingOutputStreamAdaptor mImpl; |
| 109 | }; |
| 110 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 111 | bool GraphicsStatsService::parseFromFile(const std::string& path, |
Kweku Adams | 1856a4c | 2018-04-03 16:31:10 -0700 | [diff] [blame] | 112 | protos::GraphicsStatsProto* output) { |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 113 | FileDescriptor fd{open(path.c_str(), O_RDONLY)}; |
| 114 | if (!fd.valid()) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 115 | int err = errno; |
| 116 | // The file not existing is normal for addToDump(), so only log if |
| 117 | // we get an unexpected error |
| 118 | if (err != ENOENT) { |
| 119 | ALOGW("Failed to open '%s', errno=%d (%s)", path.c_str(), err, strerror(err)); |
| 120 | } |
| 121 | return false; |
| 122 | } |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 123 | struct stat sb; |
| 124 | if (fstat(fd, &sb) || sb.st_size < sHeaderSize) { |
| 125 | int err = errno; |
| 126 | // The file not existing is normal for addToDump(), so only log if |
| 127 | // we get an unexpected error |
| 128 | if (err != ENOENT) { |
| 129 | ALOGW("Failed to fstat '%s', errno=%d (%s) (st_size %d)", path.c_str(), err, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 130 | strerror(err), (int)sb.st_size); |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 131 | } |
| 132 | return false; |
| 133 | } |
| 134 | void* addr = mmap(nullptr, sb.st_size, PROT_READ, MAP_SHARED, fd, 0); |
zhangkuili | 24a1bc3 | 2018-05-29 10:23:29 +0800 | [diff] [blame] | 135 | if (addr == MAP_FAILED) { |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 136 | int err = errno; |
| 137 | // The file not existing is normal for addToDump(), so only log if |
| 138 | // we get an unexpected error |
| 139 | if (err != ENOENT) { |
| 140 | ALOGW("Failed to mmap '%s', errno=%d (%s)", path.c_str(), err, strerror(err)); |
| 141 | } |
| 142 | return false; |
| 143 | } |
| 144 | uint32_t file_version = *reinterpret_cast<uint32_t*>(addr); |
| 145 | if (file_version != sCurrentFileVersion) { |
| 146 | ALOGW("file_version mismatch! expected %d got %d", sCurrentFileVersion, file_version); |
liulvping | 4832438c | 2018-12-20 20:34:56 +0800 | [diff] [blame] | 147 | munmap(addr, sb.st_size); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 148 | return false; |
| 149 | } |
| 150 | |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 151 | void* data = reinterpret_cast<uint8_t*>(addr) + sHeaderSize; |
| 152 | int dataSize = sb.st_size - sHeaderSize; |
| 153 | io::ArrayInputStream input{data, dataSize}; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 154 | bool success = output->ParseFromZeroCopyStream(&input); |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 155 | if (!success) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 156 | ALOGW("Parse failed on '%s' error='%s'", path.c_str(), |
| 157 | output->InitializationErrorString().c_str()); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 158 | } |
liulvping | 4832438c | 2018-12-20 20:34:56 +0800 | [diff] [blame] | 159 | munmap(addr, sb.st_size); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 160 | return success; |
| 161 | } |
| 162 | |
Kweku Adams | 1856a4c | 2018-04-03 16:31:10 -0700 | [diff] [blame] | 163 | bool mergeProfileDataIntoProto(protos::GraphicsStatsProto* proto, const std::string& package, |
Dianne Hackborn | 73453e4 | 2017-12-11 16:30:36 -0800 | [diff] [blame] | 164 | int64_t versionCode, int64_t startTime, int64_t endTime, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 165 | const ProfileData* data) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 166 | if (proto->stats_start() == 0 || proto->stats_start() > startTime) { |
| 167 | proto->set_stats_start(startTime); |
| 168 | } |
| 169 | if (proto->stats_end() == 0 || proto->stats_end() < endTime) { |
| 170 | proto->set_stats_end(endTime); |
| 171 | } |
| 172 | proto->set_package_name(package); |
| 173 | proto->set_version_code(versionCode); |
Stan Iliev | 637ba5e | 2019-08-16 13:43:08 -0400 | [diff] [blame] | 174 | proto->set_pipeline(data->pipelineType() == RenderPipelineType::SkiaGL ? |
| 175 | GraphicsStatsProto_PipelineType_GL : GraphicsStatsProto_PipelineType_VULKAN); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 176 | auto summary = proto->mutable_summary(); |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 177 | summary->set_total_frames(summary->total_frames() + data->totalFrameCount()); |
| 178 | summary->set_janky_frames(summary->janky_frames() + data->jankFrameCount()); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 179 | summary->set_missed_vsync_count(summary->missed_vsync_count() + |
| 180 | data->jankTypeCount(kMissedVsync)); |
| 181 | summary->set_high_input_latency_count(summary->high_input_latency_count() + |
| 182 | data->jankTypeCount(kHighInputLatency)); |
| 183 | summary->set_slow_ui_thread_count(summary->slow_ui_thread_count() + |
| 184 | data->jankTypeCount(kSlowUI)); |
| 185 | summary->set_slow_bitmap_upload_count(summary->slow_bitmap_upload_count() + |
| 186 | data->jankTypeCount(kSlowSync)); |
| 187 | summary->set_slow_draw_count(summary->slow_draw_count() + data->jankTypeCount(kSlowRT)); |
Stan Iliev | 637ba5e | 2019-08-16 13:43:08 -0400 | [diff] [blame] | 188 | summary->set_missed_deadline_count(summary->missed_deadline_count() + |
| 189 | data->jankTypeCount(kMissedDeadline)); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 190 | |
| 191 | bool creatingHistogram = false; |
| 192 | if (proto->histogram_size() == 0) { |
| 193 | proto->mutable_histogram()->Reserve(sHistogramSize); |
| 194 | creatingHistogram = true; |
| 195 | } else if (proto->histogram_size() != sHistogramSize) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 196 | ALOGE("Histogram size mismatch, proto is %d expected %d", proto->histogram_size(), |
| 197 | sHistogramSize); |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 198 | return false; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 199 | } |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 200 | int index = 0; |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 201 | bool hitMergeError = false; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 202 | data->histogramForEach([&](ProfileData::HistogramEntry entry) { |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 203 | if (hitMergeError) return; |
| 204 | |
Kweku Adams | 1856a4c | 2018-04-03 16:31:10 -0700 | [diff] [blame] | 205 | protos::GraphicsStatsHistogramBucketProto* bucket; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 206 | if (creatingHistogram) { |
| 207 | bucket = proto->add_histogram(); |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 208 | bucket->set_render_millis(entry.renderTimeMs); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 209 | } else { |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 210 | bucket = proto->mutable_histogram(index); |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 211 | if (bucket->render_millis() != static_cast<int32_t>(entry.renderTimeMs)) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 212 | ALOGW("Frame time mistmatch %d vs. %u", bucket->render_millis(), |
| 213 | entry.renderTimeMs); |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 214 | hitMergeError = true; |
| 215 | return; |
| 216 | } |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 217 | } |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 218 | bucket->set_frame_count(bucket->frame_count() + entry.frameCount); |
| 219 | index++; |
| 220 | }); |
Stan Iliev | 7203e1f | 2019-07-25 13:12:02 -0400 | [diff] [blame] | 221 | if (hitMergeError) return false; |
| 222 | // fill in GPU frame time histogram |
| 223 | creatingHistogram = false; |
| 224 | if (proto->gpu_histogram_size() == 0) { |
| 225 | proto->mutable_gpu_histogram()->Reserve(sGPUHistogramSize); |
| 226 | creatingHistogram = true; |
| 227 | } else if (proto->gpu_histogram_size() != sGPUHistogramSize) { |
| 228 | ALOGE("GPU histogram size mismatch, proto is %d expected %d", proto->gpu_histogram_size(), |
| 229 | sGPUHistogramSize); |
| 230 | return false; |
| 231 | } |
| 232 | index = 0; |
| 233 | data->histogramGPUForEach([&](ProfileData::HistogramEntry entry) { |
| 234 | if (hitMergeError) return; |
| 235 | |
| 236 | protos::GraphicsStatsHistogramBucketProto* bucket; |
| 237 | if (creatingHistogram) { |
| 238 | bucket = proto->add_gpu_histogram(); |
| 239 | bucket->set_render_millis(entry.renderTimeMs); |
| 240 | } else { |
| 241 | bucket = proto->mutable_gpu_histogram(index); |
| 242 | if (bucket->render_millis() != static_cast<int32_t>(entry.renderTimeMs)) { |
| 243 | ALOGW("GPU frame time mistmatch %d vs. %u", bucket->render_millis(), |
| 244 | entry.renderTimeMs); |
| 245 | hitMergeError = true; |
| 246 | return; |
| 247 | } |
| 248 | } |
| 249 | bucket->set_frame_count(bucket->frame_count() + entry.frameCount); |
| 250 | index++; |
| 251 | }); |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 252 | return !hitMergeError; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Kweku Adams | 1856a4c | 2018-04-03 16:31:10 -0700 | [diff] [blame] | 255 | static int32_t findPercentile(protos::GraphicsStatsProto* proto, int percentile) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 256 | int32_t pos = percentile * proto->summary().total_frames() / 100; |
| 257 | int32_t remaining = proto->summary().total_frames() - pos; |
| 258 | for (auto it = proto->histogram().rbegin(); it != proto->histogram().rend(); ++it) { |
| 259 | remaining -= it->frame_count(); |
| 260 | if (remaining <= 0) { |
| 261 | return it->render_millis(); |
| 262 | } |
| 263 | } |
| 264 | return 0; |
| 265 | } |
| 266 | |
Stan Iliev | 7203e1f | 2019-07-25 13:12:02 -0400 | [diff] [blame] | 267 | static int32_t findGPUPercentile(protos::GraphicsStatsProto* proto, int percentile) { |
| 268 | uint32_t totalGPUFrameCount = 0; // this is usually proto->summary().total_frames() - 3. |
| 269 | for (auto it = proto->gpu_histogram().rbegin(); it != proto->gpu_histogram().rend(); ++it) { |
| 270 | totalGPUFrameCount += it->frame_count(); |
| 271 | } |
| 272 | int32_t pos = percentile * totalGPUFrameCount / 100; |
| 273 | int32_t remaining = totalGPUFrameCount - pos; |
| 274 | for (auto it = proto->gpu_histogram().rbegin(); it != proto->gpu_histogram().rend(); ++it) { |
| 275 | remaining -= it->frame_count(); |
| 276 | if (remaining <= 0) { |
| 277 | return it->render_millis(); |
| 278 | } |
| 279 | } |
| 280 | return 0; |
| 281 | } |
| 282 | |
Kweku Adams | 1856a4c | 2018-04-03 16:31:10 -0700 | [diff] [blame] | 283 | void dumpAsTextToFd(protos::GraphicsStatsProto* proto, int fd) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 284 | // This isn't a full validation, just enough that we can deref at will |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 285 | if (proto->package_name().empty() || !proto->has_summary()) { |
| 286 | ALOGW("Skipping dump, invalid package_name() '%s' or summary %d", |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 287 | proto->package_name().c_str(), proto->has_summary()); |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 288 | return; |
| 289 | } |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 290 | dprintf(fd, "\nPackage: %s", proto->package_name().c_str()); |
Colin Cross | d013a88 | 2018-10-26 13:04:41 -0700 | [diff] [blame] | 291 | dprintf(fd, "\nVersion: %" PRId64, proto->version_code()); |
| 292 | dprintf(fd, "\nStats since: %" PRId64 "ns", proto->stats_start()); |
| 293 | dprintf(fd, "\nStats end: %" PRId64 "ns", proto->stats_end()); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 294 | auto summary = proto->summary(); |
| 295 | dprintf(fd, "\nTotal frames rendered: %d", summary.total_frames()); |
| 296 | dprintf(fd, "\nJanky frames: %d (%.2f%%)", summary.janky_frames(), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 297 | (float)summary.janky_frames() / (float)summary.total_frames() * 100.0f); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 298 | dprintf(fd, "\n50th percentile: %dms", findPercentile(proto, 50)); |
| 299 | dprintf(fd, "\n90th percentile: %dms", findPercentile(proto, 90)); |
| 300 | dprintf(fd, "\n95th percentile: %dms", findPercentile(proto, 95)); |
| 301 | dprintf(fd, "\n99th percentile: %dms", findPercentile(proto, 99)); |
| 302 | dprintf(fd, "\nNumber Missed Vsync: %d", summary.missed_vsync_count()); |
| 303 | dprintf(fd, "\nNumber High input latency: %d", summary.high_input_latency_count()); |
| 304 | dprintf(fd, "\nNumber Slow UI thread: %d", summary.slow_ui_thread_count()); |
| 305 | dprintf(fd, "\nNumber Slow bitmap uploads: %d", summary.slow_bitmap_upload_count()); |
| 306 | dprintf(fd, "\nNumber Slow issue draw commands: %d", summary.slow_draw_count()); |
John Reck | 0e48647 | 2018-03-19 14:06:16 -0700 | [diff] [blame] | 307 | dprintf(fd, "\nNumber Frame deadline missed: %d", summary.missed_deadline_count()); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 308 | dprintf(fd, "\nHISTOGRAM:"); |
| 309 | for (const auto& it : proto->histogram()) { |
| 310 | dprintf(fd, " %dms=%d", it.render_millis(), it.frame_count()); |
| 311 | } |
Stan Iliev | 7203e1f | 2019-07-25 13:12:02 -0400 | [diff] [blame] | 312 | dprintf(fd, "\n50th gpu percentile: %dms", findGPUPercentile(proto, 50)); |
| 313 | dprintf(fd, "\n90th gpu percentile: %dms", findGPUPercentile(proto, 90)); |
| 314 | dprintf(fd, "\n95th gpu percentile: %dms", findGPUPercentile(proto, 95)); |
| 315 | dprintf(fd, "\n99th gpu percentile: %dms", findGPUPercentile(proto, 99)); |
| 316 | dprintf(fd, "\nGPU HISTOGRAM:"); |
| 317 | for (const auto& it : proto->gpu_histogram()) { |
| 318 | dprintf(fd, " %dms=%d", it.render_millis(), it.frame_count()); |
| 319 | } |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 320 | dprintf(fd, "\n"); |
| 321 | } |
| 322 | |
| 323 | void GraphicsStatsService::saveBuffer(const std::string& path, const std::string& package, |
Dianne Hackborn | 73453e4 | 2017-12-11 16:30:36 -0800 | [diff] [blame] | 324 | int64_t versionCode, int64_t startTime, int64_t endTime, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 325 | const ProfileData* data) { |
Kweku Adams | 1856a4c | 2018-04-03 16:31:10 -0700 | [diff] [blame] | 326 | protos::GraphicsStatsProto statsProto; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 327 | if (!parseFromFile(path, &statsProto)) { |
| 328 | statsProto.Clear(); |
| 329 | } |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 330 | if (!mergeProfileDataIntoProto(&statsProto, package, versionCode, startTime, endTime, data)) { |
| 331 | return; |
| 332 | } |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 333 | // Although we might not have read any data from the file, merging the existing data |
| 334 | // should always fully-initialize the proto |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 335 | if (!statsProto.IsInitialized()) { |
| 336 | ALOGE("proto initialization error %s", statsProto.InitializationErrorString().c_str()); |
| 337 | return; |
| 338 | } |
| 339 | if (statsProto.package_name().empty() || !statsProto.has_summary()) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 340 | ALOGE("missing package_name() '%s' summary %d", statsProto.package_name().c_str(), |
| 341 | statsProto.has_summary()); |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 342 | return; |
| 343 | } |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 344 | int outFd = open(path.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0660); |
| 345 | if (outFd <= 0) { |
| 346 | int err = errno; |
| 347 | ALOGW("Failed to open '%s', error=%d (%s)", path.c_str(), err, strerror(err)); |
| 348 | return; |
| 349 | } |
| 350 | int wrote = write(outFd, &sCurrentFileVersion, sHeaderSize); |
| 351 | if (wrote != sHeaderSize) { |
| 352 | int err = errno; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 353 | ALOGW("Failed to write header to '%s', returned=%d errno=%d (%s)", path.c_str(), wrote, err, |
| 354 | strerror(err)); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 355 | close(outFd); |
| 356 | return; |
| 357 | } |
| 358 | { |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 359 | FileOutputStreamLite output(outFd); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 360 | bool success = statsProto.SerializeToZeroCopyStream(&output) && output.Flush(); |
| 361 | if (output.GetErrno() != 0) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 362 | ALOGW("Error writing to fd=%d, path='%s' err=%d (%s)", outFd, path.c_str(), |
| 363 | output.GetErrno(), strerror(output.GetErrno())); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 364 | success = false; |
| 365 | } else if (!success) { |
| 366 | ALOGW("Serialize failed on '%s' unknown error", path.c_str()); |
| 367 | } |
| 368 | } |
| 369 | close(outFd); |
| 370 | } |
| 371 | |
| 372 | class GraphicsStatsService::Dump { |
| 373 | public: |
Stan Iliev | 637ba5e | 2019-08-16 13:43:08 -0400 | [diff] [blame] | 374 | Dump(int outFd, DumpType type) : mFd(outFd), mType(type) { |
| 375 | if (mFd == -1 && mType == DumpType::Protobuf) { |
| 376 | mType = DumpType::ProtobufStatsd; |
| 377 | } |
| 378 | } |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 379 | int fd() { return mFd; } |
| 380 | DumpType type() { return mType; } |
Kweku Adams | 1856a4c | 2018-04-03 16:31:10 -0700 | [diff] [blame] | 381 | protos::GraphicsStatsServiceDumpProto& proto() { return mProto; } |
Stan Iliev | 637ba5e | 2019-08-16 13:43:08 -0400 | [diff] [blame] | 382 | void mergeStat(const protos::GraphicsStatsProto& stat); |
| 383 | void updateProto(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 384 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 385 | private: |
Stan Iliev | 637ba5e | 2019-08-16 13:43:08 -0400 | [diff] [blame] | 386 | // use package name and app version for a key |
| 387 | typedef std::pair<std::string, int64_t> DumpKey; |
| 388 | |
| 389 | std::map<DumpKey, protos::GraphicsStatsProto> mStats; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 390 | int mFd; |
| 391 | DumpType mType; |
Kweku Adams | 1856a4c | 2018-04-03 16:31:10 -0700 | [diff] [blame] | 392 | protos::GraphicsStatsServiceDumpProto mProto; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 393 | }; |
| 394 | |
Stan Iliev | 637ba5e | 2019-08-16 13:43:08 -0400 | [diff] [blame] | 395 | void GraphicsStatsService::Dump::mergeStat(const protos::GraphicsStatsProto& stat) { |
| 396 | auto dumpKey = std::make_pair(stat.package_name(), stat.version_code()); |
| 397 | auto findIt = mStats.find(dumpKey); |
| 398 | if (findIt == mStats.end()) { |
| 399 | mStats[dumpKey] = stat; |
| 400 | } else { |
| 401 | auto summary = findIt->second.mutable_summary(); |
| 402 | summary->set_total_frames(summary->total_frames() + stat.summary().total_frames()); |
| 403 | summary->set_janky_frames(summary->janky_frames() + stat.summary().janky_frames()); |
| 404 | summary->set_missed_vsync_count(summary->missed_vsync_count() + |
| 405 | stat.summary().missed_vsync_count()); |
| 406 | summary->set_high_input_latency_count(summary->high_input_latency_count() + |
| 407 | stat.summary().high_input_latency_count()); |
| 408 | summary->set_slow_ui_thread_count(summary->slow_ui_thread_count() + |
| 409 | stat.summary().slow_ui_thread_count()); |
| 410 | summary->set_slow_bitmap_upload_count(summary->slow_bitmap_upload_count() + |
| 411 | stat.summary().slow_bitmap_upload_count()); |
| 412 | summary->set_slow_draw_count(summary->slow_draw_count() + stat.summary().slow_draw_count()); |
| 413 | summary->set_missed_deadline_count(summary->missed_deadline_count() + |
| 414 | stat.summary().missed_deadline_count()); |
| 415 | for (int bucketIndex = 0; bucketIndex < findIt->second.histogram_size(); bucketIndex++) { |
| 416 | auto bucket = findIt->second.mutable_histogram(bucketIndex); |
| 417 | bucket->set_frame_count(bucket->frame_count() + |
| 418 | stat.histogram(bucketIndex).frame_count()); |
| 419 | } |
| 420 | for (int bucketIndex = 0; bucketIndex < findIt->second.gpu_histogram_size(); |
| 421 | bucketIndex++) { |
| 422 | auto bucket = findIt->second.mutable_gpu_histogram(bucketIndex); |
| 423 | bucket->set_frame_count(bucket->frame_count() + |
| 424 | stat.gpu_histogram(bucketIndex).frame_count()); |
| 425 | } |
| 426 | findIt->second.set_stats_start(std::min(findIt->second.stats_start(), stat.stats_start())); |
| 427 | findIt->second.set_stats_end(std::max(findIt->second.stats_end(), stat.stats_end())); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | void GraphicsStatsService::Dump::updateProto() { |
| 432 | for (auto& stat : mStats) { |
| 433 | mProto.add_stats()->CopyFrom(stat.second); |
| 434 | } |
| 435 | } |
| 436 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 437 | GraphicsStatsService::Dump* GraphicsStatsService::createDump(int outFd, DumpType type) { |
| 438 | return new Dump(outFd, type); |
| 439 | } |
| 440 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 441 | void GraphicsStatsService::addToDump(Dump* dump, const std::string& path, |
Dianne Hackborn | 73453e4 | 2017-12-11 16:30:36 -0800 | [diff] [blame] | 442 | const std::string& package, int64_t versionCode, |
| 443 | int64_t startTime, int64_t endTime, const ProfileData* data) { |
Kweku Adams | 1856a4c | 2018-04-03 16:31:10 -0700 | [diff] [blame] | 444 | protos::GraphicsStatsProto statsProto; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 445 | if (!path.empty() && !parseFromFile(path, &statsProto)) { |
| 446 | statsProto.Clear(); |
| 447 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 448 | if (data && |
| 449 | !mergeProfileDataIntoProto(&statsProto, package, versionCode, startTime, endTime, data)) { |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 450 | return; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 451 | } |
| 452 | if (!statsProto.IsInitialized()) { |
| 453 | ALOGW("Failed to load profile data from path '%s' and data %p", |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 454 | path.empty() ? "<empty>" : path.c_str(), data); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 455 | return; |
| 456 | } |
Stan Iliev | 637ba5e | 2019-08-16 13:43:08 -0400 | [diff] [blame] | 457 | if (dump->type() == DumpType::ProtobufStatsd) { |
| 458 | dump->mergeStat(statsProto); |
| 459 | } else if (dump->type() == DumpType::Protobuf) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 460 | dump->proto().add_stats()->CopyFrom(statsProto); |
| 461 | } else { |
| 462 | dumpAsTextToFd(&statsProto, dump->fd()); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | void GraphicsStatsService::addToDump(Dump* dump, const std::string& path) { |
Kweku Adams | 1856a4c | 2018-04-03 16:31:10 -0700 | [diff] [blame] | 467 | protos::GraphicsStatsProto statsProto; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 468 | if (!parseFromFile(path, &statsProto)) { |
| 469 | return; |
| 470 | } |
Stan Iliev | 637ba5e | 2019-08-16 13:43:08 -0400 | [diff] [blame] | 471 | if (dump->type() == DumpType::ProtobufStatsd) { |
| 472 | dump->mergeStat(statsProto); |
| 473 | } else if (dump->type() == DumpType::Protobuf) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 474 | dump->proto().add_stats()->CopyFrom(statsProto); |
| 475 | } else { |
| 476 | dumpAsTextToFd(&statsProto, dump->fd()); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | void GraphicsStatsService::finishDump(Dump* dump) { |
| 481 | if (dump->type() == DumpType::Protobuf) { |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 482 | FileOutputStreamLite stream(dump->fd()); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 483 | dump->proto().SerializeToZeroCopyStream(&stream); |
| 484 | } |
| 485 | delete dump; |
| 486 | } |
| 487 | |
Stan Iliev | 637ba5e | 2019-08-16 13:43:08 -0400 | [diff] [blame] | 488 | class MemOutputStreamLite : public io::ZeroCopyOutputStream { |
| 489 | public: |
| 490 | explicit MemOutputStreamLite() : mCopyAdapter(), mImpl(&mCopyAdapter) {} |
| 491 | virtual ~MemOutputStreamLite() {} |
| 492 | |
| 493 | virtual bool Next(void** data, int* size) override { return mImpl.Next(data, size); } |
| 494 | |
| 495 | virtual void BackUp(int count) override { mImpl.BackUp(count); } |
| 496 | |
| 497 | virtual int64 ByteCount() const override { return mImpl.ByteCount(); } |
| 498 | |
| 499 | bool Flush() { return mImpl.Flush(); } |
| 500 | |
| 501 | void copyData(const DumpMemoryFn& reader, void* param1, void* param2) { |
| 502 | int bufferOffset = 0; |
| 503 | int totalSize = mCopyAdapter.mBuffersSize - mCopyAdapter.mCurrentBufferUnusedSize; |
| 504 | int totalDataLeft = totalSize; |
| 505 | for (auto& it : mCopyAdapter.mBuffers) { |
| 506 | int bufferSize = std::min(totalDataLeft, (int)it.size()); // last buffer is not full |
| 507 | reader(it.data(), bufferOffset, bufferSize, totalSize, param1, param2); |
| 508 | bufferOffset += bufferSize; |
| 509 | totalDataLeft -= bufferSize; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | private: |
| 514 | struct MemAdapter : public io::CopyingOutputStream { |
| 515 | // Data is stored in an array of buffers. |
| 516 | // JNI SetByteArrayRegion assembles data in one continuous Java byte[] buffer. |
| 517 | std::vector<std::vector<unsigned char>> mBuffers; |
| 518 | int mBuffersSize = 0; // total bytes allocated in mBuffers |
| 519 | int mCurrentBufferUnusedSize = 0; // unused bytes in the last buffer mBuffers.back() |
| 520 | unsigned char* mCurrentBuffer = nullptr; // pointer to next free byte in mBuffers.back() |
| 521 | |
| 522 | explicit MemAdapter() {} |
| 523 | virtual ~MemAdapter() {} |
| 524 | |
| 525 | virtual bool Write(const void* buffer, int size) override { |
| 526 | while (size > 0) { |
| 527 | if (0 == mCurrentBufferUnusedSize) { |
| 528 | mCurrentBufferUnusedSize = |
| 529 | std::max(size, mBuffersSize ? 2 * mBuffersSize : 10000); |
| 530 | mBuffers.emplace_back(); |
| 531 | mBuffers.back().resize(mCurrentBufferUnusedSize); |
| 532 | mCurrentBuffer = mBuffers.back().data(); |
| 533 | mBuffersSize += mCurrentBufferUnusedSize; |
| 534 | } |
| 535 | int dataMoved = std::min(mCurrentBufferUnusedSize, size); |
| 536 | memcpy(mCurrentBuffer, buffer, dataMoved); |
| 537 | mCurrentBufferUnusedSize -= dataMoved; |
| 538 | mCurrentBuffer += dataMoved; |
| 539 | buffer = reinterpret_cast<const unsigned char*>(buffer) + dataMoved; |
| 540 | size -= dataMoved; |
| 541 | } |
| 542 | return true; |
| 543 | } |
| 544 | }; |
| 545 | |
| 546 | MemOutputStreamLite::MemAdapter mCopyAdapter; |
| 547 | io::CopyingOutputStreamAdaptor mImpl; |
| 548 | }; |
| 549 | |
| 550 | void GraphicsStatsService::finishDumpInMemory(Dump* dump, const DumpMemoryFn& reader, void* param1, |
| 551 | void* param2) { |
| 552 | MemOutputStreamLite stream; |
| 553 | dump->updateProto(); |
| 554 | bool success = dump->proto().SerializeToZeroCopyStream(&stream) && stream.Flush(); |
| 555 | delete dump; |
| 556 | if (!success) { |
| 557 | return; |
| 558 | } |
| 559 | stream.copyData(reader, param1, param2); |
| 560 | } |
| 561 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 562 | } /* namespace uirenderer */ |
Dan Albert | 110e007 | 2017-10-11 12:41:26 -0700 | [diff] [blame] | 563 | } /* namespace android */ |