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 | |
| 19 | #include "JankTracker.h" |
| 20 | |
| 21 | #include <frameworks/base/core/proto/android/service/graphicsstats.pb.h> |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 22 | #include <google/protobuf/io/zero_copy_stream_impl_lite.h> |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 23 | #include <log/log.h> |
| 24 | |
Dan Albert | 110e007 | 2017-10-11 12:41:26 -0700 | [diff] [blame] | 25 | #include <errno.h> |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 26 | #include <fcntl.h> |
Dan Albert | 110e007 | 2017-10-11 12:41:26 -0700 | [diff] [blame] | 27 | #include <inttypes.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 28 | #include <sys/mman.h> |
Dan Albert | 110e007 | 2017-10-11 12:41:26 -0700 | [diff] [blame] | 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 31 | #include <unistd.h> |
| 32 | |
| 33 | namespace android { |
| 34 | namespace uirenderer { |
| 35 | |
| 36 | using namespace google::protobuf; |
| 37 | |
| 38 | constexpr int32_t sCurrentFileVersion = 1; |
| 39 | constexpr int32_t sHeaderSize = 4; |
| 40 | static_assert(sizeof(sCurrentFileVersion) == sHeaderSize, "Header size is wrong"); |
| 41 | |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 42 | constexpr int sHistogramSize = ProfileData::HistogramSize(); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 43 | |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 44 | static bool mergeProfileDataIntoProto(service::GraphicsStatsProto* proto, |
Dianne Hackborn | 73453e4 | 2017-12-11 16:30:36 -0800 | [diff] [blame^] | 45 | const std::string& package, int64_t versionCode, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 46 | int64_t startTime, int64_t endTime, const ProfileData* data); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 47 | static void dumpAsTextToFd(service::GraphicsStatsProto* proto, int outFd); |
| 48 | |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 49 | class FileDescriptor { |
| 50 | public: |
| 51 | FileDescriptor(int fd) : mFd(fd) {} |
| 52 | ~FileDescriptor() { |
| 53 | if (mFd != -1) { |
| 54 | close(mFd); |
| 55 | mFd = -1; |
| 56 | } |
| 57 | } |
| 58 | bool valid() { return mFd != -1; } |
| 59 | operator int() { return mFd; } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 60 | |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 61 | private: |
| 62 | int mFd; |
| 63 | }; |
| 64 | |
| 65 | class FileOutputStreamLite : public io::ZeroCopyOutputStream { |
| 66 | public: |
| 67 | FileOutputStreamLite(int fd) : mCopyAdapter(fd), mImpl(&mCopyAdapter) {} |
| 68 | virtual ~FileOutputStreamLite() {} |
| 69 | |
| 70 | int GetErrno() { return mCopyAdapter.mErrno; } |
| 71 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 72 | 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] | 73 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 74 | virtual void BackUp(int count) override { mImpl.BackUp(count); } |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 75 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 76 | virtual int64 ByteCount() const override { return mImpl.ByteCount(); } |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 77 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 78 | bool Flush() { return mImpl.Flush(); } |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 79 | |
| 80 | private: |
| 81 | struct FDAdapter : public io::CopyingOutputStream { |
| 82 | int mFd; |
| 83 | int mErrno = 0; |
| 84 | |
| 85 | FDAdapter(int fd) : mFd(fd) {} |
| 86 | virtual ~FDAdapter() {} |
| 87 | |
| 88 | virtual bool Write(const void* buffer, int size) override { |
| 89 | int ret; |
| 90 | while (size) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 91 | ret = TEMP_FAILURE_RETRY(write(mFd, buffer, size)); |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 92 | if (ret <= 0) { |
| 93 | mErrno = errno; |
| 94 | return false; |
| 95 | } |
| 96 | size -= ret; |
| 97 | } |
| 98 | return true; |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | FileOutputStreamLite::FDAdapter mCopyAdapter; |
| 103 | io::CopyingOutputStreamAdaptor mImpl; |
| 104 | }; |
| 105 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 106 | bool GraphicsStatsService::parseFromFile(const std::string& path, |
| 107 | service::GraphicsStatsProto* output) { |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 108 | FileDescriptor fd{open(path.c_str(), O_RDONLY)}; |
| 109 | if (!fd.valid()) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 110 | int err = errno; |
| 111 | // The file not existing is normal for addToDump(), so only log if |
| 112 | // we get an unexpected error |
| 113 | if (err != ENOENT) { |
| 114 | ALOGW("Failed to open '%s', errno=%d (%s)", path.c_str(), err, strerror(err)); |
| 115 | } |
| 116 | return false; |
| 117 | } |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 118 | struct stat sb; |
| 119 | if (fstat(fd, &sb) || sb.st_size < sHeaderSize) { |
| 120 | int err = errno; |
| 121 | // The file not existing is normal for addToDump(), so only log if |
| 122 | // we get an unexpected error |
| 123 | if (err != ENOENT) { |
| 124 | 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] | 125 | strerror(err), (int)sb.st_size); |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 126 | } |
| 127 | return false; |
| 128 | } |
| 129 | void* addr = mmap(nullptr, sb.st_size, PROT_READ, MAP_SHARED, fd, 0); |
| 130 | if (!addr) { |
| 131 | int err = errno; |
| 132 | // The file not existing is normal for addToDump(), so only log if |
| 133 | // we get an unexpected error |
| 134 | if (err != ENOENT) { |
| 135 | ALOGW("Failed to mmap '%s', errno=%d (%s)", path.c_str(), err, strerror(err)); |
| 136 | } |
| 137 | return false; |
| 138 | } |
| 139 | uint32_t file_version = *reinterpret_cast<uint32_t*>(addr); |
| 140 | if (file_version != sCurrentFileVersion) { |
| 141 | ALOGW("file_version mismatch! expected %d got %d", sCurrentFileVersion, file_version); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 142 | return false; |
| 143 | } |
| 144 | |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 145 | void* data = reinterpret_cast<uint8_t*>(addr) + sHeaderSize; |
| 146 | int dataSize = sb.st_size - sHeaderSize; |
| 147 | io::ArrayInputStream input{data, dataSize}; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 148 | bool success = output->ParseFromZeroCopyStream(&input); |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 149 | if (!success) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 150 | ALOGW("Parse failed on '%s' error='%s'", path.c_str(), |
| 151 | output->InitializationErrorString().c_str()); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 152 | } |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 153 | return success; |
| 154 | } |
| 155 | |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 156 | bool mergeProfileDataIntoProto(service::GraphicsStatsProto* proto, const std::string& package, |
Dianne Hackborn | 73453e4 | 2017-12-11 16:30:36 -0800 | [diff] [blame^] | 157 | int64_t versionCode, int64_t startTime, int64_t endTime, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 158 | const ProfileData* data) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 159 | if (proto->stats_start() == 0 || proto->stats_start() > startTime) { |
| 160 | proto->set_stats_start(startTime); |
| 161 | } |
| 162 | if (proto->stats_end() == 0 || proto->stats_end() < endTime) { |
| 163 | proto->set_stats_end(endTime); |
| 164 | } |
| 165 | proto->set_package_name(package); |
| 166 | proto->set_version_code(versionCode); |
| 167 | auto summary = proto->mutable_summary(); |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 168 | summary->set_total_frames(summary->total_frames() + data->totalFrameCount()); |
| 169 | summary->set_janky_frames(summary->janky_frames() + data->jankFrameCount()); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 170 | summary->set_missed_vsync_count(summary->missed_vsync_count() + |
| 171 | data->jankTypeCount(kMissedVsync)); |
| 172 | summary->set_high_input_latency_count(summary->high_input_latency_count() + |
| 173 | data->jankTypeCount(kHighInputLatency)); |
| 174 | summary->set_slow_ui_thread_count(summary->slow_ui_thread_count() + |
| 175 | data->jankTypeCount(kSlowUI)); |
| 176 | summary->set_slow_bitmap_upload_count(summary->slow_bitmap_upload_count() + |
| 177 | data->jankTypeCount(kSlowSync)); |
| 178 | summary->set_slow_draw_count(summary->slow_draw_count() + data->jankTypeCount(kSlowRT)); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 179 | |
| 180 | bool creatingHistogram = false; |
| 181 | if (proto->histogram_size() == 0) { |
| 182 | proto->mutable_histogram()->Reserve(sHistogramSize); |
| 183 | creatingHistogram = true; |
| 184 | } else if (proto->histogram_size() != sHistogramSize) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 185 | ALOGE("Histogram size mismatch, proto is %d expected %d", proto->histogram_size(), |
| 186 | sHistogramSize); |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 187 | return false; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 188 | } |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 189 | int index = 0; |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 190 | bool hitMergeError = false; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 191 | data->histogramForEach([&](ProfileData::HistogramEntry entry) { |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 192 | if (hitMergeError) return; |
| 193 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 194 | service::GraphicsStatsHistogramBucketProto* bucket; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 195 | if (creatingHistogram) { |
| 196 | bucket = proto->add_histogram(); |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 197 | bucket->set_render_millis(entry.renderTimeMs); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 198 | } else { |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 199 | bucket = proto->mutable_histogram(index); |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 200 | if (bucket->render_millis() != static_cast<int32_t>(entry.renderTimeMs)) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 201 | ALOGW("Frame time mistmatch %d vs. %u", bucket->render_millis(), |
| 202 | entry.renderTimeMs); |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 203 | hitMergeError = true; |
| 204 | return; |
| 205 | } |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 206 | } |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 207 | bucket->set_frame_count(bucket->frame_count() + entry.frameCount); |
| 208 | index++; |
| 209 | }); |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 210 | return !hitMergeError; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static int32_t findPercentile(service::GraphicsStatsProto* proto, int percentile) { |
| 214 | int32_t pos = percentile * proto->summary().total_frames() / 100; |
| 215 | int32_t remaining = proto->summary().total_frames() - pos; |
| 216 | for (auto it = proto->histogram().rbegin(); it != proto->histogram().rend(); ++it) { |
| 217 | remaining -= it->frame_count(); |
| 218 | if (remaining <= 0) { |
| 219 | return it->render_millis(); |
| 220 | } |
| 221 | } |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | void dumpAsTextToFd(service::GraphicsStatsProto* proto, int fd) { |
| 226 | // 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] | 227 | if (proto->package_name().empty() || !proto->has_summary()) { |
| 228 | ALOGW("Skipping dump, invalid package_name() '%s' or summary %d", |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 229 | proto->package_name().c_str(), proto->has_summary()); |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 230 | return; |
| 231 | } |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 232 | dprintf(fd, "\nPackage: %s", proto->package_name().c_str()); |
Dianne Hackborn | 73453e4 | 2017-12-11 16:30:36 -0800 | [diff] [blame^] | 233 | dprintf(fd, "\nVersion: %lld", proto->version_code()); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 234 | dprintf(fd, "\nStats since: %lldns", proto->stats_start()); |
| 235 | dprintf(fd, "\nStats end: %lldns", proto->stats_end()); |
| 236 | auto summary = proto->summary(); |
| 237 | dprintf(fd, "\nTotal frames rendered: %d", summary.total_frames()); |
| 238 | dprintf(fd, "\nJanky frames: %d (%.2f%%)", summary.janky_frames(), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 239 | (float)summary.janky_frames() / (float)summary.total_frames() * 100.0f); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 240 | dprintf(fd, "\n50th percentile: %dms", findPercentile(proto, 50)); |
| 241 | dprintf(fd, "\n90th percentile: %dms", findPercentile(proto, 90)); |
| 242 | dprintf(fd, "\n95th percentile: %dms", findPercentile(proto, 95)); |
| 243 | dprintf(fd, "\n99th percentile: %dms", findPercentile(proto, 99)); |
| 244 | dprintf(fd, "\nNumber Missed Vsync: %d", summary.missed_vsync_count()); |
| 245 | dprintf(fd, "\nNumber High input latency: %d", summary.high_input_latency_count()); |
| 246 | dprintf(fd, "\nNumber Slow UI thread: %d", summary.slow_ui_thread_count()); |
| 247 | dprintf(fd, "\nNumber Slow bitmap uploads: %d", summary.slow_bitmap_upload_count()); |
| 248 | dprintf(fd, "\nNumber Slow issue draw commands: %d", summary.slow_draw_count()); |
| 249 | dprintf(fd, "\nHISTOGRAM:"); |
| 250 | for (const auto& it : proto->histogram()) { |
| 251 | dprintf(fd, " %dms=%d", it.render_millis(), it.frame_count()); |
| 252 | } |
| 253 | dprintf(fd, "\n"); |
| 254 | } |
| 255 | |
| 256 | void GraphicsStatsService::saveBuffer(const std::string& path, const std::string& package, |
Dianne Hackborn | 73453e4 | 2017-12-11 16:30:36 -0800 | [diff] [blame^] | 257 | int64_t versionCode, int64_t startTime, int64_t endTime, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 258 | const ProfileData* data) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 259 | service::GraphicsStatsProto statsProto; |
| 260 | if (!parseFromFile(path, &statsProto)) { |
| 261 | statsProto.Clear(); |
| 262 | } |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 263 | if (!mergeProfileDataIntoProto(&statsProto, package, versionCode, startTime, endTime, data)) { |
| 264 | return; |
| 265 | } |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 266 | // Although we might not have read any data from the file, merging the existing data |
| 267 | // should always fully-initialize the proto |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 268 | if (!statsProto.IsInitialized()) { |
| 269 | ALOGE("proto initialization error %s", statsProto.InitializationErrorString().c_str()); |
| 270 | return; |
| 271 | } |
| 272 | if (statsProto.package_name().empty() || !statsProto.has_summary()) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 273 | ALOGE("missing package_name() '%s' summary %d", statsProto.package_name().c_str(), |
| 274 | statsProto.has_summary()); |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 275 | return; |
| 276 | } |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 277 | int outFd = open(path.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0660); |
| 278 | if (outFd <= 0) { |
| 279 | int err = errno; |
| 280 | ALOGW("Failed to open '%s', error=%d (%s)", path.c_str(), err, strerror(err)); |
| 281 | return; |
| 282 | } |
| 283 | int wrote = write(outFd, &sCurrentFileVersion, sHeaderSize); |
| 284 | if (wrote != sHeaderSize) { |
| 285 | int err = errno; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 286 | ALOGW("Failed to write header to '%s', returned=%d errno=%d (%s)", path.c_str(), wrote, err, |
| 287 | strerror(err)); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 288 | close(outFd); |
| 289 | return; |
| 290 | } |
| 291 | { |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 292 | FileOutputStreamLite output(outFd); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 293 | bool success = statsProto.SerializeToZeroCopyStream(&output) && output.Flush(); |
| 294 | if (output.GetErrno() != 0) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 295 | ALOGW("Error writing to fd=%d, path='%s' err=%d (%s)", outFd, path.c_str(), |
| 296 | output.GetErrno(), strerror(output.GetErrno())); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 297 | success = false; |
| 298 | } else if (!success) { |
| 299 | ALOGW("Serialize failed on '%s' unknown error", path.c_str()); |
| 300 | } |
| 301 | } |
| 302 | close(outFd); |
| 303 | } |
| 304 | |
| 305 | class GraphicsStatsService::Dump { |
| 306 | public: |
| 307 | Dump(int outFd, DumpType type) : mFd(outFd), mType(type) {} |
| 308 | int fd() { return mFd; } |
| 309 | DumpType type() { return mType; } |
| 310 | service::GraphicsStatsServiceDumpProto& proto() { return mProto; } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 311 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 312 | private: |
| 313 | int mFd; |
| 314 | DumpType mType; |
| 315 | service::GraphicsStatsServiceDumpProto mProto; |
| 316 | }; |
| 317 | |
| 318 | GraphicsStatsService::Dump* GraphicsStatsService::createDump(int outFd, DumpType type) { |
| 319 | return new Dump(outFd, type); |
| 320 | } |
| 321 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 322 | void GraphicsStatsService::addToDump(Dump* dump, const std::string& path, |
Dianne Hackborn | 73453e4 | 2017-12-11 16:30:36 -0800 | [diff] [blame^] | 323 | const std::string& package, int64_t versionCode, |
| 324 | int64_t startTime, int64_t endTime, const ProfileData* data) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 325 | service::GraphicsStatsProto statsProto; |
| 326 | if (!path.empty() && !parseFromFile(path, &statsProto)) { |
| 327 | statsProto.Clear(); |
| 328 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 329 | if (data && |
| 330 | !mergeProfileDataIntoProto(&statsProto, package, versionCode, startTime, endTime, data)) { |
John Reck | 5206a87 | 2017-09-18 11:08:31 -0700 | [diff] [blame] | 331 | return; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 332 | } |
| 333 | if (!statsProto.IsInitialized()) { |
| 334 | ALOGW("Failed to load profile data from path '%s' and data %p", |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 335 | path.empty() ? "<empty>" : path.c_str(), data); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 336 | return; |
| 337 | } |
| 338 | |
| 339 | if (dump->type() == DumpType::Protobuf) { |
| 340 | dump->proto().add_stats()->CopyFrom(statsProto); |
| 341 | } else { |
| 342 | dumpAsTextToFd(&statsProto, dump->fd()); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | void GraphicsStatsService::addToDump(Dump* dump, const std::string& path) { |
| 347 | service::GraphicsStatsProto statsProto; |
| 348 | if (!parseFromFile(path, &statsProto)) { |
| 349 | return; |
| 350 | } |
| 351 | if (dump->type() == DumpType::Protobuf) { |
| 352 | dump->proto().add_stats()->CopyFrom(statsProto); |
| 353 | } else { |
| 354 | dumpAsTextToFd(&statsProto, dump->fd()); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | void GraphicsStatsService::finishDump(Dump* dump) { |
| 359 | if (dump->type() == DumpType::Protobuf) { |
John Reck | 915883b | 2017-05-03 10:27:20 -0700 | [diff] [blame] | 360 | FileOutputStreamLite stream(dump->fd()); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 361 | dump->proto().SerializeToZeroCopyStream(&stream); |
| 362 | } |
| 363 | delete dump; |
| 364 | } |
| 365 | |
| 366 | } /* namespace uirenderer */ |
Dan Albert | 110e007 | 2017-10-11 12:41:26 -0700 | [diff] [blame] | 367 | } /* namespace android */ |