blob: c8c719593909dc66ea208e12f80ebcb1234762f8 [file] [log] [blame]
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001/*
2 * Copyright (C) 2013 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.
Sanna Catherine de Treville Wager14316442017-08-11 10:45:29 -070015 *
16 *
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080017 */
18
19#define LOG_TAG "NBLog"
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080020
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070021#include <algorithm>
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -080022#include <climits>
Sanna Catherine de Treville Wager201079a2017-05-18 16:36:29 -070023#include <deque>
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070024#include <fstream>
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070025#include <iostream>
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -070026#include <math.h>
Sanna Catherine de Treville Wager201079a2017-05-18 16:36:29 -070027#include <numeric>
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070028#include <vector>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080029#include <stdarg.h>
30#include <stdint.h>
31#include <stdio.h>
32#include <string.h>
Nicolas Rouletc20cb502017-02-01 12:35:24 -080033#include <sys/prctl.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080034#include <time.h>
35#include <new>
Glenn Kasten535e1612016-12-05 12:19:36 -080036#include <audio_utils/roundup.h>
Glenn Kasten8589ce72017-09-08 17:03:42 -070037#include <media/nblog/NBLog.h>
38#include <media/nblog/PerformanceAnalysis.h>
39#include <media/nblog/ReportPerformance.h>
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -070040#include <utils/CallStack.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080041#include <utils/Log.h>
Glenn Kasten4e01ef62013-07-11 14:29:59 -070042#include <utils/String8.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080043
Nicolas Roulet40a44982017-02-03 13:39:57 -080044#include <queue>
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070045#include <utility>
Nicolas Roulet40a44982017-02-03 13:39:57 -080046
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080047namespace android {
48
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070049int NBLog::Entry::copyEntryDataAt(size_t offset) const
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080050{
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070051 // FIXME This is too slow
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080052 if (offset == 0)
53 return mEvent;
54 else if (offset == 1)
55 return mLength;
56 else if (offset < (size_t) (mLength + 2))
57 return ((char *) mData)[offset - 2];
58 else if (offset == (size_t) (mLength + 2))
59 return mLength;
60 else
61 return 0;
62}
63
64// ---------------------------------------------------------------------------
65
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070066/*static*/
67std::unique_ptr<NBLog::AbstractEntry> NBLog::AbstractEntry::buildEntry(const uint8_t *ptr) {
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -070068 const uint8_t type = EntryIterator(ptr)->type;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070069 switch (type) {
70 case EVENT_START_FMT:
71 return std::make_unique<FormatEntry>(FormatEntry(ptr));
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -070072 case EVENT_AUDIO_STATE:
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070073 case EVENT_HISTOGRAM_ENTRY_TS:
74 return std::make_unique<HistogramEntry>(HistogramEntry(ptr));
75 default:
76 ALOGW("Tried to create AbstractEntry of type %d", type);
77 return nullptr;
78 }
Nicolas Roulet40a44982017-02-03 13:39:57 -080079}
80
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070081NBLog::AbstractEntry::AbstractEntry(const uint8_t *entry) : mEntry(entry) {
82}
83
84// ---------------------------------------------------------------------------
Nicolas Rouletcd5dd012017-02-13 12:09:28 -080085
Sanna Catherine de Treville Wagerdd92d7e2017-05-15 14:56:53 -070086NBLog::EntryIterator NBLog::FormatEntry::begin() const {
87 return EntryIterator(mEntry);
88}
89
Nicolas Roulet40a44982017-02-03 13:39:57 -080090const char *NBLog::FormatEntry::formatString() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -080091 return (const char*) mEntry + offsetof(entry, data);
Nicolas Roulet40a44982017-02-03 13:39:57 -080092}
93
94size_t NBLog::FormatEntry::formatStringLength() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -080095 return mEntry[offsetof(entry, length)];
Nicolas Roulet40a44982017-02-03 13:39:57 -080096}
97
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070098NBLog::EntryIterator NBLog::FormatEntry::args() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -080099 auto it = begin();
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700100 // skip start fmt
101 ++it;
102 // skip timestamp
103 ++it;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700104 // skip hash
105 ++it;
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700106 // Skip author if present
107 if (it->type == EVENT_AUTHOR) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800108 ++it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800109 }
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700110 return it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800111}
112
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700113int64_t NBLog::FormatEntry::timestamp() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800114 auto it = begin();
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700115 // skip start fmt
116 ++it;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700117 return it.payload<int64_t>();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800118}
119
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700120NBLog::log_hash_t NBLog::FormatEntry::hash() const {
121 auto it = begin();
122 // skip start fmt
123 ++it;
124 // skip timestamp
125 ++it;
126 // unaligned 64-bit read not supported
127 log_hash_t hash;
128 memcpy(&hash, it->data, sizeof(hash));
129 return hash;
130}
131
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700132int NBLog::FormatEntry::author() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800133 auto it = begin();
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700134 // skip start fmt
135 ++it;
136 // skip timestamp
137 ++it;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700138 // skip hash
139 ++it;
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700140 // if there is an author entry, return it, return -1 otherwise
141 if (it->type == EVENT_AUTHOR) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800142 return it.payload<int>();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800143 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800144 return -1;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800145}
146
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700147NBLog::EntryIterator NBLog::FormatEntry::copyWithAuthor(
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800148 std::unique_ptr<audio_utils_fifo_writer> &dst, int author) const {
149 auto it = begin();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800150 // copy fmt start entry
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800151 it.copyTo(dst);
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700152 // copy timestamp
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700153 (++it).copyTo(dst); // copy hash
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700154 (++it).copyTo(dst);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800155 // insert author entry
156 size_t authorEntrySize = NBLog::Entry::kOverhead + sizeof(author);
157 uint8_t authorEntry[authorEntrySize];
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800158 authorEntry[offsetof(entry, type)] = EVENT_AUTHOR;
159 authorEntry[offsetof(entry, length)] =
160 authorEntry[authorEntrySize + NBLog::Entry::kPreviousLengthOffset] =
161 sizeof(author);
162 *(int*) (&authorEntry[offsetof(entry, data)]) = author;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800163 dst->write(authorEntry, authorEntrySize);
164 // copy rest of entries
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800165 while ((++it)->type != EVENT_END_FMT) {
166 it.copyTo(dst);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800167 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800168 it.copyTo(dst);
169 ++it;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800170 return it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800171}
172
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700173void NBLog::EntryIterator::copyTo(std::unique_ptr<audio_utils_fifo_writer> &dst) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800174 size_t length = ptr[offsetof(entry, length)] + NBLog::Entry::kOverhead;
175 dst->write(ptr, length);
176}
Nicolas Roulet40a44982017-02-03 13:39:57 -0800177
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700178void NBLog::EntryIterator::copyData(uint8_t *dst) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800179 memcpy((void*) dst, ptr + offsetof(entry, data), ptr[offsetof(entry, length)]);
180}
181
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700182NBLog::EntryIterator::EntryIterator()
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800183 : ptr(nullptr) {}
184
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700185NBLog::EntryIterator::EntryIterator(const uint8_t *entry)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800186 : ptr(entry) {}
187
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700188NBLog::EntryIterator::EntryIterator(const NBLog::EntryIterator &other)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800189 : ptr(other.ptr) {}
190
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700191const NBLog::entry& NBLog::EntryIterator::operator*() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800192 return *(entry*) ptr;
193}
194
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700195const NBLog::entry* NBLog::EntryIterator::operator->() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800196 return (entry*) ptr;
197}
198
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700199NBLog::EntryIterator& NBLog::EntryIterator::operator++() {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800200 ptr += ptr[offsetof(entry, length)] + NBLog::Entry::kOverhead;
201 return *this;
202}
203
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700204NBLog::EntryIterator& NBLog::EntryIterator::operator--() {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800205 ptr -= ptr[NBLog::Entry::kPreviousLengthOffset] + NBLog::Entry::kOverhead;
206 return *this;
207}
208
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700209NBLog::EntryIterator NBLog::EntryIterator::next() const {
210 EntryIterator aux(*this);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800211 return ++aux;
212}
213
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700214NBLog::EntryIterator NBLog::EntryIterator::prev() const {
215 EntryIterator aux(*this);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800216 return --aux;
217}
218
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700219int NBLog::EntryIterator::operator-(const NBLog::EntryIterator &other) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800220 return ptr - other.ptr;
221}
222
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700223bool NBLog::EntryIterator::operator!=(const EntryIterator &other) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800224 return ptr != other.ptr;
225}
226
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700227bool NBLog::EntryIterator::hasConsistentLength() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800228 return ptr[offsetof(entry, length)] == ptr[ptr[offsetof(entry, length)] +
229 NBLog::Entry::kOverhead + NBLog::Entry::kPreviousLengthOffset];
Nicolas Roulet40a44982017-02-03 13:39:57 -0800230}
231
232// ---------------------------------------------------------------------------
233
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700234int64_t NBLog::HistogramEntry::timestamp() const {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700235 return EntryIterator(mEntry).payload<HistTsEntry>().ts;
236}
237
238NBLog::log_hash_t NBLog::HistogramEntry::hash() const {
239 return EntryIterator(mEntry).payload<HistTsEntry>().hash;
240}
241
242int NBLog::HistogramEntry::author() const {
243 EntryIterator it(mEntry);
244 if (it->length == sizeof(HistTsEntryWithAuthor)) {
245 return it.payload<HistTsEntryWithAuthor>().author;
246 } else {
247 return -1;
248 }
249}
250
251NBLog::EntryIterator NBLog::HistogramEntry::copyWithAuthor(
252 std::unique_ptr<audio_utils_fifo_writer> &dst, int author) const {
253 // Current histogram entry has {type, length, struct HistTsEntry, length}.
254 // We now want {type, length, struct HistTsEntryWithAuthor, length}
255 uint8_t buffer[Entry::kOverhead + sizeof(HistTsEntryWithAuthor)];
256 // Copy content until the point we want to add the author
257 memcpy(buffer, mEntry, sizeof(entry) + sizeof(HistTsEntry));
258 // Copy the author
259 *(int*) (buffer + sizeof(entry) + sizeof(HistTsEntry)) = author;
260 // Update lengths
261 buffer[offsetof(entry, length)] = sizeof(HistTsEntryWithAuthor);
262 buffer[sizeof(buffer) + Entry::kPreviousLengthOffset] = sizeof(HistTsEntryWithAuthor);
263 // Write new buffer into FIFO
264 dst->write(buffer, sizeof(buffer));
265 return EntryIterator(mEntry).next();
266}
267
268// ---------------------------------------------------------------------------
269
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800270#if 0 // FIXME see note in NBLog.h
271NBLog::Timeline::Timeline(size_t size, void *shared)
272 : mSize(roundup(size)), mOwn(shared == NULL),
273 mShared((Shared *) (mOwn ? new char[sharedSize(size)] : shared))
274{
275 new (mShared) Shared;
276}
277
278NBLog::Timeline::~Timeline()
279{
280 mShared->~Shared();
281 if (mOwn) {
282 delete[] (char *) mShared;
283 }
284}
285#endif
286
287/*static*/
288size_t NBLog::Timeline::sharedSize(size_t size)
289{
Glenn Kastened99c2b2016-12-12 08:31:24 -0800290 // TODO fifo now supports non-power-of-2 buffer sizes, so could remove the roundup
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800291 return sizeof(Shared) + roundup(size);
292}
293
294// ---------------------------------------------------------------------------
295
296NBLog::Writer::Writer()
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800297 : mShared(NULL), mFifo(NULL), mFifoWriter(NULL), mEnabled(false), mPidTag(NULL), mPidTagSize(0)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800298{
299}
300
Glenn Kasten535e1612016-12-05 12:19:36 -0800301NBLog::Writer::Writer(void *shared, size_t size)
302 : mShared((Shared *) shared),
303 mFifo(mShared != NULL ?
304 new audio_utils_fifo(size, sizeof(uint8_t),
305 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
306 mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL),
307 mEnabled(mFifoWriter != NULL)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800308{
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800309 // caching pid and process name
310 pid_t id = ::getpid();
311 char procName[16];
312 int status = prctl(PR_GET_NAME, procName);
313 if (status) { // error getting process name
314 procName[0] = '\0';
315 }
316 size_t length = strlen(procName);
317 mPidTagSize = length + sizeof(pid_t);
318 mPidTag = new char[mPidTagSize];
319 memcpy(mPidTag, &id, sizeof(pid_t));
320 memcpy(mPidTag + sizeof(pid_t), procName, length);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800321}
322
Glenn Kasten535e1612016-12-05 12:19:36 -0800323NBLog::Writer::Writer(const sp<IMemory>& iMemory, size_t size)
324 : Writer(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800325{
Glenn Kasten535e1612016-12-05 12:19:36 -0800326 mIMemory = iMemory;
327}
328
329NBLog::Writer::~Writer()
330{
331 delete mFifoWriter;
332 delete mFifo;
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800333 delete[] mPidTag;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800334}
335
336void NBLog::Writer::log(const char *string)
337{
338 if (!mEnabled) {
339 return;
340 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800341 LOG_ALWAYS_FATAL_IF(string == NULL, "Attempted to log NULL string");
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800342 size_t length = strlen(string);
Glenn Kasten535e1612016-12-05 12:19:36 -0800343 if (length > Entry::kMaxLength) {
344 length = Entry::kMaxLength;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800345 }
346 log(EVENT_STRING, string, length);
347}
348
349void NBLog::Writer::logf(const char *fmt, ...)
350{
351 if (!mEnabled) {
352 return;
353 }
354 va_list ap;
355 va_start(ap, fmt);
356 Writer::logvf(fmt, ap); // the Writer:: is needed to avoid virtual dispatch for LockedWriter
357 va_end(ap);
358}
359
360void NBLog::Writer::logvf(const char *fmt, va_list ap)
361{
362 if (!mEnabled) {
363 return;
364 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800365 char buffer[Entry::kMaxLength + 1 /*NUL*/];
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800366 int length = vsnprintf(buffer, sizeof(buffer), fmt, ap);
367 if (length >= (int) sizeof(buffer)) {
368 length = sizeof(buffer) - 1;
369 // NUL termination is not required
370 // buffer[length] = '\0';
371 }
372 if (length >= 0) {
373 log(EVENT_STRING, buffer, length);
374 }
375}
376
377void NBLog::Writer::logTimestamp()
378{
379 if (!mEnabled) {
380 return;
381 }
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700382 int64_t ts = get_monotonic_ns();
383 if (ts > 0) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800384 log(EVENT_TIMESTAMP, &ts, sizeof(ts));
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700385 } else {
386 ALOGE("Failed to get timestamp");
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800387 }
388}
389
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700390void NBLog::Writer::logTimestamp(const int64_t ts)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800391{
392 if (!mEnabled) {
393 return;
394 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800395 log(EVENT_TIMESTAMP, &ts, sizeof(ts));
396}
397
398void NBLog::Writer::logInteger(const int x)
399{
400 if (!mEnabled) {
401 return;
402 }
403 log(EVENT_INTEGER, &x, sizeof(x));
404}
405
406void NBLog::Writer::logFloat(const float x)
407{
408 if (!mEnabled) {
409 return;
410 }
411 log(EVENT_FLOAT, &x, sizeof(x));
412}
413
414void NBLog::Writer::logPID()
415{
416 if (!mEnabled) {
417 return;
418 }
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800419 log(EVENT_PID, mPidTag, mPidTagSize);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800420}
421
422void NBLog::Writer::logStart(const char *fmt)
423{
424 if (!mEnabled) {
425 return;
426 }
427 size_t length = strlen(fmt);
428 if (length > Entry::kMaxLength) {
429 length = Entry::kMaxLength;
430 }
431 log(EVENT_START_FMT, fmt, length);
432}
433
434void NBLog::Writer::logEnd()
435{
436 if (!mEnabled) {
437 return;
438 }
439 Entry entry = Entry(EVENT_END_FMT, NULL, 0);
440 log(&entry, true);
441}
442
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700443void NBLog::Writer::logHash(log_hash_t hash)
444{
445 if (!mEnabled) {
446 return;
447 }
448 log(EVENT_HASH, &hash, sizeof(hash));
449}
450
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700451void NBLog::Writer::logEventHistTs(Event event, log_hash_t hash)
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700452{
453 if (!mEnabled) {
454 return;
455 }
456 HistTsEntry data;
457 data.hash = hash;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700458 data.ts = get_monotonic_ns();
459 if (data.ts > 0) {
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700460 log(event, &data, sizeof(data));
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700461 } else {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700462 ALOGE("Failed to get timestamp");
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700463 }
464}
465
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700466void NBLog::Writer::logFormat(const char *fmt, log_hash_t hash, ...)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800467{
468 if (!mEnabled) {
469 return;
470 }
471
472 va_list ap;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700473 va_start(ap, hash);
474 Writer::logVFormat(fmt, hash, ap);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800475 va_end(ap);
476}
477
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700478void NBLog::Writer::logVFormat(const char *fmt, log_hash_t hash, va_list argp)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800479{
480 if (!mEnabled) {
481 return;
482 }
483 Writer::logStart(fmt);
484 int i;
485 double f;
486 char* s;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700487 int64_t t;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800488 Writer::logTimestamp();
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700489 Writer::logHash(hash);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800490 for (const char *p = fmt; *p != '\0'; p++) {
491 // TODO: implement more complex formatting such as %.3f
492 if (*p != '%') {
493 continue;
494 }
495 switch(*++p) {
496 case 's': // string
497 s = va_arg(argp, char *);
498 Writer::log(s);
499 break;
500
501 case 't': // timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700502 t = va_arg(argp, int64_t);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800503 Writer::logTimestamp(t);
504 break;
505
506 case 'd': // integer
507 i = va_arg(argp, int);
508 Writer::logInteger(i);
509 break;
510
511 case 'f': // float
512 f = va_arg(argp, double); // float arguments are promoted to double in vararg lists
513 Writer::logFloat((float)f);
514 break;
515
516 case 'p': // pid
517 Writer::logPID();
518 break;
519
520 // the "%\0" case finishes parsing
521 case '\0':
522 --p;
523 break;
524
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800525 case '%':
526 break;
527
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800528 default:
529 ALOGW("NBLog Writer parsed invalid format specifier: %c", *p);
530 break;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800531 }
532 }
533 Writer::logEnd();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800534}
535
536void NBLog::Writer::log(Event event, const void *data, size_t length)
537{
538 if (!mEnabled) {
539 return;
540 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800541 if (data == NULL || length > Entry::kMaxLength) {
542 // TODO Perhaps it makes sense to display truncated data or at least a
543 // message that the data is too long? The current behavior can create
544 // a confusion for a programmer debugging their code.
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800545 return;
546 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700547 // Ignore if invalid event
548 if (event == EVENT_RESERVED || event >= EVENT_UPPER_BOUND) {
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800549 return;
550 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700551 Entry etr(event, data, length);
552 log(&etr, true /*trusted*/);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800553}
554
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700555void NBLog::Writer::log(const NBLog::Entry *etr, bool trusted)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800556{
557 if (!mEnabled) {
558 return;
559 }
560 if (!trusted) {
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700561 log(etr->mEvent, etr->mData, etr->mLength);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800562 return;
563 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700564 size_t need = etr->mLength + Entry::kOverhead; // mEvent, mLength, data[mLength], mLength
565 // need = number of bytes written to FIFO
Glenn Kasten535e1612016-12-05 12:19:36 -0800566
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800567 // FIXME optimize this using memcpy for the data part of the Entry.
568 // The Entry could have a method copyTo(ptr, offset, size) to optimize the copy.
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700569 // checks size of a single log Entry: type, length, data pointer and ending
Glenn Kasten535e1612016-12-05 12:19:36 -0800570 uint8_t temp[Entry::kMaxLength + Entry::kOverhead];
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700571 // write this data to temp array
Glenn Kasten535e1612016-12-05 12:19:36 -0800572 for (size_t i = 0; i < need; i++) {
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700573 temp[i] = etr->copyEntryDataAt(i);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800574 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700575 // write to circular buffer
Glenn Kasten535e1612016-12-05 12:19:36 -0800576 mFifoWriter->write(temp, need);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800577}
578
579bool NBLog::Writer::isEnabled() const
580{
581 return mEnabled;
582}
583
584bool NBLog::Writer::setEnabled(bool enabled)
585{
586 bool old = mEnabled;
587 mEnabled = enabled && mShared != NULL;
588 return old;
589}
590
591// ---------------------------------------------------------------------------
592
593NBLog::LockedWriter::LockedWriter()
594 : Writer()
595{
596}
597
Glenn Kasten535e1612016-12-05 12:19:36 -0800598NBLog::LockedWriter::LockedWriter(void *shared, size_t size)
599 : Writer(shared, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800600{
601}
602
603void NBLog::LockedWriter::log(const char *string)
604{
605 Mutex::Autolock _l(mLock);
606 Writer::log(string);
607}
608
609void NBLog::LockedWriter::logf(const char *fmt, ...)
610{
611 // FIXME should not take the lock until after formatting is done
612 Mutex::Autolock _l(mLock);
613 va_list ap;
614 va_start(ap, fmt);
615 Writer::logvf(fmt, ap);
616 va_end(ap);
617}
618
619void NBLog::LockedWriter::logvf(const char *fmt, va_list ap)
620{
621 // FIXME should not take the lock until after formatting is done
622 Mutex::Autolock _l(mLock);
623 Writer::logvf(fmt, ap);
624}
625
626void NBLog::LockedWriter::logTimestamp()
627{
628 // FIXME should not take the lock until after the clock_gettime() syscall
629 Mutex::Autolock _l(mLock);
630 Writer::logTimestamp();
631}
632
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700633void NBLog::LockedWriter::logTimestamp(const int64_t ts)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800634{
635 Mutex::Autolock _l(mLock);
636 Writer::logTimestamp(ts);
637}
638
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800639void NBLog::LockedWriter::logInteger(const int x)
640{
641 Mutex::Autolock _l(mLock);
642 Writer::logInteger(x);
643}
644
645void NBLog::LockedWriter::logFloat(const float x)
646{
647 Mutex::Autolock _l(mLock);
648 Writer::logFloat(x);
649}
650
651void NBLog::LockedWriter::logPID()
652{
653 Mutex::Autolock _l(mLock);
654 Writer::logPID();
655}
656
657void NBLog::LockedWriter::logStart(const char *fmt)
658{
659 Mutex::Autolock _l(mLock);
660 Writer::logStart(fmt);
661}
662
663
664void NBLog::LockedWriter::logEnd()
665{
666 Mutex::Autolock _l(mLock);
667 Writer::logEnd();
668}
669
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700670void NBLog::LockedWriter::logHash(log_hash_t hash)
671{
672 Mutex::Autolock _l(mLock);
673 Writer::logHash(hash);
674}
675
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800676bool NBLog::LockedWriter::isEnabled() const
677{
678 Mutex::Autolock _l(mLock);
679 return Writer::isEnabled();
680}
681
682bool NBLog::LockedWriter::setEnabled(bool enabled)
683{
684 Mutex::Autolock _l(mLock);
685 return Writer::setEnabled(enabled);
686}
687
688// ---------------------------------------------------------------------------
689
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700690const std::set<NBLog::Event> NBLog::Reader::startingTypes {NBLog::Event::EVENT_START_FMT,
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700691 NBLog::Event::EVENT_HISTOGRAM_ENTRY_TS,
692 NBLog::Event::EVENT_AUDIO_STATE};
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700693const std::set<NBLog::Event> NBLog::Reader::endingTypes {NBLog::Event::EVENT_END_FMT,
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700694 NBLog::Event::EVENT_HISTOGRAM_ENTRY_TS,
695 NBLog::Event::EVENT_AUDIO_STATE};
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700696
Glenn Kasten535e1612016-12-05 12:19:36 -0800697NBLog::Reader::Reader(const void *shared, size_t size)
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700698 : mFd(-1), mIndent(0), mLost(0),
699 mShared((/*const*/ Shared *) shared), /*mIMemory*/
Glenn Kasten535e1612016-12-05 12:19:36 -0800700 mFifo(mShared != NULL ?
701 new audio_utils_fifo(size, sizeof(uint8_t),
702 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700703 mFifoReader(mFifo != NULL ? new audio_utils_fifo_reader(*mFifo) : NULL)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800704{
705}
706
Glenn Kasten535e1612016-12-05 12:19:36 -0800707NBLog::Reader::Reader(const sp<IMemory>& iMemory, size_t size)
708 : Reader(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800709{
Glenn Kasten535e1612016-12-05 12:19:36 -0800710 mIMemory = iMemory;
711}
712
713NBLog::Reader::~Reader()
714{
715 delete mFifoReader;
716 delete mFifo;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800717}
718
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700719const uint8_t *NBLog::Reader::findLastEntryOfTypes(const uint8_t *front, const uint8_t *back,
720 const std::set<Event> &types) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800721 while (back + Entry::kPreviousLengthOffset >= front) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700722 const uint8_t *prev = back - back[Entry::kPreviousLengthOffset] - Entry::kOverhead;
723 if (prev < front || prev + prev[offsetof(entry, length)] +
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800724 Entry::kOverhead != back) {
725
726 // prev points to an out of limits or inconsistent entry
727 return nullptr;
728 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700729 if (types.find((const Event) prev[offsetof(entry, type)]) != types.end()) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800730 return prev;
731 }
732 back = prev;
733 }
734 return nullptr; // no entry found
735}
736
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700737// Copies content of a Reader FIFO into its Snapshot
738// The Snapshot has the same raw data, but represented as a sequence of entries
739// and an EntryIterator making it possible to process the data.
Nicolas Roulet40a44982017-02-03 13:39:57 -0800740std::unique_ptr<NBLog::Reader::Snapshot> NBLog::Reader::getSnapshot()
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800741{
Glenn Kasten535e1612016-12-05 12:19:36 -0800742 if (mFifoReader == NULL) {
Nicolas Roulet40a44982017-02-03 13:39:57 -0800743 return std::unique_ptr<NBLog::Reader::Snapshot>(new Snapshot());
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800744 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800745 // make a copy to avoid race condition with writer
Glenn Kasten535e1612016-12-05 12:19:36 -0800746 size_t capacity = mFifo->capacity();
747
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800748 // This emulates the behaviour of audio_utils_fifo_reader::read, but without incrementing the
749 // reader index. The index is incremented after handling corruption, to after the last complete
750 // entry of the buffer
751 size_t lost;
752 audio_utils_iovec iovec[2];
753 ssize_t availToRead = mFifoReader->obtain(iovec, capacity, NULL /*timeout*/, &lost);
754 if (availToRead <= 0) {
755 return std::unique_ptr<NBLog::Reader::Snapshot>(new Snapshot());
756 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800757
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800758 std::unique_ptr<Snapshot> snapshot(new Snapshot(availToRead));
759 memcpy(snapshot->mData, (const char *) mFifo->buffer() + iovec[0].mOffset, iovec[0].mLength);
760 if (iovec[1].mLength > 0) {
761 memcpy(snapshot->mData + (iovec[0].mLength),
762 (const char *) mFifo->buffer() + iovec[1].mOffset, iovec[1].mLength);
763 }
764
765 // Handle corrupted buffer
766 // Potentially, a buffer has corrupted data on both beginning (due to overflow) and end
767 // (due to incomplete format entry). But even if the end format entry is incomplete,
768 // it ends in a complete entry (which is not an END_FMT). So is safe to traverse backwards.
769 // TODO: handle client corruption (in the middle of a buffer)
770
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700771 const uint8_t *back = snapshot->mData + availToRead;
772 const uint8_t *front = snapshot->mData;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800773
774 // Find last END_FMT. <back> is sitting on an entry which might be the middle of a FormatEntry.
775 // We go backwards until we find an EVENT_END_FMT.
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700776 const uint8_t *lastEnd = findLastEntryOfTypes(front, back, endingTypes);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800777 if (lastEnd == nullptr) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700778 snapshot->mEnd = snapshot->mBegin = EntryIterator(front);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800779 } else {
780 // end of snapshot points to after last END_FMT entry
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700781 snapshot->mEnd = EntryIterator(lastEnd).next();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800782 // find first START_FMT
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700783 const uint8_t *firstStart = nullptr;
784 const uint8_t *firstStartTmp = snapshot->mEnd;
785 while ((firstStartTmp = findLastEntryOfTypes(front, firstStartTmp, startingTypes))
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800786 != nullptr) {
787 firstStart = firstStartTmp;
788 }
789 // firstStart is null if no START_FMT entry was found before lastEnd
790 if (firstStart == nullptr) {
791 snapshot->mBegin = snapshot->mEnd;
792 } else {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700793 snapshot->mBegin = EntryIterator(firstStart);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800794 }
795 }
796
797 // advance fifo reader index to after last entry read.
798 mFifoReader->release(snapshot->mEnd - front);
799
800 snapshot->mLost = lost;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800801 return snapshot;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800802
Nicolas Roulet40a44982017-02-03 13:39:57 -0800803}
804
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700805// Takes raw content of the local merger FIFO, processes log entries, and
806// writes the data to a map of class PerformanceAnalysis, based on their thread ID.
807void NBLog::MergeReader::getAndProcessSnapshot(NBLog::Reader::Snapshot &snapshot)
Nicolas Roulet40a44982017-02-03 13:39:57 -0800808{
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700809 String8 timestamp, body;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700810
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800811 for (auto entry = snapshot.begin(); entry != snapshot.end();) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800812 switch (entry->type) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800813 case EVENT_START_FMT:
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800814 entry = handleFormat(FormatEntry(entry), &timestamp, &body);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800815 break;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700816 case EVENT_HISTOGRAM_ENTRY_TS: {
817 HistTsEntryWithAuthor *data = (HistTsEntryWithAuthor *) (entry->data);
818 // TODO This memcpies are here to avoid unaligned memory access crash.
819 // There's probably a more efficient way to do it
820 log_hash_t hash;
821 memcpy(&hash, &(data->hash), sizeof(hash));
Nicolas Rouletad82aa62017-04-03 19:15:20 -0700822 int64_t ts;
823 memcpy(&ts, &data->ts, sizeof(ts));
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700824 // TODO: hash for histogram ts and audio state need to match
825 // and correspond to audio production source file location
826 mThreadPerformanceAnalysis[data->author][0 /*hash*/].logTsEntry(ts);
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700827 ++entry;
828 break;
829 }
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700830 case EVENT_AUDIO_STATE: {
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700831 HistTsEntryWithAuthor *data = (HistTsEntryWithAuthor *) (entry->data);
832 // TODO This memcpies are here to avoid unaligned memory access crash.
833 // There's probably a more efficient way to do it
Sanna Catherine de Treville Wagerd0965172017-07-24 13:42:44 -0700834 log_hash_t hash;
835 memcpy(&hash, &(data->hash), sizeof(hash));
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700836 // TODO: remove ts if unused
837 int64_t ts;
838 memcpy(&ts, &data->ts, sizeof(ts));
839 mThreadPerformanceAnalysis[data->author][0 /*hash*/].handleStateChange();
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700840 ++entry;
841 break;
842 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800843 case EVENT_END_FMT:
844 body.appendFormat("warning: got to end format event");
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800845 ++entry;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800846 break;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800847 case EVENT_RESERVED:
848 default:
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800849 body.appendFormat("warning: unexpected event %d", entry->type);
850 ++entry;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800851 break;
852 }
Sanna Catherine de Treville Wager9484bae2017-06-15 14:39:44 -0700853 }
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700854 // FIXME: decide whether to print the warnings here or elsewhere
Sanna Catherine de Treville Wager9484bae2017-06-15 14:39:44 -0700855 if (!body.isEmpty()) {
856 dumpLine(timestamp, body);
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700857 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800858}
859
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700860void NBLog::MergeReader::getAndProcessSnapshot()
Nicolas Roulet40a44982017-02-03 13:39:57 -0800861{
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700862 // get a snapshot, process it
Nicolas Roulet40a44982017-02-03 13:39:57 -0800863 std::unique_ptr<Snapshot> snap = getSnapshot();
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700864 getAndProcessSnapshot(*snap);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800865}
866
Sanna Catherine de Treville Wagercf6c75a2017-07-21 17:05:25 -0700867void NBLog::MergeReader::dump(int fd, int indent) {
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700868 // TODO: add a mutex around media.log dump
Sanna Catherine de Treville Wagercf6c75a2017-07-21 17:05:25 -0700869 ReportPerformance::dump(fd, indent, mThreadPerformanceAnalysis);
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700870}
871
872// Writes a string to the console
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800873void NBLog::Reader::dumpLine(const String8 &timestamp, String8 &body)
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700874{
875 if (mFd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700876 dprintf(mFd, "%.*s%s %s\n", mIndent, "", timestamp.string(), body.string());
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700877 } else {
878 ALOGI("%.*s%s %s", mIndent, "", timestamp.string(), body.string());
879 }
880 body.clear();
881}
882
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800883bool NBLog::Reader::isIMemory(const sp<IMemory>& iMemory) const
884{
Glenn Kasten481fb672013-09-30 14:39:28 -0700885 return iMemory != 0 && mIMemory != 0 && iMemory->pointer() == mIMemory->pointer();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800886}
887
Glenn Kasten1c446272017-04-07 09:49:07 -0700888// ---------------------------------------------------------------------------
889
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800890void NBLog::appendTimestamp(String8 *body, const void *data) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700891 int64_t ts;
892 memcpy(&ts, data, sizeof(ts));
893 body->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
894 (int) ((ts / (1000 * 1000)) % 1000));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800895}
896
897void NBLog::appendInt(String8 *body, const void *data) {
898 int x = *((int*) data);
899 body->appendFormat("<%d>", x);
900}
901
902void NBLog::appendFloat(String8 *body, const void *data) {
903 float f;
904 memcpy(&f, data, sizeof(float));
905 body->appendFormat("<%f>", f);
906}
907
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800908void NBLog::appendPID(String8 *body, const void* data, size_t length) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800909 pid_t id = *((pid_t*) data);
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800910 char * name = &((char*) data)[sizeof(pid_t)];
911 body->appendFormat("<PID: %d, name: %.*s>", id, (int) (length - sizeof(pid_t)), name);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800912}
913
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700914String8 NBLog::bufferDump(const uint8_t *buffer, size_t size)
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700915{
916 String8 str;
917 str.append("[ ");
918 for(size_t i = 0; i < size; i++)
919 {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700920 str.appendFormat("%d ", buffer[i]);
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700921 }
922 str.append("]");
923 return str;
924}
925
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700926String8 NBLog::bufferDump(const EntryIterator &it)
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700927{
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700928 return bufferDump(it, it->length + Entry::kOverhead);
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700929}
930
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700931NBLog::EntryIterator NBLog::Reader::handleFormat(const FormatEntry &fmtEntry,
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800932 String8 *timestamp,
933 String8 *body) {
Nicolas Roulet40a44982017-02-03 13:39:57 -0800934 // log timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700935 int64_t ts = fmtEntry.timestamp();
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800936 timestamp->clear();
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700937 timestamp->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
938 (int) ((ts / (1000 * 1000)) % 1000));
Nicolas Roulet40a44982017-02-03 13:39:57 -0800939
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700940 // log unique hash
941 log_hash_t hash = fmtEntry.hash();
942 // print only lower 16bit of hash as hex and line as int to reduce spam in the log
943 body->appendFormat("%.4X-%d ", (int)(hash >> 16) & 0xFFFF, (int) hash & 0xFFFF);
944
Nicolas Roulet40a44982017-02-03 13:39:57 -0800945 // log author (if present)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800946 handleAuthor(fmtEntry, body);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800947
948 // log string
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700949 NBLog::EntryIterator arg = fmtEntry.args();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800950
951 const char* fmt = fmtEntry.formatString();
952 size_t fmt_length = fmtEntry.formatStringLength();
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800953
954 for (size_t fmt_offset = 0; fmt_offset < fmt_length; ++fmt_offset) {
955 if (fmt[fmt_offset] != '%') {
956 body->append(&fmt[fmt_offset], 1); // TODO optimize to write consecutive strings at once
957 continue;
958 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800959 // case "%%""
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800960 if (fmt[++fmt_offset] == '%') {
961 body->append("%");
962 continue;
963 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800964 // case "%\0"
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800965 if (fmt_offset == fmt_length) {
966 continue;
967 }
968
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800969 NBLog::Event event = (NBLog::Event) arg->type;
970 size_t length = arg->length;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800971
972 // TODO check length for event type is correct
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800973
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800974 if (event == EVENT_END_FMT) {
975 break;
976 }
977
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800978 // TODO: implement more complex formatting such as %.3f
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800979 const uint8_t *datum = arg->data; // pointer to the current event args
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800980 switch(fmt[fmt_offset])
981 {
982 case 's': // string
Nicolas Roulet4da78202017-02-03 12:53:39 -0800983 ALOGW_IF(event != EVENT_STRING,
984 "NBLog Reader incompatible event for string specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800985 body->append((const char*) datum, length);
986 break;
987
988 case 't': // timestamp
Nicolas Roulet4da78202017-02-03 12:53:39 -0800989 ALOGW_IF(event != EVENT_TIMESTAMP,
990 "NBLog Reader incompatible event for timestamp specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800991 appendTimestamp(body, datum);
992 break;
993
994 case 'd': // integer
Nicolas Roulet4da78202017-02-03 12:53:39 -0800995 ALOGW_IF(event != EVENT_INTEGER,
996 "NBLog Reader incompatible event for integer specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800997 appendInt(body, datum);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800998 break;
999
1000 case 'f': // float
Nicolas Roulet4da78202017-02-03 12:53:39 -08001001 ALOGW_IF(event != EVENT_FLOAT,
1002 "NBLog Reader incompatible event for float specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001003 appendFloat(body, datum);
1004 break;
1005
1006 case 'p': // pid
Nicolas Roulet4da78202017-02-03 12:53:39 -08001007 ALOGW_IF(event != EVENT_PID,
1008 "NBLog Reader incompatible event for pid specifier: %d", event);
Nicolas Rouletc20cb502017-02-01 12:35:24 -08001009 appendPID(body, datum, length);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001010 break;
1011
1012 default:
1013 ALOGW("NBLog Reader encountered unknown character %c", fmt[fmt_offset]);
1014 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001015 ++arg;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001016 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001017 ALOGW_IF(arg->type != EVENT_END_FMT, "Expected end of format, got %d", arg->type);
1018 ++arg;
1019 return arg;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001020}
1021
Nicolas Roulet40a44982017-02-03 13:39:57 -08001022NBLog::Merger::Merger(const void *shared, size_t size):
Nicolas Roulet40a44982017-02-03 13:39:57 -08001023 mShared((Shared *) shared),
1024 mFifo(mShared != NULL ?
1025 new audio_utils_fifo(size, sizeof(uint8_t),
1026 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
1027 mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL)
1028 {}
1029
1030void NBLog::Merger::addReader(const NBLog::NamedReader &reader) {
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001031
Glenn Kasten1c446272017-04-07 09:49:07 -07001032 // FIXME This is called by binder thread in MediaLogService::registerWriter
1033 // but the access to shared variable mNamedReaders is not yet protected by a lock.
Nicolas Roulet40a44982017-02-03 13:39:57 -08001034 mNamedReaders.push_back(reader);
1035}
1036
1037// items placed in priority queue during merge
1038// composed by a timestamp and the index of the snapshot where the timestamp came from
1039struct MergeItem
1040{
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001041 int64_t ts;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001042 int index;
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001043 MergeItem(int64_t ts, int index): ts(ts), index(index) {}
Nicolas Roulet40a44982017-02-03 13:39:57 -08001044};
1045
1046// operators needed for priority queue in merge
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001047// bool operator>(const int64_t &t1, const int64_t &t2) {
1048// return t1.tv_sec > t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec);
1049// }
Nicolas Roulet40a44982017-02-03 13:39:57 -08001050
1051bool operator>(const struct MergeItem &i1, const struct MergeItem &i2) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001052 return i1.ts > i2.ts || (i1.ts == i2.ts && i1.index > i2.index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001053}
1054
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001055// Merge registered readers, sorted by timestamp, and write data to a single FIFO in local memory
Nicolas Roulet40a44982017-02-03 13:39:57 -08001056void NBLog::Merger::merge() {
Glenn Kasten1c446272017-04-07 09:49:07 -07001057 // FIXME This is called by merge thread
1058 // but the access to shared variable mNamedReaders is not yet protected by a lock.
Nicolas Roulet40a44982017-02-03 13:39:57 -08001059 int nLogs = mNamedReaders.size();
1060 std::vector<std::unique_ptr<NBLog::Reader::Snapshot>> snapshots(nLogs);
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001061 std::vector<NBLog::EntryIterator> offsets(nLogs);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001062 for (int i = 0; i < nLogs; ++i) {
1063 snapshots[i] = mNamedReaders[i].reader()->getSnapshot();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001064 offsets[i] = snapshots[i]->begin();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001065 }
1066 // initialize offsets
Nicolas Roulet40a44982017-02-03 13:39:57 -08001067 // TODO custom heap implementation could allow to update top, improving performance
1068 // for bursty buffers
1069 std::priority_queue<MergeItem, std::vector<MergeItem>, std::greater<MergeItem>> timestamps;
1070 for (int i = 0; i < nLogs; ++i)
1071 {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001072 if (offsets[i] != snapshots[i]->end()) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001073 int64_t ts = AbstractEntry::buildEntry(offsets[i])->timestamp();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001074 timestamps.emplace(ts, i);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001075 }
1076 }
1077
1078 while (!timestamps.empty()) {
1079 // find minimum timestamp
1080 int index = timestamps.top().index;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001081 // copy it to the log, increasing offset
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001082 offsets[index] = AbstractEntry::buildEntry(offsets[index])->copyWithAuthor(mFifoWriter,
1083 index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001084 // update data structures
Nicolas Roulet40a44982017-02-03 13:39:57 -08001085 timestamps.pop();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001086 if (offsets[index] != snapshots[index]->end()) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001087 int64_t ts = AbstractEntry::buildEntry(offsets[index])->timestamp();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001088 timestamps.emplace(ts, index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001089 }
1090 }
1091}
1092
Glenn Kasten1c446272017-04-07 09:49:07 -07001093const std::vector<NBLog::NamedReader>& NBLog::Merger::getNamedReaders() const {
1094 // FIXME This is returning a reference to a shared variable that needs a lock
1095 return mNamedReaders;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001096}
1097
Glenn Kasten1c446272017-04-07 09:49:07 -07001098// ---------------------------------------------------------------------------
1099
Nicolas Roulet40a44982017-02-03 13:39:57 -08001100NBLog::MergeReader::MergeReader(const void *shared, size_t size, Merger &merger)
1101 : Reader(shared, size), mNamedReaders(merger.getNamedReaders()) {}
1102
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001103void NBLog::MergeReader::handleAuthor(const NBLog::AbstractEntry &entry, String8 *body) {
1104 int author = entry.author();
Glenn Kasten1c446272017-04-07 09:49:07 -07001105 // FIXME Needs a lock
1106 const char* name = mNamedReaders[author].name();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001107 body->appendFormat("%s: ", name);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001108}
1109
Glenn Kasten1c446272017-04-07 09:49:07 -07001110// ---------------------------------------------------------------------------
1111
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001112NBLog::MergeThread::MergeThread(NBLog::Merger &merger, NBLog::MergeReader &mergeReader)
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001113 : mMerger(merger),
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001114 mMergeReader(mergeReader),
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001115 mTimeoutUs(0) {}
1116
1117NBLog::MergeThread::~MergeThread() {
1118 // set exit flag, set timeout to 0 to force threadLoop to exit and wait for the thread to join
1119 requestExit();
1120 setTimeoutUs(0);
1121 join();
1122}
1123
1124bool NBLog::MergeThread::threadLoop() {
1125 bool doMerge;
1126 {
1127 AutoMutex _l(mMutex);
1128 // If mTimeoutUs is negative, wait on the condition variable until it's positive.
1129 // If it's positive, wait kThreadSleepPeriodUs and then merge
1130 nsecs_t waitTime = mTimeoutUs > 0 ? kThreadSleepPeriodUs * 1000 : LLONG_MAX;
1131 mCond.waitRelative(mMutex, waitTime);
1132 doMerge = mTimeoutUs > 0;
1133 mTimeoutUs -= kThreadSleepPeriodUs;
1134 }
1135 if (doMerge) {
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001136 // Merge data from all the readers
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001137 mMerger.merge();
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001138 // Process the data collected by mMerger and write it to PerformanceAnalysis
1139 // FIXME: decide whether to call getAndProcessSnapshot every time
1140 // or whether to have a separate thread that calls it with a lower frequency
1141 mMergeReader.getAndProcessSnapshot();
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001142 }
1143 return true;
1144}
1145
1146void NBLog::MergeThread::wakeup() {
1147 setTimeoutUs(kThreadWakeupPeriodUs);
1148}
1149
1150void NBLog::MergeThread::setTimeoutUs(int time) {
1151 AutoMutex _l(mMutex);
1152 mTimeoutUs = time;
1153 mCond.signal();
1154}
1155
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001156} // namespace android