blob: f5d2d6d42707ee2174a570bb539510e3a4e21b38 [file] [log] [blame]
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Sanna Catherine de Treville Wager14316442017-08-11 10:45:29 -070015 *
16 *
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080017 */
18
19#define LOG_TAG "NBLog"
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080020
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070021#include <algorithm>
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -080022#include <climits>
Sanna Catherine de Treville Wager201079a2017-05-18 16:36:29 -070023#include <deque>
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070024#include <fstream>
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070025#include <iostream>
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -070026#include <math.h>
Sanna Catherine de Treville Wager201079a2017-05-18 16:36:29 -070027#include <numeric>
Eric Tan5786e012018-08-15 09:03:47 -070028#include <unordered_set>
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070029#include <vector>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080030#include <stdarg.h>
31#include <stdint.h>
32#include <stdio.h>
33#include <string.h>
Nicolas Rouletc20cb502017-02-01 12:35:24 -080034#include <sys/prctl.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080035#include <time.h>
36#include <new>
Glenn Kasten535e1612016-12-05 12:19:36 -080037#include <audio_utils/roundup.h>
Glenn Kasten8589ce72017-09-08 17:03:42 -070038#include <media/nblog/NBLog.h>
39#include <media/nblog/PerformanceAnalysis.h>
40#include <media/nblog/ReportPerformance.h>
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -070041#include <utils/CallStack.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080042#include <utils/Log.h>
Glenn Kasten4e01ef62013-07-11 14:29:59 -070043#include <utils/String8.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080044
Nicolas Roulet40a44982017-02-03 13:39:57 -080045#include <queue>
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070046#include <utility>
Nicolas Roulet40a44982017-02-03 13:39:57 -080047
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080048namespace android {
49
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070050int NBLog::Entry::copyEntryDataAt(size_t offset) const
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080051{
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070052 // FIXME This is too slow
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080053 if (offset == 0)
54 return mEvent;
55 else if (offset == 1)
56 return mLength;
57 else if (offset < (size_t) (mLength + 2))
58 return ((char *) mData)[offset - 2];
59 else if (offset == (size_t) (mLength + 2))
60 return mLength;
61 else
62 return 0;
63}
64
65// ---------------------------------------------------------------------------
66
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070067/*static*/
Eric Tan86be8722018-08-10 10:25:47 -070068std::unique_ptr<NBLog::AbstractEntry> NBLog::AbstractEntry::buildEntry(const uint8_t *ptr)
69{
Eric Tan5786e012018-08-15 09:03:47 -070070 if (ptr == nullptr) {
71 return nullptr;
72 }
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -070073 const uint8_t type = EntryIterator(ptr)->type;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070074 switch (type) {
75 case EVENT_START_FMT:
76 return std::make_unique<FormatEntry>(FormatEntry(ptr));
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -070077 case EVENT_AUDIO_STATE:
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070078 case EVENT_HISTOGRAM_ENTRY_TS:
79 return std::make_unique<HistogramEntry>(HistogramEntry(ptr));
80 default:
81 ALOGW("Tried to create AbstractEntry of type %d", type);
82 return nullptr;
83 }
Nicolas Roulet40a44982017-02-03 13:39:57 -080084}
85
Eric Tan86be8722018-08-10 10:25:47 -070086NBLog::AbstractEntry::AbstractEntry(const uint8_t *entry) : mEntry(entry)
87{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070088}
89
90// ---------------------------------------------------------------------------
Nicolas Rouletcd5dd012017-02-13 12:09:28 -080091
Eric Tan86be8722018-08-10 10:25:47 -070092NBLog::EntryIterator NBLog::FormatEntry::begin() const
93{
Sanna Catherine de Treville Wagerdd92d7e2017-05-15 14:56:53 -070094 return EntryIterator(mEntry);
95}
96
Eric Tan86be8722018-08-10 10:25:47 -070097const char *NBLog::FormatEntry::formatString() const
98{
Nicolas Rouletcd5dd012017-02-13 12:09:28 -080099 return (const char*) mEntry + offsetof(entry, data);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800100}
101
Eric Tan86be8722018-08-10 10:25:47 -0700102size_t NBLog::FormatEntry::formatStringLength() const
103{
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800104 return mEntry[offsetof(entry, length)];
Nicolas Roulet40a44982017-02-03 13:39:57 -0800105}
106
Eric Tan86be8722018-08-10 10:25:47 -0700107NBLog::EntryIterator NBLog::FormatEntry::args() const
108{
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800109 auto it = begin();
Eric Tan86be8722018-08-10 10:25:47 -0700110 ++it; // skip start fmt
111 ++it; // skip timestamp
112 ++it; // skip hash
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700113 // Skip author if present
114 if (it->type == EVENT_AUTHOR) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800115 ++it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800116 }
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700117 return it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800118}
119
Eric Tan86be8722018-08-10 10:25:47 -0700120int64_t NBLog::FormatEntry::timestamp() const
121{
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800122 auto it = begin();
Eric Tan86be8722018-08-10 10:25:47 -0700123 ++it; // skip start fmt
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700124 return it.payload<int64_t>();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800125}
126
Eric Tan86be8722018-08-10 10:25:47 -0700127NBLog::log_hash_t NBLog::FormatEntry::hash() const
128{
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700129 auto it = begin();
Eric Tan86be8722018-08-10 10:25:47 -0700130 ++it; // skip start fmt
131 ++it; // skip timestamp
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700132 // unaligned 64-bit read not supported
133 log_hash_t hash;
134 memcpy(&hash, it->data, sizeof(hash));
135 return hash;
136}
137
Eric Tan86be8722018-08-10 10:25:47 -0700138int NBLog::FormatEntry::author() const
139{
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800140 auto it = begin();
Eric Tan86be8722018-08-10 10:25:47 -0700141 ++it; // skip start fmt
142 ++it; // skip timestamp
143 ++it; // skip hash
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700144 // if there is an author entry, return it, return -1 otherwise
Eric Tan5786e012018-08-15 09:03:47 -0700145 return it->type == EVENT_AUTHOR ? it.payload<int>() : -1;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800146}
147
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700148NBLog::EntryIterator NBLog::FormatEntry::copyWithAuthor(
Eric Tan86be8722018-08-10 10:25:47 -0700149 std::unique_ptr<audio_utils_fifo_writer> &dst, int author) const
150{
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800151 auto it = begin();
Eric Tan86be8722018-08-10 10:25:47 -0700152 it.copyTo(dst); // copy fmt start entry
153 (++it).copyTo(dst); // copy timestamp
154 (++it).copyTo(dst); // copy hash
Nicolas Roulet40a44982017-02-03 13:39:57 -0800155 // insert author entry
Eric Tan86be8722018-08-10 10:25:47 -0700156 size_t authorEntrySize = Entry::kOverhead + sizeof(author);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800157 uint8_t authorEntry[authorEntrySize];
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800158 authorEntry[offsetof(entry, type)] = EVENT_AUTHOR;
159 authorEntry[offsetof(entry, length)] =
Eric Tan86be8722018-08-10 10:25:47 -0700160 authorEntry[authorEntrySize + Entry::kPreviousLengthOffset] =
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800161 sizeof(author);
162 *(int*) (&authorEntry[offsetof(entry, data)]) = author;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800163 dst->write(authorEntry, authorEntrySize);
164 // copy rest of entries
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800165 while ((++it)->type != EVENT_END_FMT) {
166 it.copyTo(dst);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800167 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800168 it.copyTo(dst);
169 ++it;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800170 return it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800171}
172
Eric Tan86be8722018-08-10 10:25:47 -0700173void NBLog::EntryIterator::copyTo(std::unique_ptr<audio_utils_fifo_writer> &dst) const
174{
175 size_t length = mPtr[offsetof(entry, length)] + Entry::kOverhead;
176 dst->write(mPtr, length);
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800177}
Nicolas Roulet40a44982017-02-03 13:39:57 -0800178
Eric Tan86be8722018-08-10 10:25:47 -0700179void NBLog::EntryIterator::copyData(uint8_t *dst) const
180{
181 memcpy((void*) dst, mPtr + offsetof(entry, data), mPtr[offsetof(entry, length)]);
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800182}
183
Eric Tan86be8722018-08-10 10:25:47 -0700184NBLog::EntryIterator::EntryIterator() // Dummy initialization.
185 : mPtr(nullptr)
186{
187}
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800188
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700189NBLog::EntryIterator::EntryIterator(const uint8_t *entry)
Eric Tan86be8722018-08-10 10:25:47 -0700190 : mPtr(entry)
191{
192}
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800193
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700194NBLog::EntryIterator::EntryIterator(const NBLog::EntryIterator &other)
Eric Tan86be8722018-08-10 10:25:47 -0700195 : mPtr(other.mPtr)
196{
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800197}
198
Eric Tan86be8722018-08-10 10:25:47 -0700199const NBLog::entry& NBLog::EntryIterator::operator*() const
200{
201 return *(entry*) mPtr;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800202}
203
Eric Tan86be8722018-08-10 10:25:47 -0700204const NBLog::entry* NBLog::EntryIterator::operator->() const
205{
206 return (entry*) mPtr;
207}
208
209NBLog::EntryIterator& NBLog::EntryIterator::operator++()
210{
211 mPtr += mPtr[offsetof(entry, length)] + Entry::kOverhead;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800212 return *this;
213}
214
Eric Tan86be8722018-08-10 10:25:47 -0700215NBLog::EntryIterator& NBLog::EntryIterator::operator--()
216{
217 mPtr -= mPtr[Entry::kPreviousLengthOffset] + Entry::kOverhead;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800218 return *this;
219}
220
Eric Tan86be8722018-08-10 10:25:47 -0700221NBLog::EntryIterator NBLog::EntryIterator::next() const
222{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700223 EntryIterator aux(*this);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800224 return ++aux;
225}
226
Eric Tan86be8722018-08-10 10:25:47 -0700227NBLog::EntryIterator NBLog::EntryIterator::prev() const
228{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700229 EntryIterator aux(*this);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800230 return --aux;
231}
232
Eric Tan86be8722018-08-10 10:25:47 -0700233int NBLog::EntryIterator::operator-(const NBLog::EntryIterator &other) const
234{
235 return mPtr - other.mPtr;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800236}
237
Eric Tan86be8722018-08-10 10:25:47 -0700238bool NBLog::EntryIterator::operator!=(const EntryIterator &other) const
239{
240 return mPtr != other.mPtr;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800241}
242
Eric Tan86be8722018-08-10 10:25:47 -0700243bool NBLog::EntryIterator::hasConsistentLength() const
244{
245 return mPtr[offsetof(entry, length)] == mPtr[mPtr[offsetof(entry, length)] +
246 Entry::kOverhead + Entry::kPreviousLengthOffset];
Nicolas Roulet40a44982017-02-03 13:39:57 -0800247}
248
249// ---------------------------------------------------------------------------
250
Eric Tan86be8722018-08-10 10:25:47 -0700251int64_t NBLog::HistogramEntry::timestamp() const
252{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700253 return EntryIterator(mEntry).payload<HistTsEntry>().ts;
254}
255
Eric Tan86be8722018-08-10 10:25:47 -0700256NBLog::log_hash_t NBLog::HistogramEntry::hash() const
257{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700258 return EntryIterator(mEntry).payload<HistTsEntry>().hash;
259}
260
Eric Tan86be8722018-08-10 10:25:47 -0700261int NBLog::HistogramEntry::author() const
262{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700263 EntryIterator it(mEntry);
Eric Tan5786e012018-08-15 09:03:47 -0700264 return it->length == sizeof(HistTsEntryWithAuthor)
265 ? it.payload<HistTsEntryWithAuthor>().author : -1;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700266}
267
268NBLog::EntryIterator NBLog::HistogramEntry::copyWithAuthor(
Eric Tan86be8722018-08-10 10:25:47 -0700269 std::unique_ptr<audio_utils_fifo_writer> &dst, int author) const
270{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700271 // Current histogram entry has {type, length, struct HistTsEntry, length}.
272 // We now want {type, length, struct HistTsEntryWithAuthor, length}
273 uint8_t buffer[Entry::kOverhead + sizeof(HistTsEntryWithAuthor)];
274 // Copy content until the point we want to add the author
275 memcpy(buffer, mEntry, sizeof(entry) + sizeof(HistTsEntry));
276 // Copy the author
277 *(int*) (buffer + sizeof(entry) + sizeof(HistTsEntry)) = author;
278 // Update lengths
279 buffer[offsetof(entry, length)] = sizeof(HistTsEntryWithAuthor);
Ivan Lozano9ef855d2018-01-08 15:19:09 -0800280 buffer[offsetof(entry, data) + sizeof(HistTsEntryWithAuthor) + offsetof(ending, length)]
281 = sizeof(HistTsEntryWithAuthor);
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700282 // Write new buffer into FIFO
283 dst->write(buffer, sizeof(buffer));
284 return EntryIterator(mEntry).next();
285}
286
287// ---------------------------------------------------------------------------
288
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800289#if 0 // FIXME see note in NBLog.h
290NBLog::Timeline::Timeline(size_t size, void *shared)
291 : mSize(roundup(size)), mOwn(shared == NULL),
292 mShared((Shared *) (mOwn ? new char[sharedSize(size)] : shared))
293{
294 new (mShared) Shared;
295}
296
297NBLog::Timeline::~Timeline()
298{
299 mShared->~Shared();
300 if (mOwn) {
301 delete[] (char *) mShared;
302 }
303}
304#endif
305
306/*static*/
307size_t NBLog::Timeline::sharedSize(size_t size)
308{
Glenn Kastened99c2b2016-12-12 08:31:24 -0800309 // TODO fifo now supports non-power-of-2 buffer sizes, so could remove the roundup
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800310 return sizeof(Shared) + roundup(size);
311}
312
313// ---------------------------------------------------------------------------
314
315NBLog::Writer::Writer()
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800316 : mShared(NULL), mFifo(NULL), mFifoWriter(NULL), mEnabled(false), mPidTag(NULL), mPidTagSize(0)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800317{
318}
319
Glenn Kasten535e1612016-12-05 12:19:36 -0800320NBLog::Writer::Writer(void *shared, size_t size)
321 : mShared((Shared *) shared),
322 mFifo(mShared != NULL ?
323 new audio_utils_fifo(size, sizeof(uint8_t),
324 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
325 mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL),
326 mEnabled(mFifoWriter != NULL)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800327{
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800328 // caching pid and process name
329 pid_t id = ::getpid();
330 char procName[16];
331 int status = prctl(PR_GET_NAME, procName);
332 if (status) { // error getting process name
333 procName[0] = '\0';
334 }
335 size_t length = strlen(procName);
336 mPidTagSize = length + sizeof(pid_t);
337 mPidTag = new char[mPidTagSize];
338 memcpy(mPidTag, &id, sizeof(pid_t));
339 memcpy(mPidTag + sizeof(pid_t), procName, length);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800340}
341
Glenn Kasten535e1612016-12-05 12:19:36 -0800342NBLog::Writer::Writer(const sp<IMemory>& iMemory, size_t size)
343 : Writer(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800344{
Glenn Kasten535e1612016-12-05 12:19:36 -0800345 mIMemory = iMemory;
346}
347
348NBLog::Writer::~Writer()
349{
350 delete mFifoWriter;
351 delete mFifo;
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800352 delete[] mPidTag;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800353}
354
355void NBLog::Writer::log(const char *string)
356{
Eric Tan5786e012018-08-15 09:03:47 -0700357 if (!mEnabled) {
358 return;
359 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800360 LOG_ALWAYS_FATAL_IF(string == NULL, "Attempted to log NULL string");
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800361 size_t length = strlen(string);
Glenn Kasten535e1612016-12-05 12:19:36 -0800362 if (length > Entry::kMaxLength) {
363 length = Entry::kMaxLength;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800364 }
365 log(EVENT_STRING, string, length);
366}
367
368void NBLog::Writer::logf(const char *fmt, ...)
369{
Eric Tan5786e012018-08-15 09:03:47 -0700370 if (!mEnabled) {
371 return;
372 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800373 va_list ap;
374 va_start(ap, fmt);
375 Writer::logvf(fmt, ap); // the Writer:: is needed to avoid virtual dispatch for LockedWriter
376 va_end(ap);
377}
378
379void NBLog::Writer::logvf(const char *fmt, va_list ap)
380{
Eric Tan5786e012018-08-15 09:03:47 -0700381 if (!mEnabled) {
382 return;
383 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800384 char buffer[Entry::kMaxLength + 1 /*NUL*/];
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800385 int length = vsnprintf(buffer, sizeof(buffer), fmt, ap);
386 if (length >= (int) sizeof(buffer)) {
387 length = sizeof(buffer) - 1;
388 // NUL termination is not required
389 // buffer[length] = '\0';
390 }
391 if (length >= 0) {
392 log(EVENT_STRING, buffer, length);
393 }
394}
395
396void NBLog::Writer::logTimestamp()
397{
Eric Tan5786e012018-08-15 09:03:47 -0700398 if (!mEnabled) {
399 return;
400 }
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700401 int64_t ts = get_monotonic_ns();
402 if (ts > 0) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800403 log(EVENT_TIMESTAMP, &ts, sizeof(ts));
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700404 } else {
405 ALOGE("Failed to get timestamp");
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800406 }
407}
408
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700409void NBLog::Writer::logTimestamp(const int64_t ts)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800410{
Eric Tan5786e012018-08-15 09:03:47 -0700411 if (!mEnabled) {
412 return;
413 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800414 log(EVENT_TIMESTAMP, &ts, sizeof(ts));
415}
416
417void NBLog::Writer::logInteger(const int x)
418{
Eric Tan5786e012018-08-15 09:03:47 -0700419 if (!mEnabled) {
420 return;
421 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800422 log(EVENT_INTEGER, &x, sizeof(x));
423}
424
425void NBLog::Writer::logFloat(const float x)
426{
Eric Tan5786e012018-08-15 09:03:47 -0700427 if (!mEnabled) {
428 return;
429 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800430 log(EVENT_FLOAT, &x, sizeof(x));
431}
432
433void NBLog::Writer::logPID()
434{
Eric Tan5786e012018-08-15 09:03:47 -0700435 if (!mEnabled) {
436 return;
437 }
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800438 log(EVENT_PID, mPidTag, mPidTagSize);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800439}
440
441void NBLog::Writer::logStart(const char *fmt)
442{
Eric Tan5786e012018-08-15 09:03:47 -0700443 if (!mEnabled) {
444 return;
445 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800446 size_t length = strlen(fmt);
447 if (length > Entry::kMaxLength) {
448 length = Entry::kMaxLength;
449 }
450 log(EVENT_START_FMT, fmt, length);
451}
452
453void NBLog::Writer::logEnd()
454{
Eric Tan5786e012018-08-15 09:03:47 -0700455 if (!mEnabled) {
456 return;
457 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800458 Entry entry = Entry(EVENT_END_FMT, NULL, 0);
Eric Tan86be8722018-08-10 10:25:47 -0700459 log(entry, true);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800460}
461
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700462void NBLog::Writer::logHash(log_hash_t hash)
463{
Eric Tan5786e012018-08-15 09:03:47 -0700464 if (!mEnabled) {
465 return;
466 }
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700467 log(EVENT_HASH, &hash, sizeof(hash));
468}
469
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700470void NBLog::Writer::logEventHistTs(Event event, log_hash_t hash)
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700471{
Eric Tan5786e012018-08-15 09:03:47 -0700472 if (!mEnabled) {
473 return;
474 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700475 HistTsEntry data;
476 data.hash = hash;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700477 data.ts = get_monotonic_ns();
478 if (data.ts > 0) {
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700479 log(event, &data, sizeof(data));
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700480 } else {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700481 ALOGE("Failed to get timestamp");
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700482 }
483}
484
Eric Tan5786e012018-08-15 09:03:47 -0700485void NBLog::Writer::logMonotonicCycleTime(uint32_t monotonicNs)
486{
487 if (!mEnabled) {
488 return;
489 }
490 log(EVENT_MONOTONIC_CYCLE_TIME, &monotonicNs, sizeof(&monotonicNs));
491}
492
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700493void NBLog::Writer::logFormat(const char *fmt, log_hash_t hash, ...)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800494{
Eric Tan5786e012018-08-15 09:03:47 -0700495 if (!mEnabled) {
496 return;
497 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800498 va_list ap;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700499 va_start(ap, hash);
500 Writer::logVFormat(fmt, hash, ap);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800501 va_end(ap);
502}
503
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700504void NBLog::Writer::logVFormat(const char *fmt, log_hash_t hash, va_list argp)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800505{
Eric Tan5786e012018-08-15 09:03:47 -0700506 if (!mEnabled) {
507 return;
508 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800509 Writer::logStart(fmt);
510 int i;
511 double f;
512 char* s;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700513 int64_t t;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800514 Writer::logTimestamp();
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700515 Writer::logHash(hash);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800516 for (const char *p = fmt; *p != '\0'; p++) {
517 // TODO: implement more complex formatting such as %.3f
518 if (*p != '%') {
519 continue;
520 }
521 switch(*++p) {
522 case 's': // string
523 s = va_arg(argp, char *);
524 Writer::log(s);
525 break;
526
527 case 't': // timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700528 t = va_arg(argp, int64_t);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800529 Writer::logTimestamp(t);
530 break;
531
532 case 'd': // integer
533 i = va_arg(argp, int);
534 Writer::logInteger(i);
535 break;
536
537 case 'f': // float
538 f = va_arg(argp, double); // float arguments are promoted to double in vararg lists
539 Writer::logFloat((float)f);
540 break;
541
542 case 'p': // pid
543 Writer::logPID();
544 break;
545
546 // the "%\0" case finishes parsing
547 case '\0':
548 --p;
549 break;
550
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800551 case '%':
552 break;
553
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800554 default:
555 ALOGW("NBLog Writer parsed invalid format specifier: %c", *p);
556 break;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800557 }
558 }
559 Writer::logEnd();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800560}
561
562void NBLog::Writer::log(Event event, const void *data, size_t length)
563{
Eric Tan5786e012018-08-15 09:03:47 -0700564 if (!mEnabled) {
565 return;
566 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800567 if (data == NULL || length > Entry::kMaxLength) {
568 // TODO Perhaps it makes sense to display truncated data or at least a
569 // message that the data is too long? The current behavior can create
570 // a confusion for a programmer debugging their code.
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800571 return;
572 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700573 // Ignore if invalid event
574 if (event == EVENT_RESERVED || event >= EVENT_UPPER_BOUND) {
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800575 return;
576 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700577 Entry etr(event, data, length);
Eric Tan86be8722018-08-10 10:25:47 -0700578 log(etr, true /*trusted*/);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800579}
580
Eric Tan86be8722018-08-10 10:25:47 -0700581void NBLog::Writer::log(const NBLog::Entry &etr, bool trusted)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800582{
Eric Tan5786e012018-08-15 09:03:47 -0700583 if (!mEnabled) {
584 return;
585 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800586 if (!trusted) {
Eric Tan86be8722018-08-10 10:25:47 -0700587 log(etr.mEvent, etr.mData, etr.mLength);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800588 return;
589 }
Eric Tan5786e012018-08-15 09:03:47 -0700590 const size_t need = etr.mLength + Entry::kOverhead; // mEvent, mLength, data[mLength], mLength
591 // need = number of bytes written to FIFO
Glenn Kasten535e1612016-12-05 12:19:36 -0800592
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800593 // FIXME optimize this using memcpy for the data part of the Entry.
594 // The Entry could have a method copyTo(ptr, offset, size) to optimize the copy.
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700595 // checks size of a single log Entry: type, length, data pointer and ending
Glenn Kasten535e1612016-12-05 12:19:36 -0800596 uint8_t temp[Entry::kMaxLength + Entry::kOverhead];
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700597 // write this data to temp array
Glenn Kasten535e1612016-12-05 12:19:36 -0800598 for (size_t i = 0; i < need; i++) {
Eric Tan86be8722018-08-10 10:25:47 -0700599 temp[i] = etr.copyEntryDataAt(i);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800600 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700601 // write to circular buffer
Glenn Kasten535e1612016-12-05 12:19:36 -0800602 mFifoWriter->write(temp, need);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800603}
604
605bool NBLog::Writer::isEnabled() const
606{
607 return mEnabled;
608}
609
610bool NBLog::Writer::setEnabled(bool enabled)
611{
612 bool old = mEnabled;
613 mEnabled = enabled && mShared != NULL;
614 return old;
615}
616
617// ---------------------------------------------------------------------------
618
619NBLog::LockedWriter::LockedWriter()
620 : Writer()
621{
622}
623
Glenn Kasten535e1612016-12-05 12:19:36 -0800624NBLog::LockedWriter::LockedWriter(void *shared, size_t size)
625 : Writer(shared, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800626{
627}
628
629void NBLog::LockedWriter::log(const char *string)
630{
631 Mutex::Autolock _l(mLock);
632 Writer::log(string);
633}
634
635void NBLog::LockedWriter::logf(const char *fmt, ...)
636{
637 // FIXME should not take the lock until after formatting is done
638 Mutex::Autolock _l(mLock);
639 va_list ap;
640 va_start(ap, fmt);
641 Writer::logvf(fmt, ap);
642 va_end(ap);
643}
644
645void NBLog::LockedWriter::logvf(const char *fmt, va_list ap)
646{
647 // FIXME should not take the lock until after formatting is done
648 Mutex::Autolock _l(mLock);
649 Writer::logvf(fmt, ap);
650}
651
652void NBLog::LockedWriter::logTimestamp()
653{
654 // FIXME should not take the lock until after the clock_gettime() syscall
655 Mutex::Autolock _l(mLock);
656 Writer::logTimestamp();
657}
658
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700659void NBLog::LockedWriter::logTimestamp(const int64_t ts)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800660{
661 Mutex::Autolock _l(mLock);
662 Writer::logTimestamp(ts);
663}
664
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800665void NBLog::LockedWriter::logInteger(const int x)
666{
667 Mutex::Autolock _l(mLock);
668 Writer::logInteger(x);
669}
670
671void NBLog::LockedWriter::logFloat(const float x)
672{
673 Mutex::Autolock _l(mLock);
674 Writer::logFloat(x);
675}
676
677void NBLog::LockedWriter::logPID()
678{
679 Mutex::Autolock _l(mLock);
680 Writer::logPID();
681}
682
683void NBLog::LockedWriter::logStart(const char *fmt)
684{
685 Mutex::Autolock _l(mLock);
686 Writer::logStart(fmt);
687}
688
689
690void NBLog::LockedWriter::logEnd()
691{
692 Mutex::Autolock _l(mLock);
693 Writer::logEnd();
694}
695
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700696void NBLog::LockedWriter::logHash(log_hash_t hash)
697{
698 Mutex::Autolock _l(mLock);
699 Writer::logHash(hash);
700}
701
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800702bool NBLog::LockedWriter::isEnabled() const
703{
704 Mutex::Autolock _l(mLock);
705 return Writer::isEnabled();
706}
707
708bool NBLog::LockedWriter::setEnabled(bool enabled)
709{
710 Mutex::Autolock _l(mLock);
711 return Writer::setEnabled(enabled);
712}
713
714// ---------------------------------------------------------------------------
715
Eric Tan5786e012018-08-15 09:03:47 -0700716const std::unordered_set<NBLog::Event> NBLog::Reader::startingTypes {
717 NBLog::Event::EVENT_START_FMT,
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700718 NBLog::Event::EVENT_HISTOGRAM_ENTRY_TS,
Eric Tan5786e012018-08-15 09:03:47 -0700719 NBLog::Event::EVENT_AUDIO_STATE,
720 NBLog::Event::EVENT_MONOTONIC_CYCLE_TIME
721};
722const std::unordered_set<NBLog::Event> NBLog::Reader::endingTypes {
723 NBLog::Event::EVENT_END_FMT,
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700724 NBLog::Event::EVENT_HISTOGRAM_ENTRY_TS,
Eric Tan5786e012018-08-15 09:03:47 -0700725 NBLog::Event::EVENT_AUDIO_STATE,
726 NBLog::Event::EVENT_MONOTONIC_CYCLE_TIME
727};
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700728
Eric Tan5786e012018-08-15 09:03:47 -0700729NBLog::Reader::Reader(const void *shared, size_t size, const std::string &name)
730 : mFd(-1), mIndent(0), mLost(0), mName(name),
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700731 mShared((/*const*/ Shared *) shared), /*mIMemory*/
Glenn Kasten535e1612016-12-05 12:19:36 -0800732 mFifo(mShared != NULL ?
733 new audio_utils_fifo(size, sizeof(uint8_t),
734 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700735 mFifoReader(mFifo != NULL ? new audio_utils_fifo_reader(*mFifo) : NULL)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800736{
737}
738
Eric Tan5786e012018-08-15 09:03:47 -0700739NBLog::Reader::Reader(const sp<IMemory>& iMemory, size_t size, const std::string &name)
740 : Reader(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size, name)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800741{
Glenn Kasten535e1612016-12-05 12:19:36 -0800742 mIMemory = iMemory;
743}
744
745NBLog::Reader::~Reader()
746{
747 delete mFifoReader;
748 delete mFifo;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800749}
750
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700751const uint8_t *NBLog::Reader::findLastEntryOfTypes(const uint8_t *front, const uint8_t *back,
Eric Tan5786e012018-08-15 09:03:47 -0700752 const std::unordered_set<Event> &types) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800753 while (back + Entry::kPreviousLengthOffset >= front) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700754 const uint8_t *prev = back - back[Entry::kPreviousLengthOffset] - Entry::kOverhead;
755 if (prev < front || prev + prev[offsetof(entry, length)] +
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800756 Entry::kOverhead != back) {
757
758 // prev points to an out of limits or inconsistent entry
759 return nullptr;
760 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700761 if (types.find((const Event) prev[offsetof(entry, type)]) != types.end()) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800762 return prev;
763 }
764 back = prev;
765 }
766 return nullptr; // no entry found
767}
768
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700769// Copies content of a Reader FIFO into its Snapshot
770// The Snapshot has the same raw data, but represented as a sequence of entries
771// and an EntryIterator making it possible to process the data.
Nicolas Roulet40a44982017-02-03 13:39:57 -0800772std::unique_ptr<NBLog::Reader::Snapshot> NBLog::Reader::getSnapshot()
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800773{
Glenn Kasten535e1612016-12-05 12:19:36 -0800774 if (mFifoReader == NULL) {
Nicolas Roulet40a44982017-02-03 13:39:57 -0800775 return std::unique_ptr<NBLog::Reader::Snapshot>(new Snapshot());
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800776 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800777
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800778 // This emulates the behaviour of audio_utils_fifo_reader::read, but without incrementing the
779 // reader index. The index is incremented after handling corruption, to after the last complete
780 // entry of the buffer
Eric Tan5786e012018-08-15 09:03:47 -0700781 size_t lost = 0;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800782 audio_utils_iovec iovec[2];
Eric Tan5786e012018-08-15 09:03:47 -0700783 const size_t capacity = mFifo->capacity();
784 ssize_t availToRead;
785 // A call to audio_utils_fifo_reader::obtain() places the read pointer one buffer length
786 // before the writer's pointer (since mFifoReader was constructed with flush=false). The
787 // do while loop is an attempt to read all of the FIFO's contents regardless of how behind
788 // the reader is with respect to the writer. However, the following scheduling sequence is
789 // possible and can lead to a starvation situation:
790 // - Writer T1 writes, overrun with respect to Reader T2
791 // - T2 calls obtain() and gets EOVERFLOW, T2 ptr placed one buffer size behind T1 ptr
792 // - T1 write, overrun
793 // - T2 obtain(), EOVERFLOW (and so on...)
794 // To address this issue, we limit the number of tries for the reader to catch up with
795 // the writer.
796 int tries = 0;
797 size_t lostTemp;
798 do {
799 availToRead = mFifoReader->obtain(iovec, capacity, NULL /*timeout*/, &lostTemp);
800 lost += lostTemp;
801 } while (availToRead < 0 || ++tries <= kMaxObtainTries);
802
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800803 if (availToRead <= 0) {
Eric Tan5786e012018-08-15 09:03:47 -0700804 ALOGW_IF(availToRead < 0, "NBLog Reader %s failed to catch up with Writer", mName.c_str());
805 return std::make_unique<NBLog::Reader::Snapshot>();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800806 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800807
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800808 std::unique_ptr<Snapshot> snapshot(new Snapshot(availToRead));
809 memcpy(snapshot->mData, (const char *) mFifo->buffer() + iovec[0].mOffset, iovec[0].mLength);
810 if (iovec[1].mLength > 0) {
811 memcpy(snapshot->mData + (iovec[0].mLength),
Eric Tan5786e012018-08-15 09:03:47 -0700812 (const char *) mFifo->buffer() + iovec[1].mOffset, iovec[1].mLength);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800813 }
814
815 // Handle corrupted buffer
816 // Potentially, a buffer has corrupted data on both beginning (due to overflow) and end
817 // (due to incomplete format entry). But even if the end format entry is incomplete,
818 // it ends in a complete entry (which is not an END_FMT). So is safe to traverse backwards.
819 // TODO: handle client corruption (in the middle of a buffer)
820
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700821 const uint8_t *back = snapshot->mData + availToRead;
822 const uint8_t *front = snapshot->mData;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800823
824 // Find last END_FMT. <back> is sitting on an entry which might be the middle of a FormatEntry.
825 // We go backwards until we find an EVENT_END_FMT.
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700826 const uint8_t *lastEnd = findLastEntryOfTypes(front, back, endingTypes);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800827 if (lastEnd == nullptr) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700828 snapshot->mEnd = snapshot->mBegin = EntryIterator(front);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800829 } else {
830 // end of snapshot points to after last END_FMT entry
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700831 snapshot->mEnd = EntryIterator(lastEnd).next();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800832 // find first START_FMT
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700833 const uint8_t *firstStart = nullptr;
834 const uint8_t *firstStartTmp = snapshot->mEnd;
835 while ((firstStartTmp = findLastEntryOfTypes(front, firstStartTmp, startingTypes))
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800836 != nullptr) {
837 firstStart = firstStartTmp;
838 }
839 // firstStart is null if no START_FMT entry was found before lastEnd
840 if (firstStart == nullptr) {
841 snapshot->mBegin = snapshot->mEnd;
842 } else {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700843 snapshot->mBegin = EntryIterator(firstStart);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800844 }
845 }
846
847 // advance fifo reader index to after last entry read.
848 mFifoReader->release(snapshot->mEnd - front);
849
850 snapshot->mLost = lost;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800851 return snapshot;
852}
853
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700854// Takes raw content of the local merger FIFO, processes log entries, and
855// writes the data to a map of class PerformanceAnalysis, based on their thread ID.
856void NBLog::MergeReader::getAndProcessSnapshot(NBLog::Reader::Snapshot &snapshot)
Nicolas Roulet40a44982017-02-03 13:39:57 -0800857{
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700858 String8 timestamp, body;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700859
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800860 for (auto entry = snapshot.begin(); entry != snapshot.end();) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800861 switch (entry->type) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800862 case EVENT_START_FMT:
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800863 entry = handleFormat(FormatEntry(entry), &timestamp, &body);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800864 break;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700865 case EVENT_HISTOGRAM_ENTRY_TS: {
866 HistTsEntryWithAuthor *data = (HistTsEntryWithAuthor *) (entry->data);
867 // TODO This memcpies are here to avoid unaligned memory access crash.
868 // There's probably a more efficient way to do it
869 log_hash_t hash;
870 memcpy(&hash, &(data->hash), sizeof(hash));
Nicolas Rouletad82aa62017-04-03 19:15:20 -0700871 int64_t ts;
872 memcpy(&ts, &data->ts, sizeof(ts));
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700873 // TODO: hash for histogram ts and audio state need to match
874 // and correspond to audio production source file location
875 mThreadPerformanceAnalysis[data->author][0 /*hash*/].logTsEntry(ts);
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700876 ++entry;
877 break;
878 }
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700879 case EVENT_AUDIO_STATE: {
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700880 HistTsEntryWithAuthor *data = (HistTsEntryWithAuthor *) (entry->data);
881 // TODO This memcpies are here to avoid unaligned memory access crash.
882 // There's probably a more efficient way to do it
Sanna Catherine de Treville Wagerd0965172017-07-24 13:42:44 -0700883 log_hash_t hash;
884 memcpy(&hash, &(data->hash), sizeof(hash));
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700885 // TODO: remove ts if unused
886 int64_t ts;
887 memcpy(&ts, &data->ts, sizeof(ts));
888 mThreadPerformanceAnalysis[data->author][0 /*hash*/].handleStateChange();
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700889 ++entry;
890 break;
891 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800892 case EVENT_END_FMT:
893 body.appendFormat("warning: got to end format event");
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800894 ++entry;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800895 break;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800896 case EVENT_RESERVED:
897 default:
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800898 body.appendFormat("warning: unexpected event %d", entry->type);
899 ++entry;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800900 break;
901 }
Sanna Catherine de Treville Wager9484bae2017-06-15 14:39:44 -0700902 }
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700903 // FIXME: decide whether to print the warnings here or elsewhere
Sanna Catherine de Treville Wager9484bae2017-06-15 14:39:44 -0700904 if (!body.isEmpty()) {
Eric Tan86be8722018-08-10 10:25:47 -0700905 dumpLine(&timestamp, &body);
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700906 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800907}
908
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700909void NBLog::MergeReader::getAndProcessSnapshot()
Nicolas Roulet40a44982017-02-03 13:39:57 -0800910{
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700911 // get a snapshot, process it
Nicolas Roulet40a44982017-02-03 13:39:57 -0800912 std::unique_ptr<Snapshot> snap = getSnapshot();
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700913 getAndProcessSnapshot(*snap);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800914}
915
Eric Tan86be8722018-08-10 10:25:47 -0700916void NBLog::MergeReader::dump(int fd, int indent)
917{
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700918 // TODO: add a mutex around media.log dump
Sanna Catherine de Treville Wagercf6c75a2017-07-21 17:05:25 -0700919 ReportPerformance::dump(fd, indent, mThreadPerformanceAnalysis);
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700920}
921
Eric Tan5786e012018-08-15 09:03:47 -0700922void NBLog::Reader::dump(int fd, size_t indent, NBLog::Reader::Snapshot &snapshot)
923{
924 mFd = fd;
925 mIndent = indent;
926 String8 timestamp, body;
927
928 // Range-based for loop isn't used here because handleFormat() returns an EntryIterator
929 // that points to the next entry (it handles all of the necessary operator++() calls).
930 for (auto entry = snapshot.begin(); entry != snapshot.end();) {
931 switch (entry->type) {
932 case EVENT_START_FMT:
933 entry = handleFormat(FormatEntry(entry), &timestamp, &body);
934 break;
935 case EVENT_HISTOGRAM_ENTRY_TS:
936 ++entry;
937 break;
938 case EVENT_AUDIO_STATE:
939 ++entry;
940 break;
941 case EVENT_END_FMT:
942 body.appendFormat("warning: got to end format event");
943 ++entry;
944 break;
945 case EVENT_MONOTONIC_CYCLE_TIME: {
946 uint32_t monotonicNs = *(uint32_t *) (entry->data);
947 body.appendFormat("Thread cycle took %u ns", monotonicNs);
948 ++entry;
949 } break;
950 case EVENT_RESERVED:
951 default:
952 body.appendFormat("warning: unexpected event %d", entry->type);
953 ++entry;
954 break;
955 }
956 // FIXME: decide whether to print the warnings here or elsewhere
957 if (!body.isEmpty()) {
958 dumpLine(&timestamp, &body);
959 }
960 }
961}
962
963void NBLog::Reader::dump(int fd, size_t indent)
964{
965 // get a snapshot, dump it
966 std::unique_ptr<Snapshot> snap = getSnapshot();
967 dump(fd, indent, *snap);
968}
969
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700970// Writes a string to the console
Eric Tan86be8722018-08-10 10:25:47 -0700971void NBLog::Reader::dumpLine(const String8 *timestamp, String8 *body)
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700972{
Eric Tan5786e012018-08-15 09:03:47 -0700973 if (timestamp == nullptr || body == nullptr) {
974 return;
975 }
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700976 if (mFd >= 0) {
Eric Tan86be8722018-08-10 10:25:47 -0700977 dprintf(mFd, "%.*s%s %s\n", mIndent, "", timestamp->string(), body->string());
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700978 } else {
Eric Tan86be8722018-08-10 10:25:47 -0700979 ALOGI("%.*s%s %s", mIndent, "", timestamp->string(), body->string());
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700980 }
Eric Tan86be8722018-08-10 10:25:47 -0700981 body->clear();
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700982}
983
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800984bool NBLog::Reader::isIMemory(const sp<IMemory>& iMemory) const
985{
Glenn Kasten481fb672013-09-30 14:39:28 -0700986 return iMemory != 0 && mIMemory != 0 && iMemory->pointer() == mIMemory->pointer();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800987}
988
Glenn Kasten1c446272017-04-07 09:49:07 -0700989// ---------------------------------------------------------------------------
990
Eric Tan86be8722018-08-10 10:25:47 -0700991void NBLog::appendTimestamp(String8 *body, const void *data)
992{
Eric Tan5786e012018-08-15 09:03:47 -0700993 if (body == nullptr || data == nullptr) {
994 return;
995 }
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700996 int64_t ts;
997 memcpy(&ts, data, sizeof(ts));
998 body->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
999 (int) ((ts / (1000 * 1000)) % 1000));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001000}
1001
Eric Tan86be8722018-08-10 10:25:47 -07001002void NBLog::appendInt(String8 *body, const void *data)
1003{
Eric Tan5786e012018-08-15 09:03:47 -07001004 if (body == nullptr || data == nullptr) {
1005 return;
1006 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001007 int x = *((int*) data);
1008 body->appendFormat("<%d>", x);
1009}
1010
Eric Tan86be8722018-08-10 10:25:47 -07001011void NBLog::appendFloat(String8 *body, const void *data)
1012{
Eric Tan5786e012018-08-15 09:03:47 -07001013 if (body == nullptr || data == nullptr) {
1014 return;
1015 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001016 float f;
Eric Tan86be8722018-08-10 10:25:47 -07001017 memcpy(&f, data, sizeof(f));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001018 body->appendFormat("<%f>", f);
1019}
1020
Eric Tan86be8722018-08-10 10:25:47 -07001021void NBLog::appendPID(String8 *body, const void* data, size_t length)
1022{
Eric Tan5786e012018-08-15 09:03:47 -07001023 if (body == nullptr || data == nullptr) {
1024 return;
1025 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001026 pid_t id = *((pid_t*) data);
Nicolas Rouletc20cb502017-02-01 12:35:24 -08001027 char * name = &((char*) data)[sizeof(pid_t)];
1028 body->appendFormat("<PID: %d, name: %.*s>", id, (int) (length - sizeof(pid_t)), name);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001029}
1030
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001031String8 NBLog::bufferDump(const uint8_t *buffer, size_t size)
Nicolas Roulet2aedf372017-03-29 11:27:03 -07001032{
1033 String8 str;
Eric Tan5786e012018-08-15 09:03:47 -07001034 if (buffer == nullptr) {
1035 return str;
1036 }
Nicolas Roulet2aedf372017-03-29 11:27:03 -07001037 str.append("[ ");
Eric Tan86be8722018-08-10 10:25:47 -07001038 for(size_t i = 0; i < size; i++) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001039 str.appendFormat("%d ", buffer[i]);
Nicolas Roulet2aedf372017-03-29 11:27:03 -07001040 }
1041 str.append("]");
1042 return str;
1043}
1044
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001045String8 NBLog::bufferDump(const EntryIterator &it)
Nicolas Roulet2aedf372017-03-29 11:27:03 -07001046{
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001047 return bufferDump(it, it->length + Entry::kOverhead);
Nicolas Roulet2aedf372017-03-29 11:27:03 -07001048}
1049
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001050NBLog::EntryIterator NBLog::Reader::handleFormat(const FormatEntry &fmtEntry,
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001051 String8 *timestamp,
Eric Tan86be8722018-08-10 10:25:47 -07001052 String8 *body)
1053{
Nicolas Roulet40a44982017-02-03 13:39:57 -08001054 // log timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001055 int64_t ts = fmtEntry.timestamp();
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001056 timestamp->clear();
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001057 timestamp->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
1058 (int) ((ts / (1000 * 1000)) % 1000));
Nicolas Roulet40a44982017-02-03 13:39:57 -08001059
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -07001060 // log unique hash
1061 log_hash_t hash = fmtEntry.hash();
1062 // print only lower 16bit of hash as hex and line as int to reduce spam in the log
1063 body->appendFormat("%.4X-%d ", (int)(hash >> 16) & 0xFFFF, (int) hash & 0xFFFF);
1064
Nicolas Roulet40a44982017-02-03 13:39:57 -08001065 // log author (if present)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001066 handleAuthor(fmtEntry, body);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001067
1068 // log string
Eric Tan86be8722018-08-10 10:25:47 -07001069 EntryIterator arg = fmtEntry.args();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001070
1071 const char* fmt = fmtEntry.formatString();
1072 size_t fmt_length = fmtEntry.formatStringLength();
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001073
1074 for (size_t fmt_offset = 0; fmt_offset < fmt_length; ++fmt_offset) {
1075 if (fmt[fmt_offset] != '%') {
1076 body->append(&fmt[fmt_offset], 1); // TODO optimize to write consecutive strings at once
1077 continue;
1078 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001079 // case "%%""
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001080 if (fmt[++fmt_offset] == '%') {
1081 body->append("%");
1082 continue;
1083 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001084 // case "%\0"
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001085 if (fmt_offset == fmt_length) {
1086 continue;
1087 }
1088
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001089 NBLog::Event event = (NBLog::Event) arg->type;
1090 size_t length = arg->length;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001091
1092 // TODO check length for event type is correct
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001093
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001094 if (event == EVENT_END_FMT) {
1095 break;
1096 }
1097
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001098 // TODO: implement more complex formatting such as %.3f
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001099 const uint8_t *datum = arg->data; // pointer to the current event args
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001100 switch(fmt[fmt_offset])
1101 {
1102 case 's': // string
Nicolas Roulet4da78202017-02-03 12:53:39 -08001103 ALOGW_IF(event != EVENT_STRING,
1104 "NBLog Reader incompatible event for string specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001105 body->append((const char*) datum, length);
1106 break;
1107
1108 case 't': // timestamp
Nicolas Roulet4da78202017-02-03 12:53:39 -08001109 ALOGW_IF(event != EVENT_TIMESTAMP,
1110 "NBLog Reader incompatible event for timestamp specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001111 appendTimestamp(body, datum);
1112 break;
1113
1114 case 'd': // integer
Nicolas Roulet4da78202017-02-03 12:53:39 -08001115 ALOGW_IF(event != EVENT_INTEGER,
1116 "NBLog Reader incompatible event for integer specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001117 appendInt(body, datum);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001118 break;
1119
1120 case 'f': // float
Nicolas Roulet4da78202017-02-03 12:53:39 -08001121 ALOGW_IF(event != EVENT_FLOAT,
1122 "NBLog Reader incompatible event for float specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001123 appendFloat(body, datum);
1124 break;
1125
1126 case 'p': // pid
Nicolas Roulet4da78202017-02-03 12:53:39 -08001127 ALOGW_IF(event != EVENT_PID,
1128 "NBLog Reader incompatible event for pid specifier: %d", event);
Nicolas Rouletc20cb502017-02-01 12:35:24 -08001129 appendPID(body, datum, length);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001130 break;
1131
1132 default:
1133 ALOGW("NBLog Reader encountered unknown character %c", fmt[fmt_offset]);
1134 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001135 ++arg;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001136 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001137 ALOGW_IF(arg->type != EVENT_END_FMT, "Expected end of format, got %d", arg->type);
1138 ++arg;
1139 return arg;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001140}
1141
Nicolas Roulet40a44982017-02-03 13:39:57 -08001142NBLog::Merger::Merger(const void *shared, size_t size):
Nicolas Roulet40a44982017-02-03 13:39:57 -08001143 mShared((Shared *) shared),
1144 mFifo(mShared != NULL ?
1145 new audio_utils_fifo(size, sizeof(uint8_t),
1146 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
1147 mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL)
Eric Tan86be8722018-08-10 10:25:47 -07001148{
1149}
Nicolas Roulet40a44982017-02-03 13:39:57 -08001150
Eric Tan5786e012018-08-15 09:03:47 -07001151void NBLog::Merger::addReader(const sp<NBLog::Reader> &reader)
Eric Tan86be8722018-08-10 10:25:47 -07001152{
Glenn Kasten1c446272017-04-07 09:49:07 -07001153 // FIXME This is called by binder thread in MediaLogService::registerWriter
Eric Tan5786e012018-08-15 09:03:47 -07001154 // but the access to shared variable mReaders is not yet protected by a lock.
1155 mReaders.push_back(reader);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001156}
1157
1158// items placed in priority queue during merge
1159// composed by a timestamp and the index of the snapshot where the timestamp came from
1160struct MergeItem
1161{
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001162 int64_t ts;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001163 int index;
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001164 MergeItem(int64_t ts, int index): ts(ts), index(index) {}
Nicolas Roulet40a44982017-02-03 13:39:57 -08001165};
1166
Eric Tan86be8722018-08-10 10:25:47 -07001167bool operator>(const struct MergeItem &i1, const struct MergeItem &i2)
1168{
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001169 return i1.ts > i2.ts || (i1.ts == i2.ts && i1.index > i2.index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001170}
1171
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001172// Merge registered readers, sorted by timestamp, and write data to a single FIFO in local memory
Eric Tan86be8722018-08-10 10:25:47 -07001173void NBLog::Merger::merge()
1174{
Glenn Kasten1c446272017-04-07 09:49:07 -07001175 // FIXME This is called by merge thread
1176 // but the access to shared variable mNamedReaders is not yet protected by a lock.
Eric Tan5786e012018-08-15 09:03:47 -07001177 const int nLogs = mReaders.size();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001178 std::vector<std::unique_ptr<NBLog::Reader::Snapshot>> snapshots(nLogs);
Eric Tan86be8722018-08-10 10:25:47 -07001179 std::vector<EntryIterator> offsets;
1180 offsets.reserve(nLogs);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001181 for (int i = 0; i < nLogs; ++i) {
Eric Tan5786e012018-08-15 09:03:47 -07001182 snapshots[i] = mReaders[i]->getSnapshot();
Eric Tan86be8722018-08-10 10:25:47 -07001183 offsets.push_back(snapshots[i]->begin());
Nicolas Roulet40a44982017-02-03 13:39:57 -08001184 }
1185 // initialize offsets
Nicolas Roulet40a44982017-02-03 13:39:57 -08001186 // TODO custom heap implementation could allow to update top, improving performance
1187 // for bursty buffers
1188 std::priority_queue<MergeItem, std::vector<MergeItem>, std::greater<MergeItem>> timestamps;
1189 for (int i = 0; i < nLogs; ++i)
1190 {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001191 if (offsets[i] != snapshots[i]->end()) {
Eric Tan86be8722018-08-10 10:25:47 -07001192 std::unique_ptr<AbstractEntry> abstractEntry = AbstractEntry::buildEntry(offsets[i]);
1193 if (abstractEntry == nullptr) {
1194 continue;
1195 }
1196 timestamps.emplace(abstractEntry->timestamp(), i);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001197 }
1198 }
1199
1200 while (!timestamps.empty()) {
Eric Tan86be8722018-08-10 10:25:47 -07001201 int index = timestamps.top().index; // find minimum timestamp
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001202 // copy it to the log, increasing offset
Eric Tan86be8722018-08-10 10:25:47 -07001203 offsets[index] = AbstractEntry::buildEntry(offsets[index])->
1204 copyWithAuthor(mFifoWriter, index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001205 // update data structures
Nicolas Roulet40a44982017-02-03 13:39:57 -08001206 timestamps.pop();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001207 if (offsets[index] != snapshots[index]->end()) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001208 int64_t ts = AbstractEntry::buildEntry(offsets[index])->timestamp();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001209 timestamps.emplace(ts, index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001210 }
1211 }
1212}
1213
Eric Tan5786e012018-08-15 09:03:47 -07001214const std::vector<sp<NBLog::Reader>>& NBLog::Merger::getReaders() const
Eric Tan86be8722018-08-10 10:25:47 -07001215{
Eric Tan5786e012018-08-15 09:03:47 -07001216 //AutoMutex _l(mLock);
1217 return mReaders;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001218}
1219
Glenn Kasten1c446272017-04-07 09:49:07 -07001220// ---------------------------------------------------------------------------
1221
Nicolas Roulet40a44982017-02-03 13:39:57 -08001222NBLog::MergeReader::MergeReader(const void *shared, size_t size, Merger &merger)
Eric Tan5786e012018-08-15 09:03:47 -07001223 : Reader(shared, size, "MergeReader"), mReaders(merger.getReaders())
Eric Tan86be8722018-08-10 10:25:47 -07001224{
1225}
Nicolas Roulet40a44982017-02-03 13:39:57 -08001226
Eric Tan86be8722018-08-10 10:25:47 -07001227void NBLog::MergeReader::handleAuthor(const NBLog::AbstractEntry &entry, String8 *body)
1228{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001229 int author = entry.author();
Eric Tan86be8722018-08-10 10:25:47 -07001230 if (author == -1) {
1231 return;
1232 }
Glenn Kasten1c446272017-04-07 09:49:07 -07001233 // FIXME Needs a lock
Eric Tan5786e012018-08-15 09:03:47 -07001234 const char* name = mReaders[author]->name().c_str();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001235 body->appendFormat("%s: ", name);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001236}
1237
Glenn Kasten1c446272017-04-07 09:49:07 -07001238// ---------------------------------------------------------------------------
1239
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001240NBLog::MergeThread::MergeThread(NBLog::Merger &merger, NBLog::MergeReader &mergeReader)
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001241 : mMerger(merger),
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001242 mMergeReader(mergeReader),
Eric Tan86be8722018-08-10 10:25:47 -07001243 mTimeoutUs(0)
1244{
1245}
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001246
Eric Tan86be8722018-08-10 10:25:47 -07001247NBLog::MergeThread::~MergeThread()
1248{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001249 // set exit flag, set timeout to 0 to force threadLoop to exit and wait for the thread to join
1250 requestExit();
1251 setTimeoutUs(0);
1252 join();
1253}
1254
Eric Tan86be8722018-08-10 10:25:47 -07001255bool NBLog::MergeThread::threadLoop()
1256{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001257 bool doMerge;
1258 {
1259 AutoMutex _l(mMutex);
1260 // If mTimeoutUs is negative, wait on the condition variable until it's positive.
1261 // If it's positive, wait kThreadSleepPeriodUs and then merge
1262 nsecs_t waitTime = mTimeoutUs > 0 ? kThreadSleepPeriodUs * 1000 : LLONG_MAX;
1263 mCond.waitRelative(mMutex, waitTime);
1264 doMerge = mTimeoutUs > 0;
1265 mTimeoutUs -= kThreadSleepPeriodUs;
1266 }
Eric Tan5786e012018-08-15 09:03:47 -07001267 doMerge = false; // Disable merging for now.
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001268 if (doMerge) {
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001269 // Merge data from all the readers
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001270 mMerger.merge();
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001271 // Process the data collected by mMerger and write it to PerformanceAnalysis
1272 // FIXME: decide whether to call getAndProcessSnapshot every time
1273 // or whether to have a separate thread that calls it with a lower frequency
1274 mMergeReader.getAndProcessSnapshot();
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001275 }
1276 return true;
1277}
1278
Eric Tan86be8722018-08-10 10:25:47 -07001279void NBLog::MergeThread::wakeup()
1280{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001281 setTimeoutUs(kThreadWakeupPeriodUs);
1282}
1283
Eric Tan86be8722018-08-10 10:25:47 -07001284void NBLog::MergeThread::setTimeoutUs(int time)
1285{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001286 AutoMutex _l(mMutex);
1287 mTimeoutUs = time;
1288 mCond.signal();
1289}
1290
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001291} // namespace android