blob: d6fa3e3d0289cc6bf2767894832b190ed785314f [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*/
67std::unique_ptr<NBLog::AbstractEntry> NBLog::AbstractEntry::buildEntry(const uint8_t *ptr) {
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -070068 const uint8_t type = EntryIterator(ptr)->type;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070069 switch (type) {
70 case EVENT_START_FMT:
71 return std::make_unique<FormatEntry>(FormatEntry(ptr));
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -070072 case EVENT_AUDIO_STATE:
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070073 case EVENT_HISTOGRAM_ENTRY_TS:
74 return std::make_unique<HistogramEntry>(HistogramEntry(ptr));
75 default:
76 ALOGW("Tried to create AbstractEntry of type %d", type);
77 return nullptr;
78 }
Nicolas Roulet40a44982017-02-03 13:39:57 -080079}
80
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070081NBLog::AbstractEntry::AbstractEntry(const uint8_t *entry) : mEntry(entry) {
82}
83
84// ---------------------------------------------------------------------------
Nicolas Rouletcd5dd012017-02-13 12:09:28 -080085
Sanna Catherine de Treville Wagerdd92d7e2017-05-15 14:56:53 -070086NBLog::EntryIterator NBLog::FormatEntry::begin() const {
87 return EntryIterator(mEntry);
88}
89
Nicolas Roulet40a44982017-02-03 13:39:57 -080090const char *NBLog::FormatEntry::formatString() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -080091 return (const char*) mEntry + offsetof(entry, data);
Nicolas Roulet40a44982017-02-03 13:39:57 -080092}
93
94size_t NBLog::FormatEntry::formatStringLength() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -080095 return mEntry[offsetof(entry, length)];
Nicolas Roulet40a44982017-02-03 13:39:57 -080096}
97
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070098NBLog::EntryIterator NBLog::FormatEntry::args() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -080099 auto it = begin();
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700100 // skip start fmt
101 ++it;
102 // skip timestamp
103 ++it;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700104 // skip hash
105 ++it;
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700106 // Skip author if present
107 if (it->type == EVENT_AUTHOR) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800108 ++it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800109 }
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700110 return it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800111}
112
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700113int64_t NBLog::FormatEntry::timestamp() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800114 auto it = begin();
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700115 // skip start fmt
116 ++it;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700117 return it.payload<int64_t>();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800118}
119
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700120NBLog::log_hash_t NBLog::FormatEntry::hash() const {
121 auto it = begin();
122 // skip start fmt
123 ++it;
124 // skip timestamp
125 ++it;
126 // unaligned 64-bit read not supported
127 log_hash_t hash;
128 memcpy(&hash, it->data, sizeof(hash));
129 return hash;
130}
131
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700132int NBLog::FormatEntry::author() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800133 auto it = begin();
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700134 // skip start fmt
135 ++it;
136 // skip timestamp
137 ++it;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700138 // skip hash
139 ++it;
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700140 // if there is an author entry, return it, return -1 otherwise
141 if (it->type == EVENT_AUTHOR) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800142 return it.payload<int>();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800143 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800144 return -1;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800145}
146
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700147NBLog::EntryIterator NBLog::FormatEntry::copyWithAuthor(
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800148 std::unique_ptr<audio_utils_fifo_writer> &dst, int author) const {
149 auto it = begin();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800150 // copy fmt start entry
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800151 it.copyTo(dst);
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700152 // copy timestamp
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700153 (++it).copyTo(dst); // copy hash
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700154 (++it).copyTo(dst);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800155 // insert author entry
156 size_t authorEntrySize = NBLog::Entry::kOverhead + sizeof(author);
157 uint8_t authorEntry[authorEntrySize];
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800158 authorEntry[offsetof(entry, type)] = EVENT_AUTHOR;
159 authorEntry[offsetof(entry, length)] =
160 authorEntry[authorEntrySize + NBLog::Entry::kPreviousLengthOffset] =
161 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
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700173void NBLog::EntryIterator::copyTo(std::unique_ptr<audio_utils_fifo_writer> &dst) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800174 size_t length = ptr[offsetof(entry, length)] + NBLog::Entry::kOverhead;
175 dst->write(ptr, length);
176}
Nicolas Roulet40a44982017-02-03 13:39:57 -0800177
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700178void NBLog::EntryIterator::copyData(uint8_t *dst) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800179 memcpy((void*) dst, ptr + offsetof(entry, data), ptr[offsetof(entry, length)]);
180}
181
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700182NBLog::EntryIterator::EntryIterator()
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800183 : ptr(nullptr) {}
184
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700185NBLog::EntryIterator::EntryIterator(const uint8_t *entry)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800186 : ptr(entry) {}
187
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700188NBLog::EntryIterator::EntryIterator(const NBLog::EntryIterator &other)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800189 : ptr(other.ptr) {}
190
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700191const NBLog::entry& NBLog::EntryIterator::operator*() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800192 return *(entry*) ptr;
193}
194
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700195const NBLog::entry* NBLog::EntryIterator::operator->() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800196 return (entry*) ptr;
197}
198
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700199NBLog::EntryIterator& NBLog::EntryIterator::operator++() {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800200 ptr += ptr[offsetof(entry, length)] + NBLog::Entry::kOverhead;
201 return *this;
202}
203
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700204NBLog::EntryIterator& NBLog::EntryIterator::operator--() {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800205 ptr -= ptr[NBLog::Entry::kPreviousLengthOffset] + NBLog::Entry::kOverhead;
206 return *this;
207}
208
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700209NBLog::EntryIterator NBLog::EntryIterator::next() const {
210 EntryIterator aux(*this);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800211 return ++aux;
212}
213
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700214NBLog::EntryIterator NBLog::EntryIterator::prev() const {
215 EntryIterator aux(*this);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800216 return --aux;
217}
218
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700219int NBLog::EntryIterator::operator-(const NBLog::EntryIterator &other) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800220 return ptr - other.ptr;
221}
222
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700223bool NBLog::EntryIterator::operator!=(const EntryIterator &other) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800224 return ptr != other.ptr;
225}
226
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700227bool NBLog::EntryIterator::hasConsistentLength() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800228 return ptr[offsetof(entry, length)] == ptr[ptr[offsetof(entry, length)] +
229 NBLog::Entry::kOverhead + NBLog::Entry::kPreviousLengthOffset];
Nicolas Roulet40a44982017-02-03 13:39:57 -0800230}
231
232// ---------------------------------------------------------------------------
233
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700234int64_t NBLog::HistogramEntry::timestamp() const {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700235 return EntryIterator(mEntry).payload<HistTsEntry>().ts;
236}
237
238NBLog::log_hash_t NBLog::HistogramEntry::hash() const {
239 return EntryIterator(mEntry).payload<HistTsEntry>().hash;
240}
241
242int NBLog::HistogramEntry::author() const {
243 EntryIterator it(mEntry);
244 if (it->length == sizeof(HistTsEntryWithAuthor)) {
245 return it.payload<HistTsEntryWithAuthor>().author;
246 } else {
247 return -1;
248 }
249}
250
251NBLog::EntryIterator NBLog::HistogramEntry::copyWithAuthor(
252 std::unique_ptr<audio_utils_fifo_writer> &dst, int author) const {
253 // Current histogram entry has {type, length, struct HistTsEntry, length}.
254 // We now want {type, length, struct HistTsEntryWithAuthor, length}
255 uint8_t buffer[Entry::kOverhead + sizeof(HistTsEntryWithAuthor)];
256 // Copy content until the point we want to add the author
257 memcpy(buffer, mEntry, sizeof(entry) + sizeof(HistTsEntry));
258 // Copy the author
259 *(int*) (buffer + sizeof(entry) + sizeof(HistTsEntry)) = author;
260 // Update lengths
261 buffer[offsetof(entry, length)] = sizeof(HistTsEntryWithAuthor);
Ivan Lozano9ef855d2018-01-08 15:19:09 -0800262 buffer[offsetof(entry, data) + sizeof(HistTsEntryWithAuthor) + offsetof(ending, length)]
263 = sizeof(HistTsEntryWithAuthor);
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700264 // Write new buffer into FIFO
265 dst->write(buffer, sizeof(buffer));
266 return EntryIterator(mEntry).next();
267}
268
269// ---------------------------------------------------------------------------
270
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800271#if 0 // FIXME see note in NBLog.h
272NBLog::Timeline::Timeline(size_t size, void *shared)
273 : mSize(roundup(size)), mOwn(shared == NULL),
274 mShared((Shared *) (mOwn ? new char[sharedSize(size)] : shared))
275{
276 new (mShared) Shared;
277}
278
279NBLog::Timeline::~Timeline()
280{
281 mShared->~Shared();
282 if (mOwn) {
283 delete[] (char *) mShared;
284 }
285}
286#endif
287
288/*static*/
289size_t NBLog::Timeline::sharedSize(size_t size)
290{
Glenn Kastened99c2b2016-12-12 08:31:24 -0800291 // TODO fifo now supports non-power-of-2 buffer sizes, so could remove the roundup
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800292 return sizeof(Shared) + roundup(size);
293}
294
295// ---------------------------------------------------------------------------
296
297NBLog::Writer::Writer()
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800298 : mShared(NULL), mFifo(NULL), mFifoWriter(NULL), mEnabled(false), mPidTag(NULL), mPidTagSize(0)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800299{
300}
301
Glenn Kasten535e1612016-12-05 12:19:36 -0800302NBLog::Writer::Writer(void *shared, size_t size)
303 : mShared((Shared *) shared),
304 mFifo(mShared != NULL ?
305 new audio_utils_fifo(size, sizeof(uint8_t),
306 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
307 mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL),
308 mEnabled(mFifoWriter != NULL)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800309{
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800310 // caching pid and process name
311 pid_t id = ::getpid();
312 char procName[16];
313 int status = prctl(PR_GET_NAME, procName);
314 if (status) { // error getting process name
315 procName[0] = '\0';
316 }
317 size_t length = strlen(procName);
318 mPidTagSize = length + sizeof(pid_t);
319 mPidTag = new char[mPidTagSize];
320 memcpy(mPidTag, &id, sizeof(pid_t));
321 memcpy(mPidTag + sizeof(pid_t), procName, length);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800322}
323
Glenn Kasten535e1612016-12-05 12:19:36 -0800324NBLog::Writer::Writer(const sp<IMemory>& iMemory, size_t size)
325 : Writer(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800326{
Glenn Kasten535e1612016-12-05 12:19:36 -0800327 mIMemory = iMemory;
328}
329
330NBLog::Writer::~Writer()
331{
332 delete mFifoWriter;
333 delete mFifo;
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800334 delete[] mPidTag;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800335}
336
337void NBLog::Writer::log(const char *string)
338{
339 if (!mEnabled) {
340 return;
341 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800342 LOG_ALWAYS_FATAL_IF(string == NULL, "Attempted to log NULL string");
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800343 size_t length = strlen(string);
Glenn Kasten535e1612016-12-05 12:19:36 -0800344 if (length > Entry::kMaxLength) {
345 length = Entry::kMaxLength;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800346 }
347 log(EVENT_STRING, string, length);
348}
349
350void NBLog::Writer::logf(const char *fmt, ...)
351{
352 if (!mEnabled) {
353 return;
354 }
355 va_list ap;
356 va_start(ap, fmt);
357 Writer::logvf(fmt, ap); // the Writer:: is needed to avoid virtual dispatch for LockedWriter
358 va_end(ap);
359}
360
361void NBLog::Writer::logvf(const char *fmt, va_list ap)
362{
363 if (!mEnabled) {
364 return;
365 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800366 char buffer[Entry::kMaxLength + 1 /*NUL*/];
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800367 int length = vsnprintf(buffer, sizeof(buffer), fmt, ap);
368 if (length >= (int) sizeof(buffer)) {
369 length = sizeof(buffer) - 1;
370 // NUL termination is not required
371 // buffer[length] = '\0';
372 }
373 if (length >= 0) {
374 log(EVENT_STRING, buffer, length);
375 }
376}
377
378void NBLog::Writer::logTimestamp()
379{
380 if (!mEnabled) {
381 return;
382 }
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700383 int64_t ts = get_monotonic_ns();
384 if (ts > 0) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800385 log(EVENT_TIMESTAMP, &ts, sizeof(ts));
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700386 } else {
387 ALOGE("Failed to get timestamp");
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800388 }
389}
390
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700391void NBLog::Writer::logTimestamp(const int64_t ts)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800392{
393 if (!mEnabled) {
394 return;
395 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800396 log(EVENT_TIMESTAMP, &ts, sizeof(ts));
397}
398
399void NBLog::Writer::logInteger(const int x)
400{
401 if (!mEnabled) {
402 return;
403 }
404 log(EVENT_INTEGER, &x, sizeof(x));
405}
406
407void NBLog::Writer::logFloat(const float x)
408{
409 if (!mEnabled) {
410 return;
411 }
412 log(EVENT_FLOAT, &x, sizeof(x));
413}
414
415void NBLog::Writer::logPID()
416{
417 if (!mEnabled) {
418 return;
419 }
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800420 log(EVENT_PID, mPidTag, mPidTagSize);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800421}
422
423void NBLog::Writer::logStart(const char *fmt)
424{
425 if (!mEnabled) {
426 return;
427 }
428 size_t length = strlen(fmt);
429 if (length > Entry::kMaxLength) {
430 length = Entry::kMaxLength;
431 }
432 log(EVENT_START_FMT, fmt, length);
433}
434
435void NBLog::Writer::logEnd()
436{
437 if (!mEnabled) {
438 return;
439 }
440 Entry entry = Entry(EVENT_END_FMT, NULL, 0);
441 log(&entry, true);
442}
443
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700444void NBLog::Writer::logHash(log_hash_t hash)
445{
446 if (!mEnabled) {
447 return;
448 }
449 log(EVENT_HASH, &hash, sizeof(hash));
450}
451
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700452void NBLog::Writer::logEventHistTs(Event event, log_hash_t hash)
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700453{
454 if (!mEnabled) {
455 return;
456 }
457 HistTsEntry data;
458 data.hash = hash;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700459 data.ts = get_monotonic_ns();
460 if (data.ts > 0) {
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700461 log(event, &data, sizeof(data));
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700462 } else {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700463 ALOGE("Failed to get timestamp");
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700464 }
465}
466
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700467void NBLog::Writer::logFormat(const char *fmt, log_hash_t hash, ...)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800468{
469 if (!mEnabled) {
470 return;
471 }
472
473 va_list ap;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700474 va_start(ap, hash);
475 Writer::logVFormat(fmt, hash, ap);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800476 va_end(ap);
477}
478
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700479void NBLog::Writer::logVFormat(const char *fmt, log_hash_t hash, va_list argp)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800480{
481 if (!mEnabled) {
482 return;
483 }
484 Writer::logStart(fmt);
485 int i;
486 double f;
487 char* s;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700488 int64_t t;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800489 Writer::logTimestamp();
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700490 Writer::logHash(hash);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800491 for (const char *p = fmt; *p != '\0'; p++) {
492 // TODO: implement more complex formatting such as %.3f
493 if (*p != '%') {
494 continue;
495 }
496 switch(*++p) {
497 case 's': // string
498 s = va_arg(argp, char *);
499 Writer::log(s);
500 break;
501
502 case 't': // timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700503 t = va_arg(argp, int64_t);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800504 Writer::logTimestamp(t);
505 break;
506
507 case 'd': // integer
508 i = va_arg(argp, int);
509 Writer::logInteger(i);
510 break;
511
512 case 'f': // float
513 f = va_arg(argp, double); // float arguments are promoted to double in vararg lists
514 Writer::logFloat((float)f);
515 break;
516
517 case 'p': // pid
518 Writer::logPID();
519 break;
520
521 // the "%\0" case finishes parsing
522 case '\0':
523 --p;
524 break;
525
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800526 case '%':
527 break;
528
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800529 default:
530 ALOGW("NBLog Writer parsed invalid format specifier: %c", *p);
531 break;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800532 }
533 }
534 Writer::logEnd();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800535}
536
537void NBLog::Writer::log(Event event, const void *data, size_t length)
538{
539 if (!mEnabled) {
540 return;
541 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800542 if (data == NULL || length > Entry::kMaxLength) {
543 // TODO Perhaps it makes sense to display truncated data or at least a
544 // message that the data is too long? The current behavior can create
545 // a confusion for a programmer debugging their code.
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800546 return;
547 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700548 // Ignore if invalid event
549 if (event == EVENT_RESERVED || event >= EVENT_UPPER_BOUND) {
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800550 return;
551 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700552 Entry etr(event, data, length);
553 log(&etr, true /*trusted*/);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800554}
555
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700556void NBLog::Writer::log(const NBLog::Entry *etr, bool trusted)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800557{
558 if (!mEnabled) {
559 return;
560 }
561 if (!trusted) {
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700562 log(etr->mEvent, etr->mData, etr->mLength);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800563 return;
564 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700565 size_t need = etr->mLength + Entry::kOverhead; // mEvent, mLength, data[mLength], mLength
566 // need = number of bytes written to FIFO
Glenn Kasten535e1612016-12-05 12:19:36 -0800567
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800568 // FIXME optimize this using memcpy for the data part of the Entry.
569 // The Entry could have a method copyTo(ptr, offset, size) to optimize the copy.
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700570 // checks size of a single log Entry: type, length, data pointer and ending
Glenn Kasten535e1612016-12-05 12:19:36 -0800571 uint8_t temp[Entry::kMaxLength + Entry::kOverhead];
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700572 // write this data to temp array
Glenn Kasten535e1612016-12-05 12:19:36 -0800573 for (size_t i = 0; i < need; i++) {
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700574 temp[i] = etr->copyEntryDataAt(i);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800575 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700576 // write to circular buffer
Glenn Kasten535e1612016-12-05 12:19:36 -0800577 mFifoWriter->write(temp, need);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800578}
579
580bool NBLog::Writer::isEnabled() const
581{
582 return mEnabled;
583}
584
585bool NBLog::Writer::setEnabled(bool enabled)
586{
587 bool old = mEnabled;
588 mEnabled = enabled && mShared != NULL;
589 return old;
590}
591
592// ---------------------------------------------------------------------------
593
594NBLog::LockedWriter::LockedWriter()
595 : Writer()
596{
597}
598
Glenn Kasten535e1612016-12-05 12:19:36 -0800599NBLog::LockedWriter::LockedWriter(void *shared, size_t size)
600 : Writer(shared, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800601{
602}
603
604void NBLog::LockedWriter::log(const char *string)
605{
606 Mutex::Autolock _l(mLock);
607 Writer::log(string);
608}
609
610void NBLog::LockedWriter::logf(const char *fmt, ...)
611{
612 // FIXME should not take the lock until after formatting is done
613 Mutex::Autolock _l(mLock);
614 va_list ap;
615 va_start(ap, fmt);
616 Writer::logvf(fmt, ap);
617 va_end(ap);
618}
619
620void NBLog::LockedWriter::logvf(const char *fmt, va_list ap)
621{
622 // FIXME should not take the lock until after formatting is done
623 Mutex::Autolock _l(mLock);
624 Writer::logvf(fmt, ap);
625}
626
627void NBLog::LockedWriter::logTimestamp()
628{
629 // FIXME should not take the lock until after the clock_gettime() syscall
630 Mutex::Autolock _l(mLock);
631 Writer::logTimestamp();
632}
633
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700634void NBLog::LockedWriter::logTimestamp(const int64_t ts)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800635{
636 Mutex::Autolock _l(mLock);
637 Writer::logTimestamp(ts);
638}
639
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800640void NBLog::LockedWriter::logInteger(const int x)
641{
642 Mutex::Autolock _l(mLock);
643 Writer::logInteger(x);
644}
645
646void NBLog::LockedWriter::logFloat(const float x)
647{
648 Mutex::Autolock _l(mLock);
649 Writer::logFloat(x);
650}
651
652void NBLog::LockedWriter::logPID()
653{
654 Mutex::Autolock _l(mLock);
655 Writer::logPID();
656}
657
658void NBLog::LockedWriter::logStart(const char *fmt)
659{
660 Mutex::Autolock _l(mLock);
661 Writer::logStart(fmt);
662}
663
664
665void NBLog::LockedWriter::logEnd()
666{
667 Mutex::Autolock _l(mLock);
668 Writer::logEnd();
669}
670
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700671void NBLog::LockedWriter::logHash(log_hash_t hash)
672{
673 Mutex::Autolock _l(mLock);
674 Writer::logHash(hash);
675}
676
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800677bool NBLog::LockedWriter::isEnabled() const
678{
679 Mutex::Autolock _l(mLock);
680 return Writer::isEnabled();
681}
682
683bool NBLog::LockedWriter::setEnabled(bool enabled)
684{
685 Mutex::Autolock _l(mLock);
686 return Writer::setEnabled(enabled);
687}
688
689// ---------------------------------------------------------------------------
690
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700691const std::set<NBLog::Event> NBLog::Reader::startingTypes {NBLog::Event::EVENT_START_FMT,
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700692 NBLog::Event::EVENT_HISTOGRAM_ENTRY_TS,
693 NBLog::Event::EVENT_AUDIO_STATE};
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700694const std::set<NBLog::Event> NBLog::Reader::endingTypes {NBLog::Event::EVENT_END_FMT,
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700695 NBLog::Event::EVENT_HISTOGRAM_ENTRY_TS,
696 NBLog::Event::EVENT_AUDIO_STATE};
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700697
Glenn Kasten535e1612016-12-05 12:19:36 -0800698NBLog::Reader::Reader(const void *shared, size_t size)
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700699 : mFd(-1), mIndent(0), mLost(0),
700 mShared((/*const*/ Shared *) shared), /*mIMemory*/
Glenn Kasten535e1612016-12-05 12:19:36 -0800701 mFifo(mShared != NULL ?
702 new audio_utils_fifo(size, sizeof(uint8_t),
703 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700704 mFifoReader(mFifo != NULL ? new audio_utils_fifo_reader(*mFifo) : NULL)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800705{
706}
707
Glenn Kasten535e1612016-12-05 12:19:36 -0800708NBLog::Reader::Reader(const sp<IMemory>& iMemory, size_t size)
709 : Reader(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800710{
Glenn Kasten535e1612016-12-05 12:19:36 -0800711 mIMemory = iMemory;
712}
713
714NBLog::Reader::~Reader()
715{
716 delete mFifoReader;
717 delete mFifo;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800718}
719
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700720const uint8_t *NBLog::Reader::findLastEntryOfTypes(const uint8_t *front, const uint8_t *back,
721 const std::set<Event> &types) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800722 while (back + Entry::kPreviousLengthOffset >= front) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700723 const uint8_t *prev = back - back[Entry::kPreviousLengthOffset] - Entry::kOverhead;
724 if (prev < front || prev + prev[offsetof(entry, length)] +
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800725 Entry::kOverhead != back) {
726
727 // prev points to an out of limits or inconsistent entry
728 return nullptr;
729 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700730 if (types.find((const Event) prev[offsetof(entry, type)]) != types.end()) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800731 return prev;
732 }
733 back = prev;
734 }
735 return nullptr; // no entry found
736}
737
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700738// Copies content of a Reader FIFO into its Snapshot
739// The Snapshot has the same raw data, but represented as a sequence of entries
740// and an EntryIterator making it possible to process the data.
Nicolas Roulet40a44982017-02-03 13:39:57 -0800741std::unique_ptr<NBLog::Reader::Snapshot> NBLog::Reader::getSnapshot()
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800742{
Glenn Kasten535e1612016-12-05 12:19:36 -0800743 if (mFifoReader == NULL) {
Nicolas Roulet40a44982017-02-03 13:39:57 -0800744 return std::unique_ptr<NBLog::Reader::Snapshot>(new Snapshot());
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800745 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800746 // make a copy to avoid race condition with writer
Glenn Kasten535e1612016-12-05 12:19:36 -0800747 size_t capacity = mFifo->capacity();
748
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800749 // This emulates the behaviour of audio_utils_fifo_reader::read, but without incrementing the
750 // reader index. The index is incremented after handling corruption, to after the last complete
751 // entry of the buffer
752 size_t lost;
753 audio_utils_iovec iovec[2];
754 ssize_t availToRead = mFifoReader->obtain(iovec, capacity, NULL /*timeout*/, &lost);
755 if (availToRead <= 0) {
756 return std::unique_ptr<NBLog::Reader::Snapshot>(new Snapshot());
757 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800758
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800759 std::unique_ptr<Snapshot> snapshot(new Snapshot(availToRead));
760 memcpy(snapshot->mData, (const char *) mFifo->buffer() + iovec[0].mOffset, iovec[0].mLength);
761 if (iovec[1].mLength > 0) {
762 memcpy(snapshot->mData + (iovec[0].mLength),
763 (const char *) mFifo->buffer() + iovec[1].mOffset, iovec[1].mLength);
764 }
765
766 // Handle corrupted buffer
767 // Potentially, a buffer has corrupted data on both beginning (due to overflow) and end
768 // (due to incomplete format entry). But even if the end format entry is incomplete,
769 // it ends in a complete entry (which is not an END_FMT). So is safe to traverse backwards.
770 // TODO: handle client corruption (in the middle of a buffer)
771
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700772 const uint8_t *back = snapshot->mData + availToRead;
773 const uint8_t *front = snapshot->mData;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800774
775 // Find last END_FMT. <back> is sitting on an entry which might be the middle of a FormatEntry.
776 // We go backwards until we find an EVENT_END_FMT.
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700777 const uint8_t *lastEnd = findLastEntryOfTypes(front, back, endingTypes);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800778 if (lastEnd == nullptr) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700779 snapshot->mEnd = snapshot->mBegin = EntryIterator(front);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800780 } else {
781 // end of snapshot points to after last END_FMT entry
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700782 snapshot->mEnd = EntryIterator(lastEnd).next();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800783 // find first START_FMT
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700784 const uint8_t *firstStart = nullptr;
785 const uint8_t *firstStartTmp = snapshot->mEnd;
786 while ((firstStartTmp = findLastEntryOfTypes(front, firstStartTmp, startingTypes))
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800787 != nullptr) {
788 firstStart = firstStartTmp;
789 }
790 // firstStart is null if no START_FMT entry was found before lastEnd
791 if (firstStart == nullptr) {
792 snapshot->mBegin = snapshot->mEnd;
793 } else {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700794 snapshot->mBegin = EntryIterator(firstStart);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800795 }
796 }
797
798 // advance fifo reader index to after last entry read.
799 mFifoReader->release(snapshot->mEnd - front);
800
801 snapshot->mLost = lost;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800802 return snapshot;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800803
Nicolas Roulet40a44982017-02-03 13:39:57 -0800804}
805
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700806// Takes raw content of the local merger FIFO, processes log entries, and
807// writes the data to a map of class PerformanceAnalysis, based on their thread ID.
808void NBLog::MergeReader::getAndProcessSnapshot(NBLog::Reader::Snapshot &snapshot)
Nicolas Roulet40a44982017-02-03 13:39:57 -0800809{
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700810 String8 timestamp, body;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700811
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800812 for (auto entry = snapshot.begin(); entry != snapshot.end();) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800813 switch (entry->type) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800814 case EVENT_START_FMT:
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800815 entry = handleFormat(FormatEntry(entry), &timestamp, &body);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800816 break;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700817 case EVENT_HISTOGRAM_ENTRY_TS: {
818 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
821 log_hash_t hash;
822 memcpy(&hash, &(data->hash), sizeof(hash));
Nicolas Rouletad82aa62017-04-03 19:15:20 -0700823 int64_t ts;
824 memcpy(&ts, &data->ts, sizeof(ts));
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700825 // TODO: hash for histogram ts and audio state need to match
826 // and correspond to audio production source file location
827 mThreadPerformanceAnalysis[data->author][0 /*hash*/].logTsEntry(ts);
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700828 ++entry;
829 break;
830 }
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700831 case EVENT_AUDIO_STATE: {
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700832 HistTsEntryWithAuthor *data = (HistTsEntryWithAuthor *) (entry->data);
833 // TODO This memcpies are here to avoid unaligned memory access crash.
834 // There's probably a more efficient way to do it
Sanna Catherine de Treville Wagerd0965172017-07-24 13:42:44 -0700835 log_hash_t hash;
836 memcpy(&hash, &(data->hash), sizeof(hash));
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700837 // TODO: remove ts if unused
838 int64_t ts;
839 memcpy(&ts, &data->ts, sizeof(ts));
840 mThreadPerformanceAnalysis[data->author][0 /*hash*/].handleStateChange();
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700841 ++entry;
842 break;
843 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800844 case EVENT_END_FMT:
845 body.appendFormat("warning: got to end format event");
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800846 ++entry;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800847 break;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800848 case EVENT_RESERVED:
849 default:
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800850 body.appendFormat("warning: unexpected event %d", entry->type);
851 ++entry;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800852 break;
853 }
Sanna Catherine de Treville Wager9484bae2017-06-15 14:39:44 -0700854 }
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700855 // FIXME: decide whether to print the warnings here or elsewhere
Sanna Catherine de Treville Wager9484bae2017-06-15 14:39:44 -0700856 if (!body.isEmpty()) {
857 dumpLine(timestamp, body);
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700858 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800859}
860
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700861void NBLog::MergeReader::getAndProcessSnapshot()
Nicolas Roulet40a44982017-02-03 13:39:57 -0800862{
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700863 // get a snapshot, process it
Nicolas Roulet40a44982017-02-03 13:39:57 -0800864 std::unique_ptr<Snapshot> snap = getSnapshot();
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700865 getAndProcessSnapshot(*snap);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800866}
867
Sanna Catherine de Treville Wagercf6c75a2017-07-21 17:05:25 -0700868void NBLog::MergeReader::dump(int fd, int indent) {
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700869 // TODO: add a mutex around media.log dump
Sanna Catherine de Treville Wagercf6c75a2017-07-21 17:05:25 -0700870 ReportPerformance::dump(fd, indent, mThreadPerformanceAnalysis);
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700871}
872
873// Writes a string to the console
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800874void NBLog::Reader::dumpLine(const String8 &timestamp, String8 &body)
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700875{
876 if (mFd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700877 dprintf(mFd, "%.*s%s %s\n", mIndent, "", timestamp.string(), body.string());
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700878 } else {
879 ALOGI("%.*s%s %s", mIndent, "", timestamp.string(), body.string());
880 }
881 body.clear();
882}
883
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800884bool NBLog::Reader::isIMemory(const sp<IMemory>& iMemory) const
885{
Glenn Kasten481fb672013-09-30 14:39:28 -0700886 return iMemory != 0 && mIMemory != 0 && iMemory->pointer() == mIMemory->pointer();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800887}
888
Glenn Kasten1c446272017-04-07 09:49:07 -0700889// ---------------------------------------------------------------------------
890
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800891void NBLog::appendTimestamp(String8 *body, const void *data) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700892 int64_t ts;
893 memcpy(&ts, data, sizeof(ts));
894 body->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
895 (int) ((ts / (1000 * 1000)) % 1000));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800896}
897
898void NBLog::appendInt(String8 *body, const void *data) {
899 int x = *((int*) data);
900 body->appendFormat("<%d>", x);
901}
902
903void NBLog::appendFloat(String8 *body, const void *data) {
904 float f;
905 memcpy(&f, data, sizeof(float));
906 body->appendFormat("<%f>", f);
907}
908
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800909void NBLog::appendPID(String8 *body, const void* data, size_t length) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800910 pid_t id = *((pid_t*) data);
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800911 char * name = &((char*) data)[sizeof(pid_t)];
912 body->appendFormat("<PID: %d, name: %.*s>", id, (int) (length - sizeof(pid_t)), name);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800913}
914
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700915String8 NBLog::bufferDump(const uint8_t *buffer, size_t size)
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700916{
917 String8 str;
918 str.append("[ ");
919 for(size_t i = 0; i < size; i++)
920 {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700921 str.appendFormat("%d ", buffer[i]);
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700922 }
923 str.append("]");
924 return str;
925}
926
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700927String8 NBLog::bufferDump(const EntryIterator &it)
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700928{
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700929 return bufferDump(it, it->length + Entry::kOverhead);
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700930}
931
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700932NBLog::EntryIterator NBLog::Reader::handleFormat(const FormatEntry &fmtEntry,
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800933 String8 *timestamp,
934 String8 *body) {
Nicolas Roulet40a44982017-02-03 13:39:57 -0800935 // log timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700936 int64_t ts = fmtEntry.timestamp();
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800937 timestamp->clear();
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700938 timestamp->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
939 (int) ((ts / (1000 * 1000)) % 1000));
Nicolas Roulet40a44982017-02-03 13:39:57 -0800940
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700941 // log unique hash
942 log_hash_t hash = fmtEntry.hash();
943 // print only lower 16bit of hash as hex and line as int to reduce spam in the log
944 body->appendFormat("%.4X-%d ", (int)(hash >> 16) & 0xFFFF, (int) hash & 0xFFFF);
945
Nicolas Roulet40a44982017-02-03 13:39:57 -0800946 // log author (if present)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800947 handleAuthor(fmtEntry, body);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800948
949 // log string
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700950 NBLog::EntryIterator arg = fmtEntry.args();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800951
952 const char* fmt = fmtEntry.formatString();
953 size_t fmt_length = fmtEntry.formatStringLength();
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800954
955 for (size_t fmt_offset = 0; fmt_offset < fmt_length; ++fmt_offset) {
956 if (fmt[fmt_offset] != '%') {
957 body->append(&fmt[fmt_offset], 1); // TODO optimize to write consecutive strings at once
958 continue;
959 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800960 // case "%%""
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800961 if (fmt[++fmt_offset] == '%') {
962 body->append("%");
963 continue;
964 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800965 // case "%\0"
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800966 if (fmt_offset == fmt_length) {
967 continue;
968 }
969
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800970 NBLog::Event event = (NBLog::Event) arg->type;
971 size_t length = arg->length;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800972
973 // TODO check length for event type is correct
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800974
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800975 if (event == EVENT_END_FMT) {
976 break;
977 }
978
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800979 // TODO: implement more complex formatting such as %.3f
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800980 const uint8_t *datum = arg->data; // pointer to the current event args
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800981 switch(fmt[fmt_offset])
982 {
983 case 's': // string
Nicolas Roulet4da78202017-02-03 12:53:39 -0800984 ALOGW_IF(event != EVENT_STRING,
985 "NBLog Reader incompatible event for string specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800986 body->append((const char*) datum, length);
987 break;
988
989 case 't': // timestamp
Nicolas Roulet4da78202017-02-03 12:53:39 -0800990 ALOGW_IF(event != EVENT_TIMESTAMP,
991 "NBLog Reader incompatible event for timestamp specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800992 appendTimestamp(body, datum);
993 break;
994
995 case 'd': // integer
Nicolas Roulet4da78202017-02-03 12:53:39 -0800996 ALOGW_IF(event != EVENT_INTEGER,
997 "NBLog Reader incompatible event for integer specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800998 appendInt(body, datum);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800999 break;
1000
1001 case 'f': // float
Nicolas Roulet4da78202017-02-03 12:53:39 -08001002 ALOGW_IF(event != EVENT_FLOAT,
1003 "NBLog Reader incompatible event for float specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001004 appendFloat(body, datum);
1005 break;
1006
1007 case 'p': // pid
Nicolas Roulet4da78202017-02-03 12:53:39 -08001008 ALOGW_IF(event != EVENT_PID,
1009 "NBLog Reader incompatible event for pid specifier: %d", event);
Nicolas Rouletc20cb502017-02-01 12:35:24 -08001010 appendPID(body, datum, length);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001011 break;
1012
1013 default:
1014 ALOGW("NBLog Reader encountered unknown character %c", fmt[fmt_offset]);
1015 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001016 ++arg;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001017 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001018 ALOGW_IF(arg->type != EVENT_END_FMT, "Expected end of format, got %d", arg->type);
1019 ++arg;
1020 return arg;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001021}
1022
Nicolas Roulet40a44982017-02-03 13:39:57 -08001023NBLog::Merger::Merger(const void *shared, size_t size):
Nicolas Roulet40a44982017-02-03 13:39:57 -08001024 mShared((Shared *) shared),
1025 mFifo(mShared != NULL ?
1026 new audio_utils_fifo(size, sizeof(uint8_t),
1027 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
1028 mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL)
1029 {}
1030
1031void NBLog::Merger::addReader(const NBLog::NamedReader &reader) {
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001032
Glenn Kasten1c446272017-04-07 09:49:07 -07001033 // FIXME This is called by binder thread in MediaLogService::registerWriter
1034 // but the access to shared variable mNamedReaders is not yet protected by a lock.
Nicolas Roulet40a44982017-02-03 13:39:57 -08001035 mNamedReaders.push_back(reader);
1036}
1037
1038// items placed in priority queue during merge
1039// composed by a timestamp and the index of the snapshot where the timestamp came from
1040struct MergeItem
1041{
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001042 int64_t ts;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001043 int index;
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001044 MergeItem(int64_t ts, int index): ts(ts), index(index) {}
Nicolas Roulet40a44982017-02-03 13:39:57 -08001045};
1046
1047// operators needed for priority queue in merge
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001048// bool operator>(const int64_t &t1, const int64_t &t2) {
1049// return t1.tv_sec > t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec);
1050// }
Nicolas Roulet40a44982017-02-03 13:39:57 -08001051
1052bool operator>(const struct MergeItem &i1, const struct MergeItem &i2) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001053 return i1.ts > i2.ts || (i1.ts == i2.ts && i1.index > i2.index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001054}
1055
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001056// Merge registered readers, sorted by timestamp, and write data to a single FIFO in local memory
Nicolas Roulet40a44982017-02-03 13:39:57 -08001057void NBLog::Merger::merge() {
Glenn Kasten1c446272017-04-07 09:49:07 -07001058 // FIXME This is called by merge thread
1059 // but the access to shared variable mNamedReaders is not yet protected by a lock.
Nicolas Roulet40a44982017-02-03 13:39:57 -08001060 int nLogs = mNamedReaders.size();
1061 std::vector<std::unique_ptr<NBLog::Reader::Snapshot>> snapshots(nLogs);
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001062 std::vector<NBLog::EntryIterator> offsets(nLogs);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001063 for (int i = 0; i < nLogs; ++i) {
1064 snapshots[i] = mNamedReaders[i].reader()->getSnapshot();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001065 offsets[i] = snapshots[i]->begin();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001066 }
1067 // initialize offsets
Nicolas Roulet40a44982017-02-03 13:39:57 -08001068 // TODO custom heap implementation could allow to update top, improving performance
1069 // for bursty buffers
1070 std::priority_queue<MergeItem, std::vector<MergeItem>, std::greater<MergeItem>> timestamps;
1071 for (int i = 0; i < nLogs; ++i)
1072 {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001073 if (offsets[i] != snapshots[i]->end()) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001074 int64_t ts = AbstractEntry::buildEntry(offsets[i])->timestamp();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001075 timestamps.emplace(ts, i);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001076 }
1077 }
1078
1079 while (!timestamps.empty()) {
1080 // find minimum timestamp
1081 int index = timestamps.top().index;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001082 // copy it to the log, increasing offset
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001083 offsets[index] = AbstractEntry::buildEntry(offsets[index])->copyWithAuthor(mFifoWriter,
1084 index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001085 // update data structures
Nicolas Roulet40a44982017-02-03 13:39:57 -08001086 timestamps.pop();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001087 if (offsets[index] != snapshots[index]->end()) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001088 int64_t ts = AbstractEntry::buildEntry(offsets[index])->timestamp();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001089 timestamps.emplace(ts, index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001090 }
1091 }
1092}
1093
Glenn Kasten1c446272017-04-07 09:49:07 -07001094const std::vector<NBLog::NamedReader>& NBLog::Merger::getNamedReaders() const {
1095 // FIXME This is returning a reference to a shared variable that needs a lock
1096 return mNamedReaders;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001097}
1098
Glenn Kasten1c446272017-04-07 09:49:07 -07001099// ---------------------------------------------------------------------------
1100
Nicolas Roulet40a44982017-02-03 13:39:57 -08001101NBLog::MergeReader::MergeReader(const void *shared, size_t size, Merger &merger)
1102 : Reader(shared, size), mNamedReaders(merger.getNamedReaders()) {}
1103
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001104void NBLog::MergeReader::handleAuthor(const NBLog::AbstractEntry &entry, String8 *body) {
1105 int author = entry.author();
Glenn Kasten1c446272017-04-07 09:49:07 -07001106 // FIXME Needs a lock
1107 const char* name = mNamedReaders[author].name();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001108 body->appendFormat("%s: ", name);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001109}
1110
Glenn Kasten1c446272017-04-07 09:49:07 -07001111// ---------------------------------------------------------------------------
1112
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001113NBLog::MergeThread::MergeThread(NBLog::Merger &merger, NBLog::MergeReader &mergeReader)
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001114 : mMerger(merger),
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001115 mMergeReader(mergeReader),
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001116 mTimeoutUs(0) {}
1117
1118NBLog::MergeThread::~MergeThread() {
1119 // set exit flag, set timeout to 0 to force threadLoop to exit and wait for the thread to join
1120 requestExit();
1121 setTimeoutUs(0);
1122 join();
1123}
1124
1125bool NBLog::MergeThread::threadLoop() {
1126 bool doMerge;
1127 {
1128 AutoMutex _l(mMutex);
1129 // If mTimeoutUs is negative, wait on the condition variable until it's positive.
1130 // If it's positive, wait kThreadSleepPeriodUs and then merge
1131 nsecs_t waitTime = mTimeoutUs > 0 ? kThreadSleepPeriodUs * 1000 : LLONG_MAX;
1132 mCond.waitRelative(mMutex, waitTime);
1133 doMerge = mTimeoutUs > 0;
1134 mTimeoutUs -= kThreadSleepPeriodUs;
1135 }
1136 if (doMerge) {
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001137 // Merge data from all the readers
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001138 mMerger.merge();
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001139 // Process the data collected by mMerger and write it to PerformanceAnalysis
1140 // FIXME: decide whether to call getAndProcessSnapshot every time
1141 // or whether to have a separate thread that calls it with a lower frequency
1142 mMergeReader.getAndProcessSnapshot();
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001143 }
1144 return true;
1145}
1146
1147void NBLog::MergeThread::wakeup() {
1148 setTimeoutUs(kThreadWakeupPeriodUs);
1149}
1150
1151void NBLog::MergeThread::setTimeoutUs(int time) {
1152 AutoMutex _l(mMutex);
1153 mTimeoutUs = time;
1154 mCond.signal();
1155}
1156
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001157} // namespace android