blob: cd967e417b4e4e5f1e89d0c17382fc318f6f47af [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 Wagercced6742017-05-10 14:42:54 -070023#include <math.h>
Eric Tanfefe3162018-09-07 10:09:11 -070024#include <memory>
Eric Tan5786e012018-08-15 09:03:47 -070025#include <unordered_set>
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070026#include <vector>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080027#include <stdarg.h>
28#include <stdint.h>
29#include <stdio.h>
30#include <string.h>
Nicolas Rouletc20cb502017-02-01 12:35:24 -080031#include <sys/prctl.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080032#include <time.h>
33#include <new>
Glenn Kasten535e1612016-12-05 12:19:36 -080034#include <audio_utils/roundup.h>
Eric Tanfefe3162018-09-07 10:09:11 -070035#include <json/json.h>
Glenn Kasten8589ce72017-09-08 17:03:42 -070036#include <media/nblog/NBLog.h>
37#include <media/nblog/PerformanceAnalysis.h>
38#include <media/nblog/ReportPerformance.h>
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -070039#include <utils/CallStack.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080040#include <utils/Log.h>
Glenn Kasten4e01ef62013-07-11 14:29:59 -070041#include <utils/String8.h>
Eric Tanfefe3162018-09-07 10:09:11 -070042#include <utils/Timers.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*/
Eric Tan86be8722018-08-10 10:25:47 -070067std::unique_ptr<NBLog::AbstractEntry> NBLog::AbstractEntry::buildEntry(const uint8_t *ptr)
68{
Eric Tan5786e012018-08-15 09:03:47 -070069 if (ptr == nullptr) {
70 return nullptr;
71 }
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -070072 const uint8_t type = EntryIterator(ptr)->type;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070073 switch (type) {
Eric Tancf3d82c2018-09-04 15:44:45 -070074 case EVENT_FMT_START:
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070075 return std::make_unique<FormatEntry>(FormatEntry(ptr));
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -070076 case EVENT_AUDIO_STATE:
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070077 case EVENT_HISTOGRAM_ENTRY_TS:
78 return std::make_unique<HistogramEntry>(HistogramEntry(ptr));
79 default:
80 ALOGW("Tried to create AbstractEntry of type %d", type);
81 return nullptr;
82 }
Nicolas Roulet40a44982017-02-03 13:39:57 -080083}
84
Eric Tan86be8722018-08-10 10:25:47 -070085NBLog::AbstractEntry::AbstractEntry(const uint8_t *entry) : mEntry(entry)
86{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070087}
88
89// ---------------------------------------------------------------------------
Nicolas Rouletcd5dd012017-02-13 12:09:28 -080090
Eric Tan86be8722018-08-10 10:25:47 -070091NBLog::EntryIterator NBLog::FormatEntry::begin() const
92{
Sanna Catherine de Treville Wagerdd92d7e2017-05-15 14:56:53 -070093 return EntryIterator(mEntry);
94}
95
Eric Tan86be8722018-08-10 10:25:47 -070096const char *NBLog::FormatEntry::formatString() const
97{
Nicolas Rouletcd5dd012017-02-13 12:09:28 -080098 return (const char*) mEntry + offsetof(entry, data);
Nicolas Roulet40a44982017-02-03 13:39:57 -080099}
100
Eric Tan86be8722018-08-10 10:25:47 -0700101size_t NBLog::FormatEntry::formatStringLength() const
102{
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800103 return mEntry[offsetof(entry, length)];
Nicolas Roulet40a44982017-02-03 13:39:57 -0800104}
105
Eric Tan86be8722018-08-10 10:25:47 -0700106NBLog::EntryIterator NBLog::FormatEntry::args() const
107{
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800108 auto it = begin();
Eric Tan86be8722018-08-10 10:25:47 -0700109 ++it; // skip start fmt
110 ++it; // skip timestamp
111 ++it; // skip hash
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700112 // Skip author if present
Eric Tancf3d82c2018-09-04 15:44:45 -0700113 if (it->type == EVENT_FMT_AUTHOR) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800114 ++it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800115 }
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700116 return it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800117}
118
Eric Tan86be8722018-08-10 10:25:47 -0700119int64_t NBLog::FormatEntry::timestamp() const
120{
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800121 auto it = begin();
Eric Tan86be8722018-08-10 10:25:47 -0700122 ++it; // skip start fmt
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700123 return it.payload<int64_t>();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800124}
125
Eric Tan86be8722018-08-10 10:25:47 -0700126NBLog::log_hash_t NBLog::FormatEntry::hash() const
127{
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700128 auto it = begin();
Eric Tan86be8722018-08-10 10:25:47 -0700129 ++it; // skip start fmt
130 ++it; // skip timestamp
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700131 // unaligned 64-bit read not supported
132 log_hash_t hash;
133 memcpy(&hash, it->data, sizeof(hash));
134 return hash;
135}
136
Eric Tan86be8722018-08-10 10:25:47 -0700137int NBLog::FormatEntry::author() const
138{
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800139 auto it = begin();
Eric Tan86be8722018-08-10 10:25:47 -0700140 ++it; // skip start fmt
141 ++it; // skip timestamp
142 ++it; // skip hash
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700143 // if there is an author entry, return it, return -1 otherwise
Eric Tancf3d82c2018-09-04 15:44:45 -0700144 return it->type == EVENT_FMT_AUTHOR ? it.payload<int>() : -1;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800145}
146
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700147NBLog::EntryIterator NBLog::FormatEntry::copyWithAuthor(
Eric Tan86be8722018-08-10 10:25:47 -0700148 std::unique_ptr<audio_utils_fifo_writer> &dst, int author) const
149{
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800150 auto it = begin();
Eric Tan86be8722018-08-10 10:25:47 -0700151 it.copyTo(dst); // copy fmt start entry
152 (++it).copyTo(dst); // copy timestamp
153 (++it).copyTo(dst); // copy hash
Nicolas Roulet40a44982017-02-03 13:39:57 -0800154 // insert author entry
Eric Tan86be8722018-08-10 10:25:47 -0700155 size_t authorEntrySize = Entry::kOverhead + sizeof(author);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800156 uint8_t authorEntry[authorEntrySize];
Eric Tancf3d82c2018-09-04 15:44:45 -0700157 authorEntry[offsetof(entry, type)] = EVENT_FMT_AUTHOR;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800158 authorEntry[offsetof(entry, length)] =
Eric Tan86be8722018-08-10 10:25:47 -0700159 authorEntry[authorEntrySize + Entry::kPreviousLengthOffset] =
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800160 sizeof(author);
161 *(int*) (&authorEntry[offsetof(entry, data)]) = author;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800162 dst->write(authorEntry, authorEntrySize);
163 // copy rest of entries
Eric Tancf3d82c2018-09-04 15:44:45 -0700164 while ((++it)->type != EVENT_FMT_END) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800165 it.copyTo(dst);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800166 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800167 it.copyTo(dst);
168 ++it;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800169 return it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800170}
171
Eric Tan86be8722018-08-10 10:25:47 -0700172void NBLog::EntryIterator::copyTo(std::unique_ptr<audio_utils_fifo_writer> &dst) const
173{
174 size_t length = mPtr[offsetof(entry, length)] + Entry::kOverhead;
175 dst->write(mPtr, length);
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800176}
Nicolas Roulet40a44982017-02-03 13:39:57 -0800177
Eric Tan86be8722018-08-10 10:25:47 -0700178void NBLog::EntryIterator::copyData(uint8_t *dst) const
179{
180 memcpy((void*) dst, mPtr + offsetof(entry, data), mPtr[offsetof(entry, length)]);
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800181}
182
Eric Tan86be8722018-08-10 10:25:47 -0700183NBLog::EntryIterator::EntryIterator() // Dummy initialization.
184 : mPtr(nullptr)
185{
186}
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800187
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700188NBLog::EntryIterator::EntryIterator(const uint8_t *entry)
Eric Tan86be8722018-08-10 10:25:47 -0700189 : mPtr(entry)
190{
191}
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800192
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700193NBLog::EntryIterator::EntryIterator(const NBLog::EntryIterator &other)
Eric Tan86be8722018-08-10 10:25:47 -0700194 : mPtr(other.mPtr)
195{
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800196}
197
Eric Tan86be8722018-08-10 10:25:47 -0700198const NBLog::entry& NBLog::EntryIterator::operator*() const
199{
200 return *(entry*) mPtr;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800201}
202
Eric Tan86be8722018-08-10 10:25:47 -0700203const NBLog::entry* NBLog::EntryIterator::operator->() const
204{
205 return (entry*) mPtr;
206}
207
208NBLog::EntryIterator& NBLog::EntryIterator::operator++()
209{
210 mPtr += mPtr[offsetof(entry, length)] + Entry::kOverhead;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800211 return *this;
212}
213
Eric Tan86be8722018-08-10 10:25:47 -0700214NBLog::EntryIterator& NBLog::EntryIterator::operator--()
215{
216 mPtr -= mPtr[Entry::kPreviousLengthOffset] + Entry::kOverhead;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800217 return *this;
218}
219
Eric Tan86be8722018-08-10 10:25:47 -0700220NBLog::EntryIterator NBLog::EntryIterator::next() const
221{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700222 EntryIterator aux(*this);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800223 return ++aux;
224}
225
Eric Tan86be8722018-08-10 10:25:47 -0700226NBLog::EntryIterator NBLog::EntryIterator::prev() const
227{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700228 EntryIterator aux(*this);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800229 return --aux;
230}
231
Eric Tan86be8722018-08-10 10:25:47 -0700232int NBLog::EntryIterator::operator-(const NBLog::EntryIterator &other) const
233{
234 return mPtr - other.mPtr;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800235}
236
Eric Tan86be8722018-08-10 10:25:47 -0700237bool NBLog::EntryIterator::operator!=(const EntryIterator &other) const
238{
239 return mPtr != other.mPtr;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800240}
241
Eric Tan86be8722018-08-10 10:25:47 -0700242bool NBLog::EntryIterator::hasConsistentLength() const
243{
244 return mPtr[offsetof(entry, length)] == mPtr[mPtr[offsetof(entry, length)] +
245 Entry::kOverhead + Entry::kPreviousLengthOffset];
Nicolas Roulet40a44982017-02-03 13:39:57 -0800246}
247
248// ---------------------------------------------------------------------------
249
Eric Tan86be8722018-08-10 10:25:47 -0700250int64_t NBLog::HistogramEntry::timestamp() const
251{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700252 return EntryIterator(mEntry).payload<HistTsEntry>().ts;
253}
254
Eric Tan86be8722018-08-10 10:25:47 -0700255NBLog::log_hash_t NBLog::HistogramEntry::hash() const
256{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700257 return EntryIterator(mEntry).payload<HistTsEntry>().hash;
258}
259
Eric Tan86be8722018-08-10 10:25:47 -0700260int NBLog::HistogramEntry::author() const
261{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700262 EntryIterator it(mEntry);
Eric Tan5786e012018-08-15 09:03:47 -0700263 return it->length == sizeof(HistTsEntryWithAuthor)
264 ? it.payload<HistTsEntryWithAuthor>().author : -1;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700265}
266
267NBLog::EntryIterator NBLog::HistogramEntry::copyWithAuthor(
Eric Tan86be8722018-08-10 10:25:47 -0700268 std::unique_ptr<audio_utils_fifo_writer> &dst, int author) const
269{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700270 // Current histogram entry has {type, length, struct HistTsEntry, length}.
271 // We now want {type, length, struct HistTsEntryWithAuthor, length}
272 uint8_t buffer[Entry::kOverhead + sizeof(HistTsEntryWithAuthor)];
273 // Copy content until the point we want to add the author
274 memcpy(buffer, mEntry, sizeof(entry) + sizeof(HistTsEntry));
275 // Copy the author
276 *(int*) (buffer + sizeof(entry) + sizeof(HistTsEntry)) = author;
277 // Update lengths
278 buffer[offsetof(entry, length)] = sizeof(HistTsEntryWithAuthor);
Ivan Lozano9ef855d2018-01-08 15:19:09 -0800279 buffer[offsetof(entry, data) + sizeof(HistTsEntryWithAuthor) + offsetof(ending, length)]
280 = sizeof(HistTsEntryWithAuthor);
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700281 // Write new buffer into FIFO
282 dst->write(buffer, sizeof(buffer));
283 return EntryIterator(mEntry).next();
284}
285
286// ---------------------------------------------------------------------------
287
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800288#if 0 // FIXME see note in NBLog.h
289NBLog::Timeline::Timeline(size_t size, void *shared)
290 : mSize(roundup(size)), mOwn(shared == NULL),
291 mShared((Shared *) (mOwn ? new char[sharedSize(size)] : shared))
292{
293 new (mShared) Shared;
294}
295
296NBLog::Timeline::~Timeline()
297{
298 mShared->~Shared();
299 if (mOwn) {
300 delete[] (char *) mShared;
301 }
302}
303#endif
304
305/*static*/
306size_t NBLog::Timeline::sharedSize(size_t size)
307{
Glenn Kastened99c2b2016-12-12 08:31:24 -0800308 // TODO fifo now supports non-power-of-2 buffer sizes, so could remove the roundup
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800309 return sizeof(Shared) + roundup(size);
310}
311
312// ---------------------------------------------------------------------------
313
314NBLog::Writer::Writer()
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800315 : mShared(NULL), mFifo(NULL), mFifoWriter(NULL), mEnabled(false), mPidTag(NULL), mPidTagSize(0)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800316{
317}
318
Glenn Kasten535e1612016-12-05 12:19:36 -0800319NBLog::Writer::Writer(void *shared, size_t size)
320 : mShared((Shared *) shared),
321 mFifo(mShared != NULL ?
322 new audio_utils_fifo(size, sizeof(uint8_t),
323 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
324 mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL),
325 mEnabled(mFifoWriter != NULL)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800326{
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800327 // caching pid and process name
328 pid_t id = ::getpid();
329 char procName[16];
330 int status = prctl(PR_GET_NAME, procName);
331 if (status) { // error getting process name
332 procName[0] = '\0';
333 }
334 size_t length = strlen(procName);
335 mPidTagSize = length + sizeof(pid_t);
336 mPidTag = new char[mPidTagSize];
337 memcpy(mPidTag, &id, sizeof(pid_t));
338 memcpy(mPidTag + sizeof(pid_t), procName, length);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800339}
340
Glenn Kasten535e1612016-12-05 12:19:36 -0800341NBLog::Writer::Writer(const sp<IMemory>& iMemory, size_t size)
342 : Writer(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800343{
Glenn Kasten535e1612016-12-05 12:19:36 -0800344 mIMemory = iMemory;
345}
346
347NBLog::Writer::~Writer()
348{
349 delete mFifoWriter;
350 delete mFifo;
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800351 delete[] mPidTag;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800352}
353
354void NBLog::Writer::log(const char *string)
355{
Eric Tan5786e012018-08-15 09:03:47 -0700356 if (!mEnabled) {
357 return;
358 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800359 LOG_ALWAYS_FATAL_IF(string == NULL, "Attempted to log NULL string");
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800360 size_t length = strlen(string);
Glenn Kasten535e1612016-12-05 12:19:36 -0800361 if (length > Entry::kMaxLength) {
362 length = Entry::kMaxLength;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800363 }
364 log(EVENT_STRING, string, length);
365}
366
367void NBLog::Writer::logf(const char *fmt, ...)
368{
Eric Tan5786e012018-08-15 09:03:47 -0700369 if (!mEnabled) {
370 return;
371 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800372 va_list ap;
373 va_start(ap, fmt);
374 Writer::logvf(fmt, ap); // the Writer:: is needed to avoid virtual dispatch for LockedWriter
375 va_end(ap);
376}
377
378void NBLog::Writer::logvf(const char *fmt, va_list ap)
379{
Eric Tan5786e012018-08-15 09:03:47 -0700380 if (!mEnabled) {
381 return;
382 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800383 char buffer[Entry::kMaxLength + 1 /*NUL*/];
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800384 int length = vsnprintf(buffer, sizeof(buffer), fmt, ap);
385 if (length >= (int) sizeof(buffer)) {
386 length = sizeof(buffer) - 1;
387 // NUL termination is not required
388 // buffer[length] = '\0';
389 }
390 if (length >= 0) {
391 log(EVENT_STRING, buffer, length);
392 }
393}
394
395void NBLog::Writer::logTimestamp()
396{
Eric Tan5786e012018-08-15 09:03:47 -0700397 if (!mEnabled) {
398 return;
399 }
Eric Tancf3d82c2018-09-04 15:44:45 -0700400 struct timespec ts;
401 if (!clock_gettime(CLOCK_MONOTONIC, &ts)) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800402 log(EVENT_TIMESTAMP, &ts, sizeof(ts));
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800403 }
404}
405
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800406void NBLog::Writer::logStart(const char *fmt)
407{
Eric Tan5786e012018-08-15 09:03:47 -0700408 if (!mEnabled) {
409 return;
410 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800411 size_t length = strlen(fmt);
412 if (length > Entry::kMaxLength) {
413 length = Entry::kMaxLength;
414 }
Eric Tancf3d82c2018-09-04 15:44:45 -0700415 log(EVENT_FMT_START, fmt, length);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800416}
417
Eric Tancf3d82c2018-09-04 15:44:45 -0700418void NBLog::Writer::logTimestampFormat()
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800419{
Eric Tan5786e012018-08-15 09:03:47 -0700420 if (!mEnabled) {
421 return;
422 }
Eric Tanfefe3162018-09-07 10:09:11 -0700423 const nsecs_t ts = systemTime();
Eric Tancf3d82c2018-09-04 15:44:45 -0700424 if (ts > 0) {
425 log(EVENT_FMT_TIMESTAMP, &ts, sizeof(ts));
426 } else {
427 ALOGE("Failed to get timestamp");
Eric Tan5786e012018-08-15 09:03:47 -0700428 }
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700429}
430
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700431void NBLog::Writer::logEventHistTs(Event event, log_hash_t hash)
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700432{
Eric Tan5786e012018-08-15 09:03:47 -0700433 if (!mEnabled) {
434 return;
435 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700436 HistTsEntry data;
437 data.hash = hash;
Eric Tanfefe3162018-09-07 10:09:11 -0700438 data.ts = systemTime();
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700439 if (data.ts > 0) {
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700440 log(event, &data, sizeof(data));
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700441 } else {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700442 ALOGE("Failed to get timestamp");
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700443 }
444}
445
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700446void NBLog::Writer::logFormat(const char *fmt, log_hash_t hash, ...)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800447{
Eric Tan5786e012018-08-15 09:03:47 -0700448 if (!mEnabled) {
449 return;
450 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800451 va_list ap;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700452 va_start(ap, hash);
453 Writer::logVFormat(fmt, hash, ap);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800454 va_end(ap);
455}
456
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700457void NBLog::Writer::logVFormat(const char *fmt, log_hash_t hash, va_list argp)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800458{
Eric Tan5786e012018-08-15 09:03:47 -0700459 if (!mEnabled) {
460 return;
461 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800462 Writer::logStart(fmt);
463 int i;
Eric Tancf3d82c2018-09-04 15:44:45 -0700464 double d;
465 float f;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800466 char* s;
Eric Tancf3d82c2018-09-04 15:44:45 -0700467 size_t length;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700468 int64_t t;
Eric Tancf3d82c2018-09-04 15:44:45 -0700469 Writer::logTimestampFormat();
470 log(EVENT_FMT_HASH, &hash, sizeof(hash));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800471 for (const char *p = fmt; *p != '\0'; p++) {
472 // TODO: implement more complex formatting such as %.3f
473 if (*p != '%') {
474 continue;
475 }
476 switch(*++p) {
477 case 's': // string
478 s = va_arg(argp, char *);
Eric Tancf3d82c2018-09-04 15:44:45 -0700479 length = strlen(s);
480 if (length > Entry::kMaxLength) {
481 length = Entry::kMaxLength;
482 }
483 log(EVENT_FMT_STRING, s, length);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800484 break;
485
486 case 't': // timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700487 t = va_arg(argp, int64_t);
Eric Tancf3d82c2018-09-04 15:44:45 -0700488 log(EVENT_FMT_TIMESTAMP, &t, sizeof(t));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800489 break;
490
491 case 'd': // integer
492 i = va_arg(argp, int);
Eric Tancf3d82c2018-09-04 15:44:45 -0700493 log(EVENT_FMT_INTEGER, &i, sizeof(i));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800494 break;
495
496 case 'f': // float
Eric Tancf3d82c2018-09-04 15:44:45 -0700497 d = va_arg(argp, double); // float arguments are promoted to double in vararg lists
498 f = (float)d;
499 log(EVENT_FMT_FLOAT, &f, sizeof(f));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800500 break;
501
502 case 'p': // pid
Eric Tancf3d82c2018-09-04 15:44:45 -0700503 log(EVENT_FMT_PID, mPidTag, mPidTagSize);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800504 break;
505
506 // the "%\0" case finishes parsing
507 case '\0':
508 --p;
509 break;
510
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800511 case '%':
512 break;
513
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800514 default:
515 ALOGW("NBLog Writer parsed invalid format specifier: %c", *p);
516 break;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800517 }
518 }
Eric Tancf3d82c2018-09-04 15:44:45 -0700519 Entry etr(EVENT_FMT_END, nullptr, 0);
520 log(etr, true);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800521}
522
523void NBLog::Writer::log(Event event, const void *data, size_t length)
524{
Eric Tan5786e012018-08-15 09:03:47 -0700525 if (!mEnabled) {
526 return;
527 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800528 if (data == NULL || length > Entry::kMaxLength) {
529 // TODO Perhaps it makes sense to display truncated data or at least a
530 // message that the data is too long? The current behavior can create
531 // a confusion for a programmer debugging their code.
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800532 return;
533 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700534 // Ignore if invalid event
535 if (event == EVENT_RESERVED || event >= EVENT_UPPER_BOUND) {
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800536 return;
537 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700538 Entry etr(event, data, length);
Eric Tan86be8722018-08-10 10:25:47 -0700539 log(etr, true /*trusted*/);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800540}
541
Eric Tan86be8722018-08-10 10:25:47 -0700542void NBLog::Writer::log(const NBLog::Entry &etr, bool trusted)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800543{
Eric Tan5786e012018-08-15 09:03:47 -0700544 if (!mEnabled) {
545 return;
546 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800547 if (!trusted) {
Eric Tan86be8722018-08-10 10:25:47 -0700548 log(etr.mEvent, etr.mData, etr.mLength);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800549 return;
550 }
Eric Tan5786e012018-08-15 09:03:47 -0700551 const size_t need = etr.mLength + Entry::kOverhead; // mEvent, mLength, data[mLength], mLength
552 // need = number of bytes written to FIFO
Glenn Kasten535e1612016-12-05 12:19:36 -0800553
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800554 // FIXME optimize this using memcpy for the data part of the Entry.
555 // The Entry could have a method copyTo(ptr, offset, size) to optimize the copy.
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700556 // checks size of a single log Entry: type, length, data pointer and ending
Glenn Kasten535e1612016-12-05 12:19:36 -0800557 uint8_t temp[Entry::kMaxLength + Entry::kOverhead];
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700558 // write this data to temp array
Glenn Kasten535e1612016-12-05 12:19:36 -0800559 for (size_t i = 0; i < need; i++) {
Eric Tan86be8722018-08-10 10:25:47 -0700560 temp[i] = etr.copyEntryDataAt(i);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800561 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700562 // write to circular buffer
Glenn Kasten535e1612016-12-05 12:19:36 -0800563 mFifoWriter->write(temp, need);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800564}
565
566bool NBLog::Writer::isEnabled() const
567{
568 return mEnabled;
569}
570
571bool NBLog::Writer::setEnabled(bool enabled)
572{
573 bool old = mEnabled;
574 mEnabled = enabled && mShared != NULL;
575 return old;
576}
577
578// ---------------------------------------------------------------------------
579
580NBLog::LockedWriter::LockedWriter()
581 : Writer()
582{
583}
584
Glenn Kasten535e1612016-12-05 12:19:36 -0800585NBLog::LockedWriter::LockedWriter(void *shared, size_t size)
586 : Writer(shared, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800587{
588}
589
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800590bool NBLog::LockedWriter::isEnabled() const
591{
592 Mutex::Autolock _l(mLock);
593 return Writer::isEnabled();
594}
595
596bool NBLog::LockedWriter::setEnabled(bool enabled)
597{
598 Mutex::Autolock _l(mLock);
599 return Writer::setEnabled(enabled);
600}
601
Eric Tancf3d82c2018-09-04 15:44:45 -0700602void NBLog::LockedWriter::log(const Entry &entry, bool trusted) {
603 Mutex::Autolock _l(mLock);
604 Writer::log(entry, trusted);
605}
606
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800607// ---------------------------------------------------------------------------
608
Eric Tancf3d82c2018-09-04 15:44:45 -0700609// We make a set of the invalid types rather than the valid types when aligning
610// Snapshot EntryIterators to valid entries during log corruption checking.
611// This is done in order to avoid the maintenance overhead of adding a new NBLog::Event
612// type to the two sets below whenever a new NBLog::Event type is created, as it is
613// very likely that new types added will be valid types.
614// Currently, invalidBeginTypes and invalidEndTypes are used to handle the special
615// case of a Format Entry, which consists of a variable number of simple log entries.
616// If a new NBLog::Event is added that consists of a variable number of simple log entries,
617// then these sets need to be updated.
618
619// We want the beginning of a Snapshot to point to an entry that is not in
620// the middle of a formatted entry and not an FMT_END.
621const std::unordered_set<NBLog::Event> NBLog::Reader::invalidBeginTypes {
622 NBLog::Event::EVENT_FMT_TIMESTAMP,
623 NBLog::Event::EVENT_FMT_HASH,
624 NBLog::Event::EVENT_FMT_STRING,
625 NBLog::Event::EVENT_FMT_INTEGER,
626 NBLog::Event::EVENT_FMT_FLOAT,
627 NBLog::Event::EVENT_FMT_PID,
628 NBLog::Event::EVENT_FMT_AUTHOR,
629 NBLog::Event::EVENT_FMT_END
Eric Tan5786e012018-08-15 09:03:47 -0700630};
Eric Tancf3d82c2018-09-04 15:44:45 -0700631
632// We want the end of a Snapshot to point to an entry that is not in
633// the middle of a formatted entry and not a FMT_START.
634const std::unordered_set<NBLog::Event> NBLog::Reader::invalidEndTypes {
635 NBLog::Event::EVENT_FMT_START,
636 NBLog::Event::EVENT_FMT_TIMESTAMP,
637 NBLog::Event::EVENT_FMT_HASH,
638 NBLog::Event::EVENT_FMT_STRING,
639 NBLog::Event::EVENT_FMT_INTEGER,
640 NBLog::Event::EVENT_FMT_FLOAT,
641 NBLog::Event::EVENT_FMT_PID,
642 NBLog::Event::EVENT_FMT_AUTHOR
Eric Tan5786e012018-08-15 09:03:47 -0700643};
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700644
Eric Tan5786e012018-08-15 09:03:47 -0700645NBLog::Reader::Reader(const void *shared, size_t size, const std::string &name)
Eric Tan6af18472018-08-20 09:27:50 -0700646 : mName(name),
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700647 mShared((/*const*/ Shared *) shared), /*mIMemory*/
Glenn Kasten535e1612016-12-05 12:19:36 -0800648 mFifo(mShared != NULL ?
649 new audio_utils_fifo(size, sizeof(uint8_t),
650 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700651 mFifoReader(mFifo != NULL ? new audio_utils_fifo_reader(*mFifo) : NULL)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800652{
653}
654
Eric Tan5786e012018-08-15 09:03:47 -0700655NBLog::Reader::Reader(const sp<IMemory>& iMemory, size_t size, const std::string &name)
656 : Reader(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size, name)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800657{
Glenn Kasten535e1612016-12-05 12:19:36 -0800658 mIMemory = iMemory;
659}
660
661NBLog::Reader::~Reader()
662{
663 delete mFifoReader;
664 delete mFifo;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800665}
666
Eric Tancf3d82c2018-09-04 15:44:45 -0700667const uint8_t *NBLog::Reader::findLastValidEntry(const uint8_t *front, const uint8_t *back,
668 const std::unordered_set<Event> &invalidTypes) {
669 if (front == nullptr || back == nullptr) {
670 return nullptr;
671 }
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800672 while (back + Entry::kPreviousLengthOffset >= front) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700673 const uint8_t *prev = back - back[Entry::kPreviousLengthOffset] - Entry::kOverhead;
Eric Tancf3d82c2018-09-04 15:44:45 -0700674 const Event type = (const Event)prev[offsetof(entry, type)];
675 if (prev < front
676 || prev + prev[offsetof(entry, length)] + Entry::kOverhead != back
677 || type <= NBLog::EVENT_RESERVED || type >= NBLog::EVENT_UPPER_BOUND) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800678 // prev points to an out of limits or inconsistent entry
679 return nullptr;
680 }
Eric Tancf3d82c2018-09-04 15:44:45 -0700681 // if invalidTypes does not contain the type, then the type is valid.
682 if (invalidTypes.find(type) == invalidTypes.end()) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800683 return prev;
684 }
685 back = prev;
686 }
687 return nullptr; // no entry found
688}
689
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700690// Copies content of a Reader FIFO into its Snapshot
691// The Snapshot has the same raw data, but represented as a sequence of entries
692// and an EntryIterator making it possible to process the data.
Eric Tan6af18472018-08-20 09:27:50 -0700693std::unique_ptr<NBLog::Snapshot> NBLog::Reader::getSnapshot()
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800694{
Glenn Kasten535e1612016-12-05 12:19:36 -0800695 if (mFifoReader == NULL) {
Eric Tancf3d82c2018-09-04 15:44:45 -0700696 return std::unique_ptr<Snapshot>(new Snapshot());
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800697 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800698
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800699 // This emulates the behaviour of audio_utils_fifo_reader::read, but without incrementing the
700 // reader index. The index is incremented after handling corruption, to after the last complete
701 // entry of the buffer
Eric Tan5786e012018-08-15 09:03:47 -0700702 size_t lost = 0;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800703 audio_utils_iovec iovec[2];
Eric Tan5786e012018-08-15 09:03:47 -0700704 const size_t capacity = mFifo->capacity();
705 ssize_t availToRead;
706 // A call to audio_utils_fifo_reader::obtain() places the read pointer one buffer length
707 // before the writer's pointer (since mFifoReader was constructed with flush=false). The
708 // do while loop is an attempt to read all of the FIFO's contents regardless of how behind
709 // the reader is with respect to the writer. However, the following scheduling sequence is
710 // possible and can lead to a starvation situation:
711 // - Writer T1 writes, overrun with respect to Reader T2
712 // - T2 calls obtain() and gets EOVERFLOW, T2 ptr placed one buffer size behind T1 ptr
713 // - T1 write, overrun
714 // - T2 obtain(), EOVERFLOW (and so on...)
715 // To address this issue, we limit the number of tries for the reader to catch up with
716 // the writer.
717 int tries = 0;
718 size_t lostTemp;
719 do {
720 availToRead = mFifoReader->obtain(iovec, capacity, NULL /*timeout*/, &lostTemp);
721 lost += lostTemp;
722 } while (availToRead < 0 || ++tries <= kMaxObtainTries);
723
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800724 if (availToRead <= 0) {
Eric Tan5786e012018-08-15 09:03:47 -0700725 ALOGW_IF(availToRead < 0, "NBLog Reader %s failed to catch up with Writer", mName.c_str());
Eric Tancf3d82c2018-09-04 15:44:45 -0700726 return std::unique_ptr<Snapshot>(new Snapshot());
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800727 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800728
Eric Tancf3d82c2018-09-04 15:44:45 -0700729 // Change to #if 1 for debugging. This statement is useful for checking buffer fullness levels
730 // (as seen by reader) and how much data was lost. If you find that the fullness level is
731 // getting close to full, or that data loss is happening to often, then you should
732 // probably try some of the following:
733 // - log less data
734 // - log less often
735 // - increase the initial shared memory allocation for the buffer
736#if 0
737 ALOGD("getSnapshot name=%s, availToRead=%zd, capacity=%zu, fullness=%.3f, lost=%zu",
738 name().c_str(), availToRead, capacity, (double)availToRead / (double)capacity, lost);
739#endif
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800740 std::unique_ptr<Snapshot> snapshot(new Snapshot(availToRead));
741 memcpy(snapshot->mData, (const char *) mFifo->buffer() + iovec[0].mOffset, iovec[0].mLength);
742 if (iovec[1].mLength > 0) {
743 memcpy(snapshot->mData + (iovec[0].mLength),
Eric Tan5786e012018-08-15 09:03:47 -0700744 (const char *) mFifo->buffer() + iovec[1].mOffset, iovec[1].mLength);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800745 }
746
747 // Handle corrupted buffer
748 // Potentially, a buffer has corrupted data on both beginning (due to overflow) and end
749 // (due to incomplete format entry). But even if the end format entry is incomplete,
Eric Tancf3d82c2018-09-04 15:44:45 -0700750 // it ends in a complete entry (which is not an FMT_END). So is safe to traverse backwards.
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800751 // TODO: handle client corruption (in the middle of a buffer)
752
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700753 const uint8_t *back = snapshot->mData + availToRead;
754 const uint8_t *front = snapshot->mData;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800755
Eric Tancf3d82c2018-09-04 15:44:45 -0700756 // Find last FMT_END. <back> is sitting on an entry which might be the middle of a FormatEntry.
757 // We go backwards until we find an EVENT_FMT_END.
758 const uint8_t *lastEnd = findLastValidEntry(front, back, invalidEndTypes);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800759 if (lastEnd == nullptr) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700760 snapshot->mEnd = snapshot->mBegin = EntryIterator(front);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800761 } else {
Eric Tancf3d82c2018-09-04 15:44:45 -0700762 // end of snapshot points to after last FMT_END entry
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700763 snapshot->mEnd = EntryIterator(lastEnd).next();
Eric Tancf3d82c2018-09-04 15:44:45 -0700764 // find first FMT_START
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700765 const uint8_t *firstStart = nullptr;
766 const uint8_t *firstStartTmp = snapshot->mEnd;
Eric Tancf3d82c2018-09-04 15:44:45 -0700767 while ((firstStartTmp = findLastValidEntry(front, firstStartTmp, invalidBeginTypes))
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800768 != nullptr) {
769 firstStart = firstStartTmp;
770 }
Eric Tancf3d82c2018-09-04 15:44:45 -0700771 // firstStart is null if no FMT_START entry was found before lastEnd
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800772 if (firstStart == nullptr) {
773 snapshot->mBegin = snapshot->mEnd;
774 } else {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700775 snapshot->mBegin = EntryIterator(firstStart);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800776 }
777 }
778
779 // advance fifo reader index to after last entry read.
780 mFifoReader->release(snapshot->mEnd - front);
781
782 snapshot->mLost = lost;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800783 return snapshot;
784}
785
Eric Tanfefe3162018-09-07 10:09:11 -0700786// TODO separate this method from the rest of NBLog
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700787// Takes raw content of the local merger FIFO, processes log entries, and
788// writes the data to a map of class PerformanceAnalysis, based on their thread ID.
Eric Tanfefe3162018-09-07 10:09:11 -0700789void NBLog::MergeReader::processSnapshot(NBLog::Snapshot &snapshot, int author)
Nicolas Roulet40a44982017-02-03 13:39:57 -0800790{
Eric Tanfefe3162018-09-07 10:09:11 -0700791 PerformanceData& data = mThreadPerformanceData[author];
Eric Tancf3d82c2018-09-04 15:44:45 -0700792 // We don't do "auto it" because it reduces readability in this case.
793 for (EntryIterator it = snapshot.begin(); it != snapshot.end(); ++it) {
794 switch (it->type) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700795 case EVENT_HISTOGRAM_ENTRY_TS: {
Eric Tanfefe3162018-09-07 10:09:11 -0700796 const HistTsEntry payload = it.payload<HistTsEntry>();
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700797 // TODO: hash for histogram ts and audio state need to match
798 // and correspond to audio production source file location
Eric Tancf3d82c2018-09-04 15:44:45 -0700799 mThreadPerformanceAnalysis[author][0 /*hash*/].logTsEntry(payload.ts);
Eric Tan6af18472018-08-20 09:27:50 -0700800 } break;
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700801 case EVENT_AUDIO_STATE: {
Eric Tan6af18472018-08-20 09:27:50 -0700802 mThreadPerformanceAnalysis[author][0 /*hash*/].handleStateChange();
803 } break;
Eric Tanfefe3162018-09-07 10:09:11 -0700804 case EVENT_THREAD_INFO: {
805 const thread_info_t info = it.payload<thread_info_t>();
806 // TODO make PerformanceData hold a type of thread_info_t.
807 // Currently, thread_info_t is defined in NBLog.h, which includes
808 // PerformanceAnalysis.h. PerformanceData is defined in PerformanceAnalysis.h,
809 // where including NBLog.h would result in circular includes. The organization
810 // of files will need to change to avoid this problem.
811 data.type = info.type;
812 data.frameCount = info.frameCount;
813 data.sampleRate = info.sampleRate;
814 } break;
Eric Tane98dd6f2018-08-22 18:23:50 -0700815 case EVENT_LATENCY: {
Eric Tanfefe3162018-09-07 10:09:11 -0700816 const double latencyMs = it.payload<double>();
817 data.latencyHist.add(latencyMs);
Eric Tane98dd6f2018-08-22 18:23:50 -0700818 } break;
Eric Tancf3d82c2018-09-04 15:44:45 -0700819 case EVENT_WORK_TIME: {
Eric Tanfefe3162018-09-07 10:09:11 -0700820 const int64_t monotonicNs = it.payload<int64_t>();
Eric Tane98dd6f2018-08-22 18:23:50 -0700821 const double monotonicMs = monotonicNs * 1e-6;
Eric Tanfefe3162018-09-07 10:09:11 -0700822 data.workHist.add(monotonicMs);
823 data.active += monotonicNs;
Eric Tane98dd6f2018-08-22 18:23:50 -0700824 } break;
Eric Tanfefe3162018-09-07 10:09:11 -0700825 case EVENT_WARMUP_TIME: {
826 const double timeMs = it.payload<double>();
827 data.warmupHist.add(timeMs);
828 } break;
829 case EVENT_UNDERRUN:
830 data.underruns++;
831 break;
832 case EVENT_OVERRUN:
833 data.overruns++;
834 break;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800835 case EVENT_RESERVED:
Eric Tan6af18472018-08-20 09:27:50 -0700836 case EVENT_UPPER_BOUND:
Eric Tancf3d82c2018-09-04 15:44:45 -0700837 ALOGW("warning: unexpected event %d", it->type);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800838 default:
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800839 break;
840 }
Sanna Catherine de Treville Wager9484bae2017-06-15 14:39:44 -0700841 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800842}
843
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700844void NBLog::MergeReader::getAndProcessSnapshot()
Nicolas Roulet40a44982017-02-03 13:39:57 -0800845{
Eric Tan6af18472018-08-20 09:27:50 -0700846 // get a snapshot of each reader and process them
847 // TODO insert lock here
848 const size_t nLogs = mReaders.size();
849 std::vector<std::unique_ptr<Snapshot>> snapshots(nLogs);
850 for (size_t i = 0; i < nLogs; i++) {
851 snapshots[i] = mReaders[i]->getSnapshot();
852 }
853 // TODO unlock lock here
854 for (size_t i = 0; i < nLogs; i++) {
855 if (snapshots[i] != nullptr) {
Eric Tanfefe3162018-09-07 10:09:11 -0700856 processSnapshot(*(snapshots[i]), i);
Eric Tan6af18472018-08-20 09:27:50 -0700857 }
858 }
Nicolas Roulet40a44982017-02-03 13:39:57 -0800859}
860
Eric Tan86be8722018-08-10 10:25:47 -0700861void NBLog::MergeReader::dump(int fd, int indent)
862{
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700863 // TODO: add a mutex around media.log dump
Sanna Catherine de Treville Wagercf6c75a2017-07-21 17:05:25 -0700864 ReportPerformance::dump(fd, indent, mThreadPerformanceAnalysis);
Eric Tanfefe3162018-09-07 10:09:11 -0700865 Json::Value root(Json::arrayValue);
866 for (const auto& item : mThreadPerformanceData) {
867 const PerformanceData& data = item.second;
868 std::unique_ptr<Json::Value> threadData = dumpToJson(data);
869 if (threadData == nullptr) {
870 continue;
871 }
872 (*threadData)["threadNum"] = item.first;
873 root.append(*threadData);
874 }
875 Json::StyledWriter writer;
876 std::string rootStr = writer.write(root);
877 dprintf(fd, "%s", rootStr.c_str());
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700878}
879
Eric Tan6af18472018-08-20 09:27:50 -0700880// TODO for future compatibility, would prefer to have a dump() go to string, and then go
881// to fd only when invoked through binder.
882void NBLog::DumpReader::dump(int fd, size_t indent)
Eric Tan5786e012018-08-15 09:03:47 -0700883{
Eric Tan6af18472018-08-20 09:27:50 -0700884 if (fd < 0) return;
885 std::unique_ptr<Snapshot> snapshot = getSnapshot();
886 if (snapshot == nullptr) {
Eric Tan5786e012018-08-15 09:03:47 -0700887 return;
888 }
Eric Tan6af18472018-08-20 09:27:50 -0700889 String8 timestamp, body;
890
891 // TODO all logged types should have a printable format.
Eric Tancf3d82c2018-09-04 15:44:45 -0700892 for (EntryIterator it = snapshot->begin(); it != snapshot->end(); ++it) {
Eric Tan6af18472018-08-20 09:27:50 -0700893 switch (it->type) {
Eric Tancf3d82c2018-09-04 15:44:45 -0700894 case EVENT_FMT_START:
Eric Tan6af18472018-08-20 09:27:50 -0700895 it = handleFormat(FormatEntry(it), &timestamp, &body);
896 break;
Eric Tancf3d82c2018-09-04 15:44:45 -0700897 case EVENT_WORK_TIME: {
Eric Tanfefe3162018-09-07 10:09:11 -0700898 const int64_t monotonicNs = it.payload<int64_t>();
899 body.appendFormat("Thread cycle: %ld ns", (long)monotonicNs);
Eric Tane98dd6f2018-08-22 18:23:50 -0700900 } break;
901 case EVENT_LATENCY: {
Eric Tanfefe3162018-09-07 10:09:11 -0700902 const double latencyMs = it.payload<double>();
Eric Tane98dd6f2018-08-22 18:23:50 -0700903 body.appendFormat("latency: %.3f ms", latencyMs);
Eric Tan6af18472018-08-20 09:27:50 -0700904 } break;
Eric Tanfefe3162018-09-07 10:09:11 -0700905 case EVENT_WARMUP_TIME: {
906 const double timeMs = it.payload<double>();
907 body.appendFormat("warmup time: %.3f ms", timeMs);
908 } break;
909 case EVENT_UNDERRUN:
910 body.appendFormat("underrun");
911 break;
912 case EVENT_OVERRUN:
913 body.appendFormat("overrun");
914 break;
Eric Tancf3d82c2018-09-04 15:44:45 -0700915 case EVENT_FMT_END:
Eric Tan6af18472018-08-20 09:27:50 -0700916 case EVENT_RESERVED:
917 case EVENT_UPPER_BOUND:
918 body.appendFormat("warning: unexpected event %d", it->type);
919 default:
920 break;
921 }
922 if (!body.isEmpty()) {
923 dprintf(fd, "%.*s%s %s\n", (int)indent, "", timestamp.string(), body.string());
924 body.clear();
925 }
926 timestamp.clear();
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700927 }
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700928}
929
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800930bool NBLog::Reader::isIMemory(const sp<IMemory>& iMemory) const
931{
Glenn Kasten481fb672013-09-30 14:39:28 -0700932 return iMemory != 0 && mIMemory != 0 && iMemory->pointer() == mIMemory->pointer();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800933}
934
Eric Tan6af18472018-08-20 09:27:50 -0700935void NBLog::DumpReader::appendTimestamp(String8 *body, const void *data)
Eric Tan86be8722018-08-10 10:25:47 -0700936{
Eric Tan5786e012018-08-15 09:03:47 -0700937 if (body == nullptr || data == nullptr) {
938 return;
939 }
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700940 int64_t ts;
941 memcpy(&ts, data, sizeof(ts));
942 body->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
943 (int) ((ts / (1000 * 1000)) % 1000));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800944}
945
Eric Tan6af18472018-08-20 09:27:50 -0700946void NBLog::DumpReader::appendInt(String8 *body, const void *data)
Eric Tan86be8722018-08-10 10:25:47 -0700947{
Eric Tan5786e012018-08-15 09:03:47 -0700948 if (body == nullptr || data == nullptr) {
949 return;
950 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800951 int x = *((int*) data);
952 body->appendFormat("<%d>", x);
953}
954
Eric Tan6af18472018-08-20 09:27:50 -0700955void NBLog::DumpReader::appendFloat(String8 *body, const void *data)
Eric Tan86be8722018-08-10 10:25:47 -0700956{
Eric Tan5786e012018-08-15 09:03:47 -0700957 if (body == nullptr || data == nullptr) {
958 return;
959 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800960 float f;
Eric Tan86be8722018-08-10 10:25:47 -0700961 memcpy(&f, data, sizeof(f));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800962 body->appendFormat("<%f>", f);
963}
964
Eric Tan6af18472018-08-20 09:27:50 -0700965void NBLog::DumpReader::appendPID(String8 *body, const void* data, size_t length)
Eric Tan86be8722018-08-10 10:25:47 -0700966{
Eric Tan5786e012018-08-15 09:03:47 -0700967 if (body == nullptr || data == nullptr) {
968 return;
969 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800970 pid_t id = *((pid_t*) data);
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800971 char * name = &((char*) data)[sizeof(pid_t)];
972 body->appendFormat("<PID: %d, name: %.*s>", id, (int) (length - sizeof(pid_t)), name);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800973}
974
Eric Tan6af18472018-08-20 09:27:50 -0700975String8 NBLog::DumpReader::bufferDump(const uint8_t *buffer, size_t size)
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700976{
977 String8 str;
Eric Tan5786e012018-08-15 09:03:47 -0700978 if (buffer == nullptr) {
979 return str;
980 }
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700981 str.append("[ ");
Eric Tan86be8722018-08-10 10:25:47 -0700982 for(size_t i = 0; i < size; i++) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700983 str.appendFormat("%d ", buffer[i]);
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700984 }
985 str.append("]");
986 return str;
987}
988
Eric Tan6af18472018-08-20 09:27:50 -0700989String8 NBLog::DumpReader::bufferDump(const EntryIterator &it)
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700990{
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700991 return bufferDump(it, it->length + Entry::kOverhead);
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700992}
993
Eric Tan6af18472018-08-20 09:27:50 -0700994NBLog::EntryIterator NBLog::DumpReader::handleFormat(const FormatEntry &fmtEntry,
995 String8 *timestamp,
996 String8 *body)
Eric Tan86be8722018-08-10 10:25:47 -0700997{
Nicolas Roulet40a44982017-02-03 13:39:57 -0800998 // log timestamp
Eric Tanfefe3162018-09-07 10:09:11 -0700999 const int64_t ts = fmtEntry.timestamp();
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001000 timestamp->clear();
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001001 timestamp->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
1002 (int) ((ts / (1000 * 1000)) % 1000));
Nicolas Roulet40a44982017-02-03 13:39:57 -08001003
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -07001004 // log unique hash
1005 log_hash_t hash = fmtEntry.hash();
1006 // print only lower 16bit of hash as hex and line as int to reduce spam in the log
1007 body->appendFormat("%.4X-%d ", (int)(hash >> 16) & 0xFFFF, (int) hash & 0xFFFF);
1008
Nicolas Roulet40a44982017-02-03 13:39:57 -08001009 // log author (if present)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001010 handleAuthor(fmtEntry, body);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001011
1012 // log string
Eric Tan86be8722018-08-10 10:25:47 -07001013 EntryIterator arg = fmtEntry.args();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001014
1015 const char* fmt = fmtEntry.formatString();
1016 size_t fmt_length = fmtEntry.formatStringLength();
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001017
1018 for (size_t fmt_offset = 0; fmt_offset < fmt_length; ++fmt_offset) {
1019 if (fmt[fmt_offset] != '%') {
1020 body->append(&fmt[fmt_offset], 1); // TODO optimize to write consecutive strings at once
1021 continue;
1022 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001023 // case "%%""
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001024 if (fmt[++fmt_offset] == '%') {
1025 body->append("%");
1026 continue;
1027 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001028 // case "%\0"
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001029 if (fmt_offset == fmt_length) {
1030 continue;
1031 }
1032
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001033 NBLog::Event event = (NBLog::Event) arg->type;
1034 size_t length = arg->length;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001035
1036 // TODO check length for event type is correct
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001037
Eric Tancf3d82c2018-09-04 15:44:45 -07001038 if (event == EVENT_FMT_END) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001039 break;
1040 }
1041
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001042 // TODO: implement more complex formatting such as %.3f
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001043 const uint8_t *datum = arg->data; // pointer to the current event args
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001044 switch(fmt[fmt_offset])
1045 {
1046 case 's': // string
Eric Tancf3d82c2018-09-04 15:44:45 -07001047 ALOGW_IF(event != EVENT_FMT_STRING,
Nicolas Roulet4da78202017-02-03 12:53:39 -08001048 "NBLog Reader incompatible event for string specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001049 body->append((const char*) datum, length);
1050 break;
1051
1052 case 't': // timestamp
Eric Tancf3d82c2018-09-04 15:44:45 -07001053 ALOGW_IF(event != EVENT_FMT_TIMESTAMP,
Nicolas Roulet4da78202017-02-03 12:53:39 -08001054 "NBLog Reader incompatible event for timestamp specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001055 appendTimestamp(body, datum);
1056 break;
1057
1058 case 'd': // integer
Eric Tancf3d82c2018-09-04 15:44:45 -07001059 ALOGW_IF(event != EVENT_FMT_INTEGER,
Nicolas Roulet4da78202017-02-03 12:53:39 -08001060 "NBLog Reader incompatible event for integer specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001061 appendInt(body, datum);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001062 break;
1063
1064 case 'f': // float
Eric Tancf3d82c2018-09-04 15:44:45 -07001065 ALOGW_IF(event != EVENT_FMT_FLOAT,
Nicolas Roulet4da78202017-02-03 12:53:39 -08001066 "NBLog Reader incompatible event for float specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001067 appendFloat(body, datum);
1068 break;
1069
1070 case 'p': // pid
Eric Tancf3d82c2018-09-04 15:44:45 -07001071 ALOGW_IF(event != EVENT_FMT_PID,
Nicolas Roulet4da78202017-02-03 12:53:39 -08001072 "NBLog Reader incompatible event for pid specifier: %d", event);
Nicolas Rouletc20cb502017-02-01 12:35:24 -08001073 appendPID(body, datum, length);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001074 break;
1075
1076 default:
1077 ALOGW("NBLog Reader encountered unknown character %c", fmt[fmt_offset]);
1078 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001079 ++arg;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001080 }
Eric Tancf3d82c2018-09-04 15:44:45 -07001081 ALOGW_IF(arg->type != EVENT_FMT_END, "Expected end of format, got %d", arg->type);
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001082 return arg;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001083}
1084
Nicolas Roulet40a44982017-02-03 13:39:57 -08001085NBLog::Merger::Merger(const void *shared, size_t size):
Nicolas Roulet40a44982017-02-03 13:39:57 -08001086 mShared((Shared *) shared),
1087 mFifo(mShared != NULL ?
1088 new audio_utils_fifo(size, sizeof(uint8_t),
1089 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
1090 mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL)
Eric Tan86be8722018-08-10 10:25:47 -07001091{
1092}
Nicolas Roulet40a44982017-02-03 13:39:57 -08001093
Eric Tan5786e012018-08-15 09:03:47 -07001094void NBLog::Merger::addReader(const sp<NBLog::Reader> &reader)
Eric Tan86be8722018-08-10 10:25:47 -07001095{
Glenn Kasten1c446272017-04-07 09:49:07 -07001096 // FIXME This is called by binder thread in MediaLogService::registerWriter
Eric Tan5786e012018-08-15 09:03:47 -07001097 // but the access to shared variable mReaders is not yet protected by a lock.
1098 mReaders.push_back(reader);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001099}
1100
1101// items placed in priority queue during merge
1102// composed by a timestamp and the index of the snapshot where the timestamp came from
1103struct MergeItem
1104{
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001105 int64_t ts;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001106 int index;
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001107 MergeItem(int64_t ts, int index): ts(ts), index(index) {}
Nicolas Roulet40a44982017-02-03 13:39:57 -08001108};
1109
Eric Tan86be8722018-08-10 10:25:47 -07001110bool operator>(const struct MergeItem &i1, const struct MergeItem &i2)
1111{
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001112 return i1.ts > i2.ts || (i1.ts == i2.ts && i1.index > i2.index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001113}
1114
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001115// Merge registered readers, sorted by timestamp, and write data to a single FIFO in local memory
Eric Tan86be8722018-08-10 10:25:47 -07001116void NBLog::Merger::merge()
1117{
Eric Tan6af18472018-08-20 09:27:50 -07001118 if (true) return; // Merging is not necessary at the moment, so this is to disable it
1119 // and bypass compiler warnings about member variables not being used.
Eric Tan5786e012018-08-15 09:03:47 -07001120 const int nLogs = mReaders.size();
Eric Tan6af18472018-08-20 09:27:50 -07001121 std::vector<std::unique_ptr<Snapshot>> snapshots(nLogs);
Eric Tan86be8722018-08-10 10:25:47 -07001122 std::vector<EntryIterator> offsets;
1123 offsets.reserve(nLogs);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001124 for (int i = 0; i < nLogs; ++i) {
Eric Tan5786e012018-08-15 09:03:47 -07001125 snapshots[i] = mReaders[i]->getSnapshot();
Eric Tan86be8722018-08-10 10:25:47 -07001126 offsets.push_back(snapshots[i]->begin());
Nicolas Roulet40a44982017-02-03 13:39:57 -08001127 }
1128 // initialize offsets
Nicolas Roulet40a44982017-02-03 13:39:57 -08001129 // TODO custom heap implementation could allow to update top, improving performance
1130 // for bursty buffers
1131 std::priority_queue<MergeItem, std::vector<MergeItem>, std::greater<MergeItem>> timestamps;
1132 for (int i = 0; i < nLogs; ++i)
1133 {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001134 if (offsets[i] != snapshots[i]->end()) {
Eric Tan86be8722018-08-10 10:25:47 -07001135 std::unique_ptr<AbstractEntry> abstractEntry = AbstractEntry::buildEntry(offsets[i]);
1136 if (abstractEntry == nullptr) {
1137 continue;
1138 }
1139 timestamps.emplace(abstractEntry->timestamp(), i);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001140 }
1141 }
1142
1143 while (!timestamps.empty()) {
Eric Tan86be8722018-08-10 10:25:47 -07001144 int index = timestamps.top().index; // find minimum timestamp
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001145 // copy it to the log, increasing offset
Eric Tan86be8722018-08-10 10:25:47 -07001146 offsets[index] = AbstractEntry::buildEntry(offsets[index])->
1147 copyWithAuthor(mFifoWriter, index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001148 // update data structures
Nicolas Roulet40a44982017-02-03 13:39:57 -08001149 timestamps.pop();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001150 if (offsets[index] != snapshots[index]->end()) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001151 int64_t ts = AbstractEntry::buildEntry(offsets[index])->timestamp();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001152 timestamps.emplace(ts, index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001153 }
1154 }
1155}
1156
Eric Tan5786e012018-08-15 09:03:47 -07001157const std::vector<sp<NBLog::Reader>>& NBLog::Merger::getReaders() const
Eric Tan86be8722018-08-10 10:25:47 -07001158{
Eric Tan5786e012018-08-15 09:03:47 -07001159 //AutoMutex _l(mLock);
1160 return mReaders;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001161}
1162
Glenn Kasten1c446272017-04-07 09:49:07 -07001163// ---------------------------------------------------------------------------
1164
Nicolas Roulet40a44982017-02-03 13:39:57 -08001165NBLog::MergeReader::MergeReader(const void *shared, size_t size, Merger &merger)
Eric Tan5786e012018-08-15 09:03:47 -07001166 : Reader(shared, size, "MergeReader"), mReaders(merger.getReaders())
Eric Tan86be8722018-08-10 10:25:47 -07001167{
1168}
Nicolas Roulet40a44982017-02-03 13:39:57 -08001169
Eric Tan86be8722018-08-10 10:25:47 -07001170void NBLog::MergeReader::handleAuthor(const NBLog::AbstractEntry &entry, String8 *body)
1171{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001172 int author = entry.author();
Eric Tan86be8722018-08-10 10:25:47 -07001173 if (author == -1) {
1174 return;
1175 }
Glenn Kasten1c446272017-04-07 09:49:07 -07001176 // FIXME Needs a lock
Eric Tan5786e012018-08-15 09:03:47 -07001177 const char* name = mReaders[author]->name().c_str();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001178 body->appendFormat("%s: ", name);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001179}
1180
Glenn Kasten1c446272017-04-07 09:49:07 -07001181// ---------------------------------------------------------------------------
1182
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001183NBLog::MergeThread::MergeThread(NBLog::Merger &merger, NBLog::MergeReader &mergeReader)
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001184 : mMerger(merger),
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001185 mMergeReader(mergeReader),
Eric Tan86be8722018-08-10 10:25:47 -07001186 mTimeoutUs(0)
1187{
1188}
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001189
Eric Tan86be8722018-08-10 10:25:47 -07001190NBLog::MergeThread::~MergeThread()
1191{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001192 // set exit flag, set timeout to 0 to force threadLoop to exit and wait for the thread to join
1193 requestExit();
1194 setTimeoutUs(0);
1195 join();
1196}
1197
Eric Tan86be8722018-08-10 10:25:47 -07001198bool NBLog::MergeThread::threadLoop()
1199{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001200 bool doMerge;
1201 {
1202 AutoMutex _l(mMutex);
1203 // If mTimeoutUs is negative, wait on the condition variable until it's positive.
Eric Tan6af18472018-08-20 09:27:50 -07001204 // If it's positive, merge. The minimum period between waking the condition variable
1205 // is handled in AudioFlinger::MediaLogNotifier::threadLoop().
1206 mCond.wait(mMutex);
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001207 doMerge = mTimeoutUs > 0;
1208 mTimeoutUs -= kThreadSleepPeriodUs;
1209 }
1210 if (doMerge) {
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001211 // Merge data from all the readers
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001212 mMerger.merge();
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001213 // Process the data collected by mMerger and write it to PerformanceAnalysis
1214 // FIXME: decide whether to call getAndProcessSnapshot every time
1215 // or whether to have a separate thread that calls it with a lower frequency
1216 mMergeReader.getAndProcessSnapshot();
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001217 }
1218 return true;
1219}
1220
Eric Tan86be8722018-08-10 10:25:47 -07001221void NBLog::MergeThread::wakeup()
1222{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001223 setTimeoutUs(kThreadWakeupPeriodUs);
1224}
1225
Eric Tan86be8722018-08-10 10:25:47 -07001226void NBLog::MergeThread::setTimeoutUs(int time)
1227{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001228 AutoMutex _l(mMutex);
1229 mTimeoutUs = time;
1230 mCond.signal();
1231}
1232
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001233} // namespace android