blob: 29e4256e2ab786d8e1559a032dfb40a0f4f3ce43 [file] [log] [blame]
John Reckdf1742e2017-01-19 15:56:21 -08001/*
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"
Kweku Adams1856a4c2018-04-03 16:31:10 -070020#include "protos/graphicsstats.pb.h"
John Reckdf1742e2017-01-19 15:56:21 -080021
John Reck915883b2017-05-03 10:27:20 -070022#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
John Reckdf1742e2017-01-19 15:56:21 -080023#include <log/log.h>
24
Dan Albert110e0072017-10-11 12:41:26 -070025#include <errno.h>
John Reckdf1742e2017-01-19 15:56:21 -080026#include <fcntl.h>
Dan Albert110e0072017-10-11 12:41:26 -070027#include <inttypes.h>
John Reck1bcacfd2017-11-03 10:12:19 -070028#include <sys/mman.h>
Dan Albert110e0072017-10-11 12:41:26 -070029#include <sys/stat.h>
30#include <sys/types.h>
John Reckdf1742e2017-01-19 15:56:21 -080031#include <unistd.h>
32
33namespace android {
34namespace uirenderer {
35
36using namespace google::protobuf;
37
38constexpr int32_t sCurrentFileVersion = 1;
39constexpr int32_t sHeaderSize = 4;
40static_assert(sizeof(sCurrentFileVersion) == sHeaderSize, "Header size is wrong");
41
John Reck7075c792017-07-05 14:03:43 -070042constexpr int sHistogramSize = ProfileData::HistogramSize();
John Reckdf1742e2017-01-19 15:56:21 -080043
Kweku Adams1856a4c2018-04-03 16:31:10 -070044static bool mergeProfileDataIntoProto(protos::GraphicsStatsProto* proto,
Dianne Hackborn73453e42017-12-11 16:30:36 -080045 const std::string& package, int64_t versionCode,
John Reck1bcacfd2017-11-03 10:12:19 -070046 int64_t startTime, int64_t endTime, const ProfileData* data);
Kweku Adams1856a4c2018-04-03 16:31:10 -070047static void dumpAsTextToFd(protos::GraphicsStatsProto* proto, int outFd);
John Reckdf1742e2017-01-19 15:56:21 -080048
John Reck915883b2017-05-03 10:27:20 -070049class FileDescriptor {
50public:
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 Reck1bcacfd2017-11-03 10:12:19 -070060
John Reck915883b2017-05-03 10:27:20 -070061private:
62 int mFd;
63};
64
65class FileOutputStreamLite : public io::ZeroCopyOutputStream {
66public:
67 FileOutputStreamLite(int fd) : mCopyAdapter(fd), mImpl(&mCopyAdapter) {}
68 virtual ~FileOutputStreamLite() {}
69
70 int GetErrno() { return mCopyAdapter.mErrno; }
71
John Reck1bcacfd2017-11-03 10:12:19 -070072 virtual bool Next(void** data, int* size) override { return mImpl.Next(data, size); }
John Reck915883b2017-05-03 10:27:20 -070073
John Reck1bcacfd2017-11-03 10:12:19 -070074 virtual void BackUp(int count) override { mImpl.BackUp(count); }
John Reck915883b2017-05-03 10:27:20 -070075
John Reck1bcacfd2017-11-03 10:12:19 -070076 virtual int64 ByteCount() const override { return mImpl.ByteCount(); }
John Reck915883b2017-05-03 10:27:20 -070077
John Reck1bcacfd2017-11-03 10:12:19 -070078 bool Flush() { return mImpl.Flush(); }
John Reck915883b2017-05-03 10:27:20 -070079
80private:
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 Reck1bcacfd2017-11-03 10:12:19 -070091 ret = TEMP_FAILURE_RETRY(write(mFd, buffer, size));
John Reck915883b2017-05-03 10:27:20 -070092 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 Reck1bcacfd2017-11-03 10:12:19 -0700106bool GraphicsStatsService::parseFromFile(const std::string& path,
Kweku Adams1856a4c2018-04-03 16:31:10 -0700107 protos::GraphicsStatsProto* output) {
John Reck915883b2017-05-03 10:27:20 -0700108 FileDescriptor fd{open(path.c_str(), O_RDONLY)};
109 if (!fd.valid()) {
John Reckdf1742e2017-01-19 15:56:21 -0800110 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 Reck915883b2017-05-03 10:27:20 -0700118 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 Reck1bcacfd2017-11-03 10:12:19 -0700125 strerror(err), (int)sb.st_size);
John Reck915883b2017-05-03 10:27:20 -0700126 }
127 return false;
128 }
129 void* addr = mmap(nullptr, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
zhangkuili24a1bc32018-05-29 10:23:29 +0800130 if (addr == MAP_FAILED) {
John Reck915883b2017-05-03 10:27:20 -0700131 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);
liulvping4832438c2018-12-20 20:34:56 +0800142 munmap(addr, sb.st_size);
John Reckdf1742e2017-01-19 15:56:21 -0800143 return false;
144 }
145
John Reck915883b2017-05-03 10:27:20 -0700146 void* data = reinterpret_cast<uint8_t*>(addr) + sHeaderSize;
147 int dataSize = sb.st_size - sHeaderSize;
148 io::ArrayInputStream input{data, dataSize};
John Reckdf1742e2017-01-19 15:56:21 -0800149 bool success = output->ParseFromZeroCopyStream(&input);
John Reck915883b2017-05-03 10:27:20 -0700150 if (!success) {
John Reck1bcacfd2017-11-03 10:12:19 -0700151 ALOGW("Parse failed on '%s' error='%s'", path.c_str(),
152 output->InitializationErrorString().c_str());
John Reckdf1742e2017-01-19 15:56:21 -0800153 }
liulvping4832438c2018-12-20 20:34:56 +0800154 munmap(addr, sb.st_size);
John Reckdf1742e2017-01-19 15:56:21 -0800155 return success;
156}
157
Kweku Adams1856a4c2018-04-03 16:31:10 -0700158bool mergeProfileDataIntoProto(protos::GraphicsStatsProto* proto, const std::string& package,
Dianne Hackborn73453e42017-12-11 16:30:36 -0800159 int64_t versionCode, int64_t startTime, int64_t endTime,
John Reck1bcacfd2017-11-03 10:12:19 -0700160 const ProfileData* data) {
John Reckdf1742e2017-01-19 15:56:21 -0800161 if (proto->stats_start() == 0 || proto->stats_start() > startTime) {
162 proto->set_stats_start(startTime);
163 }
164 if (proto->stats_end() == 0 || proto->stats_end() < endTime) {
165 proto->set_stats_end(endTime);
166 }
167 proto->set_package_name(package);
168 proto->set_version_code(versionCode);
169 auto summary = proto->mutable_summary();
John Reck7075c792017-07-05 14:03:43 -0700170 summary->set_total_frames(summary->total_frames() + data->totalFrameCount());
171 summary->set_janky_frames(summary->janky_frames() + data->jankFrameCount());
John Reck1bcacfd2017-11-03 10:12:19 -0700172 summary->set_missed_vsync_count(summary->missed_vsync_count() +
173 data->jankTypeCount(kMissedVsync));
174 summary->set_high_input_latency_count(summary->high_input_latency_count() +
175 data->jankTypeCount(kHighInputLatency));
176 summary->set_slow_ui_thread_count(summary->slow_ui_thread_count() +
177 data->jankTypeCount(kSlowUI));
178 summary->set_slow_bitmap_upload_count(summary->slow_bitmap_upload_count() +
179 data->jankTypeCount(kSlowSync));
180 summary->set_slow_draw_count(summary->slow_draw_count() + data->jankTypeCount(kSlowRT));
John Reck0e486472018-03-19 14:06:16 -0700181 summary->set_missed_deadline_count(summary->missed_deadline_count()
182 + data->jankTypeCount(kMissedDeadline));
John Reckdf1742e2017-01-19 15:56:21 -0800183
184 bool creatingHistogram = false;
185 if (proto->histogram_size() == 0) {
186 proto->mutable_histogram()->Reserve(sHistogramSize);
187 creatingHistogram = true;
188 } else if (proto->histogram_size() != sHistogramSize) {
John Reck1bcacfd2017-11-03 10:12:19 -0700189 ALOGE("Histogram size mismatch, proto is %d expected %d", proto->histogram_size(),
190 sHistogramSize);
John Reck5206a872017-09-18 11:08:31 -0700191 return false;
John Reckdf1742e2017-01-19 15:56:21 -0800192 }
John Reck7075c792017-07-05 14:03:43 -0700193 int index = 0;
John Reck5206a872017-09-18 11:08:31 -0700194 bool hitMergeError = false;
John Reck7075c792017-07-05 14:03:43 -0700195 data->histogramForEach([&](ProfileData::HistogramEntry entry) {
John Reck5206a872017-09-18 11:08:31 -0700196 if (hitMergeError) return;
197
Kweku Adams1856a4c2018-04-03 16:31:10 -0700198 protos::GraphicsStatsHistogramBucketProto* bucket;
John Reckdf1742e2017-01-19 15:56:21 -0800199 if (creatingHistogram) {
200 bucket = proto->add_histogram();
John Reck7075c792017-07-05 14:03:43 -0700201 bucket->set_render_millis(entry.renderTimeMs);
John Reckdf1742e2017-01-19 15:56:21 -0800202 } else {
John Reck7075c792017-07-05 14:03:43 -0700203 bucket = proto->mutable_histogram(index);
John Reck5206a872017-09-18 11:08:31 -0700204 if (bucket->render_millis() != static_cast<int32_t>(entry.renderTimeMs)) {
John Reck1bcacfd2017-11-03 10:12:19 -0700205 ALOGW("Frame time mistmatch %d vs. %u", bucket->render_millis(),
206 entry.renderTimeMs);
John Reck5206a872017-09-18 11:08:31 -0700207 hitMergeError = true;
208 return;
209 }
John Reckdf1742e2017-01-19 15:56:21 -0800210 }
John Reck7075c792017-07-05 14:03:43 -0700211 bucket->set_frame_count(bucket->frame_count() + entry.frameCount);
212 index++;
213 });
John Reck5206a872017-09-18 11:08:31 -0700214 return !hitMergeError;
John Reckdf1742e2017-01-19 15:56:21 -0800215}
216
Kweku Adams1856a4c2018-04-03 16:31:10 -0700217static int32_t findPercentile(protos::GraphicsStatsProto* proto, int percentile) {
John Reckdf1742e2017-01-19 15:56:21 -0800218 int32_t pos = percentile * proto->summary().total_frames() / 100;
219 int32_t remaining = proto->summary().total_frames() - pos;
220 for (auto it = proto->histogram().rbegin(); it != proto->histogram().rend(); ++it) {
221 remaining -= it->frame_count();
222 if (remaining <= 0) {
223 return it->render_millis();
224 }
225 }
226 return 0;
227}
228
Kweku Adams1856a4c2018-04-03 16:31:10 -0700229void dumpAsTextToFd(protos::GraphicsStatsProto* proto, int fd) {
John Reckdf1742e2017-01-19 15:56:21 -0800230 // This isn't a full validation, just enough that we can deref at will
John Reck5206a872017-09-18 11:08:31 -0700231 if (proto->package_name().empty() || !proto->has_summary()) {
232 ALOGW("Skipping dump, invalid package_name() '%s' or summary %d",
John Reck1bcacfd2017-11-03 10:12:19 -0700233 proto->package_name().c_str(), proto->has_summary());
John Reck5206a872017-09-18 11:08:31 -0700234 return;
235 }
John Reckdf1742e2017-01-19 15:56:21 -0800236 dprintf(fd, "\nPackage: %s", proto->package_name().c_str());
Colin Cross054b0c02018-11-04 17:24:17 -0800237 dprintf(fd, "\nVersion: %lld", proto->version_code());
238 dprintf(fd, "\nStats since: %lldns", proto->stats_start());
239 dprintf(fd, "\nStats end: %lldns", proto->stats_end());
John Reckdf1742e2017-01-19 15:56:21 -0800240 auto summary = proto->summary();
241 dprintf(fd, "\nTotal frames rendered: %d", summary.total_frames());
242 dprintf(fd, "\nJanky frames: %d (%.2f%%)", summary.janky_frames(),
John Reck1bcacfd2017-11-03 10:12:19 -0700243 (float)summary.janky_frames() / (float)summary.total_frames() * 100.0f);
John Reckdf1742e2017-01-19 15:56:21 -0800244 dprintf(fd, "\n50th percentile: %dms", findPercentile(proto, 50));
245 dprintf(fd, "\n90th percentile: %dms", findPercentile(proto, 90));
246 dprintf(fd, "\n95th percentile: %dms", findPercentile(proto, 95));
247 dprintf(fd, "\n99th percentile: %dms", findPercentile(proto, 99));
248 dprintf(fd, "\nNumber Missed Vsync: %d", summary.missed_vsync_count());
249 dprintf(fd, "\nNumber High input latency: %d", summary.high_input_latency_count());
250 dprintf(fd, "\nNumber Slow UI thread: %d", summary.slow_ui_thread_count());
251 dprintf(fd, "\nNumber Slow bitmap uploads: %d", summary.slow_bitmap_upload_count());
252 dprintf(fd, "\nNumber Slow issue draw commands: %d", summary.slow_draw_count());
John Reck0e486472018-03-19 14:06:16 -0700253 dprintf(fd, "\nNumber Frame deadline missed: %d", summary.missed_deadline_count());
John Reckdf1742e2017-01-19 15:56:21 -0800254 dprintf(fd, "\nHISTOGRAM:");
255 for (const auto& it : proto->histogram()) {
256 dprintf(fd, " %dms=%d", it.render_millis(), it.frame_count());
257 }
258 dprintf(fd, "\n");
259}
260
261void GraphicsStatsService::saveBuffer(const std::string& path, const std::string& package,
Dianne Hackborn73453e42017-12-11 16:30:36 -0800262 int64_t versionCode, int64_t startTime, int64_t endTime,
John Reck1bcacfd2017-11-03 10:12:19 -0700263 const ProfileData* data) {
Kweku Adams1856a4c2018-04-03 16:31:10 -0700264 protos::GraphicsStatsProto statsProto;
John Reckdf1742e2017-01-19 15:56:21 -0800265 if (!parseFromFile(path, &statsProto)) {
266 statsProto.Clear();
267 }
John Reck5206a872017-09-18 11:08:31 -0700268 if (!mergeProfileDataIntoProto(&statsProto, package, versionCode, startTime, endTime, data)) {
269 return;
270 }
John Reckdf1742e2017-01-19 15:56:21 -0800271 // Although we might not have read any data from the file, merging the existing data
272 // should always fully-initialize the proto
John Reck5206a872017-09-18 11:08:31 -0700273 if (!statsProto.IsInitialized()) {
274 ALOGE("proto initialization error %s", statsProto.InitializationErrorString().c_str());
275 return;
276 }
277 if (statsProto.package_name().empty() || !statsProto.has_summary()) {
John Reck1bcacfd2017-11-03 10:12:19 -0700278 ALOGE("missing package_name() '%s' summary %d", statsProto.package_name().c_str(),
279 statsProto.has_summary());
John Reck5206a872017-09-18 11:08:31 -0700280 return;
281 }
John Reckdf1742e2017-01-19 15:56:21 -0800282 int outFd = open(path.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0660);
283 if (outFd <= 0) {
284 int err = errno;
285 ALOGW("Failed to open '%s', error=%d (%s)", path.c_str(), err, strerror(err));
286 return;
287 }
288 int wrote = write(outFd, &sCurrentFileVersion, sHeaderSize);
289 if (wrote != sHeaderSize) {
290 int err = errno;
John Reck1bcacfd2017-11-03 10:12:19 -0700291 ALOGW("Failed to write header to '%s', returned=%d errno=%d (%s)", path.c_str(), wrote, err,
292 strerror(err));
John Reckdf1742e2017-01-19 15:56:21 -0800293 close(outFd);
294 return;
295 }
296 {
John Reck915883b2017-05-03 10:27:20 -0700297 FileOutputStreamLite output(outFd);
John Reckdf1742e2017-01-19 15:56:21 -0800298 bool success = statsProto.SerializeToZeroCopyStream(&output) && output.Flush();
299 if (output.GetErrno() != 0) {
John Reck1bcacfd2017-11-03 10:12:19 -0700300 ALOGW("Error writing to fd=%d, path='%s' err=%d (%s)", outFd, path.c_str(),
301 output.GetErrno(), strerror(output.GetErrno()));
John Reckdf1742e2017-01-19 15:56:21 -0800302 success = false;
303 } else if (!success) {
304 ALOGW("Serialize failed on '%s' unknown error", path.c_str());
305 }
306 }
307 close(outFd);
308}
309
310class GraphicsStatsService::Dump {
311public:
312 Dump(int outFd, DumpType type) : mFd(outFd), mType(type) {}
313 int fd() { return mFd; }
314 DumpType type() { return mType; }
Kweku Adams1856a4c2018-04-03 16:31:10 -0700315 protos::GraphicsStatsServiceDumpProto& proto() { return mProto; }
John Reck1bcacfd2017-11-03 10:12:19 -0700316
John Reckdf1742e2017-01-19 15:56:21 -0800317private:
318 int mFd;
319 DumpType mType;
Kweku Adams1856a4c2018-04-03 16:31:10 -0700320 protos::GraphicsStatsServiceDumpProto mProto;
John Reckdf1742e2017-01-19 15:56:21 -0800321};
322
323GraphicsStatsService::Dump* GraphicsStatsService::createDump(int outFd, DumpType type) {
324 return new Dump(outFd, type);
325}
326
John Reck1bcacfd2017-11-03 10:12:19 -0700327void GraphicsStatsService::addToDump(Dump* dump, const std::string& path,
Dianne Hackborn73453e42017-12-11 16:30:36 -0800328 const std::string& package, int64_t versionCode,
329 int64_t startTime, int64_t endTime, const ProfileData* data) {
Kweku Adams1856a4c2018-04-03 16:31:10 -0700330 protos::GraphicsStatsProto statsProto;
John Reckdf1742e2017-01-19 15:56:21 -0800331 if (!path.empty() && !parseFromFile(path, &statsProto)) {
332 statsProto.Clear();
333 }
John Reck1bcacfd2017-11-03 10:12:19 -0700334 if (data &&
335 !mergeProfileDataIntoProto(&statsProto, package, versionCode, startTime, endTime, data)) {
John Reck5206a872017-09-18 11:08:31 -0700336 return;
John Reckdf1742e2017-01-19 15:56:21 -0800337 }
338 if (!statsProto.IsInitialized()) {
339 ALOGW("Failed to load profile data from path '%s' and data %p",
John Reck1bcacfd2017-11-03 10:12:19 -0700340 path.empty() ? "<empty>" : path.c_str(), data);
John Reckdf1742e2017-01-19 15:56:21 -0800341 return;
342 }
343
344 if (dump->type() == DumpType::Protobuf) {
345 dump->proto().add_stats()->CopyFrom(statsProto);
346 } else {
347 dumpAsTextToFd(&statsProto, dump->fd());
348 }
349}
350
351void GraphicsStatsService::addToDump(Dump* dump, const std::string& path) {
Kweku Adams1856a4c2018-04-03 16:31:10 -0700352 protos::GraphicsStatsProto statsProto;
John Reckdf1742e2017-01-19 15:56:21 -0800353 if (!parseFromFile(path, &statsProto)) {
354 return;
355 }
356 if (dump->type() == DumpType::Protobuf) {
357 dump->proto().add_stats()->CopyFrom(statsProto);
358 } else {
359 dumpAsTextToFd(&statsProto, dump->fd());
360 }
361}
362
363void GraphicsStatsService::finishDump(Dump* dump) {
364 if (dump->type() == DumpType::Protobuf) {
John Reck915883b2017-05-03 10:27:20 -0700365 FileOutputStreamLite stream(dump->fd());
John Reckdf1742e2017-01-19 15:56:21 -0800366 dump->proto().SerializeToZeroCopyStream(&stream);
367 }
368 delete dump;
369}
370
371} /* namespace uirenderer */
Dan Albert110e0072017-10-11 12:41:26 -0700372} /* namespace android */