blob: d659445d0f1847342f2130579a91e4cf35c7cf6b [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 Tan5786e012018-08-15 09:03:47 -0700481void NBLog::Writer::logMonotonicCycleTime(uint32_t monotonicNs)
482{
483 if (!mEnabled) {
484 return;
485 }
486 log(EVENT_MONOTONIC_CYCLE_TIME, &monotonicNs, sizeof(&monotonicNs));
487}
488
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700489void NBLog::Writer::logFormat(const char *fmt, log_hash_t hash, ...)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800490{
Eric Tan5786e012018-08-15 09:03:47 -0700491 if (!mEnabled) {
492 return;
493 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800494 va_list ap;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700495 va_start(ap, hash);
496 Writer::logVFormat(fmt, hash, ap);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800497 va_end(ap);
498}
499
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700500void NBLog::Writer::logVFormat(const char *fmt, log_hash_t hash, va_list argp)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800501{
Eric Tan5786e012018-08-15 09:03:47 -0700502 if (!mEnabled) {
503 return;
504 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800505 Writer::logStart(fmt);
506 int i;
507 double f;
508 char* s;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700509 int64_t t;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800510 Writer::logTimestamp();
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700511 Writer::logHash(hash);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800512 for (const char *p = fmt; *p != '\0'; p++) {
513 // TODO: implement more complex formatting such as %.3f
514 if (*p != '%') {
515 continue;
516 }
517 switch(*++p) {
518 case 's': // string
519 s = va_arg(argp, char *);
520 Writer::log(s);
521 break;
522
523 case 't': // timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700524 t = va_arg(argp, int64_t);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800525 Writer::logTimestamp(t);
526 break;
527
528 case 'd': // integer
529 i = va_arg(argp, int);
530 Writer::logInteger(i);
531 break;
532
533 case 'f': // float
534 f = va_arg(argp, double); // float arguments are promoted to double in vararg lists
535 Writer::logFloat((float)f);
536 break;
537
538 case 'p': // pid
539 Writer::logPID();
540 break;
541
542 // the "%\0" case finishes parsing
543 case '\0':
544 --p;
545 break;
546
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800547 case '%':
548 break;
549
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800550 default:
551 ALOGW("NBLog Writer parsed invalid format specifier: %c", *p);
552 break;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800553 }
554 }
555 Writer::logEnd();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800556}
557
558void NBLog::Writer::log(Event event, const void *data, size_t length)
559{
Eric Tan5786e012018-08-15 09:03:47 -0700560 if (!mEnabled) {
561 return;
562 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800563 if (data == NULL || length > Entry::kMaxLength) {
564 // TODO Perhaps it makes sense to display truncated data or at least a
565 // message that the data is too long? The current behavior can create
566 // a confusion for a programmer debugging their code.
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800567 return;
568 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700569 // Ignore if invalid event
570 if (event == EVENT_RESERVED || event >= EVENT_UPPER_BOUND) {
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800571 return;
572 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700573 Entry etr(event, data, length);
Eric Tan86be8722018-08-10 10:25:47 -0700574 log(etr, true /*trusted*/);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800575}
576
Eric Tan86be8722018-08-10 10:25:47 -0700577void NBLog::Writer::log(const NBLog::Entry &etr, bool trusted)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800578{
Eric Tan5786e012018-08-15 09:03:47 -0700579 if (!mEnabled) {
580 return;
581 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800582 if (!trusted) {
Eric Tan86be8722018-08-10 10:25:47 -0700583 log(etr.mEvent, etr.mData, etr.mLength);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800584 return;
585 }
Eric Tan5786e012018-08-15 09:03:47 -0700586 const size_t need = etr.mLength + Entry::kOverhead; // mEvent, mLength, data[mLength], mLength
587 // need = number of bytes written to FIFO
Glenn Kasten535e1612016-12-05 12:19:36 -0800588
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800589 // FIXME optimize this using memcpy for the data part of the Entry.
590 // The Entry could have a method copyTo(ptr, offset, size) to optimize the copy.
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700591 // checks size of a single log Entry: type, length, data pointer and ending
Glenn Kasten535e1612016-12-05 12:19:36 -0800592 uint8_t temp[Entry::kMaxLength + Entry::kOverhead];
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700593 // write this data to temp array
Glenn Kasten535e1612016-12-05 12:19:36 -0800594 for (size_t i = 0; i < need; i++) {
Eric Tan86be8722018-08-10 10:25:47 -0700595 temp[i] = etr.copyEntryDataAt(i);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800596 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700597 // write to circular buffer
Glenn Kasten535e1612016-12-05 12:19:36 -0800598 mFifoWriter->write(temp, need);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800599}
600
601bool NBLog::Writer::isEnabled() const
602{
603 return mEnabled;
604}
605
606bool NBLog::Writer::setEnabled(bool enabled)
607{
608 bool old = mEnabled;
609 mEnabled = enabled && mShared != NULL;
610 return old;
611}
612
613// ---------------------------------------------------------------------------
614
615NBLog::LockedWriter::LockedWriter()
616 : Writer()
617{
618}
619
Glenn Kasten535e1612016-12-05 12:19:36 -0800620NBLog::LockedWriter::LockedWriter(void *shared, size_t size)
621 : Writer(shared, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800622{
623}
624
625void NBLog::LockedWriter::log(const char *string)
626{
627 Mutex::Autolock _l(mLock);
628 Writer::log(string);
629}
630
631void NBLog::LockedWriter::logf(const char *fmt, ...)
632{
633 // FIXME should not take the lock until after formatting is done
634 Mutex::Autolock _l(mLock);
635 va_list ap;
636 va_start(ap, fmt);
637 Writer::logvf(fmt, ap);
638 va_end(ap);
639}
640
641void NBLog::LockedWriter::logvf(const char *fmt, va_list ap)
642{
643 // FIXME should not take the lock until after formatting is done
644 Mutex::Autolock _l(mLock);
645 Writer::logvf(fmt, ap);
646}
647
648void NBLog::LockedWriter::logTimestamp()
649{
650 // FIXME should not take the lock until after the clock_gettime() syscall
651 Mutex::Autolock _l(mLock);
652 Writer::logTimestamp();
653}
654
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700655void NBLog::LockedWriter::logTimestamp(const int64_t ts)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800656{
657 Mutex::Autolock _l(mLock);
658 Writer::logTimestamp(ts);
659}
660
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800661void NBLog::LockedWriter::logInteger(const int x)
662{
663 Mutex::Autolock _l(mLock);
664 Writer::logInteger(x);
665}
666
667void NBLog::LockedWriter::logFloat(const float x)
668{
669 Mutex::Autolock _l(mLock);
670 Writer::logFloat(x);
671}
672
673void NBLog::LockedWriter::logPID()
674{
675 Mutex::Autolock _l(mLock);
676 Writer::logPID();
677}
678
679void NBLog::LockedWriter::logStart(const char *fmt)
680{
681 Mutex::Autolock _l(mLock);
682 Writer::logStart(fmt);
683}
684
685
686void NBLog::LockedWriter::logEnd()
687{
688 Mutex::Autolock _l(mLock);
689 Writer::logEnd();
690}
691
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700692void NBLog::LockedWriter::logHash(log_hash_t hash)
693{
694 Mutex::Autolock _l(mLock);
695 Writer::logHash(hash);
696}
697
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800698bool NBLog::LockedWriter::isEnabled() const
699{
700 Mutex::Autolock _l(mLock);
701 return Writer::isEnabled();
702}
703
704bool NBLog::LockedWriter::setEnabled(bool enabled)
705{
706 Mutex::Autolock _l(mLock);
707 return Writer::setEnabled(enabled);
708}
709
710// ---------------------------------------------------------------------------
711
Eric Tan5786e012018-08-15 09:03:47 -0700712const std::unordered_set<NBLog::Event> NBLog::Reader::startingTypes {
713 NBLog::Event::EVENT_START_FMT,
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700714 NBLog::Event::EVENT_HISTOGRAM_ENTRY_TS,
Eric Tan5786e012018-08-15 09:03:47 -0700715 NBLog::Event::EVENT_AUDIO_STATE,
716 NBLog::Event::EVENT_MONOTONIC_CYCLE_TIME
717};
718const std::unordered_set<NBLog::Event> NBLog::Reader::endingTypes {
719 NBLog::Event::EVENT_END_FMT,
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700720 NBLog::Event::EVENT_HISTOGRAM_ENTRY_TS,
Eric Tan5786e012018-08-15 09:03:47 -0700721 NBLog::Event::EVENT_AUDIO_STATE,
722 NBLog::Event::EVENT_MONOTONIC_CYCLE_TIME
723};
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700724
Eric Tan5786e012018-08-15 09:03:47 -0700725NBLog::Reader::Reader(const void *shared, size_t size, const std::string &name)
Eric Tan6af18472018-08-20 09:27:50 -0700726 : mName(name),
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700727 mShared((/*const*/ Shared *) shared), /*mIMemory*/
Glenn Kasten535e1612016-12-05 12:19:36 -0800728 mFifo(mShared != NULL ?
729 new audio_utils_fifo(size, sizeof(uint8_t),
730 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700731 mFifoReader(mFifo != NULL ? new audio_utils_fifo_reader(*mFifo) : NULL)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800732{
733}
734
Eric Tan5786e012018-08-15 09:03:47 -0700735NBLog::Reader::Reader(const sp<IMemory>& iMemory, size_t size, const std::string &name)
736 : Reader(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size, name)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800737{
Glenn Kasten535e1612016-12-05 12:19:36 -0800738 mIMemory = iMemory;
739}
740
741NBLog::Reader::~Reader()
742{
743 delete mFifoReader;
744 delete mFifo;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800745}
746
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700747const uint8_t *NBLog::Reader::findLastEntryOfTypes(const uint8_t *front, const uint8_t *back,
Eric Tan5786e012018-08-15 09:03:47 -0700748 const std::unordered_set<Event> &types) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800749 while (back + Entry::kPreviousLengthOffset >= front) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700750 const uint8_t *prev = back - back[Entry::kPreviousLengthOffset] - Entry::kOverhead;
751 if (prev < front || prev + prev[offsetof(entry, length)] +
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800752 Entry::kOverhead != back) {
753
754 // prev points to an out of limits or inconsistent entry
755 return nullptr;
756 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700757 if (types.find((const Event) prev[offsetof(entry, type)]) != types.end()) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800758 return prev;
759 }
760 back = prev;
761 }
762 return nullptr; // no entry found
763}
764
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700765// Copies content of a Reader FIFO into its Snapshot
766// The Snapshot has the same raw data, but represented as a sequence of entries
767// and an EntryIterator making it possible to process the data.
Eric Tan6af18472018-08-20 09:27:50 -0700768std::unique_ptr<NBLog::Snapshot> NBLog::Reader::getSnapshot()
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800769{
Glenn Kasten535e1612016-12-05 12:19:36 -0800770 if (mFifoReader == NULL) {
Eric Tan6af18472018-08-20 09:27:50 -0700771 return std::make_unique<Snapshot>();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800772 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800773
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800774 // This emulates the behaviour of audio_utils_fifo_reader::read, but without incrementing the
775 // reader index. The index is incremented after handling corruption, to after the last complete
776 // entry of the buffer
Eric Tan5786e012018-08-15 09:03:47 -0700777 size_t lost = 0;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800778 audio_utils_iovec iovec[2];
Eric Tan5786e012018-08-15 09:03:47 -0700779 const size_t capacity = mFifo->capacity();
780 ssize_t availToRead;
781 // A call to audio_utils_fifo_reader::obtain() places the read pointer one buffer length
782 // before the writer's pointer (since mFifoReader was constructed with flush=false). The
783 // do while loop is an attempt to read all of the FIFO's contents regardless of how behind
784 // the reader is with respect to the writer. However, the following scheduling sequence is
785 // possible and can lead to a starvation situation:
786 // - Writer T1 writes, overrun with respect to Reader T2
787 // - T2 calls obtain() and gets EOVERFLOW, T2 ptr placed one buffer size behind T1 ptr
788 // - T1 write, overrun
789 // - T2 obtain(), EOVERFLOW (and so on...)
790 // To address this issue, we limit the number of tries for the reader to catch up with
791 // the writer.
792 int tries = 0;
793 size_t lostTemp;
794 do {
795 availToRead = mFifoReader->obtain(iovec, capacity, NULL /*timeout*/, &lostTemp);
796 lost += lostTemp;
797 } while (availToRead < 0 || ++tries <= kMaxObtainTries);
798
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800799 if (availToRead <= 0) {
Eric Tan5786e012018-08-15 09:03:47 -0700800 ALOGW_IF(availToRead < 0, "NBLog Reader %s failed to catch up with Writer", mName.c_str());
Eric Tan6af18472018-08-20 09:27:50 -0700801 return std::make_unique<Snapshot>();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800802 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800803
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800804 std::unique_ptr<Snapshot> snapshot(new Snapshot(availToRead));
805 memcpy(snapshot->mData, (const char *) mFifo->buffer() + iovec[0].mOffset, iovec[0].mLength);
806 if (iovec[1].mLength > 0) {
807 memcpy(snapshot->mData + (iovec[0].mLength),
Eric Tan5786e012018-08-15 09:03:47 -0700808 (const char *) mFifo->buffer() + iovec[1].mOffset, iovec[1].mLength);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800809 }
810
811 // Handle corrupted buffer
812 // Potentially, a buffer has corrupted data on both beginning (due to overflow) and end
813 // (due to incomplete format entry). But even if the end format entry is incomplete,
814 // it ends in a complete entry (which is not an END_FMT). So is safe to traverse backwards.
815 // TODO: handle client corruption (in the middle of a buffer)
816
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700817 const uint8_t *back = snapshot->mData + availToRead;
818 const uint8_t *front = snapshot->mData;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800819
820 // Find last END_FMT. <back> is sitting on an entry which might be the middle of a FormatEntry.
821 // We go backwards until we find an EVENT_END_FMT.
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700822 const uint8_t *lastEnd = findLastEntryOfTypes(front, back, endingTypes);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800823 if (lastEnd == nullptr) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700824 snapshot->mEnd = snapshot->mBegin = EntryIterator(front);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800825 } else {
826 // end of snapshot points to after last END_FMT entry
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700827 snapshot->mEnd = EntryIterator(lastEnd).next();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800828 // find first START_FMT
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700829 const uint8_t *firstStart = nullptr;
830 const uint8_t *firstStartTmp = snapshot->mEnd;
831 while ((firstStartTmp = findLastEntryOfTypes(front, firstStartTmp, startingTypes))
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800832 != nullptr) {
833 firstStart = firstStartTmp;
834 }
835 // firstStart is null if no START_FMT entry was found before lastEnd
836 if (firstStart == nullptr) {
837 snapshot->mBegin = snapshot->mEnd;
838 } else {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700839 snapshot->mBegin = EntryIterator(firstStart);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800840 }
841 }
842
843 // advance fifo reader index to after last entry read.
844 mFifoReader->release(snapshot->mEnd - front);
845
846 snapshot->mLost = lost;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800847 return snapshot;
848}
849
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700850// Takes raw content of the local merger FIFO, processes log entries, and
851// writes the data to a map of class PerformanceAnalysis, based on their thread ID.
Eric Tan6af18472018-08-20 09:27:50 -0700852void NBLog::MergeReader::getAndProcessSnapshot(NBLog::Snapshot &snapshot, int author)
Nicolas Roulet40a44982017-02-03 13:39:57 -0800853{
Eric Tan6af18472018-08-20 09:27:50 -0700854 for (const entry &etr : snapshot) {
855 switch (etr.type) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700856 case EVENT_HISTOGRAM_ENTRY_TS: {
Eric Tan6af18472018-08-20 09:27:50 -0700857 HistTsEntry *data = (HistTsEntry *) (etr.data);
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700858 // TODO This memcpies are here to avoid unaligned memory access crash.
859 // There's probably a more efficient way to do it
860 log_hash_t hash;
861 memcpy(&hash, &(data->hash), sizeof(hash));
Nicolas Rouletad82aa62017-04-03 19:15:20 -0700862 int64_t ts;
863 memcpy(&ts, &data->ts, sizeof(ts));
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700864 // TODO: hash for histogram ts and audio state need to match
865 // and correspond to audio production source file location
Eric Tan6af18472018-08-20 09:27:50 -0700866 mThreadPerformanceAnalysis[author][0 /*hash*/].logTsEntry(ts);
867 } break;
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700868 case EVENT_AUDIO_STATE: {
Eric Tan6af18472018-08-20 09:27:50 -0700869 HistTsEntry *data = (HistTsEntry *) (etr.data);
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700870 // TODO This memcpies are here to avoid unaligned memory access crash.
871 // There's probably a more efficient way to do it
Sanna Catherine de Treville Wagerd0965172017-07-24 13:42:44 -0700872 log_hash_t hash;
873 memcpy(&hash, &(data->hash), sizeof(hash));
Eric Tan6af18472018-08-20 09:27:50 -0700874 mThreadPerformanceAnalysis[author][0 /*hash*/].handleStateChange();
875 } break;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800876 case EVENT_END_FMT:
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800877 case EVENT_RESERVED:
Eric Tan6af18472018-08-20 09:27:50 -0700878 case EVENT_UPPER_BOUND:
879 ALOGW("warning: unexpected event %d", etr.type);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800880 default:
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800881 break;
882 }
Sanna Catherine de Treville Wager9484bae2017-06-15 14:39:44 -0700883 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800884}
885
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700886void NBLog::MergeReader::getAndProcessSnapshot()
Nicolas Roulet40a44982017-02-03 13:39:57 -0800887{
Eric Tan6af18472018-08-20 09:27:50 -0700888 // get a snapshot of each reader and process them
889 // TODO insert lock here
890 const size_t nLogs = mReaders.size();
891 std::vector<std::unique_ptr<Snapshot>> snapshots(nLogs);
892 for (size_t i = 0; i < nLogs; i++) {
893 snapshots[i] = mReaders[i]->getSnapshot();
894 }
895 // TODO unlock lock here
896 for (size_t i = 0; i < nLogs; i++) {
897 if (snapshots[i] != nullptr) {
898 getAndProcessSnapshot(*(snapshots[i]), i);
899 }
900 }
Nicolas Roulet40a44982017-02-03 13:39:57 -0800901}
902
Eric Tan86be8722018-08-10 10:25:47 -0700903void NBLog::MergeReader::dump(int fd, int indent)
904{
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700905 // TODO: add a mutex around media.log dump
Sanna Catherine de Treville Wagercf6c75a2017-07-21 17:05:25 -0700906 ReportPerformance::dump(fd, indent, mThreadPerformanceAnalysis);
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700907}
908
Eric Tan6af18472018-08-20 09:27:50 -0700909// TODO for future compatibility, would prefer to have a dump() go to string, and then go
910// to fd only when invoked through binder.
911void NBLog::DumpReader::dump(int fd, size_t indent)
Eric Tan5786e012018-08-15 09:03:47 -0700912{
Eric Tan6af18472018-08-20 09:27:50 -0700913 if (fd < 0) return;
914 std::unique_ptr<Snapshot> snapshot = getSnapshot();
915 if (snapshot == nullptr) {
Eric Tan5786e012018-08-15 09:03:47 -0700916 return;
917 }
Eric Tan6af18472018-08-20 09:27:50 -0700918 String8 timestamp, body;
919
920 // TODO all logged types should have a printable format.
921 for (auto it = snapshot->begin(); it != snapshot->end(); ++it) {
922 switch (it->type) {
923 case EVENT_START_FMT:
924 it = handleFormat(FormatEntry(it), &timestamp, &body);
925 break;
926 case EVENT_MONOTONIC_CYCLE_TIME: {
927 uint32_t monotonicNs;
928 memcpy(&monotonicNs, it->data, sizeof(monotonicNs));
929 body.appendFormat("Thread cycle took %u ns", monotonicNs);
930 } break;
931 case EVENT_END_FMT:
932 case EVENT_RESERVED:
933 case EVENT_UPPER_BOUND:
934 body.appendFormat("warning: unexpected event %d", it->type);
935 default:
936 break;
937 }
938 if (!body.isEmpty()) {
939 dprintf(fd, "%.*s%s %s\n", (int)indent, "", timestamp.string(), body.string());
940 body.clear();
941 }
942 timestamp.clear();
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700943 }
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700944}
945
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800946bool NBLog::Reader::isIMemory(const sp<IMemory>& iMemory) const
947{
Glenn Kasten481fb672013-09-30 14:39:28 -0700948 return iMemory != 0 && mIMemory != 0 && iMemory->pointer() == mIMemory->pointer();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800949}
950
Eric Tan6af18472018-08-20 09:27:50 -0700951void NBLog::DumpReader::appendTimestamp(String8 *body, const void *data)
Eric Tan86be8722018-08-10 10:25:47 -0700952{
Eric Tan5786e012018-08-15 09:03:47 -0700953 if (body == nullptr || data == nullptr) {
954 return;
955 }
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700956 int64_t ts;
957 memcpy(&ts, data, sizeof(ts));
958 body->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
959 (int) ((ts / (1000 * 1000)) % 1000));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800960}
961
Eric Tan6af18472018-08-20 09:27:50 -0700962void NBLog::DumpReader::appendInt(String8 *body, const void *data)
Eric Tan86be8722018-08-10 10:25:47 -0700963{
Eric Tan5786e012018-08-15 09:03:47 -0700964 if (body == nullptr || data == nullptr) {
965 return;
966 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800967 int x = *((int*) data);
968 body->appendFormat("<%d>", x);
969}
970
Eric Tan6af18472018-08-20 09:27:50 -0700971void NBLog::DumpReader::appendFloat(String8 *body, const void *data)
Eric Tan86be8722018-08-10 10:25:47 -0700972{
Eric Tan5786e012018-08-15 09:03:47 -0700973 if (body == nullptr || data == nullptr) {
974 return;
975 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800976 float f;
Eric Tan86be8722018-08-10 10:25:47 -0700977 memcpy(&f, data, sizeof(f));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800978 body->appendFormat("<%f>", f);
979}
980
Eric Tan6af18472018-08-20 09:27:50 -0700981void NBLog::DumpReader::appendPID(String8 *body, const void* data, size_t length)
Eric Tan86be8722018-08-10 10:25:47 -0700982{
Eric Tan5786e012018-08-15 09:03:47 -0700983 if (body == nullptr || data == nullptr) {
984 return;
985 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800986 pid_t id = *((pid_t*) data);
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800987 char * name = &((char*) data)[sizeof(pid_t)];
988 body->appendFormat("<PID: %d, name: %.*s>", id, (int) (length - sizeof(pid_t)), name);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800989}
990
Eric Tan6af18472018-08-20 09:27:50 -0700991String8 NBLog::DumpReader::bufferDump(const uint8_t *buffer, size_t size)
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700992{
993 String8 str;
Eric Tan5786e012018-08-15 09:03:47 -0700994 if (buffer == nullptr) {
995 return str;
996 }
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700997 str.append("[ ");
Eric Tan86be8722018-08-10 10:25:47 -0700998 for(size_t i = 0; i < size; i++) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700999 str.appendFormat("%d ", buffer[i]);
Nicolas Roulet2aedf372017-03-29 11:27:03 -07001000 }
1001 str.append("]");
1002 return str;
1003}
1004
Eric Tan6af18472018-08-20 09:27:50 -07001005String8 NBLog::DumpReader::bufferDump(const EntryIterator &it)
Nicolas Roulet2aedf372017-03-29 11:27:03 -07001006{
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001007 return bufferDump(it, it->length + Entry::kOverhead);
Nicolas Roulet2aedf372017-03-29 11:27:03 -07001008}
1009
Eric Tan6af18472018-08-20 09:27:50 -07001010NBLog::EntryIterator NBLog::DumpReader::handleFormat(const FormatEntry &fmtEntry,
1011 String8 *timestamp,
1012 String8 *body)
Eric Tan86be8722018-08-10 10:25:47 -07001013{
Nicolas Roulet40a44982017-02-03 13:39:57 -08001014 // log timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001015 int64_t ts = fmtEntry.timestamp();
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001016 timestamp->clear();
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001017 timestamp->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
1018 (int) ((ts / (1000 * 1000)) % 1000));
Nicolas Roulet40a44982017-02-03 13:39:57 -08001019
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -07001020 // log unique hash
1021 log_hash_t hash = fmtEntry.hash();
1022 // print only lower 16bit of hash as hex and line as int to reduce spam in the log
1023 body->appendFormat("%.4X-%d ", (int)(hash >> 16) & 0xFFFF, (int) hash & 0xFFFF);
1024
Nicolas Roulet40a44982017-02-03 13:39:57 -08001025 // log author (if present)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001026 handleAuthor(fmtEntry, body);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001027
1028 // log string
Eric Tan86be8722018-08-10 10:25:47 -07001029 EntryIterator arg = fmtEntry.args();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001030
1031 const char* fmt = fmtEntry.formatString();
1032 size_t fmt_length = fmtEntry.formatStringLength();
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001033
1034 for (size_t fmt_offset = 0; fmt_offset < fmt_length; ++fmt_offset) {
1035 if (fmt[fmt_offset] != '%') {
1036 body->append(&fmt[fmt_offset], 1); // TODO optimize to write consecutive strings at once
1037 continue;
1038 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001039 // case "%%""
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001040 if (fmt[++fmt_offset] == '%') {
1041 body->append("%");
1042 continue;
1043 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001044 // case "%\0"
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001045 if (fmt_offset == fmt_length) {
1046 continue;
1047 }
1048
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001049 NBLog::Event event = (NBLog::Event) arg->type;
1050 size_t length = arg->length;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001051
1052 // TODO check length for event type is correct
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001053
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001054 if (event == EVENT_END_FMT) {
1055 break;
1056 }
1057
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001058 // TODO: implement more complex formatting such as %.3f
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001059 const uint8_t *datum = arg->data; // pointer to the current event args
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001060 switch(fmt[fmt_offset])
1061 {
1062 case 's': // string
Nicolas Roulet4da78202017-02-03 12:53:39 -08001063 ALOGW_IF(event != EVENT_STRING,
1064 "NBLog Reader incompatible event for string specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001065 body->append((const char*) datum, length);
1066 break;
1067
1068 case 't': // timestamp
Nicolas Roulet4da78202017-02-03 12:53:39 -08001069 ALOGW_IF(event != EVENT_TIMESTAMP,
1070 "NBLog Reader incompatible event for timestamp specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001071 appendTimestamp(body, datum);
1072 break;
1073
1074 case 'd': // integer
Nicolas Roulet4da78202017-02-03 12:53:39 -08001075 ALOGW_IF(event != EVENT_INTEGER,
1076 "NBLog Reader incompatible event for integer specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001077 appendInt(body, datum);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001078 break;
1079
1080 case 'f': // float
Nicolas Roulet4da78202017-02-03 12:53:39 -08001081 ALOGW_IF(event != EVENT_FLOAT,
1082 "NBLog Reader incompatible event for float specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001083 appendFloat(body, datum);
1084 break;
1085
1086 case 'p': // pid
Nicolas Roulet4da78202017-02-03 12:53:39 -08001087 ALOGW_IF(event != EVENT_PID,
1088 "NBLog Reader incompatible event for pid specifier: %d", event);
Nicolas Rouletc20cb502017-02-01 12:35:24 -08001089 appendPID(body, datum, length);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001090 break;
1091
1092 default:
1093 ALOGW("NBLog Reader encountered unknown character %c", fmt[fmt_offset]);
1094 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001095 ++arg;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001096 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001097 ALOGW_IF(arg->type != EVENT_END_FMT, "Expected end of format, got %d", arg->type);
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001098 return arg;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001099}
1100
Nicolas Roulet40a44982017-02-03 13:39:57 -08001101NBLog::Merger::Merger(const void *shared, size_t size):
Nicolas Roulet40a44982017-02-03 13:39:57 -08001102 mShared((Shared *) shared),
1103 mFifo(mShared != NULL ?
1104 new audio_utils_fifo(size, sizeof(uint8_t),
1105 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
1106 mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL)
Eric Tan86be8722018-08-10 10:25:47 -07001107{
1108}
Nicolas Roulet40a44982017-02-03 13:39:57 -08001109
Eric Tan5786e012018-08-15 09:03:47 -07001110void NBLog::Merger::addReader(const sp<NBLog::Reader> &reader)
Eric Tan86be8722018-08-10 10:25:47 -07001111{
Glenn Kasten1c446272017-04-07 09:49:07 -07001112 // FIXME This is called by binder thread in MediaLogService::registerWriter
Eric Tan5786e012018-08-15 09:03:47 -07001113 // but the access to shared variable mReaders is not yet protected by a lock.
1114 mReaders.push_back(reader);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001115}
1116
1117// items placed in priority queue during merge
1118// composed by a timestamp and the index of the snapshot where the timestamp came from
1119struct MergeItem
1120{
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001121 int64_t ts;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001122 int index;
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001123 MergeItem(int64_t ts, int index): ts(ts), index(index) {}
Nicolas Roulet40a44982017-02-03 13:39:57 -08001124};
1125
Eric Tan86be8722018-08-10 10:25:47 -07001126bool operator>(const struct MergeItem &i1, const struct MergeItem &i2)
1127{
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001128 return i1.ts > i2.ts || (i1.ts == i2.ts && i1.index > i2.index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001129}
1130
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001131// Merge registered readers, sorted by timestamp, and write data to a single FIFO in local memory
Eric Tan86be8722018-08-10 10:25:47 -07001132void NBLog::Merger::merge()
1133{
Eric Tan6af18472018-08-20 09:27:50 -07001134 if (true) return; // Merging is not necessary at the moment, so this is to disable it
1135 // and bypass compiler warnings about member variables not being used.
Eric Tan5786e012018-08-15 09:03:47 -07001136 const int nLogs = mReaders.size();
Eric Tan6af18472018-08-20 09:27:50 -07001137 std::vector<std::unique_ptr<Snapshot>> snapshots(nLogs);
Eric Tan86be8722018-08-10 10:25:47 -07001138 std::vector<EntryIterator> offsets;
1139 offsets.reserve(nLogs);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001140 for (int i = 0; i < nLogs; ++i) {
Eric Tan5786e012018-08-15 09:03:47 -07001141 snapshots[i] = mReaders[i]->getSnapshot();
Eric Tan86be8722018-08-10 10:25:47 -07001142 offsets.push_back(snapshots[i]->begin());
Nicolas Roulet40a44982017-02-03 13:39:57 -08001143 }
1144 // initialize offsets
Nicolas Roulet40a44982017-02-03 13:39:57 -08001145 // TODO custom heap implementation could allow to update top, improving performance
1146 // for bursty buffers
1147 std::priority_queue<MergeItem, std::vector<MergeItem>, std::greater<MergeItem>> timestamps;
1148 for (int i = 0; i < nLogs; ++i)
1149 {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001150 if (offsets[i] != snapshots[i]->end()) {
Eric Tan86be8722018-08-10 10:25:47 -07001151 std::unique_ptr<AbstractEntry> abstractEntry = AbstractEntry::buildEntry(offsets[i]);
1152 if (abstractEntry == nullptr) {
1153 continue;
1154 }
1155 timestamps.emplace(abstractEntry->timestamp(), i);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001156 }
1157 }
1158
1159 while (!timestamps.empty()) {
Eric Tan86be8722018-08-10 10:25:47 -07001160 int index = timestamps.top().index; // find minimum timestamp
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001161 // copy it to the log, increasing offset
Eric Tan86be8722018-08-10 10:25:47 -07001162 offsets[index] = AbstractEntry::buildEntry(offsets[index])->
1163 copyWithAuthor(mFifoWriter, index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001164 // update data structures
Nicolas Roulet40a44982017-02-03 13:39:57 -08001165 timestamps.pop();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001166 if (offsets[index] != snapshots[index]->end()) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001167 int64_t ts = AbstractEntry::buildEntry(offsets[index])->timestamp();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001168 timestamps.emplace(ts, index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001169 }
1170 }
1171}
1172
Eric Tan5786e012018-08-15 09:03:47 -07001173const std::vector<sp<NBLog::Reader>>& NBLog::Merger::getReaders() const
Eric Tan86be8722018-08-10 10:25:47 -07001174{
Eric Tan5786e012018-08-15 09:03:47 -07001175 //AutoMutex _l(mLock);
1176 return mReaders;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001177}
1178
Glenn Kasten1c446272017-04-07 09:49:07 -07001179// ---------------------------------------------------------------------------
1180
Nicolas Roulet40a44982017-02-03 13:39:57 -08001181NBLog::MergeReader::MergeReader(const void *shared, size_t size, Merger &merger)
Eric Tan5786e012018-08-15 09:03:47 -07001182 : Reader(shared, size, "MergeReader"), mReaders(merger.getReaders())
Eric Tan86be8722018-08-10 10:25:47 -07001183{
1184}
Nicolas Roulet40a44982017-02-03 13:39:57 -08001185
Eric Tan86be8722018-08-10 10:25:47 -07001186void NBLog::MergeReader::handleAuthor(const NBLog::AbstractEntry &entry, String8 *body)
1187{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001188 int author = entry.author();
Eric Tan86be8722018-08-10 10:25:47 -07001189 if (author == -1) {
1190 return;
1191 }
Glenn Kasten1c446272017-04-07 09:49:07 -07001192 // FIXME Needs a lock
Eric Tan5786e012018-08-15 09:03:47 -07001193 const char* name = mReaders[author]->name().c_str();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001194 body->appendFormat("%s: ", name);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001195}
1196
Glenn Kasten1c446272017-04-07 09:49:07 -07001197// ---------------------------------------------------------------------------
1198
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001199NBLog::MergeThread::MergeThread(NBLog::Merger &merger, NBLog::MergeReader &mergeReader)
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001200 : mMerger(merger),
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001201 mMergeReader(mergeReader),
Eric Tan86be8722018-08-10 10:25:47 -07001202 mTimeoutUs(0)
1203{
1204}
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001205
Eric Tan86be8722018-08-10 10:25:47 -07001206NBLog::MergeThread::~MergeThread()
1207{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001208 // set exit flag, set timeout to 0 to force threadLoop to exit and wait for the thread to join
1209 requestExit();
1210 setTimeoutUs(0);
1211 join();
1212}
1213
Eric Tan86be8722018-08-10 10:25:47 -07001214bool NBLog::MergeThread::threadLoop()
1215{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001216 bool doMerge;
1217 {
1218 AutoMutex _l(mMutex);
1219 // If mTimeoutUs is negative, wait on the condition variable until it's positive.
Eric Tan6af18472018-08-20 09:27:50 -07001220 // If it's positive, merge. The minimum period between waking the condition variable
1221 // is handled in AudioFlinger::MediaLogNotifier::threadLoop().
1222 mCond.wait(mMutex);
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001223 doMerge = mTimeoutUs > 0;
1224 mTimeoutUs -= kThreadSleepPeriodUs;
1225 }
1226 if (doMerge) {
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001227 // Merge data from all the readers
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001228 mMerger.merge();
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001229 // Process the data collected by mMerger and write it to PerformanceAnalysis
1230 // FIXME: decide whether to call getAndProcessSnapshot every time
1231 // or whether to have a separate thread that calls it with a lower frequency
1232 mMergeReader.getAndProcessSnapshot();
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001233 }
1234 return true;
1235}
1236
Eric Tan86be8722018-08-10 10:25:47 -07001237void NBLog::MergeThread::wakeup()
1238{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001239 setTimeoutUs(kThreadWakeupPeriodUs);
1240}
1241
Eric Tan86be8722018-08-10 10:25:47 -07001242void NBLog::MergeThread::setTimeoutUs(int time)
1243{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001244 AutoMutex _l(mMutex);
1245 mTimeoutUs = time;
1246 mCond.signal();
1247}
1248
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001249} // namespace android