blob: bfc797c7b90e3878b124a9c84d310368cd84b01d [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>
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070028#include <vector>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080029#include <stdarg.h>
30#include <stdint.h>
31#include <stdio.h>
32#include <string.h>
Nicolas Rouletc20cb502017-02-01 12:35:24 -080033#include <sys/prctl.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080034#include <time.h>
35#include <new>
Glenn Kasten535e1612016-12-05 12:19:36 -080036#include <audio_utils/roundup.h>
Glenn Kasten8589ce72017-09-08 17:03:42 -070037#include <media/nblog/NBLog.h>
38#include <media/nblog/PerformanceAnalysis.h>
39#include <media/nblog/ReportPerformance.h>
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -070040#include <utils/CallStack.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080041#include <utils/Log.h>
Glenn Kasten4e01ef62013-07-11 14:29:59 -070042#include <utils/String8.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080043
Nicolas Roulet40a44982017-02-03 13:39:57 -080044#include <queue>
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070045#include <utility>
Nicolas Roulet40a44982017-02-03 13:39:57 -080046
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080047namespace android {
48
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070049int NBLog::Entry::copyEntryDataAt(size_t offset) const
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080050{
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070051 // FIXME This is too slow
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080052 if (offset == 0)
53 return mEvent;
54 else if (offset == 1)
55 return mLength;
56 else if (offset < (size_t) (mLength + 2))
57 return ((char *) mData)[offset - 2];
58 else if (offset == (size_t) (mLength + 2))
59 return mLength;
60 else
61 return 0;
62}
63
64// ---------------------------------------------------------------------------
65
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070066/*static*/
Eric Tan86be8722018-08-10 10:25:47 -070067std::unique_ptr<NBLog::AbstractEntry> NBLog::AbstractEntry::buildEntry(const uint8_t *ptr)
68{
69 if (ptr == nullptr) return nullptr;
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -070070 const uint8_t type = EntryIterator(ptr)->type;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070071 switch (type) {
72 case EVENT_START_FMT:
73 return std::make_unique<FormatEntry>(FormatEntry(ptr));
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -070074 case EVENT_AUDIO_STATE:
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070075 case EVENT_HISTOGRAM_ENTRY_TS:
76 return std::make_unique<HistogramEntry>(HistogramEntry(ptr));
77 default:
78 ALOGW("Tried to create AbstractEntry of type %d", type);
79 return nullptr;
80 }
Nicolas Roulet40a44982017-02-03 13:39:57 -080081}
82
Eric Tan86be8722018-08-10 10:25:47 -070083NBLog::AbstractEntry::AbstractEntry(const uint8_t *entry) : mEntry(entry)
84{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070085}
86
87// ---------------------------------------------------------------------------
Nicolas Rouletcd5dd012017-02-13 12:09:28 -080088
Eric Tan86be8722018-08-10 10:25:47 -070089NBLog::EntryIterator NBLog::FormatEntry::begin() const
90{
Sanna Catherine de Treville Wagerdd92d7e2017-05-15 14:56:53 -070091 return EntryIterator(mEntry);
92}
93
Eric Tan86be8722018-08-10 10:25:47 -070094const char *NBLog::FormatEntry::formatString() const
95{
Nicolas Rouletcd5dd012017-02-13 12:09:28 -080096 return (const char*) mEntry + offsetof(entry, data);
Nicolas Roulet40a44982017-02-03 13:39:57 -080097}
98
Eric Tan86be8722018-08-10 10:25:47 -070099size_t NBLog::FormatEntry::formatStringLength() const
100{
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800101 return mEntry[offsetof(entry, length)];
Nicolas Roulet40a44982017-02-03 13:39:57 -0800102}
103
Eric Tan86be8722018-08-10 10:25:47 -0700104NBLog::EntryIterator NBLog::FormatEntry::args() const
105{
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800106 auto it = begin();
Eric Tan86be8722018-08-10 10:25:47 -0700107 ++it; // skip start fmt
108 ++it; // skip timestamp
109 ++it; // skip hash
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700110 // Skip author if present
111 if (it->type == EVENT_AUTHOR) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800112 ++it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800113 }
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700114 return it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800115}
116
Eric Tan86be8722018-08-10 10:25:47 -0700117int64_t NBLog::FormatEntry::timestamp() const
118{
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800119 auto it = begin();
Eric Tan86be8722018-08-10 10:25:47 -0700120 ++it; // skip start fmt
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700121 return it.payload<int64_t>();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800122}
123
Eric Tan86be8722018-08-10 10:25:47 -0700124NBLog::log_hash_t NBLog::FormatEntry::hash() const
125{
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700126 auto it = begin();
Eric Tan86be8722018-08-10 10:25:47 -0700127 ++it; // skip start fmt
128 ++it; // skip timestamp
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700129 // unaligned 64-bit read not supported
130 log_hash_t hash;
131 memcpy(&hash, it->data, sizeof(hash));
132 return hash;
133}
134
Eric Tan86be8722018-08-10 10:25:47 -0700135int NBLog::FormatEntry::author() const
136{
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800137 auto it = begin();
Eric Tan86be8722018-08-10 10:25:47 -0700138 ++it; // skip start fmt
139 ++it; // skip timestamp
140 ++it; // skip hash
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700141 // if there is an author entry, return it, return -1 otherwise
142 if (it->type == EVENT_AUTHOR) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800143 return it.payload<int>();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800144 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800145 return -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);
264 if (it->length == sizeof(HistTsEntryWithAuthor)) {
265 return it.payload<HistTsEntryWithAuthor>().author;
266 } else {
267 return -1;
268 }
269}
270
271NBLog::EntryIterator NBLog::HistogramEntry::copyWithAuthor(
Eric Tan86be8722018-08-10 10:25:47 -0700272 std::unique_ptr<audio_utils_fifo_writer> &dst, int author) const
273{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700274 // Current histogram entry has {type, length, struct HistTsEntry, length}.
275 // We now want {type, length, struct HistTsEntryWithAuthor, length}
276 uint8_t buffer[Entry::kOverhead + sizeof(HistTsEntryWithAuthor)];
277 // Copy content until the point we want to add the author
278 memcpy(buffer, mEntry, sizeof(entry) + sizeof(HistTsEntry));
279 // Copy the author
280 *(int*) (buffer + sizeof(entry) + sizeof(HistTsEntry)) = author;
281 // Update lengths
282 buffer[offsetof(entry, length)] = sizeof(HistTsEntryWithAuthor);
Ivan Lozano9ef855d2018-01-08 15:19:09 -0800283 buffer[offsetof(entry, data) + sizeof(HistTsEntryWithAuthor) + offsetof(ending, length)]
284 = sizeof(HistTsEntryWithAuthor);
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700285 // Write new buffer into FIFO
286 dst->write(buffer, sizeof(buffer));
287 return EntryIterator(mEntry).next();
288}
289
290// ---------------------------------------------------------------------------
291
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800292#if 0 // FIXME see note in NBLog.h
293NBLog::Timeline::Timeline(size_t size, void *shared)
294 : mSize(roundup(size)), mOwn(shared == NULL),
295 mShared((Shared *) (mOwn ? new char[sharedSize(size)] : shared))
296{
297 new (mShared) Shared;
298}
299
300NBLog::Timeline::~Timeline()
301{
302 mShared->~Shared();
303 if (mOwn) {
304 delete[] (char *) mShared;
305 }
306}
307#endif
308
309/*static*/
310size_t NBLog::Timeline::sharedSize(size_t size)
311{
Glenn Kastened99c2b2016-12-12 08:31:24 -0800312 // TODO fifo now supports non-power-of-2 buffer sizes, so could remove the roundup
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800313 return sizeof(Shared) + roundup(size);
314}
315
316// ---------------------------------------------------------------------------
317
318NBLog::Writer::Writer()
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800319 : mShared(NULL), mFifo(NULL), mFifoWriter(NULL), mEnabled(false), mPidTag(NULL), mPidTagSize(0)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800320{
321}
322
Glenn Kasten535e1612016-12-05 12:19:36 -0800323NBLog::Writer::Writer(void *shared, size_t size)
324 : mShared((Shared *) shared),
325 mFifo(mShared != NULL ?
326 new audio_utils_fifo(size, sizeof(uint8_t),
327 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
328 mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL),
329 mEnabled(mFifoWriter != NULL)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800330{
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800331 // caching pid and process name
332 pid_t id = ::getpid();
333 char procName[16];
334 int status = prctl(PR_GET_NAME, procName);
335 if (status) { // error getting process name
336 procName[0] = '\0';
337 }
338 size_t length = strlen(procName);
339 mPidTagSize = length + sizeof(pid_t);
340 mPidTag = new char[mPidTagSize];
341 memcpy(mPidTag, &id, sizeof(pid_t));
342 memcpy(mPidTag + sizeof(pid_t), procName, length);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800343}
344
Glenn Kasten535e1612016-12-05 12:19:36 -0800345NBLog::Writer::Writer(const sp<IMemory>& iMemory, size_t size)
346 : Writer(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800347{
Glenn Kasten535e1612016-12-05 12:19:36 -0800348 mIMemory = iMemory;
349}
350
351NBLog::Writer::~Writer()
352{
353 delete mFifoWriter;
354 delete mFifo;
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800355 delete[] mPidTag;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800356}
357
358void NBLog::Writer::log(const char *string)
359{
Eric Tan86be8722018-08-10 10:25:47 -0700360 if (!mEnabled) return;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800361 LOG_ALWAYS_FATAL_IF(string == NULL, "Attempted to log NULL string");
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800362 size_t length = strlen(string);
Glenn Kasten535e1612016-12-05 12:19:36 -0800363 if (length > Entry::kMaxLength) {
364 length = Entry::kMaxLength;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800365 }
366 log(EVENT_STRING, string, length);
367}
368
369void NBLog::Writer::logf(const char *fmt, ...)
370{
Eric Tan86be8722018-08-10 10:25:47 -0700371 if (!mEnabled) return;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800372 va_list ap;
373 va_start(ap, fmt);
374 Writer::logvf(fmt, ap); // the Writer:: is needed to avoid virtual dispatch for LockedWriter
375 va_end(ap);
376}
377
378void NBLog::Writer::logvf(const char *fmt, va_list ap)
379{
Eric Tan86be8722018-08-10 10:25:47 -0700380 if (!mEnabled) return;
Glenn Kasten535e1612016-12-05 12:19:36 -0800381 char buffer[Entry::kMaxLength + 1 /*NUL*/];
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800382 int length = vsnprintf(buffer, sizeof(buffer), fmt, ap);
383 if (length >= (int) sizeof(buffer)) {
384 length = sizeof(buffer) - 1;
385 // NUL termination is not required
386 // buffer[length] = '\0';
387 }
388 if (length >= 0) {
389 log(EVENT_STRING, buffer, length);
390 }
391}
392
393void NBLog::Writer::logTimestamp()
394{
Eric Tan86be8722018-08-10 10:25:47 -0700395 if (!mEnabled) return;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700396 int64_t ts = get_monotonic_ns();
397 if (ts > 0) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800398 log(EVENT_TIMESTAMP, &ts, sizeof(ts));
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700399 } else {
400 ALOGE("Failed to get timestamp");
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800401 }
402}
403
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700404void NBLog::Writer::logTimestamp(const int64_t ts)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800405{
Eric Tan86be8722018-08-10 10:25:47 -0700406 if (!mEnabled) return;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800407 log(EVENT_TIMESTAMP, &ts, sizeof(ts));
408}
409
410void NBLog::Writer::logInteger(const int x)
411{
Eric Tan86be8722018-08-10 10:25:47 -0700412 if (!mEnabled) return;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800413 log(EVENT_INTEGER, &x, sizeof(x));
414}
415
416void NBLog::Writer::logFloat(const float x)
417{
Eric Tan86be8722018-08-10 10:25:47 -0700418 if (!mEnabled) return;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800419 log(EVENT_FLOAT, &x, sizeof(x));
420}
421
422void NBLog::Writer::logPID()
423{
Eric Tan86be8722018-08-10 10:25:47 -0700424 if (!mEnabled) return;
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800425 log(EVENT_PID, mPidTag, mPidTagSize);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800426}
427
428void NBLog::Writer::logStart(const char *fmt)
429{
Eric Tan86be8722018-08-10 10:25:47 -0700430 if (!mEnabled) return;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800431 size_t length = strlen(fmt);
432 if (length > Entry::kMaxLength) {
433 length = Entry::kMaxLength;
434 }
435 log(EVENT_START_FMT, fmt, length);
436}
437
438void NBLog::Writer::logEnd()
439{
Eric Tan86be8722018-08-10 10:25:47 -0700440 if (!mEnabled) return;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800441 Entry entry = Entry(EVENT_END_FMT, NULL, 0);
Eric Tan86be8722018-08-10 10:25:47 -0700442 log(entry, true);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800443}
444
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700445void NBLog::Writer::logHash(log_hash_t hash)
446{
Eric Tan86be8722018-08-10 10:25:47 -0700447 if (!mEnabled) return;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700448 log(EVENT_HASH, &hash, sizeof(hash));
449}
450
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700451void NBLog::Writer::logEventHistTs(Event event, log_hash_t hash)
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700452{
Eric Tan86be8722018-08-10 10:25:47 -0700453 if (!mEnabled) return;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700454 HistTsEntry data;
455 data.hash = hash;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700456 data.ts = get_monotonic_ns();
457 if (data.ts > 0) {
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700458 log(event, &data, sizeof(data));
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700459 } else {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700460 ALOGE("Failed to get timestamp");
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700461 }
462}
463
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700464void NBLog::Writer::logFormat(const char *fmt, log_hash_t hash, ...)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800465{
Eric Tan86be8722018-08-10 10:25:47 -0700466 if (!mEnabled) return;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800467 va_list ap;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700468 va_start(ap, hash);
469 Writer::logVFormat(fmt, hash, ap);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800470 va_end(ap);
471}
472
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700473void NBLog::Writer::logVFormat(const char *fmt, log_hash_t hash, va_list argp)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800474{
Eric Tan86be8722018-08-10 10:25:47 -0700475 if (!mEnabled) return;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800476 Writer::logStart(fmt);
477 int i;
478 double f;
479 char* s;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700480 int64_t t;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800481 Writer::logTimestamp();
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700482 Writer::logHash(hash);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800483 for (const char *p = fmt; *p != '\0'; p++) {
484 // TODO: implement more complex formatting such as %.3f
485 if (*p != '%') {
486 continue;
487 }
488 switch(*++p) {
489 case 's': // string
490 s = va_arg(argp, char *);
491 Writer::log(s);
492 break;
493
494 case 't': // timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700495 t = va_arg(argp, int64_t);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800496 Writer::logTimestamp(t);
497 break;
498
499 case 'd': // integer
500 i = va_arg(argp, int);
501 Writer::logInteger(i);
502 break;
503
504 case 'f': // float
505 f = va_arg(argp, double); // float arguments are promoted to double in vararg lists
506 Writer::logFloat((float)f);
507 break;
508
509 case 'p': // pid
510 Writer::logPID();
511 break;
512
513 // the "%\0" case finishes parsing
514 case '\0':
515 --p;
516 break;
517
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800518 case '%':
519 break;
520
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800521 default:
522 ALOGW("NBLog Writer parsed invalid format specifier: %c", *p);
523 break;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800524 }
525 }
526 Writer::logEnd();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800527}
528
529void NBLog::Writer::log(Event event, const void *data, size_t length)
530{
Eric Tan86be8722018-08-10 10:25:47 -0700531 if (!mEnabled) return;
Glenn Kasten535e1612016-12-05 12:19:36 -0800532 if (data == NULL || length > Entry::kMaxLength) {
533 // TODO Perhaps it makes sense to display truncated data or at least a
534 // message that the data is too long? The current behavior can create
535 // a confusion for a programmer debugging their code.
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800536 return;
537 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700538 // Ignore if invalid event
539 if (event == EVENT_RESERVED || event >= EVENT_UPPER_BOUND) {
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800540 return;
541 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700542 Entry etr(event, data, length);
Eric Tan86be8722018-08-10 10:25:47 -0700543 log(etr, true /*trusted*/);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800544}
545
Eric Tan86be8722018-08-10 10:25:47 -0700546void NBLog::Writer::log(const NBLog::Entry &etr, bool trusted)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800547{
Eric Tan86be8722018-08-10 10:25:47 -0700548 if (!mEnabled) return;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800549 if (!trusted) {
Eric Tan86be8722018-08-10 10:25:47 -0700550 log(etr.mEvent, etr.mData, etr.mLength);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800551 return;
552 }
Eric Tan86be8722018-08-10 10:25:47 -0700553 size_t need = etr.mLength + Entry::kOverhead; // mEvent, mLength, data[mLength], mLength
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700554 // need = number of bytes written to FIFO
Glenn Kasten535e1612016-12-05 12:19:36 -0800555
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800556 // FIXME optimize this using memcpy for the data part of the Entry.
557 // The Entry could have a method copyTo(ptr, offset, size) to optimize the copy.
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700558 // checks size of a single log Entry: type, length, data pointer and ending
Glenn Kasten535e1612016-12-05 12:19:36 -0800559 uint8_t temp[Entry::kMaxLength + Entry::kOverhead];
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700560 // write this data to temp array
Glenn Kasten535e1612016-12-05 12:19:36 -0800561 for (size_t i = 0; i < need; i++) {
Eric Tan86be8722018-08-10 10:25:47 -0700562 temp[i] = etr.copyEntryDataAt(i);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800563 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700564 // write to circular buffer
Glenn Kasten535e1612016-12-05 12:19:36 -0800565 mFifoWriter->write(temp, need);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800566}
567
568bool NBLog::Writer::isEnabled() const
569{
570 return mEnabled;
571}
572
573bool NBLog::Writer::setEnabled(bool enabled)
574{
575 bool old = mEnabled;
576 mEnabled = enabled && mShared != NULL;
577 return old;
578}
579
580// ---------------------------------------------------------------------------
581
582NBLog::LockedWriter::LockedWriter()
583 : Writer()
584{
585}
586
Glenn Kasten535e1612016-12-05 12:19:36 -0800587NBLog::LockedWriter::LockedWriter(void *shared, size_t size)
588 : Writer(shared, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800589{
590}
591
592void NBLog::LockedWriter::log(const char *string)
593{
594 Mutex::Autolock _l(mLock);
595 Writer::log(string);
596}
597
598void NBLog::LockedWriter::logf(const char *fmt, ...)
599{
600 // FIXME should not take the lock until after formatting is done
601 Mutex::Autolock _l(mLock);
602 va_list ap;
603 va_start(ap, fmt);
604 Writer::logvf(fmt, ap);
605 va_end(ap);
606}
607
608void NBLog::LockedWriter::logvf(const char *fmt, va_list ap)
609{
610 // FIXME should not take the lock until after formatting is done
611 Mutex::Autolock _l(mLock);
612 Writer::logvf(fmt, ap);
613}
614
615void NBLog::LockedWriter::logTimestamp()
616{
617 // FIXME should not take the lock until after the clock_gettime() syscall
618 Mutex::Autolock _l(mLock);
619 Writer::logTimestamp();
620}
621
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700622void NBLog::LockedWriter::logTimestamp(const int64_t ts)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800623{
624 Mutex::Autolock _l(mLock);
625 Writer::logTimestamp(ts);
626}
627
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800628void NBLog::LockedWriter::logInteger(const int x)
629{
630 Mutex::Autolock _l(mLock);
631 Writer::logInteger(x);
632}
633
634void NBLog::LockedWriter::logFloat(const float x)
635{
636 Mutex::Autolock _l(mLock);
637 Writer::logFloat(x);
638}
639
640void NBLog::LockedWriter::logPID()
641{
642 Mutex::Autolock _l(mLock);
643 Writer::logPID();
644}
645
646void NBLog::LockedWriter::logStart(const char *fmt)
647{
648 Mutex::Autolock _l(mLock);
649 Writer::logStart(fmt);
650}
651
652
653void NBLog::LockedWriter::logEnd()
654{
655 Mutex::Autolock _l(mLock);
656 Writer::logEnd();
657}
658
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700659void NBLog::LockedWriter::logHash(log_hash_t hash)
660{
661 Mutex::Autolock _l(mLock);
662 Writer::logHash(hash);
663}
664
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800665bool NBLog::LockedWriter::isEnabled() const
666{
667 Mutex::Autolock _l(mLock);
668 return Writer::isEnabled();
669}
670
671bool NBLog::LockedWriter::setEnabled(bool enabled)
672{
673 Mutex::Autolock _l(mLock);
674 return Writer::setEnabled(enabled);
675}
676
677// ---------------------------------------------------------------------------
678
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700679const std::set<NBLog::Event> NBLog::Reader::startingTypes {NBLog::Event::EVENT_START_FMT,
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700680 NBLog::Event::EVENT_HISTOGRAM_ENTRY_TS,
681 NBLog::Event::EVENT_AUDIO_STATE};
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700682const std::set<NBLog::Event> NBLog::Reader::endingTypes {NBLog::Event::EVENT_END_FMT,
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700683 NBLog::Event::EVENT_HISTOGRAM_ENTRY_TS,
684 NBLog::Event::EVENT_AUDIO_STATE};
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700685
Glenn Kasten535e1612016-12-05 12:19:36 -0800686NBLog::Reader::Reader(const void *shared, size_t size)
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700687 : mFd(-1), mIndent(0), mLost(0),
688 mShared((/*const*/ Shared *) shared), /*mIMemory*/
Glenn Kasten535e1612016-12-05 12:19:36 -0800689 mFifo(mShared != NULL ?
690 new audio_utils_fifo(size, sizeof(uint8_t),
691 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700692 mFifoReader(mFifo != NULL ? new audio_utils_fifo_reader(*mFifo) : NULL)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800693{
694}
695
Glenn Kasten535e1612016-12-05 12:19:36 -0800696NBLog::Reader::Reader(const sp<IMemory>& iMemory, size_t size)
697 : Reader(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800698{
Glenn Kasten535e1612016-12-05 12:19:36 -0800699 mIMemory = iMemory;
700}
701
702NBLog::Reader::~Reader()
703{
704 delete mFifoReader;
705 delete mFifo;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800706}
707
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700708const uint8_t *NBLog::Reader::findLastEntryOfTypes(const uint8_t *front, const uint8_t *back,
709 const std::set<Event> &types) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800710 while (back + Entry::kPreviousLengthOffset >= front) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700711 const uint8_t *prev = back - back[Entry::kPreviousLengthOffset] - Entry::kOverhead;
712 if (prev < front || prev + prev[offsetof(entry, length)] +
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800713 Entry::kOverhead != back) {
714
715 // prev points to an out of limits or inconsistent entry
716 return nullptr;
717 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700718 if (types.find((const Event) prev[offsetof(entry, type)]) != types.end()) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800719 return prev;
720 }
721 back = prev;
722 }
723 return nullptr; // no entry found
724}
725
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700726// Copies content of a Reader FIFO into its Snapshot
727// The Snapshot has the same raw data, but represented as a sequence of entries
728// and an EntryIterator making it possible to process the data.
Nicolas Roulet40a44982017-02-03 13:39:57 -0800729std::unique_ptr<NBLog::Reader::Snapshot> NBLog::Reader::getSnapshot()
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800730{
Glenn Kasten535e1612016-12-05 12:19:36 -0800731 if (mFifoReader == NULL) {
Nicolas Roulet40a44982017-02-03 13:39:57 -0800732 return std::unique_ptr<NBLog::Reader::Snapshot>(new Snapshot());
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800733 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800734
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800735 // This emulates the behaviour of audio_utils_fifo_reader::read, but without incrementing the
736 // reader index. The index is incremented after handling corruption, to after the last complete
737 // entry of the buffer
738 size_t lost;
739 audio_utils_iovec iovec[2];
Eric Tan86be8722018-08-10 10:25:47 -0700740 const ssize_t availToRead = mFifoReader->obtain(iovec, mFifo->capacity(),
741 NULL /*timeout*/, &lost);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800742 if (availToRead <= 0) {
743 return std::unique_ptr<NBLog::Reader::Snapshot>(new Snapshot());
744 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800745
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800746 std::unique_ptr<Snapshot> snapshot(new Snapshot(availToRead));
747 memcpy(snapshot->mData, (const char *) mFifo->buffer() + iovec[0].mOffset, iovec[0].mLength);
748 if (iovec[1].mLength > 0) {
749 memcpy(snapshot->mData + (iovec[0].mLength),
750 (const char *) mFifo->buffer() + iovec[1].mOffset, iovec[1].mLength);
751 }
752
753 // Handle corrupted buffer
754 // Potentially, a buffer has corrupted data on both beginning (due to overflow) and end
755 // (due to incomplete format entry). But even if the end format entry is incomplete,
756 // it ends in a complete entry (which is not an END_FMT). So is safe to traverse backwards.
757 // TODO: handle client corruption (in the middle of a buffer)
758
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700759 const uint8_t *back = snapshot->mData + availToRead;
760 const uint8_t *front = snapshot->mData;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800761
762 // Find last END_FMT. <back> is sitting on an entry which might be the middle of a FormatEntry.
763 // We go backwards until we find an EVENT_END_FMT.
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700764 const uint8_t *lastEnd = findLastEntryOfTypes(front, back, endingTypes);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800765 if (lastEnd == nullptr) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700766 snapshot->mEnd = snapshot->mBegin = EntryIterator(front);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800767 } else {
768 // end of snapshot points to after last END_FMT entry
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700769 snapshot->mEnd = EntryIterator(lastEnd).next();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800770 // find first START_FMT
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700771 const uint8_t *firstStart = nullptr;
772 const uint8_t *firstStartTmp = snapshot->mEnd;
773 while ((firstStartTmp = findLastEntryOfTypes(front, firstStartTmp, startingTypes))
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800774 != nullptr) {
775 firstStart = firstStartTmp;
776 }
777 // firstStart is null if no START_FMT entry was found before lastEnd
778 if (firstStart == nullptr) {
779 snapshot->mBegin = snapshot->mEnd;
780 } else {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700781 snapshot->mBegin = EntryIterator(firstStart);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800782 }
783 }
784
785 // advance fifo reader index to after last entry read.
786 mFifoReader->release(snapshot->mEnd - front);
787
788 snapshot->mLost = lost;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800789 return snapshot;
790}
791
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700792// Takes raw content of the local merger FIFO, processes log entries, and
793// writes the data to a map of class PerformanceAnalysis, based on their thread ID.
794void NBLog::MergeReader::getAndProcessSnapshot(NBLog::Reader::Snapshot &snapshot)
Nicolas Roulet40a44982017-02-03 13:39:57 -0800795{
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700796 String8 timestamp, body;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700797
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800798 for (auto entry = snapshot.begin(); entry != snapshot.end();) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800799 switch (entry->type) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800800 case EVENT_START_FMT:
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800801 entry = handleFormat(FormatEntry(entry), &timestamp, &body);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800802 break;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700803 case EVENT_HISTOGRAM_ENTRY_TS: {
804 HistTsEntryWithAuthor *data = (HistTsEntryWithAuthor *) (entry->data);
805 // TODO This memcpies are here to avoid unaligned memory access crash.
806 // There's probably a more efficient way to do it
807 log_hash_t hash;
808 memcpy(&hash, &(data->hash), sizeof(hash));
Nicolas Rouletad82aa62017-04-03 19:15:20 -0700809 int64_t ts;
810 memcpy(&ts, &data->ts, sizeof(ts));
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700811 // TODO: hash for histogram ts and audio state need to match
812 // and correspond to audio production source file location
813 mThreadPerformanceAnalysis[data->author][0 /*hash*/].logTsEntry(ts);
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700814 ++entry;
815 break;
816 }
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700817 case EVENT_AUDIO_STATE: {
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700818 HistTsEntryWithAuthor *data = (HistTsEntryWithAuthor *) (entry->data);
819 // TODO This memcpies are here to avoid unaligned memory access crash.
820 // There's probably a more efficient way to do it
Sanna Catherine de Treville Wagerd0965172017-07-24 13:42:44 -0700821 log_hash_t hash;
822 memcpy(&hash, &(data->hash), sizeof(hash));
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700823 // TODO: remove ts if unused
824 int64_t ts;
825 memcpy(&ts, &data->ts, sizeof(ts));
826 mThreadPerformanceAnalysis[data->author][0 /*hash*/].handleStateChange();
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700827 ++entry;
828 break;
829 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800830 case EVENT_END_FMT:
831 body.appendFormat("warning: got to end format event");
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800832 ++entry;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800833 break;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800834 case EVENT_RESERVED:
835 default:
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800836 body.appendFormat("warning: unexpected event %d", entry->type);
837 ++entry;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800838 break;
839 }
Sanna Catherine de Treville Wager9484bae2017-06-15 14:39:44 -0700840 }
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700841 // FIXME: decide whether to print the warnings here or elsewhere
Sanna Catherine de Treville Wager9484bae2017-06-15 14:39:44 -0700842 if (!body.isEmpty()) {
Eric Tan86be8722018-08-10 10:25:47 -0700843 dumpLine(&timestamp, &body);
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700844 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800845}
846
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700847void NBLog::MergeReader::getAndProcessSnapshot()
Nicolas Roulet40a44982017-02-03 13:39:57 -0800848{
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700849 // get a snapshot, process it
Nicolas Roulet40a44982017-02-03 13:39:57 -0800850 std::unique_ptr<Snapshot> snap = getSnapshot();
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700851 getAndProcessSnapshot(*snap);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800852}
853
Eric Tan86be8722018-08-10 10:25:47 -0700854void NBLog::MergeReader::dump(int fd, int indent)
855{
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700856 // TODO: add a mutex around media.log dump
Sanna Catherine de Treville Wagercf6c75a2017-07-21 17:05:25 -0700857 ReportPerformance::dump(fd, indent, mThreadPerformanceAnalysis);
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700858}
859
860// Writes a string to the console
Eric Tan86be8722018-08-10 10:25:47 -0700861void NBLog::Reader::dumpLine(const String8 *timestamp, String8 *body)
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700862{
Eric Tan86be8722018-08-10 10:25:47 -0700863 if (timestamp == nullptr || body == nullptr) return;
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700864 if (mFd >= 0) {
Eric Tan86be8722018-08-10 10:25:47 -0700865 dprintf(mFd, "%.*s%s %s\n", mIndent, "", timestamp->string(), body->string());
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700866 } else {
Eric Tan86be8722018-08-10 10:25:47 -0700867 ALOGI("%.*s%s %s", mIndent, "", timestamp->string(), body->string());
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700868 }
Eric Tan86be8722018-08-10 10:25:47 -0700869 body->clear();
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700870}
871
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800872bool NBLog::Reader::isIMemory(const sp<IMemory>& iMemory) const
873{
Glenn Kasten481fb672013-09-30 14:39:28 -0700874 return iMemory != 0 && mIMemory != 0 && iMemory->pointer() == mIMemory->pointer();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800875}
876
Glenn Kasten1c446272017-04-07 09:49:07 -0700877// ---------------------------------------------------------------------------
878
Eric Tan86be8722018-08-10 10:25:47 -0700879void NBLog::appendTimestamp(String8 *body, const void *data)
880{
881 if (body == nullptr || data == nullptr) return;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700882 int64_t ts;
883 memcpy(&ts, data, sizeof(ts));
884 body->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
885 (int) ((ts / (1000 * 1000)) % 1000));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800886}
887
Eric Tan86be8722018-08-10 10:25:47 -0700888void NBLog::appendInt(String8 *body, const void *data)
889{
890 if (body == nullptr || data == nullptr) return;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800891 int x = *((int*) data);
892 body->appendFormat("<%d>", x);
893}
894
Eric Tan86be8722018-08-10 10:25:47 -0700895void NBLog::appendFloat(String8 *body, const void *data)
896{
897 if (body == nullptr || data == nullptr) return;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800898 float f;
Eric Tan86be8722018-08-10 10:25:47 -0700899 memcpy(&f, data, sizeof(f));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800900 body->appendFormat("<%f>", f);
901}
902
Eric Tan86be8722018-08-10 10:25:47 -0700903void NBLog::appendPID(String8 *body, const void* data, size_t length)
904{
905 if (body == nullptr || data == nullptr) return;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800906 pid_t id = *((pid_t*) data);
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800907 char * name = &((char*) data)[sizeof(pid_t)];
908 body->appendFormat("<PID: %d, name: %.*s>", id, (int) (length - sizeof(pid_t)), name);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800909}
910
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700911String8 NBLog::bufferDump(const uint8_t *buffer, size_t size)
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700912{
913 String8 str;
Eric Tan86be8722018-08-10 10:25:47 -0700914 if (buffer == nullptr) return str;
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700915 str.append("[ ");
Eric Tan86be8722018-08-10 10:25:47 -0700916 for(size_t i = 0; i < size; i++) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700917 str.appendFormat("%d ", buffer[i]);
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700918 }
919 str.append("]");
920 return str;
921}
922
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700923String8 NBLog::bufferDump(const EntryIterator &it)
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700924{
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700925 return bufferDump(it, it->length + Entry::kOverhead);
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700926}
927
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700928NBLog::EntryIterator NBLog::Reader::handleFormat(const FormatEntry &fmtEntry,
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800929 String8 *timestamp,
Eric Tan86be8722018-08-10 10:25:47 -0700930 String8 *body)
931{
Nicolas Roulet40a44982017-02-03 13:39:57 -0800932 // log timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700933 int64_t ts = fmtEntry.timestamp();
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800934 timestamp->clear();
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700935 timestamp->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
936 (int) ((ts / (1000 * 1000)) % 1000));
Nicolas Roulet40a44982017-02-03 13:39:57 -0800937
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700938 // log unique hash
939 log_hash_t hash = fmtEntry.hash();
940 // print only lower 16bit of hash as hex and line as int to reduce spam in the log
941 body->appendFormat("%.4X-%d ", (int)(hash >> 16) & 0xFFFF, (int) hash & 0xFFFF);
942
Nicolas Roulet40a44982017-02-03 13:39:57 -0800943 // log author (if present)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800944 handleAuthor(fmtEntry, body);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800945
946 // log string
Eric Tan86be8722018-08-10 10:25:47 -0700947 EntryIterator arg = fmtEntry.args();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800948
949 const char* fmt = fmtEntry.formatString();
950 size_t fmt_length = fmtEntry.formatStringLength();
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800951
952 for (size_t fmt_offset = 0; fmt_offset < fmt_length; ++fmt_offset) {
953 if (fmt[fmt_offset] != '%') {
954 body->append(&fmt[fmt_offset], 1); // TODO optimize to write consecutive strings at once
955 continue;
956 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800957 // case "%%""
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800958 if (fmt[++fmt_offset] == '%') {
959 body->append("%");
960 continue;
961 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800962 // case "%\0"
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800963 if (fmt_offset == fmt_length) {
964 continue;
965 }
966
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800967 NBLog::Event event = (NBLog::Event) arg->type;
968 size_t length = arg->length;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800969
970 // TODO check length for event type is correct
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800971
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800972 if (event == EVENT_END_FMT) {
973 break;
974 }
975
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800976 // TODO: implement more complex formatting such as %.3f
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800977 const uint8_t *datum = arg->data; // pointer to the current event args
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800978 switch(fmt[fmt_offset])
979 {
980 case 's': // string
Nicolas Roulet4da78202017-02-03 12:53:39 -0800981 ALOGW_IF(event != EVENT_STRING,
982 "NBLog Reader incompatible event for string specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800983 body->append((const char*) datum, length);
984 break;
985
986 case 't': // timestamp
Nicolas Roulet4da78202017-02-03 12:53:39 -0800987 ALOGW_IF(event != EVENT_TIMESTAMP,
988 "NBLog Reader incompatible event for timestamp specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800989 appendTimestamp(body, datum);
990 break;
991
992 case 'd': // integer
Nicolas Roulet4da78202017-02-03 12:53:39 -0800993 ALOGW_IF(event != EVENT_INTEGER,
994 "NBLog Reader incompatible event for integer specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800995 appendInt(body, datum);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800996 break;
997
998 case 'f': // float
Nicolas Roulet4da78202017-02-03 12:53:39 -0800999 ALOGW_IF(event != EVENT_FLOAT,
1000 "NBLog Reader incompatible event for float specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001001 appendFloat(body, datum);
1002 break;
1003
1004 case 'p': // pid
Nicolas Roulet4da78202017-02-03 12:53:39 -08001005 ALOGW_IF(event != EVENT_PID,
1006 "NBLog Reader incompatible event for pid specifier: %d", event);
Nicolas Rouletc20cb502017-02-01 12:35:24 -08001007 appendPID(body, datum, length);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001008 break;
1009
1010 default:
1011 ALOGW("NBLog Reader encountered unknown character %c", fmt[fmt_offset]);
1012 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001013 ++arg;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001014 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001015 ALOGW_IF(arg->type != EVENT_END_FMT, "Expected end of format, got %d", arg->type);
1016 ++arg;
1017 return arg;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001018}
1019
Nicolas Roulet40a44982017-02-03 13:39:57 -08001020NBLog::Merger::Merger(const void *shared, size_t size):
Nicolas Roulet40a44982017-02-03 13:39:57 -08001021 mShared((Shared *) shared),
1022 mFifo(mShared != NULL ?
1023 new audio_utils_fifo(size, sizeof(uint8_t),
1024 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
1025 mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL)
Eric Tan86be8722018-08-10 10:25:47 -07001026{
1027}
Nicolas Roulet40a44982017-02-03 13:39:57 -08001028
Eric Tan86be8722018-08-10 10:25:47 -07001029void NBLog::Merger::addReader(const NBLog::NamedReader &reader)
1030{
Glenn Kasten1c446272017-04-07 09:49:07 -07001031 // FIXME This is called by binder thread in MediaLogService::registerWriter
1032 // but the access to shared variable mNamedReaders is not yet protected by a lock.
Nicolas Roulet40a44982017-02-03 13:39:57 -08001033 mNamedReaders.push_back(reader);
1034}
1035
1036// items placed in priority queue during merge
1037// composed by a timestamp and the index of the snapshot where the timestamp came from
1038struct MergeItem
1039{
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001040 int64_t ts;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001041 int index;
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001042 MergeItem(int64_t ts, int index): ts(ts), index(index) {}
Nicolas Roulet40a44982017-02-03 13:39:57 -08001043};
1044
Eric Tan86be8722018-08-10 10:25:47 -07001045bool operator>(const struct MergeItem &i1, const struct MergeItem &i2)
1046{
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001047 return i1.ts > i2.ts || (i1.ts == i2.ts && i1.index > i2.index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001048}
1049
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001050// Merge registered readers, sorted by timestamp, and write data to a single FIFO in local memory
Eric Tan86be8722018-08-10 10:25:47 -07001051void NBLog::Merger::merge()
1052{
Glenn Kasten1c446272017-04-07 09:49:07 -07001053 // FIXME This is called by merge thread
1054 // but the access to shared variable mNamedReaders is not yet protected by a lock.
Eric Tan86be8722018-08-10 10:25:47 -07001055 const int nLogs = mNamedReaders.size();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001056 std::vector<std::unique_ptr<NBLog::Reader::Snapshot>> snapshots(nLogs);
Eric Tan86be8722018-08-10 10:25:47 -07001057 std::vector<EntryIterator> offsets;
1058 offsets.reserve(nLogs);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001059 for (int i = 0; i < nLogs; ++i) {
1060 snapshots[i] = mNamedReaders[i].reader()->getSnapshot();
Eric Tan86be8722018-08-10 10:25:47 -07001061 offsets.push_back(snapshots[i]->begin());
Nicolas Roulet40a44982017-02-03 13:39:57 -08001062 }
1063 // initialize offsets
Nicolas Roulet40a44982017-02-03 13:39:57 -08001064 // TODO custom heap implementation could allow to update top, improving performance
1065 // for bursty buffers
1066 std::priority_queue<MergeItem, std::vector<MergeItem>, std::greater<MergeItem>> timestamps;
1067 for (int i = 0; i < nLogs; ++i)
1068 {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001069 if (offsets[i] != snapshots[i]->end()) {
Eric Tan86be8722018-08-10 10:25:47 -07001070 std::unique_ptr<AbstractEntry> abstractEntry = AbstractEntry::buildEntry(offsets[i]);
1071 if (abstractEntry == nullptr) {
1072 continue;
1073 }
1074 timestamps.emplace(abstractEntry->timestamp(), i);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001075 }
1076 }
1077
1078 while (!timestamps.empty()) {
Eric Tan86be8722018-08-10 10:25:47 -07001079 int index = timestamps.top().index; // find minimum timestamp
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001080 // copy it to the log, increasing offset
Eric Tan86be8722018-08-10 10:25:47 -07001081 offsets[index] = AbstractEntry::buildEntry(offsets[index])->
1082 copyWithAuthor(mFifoWriter, index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001083 // update data structures
Nicolas Roulet40a44982017-02-03 13:39:57 -08001084 timestamps.pop();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001085 if (offsets[index] != snapshots[index]->end()) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001086 int64_t ts = AbstractEntry::buildEntry(offsets[index])->timestamp();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001087 timestamps.emplace(ts, index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001088 }
1089 }
1090}
1091
Eric Tan86be8722018-08-10 10:25:47 -07001092const std::vector<NBLog::NamedReader>& NBLog::Merger::getNamedReaders() const
1093{
Glenn Kasten1c446272017-04-07 09:49:07 -07001094 // FIXME This is returning a reference to a shared variable that needs a lock
1095 return mNamedReaders;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001096}
1097
Glenn Kasten1c446272017-04-07 09:49:07 -07001098// ---------------------------------------------------------------------------
1099
Nicolas Roulet40a44982017-02-03 13:39:57 -08001100NBLog::MergeReader::MergeReader(const void *shared, size_t size, Merger &merger)
Eric Tan86be8722018-08-10 10:25:47 -07001101 : Reader(shared, size), mNamedReaders(merger.getNamedReaders())
1102{
1103}
Nicolas Roulet40a44982017-02-03 13:39:57 -08001104
Eric Tan86be8722018-08-10 10:25:47 -07001105void NBLog::MergeReader::handleAuthor(const NBLog::AbstractEntry &entry, String8 *body)
1106{
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001107 int author = entry.author();
Eric Tan86be8722018-08-10 10:25:47 -07001108 if (author == -1) {
1109 return;
1110 }
Glenn Kasten1c446272017-04-07 09:49:07 -07001111 // FIXME Needs a lock
1112 const char* name = mNamedReaders[author].name();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001113 body->appendFormat("%s: ", name);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001114}
1115
Glenn Kasten1c446272017-04-07 09:49:07 -07001116// ---------------------------------------------------------------------------
1117
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001118NBLog::MergeThread::MergeThread(NBLog::Merger &merger, NBLog::MergeReader &mergeReader)
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001119 : mMerger(merger),
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001120 mMergeReader(mergeReader),
Eric Tan86be8722018-08-10 10:25:47 -07001121 mTimeoutUs(0)
1122{
1123}
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001124
Eric Tan86be8722018-08-10 10:25:47 -07001125NBLog::MergeThread::~MergeThread()
1126{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001127 // set exit flag, set timeout to 0 to force threadLoop to exit and wait for the thread to join
1128 requestExit();
1129 setTimeoutUs(0);
1130 join();
1131}
1132
Eric Tan86be8722018-08-10 10:25:47 -07001133bool NBLog::MergeThread::threadLoop()
1134{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001135 bool doMerge;
1136 {
1137 AutoMutex _l(mMutex);
1138 // If mTimeoutUs is negative, wait on the condition variable until it's positive.
1139 // If it's positive, wait kThreadSleepPeriodUs and then merge
1140 nsecs_t waitTime = mTimeoutUs > 0 ? kThreadSleepPeriodUs * 1000 : LLONG_MAX;
1141 mCond.waitRelative(mMutex, waitTime);
1142 doMerge = mTimeoutUs > 0;
1143 mTimeoutUs -= kThreadSleepPeriodUs;
1144 }
1145 if (doMerge) {
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001146 // Merge data from all the readers
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001147 mMerger.merge();
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001148 // Process the data collected by mMerger and write it to PerformanceAnalysis
1149 // FIXME: decide whether to call getAndProcessSnapshot every time
1150 // or whether to have a separate thread that calls it with a lower frequency
1151 mMergeReader.getAndProcessSnapshot();
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001152 }
1153 return true;
1154}
1155
Eric Tan86be8722018-08-10 10:25:47 -07001156void NBLog::MergeThread::wakeup()
1157{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001158 setTimeoutUs(kThreadWakeupPeriodUs);
1159}
1160
Eric Tan86be8722018-08-10 10:25:47 -07001161void NBLog::MergeThread::setTimeoutUs(int time)
1162{
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001163 AutoMutex _l(mMutex);
1164 mTimeoutUs = time;
1165 mCond.signal();
1166}
1167
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001168} // namespace android