blob: 48ffb0110537c874c947e8cef0296526a3f56899 [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.
15 */
16
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070017/*
18* Documentation: Workflow summary for histogram data processing:
19* For more details on FIFO, please see system/media/audio_utils; doxygen
20* TODO: add this documentation to doxygen once it is further developed
21* 1) writing the data to a buffer
22* onWork
23* Called every period length (e.g., 4ms)
24* Calls LOG_HIST_TS
25* LOG_HIST_TS
26* Hashes file name and line number
27* calls NBLOG::Writer::logHistTS once
28* NBLOG::Writer::logHistTS
29* calls NBLOG::Writer::log on hash and current timestamp
30* time is in CLOCK_MONOTONIC converted to ns
31* NBLOG::Writer::log(Event, const void*, size_t)
32* Initializes Entry, a struct containing one log entry
33* Entry contains the event type (mEvent), data length (mLength),
34* and data pointer (mData)
35* TODO: why mLength (max length of buffer data) must be <= kMaxLength = 255?
36* calls NBLOG::Writer::log(Entry *, bool)
37* NBLog::Writer::log(Entry *, bool)
38* Calls copyEntryDataAt to format data as follows in temp array:
39* [type][length][data ... ][length]
40* calls audio_utils_fifo_writer.write on temp
41* audio_utils_fifo_writer.write
42* calls obtain(), memcpy (reference in doxygen)
43* returns number of frames written
44* ssize_t audio_utils_fifo_reader::obtain
45* Determines readable buffer section via pointer arithmetic on reader
46* and writer pointers
47*
48* 2) reading the data from shared memory
49* Thread::threadloop()
50* TODO: add description?
51* NBLog::MergeThread::threadLoop()
52* calls NBLog::Merger::merge
53* NBLog::Merger::merge
54* for each reader in vector of class NamedReader,
55* callsNamedReader::reader()->getSnapshot
56* TODO: check whether the rest of this function is relevant
57* NBLog::Reader::getSnapshot
58* copies snapshot of reader's fifo buffer into its own buffer
59* calls mFifoReader->obtain to find readable data
60* sets snapshot.begin() and .end() iterators to boundaries of valid entries
61* moves the fifo reader index to after the last entry read
62* in this case, the buffer is in shared memory. in (3), the buffer is private
63*
64* 3) reading the data from private buffer
65* MediaLogService::dump
66* calls NBLog::Reader::dump(int) on instance of subclass mergeReader
67* NBLog::Reader::dump(int)
68* calls getSnapshot on the current reader
69* calls dump(int, size_t, Snapshot)
70* NBLog::Reader::dump(int, size, snapshot)
71* iterates through snapshot's events and switches based on their type
72* (string, timestamp, etc...)
73* In the case of EVENT_HISTOGRAM_ENTRY_TS, adds a list of timestamp sequences
74* (histogram entry) to NBLog::mHists
75* In the case of EVENT_HISTOGRAM_FLUSH, calls drawHistogram on each element in
76* the list and erases it
77* TODO: when do these events occur?
78* NBLog::drawHistogram
79* input: timestamp array
80* buckets this to a histogram and prints
81*
82*/
83
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080084#define LOG_TAG "NBLog"
85//#define LOG_NDEBUG 0
86
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070087#include <algorithm>
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -080088#include <climits>
Sanna Catherine de Treville Wager201079a2017-05-18 16:36:29 -070089#include <deque>
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070090#include <fstream>
91// #include <inttypes.h>
92#include <iostream>
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -070093#include <math.h>
Sanna Catherine de Treville Wager201079a2017-05-18 16:36:29 -070094#include <numeric>
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070095#include <vector>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080096#include <stdarg.h>
97#include <stdint.h>
98#include <stdio.h>
99#include <string.h>
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800100#include <sys/prctl.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800101#include <time.h>
102#include <new>
Glenn Kasten535e1612016-12-05 12:19:36 -0800103#include <audio_utils/roundup.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800104#include <media/nbaio/NBLog.h>
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700105// #include <utils/CallStack.h> // used to print callstack
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800106#include <utils/Log.h>
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700107#include <utils/String8.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800108
Nicolas Roulet40a44982017-02-03 13:39:57 -0800109#include <queue>
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700110#include <utility>
Nicolas Roulet40a44982017-02-03 13:39:57 -0800111
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800112namespace android {
113
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700114int NBLog::Entry::copyEntryDataAt(size_t offset) const
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800115{
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700116 // FIXME This is too slow
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800117 if (offset == 0)
118 return mEvent;
119 else if (offset == 1)
120 return mLength;
121 else if (offset < (size_t) (mLength + 2))
122 return ((char *) mData)[offset - 2];
123 else if (offset == (size_t) (mLength + 2))
124 return mLength;
125 else
126 return 0;
127}
128
129// ---------------------------------------------------------------------------
130
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700131/*static*/
132std::unique_ptr<NBLog::AbstractEntry> NBLog::AbstractEntry::buildEntry(const uint8_t *ptr) {
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -0700133 const uint8_t type = EntryIterator(ptr)->type;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700134 switch (type) {
135 case EVENT_START_FMT:
136 return std::make_unique<FormatEntry>(FormatEntry(ptr));
137 case EVENT_HISTOGRAM_FLUSH:
138 case EVENT_HISTOGRAM_ENTRY_TS:
139 return std::make_unique<HistogramEntry>(HistogramEntry(ptr));
140 default:
141 ALOGW("Tried to create AbstractEntry of type %d", type);
142 return nullptr;
143 }
Nicolas Roulet40a44982017-02-03 13:39:57 -0800144}
145
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700146NBLog::AbstractEntry::AbstractEntry(const uint8_t *entry) : mEntry(entry) {
147}
148
149// ---------------------------------------------------------------------------
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800150
Sanna Catherine de Treville Wagerdd92d7e2017-05-15 14:56:53 -0700151NBLog::EntryIterator NBLog::FormatEntry::begin() const {
152 return EntryIterator(mEntry);
153}
154
Nicolas Roulet40a44982017-02-03 13:39:57 -0800155const char *NBLog::FormatEntry::formatString() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800156 return (const char*) mEntry + offsetof(entry, data);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800157}
158
159size_t NBLog::FormatEntry::formatStringLength() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800160 return mEntry[offsetof(entry, length)];
Nicolas Roulet40a44982017-02-03 13:39:57 -0800161}
162
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700163NBLog::EntryIterator NBLog::FormatEntry::args() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800164 auto it = begin();
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700165 // skip start fmt
166 ++it;
167 // skip timestamp
168 ++it;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700169 // skip hash
170 ++it;
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700171 // Skip author if present
172 if (it->type == EVENT_AUTHOR) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800173 ++it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800174 }
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700175 return it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800176}
177
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700178int64_t NBLog::FormatEntry::timestamp() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800179 auto it = begin();
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700180 // skip start fmt
181 ++it;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700182 return it.payload<int64_t>();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800183}
184
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700185NBLog::log_hash_t NBLog::FormatEntry::hash() const {
186 auto it = begin();
187 // skip start fmt
188 ++it;
189 // skip timestamp
190 ++it;
191 // unaligned 64-bit read not supported
192 log_hash_t hash;
193 memcpy(&hash, it->data, sizeof(hash));
194 return hash;
195}
196
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700197int NBLog::FormatEntry::author() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800198 auto it = begin();
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700199 // skip start fmt
200 ++it;
201 // skip timestamp
202 ++it;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700203 // skip hash
204 ++it;
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700205 // if there is an author entry, return it, return -1 otherwise
206 if (it->type == EVENT_AUTHOR) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800207 return it.payload<int>();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800208 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800209 return -1;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800210}
211
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700212NBLog::EntryIterator NBLog::FormatEntry::copyWithAuthor(
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800213 std::unique_ptr<audio_utils_fifo_writer> &dst, int author) const {
214 auto it = begin();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800215 // copy fmt start entry
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800216 it.copyTo(dst);
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700217 // copy timestamp
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700218 (++it).copyTo(dst); // copy hash
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700219 (++it).copyTo(dst);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800220 // insert author entry
221 size_t authorEntrySize = NBLog::Entry::kOverhead + sizeof(author);
222 uint8_t authorEntry[authorEntrySize];
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800223 authorEntry[offsetof(entry, type)] = EVENT_AUTHOR;
224 authorEntry[offsetof(entry, length)] =
225 authorEntry[authorEntrySize + NBLog::Entry::kPreviousLengthOffset] =
226 sizeof(author);
227 *(int*) (&authorEntry[offsetof(entry, data)]) = author;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800228 dst->write(authorEntry, authorEntrySize);
229 // copy rest of entries
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800230 while ((++it)->type != EVENT_END_FMT) {
231 it.copyTo(dst);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800232 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800233 it.copyTo(dst);
234 ++it;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800235 return it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800236}
237
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700238void NBLog::EntryIterator::copyTo(std::unique_ptr<audio_utils_fifo_writer> &dst) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800239 size_t length = ptr[offsetof(entry, length)] + NBLog::Entry::kOverhead;
240 dst->write(ptr, length);
241}
Nicolas Roulet40a44982017-02-03 13:39:57 -0800242
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700243void NBLog::EntryIterator::copyData(uint8_t *dst) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800244 memcpy((void*) dst, ptr + offsetof(entry, data), ptr[offsetof(entry, length)]);
245}
246
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700247NBLog::EntryIterator::EntryIterator()
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800248 : ptr(nullptr) {}
249
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700250NBLog::EntryIterator::EntryIterator(const uint8_t *entry)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800251 : ptr(entry) {}
252
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700253NBLog::EntryIterator::EntryIterator(const NBLog::EntryIterator &other)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800254 : ptr(other.ptr) {}
255
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700256const NBLog::entry& NBLog::EntryIterator::operator*() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800257 return *(entry*) ptr;
258}
259
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700260const NBLog::entry* NBLog::EntryIterator::operator->() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800261 return (entry*) ptr;
262}
263
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700264NBLog::EntryIterator& NBLog::EntryIterator::operator++() {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800265 ptr += ptr[offsetof(entry, length)] + NBLog::Entry::kOverhead;
266 return *this;
267}
268
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700269NBLog::EntryIterator& NBLog::EntryIterator::operator--() {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800270 ptr -= ptr[NBLog::Entry::kPreviousLengthOffset] + NBLog::Entry::kOverhead;
271 return *this;
272}
273
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700274NBLog::EntryIterator NBLog::EntryIterator::next() const {
275 EntryIterator aux(*this);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800276 return ++aux;
277}
278
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700279NBLog::EntryIterator NBLog::EntryIterator::prev() const {
280 EntryIterator aux(*this);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800281 return --aux;
282}
283
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700284int NBLog::EntryIterator::operator-(const NBLog::EntryIterator &other) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800285 return ptr - other.ptr;
286}
287
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700288bool NBLog::EntryIterator::operator!=(const EntryIterator &other) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800289 return ptr != other.ptr;
290}
291
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700292bool NBLog::EntryIterator::hasConsistentLength() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800293 return ptr[offsetof(entry, length)] == ptr[ptr[offsetof(entry, length)] +
294 NBLog::Entry::kOverhead + NBLog::Entry::kPreviousLengthOffset];
Nicolas Roulet40a44982017-02-03 13:39:57 -0800295}
296
297// ---------------------------------------------------------------------------
298
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700299int64_t NBLog::HistogramEntry::timestamp() const {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700300 return EntryIterator(mEntry).payload<HistTsEntry>().ts;
301}
302
303NBLog::log_hash_t NBLog::HistogramEntry::hash() const {
304 return EntryIterator(mEntry).payload<HistTsEntry>().hash;
305}
306
307int NBLog::HistogramEntry::author() const {
308 EntryIterator it(mEntry);
309 if (it->length == sizeof(HistTsEntryWithAuthor)) {
310 return it.payload<HistTsEntryWithAuthor>().author;
311 } else {
312 return -1;
313 }
314}
315
316NBLog::EntryIterator NBLog::HistogramEntry::copyWithAuthor(
317 std::unique_ptr<audio_utils_fifo_writer> &dst, int author) const {
318 // Current histogram entry has {type, length, struct HistTsEntry, length}.
319 // We now want {type, length, struct HistTsEntryWithAuthor, length}
320 uint8_t buffer[Entry::kOverhead + sizeof(HistTsEntryWithAuthor)];
321 // Copy content until the point we want to add the author
322 memcpy(buffer, mEntry, sizeof(entry) + sizeof(HistTsEntry));
323 // Copy the author
324 *(int*) (buffer + sizeof(entry) + sizeof(HistTsEntry)) = author;
325 // Update lengths
326 buffer[offsetof(entry, length)] = sizeof(HistTsEntryWithAuthor);
327 buffer[sizeof(buffer) + Entry::kPreviousLengthOffset] = sizeof(HistTsEntryWithAuthor);
328 // Write new buffer into FIFO
329 dst->write(buffer, sizeof(buffer));
330 return EntryIterator(mEntry).next();
331}
332
333// ---------------------------------------------------------------------------
334
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800335#if 0 // FIXME see note in NBLog.h
336NBLog::Timeline::Timeline(size_t size, void *shared)
337 : mSize(roundup(size)), mOwn(shared == NULL),
338 mShared((Shared *) (mOwn ? new char[sharedSize(size)] : shared))
339{
340 new (mShared) Shared;
341}
342
343NBLog::Timeline::~Timeline()
344{
345 mShared->~Shared();
346 if (mOwn) {
347 delete[] (char *) mShared;
348 }
349}
350#endif
351
352/*static*/
353size_t NBLog::Timeline::sharedSize(size_t size)
354{
Glenn Kastened99c2b2016-12-12 08:31:24 -0800355 // TODO fifo now supports non-power-of-2 buffer sizes, so could remove the roundup
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800356 return sizeof(Shared) + roundup(size);
357}
358
359// ---------------------------------------------------------------------------
360
361NBLog::Writer::Writer()
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800362 : mShared(NULL), mFifo(NULL), mFifoWriter(NULL), mEnabled(false), mPidTag(NULL), mPidTagSize(0)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800363{
364}
365
Glenn Kasten535e1612016-12-05 12:19:36 -0800366NBLog::Writer::Writer(void *shared, size_t size)
367 : mShared((Shared *) shared),
368 mFifo(mShared != NULL ?
369 new audio_utils_fifo(size, sizeof(uint8_t),
370 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
371 mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL),
372 mEnabled(mFifoWriter != NULL)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800373{
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800374 // caching pid and process name
375 pid_t id = ::getpid();
376 char procName[16];
377 int status = prctl(PR_GET_NAME, procName);
378 if (status) { // error getting process name
379 procName[0] = '\0';
380 }
381 size_t length = strlen(procName);
382 mPidTagSize = length + sizeof(pid_t);
383 mPidTag = new char[mPidTagSize];
384 memcpy(mPidTag, &id, sizeof(pid_t));
385 memcpy(mPidTag + sizeof(pid_t), procName, length);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800386}
387
Glenn Kasten535e1612016-12-05 12:19:36 -0800388NBLog::Writer::Writer(const sp<IMemory>& iMemory, size_t size)
389 : Writer(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800390{
Glenn Kasten535e1612016-12-05 12:19:36 -0800391 mIMemory = iMemory;
392}
393
394NBLog::Writer::~Writer()
395{
396 delete mFifoWriter;
397 delete mFifo;
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800398 delete[] mPidTag;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800399}
400
401void NBLog::Writer::log(const char *string)
402{
403 if (!mEnabled) {
404 return;
405 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800406 LOG_ALWAYS_FATAL_IF(string == NULL, "Attempted to log NULL string");
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800407 size_t length = strlen(string);
Glenn Kasten535e1612016-12-05 12:19:36 -0800408 if (length > Entry::kMaxLength) {
409 length = Entry::kMaxLength;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800410 }
411 log(EVENT_STRING, string, length);
412}
413
414void NBLog::Writer::logf(const char *fmt, ...)
415{
416 if (!mEnabled) {
417 return;
418 }
419 va_list ap;
420 va_start(ap, fmt);
421 Writer::logvf(fmt, ap); // the Writer:: is needed to avoid virtual dispatch for LockedWriter
422 va_end(ap);
423}
424
425void NBLog::Writer::logvf(const char *fmt, va_list ap)
426{
427 if (!mEnabled) {
428 return;
429 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800430 char buffer[Entry::kMaxLength + 1 /*NUL*/];
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800431 int length = vsnprintf(buffer, sizeof(buffer), fmt, ap);
432 if (length >= (int) sizeof(buffer)) {
433 length = sizeof(buffer) - 1;
434 // NUL termination is not required
435 // buffer[length] = '\0';
436 }
437 if (length >= 0) {
438 log(EVENT_STRING, buffer, length);
439 }
440}
441
442void NBLog::Writer::logTimestamp()
443{
444 if (!mEnabled) {
445 return;
446 }
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700447 int64_t ts = get_monotonic_ns();
448 if (ts > 0) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800449 log(EVENT_TIMESTAMP, &ts, sizeof(ts));
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700450 } else {
451 ALOGE("Failed to get timestamp");
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800452 }
453}
454
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700455void NBLog::Writer::logTimestamp(const int64_t ts)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800456{
457 if (!mEnabled) {
458 return;
459 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800460 log(EVENT_TIMESTAMP, &ts, sizeof(ts));
461}
462
463void NBLog::Writer::logInteger(const int x)
464{
465 if (!mEnabled) {
466 return;
467 }
468 log(EVENT_INTEGER, &x, sizeof(x));
469}
470
471void NBLog::Writer::logFloat(const float x)
472{
473 if (!mEnabled) {
474 return;
475 }
476 log(EVENT_FLOAT, &x, sizeof(x));
477}
478
479void NBLog::Writer::logPID()
480{
481 if (!mEnabled) {
482 return;
483 }
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800484 log(EVENT_PID, mPidTag, mPidTagSize);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800485}
486
487void NBLog::Writer::logStart(const char *fmt)
488{
489 if (!mEnabled) {
490 return;
491 }
492 size_t length = strlen(fmt);
493 if (length > Entry::kMaxLength) {
494 length = Entry::kMaxLength;
495 }
496 log(EVENT_START_FMT, fmt, length);
497}
498
499void NBLog::Writer::logEnd()
500{
501 if (!mEnabled) {
502 return;
503 }
504 Entry entry = Entry(EVENT_END_FMT, NULL, 0);
505 log(&entry, true);
506}
507
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700508void NBLog::Writer::logHash(log_hash_t hash)
509{
510 if (!mEnabled) {
511 return;
512 }
513 log(EVENT_HASH, &hash, sizeof(hash));
514}
515
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700516void NBLog::Writer::logHistTS(log_hash_t hash)
517{
518 if (!mEnabled) {
519 return;
520 }
521 HistTsEntry data;
522 data.hash = hash;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700523 data.ts = get_monotonic_ns();
524 if (data.ts > 0) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700525 log(EVENT_HISTOGRAM_ENTRY_TS, &data, sizeof(data));
526 } else {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700527 ALOGE("Failed to get timestamp");
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700528 }
529}
530
531void NBLog::Writer::logHistFlush(log_hash_t hash)
532{
533 if (!mEnabled) {
534 return;
535 }
536 HistTsEntry data;
537 data.hash = hash;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700538 data.ts = get_monotonic_ns();
539 if (data.ts > 0) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700540 log(EVENT_HISTOGRAM_FLUSH, &data, sizeof(data));
541 } else {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700542 ALOGE("Failed to get timestamp");
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700543 }
544}
545
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700546void NBLog::Writer::logFormat(const char *fmt, log_hash_t hash, ...)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800547{
548 if (!mEnabled) {
549 return;
550 }
551
552 va_list ap;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700553 va_start(ap, hash);
554 Writer::logVFormat(fmt, hash, ap);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800555 va_end(ap);
556}
557
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700558void NBLog::Writer::logVFormat(const char *fmt, log_hash_t hash, va_list argp)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800559{
560 if (!mEnabled) {
561 return;
562 }
563 Writer::logStart(fmt);
564 int i;
565 double f;
566 char* s;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700567 int64_t t;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800568 Writer::logTimestamp();
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700569 Writer::logHash(hash);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800570 for (const char *p = fmt; *p != '\0'; p++) {
571 // TODO: implement more complex formatting such as %.3f
572 if (*p != '%') {
573 continue;
574 }
575 switch(*++p) {
576 case 's': // string
577 s = va_arg(argp, char *);
578 Writer::log(s);
579 break;
580
581 case 't': // timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700582 t = va_arg(argp, int64_t);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800583 Writer::logTimestamp(t);
584 break;
585
586 case 'd': // integer
587 i = va_arg(argp, int);
588 Writer::logInteger(i);
589 break;
590
591 case 'f': // float
592 f = va_arg(argp, double); // float arguments are promoted to double in vararg lists
593 Writer::logFloat((float)f);
594 break;
595
596 case 'p': // pid
597 Writer::logPID();
598 break;
599
600 // the "%\0" case finishes parsing
601 case '\0':
602 --p;
603 break;
604
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800605 case '%':
606 break;
607
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800608 default:
609 ALOGW("NBLog Writer parsed invalid format specifier: %c", *p);
610 break;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800611 }
612 }
613 Writer::logEnd();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800614}
615
616void NBLog::Writer::log(Event event, const void *data, size_t length)
617{
618 if (!mEnabled) {
619 return;
620 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800621 if (data == NULL || length > Entry::kMaxLength) {
622 // TODO Perhaps it makes sense to display truncated data or at least a
623 // message that the data is too long? The current behavior can create
624 // a confusion for a programmer debugging their code.
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800625 return;
626 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700627 // Ignore if invalid event
628 if (event == EVENT_RESERVED || event >= EVENT_UPPER_BOUND) {
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800629 return;
630 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700631 Entry etr(event, data, length);
632 log(&etr, true /*trusted*/);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800633}
634
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700635void NBLog::Writer::log(const NBLog::Entry *etr, bool trusted)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800636{
637 if (!mEnabled) {
638 return;
639 }
640 if (!trusted) {
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700641 log(etr->mEvent, etr->mData, etr->mLength);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800642 return;
643 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700644 size_t need = etr->mLength + Entry::kOverhead; // mEvent, mLength, data[mLength], mLength
645 // need = number of bytes written to FIFO
Glenn Kasten535e1612016-12-05 12:19:36 -0800646
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800647 // FIXME optimize this using memcpy for the data part of the Entry.
648 // The Entry could have a method copyTo(ptr, offset, size) to optimize the copy.
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700649 // checks size of a single log Entry: type, length, data pointer and ending
Glenn Kasten535e1612016-12-05 12:19:36 -0800650 uint8_t temp[Entry::kMaxLength + Entry::kOverhead];
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700651 // write this data to temp array
Glenn Kasten535e1612016-12-05 12:19:36 -0800652 for (size_t i = 0; i < need; i++) {
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700653 temp[i] = etr->copyEntryDataAt(i);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800654 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700655 // write to circular buffer
Glenn Kasten535e1612016-12-05 12:19:36 -0800656 mFifoWriter->write(temp, need);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800657}
658
659bool NBLog::Writer::isEnabled() const
660{
661 return mEnabled;
662}
663
664bool NBLog::Writer::setEnabled(bool enabled)
665{
666 bool old = mEnabled;
667 mEnabled = enabled && mShared != NULL;
668 return old;
669}
670
671// ---------------------------------------------------------------------------
672
673NBLog::LockedWriter::LockedWriter()
674 : Writer()
675{
676}
677
Glenn Kasten535e1612016-12-05 12:19:36 -0800678NBLog::LockedWriter::LockedWriter(void *shared, size_t size)
679 : Writer(shared, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800680{
681}
682
683void NBLog::LockedWriter::log(const char *string)
684{
685 Mutex::Autolock _l(mLock);
686 Writer::log(string);
687}
688
689void NBLog::LockedWriter::logf(const char *fmt, ...)
690{
691 // FIXME should not take the lock until after formatting is done
692 Mutex::Autolock _l(mLock);
693 va_list ap;
694 va_start(ap, fmt);
695 Writer::logvf(fmt, ap);
696 va_end(ap);
697}
698
699void NBLog::LockedWriter::logvf(const char *fmt, va_list ap)
700{
701 // FIXME should not take the lock until after formatting is done
702 Mutex::Autolock _l(mLock);
703 Writer::logvf(fmt, ap);
704}
705
706void NBLog::LockedWriter::logTimestamp()
707{
708 // FIXME should not take the lock until after the clock_gettime() syscall
709 Mutex::Autolock _l(mLock);
710 Writer::logTimestamp();
711}
712
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700713void NBLog::LockedWriter::logTimestamp(const int64_t ts)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800714{
715 Mutex::Autolock _l(mLock);
716 Writer::logTimestamp(ts);
717}
718
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800719void NBLog::LockedWriter::logInteger(const int x)
720{
721 Mutex::Autolock _l(mLock);
722 Writer::logInteger(x);
723}
724
725void NBLog::LockedWriter::logFloat(const float x)
726{
727 Mutex::Autolock _l(mLock);
728 Writer::logFloat(x);
729}
730
731void NBLog::LockedWriter::logPID()
732{
733 Mutex::Autolock _l(mLock);
734 Writer::logPID();
735}
736
737void NBLog::LockedWriter::logStart(const char *fmt)
738{
739 Mutex::Autolock _l(mLock);
740 Writer::logStart(fmt);
741}
742
743
744void NBLog::LockedWriter::logEnd()
745{
746 Mutex::Autolock _l(mLock);
747 Writer::logEnd();
748}
749
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700750void NBLog::LockedWriter::logHash(log_hash_t hash)
751{
752 Mutex::Autolock _l(mLock);
753 Writer::logHash(hash);
754}
755
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800756bool NBLog::LockedWriter::isEnabled() const
757{
758 Mutex::Autolock _l(mLock);
759 return Writer::isEnabled();
760}
761
762bool NBLog::LockedWriter::setEnabled(bool enabled)
763{
764 Mutex::Autolock _l(mLock);
765 return Writer::setEnabled(enabled);
766}
767
768// ---------------------------------------------------------------------------
769
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700770const std::set<NBLog::Event> NBLog::Reader::startingTypes {NBLog::Event::EVENT_START_FMT,
771 NBLog::Event::EVENT_HISTOGRAM_ENTRY_TS};
772const std::set<NBLog::Event> NBLog::Reader::endingTypes {NBLog::Event::EVENT_END_FMT,
773 NBLog::Event::EVENT_HISTOGRAM_ENTRY_TS,
774 NBLog::Event::EVENT_HISTOGRAM_FLUSH};
Glenn Kasten535e1612016-12-05 12:19:36 -0800775NBLog::Reader::Reader(const void *shared, size_t size)
776 : mShared((/*const*/ Shared *) shared), /*mIMemory*/
777 mFd(-1), mIndent(0),
778 mFifo(mShared != NULL ?
779 new audio_utils_fifo(size, sizeof(uint8_t),
780 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
Sanna Catherine de Treville Wager201079a2017-05-18 16:36:29 -0700781 mFifoReader(mFifo != NULL ? new audio_utils_fifo_reader(*mFifo) : NULL),
782 findGlitch(false)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800783{
784}
785
Glenn Kasten535e1612016-12-05 12:19:36 -0800786NBLog::Reader::Reader(const sp<IMemory>& iMemory, size_t size)
787 : Reader(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800788{
Glenn Kasten535e1612016-12-05 12:19:36 -0800789 mIMemory = iMemory;
790}
791
792NBLog::Reader::~Reader()
793{
794 delete mFifoReader;
795 delete mFifo;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800796}
797
Sanna Catherine de Treville Wager201079a2017-05-18 16:36:29 -0700798inline static int deltaMs(int64_t ns1, int64_t ns2) {
799 return (ns2 - ns1) / (1000 * 1000);
800}
801
802// Produces a log warning if the timing of recent buffer periods caused a glitch
803// Computes sum of running window of three buffer periods
804// Checks whether the buffer periods leave enough CPU time for the next one
805// e.g. if a buffer period is expected to be 4 ms and a buffer requires 3 ms of CPU time,
806// here are some glitch cases:
807// 4 + 4 + 6 ; 5 + 4 + 5; 2 + 2 + 10
808// TODO: develop this code to track changes in histogram distribution in addition
809// to / instead of glitches
810void NBLog::Reader::alertIfGlitch(const std::vector<int64_t> &samples) {
811 //TODO: measure kPeriodLen and kRatio from the data as they may change.
812 static const int kPeriodLen = 4; // current period length is ideally 4 ms
813 static const double kRatio = 0.75; // estimate of CPU time as ratio of period length
814 // DAC processing time for 4 ms buffer
815 static const int kPeriodTime = static_cast<int>(round(kPeriodLen * kRatio));
816 static const int kNumBuff = 3; // number of buffers considered in local history
817 std::deque<int> periods(kNumBuff, kPeriodLen);
818 for (size_t i = 2; i < samples.size(); ++i) { // skip first time entry
819 periods.push_front(deltaMs(samples[i - 1], samples[i]));
820 periods.pop_back();
821 // TODO: check that all glitch cases are covered
822 if (std::accumulate(periods.begin(), periods.end(), 0) > kNumBuff * kPeriodLen +
823 kPeriodLen - kPeriodTime) {
824 ALOGW("A glitch occurred");
825 periods.assign(kNumBuff, kPeriodLen);
826 }
827 }
828 return;
829}
830
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700831const uint8_t *NBLog::Reader::findLastEntryOfTypes(const uint8_t *front, const uint8_t *back,
832 const std::set<Event> &types) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800833 while (back + Entry::kPreviousLengthOffset >= front) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700834 const uint8_t *prev = back - back[Entry::kPreviousLengthOffset] - Entry::kOverhead;
835 if (prev < front || prev + prev[offsetof(entry, length)] +
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800836 Entry::kOverhead != back) {
837
838 // prev points to an out of limits or inconsistent entry
839 return nullptr;
840 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700841 if (types.find((const Event) prev[offsetof(entry, type)]) != types.end()) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800842 return prev;
843 }
844 back = prev;
845 }
846 return nullptr; // no entry found
847}
848
Nicolas Roulet40a44982017-02-03 13:39:57 -0800849std::unique_ptr<NBLog::Reader::Snapshot> NBLog::Reader::getSnapshot()
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800850{
Glenn Kasten535e1612016-12-05 12:19:36 -0800851 if (mFifoReader == NULL) {
Nicolas Roulet40a44982017-02-03 13:39:57 -0800852 return std::unique_ptr<NBLog::Reader::Snapshot>(new Snapshot());
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800853 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800854 // make a copy to avoid race condition with writer
Glenn Kasten535e1612016-12-05 12:19:36 -0800855 size_t capacity = mFifo->capacity();
856
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800857 // This emulates the behaviour of audio_utils_fifo_reader::read, but without incrementing the
858 // reader index. The index is incremented after handling corruption, to after the last complete
859 // entry of the buffer
860 size_t lost;
861 audio_utils_iovec iovec[2];
862 ssize_t availToRead = mFifoReader->obtain(iovec, capacity, NULL /*timeout*/, &lost);
863 if (availToRead <= 0) {
864 return std::unique_ptr<NBLog::Reader::Snapshot>(new Snapshot());
865 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800866
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800867 std::unique_ptr<Snapshot> snapshot(new Snapshot(availToRead));
868 memcpy(snapshot->mData, (const char *) mFifo->buffer() + iovec[0].mOffset, iovec[0].mLength);
869 if (iovec[1].mLength > 0) {
870 memcpy(snapshot->mData + (iovec[0].mLength),
871 (const char *) mFifo->buffer() + iovec[1].mOffset, iovec[1].mLength);
872 }
873
874 // Handle corrupted buffer
875 // Potentially, a buffer has corrupted data on both beginning (due to overflow) and end
876 // (due to incomplete format entry). But even if the end format entry is incomplete,
877 // it ends in a complete entry (which is not an END_FMT). So is safe to traverse backwards.
878 // TODO: handle client corruption (in the middle of a buffer)
879
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700880 const uint8_t *back = snapshot->mData + availToRead;
881 const uint8_t *front = snapshot->mData;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800882
883 // Find last END_FMT. <back> is sitting on an entry which might be the middle of a FormatEntry.
884 // We go backwards until we find an EVENT_END_FMT.
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700885 const uint8_t *lastEnd = findLastEntryOfTypes(front, back, endingTypes);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800886 if (lastEnd == nullptr) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700887 snapshot->mEnd = snapshot->mBegin = EntryIterator(front);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800888 } else {
889 // end of snapshot points to after last END_FMT entry
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700890 snapshot->mEnd = EntryIterator(lastEnd).next();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800891 // find first START_FMT
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700892 const uint8_t *firstStart = nullptr;
893 const uint8_t *firstStartTmp = snapshot->mEnd;
894 while ((firstStartTmp = findLastEntryOfTypes(front, firstStartTmp, startingTypes))
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800895 != nullptr) {
896 firstStart = firstStartTmp;
897 }
898 // firstStart is null if no START_FMT entry was found before lastEnd
899 if (firstStart == nullptr) {
900 snapshot->mBegin = snapshot->mEnd;
901 } else {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700902 snapshot->mBegin = EntryIterator(firstStart);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800903 }
904 }
905
906 // advance fifo reader index to after last entry read.
907 mFifoReader->release(snapshot->mEnd - front);
908
909 snapshot->mLost = lost;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800910 return snapshot;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800911
Nicolas Roulet40a44982017-02-03 13:39:57 -0800912}
913
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -0700914// writes sample deltas to file, either truncating or appending
915inline void writeHistToFile(const std::vector<int64_t> &samples, bool append) {
916 // name of file on audioserver
917 static const char* const kName = (char *)"/data/misc/audioserver/sample_results.txt";
918 // stores deltas between the samples
919 std::vector<int64_t> intervals;
920 if (samples.size() == 0) return;
921 for (size_t i = 1; i < samples.size(); ++i) {
922 intervals.push_back(deltaMs(samples[i - 1], samples[i]));
923 }
924 // Deletes maximum value in a histogram. Temp quick fix.
925 // FIXME: need to find root cause of approx. 35th element from the end
926 // consistently being an outlier in the first histogram of a flush
927 // ALOGW("%" PRId64 "before", (int64_t) *(std::max_element(intervals.begin(), intervals.end())));
928 intervals.erase(std::max_element(intervals.begin(), intervals.end()));
929 // ALOGW("%" PRId64 "after", (int64_t) *(std::max_element(intervals.begin(), intervals.end())));
930 std::ofstream ofs;
931 ofs.open(kName, append ? std::ios::app : std::ios::trunc);
932 if (!ofs) {
933 ALOGW("couldn't open file %s", kName);
934 return;
935 }
936 for (size_t i = 0; i < intervals.size(); ++i) {
937 ofs << intervals[i] << "\n";
938 }
939 ofs.close();
940}
941
Nicolas Roulet40a44982017-02-03 13:39:57 -0800942void NBLog::Reader::dump(int fd, size_t indent, NBLog::Reader::Snapshot &snapshot)
943{
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700944 // CallStack cs(LOG_TAG);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800945#if 0
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800946 struct timespec ts;
947 time_t maxSec = -1;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800948 while (entry - start >= (int) Entry::kOverhead) {
949 if (prevEntry - start < 0 || !prevEntry.hasConsistentLength()) {
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800950 break;
951 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800952 if (prevEntry->type == EVENT_TIMESTAMP) {
953 if (prevEntry->length != sizeof(struct timespec)) {
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800954 // corrupt
955 break;
956 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800957 prevEntry.copyData((uint8_t*) &ts);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800958 if (ts.tv_sec > maxSec) {
959 maxSec = ts.tv_sec;
960 }
961 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800962 --entry;
963 --prevEntry;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800964 }
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800965#endif
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700966 mFd = fd;
967 mIndent = indent;
968 String8 timestamp, body;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700969 size_t lost = snapshot.lost() + (snapshot.begin() - EntryIterator(snapshot.data()));
Glenn Kastenc02c9612013-10-15 09:25:11 -0700970 if (lost > 0) {
Glenn Kasten95d287d2014-04-28 14:11:45 -0700971 body.appendFormat("warning: lost %zu bytes worth of events", lost);
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700972 // TODO timestamp empty here, only other choice to wait for the first timestamp event in the
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700973 // log to push it out. Consider keeping the timestamp/body between calls to copyEntryDataAt().
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700974 dumpLine(timestamp, body);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800975 }
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800976#if 0
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800977 size_t width = 1;
978 while (maxSec >= 10) {
979 ++width;
980 maxSec /= 10;
981 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800982 if (maxSec >= 0) {
Glenn Kasten95d287d2014-04-28 14:11:45 -0700983 timestamp.appendFormat("[%*s]", (int) width + 4, "");
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800984 }
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700985 bool deferredTimestamp = false;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800986#endif
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700987
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800988 for (auto entry = snapshot.begin(); entry != snapshot.end();) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800989 switch (entry->type) {
990#if 0
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800991 case EVENT_STRING:
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800992 body.appendFormat("%.*s", (int) entry.length(), entry.data());
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700993 break;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800994 case EVENT_TIMESTAMP: {
995 // already checked that length == sizeof(struct timespec);
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800996 entry.copyData((const uint8_t*) &ts);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800997 long prevNsec = ts.tv_nsec;
998 long deltaMin = LONG_MAX;
999 long deltaMax = -1;
1000 long deltaTotal = 0;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001001 auto aux(entry);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001002 for (;;) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001003 ++aux;
1004 if (end - aux >= 0 || aux.type() != EVENT_TIMESTAMP) {
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001005 break;
1006 }
1007 struct timespec tsNext;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001008 aux.copyData((const uint8_t*) &tsNext);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001009 if (tsNext.tv_sec != ts.tv_sec) {
1010 break;
1011 }
1012 long delta = tsNext.tv_nsec - prevNsec;
1013 if (delta < 0) {
1014 break;
1015 }
1016 if (delta < deltaMin) {
1017 deltaMin = delta;
1018 }
1019 if (delta > deltaMax) {
1020 deltaMax = delta;
1021 }
1022 deltaTotal += delta;
1023 prevNsec = tsNext.tv_nsec;
1024 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001025 size_t n = (aux - entry) / (sizeof(struct timespec) + 3 /*Entry::kOverhead?*/);
Glenn Kasten4e01ef62013-07-11 14:29:59 -07001026 if (deferredTimestamp) {
1027 dumpLine(timestamp, body);
1028 deferredTimestamp = false;
1029 }
1030 timestamp.clear();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001031 if (n >= kSquashTimestamp) {
Glenn Kasten4e01ef62013-07-11 14:29:59 -07001032 timestamp.appendFormat("[%d.%03d to .%.03d by .%.03d to .%.03d]",
1033 (int) ts.tv_sec, (int) (ts.tv_nsec / 1000000),
1034 (int) ((ts.tv_nsec + deltaTotal) / 1000000),
1035 (int) (deltaMin / 1000000), (int) (deltaMax / 1000000));
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001036 entry = aux;
1037 // advance = 0;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001038 break;
1039 }
Glenn Kasten4e01ef62013-07-11 14:29:59 -07001040 timestamp.appendFormat("[%d.%03d]", (int) ts.tv_sec,
1041 (int) (ts.tv_nsec / 1000000));
1042 deferredTimestamp = true;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001043 }
1044 break;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001045 case EVENT_INTEGER:
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001046 appendInt(&body, entry.data());
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001047 break;
1048 case EVENT_FLOAT:
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001049 appendFloat(&body, entry.data());
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001050 break;
1051 case EVENT_PID:
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001052 appendPID(&body, entry.data(), entry.length());
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001053 break;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001054#endif
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001055 case EVENT_START_FMT:
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001056 entry = handleFormat(FormatEntry(entry), &timestamp, &body);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001057 break;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001058 case EVENT_HISTOGRAM_ENTRY_TS: {
1059 HistTsEntryWithAuthor *data = (HistTsEntryWithAuthor *) (entry->data);
1060 // TODO This memcpies are here to avoid unaligned memory access crash.
1061 // There's probably a more efficient way to do it
1062 log_hash_t hash;
1063 memcpy(&hash, &(data->hash), sizeof(hash));
Nicolas Rouletad82aa62017-04-03 19:15:20 -07001064 int64_t ts;
1065 memcpy(&ts, &data->ts, sizeof(ts));
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001066 const std::pair<log_hash_t, int> key(hash, data->author);
Nicolas Rouletad82aa62017-04-03 19:15:20 -07001067 // TODO might want to filter excessively high outliers, which are usually caused
1068 // by the thread being inactive.
1069 mHists[key].push_back(ts);
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001070 ++entry;
1071 break;
1072 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -07001073 // draws histograms stored in global Reader::mHists and erases them
Nicolas Rouletad82aa62017-04-03 19:15:20 -07001074 case EVENT_HISTOGRAM_FLUSH: {
1075 HistogramEntry histEntry(entry);
1076 // Log timestamp
Sanna Catherine de Treville Wager201079a2017-05-18 16:36:29 -07001077 // Timestamp of call to drawHistogram, not when audio was generated
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -07001078 const int64_t ts = histEntry.timestamp();
Nicolas Rouletad82aa62017-04-03 19:15:20 -07001079 timestamp.clear();
1080 timestamp.appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
1081 (int) ((ts / (1000 * 1000)) % 1000));
1082 // Log histograms
Sanna Catherine de Treville Wager201079a2017-05-18 16:36:29 -07001083 setFindGlitch(true);
Nicolas Rouletad82aa62017-04-03 19:15:20 -07001084 body.appendFormat("Histogram flush - ");
1085 handleAuthor(histEntry, &body);
Nicolas Rouletad82aa62017-04-03 19:15:20 -07001086 for (auto hist = mHists.begin(); hist != mHists.end();) {
1087 if (hist->first.second == histEntry.author()) {
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -07001088 body.appendFormat("%X", (int)hist->first.first);
Sanna Catherine de Treville Wager201079a2017-05-18 16:36:29 -07001089 if (findGlitch) {
1090 alertIfGlitch(hist->second);
1091 }
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -07001092 // set file to empty and write data for all histograms in this set
1093 writeHistToFile(hist->second, hist != mHists.begin());
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -07001094 drawHistogram(&body, hist->second, true, indent);
Nicolas Rouletad82aa62017-04-03 19:15:20 -07001095 hist = mHists.erase(hist);
1096 } else {
1097 ++hist;
1098 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001099 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001100 ++entry;
1101 break;
Nicolas Rouletad82aa62017-04-03 19:15:20 -07001102 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001103 case EVENT_END_FMT:
1104 body.appendFormat("warning: got to end format event");
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001105 ++entry;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001106 break;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001107 case EVENT_RESERVED:
1108 default:
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001109 body.appendFormat("warning: unexpected event %d", entry->type);
1110 ++entry;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001111 break;
1112 }
Glenn Kasten4e01ef62013-07-11 14:29:59 -07001113
1114 if (!body.isEmpty()) {
1115 dumpLine(timestamp, body);
Glenn Kasten4e01ef62013-07-11 14:29:59 -07001116 }
1117 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001118}
1119
Nicolas Roulet40a44982017-02-03 13:39:57 -08001120void NBLog::Reader::dump(int fd, size_t indent)
1121{
1122 // get a snapshot, dump it
1123 std::unique_ptr<Snapshot> snap = getSnapshot();
1124 dump(fd, indent, *snap);
1125}
1126
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001127void NBLog::Reader::dumpLine(const String8 &timestamp, String8 &body)
Glenn Kasten4e01ef62013-07-11 14:29:59 -07001128{
1129 if (mFd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07001130 dprintf(mFd, "%.*s%s %s\n", mIndent, "", timestamp.string(), body.string());
Glenn Kasten4e01ef62013-07-11 14:29:59 -07001131 } else {
1132 ALOGI("%.*s%s %s", mIndent, "", timestamp.string(), body.string());
1133 }
1134 body.clear();
1135}
1136
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001137bool NBLog::Reader::isIMemory(const sp<IMemory>& iMemory) const
1138{
Glenn Kasten481fb672013-09-30 14:39:28 -07001139 return iMemory != 0 && mIMemory != 0 && iMemory->pointer() == mIMemory->pointer();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001140}
1141
Sanna Catherine de Treville Wager201079a2017-05-18 16:36:29 -07001142void NBLog::Reader::setFindGlitch(bool s)
1143{
1144 findGlitch = s;
1145}
1146
1147bool NBLog::Reader::isFindGlitch() const
1148{
1149 return findGlitch;
1150}
1151
Glenn Kasten1c446272017-04-07 09:49:07 -07001152// ---------------------------------------------------------------------------
1153
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001154void NBLog::appendTimestamp(String8 *body, const void *data) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001155 int64_t ts;
1156 memcpy(&ts, data, sizeof(ts));
1157 body->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
1158 (int) ((ts / (1000 * 1000)) % 1000));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001159}
1160
1161void NBLog::appendInt(String8 *body, const void *data) {
1162 int x = *((int*) data);
1163 body->appendFormat("<%d>", x);
1164}
1165
1166void NBLog::appendFloat(String8 *body, const void *data) {
1167 float f;
1168 memcpy(&f, data, sizeof(float));
1169 body->appendFormat("<%f>", f);
1170}
1171
Nicolas Rouletc20cb502017-02-01 12:35:24 -08001172void NBLog::appendPID(String8 *body, const void* data, size_t length) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001173 pid_t id = *((pid_t*) data);
Nicolas Rouletc20cb502017-02-01 12:35:24 -08001174 char * name = &((char*) data)[sizeof(pid_t)];
1175 body->appendFormat("<PID: %d, name: %.*s>", id, (int) (length - sizeof(pid_t)), name);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001176}
1177
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001178String8 NBLog::bufferDump(const uint8_t *buffer, size_t size)
Nicolas Roulet2aedf372017-03-29 11:27:03 -07001179{
1180 String8 str;
1181 str.append("[ ");
1182 for(size_t i = 0; i < size; i++)
1183 {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001184 str.appendFormat("%d ", buffer[i]);
Nicolas Roulet2aedf372017-03-29 11:27:03 -07001185 }
1186 str.append("]");
1187 return str;
1188}
1189
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001190String8 NBLog::bufferDump(const EntryIterator &it)
Nicolas Roulet2aedf372017-03-29 11:27:03 -07001191{
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001192 return bufferDump(it, it->length + Entry::kOverhead);
Nicolas Roulet2aedf372017-03-29 11:27:03 -07001193}
1194
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001195NBLog::EntryIterator NBLog::Reader::handleFormat(const FormatEntry &fmtEntry,
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001196 String8 *timestamp,
1197 String8 *body) {
Nicolas Roulet40a44982017-02-03 13:39:57 -08001198 // log timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001199 int64_t ts = fmtEntry.timestamp();
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001200 timestamp->clear();
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001201 timestamp->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
1202 (int) ((ts / (1000 * 1000)) % 1000));
Nicolas Roulet40a44982017-02-03 13:39:57 -08001203
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -07001204 // log unique hash
1205 log_hash_t hash = fmtEntry.hash();
1206 // print only lower 16bit of hash as hex and line as int to reduce spam in the log
1207 body->appendFormat("%.4X-%d ", (int)(hash >> 16) & 0xFFFF, (int) hash & 0xFFFF);
1208
Nicolas Roulet40a44982017-02-03 13:39:57 -08001209 // log author (if present)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001210 handleAuthor(fmtEntry, body);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001211
1212 // log string
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001213 NBLog::EntryIterator arg = fmtEntry.args();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001214
1215 const char* fmt = fmtEntry.formatString();
1216 size_t fmt_length = fmtEntry.formatStringLength();
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001217
1218 for (size_t fmt_offset = 0; fmt_offset < fmt_length; ++fmt_offset) {
1219 if (fmt[fmt_offset] != '%') {
1220 body->append(&fmt[fmt_offset], 1); // TODO optimize to write consecutive strings at once
1221 continue;
1222 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001223 // case "%%""
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001224 if (fmt[++fmt_offset] == '%') {
1225 body->append("%");
1226 continue;
1227 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001228 // case "%\0"
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001229 if (fmt_offset == fmt_length) {
1230 continue;
1231 }
1232
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001233 NBLog::Event event = (NBLog::Event) arg->type;
1234 size_t length = arg->length;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001235
1236 // TODO check length for event type is correct
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001237
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001238 if (event == EVENT_END_FMT) {
1239 break;
1240 }
1241
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001242 // TODO: implement more complex formatting such as %.3f
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001243 const uint8_t *datum = arg->data; // pointer to the current event args
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001244 switch(fmt[fmt_offset])
1245 {
1246 case 's': // string
Nicolas Roulet4da78202017-02-03 12:53:39 -08001247 ALOGW_IF(event != EVENT_STRING,
1248 "NBLog Reader incompatible event for string specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001249 body->append((const char*) datum, length);
1250 break;
1251
1252 case 't': // timestamp
Nicolas Roulet4da78202017-02-03 12:53:39 -08001253 ALOGW_IF(event != EVENT_TIMESTAMP,
1254 "NBLog Reader incompatible event for timestamp specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001255 appendTimestamp(body, datum);
1256 break;
1257
1258 case 'd': // integer
Nicolas Roulet4da78202017-02-03 12:53:39 -08001259 ALOGW_IF(event != EVENT_INTEGER,
1260 "NBLog Reader incompatible event for integer specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001261 appendInt(body, datum);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001262 break;
1263
1264 case 'f': // float
Nicolas Roulet4da78202017-02-03 12:53:39 -08001265 ALOGW_IF(event != EVENT_FLOAT,
1266 "NBLog Reader incompatible event for float specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001267 appendFloat(body, datum);
1268 break;
1269
1270 case 'p': // pid
Nicolas Roulet4da78202017-02-03 12:53:39 -08001271 ALOGW_IF(event != EVENT_PID,
1272 "NBLog Reader incompatible event for pid specifier: %d", event);
Nicolas Rouletc20cb502017-02-01 12:35:24 -08001273 appendPID(body, datum, length);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001274 break;
1275
1276 default:
1277 ALOGW("NBLog Reader encountered unknown character %c", fmt[fmt_offset]);
1278 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001279 ++arg;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001280 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001281 ALOGW_IF(arg->type != EVENT_END_FMT, "Expected end of format, got %d", arg->type);
1282 ++arg;
1283 return arg;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001284}
1285
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001286static int widthOf(int x) {
1287 int width = 0;
1288 while (x > 0) {
1289 ++width;
1290 x /= 10;
1291 }
1292 return width;
1293}
1294
Nicolas Rouletad82aa62017-04-03 19:15:20 -07001295static std::map<int, int> buildBuckets(const std::vector<int64_t> &samples) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001296 // TODO allow buckets of variable resolution
1297 std::map<int, int> buckets;
Nicolas Rouletad82aa62017-04-03 19:15:20 -07001298 for (size_t i = 1; i < samples.size(); ++i) {
1299 ++buckets[deltaMs(samples[i - 1], samples[i])];
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001300 }
1301 return buckets;
1302}
1303
Nicolas Roulet4f033492017-04-03 19:17:03 -07001304static inline uint32_t log2(uint32_t x) {
1305 // This works for x > 0
1306 return 31 - __builtin_clz(x);
1307}
1308
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001309// TODO put this function in separate file. Make it return a std::string instead of modifying body
Nicolas Roulet4f033492017-04-03 19:17:03 -07001310/*
1311Example output:
1312[54.234] Histogram flush - AudioOut_D:
1313Histogram 33640BF1
1314 [ 1][ 1][ 1][ 3][54][69][ 1][ 2][ 1]
1315 64| []
1316 32| [] []
1317 16| [] []
1318 8| [] []
1319 4| [] []
1320 2|______________[]__[]__[]______[]____
1321 4 5 6 8 9 10 11 13 15
1322Notice that all values that fall in the same row have the same height (65 and 127 are displayed
1323identically). That's why exact counts are added at the top.
1324*/
Nicolas Rouletad82aa62017-04-03 19:15:20 -07001325void NBLog::Reader::drawHistogram(String8 *body,
1326 const std::vector<int64_t> &samples,
Nicolas Roulet4f033492017-04-03 19:17:03 -07001327 bool logScale,
Nicolas Rouletad82aa62017-04-03 19:15:20 -07001328 int indent,
1329 int maxHeight) {
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -07001330 // this avoids some corner cases
Nicolas Roulet4f033492017-04-03 19:17:03 -07001331 if (samples.size() <= 1) {
1332 return;
1333 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -07001334 // temp code for debugging the outlier timestamp
1335 const int kMaxMs = 100;
1336 for (size_t i = 1; i < samples.size()-1; ++i) {
1337 const int currDelta = deltaMs(samples[i - 1], samples[i]);
1338 if (currDelta > kMaxMs) {
1339 body->appendFormat("\nlocation: %zu, size: %zu, pos from end: %zu, %d\t", i,
1340 samples.size(), samples.size() - i, currDelta);
1341 }
1342 }
1343 // FIXME: as can be seen when printing the values, the outlier timestamps typically occur
1344 // in the first histogram 35 to 38 indices from the end (most often 35).
1345 // TODO: build histogram buckets earlier and discard timestamps to save memory
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001346 std::map<int, int> buckets = buildBuckets(samples);
Nicolas Roulet4f033492017-04-03 19:17:03 -07001347 // TODO consider changing all ints to uint32_t or uint64_t
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -07001348
1349 // underscores and spaces length corresponds to maximum width of histogram
1350 static const int kLen = 40;
1351 std::string underscores(kLen, '-');
1352 std::string spaces(kLen, ' ');
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001353
1354 auto it = buckets.begin();
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -07001355 int maxDelta = it->first;
1356 int maxCount = it->second;
Nicolas Rouletad82aa62017-04-03 19:15:20 -07001357 // Compute maximum values
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001358 while (++it != buckets.end()) {
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -07001359 if (it->first > maxDelta) {
1360 maxDelta = it->first;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001361 }
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -07001362 if (it->second > maxCount) {
1363 maxCount = it->second;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001364 }
1365 }
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -07001366 int height = logScale ? log2(maxCount) + 1 : maxCount; // maxCount > 0, safe to call log2
1367 const int leftPadding = widthOf(logScale ? pow(2, height) : maxCount);
1368 const int colWidth = std::max(std::max(widthOf(maxDelta) + 1, 3), leftPadding + 2);
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001369 int scalingFactor = 1;
Nicolas Rouletad82aa62017-04-03 19:15:20 -07001370 // scale data if it exceeds maximum height
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001371 if (height > maxHeight) {
1372 scalingFactor = (height + maxHeight) / maxHeight;
1373 height /= scalingFactor;
1374 }
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -07001375 body->appendFormat("\n%*s", leftPadding + 11, "Occurrences");
1376 // write histogram label line with bucket values
Nicolas Rouletad82aa62017-04-03 19:15:20 -07001377 body->appendFormat("\n%*s", indent, " ");
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -07001378 body->appendFormat("%*s", leftPadding, " ");
1379 for (auto const &x : buckets) {
1380 body->appendFormat("%*d", colWidth, x.second);
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001381 }
Nicolas Rouletad82aa62017-04-03 19:15:20 -07001382 // write histogram ascii art
1383 body->appendFormat("\n%*s", indent, " ");
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -07001384 for (int row = height * scalingFactor; row >= 0; row -= scalingFactor) {
1385 const int value = logScale ? (1 << row) : row;
1386 body->appendFormat("%.*s", leftPadding, spaces.c_str());
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001387 for (auto const &x : buckets) {
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -07001388 body->appendFormat("%.*s%s", colWidth - 1, spaces.c_str(), x.second < value ? " " : "|");
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001389 }
Nicolas Rouletad82aa62017-04-03 19:15:20 -07001390 body->appendFormat("\n%*s", indent, " ");
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001391 }
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -07001392 // print x-axis
1393 const int columns = static_cast<int>(buckets.size());
1394 body->appendFormat("%*c", leftPadding, ' ');
1395 body->appendFormat("%.*s", (columns + 1) * colWidth, underscores.c_str());
1396 body->appendFormat("\n%*s", indent, " ");
1397
Nicolas Rouletad82aa62017-04-03 19:15:20 -07001398 // write footer with bucket labels
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -07001399 body->appendFormat("%*s", leftPadding, " ");
1400 for (auto const &x : buckets) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001401 body->appendFormat("%*d", colWidth, x.first);
1402 }
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -07001403 body->appendFormat("%.*s%s", colWidth, spaces.c_str(), "ms\n");
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001404}
1405
Nicolas Roulet40a44982017-02-03 13:39:57 -08001406NBLog::Merger::Merger(const void *shared, size_t size):
Nicolas Roulet40a44982017-02-03 13:39:57 -08001407 mShared((Shared *) shared),
1408 mFifo(mShared != NULL ?
1409 new audio_utils_fifo(size, sizeof(uint8_t),
1410 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
1411 mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL)
1412 {}
1413
1414void NBLog::Merger::addReader(const NBLog::NamedReader &reader) {
Glenn Kasten1c446272017-04-07 09:49:07 -07001415 // FIXME This is called by binder thread in MediaLogService::registerWriter
1416 // but the access to shared variable mNamedReaders is not yet protected by a lock.
Nicolas Roulet40a44982017-02-03 13:39:57 -08001417 mNamedReaders.push_back(reader);
1418}
1419
1420// items placed in priority queue during merge
1421// composed by a timestamp and the index of the snapshot where the timestamp came from
1422struct MergeItem
1423{
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001424 int64_t ts;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001425 int index;
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001426 MergeItem(int64_t ts, int index): ts(ts), index(index) {}
Nicolas Roulet40a44982017-02-03 13:39:57 -08001427};
1428
1429// operators needed for priority queue in merge
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001430// bool operator>(const int64_t &t1, const int64_t &t2) {
1431// return t1.tv_sec > t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec);
1432// }
Nicolas Roulet40a44982017-02-03 13:39:57 -08001433
1434bool operator>(const struct MergeItem &i1, const struct MergeItem &i2) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001435 return i1.ts > i2.ts || (i1.ts == i2.ts && i1.index > i2.index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001436}
1437
1438// Merge registered readers, sorted by timestamp
1439void NBLog::Merger::merge() {
Glenn Kasten1c446272017-04-07 09:49:07 -07001440 // FIXME This is called by merge thread
1441 // but the access to shared variable mNamedReaders is not yet protected by a lock.
Nicolas Roulet40a44982017-02-03 13:39:57 -08001442 int nLogs = mNamedReaders.size();
1443 std::vector<std::unique_ptr<NBLog::Reader::Snapshot>> snapshots(nLogs);
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001444 std::vector<NBLog::EntryIterator> offsets(nLogs);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001445 for (int i = 0; i < nLogs; ++i) {
1446 snapshots[i] = mNamedReaders[i].reader()->getSnapshot();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001447 offsets[i] = snapshots[i]->begin();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001448 }
1449 // initialize offsets
Nicolas Roulet40a44982017-02-03 13:39:57 -08001450 // TODO custom heap implementation could allow to update top, improving performance
1451 // for bursty buffers
1452 std::priority_queue<MergeItem, std::vector<MergeItem>, std::greater<MergeItem>> timestamps;
1453 for (int i = 0; i < nLogs; ++i)
1454 {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001455 if (offsets[i] != snapshots[i]->end()) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001456 int64_t ts = AbstractEntry::buildEntry(offsets[i])->timestamp();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001457 timestamps.emplace(ts, i);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001458 }
1459 }
1460
1461 while (!timestamps.empty()) {
1462 // find minimum timestamp
1463 int index = timestamps.top().index;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001464 // copy it to the log, increasing offset
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001465 offsets[index] = AbstractEntry::buildEntry(offsets[index])->copyWithAuthor(mFifoWriter,
1466 index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001467 // update data structures
Nicolas Roulet40a44982017-02-03 13:39:57 -08001468 timestamps.pop();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001469 if (offsets[index] != snapshots[index]->end()) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001470 int64_t ts = AbstractEntry::buildEntry(offsets[index])->timestamp();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001471 timestamps.emplace(ts, index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001472 }
1473 }
1474}
1475
Glenn Kasten1c446272017-04-07 09:49:07 -07001476const std::vector<NBLog::NamedReader>& NBLog::Merger::getNamedReaders() const {
1477 // FIXME This is returning a reference to a shared variable that needs a lock
1478 return mNamedReaders;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001479}
1480
Glenn Kasten1c446272017-04-07 09:49:07 -07001481// ---------------------------------------------------------------------------
1482
Nicolas Roulet40a44982017-02-03 13:39:57 -08001483NBLog::MergeReader::MergeReader(const void *shared, size_t size, Merger &merger)
1484 : Reader(shared, size), mNamedReaders(merger.getNamedReaders()) {}
1485
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001486void NBLog::MergeReader::handleAuthor(const NBLog::AbstractEntry &entry, String8 *body) {
1487 int author = entry.author();
Glenn Kasten1c446272017-04-07 09:49:07 -07001488 // FIXME Needs a lock
1489 const char* name = mNamedReaders[author].name();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001490 body->appendFormat("%s: ", name);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001491}
1492
Glenn Kasten1c446272017-04-07 09:49:07 -07001493// ---------------------------------------------------------------------------
1494
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001495NBLog::MergeThread::MergeThread(NBLog::Merger &merger)
1496 : mMerger(merger),
1497 mTimeoutUs(0) {}
1498
1499NBLog::MergeThread::~MergeThread() {
1500 // set exit flag, set timeout to 0 to force threadLoop to exit and wait for the thread to join
1501 requestExit();
1502 setTimeoutUs(0);
1503 join();
1504}
1505
1506bool NBLog::MergeThread::threadLoop() {
1507 bool doMerge;
1508 {
1509 AutoMutex _l(mMutex);
1510 // If mTimeoutUs is negative, wait on the condition variable until it's positive.
1511 // If it's positive, wait kThreadSleepPeriodUs and then merge
1512 nsecs_t waitTime = mTimeoutUs > 0 ? kThreadSleepPeriodUs * 1000 : LLONG_MAX;
1513 mCond.waitRelative(mMutex, waitTime);
1514 doMerge = mTimeoutUs > 0;
1515 mTimeoutUs -= kThreadSleepPeriodUs;
1516 }
1517 if (doMerge) {
1518 mMerger.merge();
1519 }
1520 return true;
1521}
1522
1523void NBLog::MergeThread::wakeup() {
1524 setTimeoutUs(kThreadWakeupPeriodUs);
1525}
1526
1527void NBLog::MergeThread::setTimeoutUs(int time) {
1528 AutoMutex _l(mMutex);
1529 mTimeoutUs = time;
1530 mCond.signal();
1531}
1532
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001533} // namespace android