blob: f1d7523974d3f7c9d8c72fc19a6264277ec7afb4 [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 Tan5786e012018-08-15 09:03:47 -070024#include <unordered_set>
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070025#include <vector>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080026#include <stdarg.h>
27#include <stdint.h>
28#include <stdio.h>
29#include <string.h>
Nicolas Rouletc20cb502017-02-01 12:35:24 -080030#include <sys/prctl.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080031#include <time.h>
32#include <new>
Glenn Kasten535e1612016-12-05 12:19:36 -080033#include <audio_utils/roundup.h>
Glenn Kasten8589ce72017-09-08 17:03:42 -070034#include <media/nblog/NBLog.h>
35#include <media/nblog/PerformanceAnalysis.h>
36#include <media/nblog/ReportPerformance.h>
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -070037#include <utils/CallStack.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080038#include <utils/Log.h>
Glenn Kasten4e01ef62013-07-11 14:29:59 -070039#include <utils/String8.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080040
Nicolas Roulet40a44982017-02-03 13:39:57 -080041#include <queue>
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070042#include <utility>
Nicolas Roulet40a44982017-02-03 13:39:57 -080043
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080044namespace android {
45
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070046int NBLog::Entry::copyEntryDataAt(size_t offset) const
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080047{
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070048 // FIXME This is too slow
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080049 if (offset == 0)
50 return mEvent;
51 else if (offset == 1)
52 return mLength;
53 else if (offset < (size_t) (mLength + 2))
54 return ((char *) mData)[offset - 2];
55 else if (offset == (size_t) (mLength + 2))
56 return mLength;
57 else
58 return 0;
59}
60
61// ---------------------------------------------------------------------------
62
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070063/*static*/
Eric Tan86be8722018-08-10 10:25:47 -070064std::unique_ptr<NBLog::AbstractEntry> NBLog::AbstractEntry::buildEntry(const uint8_t *ptr)
65{
Eric Tan5786e012018-08-15 09:03:47 -070066 if (ptr == nullptr) {
67 return nullptr;
68 }
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -070069 const uint8_t type = EntryIterator(ptr)->type;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070070 switch (type) {
71 case EVENT_START_FMT:
72 return std::make_unique<FormatEntry>(FormatEntry(ptr));
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -070073 case EVENT_AUDIO_STATE:
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070074 case EVENT_HISTOGRAM_ENTRY_TS:
75 return std::make_unique<HistogramEntry>(HistogramEntry(ptr));
76 default:
77 ALOGW("Tried to create AbstractEntry of type %d", type);
78 return nullptr;
79 }
Nicolas Roulet40a44982017-02-03 13:39:57 -080080}
81
Eric Tan86be8722018-08-10 10:25:47 -070082NBLog::AbstractEntry::AbstractEntry(const uint8_t *entry) : mEntry(entry)
83{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070084}
85
86// ---------------------------------------------------------------------------
Nicolas Rouletcd5dd012017-02-13 12:09:28 -080087
Eric Tan86be8722018-08-10 10:25:47 -070088NBLog::EntryIterator NBLog::FormatEntry::begin() const
89{
Sanna Catherine de Treville Wagerdd92d7e2017-05-15 14:56:53 -070090 return EntryIterator(mEntry);
91}
92
Eric Tan86be8722018-08-10 10:25:47 -070093const char *NBLog::FormatEntry::formatString() const
94{
Nicolas Rouletcd5dd012017-02-13 12:09:28 -080095 return (const char*) mEntry + offsetof(entry, data);
Nicolas Roulet40a44982017-02-03 13:39:57 -080096}
97
Eric Tan86be8722018-08-10 10:25:47 -070098size_t NBLog::FormatEntry::formatStringLength() const
99{
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800100 return mEntry[offsetof(entry, length)];
Nicolas Roulet40a44982017-02-03 13:39:57 -0800101}
102
Eric Tan86be8722018-08-10 10:25:47 -0700103NBLog::EntryIterator NBLog::FormatEntry::args() const
104{
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800105 auto it = begin();
Eric Tan86be8722018-08-10 10:25:47 -0700106 ++it; // skip start fmt
107 ++it; // skip timestamp
108 ++it; // skip hash
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700109 // Skip author if present
110 if (it->type == EVENT_AUTHOR) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800111 ++it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800112 }
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700113 return it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800114}
115
Eric Tan86be8722018-08-10 10:25:47 -0700116int64_t NBLog::FormatEntry::timestamp() const
117{
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800118 auto it = begin();
Eric Tan86be8722018-08-10 10:25:47 -0700119 ++it; // skip start fmt
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700120 return it.payload<int64_t>();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800121}
122
Eric Tan86be8722018-08-10 10:25:47 -0700123NBLog::log_hash_t NBLog::FormatEntry::hash() const
124{
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700125 auto it = begin();
Eric Tan86be8722018-08-10 10:25:47 -0700126 ++it; // skip start fmt
127 ++it; // skip timestamp
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700128 // unaligned 64-bit read not supported
129 log_hash_t hash;
130 memcpy(&hash, it->data, sizeof(hash));
131 return hash;
132}
133
Eric Tan86be8722018-08-10 10:25:47 -0700134int NBLog::FormatEntry::author() const
135{
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800136 auto it = begin();
Eric Tan86be8722018-08-10 10:25:47 -0700137 ++it; // skip start fmt
138 ++it; // skip timestamp
139 ++it; // skip hash
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700140 // if there is an author entry, return it, return -1 otherwise
Eric Tan5786e012018-08-15 09:03:47 -0700141 return it->type == EVENT_AUTHOR ? it.payload<int>() : -1;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800142}
143
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700144NBLog::EntryIterator NBLog::FormatEntry::copyWithAuthor(
Eric Tan86be8722018-08-10 10:25:47 -0700145 std::unique_ptr<audio_utils_fifo_writer> &dst, int author) const
146{
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800147 auto it = begin();
Eric Tan86be8722018-08-10 10:25:47 -0700148 it.copyTo(dst); // copy fmt start entry
149 (++it).copyTo(dst); // copy timestamp
150 (++it).copyTo(dst); // copy hash
Nicolas Roulet40a44982017-02-03 13:39:57 -0800151 // insert author entry
Eric Tan86be8722018-08-10 10:25:47 -0700152 size_t authorEntrySize = Entry::kOverhead + sizeof(author);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800153 uint8_t authorEntry[authorEntrySize];
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800154 authorEntry[offsetof(entry, type)] = EVENT_AUTHOR;
155 authorEntry[offsetof(entry, length)] =
Eric Tan86be8722018-08-10 10:25:47 -0700156 authorEntry[authorEntrySize + Entry::kPreviousLengthOffset] =
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800157 sizeof(author);
158 *(int*) (&authorEntry[offsetof(entry, data)]) = author;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800159 dst->write(authorEntry, authorEntrySize);
160 // copy rest of entries
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800161 while ((++it)->type != EVENT_END_FMT) {
162 it.copyTo(dst);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800163 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800164 it.copyTo(dst);
165 ++it;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800166 return it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800167}
168
Eric Tan86be8722018-08-10 10:25:47 -0700169void NBLog::EntryIterator::copyTo(std::unique_ptr<audio_utils_fifo_writer> &dst) const
170{
171 size_t length = mPtr[offsetof(entry, length)] + Entry::kOverhead;
172 dst->write(mPtr, length);
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800173}
Nicolas Roulet40a44982017-02-03 13:39:57 -0800174
Eric Tan86be8722018-08-10 10:25:47 -0700175void NBLog::EntryIterator::copyData(uint8_t *dst) const
176{
177 memcpy((void*) dst, mPtr + offsetof(entry, data), mPtr[offsetof(entry, length)]);
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800178}
179
Eric Tan86be8722018-08-10 10:25:47 -0700180NBLog::EntryIterator::EntryIterator() // Dummy initialization.
181 : mPtr(nullptr)
182{
183}
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800184
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700185NBLog::EntryIterator::EntryIterator(const uint8_t *entry)
Eric Tan86be8722018-08-10 10:25:47 -0700186 : mPtr(entry)
187{
188}
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800189
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700190NBLog::EntryIterator::EntryIterator(const NBLog::EntryIterator &other)
Eric Tan86be8722018-08-10 10:25:47 -0700191 : mPtr(other.mPtr)
192{
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800193}
194
Eric Tan86be8722018-08-10 10:25:47 -0700195const NBLog::entry& NBLog::EntryIterator::operator*() const
196{
197 return *(entry*) mPtr;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800198}
199
Eric Tan86be8722018-08-10 10:25:47 -0700200const NBLog::entry* NBLog::EntryIterator::operator->() const
201{
202 return (entry*) mPtr;
203}
204
205NBLog::EntryIterator& NBLog::EntryIterator::operator++()
206{
207 mPtr += mPtr[offsetof(entry, length)] + Entry::kOverhead;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800208 return *this;
209}
210
Eric Tan86be8722018-08-10 10:25:47 -0700211NBLog::EntryIterator& NBLog::EntryIterator::operator--()
212{
213 mPtr -= mPtr[Entry::kPreviousLengthOffset] + Entry::kOverhead;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800214 return *this;
215}
216
Eric Tan86be8722018-08-10 10:25:47 -0700217NBLog::EntryIterator NBLog::EntryIterator::next() const
218{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700219 EntryIterator aux(*this);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800220 return ++aux;
221}
222
Eric Tan86be8722018-08-10 10:25:47 -0700223NBLog::EntryIterator NBLog::EntryIterator::prev() const
224{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700225 EntryIterator aux(*this);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800226 return --aux;
227}
228
Eric Tan86be8722018-08-10 10:25:47 -0700229int NBLog::EntryIterator::operator-(const NBLog::EntryIterator &other) const
230{
231 return mPtr - other.mPtr;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800232}
233
Eric Tan86be8722018-08-10 10:25:47 -0700234bool NBLog::EntryIterator::operator!=(const EntryIterator &other) const
235{
236 return mPtr != other.mPtr;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800237}
238
Eric Tan86be8722018-08-10 10:25:47 -0700239bool NBLog::EntryIterator::hasConsistentLength() const
240{
241 return mPtr[offsetof(entry, length)] == mPtr[mPtr[offsetof(entry, length)] +
242 Entry::kOverhead + Entry::kPreviousLengthOffset];
Nicolas Roulet40a44982017-02-03 13:39:57 -0800243}
244
245// ---------------------------------------------------------------------------
246
Eric Tan86be8722018-08-10 10:25:47 -0700247int64_t NBLog::HistogramEntry::timestamp() const
248{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700249 return EntryIterator(mEntry).payload<HistTsEntry>().ts;
250}
251
Eric Tan86be8722018-08-10 10:25:47 -0700252NBLog::log_hash_t NBLog::HistogramEntry::hash() const
253{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700254 return EntryIterator(mEntry).payload<HistTsEntry>().hash;
255}
256
Eric Tan86be8722018-08-10 10:25:47 -0700257int NBLog::HistogramEntry::author() const
258{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700259 EntryIterator it(mEntry);
Eric Tan5786e012018-08-15 09:03:47 -0700260 return it->length == sizeof(HistTsEntryWithAuthor)
261 ? it.payload<HistTsEntryWithAuthor>().author : -1;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700262}
263
264NBLog::EntryIterator NBLog::HistogramEntry::copyWithAuthor(
Eric Tan86be8722018-08-10 10:25:47 -0700265 std::unique_ptr<audio_utils_fifo_writer> &dst, int author) const
266{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700267 // Current histogram entry has {type, length, struct HistTsEntry, length}.
268 // We now want {type, length, struct HistTsEntryWithAuthor, length}
269 uint8_t buffer[Entry::kOverhead + sizeof(HistTsEntryWithAuthor)];
270 // Copy content until the point we want to add the author
271 memcpy(buffer, mEntry, sizeof(entry) + sizeof(HistTsEntry));
272 // Copy the author
273 *(int*) (buffer + sizeof(entry) + sizeof(HistTsEntry)) = author;
274 // Update lengths
275 buffer[offsetof(entry, length)] = sizeof(HistTsEntryWithAuthor);
Ivan Lozano9ef855d2018-01-08 15:19:09 -0800276 buffer[offsetof(entry, data) + sizeof(HistTsEntryWithAuthor) + offsetof(ending, length)]
277 = sizeof(HistTsEntryWithAuthor);
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700278 // Write new buffer into FIFO
279 dst->write(buffer, sizeof(buffer));
280 return EntryIterator(mEntry).next();
281}
282
283// ---------------------------------------------------------------------------
284
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800285#if 0 // FIXME see note in NBLog.h
286NBLog::Timeline::Timeline(size_t size, void *shared)
287 : mSize(roundup(size)), mOwn(shared == NULL),
288 mShared((Shared *) (mOwn ? new char[sharedSize(size)] : shared))
289{
290 new (mShared) Shared;
291}
292
293NBLog::Timeline::~Timeline()
294{
295 mShared->~Shared();
296 if (mOwn) {
297 delete[] (char *) mShared;
298 }
299}
300#endif
301
302/*static*/
303size_t NBLog::Timeline::sharedSize(size_t size)
304{
Glenn Kastened99c2b2016-12-12 08:31:24 -0800305 // TODO fifo now supports non-power-of-2 buffer sizes, so could remove the roundup
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800306 return sizeof(Shared) + roundup(size);
307}
308
309// ---------------------------------------------------------------------------
310
311NBLog::Writer::Writer()
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800312 : mShared(NULL), mFifo(NULL), mFifoWriter(NULL), mEnabled(false), mPidTag(NULL), mPidTagSize(0)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800313{
314}
315
Glenn Kasten535e1612016-12-05 12:19:36 -0800316NBLog::Writer::Writer(void *shared, size_t size)
317 : mShared((Shared *) shared),
318 mFifo(mShared != NULL ?
319 new audio_utils_fifo(size, sizeof(uint8_t),
320 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
321 mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL),
322 mEnabled(mFifoWriter != NULL)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800323{
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800324 // caching pid and process name
325 pid_t id = ::getpid();
326 char procName[16];
327 int status = prctl(PR_GET_NAME, procName);
328 if (status) { // error getting process name
329 procName[0] = '\0';
330 }
331 size_t length = strlen(procName);
332 mPidTagSize = length + sizeof(pid_t);
333 mPidTag = new char[mPidTagSize];
334 memcpy(mPidTag, &id, sizeof(pid_t));
335 memcpy(mPidTag + sizeof(pid_t), procName, length);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800336}
337
Glenn Kasten535e1612016-12-05 12:19:36 -0800338NBLog::Writer::Writer(const sp<IMemory>& iMemory, size_t size)
339 : Writer(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800340{
Glenn Kasten535e1612016-12-05 12:19:36 -0800341 mIMemory = iMemory;
342}
343
344NBLog::Writer::~Writer()
345{
346 delete mFifoWriter;
347 delete mFifo;
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800348 delete[] mPidTag;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800349}
350
351void NBLog::Writer::log(const char *string)
352{
Eric Tan5786e012018-08-15 09:03:47 -0700353 if (!mEnabled) {
354 return;
355 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800356 LOG_ALWAYS_FATAL_IF(string == NULL, "Attempted to log NULL string");
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800357 size_t length = strlen(string);
Glenn Kasten535e1612016-12-05 12:19:36 -0800358 if (length > Entry::kMaxLength) {
359 length = Entry::kMaxLength;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800360 }
361 log(EVENT_STRING, string, length);
362}
363
364void NBLog::Writer::logf(const char *fmt, ...)
365{
Eric Tan5786e012018-08-15 09:03:47 -0700366 if (!mEnabled) {
367 return;
368 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800369 va_list ap;
370 va_start(ap, fmt);
371 Writer::logvf(fmt, ap); // the Writer:: is needed to avoid virtual dispatch for LockedWriter
372 va_end(ap);
373}
374
375void NBLog::Writer::logvf(const char *fmt, va_list ap)
376{
Eric Tan5786e012018-08-15 09:03:47 -0700377 if (!mEnabled) {
378 return;
379 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800380 char buffer[Entry::kMaxLength + 1 /*NUL*/];
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800381 int length = vsnprintf(buffer, sizeof(buffer), fmt, ap);
382 if (length >= (int) sizeof(buffer)) {
383 length = sizeof(buffer) - 1;
384 // NUL termination is not required
385 // buffer[length] = '\0';
386 }
387 if (length >= 0) {
388 log(EVENT_STRING, buffer, length);
389 }
390}
391
392void NBLog::Writer::logTimestamp()
393{
Eric Tan5786e012018-08-15 09:03:47 -0700394 if (!mEnabled) {
395 return;
396 }
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700397 int64_t ts = get_monotonic_ns();
398 if (ts > 0) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800399 log(EVENT_TIMESTAMP, &ts, sizeof(ts));
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700400 } else {
401 ALOGE("Failed to get timestamp");
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800402 }
403}
404
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700405void NBLog::Writer::logTimestamp(const int64_t ts)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800406{
Eric Tan5786e012018-08-15 09:03:47 -0700407 if (!mEnabled) {
408 return;
409 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800410 log(EVENT_TIMESTAMP, &ts, sizeof(ts));
411}
412
413void NBLog::Writer::logInteger(const int x)
414{
Eric Tan5786e012018-08-15 09:03:47 -0700415 if (!mEnabled) {
416 return;
417 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800418 log(EVENT_INTEGER, &x, sizeof(x));
419}
420
421void NBLog::Writer::logFloat(const float x)
422{
Eric Tan5786e012018-08-15 09:03:47 -0700423 if (!mEnabled) {
424 return;
425 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800426 log(EVENT_FLOAT, &x, sizeof(x));
427}
428
429void NBLog::Writer::logPID()
430{
Eric Tan5786e012018-08-15 09:03:47 -0700431 if (!mEnabled) {
432 return;
433 }
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800434 log(EVENT_PID, mPidTag, mPidTagSize);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800435}
436
437void NBLog::Writer::logStart(const char *fmt)
438{
Eric Tan5786e012018-08-15 09:03:47 -0700439 if (!mEnabled) {
440 return;
441 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800442 size_t length = strlen(fmt);
443 if (length > Entry::kMaxLength) {
444 length = Entry::kMaxLength;
445 }
446 log(EVENT_START_FMT, fmt, length);
447}
448
449void NBLog::Writer::logEnd()
450{
Eric Tan5786e012018-08-15 09:03:47 -0700451 if (!mEnabled) {
452 return;
453 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800454 Entry entry = Entry(EVENT_END_FMT, NULL, 0);
Eric Tan86be8722018-08-10 10:25:47 -0700455 log(entry, true);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800456}
457
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700458void NBLog::Writer::logHash(log_hash_t hash)
459{
Eric Tan5786e012018-08-15 09:03:47 -0700460 if (!mEnabled) {
461 return;
462 }
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700463 log(EVENT_HASH, &hash, sizeof(hash));
464}
465
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700466void NBLog::Writer::logEventHistTs(Event event, log_hash_t hash)
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700467{
Eric Tan5786e012018-08-15 09:03:47 -0700468 if (!mEnabled) {
469 return;
470 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700471 HistTsEntry data;
472 data.hash = hash;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700473 data.ts = get_monotonic_ns();
474 if (data.ts > 0) {
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700475 log(event, &data, sizeof(data));
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700476 } else {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700477 ALOGE("Failed to get timestamp");
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700478 }
479}
480
Eric Tane98dd6f2018-08-22 18:23:50 -0700481void NBLog::Writer::logLatency(double latencyMs)
482{
483 if (!mEnabled) {
484 return;
485 }
486 log(EVENT_LATENCY, &latencyMs, sizeof(latencyMs));
487}
488
Eric Tan5786e012018-08-15 09:03:47 -0700489void NBLog::Writer::logMonotonicCycleTime(uint32_t monotonicNs)
490{
491 if (!mEnabled) {
492 return;
493 }
Eric Tane98dd6f2018-08-22 18:23:50 -0700494 log(EVENT_MONOTONIC_CYCLE_TIME, &monotonicNs, sizeof(monotonicNs));
Eric Tan5786e012018-08-15 09:03:47 -0700495}
496
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700497void NBLog::Writer::logFormat(const char *fmt, log_hash_t hash, ...)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800498{
Eric Tan5786e012018-08-15 09:03:47 -0700499 if (!mEnabled) {
500 return;
501 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800502 va_list ap;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700503 va_start(ap, hash);
504 Writer::logVFormat(fmt, hash, ap);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800505 va_end(ap);
506}
507
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700508void NBLog::Writer::logVFormat(const char *fmt, log_hash_t hash, va_list argp)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800509{
Eric Tan5786e012018-08-15 09:03:47 -0700510 if (!mEnabled) {
511 return;
512 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800513 Writer::logStart(fmt);
514 int i;
515 double f;
516 char* s;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700517 int64_t t;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800518 Writer::logTimestamp();
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700519 Writer::logHash(hash);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800520 for (const char *p = fmt; *p != '\0'; p++) {
521 // TODO: implement more complex formatting such as %.3f
522 if (*p != '%') {
523 continue;
524 }
525 switch(*++p) {
526 case 's': // string
527 s = va_arg(argp, char *);
528 Writer::log(s);
529 break;
530
531 case 't': // timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700532 t = va_arg(argp, int64_t);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800533 Writer::logTimestamp(t);
534 break;
535
536 case 'd': // integer
537 i = va_arg(argp, int);
538 Writer::logInteger(i);
539 break;
540
541 case 'f': // float
542 f = va_arg(argp, double); // float arguments are promoted to double in vararg lists
543 Writer::logFloat((float)f);
544 break;
545
546 case 'p': // pid
547 Writer::logPID();
548 break;
549
550 // the "%\0" case finishes parsing
551 case '\0':
552 --p;
553 break;
554
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800555 case '%':
556 break;
557
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800558 default:
559 ALOGW("NBLog Writer parsed invalid format specifier: %c", *p);
560 break;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800561 }
562 }
563 Writer::logEnd();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800564}
565
566void NBLog::Writer::log(Event event, const void *data, size_t length)
567{
Eric Tan5786e012018-08-15 09:03:47 -0700568 if (!mEnabled) {
569 return;
570 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800571 if (data == NULL || length > Entry::kMaxLength) {
572 // TODO Perhaps it makes sense to display truncated data or at least a
573 // message that the data is too long? The current behavior can create
574 // a confusion for a programmer debugging their code.
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800575 return;
576 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700577 // Ignore if invalid event
578 if (event == EVENT_RESERVED || event >= EVENT_UPPER_BOUND) {
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800579 return;
580 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700581 Entry etr(event, data, length);
Eric Tan86be8722018-08-10 10:25:47 -0700582 log(etr, true /*trusted*/);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800583}
584
Eric Tan86be8722018-08-10 10:25:47 -0700585void NBLog::Writer::log(const NBLog::Entry &etr, bool trusted)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800586{
Eric Tan5786e012018-08-15 09:03:47 -0700587 if (!mEnabled) {
588 return;
589 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800590 if (!trusted) {
Eric Tan86be8722018-08-10 10:25:47 -0700591 log(etr.mEvent, etr.mData, etr.mLength);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800592 return;
593 }
Eric Tan5786e012018-08-15 09:03:47 -0700594 const size_t need = etr.mLength + Entry::kOverhead; // mEvent, mLength, data[mLength], mLength
595 // need = number of bytes written to FIFO
Glenn Kasten535e1612016-12-05 12:19:36 -0800596
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800597 // FIXME optimize this using memcpy for the data part of the Entry.
598 // The Entry could have a method copyTo(ptr, offset, size) to optimize the copy.
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700599 // checks size of a single log Entry: type, length, data pointer and ending
Glenn Kasten535e1612016-12-05 12:19:36 -0800600 uint8_t temp[Entry::kMaxLength + Entry::kOverhead];
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700601 // write this data to temp array
Glenn Kasten535e1612016-12-05 12:19:36 -0800602 for (size_t i = 0; i < need; i++) {
Eric Tan86be8722018-08-10 10:25:47 -0700603 temp[i] = etr.copyEntryDataAt(i);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800604 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700605 // write to circular buffer
Glenn Kasten535e1612016-12-05 12:19:36 -0800606 mFifoWriter->write(temp, need);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800607}
608
609bool NBLog::Writer::isEnabled() const
610{
611 return mEnabled;
612}
613
614bool NBLog::Writer::setEnabled(bool enabled)
615{
616 bool old = mEnabled;
617 mEnabled = enabled && mShared != NULL;
618 return old;
619}
620
621// ---------------------------------------------------------------------------
622
623NBLog::LockedWriter::LockedWriter()
624 : Writer()
625{
626}
627
Glenn Kasten535e1612016-12-05 12:19:36 -0800628NBLog::LockedWriter::LockedWriter(void *shared, size_t size)
629 : Writer(shared, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800630{
631}
632
633void NBLog::LockedWriter::log(const char *string)
634{
635 Mutex::Autolock _l(mLock);
636 Writer::log(string);
637}
638
639void NBLog::LockedWriter::logf(const char *fmt, ...)
640{
641 // FIXME should not take the lock until after formatting is done
642 Mutex::Autolock _l(mLock);
643 va_list ap;
644 va_start(ap, fmt);
645 Writer::logvf(fmt, ap);
646 va_end(ap);
647}
648
649void NBLog::LockedWriter::logvf(const char *fmt, va_list ap)
650{
651 // FIXME should not take the lock until after formatting is done
652 Mutex::Autolock _l(mLock);
653 Writer::logvf(fmt, ap);
654}
655
656void NBLog::LockedWriter::logTimestamp()
657{
658 // FIXME should not take the lock until after the clock_gettime() syscall
659 Mutex::Autolock _l(mLock);
660 Writer::logTimestamp();
661}
662
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700663void NBLog::LockedWriter::logTimestamp(const int64_t ts)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800664{
665 Mutex::Autolock _l(mLock);
666 Writer::logTimestamp(ts);
667}
668
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800669void NBLog::LockedWriter::logInteger(const int x)
670{
671 Mutex::Autolock _l(mLock);
672 Writer::logInteger(x);
673}
674
675void NBLog::LockedWriter::logFloat(const float x)
676{
677 Mutex::Autolock _l(mLock);
678 Writer::logFloat(x);
679}
680
681void NBLog::LockedWriter::logPID()
682{
683 Mutex::Autolock _l(mLock);
684 Writer::logPID();
685}
686
687void NBLog::LockedWriter::logStart(const char *fmt)
688{
689 Mutex::Autolock _l(mLock);
690 Writer::logStart(fmt);
691}
692
693
694void NBLog::LockedWriter::logEnd()
695{
696 Mutex::Autolock _l(mLock);
697 Writer::logEnd();
698}
699
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700700void NBLog::LockedWriter::logHash(log_hash_t hash)
701{
702 Mutex::Autolock _l(mLock);
703 Writer::logHash(hash);
704}
705
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800706bool NBLog::LockedWriter::isEnabled() const
707{
708 Mutex::Autolock _l(mLock);
709 return Writer::isEnabled();
710}
711
712bool NBLog::LockedWriter::setEnabled(bool enabled)
713{
714 Mutex::Autolock _l(mLock);
715 return Writer::setEnabled(enabled);
716}
717
718// ---------------------------------------------------------------------------
719
Eric Tan5786e012018-08-15 09:03:47 -0700720const std::unordered_set<NBLog::Event> NBLog::Reader::startingTypes {
721 NBLog::Event::EVENT_START_FMT,
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700722 NBLog::Event::EVENT_HISTOGRAM_ENTRY_TS,
Eric Tan5786e012018-08-15 09:03:47 -0700723 NBLog::Event::EVENT_AUDIO_STATE,
Eric Tane98dd6f2018-08-22 18:23:50 -0700724 NBLog::Event::EVENT_LATENCY,
Eric Tan5786e012018-08-15 09:03:47 -0700725 NBLog::Event::EVENT_MONOTONIC_CYCLE_TIME
726};
727const std::unordered_set<NBLog::Event> NBLog::Reader::endingTypes {
728 NBLog::Event::EVENT_END_FMT,
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700729 NBLog::Event::EVENT_HISTOGRAM_ENTRY_TS,
Eric Tan5786e012018-08-15 09:03:47 -0700730 NBLog::Event::EVENT_AUDIO_STATE,
Eric Tane98dd6f2018-08-22 18:23:50 -0700731 NBLog::Event::EVENT_LATENCY,
Eric Tan5786e012018-08-15 09:03:47 -0700732 NBLog::Event::EVENT_MONOTONIC_CYCLE_TIME
733};
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700734
Eric Tan5786e012018-08-15 09:03:47 -0700735NBLog::Reader::Reader(const void *shared, size_t size, const std::string &name)
Eric Tan6af18472018-08-20 09:27:50 -0700736 : mName(name),
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700737 mShared((/*const*/ Shared *) shared), /*mIMemory*/
Glenn Kasten535e1612016-12-05 12:19:36 -0800738 mFifo(mShared != NULL ?
739 new audio_utils_fifo(size, sizeof(uint8_t),
740 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700741 mFifoReader(mFifo != NULL ? new audio_utils_fifo_reader(*mFifo) : NULL)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800742{
743}
744
Eric Tan5786e012018-08-15 09:03:47 -0700745NBLog::Reader::Reader(const sp<IMemory>& iMemory, size_t size, const std::string &name)
746 : Reader(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size, name)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800747{
Glenn Kasten535e1612016-12-05 12:19:36 -0800748 mIMemory = iMemory;
749}
750
751NBLog::Reader::~Reader()
752{
753 delete mFifoReader;
754 delete mFifo;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800755}
756
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700757const uint8_t *NBLog::Reader::findLastEntryOfTypes(const uint8_t *front, const uint8_t *back,
Eric Tan5786e012018-08-15 09:03:47 -0700758 const std::unordered_set<Event> &types) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800759 while (back + Entry::kPreviousLengthOffset >= front) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700760 const uint8_t *prev = back - back[Entry::kPreviousLengthOffset] - Entry::kOverhead;
761 if (prev < front || prev + prev[offsetof(entry, length)] +
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800762 Entry::kOverhead != back) {
763
764 // prev points to an out of limits or inconsistent entry
765 return nullptr;
766 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700767 if (types.find((const Event) prev[offsetof(entry, type)]) != types.end()) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800768 return prev;
769 }
770 back = prev;
771 }
772 return nullptr; // no entry found
773}
774
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700775// Copies content of a Reader FIFO into its Snapshot
776// The Snapshot has the same raw data, but represented as a sequence of entries
777// and an EntryIterator making it possible to process the data.
Eric Tan6af18472018-08-20 09:27:50 -0700778std::unique_ptr<NBLog::Snapshot> NBLog::Reader::getSnapshot()
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800779{
Glenn Kasten535e1612016-12-05 12:19:36 -0800780 if (mFifoReader == NULL) {
Eric Tan6af18472018-08-20 09:27:50 -0700781 return std::make_unique<Snapshot>();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800782 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800783
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800784 // This emulates the behaviour of audio_utils_fifo_reader::read, but without incrementing the
785 // reader index. The index is incremented after handling corruption, to after the last complete
786 // entry of the buffer
Eric Tan5786e012018-08-15 09:03:47 -0700787 size_t lost = 0;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800788 audio_utils_iovec iovec[2];
Eric Tan5786e012018-08-15 09:03:47 -0700789 const size_t capacity = mFifo->capacity();
790 ssize_t availToRead;
791 // A call to audio_utils_fifo_reader::obtain() places the read pointer one buffer length
792 // before the writer's pointer (since mFifoReader was constructed with flush=false). The
793 // do while loop is an attempt to read all of the FIFO's contents regardless of how behind
794 // the reader is with respect to the writer. However, the following scheduling sequence is
795 // possible and can lead to a starvation situation:
796 // - Writer T1 writes, overrun with respect to Reader T2
797 // - T2 calls obtain() and gets EOVERFLOW, T2 ptr placed one buffer size behind T1 ptr
798 // - T1 write, overrun
799 // - T2 obtain(), EOVERFLOW (and so on...)
800 // To address this issue, we limit the number of tries for the reader to catch up with
801 // the writer.
802 int tries = 0;
803 size_t lostTemp;
804 do {
805 availToRead = mFifoReader->obtain(iovec, capacity, NULL /*timeout*/, &lostTemp);
806 lost += lostTemp;
807 } while (availToRead < 0 || ++tries <= kMaxObtainTries);
808
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800809 if (availToRead <= 0) {
Eric Tan5786e012018-08-15 09:03:47 -0700810 ALOGW_IF(availToRead < 0, "NBLog Reader %s failed to catch up with Writer", mName.c_str());
Eric Tan6af18472018-08-20 09:27:50 -0700811 return std::make_unique<Snapshot>();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800812 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800813
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800814 std::unique_ptr<Snapshot> snapshot(new Snapshot(availToRead));
815 memcpy(snapshot->mData, (const char *) mFifo->buffer() + iovec[0].mOffset, iovec[0].mLength);
816 if (iovec[1].mLength > 0) {
817 memcpy(snapshot->mData + (iovec[0].mLength),
Eric Tan5786e012018-08-15 09:03:47 -0700818 (const char *) mFifo->buffer() + iovec[1].mOffset, iovec[1].mLength);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800819 }
820
821 // Handle corrupted buffer
822 // Potentially, a buffer has corrupted data on both beginning (due to overflow) and end
823 // (due to incomplete format entry). But even if the end format entry is incomplete,
824 // it ends in a complete entry (which is not an END_FMT). So is safe to traverse backwards.
825 // TODO: handle client corruption (in the middle of a buffer)
826
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700827 const uint8_t *back = snapshot->mData + availToRead;
828 const uint8_t *front = snapshot->mData;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800829
830 // Find last END_FMT. <back> is sitting on an entry which might be the middle of a FormatEntry.
831 // We go backwards until we find an EVENT_END_FMT.
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700832 const uint8_t *lastEnd = findLastEntryOfTypes(front, back, endingTypes);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800833 if (lastEnd == nullptr) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700834 snapshot->mEnd = snapshot->mBegin = EntryIterator(front);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800835 } else {
836 // end of snapshot points to after last END_FMT entry
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700837 snapshot->mEnd = EntryIterator(lastEnd).next();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800838 // find first START_FMT
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700839 const uint8_t *firstStart = nullptr;
840 const uint8_t *firstStartTmp = snapshot->mEnd;
841 while ((firstStartTmp = findLastEntryOfTypes(front, firstStartTmp, startingTypes))
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800842 != nullptr) {
843 firstStart = firstStartTmp;
844 }
845 // firstStart is null if no START_FMT entry was found before lastEnd
846 if (firstStart == nullptr) {
847 snapshot->mBegin = snapshot->mEnd;
848 } else {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700849 snapshot->mBegin = EntryIterator(firstStart);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800850 }
851 }
852
853 // advance fifo reader index to after last entry read.
854 mFifoReader->release(snapshot->mEnd - front);
855
856 snapshot->mLost = lost;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800857 return snapshot;
858}
859
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700860// Takes raw content of the local merger FIFO, processes log entries, and
861// writes the data to a map of class PerformanceAnalysis, based on their thread ID.
Eric Tan6af18472018-08-20 09:27:50 -0700862void NBLog::MergeReader::getAndProcessSnapshot(NBLog::Snapshot &snapshot, int author)
Nicolas Roulet40a44982017-02-03 13:39:57 -0800863{
Eric Tan6af18472018-08-20 09:27:50 -0700864 for (const entry &etr : snapshot) {
865 switch (etr.type) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700866 case EVENT_HISTOGRAM_ENTRY_TS: {
Eric Tan6af18472018-08-20 09:27:50 -0700867 HistTsEntry *data = (HistTsEntry *) (etr.data);
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700868 // TODO This memcpies are here to avoid unaligned memory access crash.
869 // There's probably a more efficient way to do it
870 log_hash_t hash;
871 memcpy(&hash, &(data->hash), sizeof(hash));
Nicolas Rouletad82aa62017-04-03 19:15:20 -0700872 int64_t ts;
873 memcpy(&ts, &data->ts, sizeof(ts));
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700874 // TODO: hash for histogram ts and audio state need to match
875 // and correspond to audio production source file location
Eric Tan6af18472018-08-20 09:27:50 -0700876 mThreadPerformanceAnalysis[author][0 /*hash*/].logTsEntry(ts);
877 } break;
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700878 case EVENT_AUDIO_STATE: {
Eric Tan6af18472018-08-20 09:27:50 -0700879 HistTsEntry *data = (HistTsEntry *) (etr.data);
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700880 // TODO This memcpies are here to avoid unaligned memory access crash.
881 // There's probably a more efficient way to do it
Sanna Catherine de Treville Wagerd0965172017-07-24 13:42:44 -0700882 log_hash_t hash;
883 memcpy(&hash, &(data->hash), sizeof(hash));
Eric Tan6af18472018-08-20 09:27:50 -0700884 mThreadPerformanceAnalysis[author][0 /*hash*/].handleStateChange();
885 } break;
Eric Tane98dd6f2018-08-22 18:23:50 -0700886 case EVENT_LATENCY: {
887 double latencyMs;
888 memcpy(&latencyMs, etr.data, sizeof(latencyMs));
889 mPerformanceData.addLatencyEntry(author, latencyMs);
890 } break;
891 case EVENT_MONOTONIC_CYCLE_TIME: {
892 uint32_t monotonicNs;
893 memcpy(&monotonicNs, etr.data, sizeof(monotonicNs));
894 const double monotonicMs = monotonicNs * 1e-6;
895 mPerformanceData.addCycleTimeEntry(author, monotonicMs);
896 } break;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800897 case EVENT_END_FMT:
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800898 case EVENT_RESERVED:
Eric Tan6af18472018-08-20 09:27:50 -0700899 case EVENT_UPPER_BOUND:
900 ALOGW("warning: unexpected event %d", etr.type);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800901 default:
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800902 break;
903 }
Sanna Catherine de Treville Wager9484bae2017-06-15 14:39:44 -0700904 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800905}
906
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700907void NBLog::MergeReader::getAndProcessSnapshot()
Nicolas Roulet40a44982017-02-03 13:39:57 -0800908{
Eric Tan6af18472018-08-20 09:27:50 -0700909 // get a snapshot of each reader and process them
910 // TODO insert lock here
911 const size_t nLogs = mReaders.size();
912 std::vector<std::unique_ptr<Snapshot>> snapshots(nLogs);
913 for (size_t i = 0; i < nLogs; i++) {
914 snapshots[i] = mReaders[i]->getSnapshot();
915 }
916 // TODO unlock lock here
917 for (size_t i = 0; i < nLogs; i++) {
918 if (snapshots[i] != nullptr) {
919 getAndProcessSnapshot(*(snapshots[i]), i);
920 }
921 }
Nicolas Roulet40a44982017-02-03 13:39:57 -0800922}
923
Eric Tan86be8722018-08-10 10:25:47 -0700924void NBLog::MergeReader::dump(int fd, int indent)
925{
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700926 // TODO: add a mutex around media.log dump
Sanna Catherine de Treville Wagercf6c75a2017-07-21 17:05:25 -0700927 ReportPerformance::dump(fd, indent, mThreadPerformanceAnalysis);
Eric Tane98dd6f2018-08-22 18:23:50 -0700928 mPerformanceData.dump(fd);
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700929}
930
Eric Tan6af18472018-08-20 09:27:50 -0700931// TODO for future compatibility, would prefer to have a dump() go to string, and then go
932// to fd only when invoked through binder.
933void NBLog::DumpReader::dump(int fd, size_t indent)
Eric Tan5786e012018-08-15 09:03:47 -0700934{
Eric Tan6af18472018-08-20 09:27:50 -0700935 if (fd < 0) return;
936 std::unique_ptr<Snapshot> snapshot = getSnapshot();
937 if (snapshot == nullptr) {
Eric Tan5786e012018-08-15 09:03:47 -0700938 return;
939 }
Eric Tan6af18472018-08-20 09:27:50 -0700940 String8 timestamp, body;
941
942 // TODO all logged types should have a printable format.
943 for (auto it = snapshot->begin(); it != snapshot->end(); ++it) {
944 switch (it->type) {
945 case EVENT_START_FMT:
946 it = handleFormat(FormatEntry(it), &timestamp, &body);
947 break;
948 case EVENT_MONOTONIC_CYCLE_TIME: {
949 uint32_t monotonicNs;
950 memcpy(&monotonicNs, it->data, sizeof(monotonicNs));
Eric Tane98dd6f2018-08-22 18:23:50 -0700951 body.appendFormat("Thread cycle: %u ns", monotonicNs);
952 } break;
953 case EVENT_LATENCY: {
954 double latencyMs;
955 memcpy(&latencyMs, it->data, sizeof(latencyMs));
956 body.appendFormat("latency: %.3f ms", latencyMs);
Eric Tan6af18472018-08-20 09:27:50 -0700957 } break;
958 case EVENT_END_FMT:
959 case EVENT_RESERVED:
960 case EVENT_UPPER_BOUND:
961 body.appendFormat("warning: unexpected event %d", it->type);
962 default:
963 break;
964 }
965 if (!body.isEmpty()) {
966 dprintf(fd, "%.*s%s %s\n", (int)indent, "", timestamp.string(), body.string());
967 body.clear();
968 }
969 timestamp.clear();
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700970 }
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700971}
972
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800973bool NBLog::Reader::isIMemory(const sp<IMemory>& iMemory) const
974{
Glenn Kasten481fb672013-09-30 14:39:28 -0700975 return iMemory != 0 && mIMemory != 0 && iMemory->pointer() == mIMemory->pointer();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800976}
977
Eric Tan6af18472018-08-20 09:27:50 -0700978void NBLog::DumpReader::appendTimestamp(String8 *body, const void *data)
Eric Tan86be8722018-08-10 10:25:47 -0700979{
Eric Tan5786e012018-08-15 09:03:47 -0700980 if (body == nullptr || data == nullptr) {
981 return;
982 }
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700983 int64_t ts;
984 memcpy(&ts, data, sizeof(ts));
985 body->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
986 (int) ((ts / (1000 * 1000)) % 1000));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800987}
988
Eric Tan6af18472018-08-20 09:27:50 -0700989void NBLog::DumpReader::appendInt(String8 *body, const void *data)
Eric Tan86be8722018-08-10 10:25:47 -0700990{
Eric Tan5786e012018-08-15 09:03:47 -0700991 if (body == nullptr || data == nullptr) {
992 return;
993 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800994 int x = *((int*) data);
995 body->appendFormat("<%d>", x);
996}
997
Eric Tan6af18472018-08-20 09:27:50 -0700998void NBLog::DumpReader::appendFloat(String8 *body, const void *data)
Eric Tan86be8722018-08-10 10:25:47 -0700999{
Eric Tan5786e012018-08-15 09:03:47 -07001000 if (body == nullptr || data == nullptr) {
1001 return;
1002 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001003 float f;
Eric Tan86be8722018-08-10 10:25:47 -07001004 memcpy(&f, data, sizeof(f));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001005 body->appendFormat("<%f>", f);
1006}
1007
Eric Tan6af18472018-08-20 09:27:50 -07001008void NBLog::DumpReader::appendPID(String8 *body, const void* data, size_t length)
Eric Tan86be8722018-08-10 10:25:47 -07001009{
Eric Tan5786e012018-08-15 09:03:47 -07001010 if (body == nullptr || data == nullptr) {
1011 return;
1012 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001013 pid_t id = *((pid_t*) data);
Nicolas Rouletc20cb502017-02-01 12:35:24 -08001014 char * name = &((char*) data)[sizeof(pid_t)];
1015 body->appendFormat("<PID: %d, name: %.*s>", id, (int) (length - sizeof(pid_t)), name);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001016}
1017
Eric Tan6af18472018-08-20 09:27:50 -07001018String8 NBLog::DumpReader::bufferDump(const uint8_t *buffer, size_t size)
Nicolas Roulet2aedf372017-03-29 11:27:03 -07001019{
1020 String8 str;
Eric Tan5786e012018-08-15 09:03:47 -07001021 if (buffer == nullptr) {
1022 return str;
1023 }
Nicolas Roulet2aedf372017-03-29 11:27:03 -07001024 str.append("[ ");
Eric Tan86be8722018-08-10 10:25:47 -07001025 for(size_t i = 0; i < size; i++) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001026 str.appendFormat("%d ", buffer[i]);
Nicolas Roulet2aedf372017-03-29 11:27:03 -07001027 }
1028 str.append("]");
1029 return str;
1030}
1031
Eric Tan6af18472018-08-20 09:27:50 -07001032String8 NBLog::DumpReader::bufferDump(const EntryIterator &it)
Nicolas Roulet2aedf372017-03-29 11:27:03 -07001033{
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001034 return bufferDump(it, it->length + Entry::kOverhead);
Nicolas Roulet2aedf372017-03-29 11:27:03 -07001035}
1036
Eric Tan6af18472018-08-20 09:27:50 -07001037NBLog::EntryIterator NBLog::DumpReader::handleFormat(const FormatEntry &fmtEntry,
1038 String8 *timestamp,
1039 String8 *body)
Eric Tan86be8722018-08-10 10:25:47 -07001040{
Nicolas Roulet40a44982017-02-03 13:39:57 -08001041 // log timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001042 int64_t ts = fmtEntry.timestamp();
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001043 timestamp->clear();
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001044 timestamp->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
1045 (int) ((ts / (1000 * 1000)) % 1000));
Nicolas Roulet40a44982017-02-03 13:39:57 -08001046
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -07001047 // log unique hash
1048 log_hash_t hash = fmtEntry.hash();
1049 // print only lower 16bit of hash as hex and line as int to reduce spam in the log
1050 body->appendFormat("%.4X-%d ", (int)(hash >> 16) & 0xFFFF, (int) hash & 0xFFFF);
1051
Nicolas Roulet40a44982017-02-03 13:39:57 -08001052 // log author (if present)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001053 handleAuthor(fmtEntry, body);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001054
1055 // log string
Eric Tan86be8722018-08-10 10:25:47 -07001056 EntryIterator arg = fmtEntry.args();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001057
1058 const char* fmt = fmtEntry.formatString();
1059 size_t fmt_length = fmtEntry.formatStringLength();
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001060
1061 for (size_t fmt_offset = 0; fmt_offset < fmt_length; ++fmt_offset) {
1062 if (fmt[fmt_offset] != '%') {
1063 body->append(&fmt[fmt_offset], 1); // TODO optimize to write consecutive strings at once
1064 continue;
1065 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001066 // case "%%""
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001067 if (fmt[++fmt_offset] == '%') {
1068 body->append("%");
1069 continue;
1070 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001071 // case "%\0"
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001072 if (fmt_offset == fmt_length) {
1073 continue;
1074 }
1075
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001076 NBLog::Event event = (NBLog::Event) arg->type;
1077 size_t length = arg->length;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001078
1079 // TODO check length for event type is correct
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001080
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001081 if (event == EVENT_END_FMT) {
1082 break;
1083 }
1084
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001085 // TODO: implement more complex formatting such as %.3f
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001086 const uint8_t *datum = arg->data; // pointer to the current event args
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001087 switch(fmt[fmt_offset])
1088 {
1089 case 's': // string
Nicolas Roulet4da78202017-02-03 12:53:39 -08001090 ALOGW_IF(event != EVENT_STRING,
1091 "NBLog Reader incompatible event for string specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001092 body->append((const char*) datum, length);
1093 break;
1094
1095 case 't': // timestamp
Nicolas Roulet4da78202017-02-03 12:53:39 -08001096 ALOGW_IF(event != EVENT_TIMESTAMP,
1097 "NBLog Reader incompatible event for timestamp specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001098 appendTimestamp(body, datum);
1099 break;
1100
1101 case 'd': // integer
Nicolas Roulet4da78202017-02-03 12:53:39 -08001102 ALOGW_IF(event != EVENT_INTEGER,
1103 "NBLog Reader incompatible event for integer specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001104 appendInt(body, datum);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001105 break;
1106
1107 case 'f': // float
Nicolas Roulet4da78202017-02-03 12:53:39 -08001108 ALOGW_IF(event != EVENT_FLOAT,
1109 "NBLog Reader incompatible event for float specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001110 appendFloat(body, datum);
1111 break;
1112
1113 case 'p': // pid
Nicolas Roulet4da78202017-02-03 12:53:39 -08001114 ALOGW_IF(event != EVENT_PID,
1115 "NBLog Reader incompatible event for pid specifier: %d", event);
Nicolas Rouletc20cb502017-02-01 12:35:24 -08001116 appendPID(body, datum, length);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001117 break;
1118
1119 default:
1120 ALOGW("NBLog Reader encountered unknown character %c", fmt[fmt_offset]);
1121 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001122 ++arg;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001123 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001124 ALOGW_IF(arg->type != EVENT_END_FMT, "Expected end of format, got %d", arg->type);
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001125 return arg;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001126}
1127
Nicolas Roulet40a44982017-02-03 13:39:57 -08001128NBLog::Merger::Merger(const void *shared, size_t size):
Nicolas Roulet40a44982017-02-03 13:39:57 -08001129 mShared((Shared *) shared),
1130 mFifo(mShared != NULL ?
1131 new audio_utils_fifo(size, sizeof(uint8_t),
1132 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
1133 mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL)
Eric Tan86be8722018-08-10 10:25:47 -07001134{
1135}
Nicolas Roulet40a44982017-02-03 13:39:57 -08001136
Eric Tan5786e012018-08-15 09:03:47 -07001137void NBLog::Merger::addReader(const sp<NBLog::Reader> &reader)
Eric Tan86be8722018-08-10 10:25:47 -07001138{
Glenn Kasten1c446272017-04-07 09:49:07 -07001139 // FIXME This is called by binder thread in MediaLogService::registerWriter
Eric Tan5786e012018-08-15 09:03:47 -07001140 // but the access to shared variable mReaders is not yet protected by a lock.
1141 mReaders.push_back(reader);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001142}
1143
1144// items placed in priority queue during merge
1145// composed by a timestamp and the index of the snapshot where the timestamp came from
1146struct MergeItem
1147{
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001148 int64_t ts;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001149 int index;
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001150 MergeItem(int64_t ts, int index): ts(ts), index(index) {}
Nicolas Roulet40a44982017-02-03 13:39:57 -08001151};
1152
Eric Tan86be8722018-08-10 10:25:47 -07001153bool operator>(const struct MergeItem &i1, const struct MergeItem &i2)
1154{
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001155 return i1.ts > i2.ts || (i1.ts == i2.ts && i1.index > i2.index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001156}
1157
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001158// Merge registered readers, sorted by timestamp, and write data to a single FIFO in local memory
Eric Tan86be8722018-08-10 10:25:47 -07001159void NBLog::Merger::merge()
1160{
Eric Tan6af18472018-08-20 09:27:50 -07001161 if (true) return; // Merging is not necessary at the moment, so this is to disable it
1162 // and bypass compiler warnings about member variables not being used.
Eric Tan5786e012018-08-15 09:03:47 -07001163 const int nLogs = mReaders.size();
Eric Tan6af18472018-08-20 09:27:50 -07001164 std::vector<std::unique_ptr<Snapshot>> snapshots(nLogs);
Eric Tan86be8722018-08-10 10:25:47 -07001165 std::vector<EntryIterator> offsets;
1166 offsets.reserve(nLogs);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001167 for (int i = 0; i < nLogs; ++i) {
Eric Tan5786e012018-08-15 09:03:47 -07001168 snapshots[i] = mReaders[i]->getSnapshot();
Eric Tan86be8722018-08-10 10:25:47 -07001169 offsets.push_back(snapshots[i]->begin());
Nicolas Roulet40a44982017-02-03 13:39:57 -08001170 }
1171 // initialize offsets
Nicolas Roulet40a44982017-02-03 13:39:57 -08001172 // TODO custom heap implementation could allow to update top, improving performance
1173 // for bursty buffers
1174 std::priority_queue<MergeItem, std::vector<MergeItem>, std::greater<MergeItem>> timestamps;
1175 for (int i = 0; i < nLogs; ++i)
1176 {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001177 if (offsets[i] != snapshots[i]->end()) {
Eric Tan86be8722018-08-10 10:25:47 -07001178 std::unique_ptr<AbstractEntry> abstractEntry = AbstractEntry::buildEntry(offsets[i]);
1179 if (abstractEntry == nullptr) {
1180 continue;
1181 }
1182 timestamps.emplace(abstractEntry->timestamp(), i);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001183 }
1184 }
1185
1186 while (!timestamps.empty()) {
Eric Tan86be8722018-08-10 10:25:47 -07001187 int index = timestamps.top().index; // find minimum timestamp
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001188 // copy it to the log, increasing offset
Eric Tan86be8722018-08-10 10:25:47 -07001189 offsets[index] = AbstractEntry::buildEntry(offsets[index])->
1190 copyWithAuthor(mFifoWriter, index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001191 // update data structures
Nicolas Roulet40a44982017-02-03 13:39:57 -08001192 timestamps.pop();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001193 if (offsets[index] != snapshots[index]->end()) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001194 int64_t ts = AbstractEntry::buildEntry(offsets[index])->timestamp();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001195 timestamps.emplace(ts, index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001196 }
1197 }
1198}
1199
Eric Tan5786e012018-08-15 09:03:47 -07001200const std::vector<sp<NBLog::Reader>>& NBLog::Merger::getReaders() const
Eric Tan86be8722018-08-10 10:25:47 -07001201{
Eric Tan5786e012018-08-15 09:03:47 -07001202 //AutoMutex _l(mLock);
1203 return mReaders;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001204}
1205
Glenn Kasten1c446272017-04-07 09:49:07 -07001206// ---------------------------------------------------------------------------
1207
Nicolas Roulet40a44982017-02-03 13:39:57 -08001208NBLog::MergeReader::MergeReader(const void *shared, size_t size, Merger &merger)
Eric Tan5786e012018-08-15 09:03:47 -07001209 : Reader(shared, size, "MergeReader"), mReaders(merger.getReaders())
Eric Tan86be8722018-08-10 10:25:47 -07001210{
1211}
Nicolas Roulet40a44982017-02-03 13:39:57 -08001212
Eric Tan86be8722018-08-10 10:25:47 -07001213void NBLog::MergeReader::handleAuthor(const NBLog::AbstractEntry &entry, String8 *body)
1214{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001215 int author = entry.author();
Eric Tan86be8722018-08-10 10:25:47 -07001216 if (author == -1) {
1217 return;
1218 }
Glenn Kasten1c446272017-04-07 09:49:07 -07001219 // FIXME Needs a lock
Eric Tan5786e012018-08-15 09:03:47 -07001220 const char* name = mReaders[author]->name().c_str();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001221 body->appendFormat("%s: ", name);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001222}
1223
Glenn Kasten1c446272017-04-07 09:49:07 -07001224// ---------------------------------------------------------------------------
1225
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001226NBLog::MergeThread::MergeThread(NBLog::Merger &merger, NBLog::MergeReader &mergeReader)
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001227 : mMerger(merger),
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001228 mMergeReader(mergeReader),
Eric Tan86be8722018-08-10 10:25:47 -07001229 mTimeoutUs(0)
1230{
1231}
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001232
Eric Tan86be8722018-08-10 10:25:47 -07001233NBLog::MergeThread::~MergeThread()
1234{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001235 // set exit flag, set timeout to 0 to force threadLoop to exit and wait for the thread to join
1236 requestExit();
1237 setTimeoutUs(0);
1238 join();
1239}
1240
Eric Tan86be8722018-08-10 10:25:47 -07001241bool NBLog::MergeThread::threadLoop()
1242{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001243 bool doMerge;
1244 {
1245 AutoMutex _l(mMutex);
1246 // If mTimeoutUs is negative, wait on the condition variable until it's positive.
Eric Tan6af18472018-08-20 09:27:50 -07001247 // If it's positive, merge. The minimum period between waking the condition variable
1248 // is handled in AudioFlinger::MediaLogNotifier::threadLoop().
1249 mCond.wait(mMutex);
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001250 doMerge = mTimeoutUs > 0;
1251 mTimeoutUs -= kThreadSleepPeriodUs;
1252 }
1253 if (doMerge) {
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001254 // Merge data from all the readers
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001255 mMerger.merge();
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001256 // Process the data collected by mMerger and write it to PerformanceAnalysis
1257 // FIXME: decide whether to call getAndProcessSnapshot every time
1258 // or whether to have a separate thread that calls it with a lower frequency
1259 mMergeReader.getAndProcessSnapshot();
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001260 }
1261 return true;
1262}
1263
Eric Tan86be8722018-08-10 10:25:47 -07001264void NBLog::MergeThread::wakeup()
1265{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001266 setTimeoutUs(kThreadWakeupPeriodUs);
1267}
1268
Eric Tan86be8722018-08-10 10:25:47 -07001269void NBLog::MergeThread::setTimeoutUs(int time)
1270{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001271 AutoMutex _l(mMutex);
1272 mTimeoutUs = time;
1273 mCond.signal();
1274}
1275
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001276} // namespace android