blob: f73e2ae2e1953646e6fbd5e9885190d343b0a9d3 [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) {
Eric Tancf3d82c2018-09-04 15:44:45 -070071 case EVENT_FMT_START:
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070072 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
Eric Tancf3d82c2018-09-04 15:44:45 -0700110 if (it->type == EVENT_FMT_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 Tancf3d82c2018-09-04 15:44:45 -0700141 return it->type == EVENT_FMT_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];
Eric Tancf3d82c2018-09-04 15:44:45 -0700154 authorEntry[offsetof(entry, type)] = EVENT_FMT_AUTHOR;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800155 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
Eric Tancf3d82c2018-09-04 15:44:45 -0700161 while ((++it)->type != EVENT_FMT_END) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800162 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 }
Eric Tancf3d82c2018-09-04 15:44:45 -0700397 struct timespec ts;
398 if (!clock_gettime(CLOCK_MONOTONIC, &ts)) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800399 log(EVENT_TIMESTAMP, &ts, sizeof(ts));
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800400 }
401}
402
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800403void NBLog::Writer::logStart(const char *fmt)
404{
Eric Tan5786e012018-08-15 09:03:47 -0700405 if (!mEnabled) {
406 return;
407 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800408 size_t length = strlen(fmt);
409 if (length > Entry::kMaxLength) {
410 length = Entry::kMaxLength;
411 }
Eric Tancf3d82c2018-09-04 15:44:45 -0700412 log(EVENT_FMT_START, fmt, length);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800413}
414
Eric Tancf3d82c2018-09-04 15:44:45 -0700415void NBLog::Writer::logTimestampFormat()
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800416{
Eric Tan5786e012018-08-15 09:03:47 -0700417 if (!mEnabled) {
418 return;
419 }
Eric Tancf3d82c2018-09-04 15:44:45 -0700420 int64_t ts = get_monotonic_ns();
421 if (ts > 0) {
422 log(EVENT_FMT_TIMESTAMP, &ts, sizeof(ts));
423 } else {
424 ALOGE("Failed to get timestamp");
Eric Tan5786e012018-08-15 09:03:47 -0700425 }
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700426}
427
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700428void NBLog::Writer::logEventHistTs(Event event, log_hash_t hash)
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700429{
Eric Tan5786e012018-08-15 09:03:47 -0700430 if (!mEnabled) {
431 return;
432 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700433 HistTsEntry data;
434 data.hash = hash;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700435 data.ts = get_monotonic_ns();
436 if (data.ts > 0) {
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700437 log(event, &data, sizeof(data));
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700438 } else {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700439 ALOGE("Failed to get timestamp");
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700440 }
441}
442
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700443void NBLog::Writer::logFormat(const char *fmt, log_hash_t hash, ...)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800444{
Eric Tan5786e012018-08-15 09:03:47 -0700445 if (!mEnabled) {
446 return;
447 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800448 va_list ap;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700449 va_start(ap, hash);
450 Writer::logVFormat(fmt, hash, ap);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800451 va_end(ap);
452}
453
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700454void NBLog::Writer::logVFormat(const char *fmt, log_hash_t hash, va_list argp)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800455{
Eric Tan5786e012018-08-15 09:03:47 -0700456 if (!mEnabled) {
457 return;
458 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800459 Writer::logStart(fmt);
460 int i;
Eric Tancf3d82c2018-09-04 15:44:45 -0700461 double d;
462 float f;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800463 char* s;
Eric Tancf3d82c2018-09-04 15:44:45 -0700464 size_t length;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700465 int64_t t;
Eric Tancf3d82c2018-09-04 15:44:45 -0700466 Writer::logTimestampFormat();
467 log(EVENT_FMT_HASH, &hash, sizeof(hash));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800468 for (const char *p = fmt; *p != '\0'; p++) {
469 // TODO: implement more complex formatting such as %.3f
470 if (*p != '%') {
471 continue;
472 }
473 switch(*++p) {
474 case 's': // string
475 s = va_arg(argp, char *);
Eric Tancf3d82c2018-09-04 15:44:45 -0700476 length = strlen(s);
477 if (length > Entry::kMaxLength) {
478 length = Entry::kMaxLength;
479 }
480 log(EVENT_FMT_STRING, s, length);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800481 break;
482
483 case 't': // timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700484 t = va_arg(argp, int64_t);
Eric Tancf3d82c2018-09-04 15:44:45 -0700485 log(EVENT_FMT_TIMESTAMP, &t, sizeof(t));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800486 break;
487
488 case 'd': // integer
489 i = va_arg(argp, int);
Eric Tancf3d82c2018-09-04 15:44:45 -0700490 log(EVENT_FMT_INTEGER, &i, sizeof(i));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800491 break;
492
493 case 'f': // float
Eric Tancf3d82c2018-09-04 15:44:45 -0700494 d = va_arg(argp, double); // float arguments are promoted to double in vararg lists
495 f = (float)d;
496 log(EVENT_FMT_FLOAT, &f, sizeof(f));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800497 break;
498
499 case 'p': // pid
Eric Tancf3d82c2018-09-04 15:44:45 -0700500 log(EVENT_FMT_PID, mPidTag, mPidTagSize);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800501 break;
502
503 // the "%\0" case finishes parsing
504 case '\0':
505 --p;
506 break;
507
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800508 case '%':
509 break;
510
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800511 default:
512 ALOGW("NBLog Writer parsed invalid format specifier: %c", *p);
513 break;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800514 }
515 }
Eric Tancf3d82c2018-09-04 15:44:45 -0700516 Entry etr(EVENT_FMT_END, nullptr, 0);
517 log(etr, true);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800518}
519
520void NBLog::Writer::log(Event event, const void *data, size_t length)
521{
Eric Tan5786e012018-08-15 09:03:47 -0700522 if (!mEnabled) {
523 return;
524 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800525 if (data == NULL || length > Entry::kMaxLength) {
526 // TODO Perhaps it makes sense to display truncated data or at least a
527 // message that the data is too long? The current behavior can create
528 // a confusion for a programmer debugging their code.
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800529 return;
530 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700531 // Ignore if invalid event
532 if (event == EVENT_RESERVED || event >= EVENT_UPPER_BOUND) {
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800533 return;
534 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700535 Entry etr(event, data, length);
Eric Tan86be8722018-08-10 10:25:47 -0700536 log(etr, true /*trusted*/);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800537}
538
Eric Tan86be8722018-08-10 10:25:47 -0700539void NBLog::Writer::log(const NBLog::Entry &etr, bool trusted)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800540{
Eric Tan5786e012018-08-15 09:03:47 -0700541 if (!mEnabled) {
542 return;
543 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800544 if (!trusted) {
Eric Tan86be8722018-08-10 10:25:47 -0700545 log(etr.mEvent, etr.mData, etr.mLength);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800546 return;
547 }
Eric Tan5786e012018-08-15 09:03:47 -0700548 const size_t need = etr.mLength + Entry::kOverhead; // mEvent, mLength, data[mLength], mLength
549 // need = number of bytes written to FIFO
Glenn Kasten535e1612016-12-05 12:19:36 -0800550
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800551 // FIXME optimize this using memcpy for the data part of the Entry.
552 // The Entry could have a method copyTo(ptr, offset, size) to optimize the copy.
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700553 // checks size of a single log Entry: type, length, data pointer and ending
Glenn Kasten535e1612016-12-05 12:19:36 -0800554 uint8_t temp[Entry::kMaxLength + Entry::kOverhead];
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700555 // write this data to temp array
Glenn Kasten535e1612016-12-05 12:19:36 -0800556 for (size_t i = 0; i < need; i++) {
Eric Tan86be8722018-08-10 10:25:47 -0700557 temp[i] = etr.copyEntryDataAt(i);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800558 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700559 // write to circular buffer
Glenn Kasten535e1612016-12-05 12:19:36 -0800560 mFifoWriter->write(temp, need);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800561}
562
563bool NBLog::Writer::isEnabled() const
564{
565 return mEnabled;
566}
567
568bool NBLog::Writer::setEnabled(bool enabled)
569{
570 bool old = mEnabled;
571 mEnabled = enabled && mShared != NULL;
572 return old;
573}
574
575// ---------------------------------------------------------------------------
576
577NBLog::LockedWriter::LockedWriter()
578 : Writer()
579{
580}
581
Glenn Kasten535e1612016-12-05 12:19:36 -0800582NBLog::LockedWriter::LockedWriter(void *shared, size_t size)
583 : Writer(shared, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800584{
585}
586
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800587bool NBLog::LockedWriter::isEnabled() const
588{
589 Mutex::Autolock _l(mLock);
590 return Writer::isEnabled();
591}
592
593bool NBLog::LockedWriter::setEnabled(bool enabled)
594{
595 Mutex::Autolock _l(mLock);
596 return Writer::setEnabled(enabled);
597}
598
Eric Tancf3d82c2018-09-04 15:44:45 -0700599void NBLog::LockedWriter::log(const Entry &entry, bool trusted) {
600 Mutex::Autolock _l(mLock);
601 Writer::log(entry, trusted);
602}
603
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800604// ---------------------------------------------------------------------------
605
Eric Tancf3d82c2018-09-04 15:44:45 -0700606// We make a set of the invalid types rather than the valid types when aligning
607// Snapshot EntryIterators to valid entries during log corruption checking.
608// This is done in order to avoid the maintenance overhead of adding a new NBLog::Event
609// type to the two sets below whenever a new NBLog::Event type is created, as it is
610// very likely that new types added will be valid types.
611// Currently, invalidBeginTypes and invalidEndTypes are used to handle the special
612// case of a Format Entry, which consists of a variable number of simple log entries.
613// If a new NBLog::Event is added that consists of a variable number of simple log entries,
614// then these sets need to be updated.
615
616// We want the beginning of a Snapshot to point to an entry that is not in
617// the middle of a formatted entry and not an FMT_END.
618const std::unordered_set<NBLog::Event> NBLog::Reader::invalidBeginTypes {
619 NBLog::Event::EVENT_FMT_TIMESTAMP,
620 NBLog::Event::EVENT_FMT_HASH,
621 NBLog::Event::EVENT_FMT_STRING,
622 NBLog::Event::EVENT_FMT_INTEGER,
623 NBLog::Event::EVENT_FMT_FLOAT,
624 NBLog::Event::EVENT_FMT_PID,
625 NBLog::Event::EVENT_FMT_AUTHOR,
626 NBLog::Event::EVENT_FMT_END
Eric Tan5786e012018-08-15 09:03:47 -0700627};
Eric Tancf3d82c2018-09-04 15:44:45 -0700628
629// We want the end of a Snapshot to point to an entry that is not in
630// the middle of a formatted entry and not a FMT_START.
631const std::unordered_set<NBLog::Event> NBLog::Reader::invalidEndTypes {
632 NBLog::Event::EVENT_FMT_START,
633 NBLog::Event::EVENT_FMT_TIMESTAMP,
634 NBLog::Event::EVENT_FMT_HASH,
635 NBLog::Event::EVENT_FMT_STRING,
636 NBLog::Event::EVENT_FMT_INTEGER,
637 NBLog::Event::EVENT_FMT_FLOAT,
638 NBLog::Event::EVENT_FMT_PID,
639 NBLog::Event::EVENT_FMT_AUTHOR
Eric Tan5786e012018-08-15 09:03:47 -0700640};
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700641
Eric Tan5786e012018-08-15 09:03:47 -0700642NBLog::Reader::Reader(const void *shared, size_t size, const std::string &name)
Eric Tan6af18472018-08-20 09:27:50 -0700643 : mName(name),
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700644 mShared((/*const*/ Shared *) shared), /*mIMemory*/
Glenn Kasten535e1612016-12-05 12:19:36 -0800645 mFifo(mShared != NULL ?
646 new audio_utils_fifo(size, sizeof(uint8_t),
647 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700648 mFifoReader(mFifo != NULL ? new audio_utils_fifo_reader(*mFifo) : NULL)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800649{
650}
651
Eric Tan5786e012018-08-15 09:03:47 -0700652NBLog::Reader::Reader(const sp<IMemory>& iMemory, size_t size, const std::string &name)
653 : Reader(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size, name)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800654{
Glenn Kasten535e1612016-12-05 12:19:36 -0800655 mIMemory = iMemory;
656}
657
658NBLog::Reader::~Reader()
659{
660 delete mFifoReader;
661 delete mFifo;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800662}
663
Eric Tancf3d82c2018-09-04 15:44:45 -0700664const uint8_t *NBLog::Reader::findLastValidEntry(const uint8_t *front, const uint8_t *back,
665 const std::unordered_set<Event> &invalidTypes) {
666 if (front == nullptr || back == nullptr) {
667 return nullptr;
668 }
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800669 while (back + Entry::kPreviousLengthOffset >= front) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700670 const uint8_t *prev = back - back[Entry::kPreviousLengthOffset] - Entry::kOverhead;
Eric Tancf3d82c2018-09-04 15:44:45 -0700671 const Event type = (const Event)prev[offsetof(entry, type)];
672 if (prev < front
673 || prev + prev[offsetof(entry, length)] + Entry::kOverhead != back
674 || type <= NBLog::EVENT_RESERVED || type >= NBLog::EVENT_UPPER_BOUND) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800675 // prev points to an out of limits or inconsistent entry
676 return nullptr;
677 }
Eric Tancf3d82c2018-09-04 15:44:45 -0700678 // if invalidTypes does not contain the type, then the type is valid.
679 if (invalidTypes.find(type) == invalidTypes.end()) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800680 return prev;
681 }
682 back = prev;
683 }
684 return nullptr; // no entry found
685}
686
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700687// Copies content of a Reader FIFO into its Snapshot
688// The Snapshot has the same raw data, but represented as a sequence of entries
689// and an EntryIterator making it possible to process the data.
Eric Tan6af18472018-08-20 09:27:50 -0700690std::unique_ptr<NBLog::Snapshot> NBLog::Reader::getSnapshot()
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800691{
Glenn Kasten535e1612016-12-05 12:19:36 -0800692 if (mFifoReader == NULL) {
Eric Tancf3d82c2018-09-04 15:44:45 -0700693 return std::unique_ptr<Snapshot>(new Snapshot());
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800694 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800695
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800696 // This emulates the behaviour of audio_utils_fifo_reader::read, but without incrementing the
697 // reader index. The index is incremented after handling corruption, to after the last complete
698 // entry of the buffer
Eric Tan5786e012018-08-15 09:03:47 -0700699 size_t lost = 0;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800700 audio_utils_iovec iovec[2];
Eric Tan5786e012018-08-15 09:03:47 -0700701 const size_t capacity = mFifo->capacity();
702 ssize_t availToRead;
703 // A call to audio_utils_fifo_reader::obtain() places the read pointer one buffer length
704 // before the writer's pointer (since mFifoReader was constructed with flush=false). The
705 // do while loop is an attempt to read all of the FIFO's contents regardless of how behind
706 // the reader is with respect to the writer. However, the following scheduling sequence is
707 // possible and can lead to a starvation situation:
708 // - Writer T1 writes, overrun with respect to Reader T2
709 // - T2 calls obtain() and gets EOVERFLOW, T2 ptr placed one buffer size behind T1 ptr
710 // - T1 write, overrun
711 // - T2 obtain(), EOVERFLOW (and so on...)
712 // To address this issue, we limit the number of tries for the reader to catch up with
713 // the writer.
714 int tries = 0;
715 size_t lostTemp;
716 do {
717 availToRead = mFifoReader->obtain(iovec, capacity, NULL /*timeout*/, &lostTemp);
718 lost += lostTemp;
719 } while (availToRead < 0 || ++tries <= kMaxObtainTries);
720
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800721 if (availToRead <= 0) {
Eric Tan5786e012018-08-15 09:03:47 -0700722 ALOGW_IF(availToRead < 0, "NBLog Reader %s failed to catch up with Writer", mName.c_str());
Eric Tancf3d82c2018-09-04 15:44:45 -0700723 return std::unique_ptr<Snapshot>(new Snapshot());
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800724 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800725
Eric Tancf3d82c2018-09-04 15:44:45 -0700726 // Change to #if 1 for debugging. This statement is useful for checking buffer fullness levels
727 // (as seen by reader) and how much data was lost. If you find that the fullness level is
728 // getting close to full, or that data loss is happening to often, then you should
729 // probably try some of the following:
730 // - log less data
731 // - log less often
732 // - increase the initial shared memory allocation for the buffer
733#if 0
734 ALOGD("getSnapshot name=%s, availToRead=%zd, capacity=%zu, fullness=%.3f, lost=%zu",
735 name().c_str(), availToRead, capacity, (double)availToRead / (double)capacity, lost);
736#endif
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800737 std::unique_ptr<Snapshot> snapshot(new Snapshot(availToRead));
738 memcpy(snapshot->mData, (const char *) mFifo->buffer() + iovec[0].mOffset, iovec[0].mLength);
739 if (iovec[1].mLength > 0) {
740 memcpy(snapshot->mData + (iovec[0].mLength),
Eric Tan5786e012018-08-15 09:03:47 -0700741 (const char *) mFifo->buffer() + iovec[1].mOffset, iovec[1].mLength);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800742 }
743
744 // Handle corrupted buffer
745 // Potentially, a buffer has corrupted data on both beginning (due to overflow) and end
746 // (due to incomplete format entry). But even if the end format entry is incomplete,
Eric Tancf3d82c2018-09-04 15:44:45 -0700747 // it ends in a complete entry (which is not an FMT_END). So is safe to traverse backwards.
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800748 // TODO: handle client corruption (in the middle of a buffer)
749
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700750 const uint8_t *back = snapshot->mData + availToRead;
751 const uint8_t *front = snapshot->mData;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800752
Eric Tancf3d82c2018-09-04 15:44:45 -0700753 // Find last FMT_END. <back> is sitting on an entry which might be the middle of a FormatEntry.
754 // We go backwards until we find an EVENT_FMT_END.
755 const uint8_t *lastEnd = findLastValidEntry(front, back, invalidEndTypes);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800756 if (lastEnd == nullptr) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700757 snapshot->mEnd = snapshot->mBegin = EntryIterator(front);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800758 } else {
Eric Tancf3d82c2018-09-04 15:44:45 -0700759 // end of snapshot points to after last FMT_END entry
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700760 snapshot->mEnd = EntryIterator(lastEnd).next();
Eric Tancf3d82c2018-09-04 15:44:45 -0700761 // find first FMT_START
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700762 const uint8_t *firstStart = nullptr;
763 const uint8_t *firstStartTmp = snapshot->mEnd;
Eric Tancf3d82c2018-09-04 15:44:45 -0700764 while ((firstStartTmp = findLastValidEntry(front, firstStartTmp, invalidBeginTypes))
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800765 != nullptr) {
766 firstStart = firstStartTmp;
767 }
Eric Tancf3d82c2018-09-04 15:44:45 -0700768 // firstStart is null if no FMT_START entry was found before lastEnd
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800769 if (firstStart == nullptr) {
770 snapshot->mBegin = snapshot->mEnd;
771 } else {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700772 snapshot->mBegin = EntryIterator(firstStart);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800773 }
774 }
775
776 // advance fifo reader index to after last entry read.
777 mFifoReader->release(snapshot->mEnd - front);
778
779 snapshot->mLost = lost;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800780 return snapshot;
781}
782
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700783// Takes raw content of the local merger FIFO, processes log entries, and
784// writes the data to a map of class PerformanceAnalysis, based on their thread ID.
Eric Tan6af18472018-08-20 09:27:50 -0700785void NBLog::MergeReader::getAndProcessSnapshot(NBLog::Snapshot &snapshot, int author)
Nicolas Roulet40a44982017-02-03 13:39:57 -0800786{
Eric Tancf3d82c2018-09-04 15:44:45 -0700787 // We don't do "auto it" because it reduces readability in this case.
788 for (EntryIterator it = snapshot.begin(); it != snapshot.end(); ++it) {
789 switch (it->type) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700790 case EVENT_HISTOGRAM_ENTRY_TS: {
Eric Tancf3d82c2018-09-04 15:44:45 -0700791 HistTsEntry payload = it.payload<HistTsEntry>();
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700792 // TODO: hash for histogram ts and audio state need to match
793 // and correspond to audio production source file location
Eric Tancf3d82c2018-09-04 15:44:45 -0700794 mThreadPerformanceAnalysis[author][0 /*hash*/].logTsEntry(payload.ts);
Eric Tan6af18472018-08-20 09:27:50 -0700795 } break;
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700796 case EVENT_AUDIO_STATE: {
Eric Tan6af18472018-08-20 09:27:50 -0700797 mThreadPerformanceAnalysis[author][0 /*hash*/].handleStateChange();
798 } break;
Eric Tane98dd6f2018-08-22 18:23:50 -0700799 case EVENT_LATENCY: {
Eric Tancf3d82c2018-09-04 15:44:45 -0700800 double latencyMs = it.payload<double>();
Eric Tane98dd6f2018-08-22 18:23:50 -0700801 mPerformanceData.addLatencyEntry(author, latencyMs);
802 } break;
Eric Tancf3d82c2018-09-04 15:44:45 -0700803 case EVENT_WORK_TIME: {
804 uint64_t monotonicNs = it.payload<uint64_t>();
Eric Tane98dd6f2018-08-22 18:23:50 -0700805 const double monotonicMs = monotonicNs * 1e-6;
806 mPerformanceData.addCycleTimeEntry(author, monotonicMs);
807 } break;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800808 case EVENT_RESERVED:
Eric Tan6af18472018-08-20 09:27:50 -0700809 case EVENT_UPPER_BOUND:
Eric Tancf3d82c2018-09-04 15:44:45 -0700810 ALOGW("warning: unexpected event %d", it->type);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800811 default:
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800812 break;
813 }
Sanna Catherine de Treville Wager9484bae2017-06-15 14:39:44 -0700814 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800815}
816
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700817void NBLog::MergeReader::getAndProcessSnapshot()
Nicolas Roulet40a44982017-02-03 13:39:57 -0800818{
Eric Tan6af18472018-08-20 09:27:50 -0700819 // get a snapshot of each reader and process them
820 // TODO insert lock here
821 const size_t nLogs = mReaders.size();
822 std::vector<std::unique_ptr<Snapshot>> snapshots(nLogs);
823 for (size_t i = 0; i < nLogs; i++) {
824 snapshots[i] = mReaders[i]->getSnapshot();
825 }
826 // TODO unlock lock here
827 for (size_t i = 0; i < nLogs; i++) {
828 if (snapshots[i] != nullptr) {
829 getAndProcessSnapshot(*(snapshots[i]), i);
830 }
831 }
Nicolas Roulet40a44982017-02-03 13:39:57 -0800832}
833
Eric Tan86be8722018-08-10 10:25:47 -0700834void NBLog::MergeReader::dump(int fd, int indent)
835{
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700836 // TODO: add a mutex around media.log dump
Sanna Catherine de Treville Wagercf6c75a2017-07-21 17:05:25 -0700837 ReportPerformance::dump(fd, indent, mThreadPerformanceAnalysis);
Eric Tane98dd6f2018-08-22 18:23:50 -0700838 mPerformanceData.dump(fd);
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700839}
840
Eric Tan6af18472018-08-20 09:27:50 -0700841// TODO for future compatibility, would prefer to have a dump() go to string, and then go
842// to fd only when invoked through binder.
843void NBLog::DumpReader::dump(int fd, size_t indent)
Eric Tan5786e012018-08-15 09:03:47 -0700844{
Eric Tan6af18472018-08-20 09:27:50 -0700845 if (fd < 0) return;
846 std::unique_ptr<Snapshot> snapshot = getSnapshot();
847 if (snapshot == nullptr) {
Eric Tan5786e012018-08-15 09:03:47 -0700848 return;
849 }
Eric Tan6af18472018-08-20 09:27:50 -0700850 String8 timestamp, body;
851
852 // TODO all logged types should have a printable format.
Eric Tancf3d82c2018-09-04 15:44:45 -0700853 for (EntryIterator it = snapshot->begin(); it != snapshot->end(); ++it) {
Eric Tan6af18472018-08-20 09:27:50 -0700854 switch (it->type) {
Eric Tancf3d82c2018-09-04 15:44:45 -0700855 case EVENT_FMT_START:
Eric Tan6af18472018-08-20 09:27:50 -0700856 it = handleFormat(FormatEntry(it), &timestamp, &body);
857 break;
Eric Tancf3d82c2018-09-04 15:44:45 -0700858 case EVENT_WORK_TIME: {
859 uint64_t monotonicNs = it.payload<uint64_t>();
860 body.appendFormat("Thread cycle: %lu ns", (unsigned long)monotonicNs);
Eric Tane98dd6f2018-08-22 18:23:50 -0700861 } break;
862 case EVENT_LATENCY: {
Eric Tancf3d82c2018-09-04 15:44:45 -0700863 double latencyMs = it.payload<double>();
Eric Tane98dd6f2018-08-22 18:23:50 -0700864 body.appendFormat("latency: %.3f ms", latencyMs);
Eric Tan6af18472018-08-20 09:27:50 -0700865 } break;
Eric Tancf3d82c2018-09-04 15:44:45 -0700866 case EVENT_FMT_END:
Eric Tan6af18472018-08-20 09:27:50 -0700867 case EVENT_RESERVED:
868 case EVENT_UPPER_BOUND:
869 body.appendFormat("warning: unexpected event %d", it->type);
870 default:
871 break;
872 }
873 if (!body.isEmpty()) {
874 dprintf(fd, "%.*s%s %s\n", (int)indent, "", timestamp.string(), body.string());
875 body.clear();
876 }
877 timestamp.clear();
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700878 }
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700879}
880
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800881bool NBLog::Reader::isIMemory(const sp<IMemory>& iMemory) const
882{
Glenn Kasten481fb672013-09-30 14:39:28 -0700883 return iMemory != 0 && mIMemory != 0 && iMemory->pointer() == mIMemory->pointer();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800884}
885
Eric Tan6af18472018-08-20 09:27:50 -0700886void NBLog::DumpReader::appendTimestamp(String8 *body, const void *data)
Eric Tan86be8722018-08-10 10:25:47 -0700887{
Eric Tan5786e012018-08-15 09:03:47 -0700888 if (body == nullptr || data == nullptr) {
889 return;
890 }
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700891 int64_t ts;
892 memcpy(&ts, data, sizeof(ts));
893 body->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
894 (int) ((ts / (1000 * 1000)) % 1000));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800895}
896
Eric Tan6af18472018-08-20 09:27:50 -0700897void NBLog::DumpReader::appendInt(String8 *body, const void *data)
Eric Tan86be8722018-08-10 10:25:47 -0700898{
Eric Tan5786e012018-08-15 09:03:47 -0700899 if (body == nullptr || data == nullptr) {
900 return;
901 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800902 int x = *((int*) data);
903 body->appendFormat("<%d>", x);
904}
905
Eric Tan6af18472018-08-20 09:27:50 -0700906void NBLog::DumpReader::appendFloat(String8 *body, const void *data)
Eric Tan86be8722018-08-10 10:25:47 -0700907{
Eric Tan5786e012018-08-15 09:03:47 -0700908 if (body == nullptr || data == nullptr) {
909 return;
910 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800911 float f;
Eric Tan86be8722018-08-10 10:25:47 -0700912 memcpy(&f, data, sizeof(f));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800913 body->appendFormat("<%f>", f);
914}
915
Eric Tan6af18472018-08-20 09:27:50 -0700916void NBLog::DumpReader::appendPID(String8 *body, const void* data, size_t length)
Eric Tan86be8722018-08-10 10:25:47 -0700917{
Eric Tan5786e012018-08-15 09:03:47 -0700918 if (body == nullptr || data == nullptr) {
919 return;
920 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800921 pid_t id = *((pid_t*) data);
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800922 char * name = &((char*) data)[sizeof(pid_t)];
923 body->appendFormat("<PID: %d, name: %.*s>", id, (int) (length - sizeof(pid_t)), name);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800924}
925
Eric Tan6af18472018-08-20 09:27:50 -0700926String8 NBLog::DumpReader::bufferDump(const uint8_t *buffer, size_t size)
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700927{
928 String8 str;
Eric Tan5786e012018-08-15 09:03:47 -0700929 if (buffer == nullptr) {
930 return str;
931 }
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700932 str.append("[ ");
Eric Tan86be8722018-08-10 10:25:47 -0700933 for(size_t i = 0; i < size; i++) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700934 str.appendFormat("%d ", buffer[i]);
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700935 }
936 str.append("]");
937 return str;
938}
939
Eric Tan6af18472018-08-20 09:27:50 -0700940String8 NBLog::DumpReader::bufferDump(const EntryIterator &it)
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700941{
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700942 return bufferDump(it, it->length + Entry::kOverhead);
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700943}
944
Eric Tan6af18472018-08-20 09:27:50 -0700945NBLog::EntryIterator NBLog::DumpReader::handleFormat(const FormatEntry &fmtEntry,
946 String8 *timestamp,
947 String8 *body)
Eric Tan86be8722018-08-10 10:25:47 -0700948{
Nicolas Roulet40a44982017-02-03 13:39:57 -0800949 // log timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700950 int64_t ts = fmtEntry.timestamp();
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800951 timestamp->clear();
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700952 timestamp->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
953 (int) ((ts / (1000 * 1000)) % 1000));
Nicolas Roulet40a44982017-02-03 13:39:57 -0800954
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700955 // log unique hash
956 log_hash_t hash = fmtEntry.hash();
957 // print only lower 16bit of hash as hex and line as int to reduce spam in the log
958 body->appendFormat("%.4X-%d ", (int)(hash >> 16) & 0xFFFF, (int) hash & 0xFFFF);
959
Nicolas Roulet40a44982017-02-03 13:39:57 -0800960 // log author (if present)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800961 handleAuthor(fmtEntry, body);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800962
963 // log string
Eric Tan86be8722018-08-10 10:25:47 -0700964 EntryIterator arg = fmtEntry.args();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800965
966 const char* fmt = fmtEntry.formatString();
967 size_t fmt_length = fmtEntry.formatStringLength();
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800968
969 for (size_t fmt_offset = 0; fmt_offset < fmt_length; ++fmt_offset) {
970 if (fmt[fmt_offset] != '%') {
971 body->append(&fmt[fmt_offset], 1); // TODO optimize to write consecutive strings at once
972 continue;
973 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800974 // case "%%""
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800975 if (fmt[++fmt_offset] == '%') {
976 body->append("%");
977 continue;
978 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800979 // case "%\0"
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800980 if (fmt_offset == fmt_length) {
981 continue;
982 }
983
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800984 NBLog::Event event = (NBLog::Event) arg->type;
985 size_t length = arg->length;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800986
987 // TODO check length for event type is correct
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800988
Eric Tancf3d82c2018-09-04 15:44:45 -0700989 if (event == EVENT_FMT_END) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800990 break;
991 }
992
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800993 // TODO: implement more complex formatting such as %.3f
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800994 const uint8_t *datum = arg->data; // pointer to the current event args
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800995 switch(fmt[fmt_offset])
996 {
997 case 's': // string
Eric Tancf3d82c2018-09-04 15:44:45 -0700998 ALOGW_IF(event != EVENT_FMT_STRING,
Nicolas Roulet4da78202017-02-03 12:53:39 -0800999 "NBLog Reader incompatible event for string specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001000 body->append((const char*) datum, length);
1001 break;
1002
1003 case 't': // timestamp
Eric Tancf3d82c2018-09-04 15:44:45 -07001004 ALOGW_IF(event != EVENT_FMT_TIMESTAMP,
Nicolas Roulet4da78202017-02-03 12:53:39 -08001005 "NBLog Reader incompatible event for timestamp specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001006 appendTimestamp(body, datum);
1007 break;
1008
1009 case 'd': // integer
Eric Tancf3d82c2018-09-04 15:44:45 -07001010 ALOGW_IF(event != EVENT_FMT_INTEGER,
Nicolas Roulet4da78202017-02-03 12:53:39 -08001011 "NBLog Reader incompatible event for integer specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001012 appendInt(body, datum);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001013 break;
1014
1015 case 'f': // float
Eric Tancf3d82c2018-09-04 15:44:45 -07001016 ALOGW_IF(event != EVENT_FMT_FLOAT,
Nicolas Roulet4da78202017-02-03 12:53:39 -08001017 "NBLog Reader incompatible event for float specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001018 appendFloat(body, datum);
1019 break;
1020
1021 case 'p': // pid
Eric Tancf3d82c2018-09-04 15:44:45 -07001022 ALOGW_IF(event != EVENT_FMT_PID,
Nicolas Roulet4da78202017-02-03 12:53:39 -08001023 "NBLog Reader incompatible event for pid specifier: %d", event);
Nicolas Rouletc20cb502017-02-01 12:35:24 -08001024 appendPID(body, datum, length);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001025 break;
1026
1027 default:
1028 ALOGW("NBLog Reader encountered unknown character %c", fmt[fmt_offset]);
1029 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001030 ++arg;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001031 }
Eric Tancf3d82c2018-09-04 15:44:45 -07001032 ALOGW_IF(arg->type != EVENT_FMT_END, "Expected end of format, got %d", arg->type);
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001033 return arg;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001034}
1035
Nicolas Roulet40a44982017-02-03 13:39:57 -08001036NBLog::Merger::Merger(const void *shared, size_t size):
Nicolas Roulet40a44982017-02-03 13:39:57 -08001037 mShared((Shared *) shared),
1038 mFifo(mShared != NULL ?
1039 new audio_utils_fifo(size, sizeof(uint8_t),
1040 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
1041 mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL)
Eric Tan86be8722018-08-10 10:25:47 -07001042{
1043}
Nicolas Roulet40a44982017-02-03 13:39:57 -08001044
Eric Tan5786e012018-08-15 09:03:47 -07001045void NBLog::Merger::addReader(const sp<NBLog::Reader> &reader)
Eric Tan86be8722018-08-10 10:25:47 -07001046{
Glenn Kasten1c446272017-04-07 09:49:07 -07001047 // FIXME This is called by binder thread in MediaLogService::registerWriter
Eric Tan5786e012018-08-15 09:03:47 -07001048 // but the access to shared variable mReaders is not yet protected by a lock.
1049 mReaders.push_back(reader);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001050}
1051
1052// items placed in priority queue during merge
1053// composed by a timestamp and the index of the snapshot where the timestamp came from
1054struct MergeItem
1055{
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001056 int64_t ts;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001057 int index;
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001058 MergeItem(int64_t ts, int index): ts(ts), index(index) {}
Nicolas Roulet40a44982017-02-03 13:39:57 -08001059};
1060
Eric Tan86be8722018-08-10 10:25:47 -07001061bool operator>(const struct MergeItem &i1, const struct MergeItem &i2)
1062{
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001063 return i1.ts > i2.ts || (i1.ts == i2.ts && i1.index > i2.index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001064}
1065
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001066// Merge registered readers, sorted by timestamp, and write data to a single FIFO in local memory
Eric Tan86be8722018-08-10 10:25:47 -07001067void NBLog::Merger::merge()
1068{
Eric Tan6af18472018-08-20 09:27:50 -07001069 if (true) return; // Merging is not necessary at the moment, so this is to disable it
1070 // and bypass compiler warnings about member variables not being used.
Eric Tan5786e012018-08-15 09:03:47 -07001071 const int nLogs = mReaders.size();
Eric Tan6af18472018-08-20 09:27:50 -07001072 std::vector<std::unique_ptr<Snapshot>> snapshots(nLogs);
Eric Tan86be8722018-08-10 10:25:47 -07001073 std::vector<EntryIterator> offsets;
1074 offsets.reserve(nLogs);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001075 for (int i = 0; i < nLogs; ++i) {
Eric Tan5786e012018-08-15 09:03:47 -07001076 snapshots[i] = mReaders[i]->getSnapshot();
Eric Tan86be8722018-08-10 10:25:47 -07001077 offsets.push_back(snapshots[i]->begin());
Nicolas Roulet40a44982017-02-03 13:39:57 -08001078 }
1079 // initialize offsets
Nicolas Roulet40a44982017-02-03 13:39:57 -08001080 // TODO custom heap implementation could allow to update top, improving performance
1081 // for bursty buffers
1082 std::priority_queue<MergeItem, std::vector<MergeItem>, std::greater<MergeItem>> timestamps;
1083 for (int i = 0; i < nLogs; ++i)
1084 {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001085 if (offsets[i] != snapshots[i]->end()) {
Eric Tan86be8722018-08-10 10:25:47 -07001086 std::unique_ptr<AbstractEntry> abstractEntry = AbstractEntry::buildEntry(offsets[i]);
1087 if (abstractEntry == nullptr) {
1088 continue;
1089 }
1090 timestamps.emplace(abstractEntry->timestamp(), i);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001091 }
1092 }
1093
1094 while (!timestamps.empty()) {
Eric Tan86be8722018-08-10 10:25:47 -07001095 int index = timestamps.top().index; // find minimum timestamp
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001096 // copy it to the log, increasing offset
Eric Tan86be8722018-08-10 10:25:47 -07001097 offsets[index] = AbstractEntry::buildEntry(offsets[index])->
1098 copyWithAuthor(mFifoWriter, index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001099 // update data structures
Nicolas Roulet40a44982017-02-03 13:39:57 -08001100 timestamps.pop();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001101 if (offsets[index] != snapshots[index]->end()) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001102 int64_t ts = AbstractEntry::buildEntry(offsets[index])->timestamp();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001103 timestamps.emplace(ts, index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001104 }
1105 }
1106}
1107
Eric Tan5786e012018-08-15 09:03:47 -07001108const std::vector<sp<NBLog::Reader>>& NBLog::Merger::getReaders() const
Eric Tan86be8722018-08-10 10:25:47 -07001109{
Eric Tan5786e012018-08-15 09:03:47 -07001110 //AutoMutex _l(mLock);
1111 return mReaders;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001112}
1113
Glenn Kasten1c446272017-04-07 09:49:07 -07001114// ---------------------------------------------------------------------------
1115
Nicolas Roulet40a44982017-02-03 13:39:57 -08001116NBLog::MergeReader::MergeReader(const void *shared, size_t size, Merger &merger)
Eric Tan5786e012018-08-15 09:03:47 -07001117 : Reader(shared, size, "MergeReader"), mReaders(merger.getReaders())
Eric Tan86be8722018-08-10 10:25:47 -07001118{
1119}
Nicolas Roulet40a44982017-02-03 13:39:57 -08001120
Eric Tan86be8722018-08-10 10:25:47 -07001121void NBLog::MergeReader::handleAuthor(const NBLog::AbstractEntry &entry, String8 *body)
1122{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001123 int author = entry.author();
Eric Tan86be8722018-08-10 10:25:47 -07001124 if (author == -1) {
1125 return;
1126 }
Glenn Kasten1c446272017-04-07 09:49:07 -07001127 // FIXME Needs a lock
Eric Tan5786e012018-08-15 09:03:47 -07001128 const char* name = mReaders[author]->name().c_str();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001129 body->appendFormat("%s: ", name);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001130}
1131
Glenn Kasten1c446272017-04-07 09:49:07 -07001132// ---------------------------------------------------------------------------
1133
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001134NBLog::MergeThread::MergeThread(NBLog::Merger &merger, NBLog::MergeReader &mergeReader)
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001135 : mMerger(merger),
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001136 mMergeReader(mergeReader),
Eric Tan86be8722018-08-10 10:25:47 -07001137 mTimeoutUs(0)
1138{
1139}
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001140
Eric Tan86be8722018-08-10 10:25:47 -07001141NBLog::MergeThread::~MergeThread()
1142{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001143 // set exit flag, set timeout to 0 to force threadLoop to exit and wait for the thread to join
1144 requestExit();
1145 setTimeoutUs(0);
1146 join();
1147}
1148
Eric Tan86be8722018-08-10 10:25:47 -07001149bool NBLog::MergeThread::threadLoop()
1150{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001151 bool doMerge;
1152 {
1153 AutoMutex _l(mMutex);
1154 // If mTimeoutUs is negative, wait on the condition variable until it's positive.
Eric Tan6af18472018-08-20 09:27:50 -07001155 // If it's positive, merge. The minimum period between waking the condition variable
1156 // is handled in AudioFlinger::MediaLogNotifier::threadLoop().
1157 mCond.wait(mMutex);
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001158 doMerge = mTimeoutUs > 0;
1159 mTimeoutUs -= kThreadSleepPeriodUs;
1160 }
1161 if (doMerge) {
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001162 // Merge data from all the readers
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001163 mMerger.merge();
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001164 // Process the data collected by mMerger and write it to PerformanceAnalysis
1165 // FIXME: decide whether to call getAndProcessSnapshot every time
1166 // or whether to have a separate thread that calls it with a lower frequency
1167 mMergeReader.getAndProcessSnapshot();
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001168 }
1169 return true;
1170}
1171
Eric Tan86be8722018-08-10 10:25:47 -07001172void NBLog::MergeThread::wakeup()
1173{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001174 setTimeoutUs(kThreadWakeupPeriodUs);
1175}
1176
Eric Tan86be8722018-08-10 10:25:47 -07001177void NBLog::MergeThread::setTimeoutUs(int time)
1178{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001179 AutoMutex _l(mMutex);
1180 mTimeoutUs = time;
1181 mCond.signal();
1182}
1183
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001184} // namespace android