blob: 08f0944e434f33e03ad663fb5c123b3b19817919 [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
Sanna Catherine de Treville Wager1bb68622017-06-14 14:18:31 -070021* 1) Writing buffer period timestamp to the circular buffer
22* onWork()
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070023* Called every period length (e.g., 4ms)
24* Calls LOG_HIST_TS
25* LOG_HIST_TS
Sanna Catherine de Treville Wagerd4a68f62017-06-23 16:09:41 -070026* Hashes file name and line number, and writes single timestamp to buffer
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -070027* calls NBLOG::Writer::logEventHistTS once
28* NBLOG::Writer::logEventHistTS
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070029* 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
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -070047* Similarly, LOG_AUDIO_STATE() is called by onStateChange whenever audio is
48* turned on or off, and writes this notification to the FIFO.
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070049*
Sanna Catherine de Treville Wagerd4a68f62017-06-23 16:09:41 -070050* 2) reading the data from shared memory
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070051* Thread::threadloop()
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070052* NBLog::MergeThread::threadLoop()
Sanna Catherine de Treville Wagerd0965172017-07-24 13:42:44 -070053* Waits on a mutex, called periodically
54* Calls NBLog::Merger::merge and MergeReader.getAndProcessSnapshot.
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070055* NBLog::Merger::merge
Sanna Catherine de Treville Wager1bb68622017-06-14 14:18:31 -070056* Merges snapshots sorted by timestamp
Sanna Catherine de Treville Wagerd0965172017-07-24 13:42:44 -070057* Calls Reader::getSnapshot on each individual thread buffer to in shared
58* memory and writes all their data to the single FIFO stored in mMerger.
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070059* NBLog::Reader::getSnapshot
60* copies snapshot of reader's fifo buffer into its own buffer
61* calls mFifoReader->obtain to find readable data
62* sets snapshot.begin() and .end() iterators to boundaries of valid entries
63* moves the fifo reader index to after the last entry read
Sanna Catherine de Treville Wager1bb68622017-06-14 14:18:31 -070064* in this case, the buffer is in shared memory. in (4), the buffer is private
Sanna Catherine de Treville Wagerd0965172017-07-24 13:42:44 -070065* NBLog::MergeThread::getAndProcessSnapshot
66* Iterates through the entries in the local FIFO. Processes the data in
67* specific ways depending on the entry type. If the data is a histogram
68* timestamp or an audio on/off signal, writes to a map of PerformanceAnalysis
69* class instances, where the wakeup() intervals are stored as histograms
70* and analyzed.
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070071*
Sanna Catherine de Treville Wagerd4a68f62017-06-23 16:09:41 -070072* 3) reading the data from private buffer
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070073* MediaLogService::dump
Sanna Catherine de Treville Wager9484bae2017-06-15 14:39:44 -070074* calls NBLog::Reader::dump(CONSOLE)
75* The private buffer contains all logs for all readers in shared memory
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070076* NBLog::Reader::dump(int)
77* calls getSnapshot on the current reader
78* calls dump(int, size_t, Snapshot)
79* NBLog::Reader::dump(int, size, snapshot)
80* iterates through snapshot's events and switches based on their type
81* (string, timestamp, etc...)
82* In the case of EVENT_HISTOGRAM_ENTRY_TS, adds a list of timestamp sequences
83* (histogram entry) to NBLog::mHists
Sanna Catherine de Treville Wagerd4a68f62017-06-23 16:09:41 -070084* TODO: add every HISTOGRAM_ENTRY_TS to two
Sanna Catherine de Treville Wager1bb68622017-06-14 14:18:31 -070085* circular buffers: one short-term and one long-term (can add even longer-term
86* structures in the future). When dump is called, print everything currently
87* in the buffer.
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070088* NBLog::drawHistogram
89* input: timestamp array
90* buckets this to a histogram and prints
91*
92*/
93
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080094#define LOG_TAG "NBLog"
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080095
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070096#include <algorithm>
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -080097#include <climits>
Sanna Catherine de Treville Wager201079a2017-05-18 16:36:29 -070098#include <deque>
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070099#include <fstream>
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -0700100#include <iostream>
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -0700101#include <math.h>
Sanna Catherine de Treville Wager201079a2017-05-18 16:36:29 -0700102#include <numeric>
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -0700103#include <vector>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800104#include <stdarg.h>
105#include <stdint.h>
106#include <stdio.h>
107#include <string.h>
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800108#include <sys/prctl.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800109#include <time.h>
110#include <new>
Glenn Kasten535e1612016-12-05 12:19:36 -0800111#include <audio_utils/roundup.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800112#include <media/nbaio/NBLog.h>
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700113#include <media/nbaio/PerformanceAnalysis.h>
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -0700114#include <media/nbaio/ReportPerformance.h>
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700115#include <utils/CallStack.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800116#include <utils/Log.h>
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700117#include <utils/String8.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800118
Nicolas Roulet40a44982017-02-03 13:39:57 -0800119#include <queue>
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700120#include <utility>
Nicolas Roulet40a44982017-02-03 13:39:57 -0800121
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800122namespace android {
123
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700124int NBLog::Entry::copyEntryDataAt(size_t offset) const
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800125{
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700126 // FIXME This is too slow
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800127 if (offset == 0)
128 return mEvent;
129 else if (offset == 1)
130 return mLength;
131 else if (offset < (size_t) (mLength + 2))
132 return ((char *) mData)[offset - 2];
133 else if (offset == (size_t) (mLength + 2))
134 return mLength;
135 else
136 return 0;
137}
138
139// ---------------------------------------------------------------------------
140
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700141/*static*/
142std::unique_ptr<NBLog::AbstractEntry> NBLog::AbstractEntry::buildEntry(const uint8_t *ptr) {
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -0700143 const uint8_t type = EntryIterator(ptr)->type;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700144 switch (type) {
145 case EVENT_START_FMT:
146 return std::make_unique<FormatEntry>(FormatEntry(ptr));
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700147 case EVENT_AUDIO_STATE:
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700148 case EVENT_HISTOGRAM_ENTRY_TS:
149 return std::make_unique<HistogramEntry>(HistogramEntry(ptr));
150 default:
151 ALOGW("Tried to create AbstractEntry of type %d", type);
152 return nullptr;
153 }
Nicolas Roulet40a44982017-02-03 13:39:57 -0800154}
155
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700156NBLog::AbstractEntry::AbstractEntry(const uint8_t *entry) : mEntry(entry) {
157}
158
159// ---------------------------------------------------------------------------
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800160
Sanna Catherine de Treville Wagerdd92d7e2017-05-15 14:56:53 -0700161NBLog::EntryIterator NBLog::FormatEntry::begin() const {
162 return EntryIterator(mEntry);
163}
164
Nicolas Roulet40a44982017-02-03 13:39:57 -0800165const char *NBLog::FormatEntry::formatString() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800166 return (const char*) mEntry + offsetof(entry, data);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800167}
168
169size_t NBLog::FormatEntry::formatStringLength() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800170 return mEntry[offsetof(entry, length)];
Nicolas Roulet40a44982017-02-03 13:39:57 -0800171}
172
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700173NBLog::EntryIterator NBLog::FormatEntry::args() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800174 auto it = begin();
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700175 // skip start fmt
176 ++it;
177 // skip timestamp
178 ++it;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700179 // skip hash
180 ++it;
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700181 // Skip author if present
182 if (it->type == EVENT_AUTHOR) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800183 ++it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800184 }
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700185 return it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800186}
187
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700188int64_t NBLog::FormatEntry::timestamp() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800189 auto it = begin();
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700190 // skip start fmt
191 ++it;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700192 return it.payload<int64_t>();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800193}
194
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700195NBLog::log_hash_t NBLog::FormatEntry::hash() const {
196 auto it = begin();
197 // skip start fmt
198 ++it;
199 // skip timestamp
200 ++it;
201 // unaligned 64-bit read not supported
202 log_hash_t hash;
203 memcpy(&hash, it->data, sizeof(hash));
204 return hash;
205}
206
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700207int NBLog::FormatEntry::author() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800208 auto it = begin();
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700209 // skip start fmt
210 ++it;
211 // skip timestamp
212 ++it;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700213 // skip hash
214 ++it;
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700215 // if there is an author entry, return it, return -1 otherwise
216 if (it->type == EVENT_AUTHOR) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800217 return it.payload<int>();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800218 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800219 return -1;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800220}
221
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700222NBLog::EntryIterator NBLog::FormatEntry::copyWithAuthor(
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800223 std::unique_ptr<audio_utils_fifo_writer> &dst, int author) const {
224 auto it = begin();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800225 // copy fmt start entry
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800226 it.copyTo(dst);
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700227 // copy timestamp
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700228 (++it).copyTo(dst); // copy hash
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700229 (++it).copyTo(dst);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800230 // insert author entry
231 size_t authorEntrySize = NBLog::Entry::kOverhead + sizeof(author);
232 uint8_t authorEntry[authorEntrySize];
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800233 authorEntry[offsetof(entry, type)] = EVENT_AUTHOR;
234 authorEntry[offsetof(entry, length)] =
235 authorEntry[authorEntrySize + NBLog::Entry::kPreviousLengthOffset] =
236 sizeof(author);
237 *(int*) (&authorEntry[offsetof(entry, data)]) = author;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800238 dst->write(authorEntry, authorEntrySize);
239 // copy rest of entries
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800240 while ((++it)->type != EVENT_END_FMT) {
241 it.copyTo(dst);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800242 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800243 it.copyTo(dst);
244 ++it;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800245 return it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800246}
247
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700248void NBLog::EntryIterator::copyTo(std::unique_ptr<audio_utils_fifo_writer> &dst) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800249 size_t length = ptr[offsetof(entry, length)] + NBLog::Entry::kOverhead;
250 dst->write(ptr, length);
251}
Nicolas Roulet40a44982017-02-03 13:39:57 -0800252
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700253void NBLog::EntryIterator::copyData(uint8_t *dst) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800254 memcpy((void*) dst, ptr + offsetof(entry, data), ptr[offsetof(entry, length)]);
255}
256
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700257NBLog::EntryIterator::EntryIterator()
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800258 : ptr(nullptr) {}
259
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700260NBLog::EntryIterator::EntryIterator(const uint8_t *entry)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800261 : ptr(entry) {}
262
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700263NBLog::EntryIterator::EntryIterator(const NBLog::EntryIterator &other)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800264 : ptr(other.ptr) {}
265
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700266const NBLog::entry& NBLog::EntryIterator::operator*() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800267 return *(entry*) ptr;
268}
269
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700270const NBLog::entry* NBLog::EntryIterator::operator->() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800271 return (entry*) ptr;
272}
273
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700274NBLog::EntryIterator& NBLog::EntryIterator::operator++() {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800275 ptr += ptr[offsetof(entry, length)] + NBLog::Entry::kOverhead;
276 return *this;
277}
278
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700279NBLog::EntryIterator& NBLog::EntryIterator::operator--() {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800280 ptr -= ptr[NBLog::Entry::kPreviousLengthOffset] + NBLog::Entry::kOverhead;
281 return *this;
282}
283
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700284NBLog::EntryIterator NBLog::EntryIterator::next() const {
285 EntryIterator aux(*this);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800286 return ++aux;
287}
288
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700289NBLog::EntryIterator NBLog::EntryIterator::prev() const {
290 EntryIterator aux(*this);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800291 return --aux;
292}
293
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700294int NBLog::EntryIterator::operator-(const NBLog::EntryIterator &other) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800295 return ptr - other.ptr;
296}
297
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700298bool NBLog::EntryIterator::operator!=(const EntryIterator &other) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800299 return ptr != other.ptr;
300}
301
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700302bool NBLog::EntryIterator::hasConsistentLength() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800303 return ptr[offsetof(entry, length)] == ptr[ptr[offsetof(entry, length)] +
304 NBLog::Entry::kOverhead + NBLog::Entry::kPreviousLengthOffset];
Nicolas Roulet40a44982017-02-03 13:39:57 -0800305}
306
307// ---------------------------------------------------------------------------
308
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700309int64_t NBLog::HistogramEntry::timestamp() const {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700310 return EntryIterator(mEntry).payload<HistTsEntry>().ts;
311}
312
313NBLog::log_hash_t NBLog::HistogramEntry::hash() const {
314 return EntryIterator(mEntry).payload<HistTsEntry>().hash;
315}
316
317int NBLog::HistogramEntry::author() const {
318 EntryIterator it(mEntry);
319 if (it->length == sizeof(HistTsEntryWithAuthor)) {
320 return it.payload<HistTsEntryWithAuthor>().author;
321 } else {
322 return -1;
323 }
324}
325
326NBLog::EntryIterator NBLog::HistogramEntry::copyWithAuthor(
327 std::unique_ptr<audio_utils_fifo_writer> &dst, int author) const {
328 // Current histogram entry has {type, length, struct HistTsEntry, length}.
329 // We now want {type, length, struct HistTsEntryWithAuthor, length}
330 uint8_t buffer[Entry::kOverhead + sizeof(HistTsEntryWithAuthor)];
331 // Copy content until the point we want to add the author
332 memcpy(buffer, mEntry, sizeof(entry) + sizeof(HistTsEntry));
333 // Copy the author
334 *(int*) (buffer + sizeof(entry) + sizeof(HistTsEntry)) = author;
335 // Update lengths
336 buffer[offsetof(entry, length)] = sizeof(HistTsEntryWithAuthor);
337 buffer[sizeof(buffer) + Entry::kPreviousLengthOffset] = sizeof(HistTsEntryWithAuthor);
338 // Write new buffer into FIFO
339 dst->write(buffer, sizeof(buffer));
340 return EntryIterator(mEntry).next();
341}
342
343// ---------------------------------------------------------------------------
344
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800345#if 0 // FIXME see note in NBLog.h
346NBLog::Timeline::Timeline(size_t size, void *shared)
347 : mSize(roundup(size)), mOwn(shared == NULL),
348 mShared((Shared *) (mOwn ? new char[sharedSize(size)] : shared))
349{
350 new (mShared) Shared;
351}
352
353NBLog::Timeline::~Timeline()
354{
355 mShared->~Shared();
356 if (mOwn) {
357 delete[] (char *) mShared;
358 }
359}
360#endif
361
362/*static*/
363size_t NBLog::Timeline::sharedSize(size_t size)
364{
Glenn Kastened99c2b2016-12-12 08:31:24 -0800365 // TODO fifo now supports non-power-of-2 buffer sizes, so could remove the roundup
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800366 return sizeof(Shared) + roundup(size);
367}
368
369// ---------------------------------------------------------------------------
370
371NBLog::Writer::Writer()
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800372 : mShared(NULL), mFifo(NULL), mFifoWriter(NULL), mEnabled(false), mPidTag(NULL), mPidTagSize(0)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800373{
374}
375
Glenn Kasten535e1612016-12-05 12:19:36 -0800376NBLog::Writer::Writer(void *shared, size_t size)
377 : mShared((Shared *) shared),
378 mFifo(mShared != NULL ?
379 new audio_utils_fifo(size, sizeof(uint8_t),
380 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
381 mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL),
382 mEnabled(mFifoWriter != NULL)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800383{
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800384 // caching pid and process name
385 pid_t id = ::getpid();
386 char procName[16];
387 int status = prctl(PR_GET_NAME, procName);
388 if (status) { // error getting process name
389 procName[0] = '\0';
390 }
391 size_t length = strlen(procName);
392 mPidTagSize = length + sizeof(pid_t);
393 mPidTag = new char[mPidTagSize];
394 memcpy(mPidTag, &id, sizeof(pid_t));
395 memcpy(mPidTag + sizeof(pid_t), procName, length);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800396}
397
Glenn Kasten535e1612016-12-05 12:19:36 -0800398NBLog::Writer::Writer(const sp<IMemory>& iMemory, size_t size)
399 : Writer(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800400{
Glenn Kasten535e1612016-12-05 12:19:36 -0800401 mIMemory = iMemory;
402}
403
404NBLog::Writer::~Writer()
405{
406 delete mFifoWriter;
407 delete mFifo;
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800408 delete[] mPidTag;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800409}
410
411void NBLog::Writer::log(const char *string)
412{
413 if (!mEnabled) {
414 return;
415 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800416 LOG_ALWAYS_FATAL_IF(string == NULL, "Attempted to log NULL string");
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800417 size_t length = strlen(string);
Glenn Kasten535e1612016-12-05 12:19:36 -0800418 if (length > Entry::kMaxLength) {
419 length = Entry::kMaxLength;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800420 }
421 log(EVENT_STRING, string, length);
422}
423
424void NBLog::Writer::logf(const char *fmt, ...)
425{
426 if (!mEnabled) {
427 return;
428 }
429 va_list ap;
430 va_start(ap, fmt);
431 Writer::logvf(fmt, ap); // the Writer:: is needed to avoid virtual dispatch for LockedWriter
432 va_end(ap);
433}
434
435void NBLog::Writer::logvf(const char *fmt, va_list ap)
436{
437 if (!mEnabled) {
438 return;
439 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800440 char buffer[Entry::kMaxLength + 1 /*NUL*/];
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800441 int length = vsnprintf(buffer, sizeof(buffer), fmt, ap);
442 if (length >= (int) sizeof(buffer)) {
443 length = sizeof(buffer) - 1;
444 // NUL termination is not required
445 // buffer[length] = '\0';
446 }
447 if (length >= 0) {
448 log(EVENT_STRING, buffer, length);
449 }
450}
451
452void NBLog::Writer::logTimestamp()
453{
454 if (!mEnabled) {
455 return;
456 }
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700457 int64_t ts = get_monotonic_ns();
458 if (ts > 0) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800459 log(EVENT_TIMESTAMP, &ts, sizeof(ts));
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700460 } else {
461 ALOGE("Failed to get timestamp");
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800462 }
463}
464
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700465void NBLog::Writer::logTimestamp(const int64_t ts)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800466{
467 if (!mEnabled) {
468 return;
469 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800470 log(EVENT_TIMESTAMP, &ts, sizeof(ts));
471}
472
473void NBLog::Writer::logInteger(const int x)
474{
475 if (!mEnabled) {
476 return;
477 }
478 log(EVENT_INTEGER, &x, sizeof(x));
479}
480
481void NBLog::Writer::logFloat(const float x)
482{
483 if (!mEnabled) {
484 return;
485 }
486 log(EVENT_FLOAT, &x, sizeof(x));
487}
488
489void NBLog::Writer::logPID()
490{
491 if (!mEnabled) {
492 return;
493 }
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800494 log(EVENT_PID, mPidTag, mPidTagSize);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800495}
496
497void NBLog::Writer::logStart(const char *fmt)
498{
499 if (!mEnabled) {
500 return;
501 }
502 size_t length = strlen(fmt);
503 if (length > Entry::kMaxLength) {
504 length = Entry::kMaxLength;
505 }
506 log(EVENT_START_FMT, fmt, length);
507}
508
509void NBLog::Writer::logEnd()
510{
511 if (!mEnabled) {
512 return;
513 }
514 Entry entry = Entry(EVENT_END_FMT, NULL, 0);
515 log(&entry, true);
516}
517
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700518void NBLog::Writer::logHash(log_hash_t hash)
519{
520 if (!mEnabled) {
521 return;
522 }
523 log(EVENT_HASH, &hash, sizeof(hash));
524}
525
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700526void NBLog::Writer::logEventHistTs(Event event, log_hash_t hash)
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700527{
528 if (!mEnabled) {
529 return;
530 }
531 HistTsEntry data;
532 data.hash = hash;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700533 data.ts = get_monotonic_ns();
534 if (data.ts > 0) {
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700535 log(event, &data, sizeof(data));
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700536 } else {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700537 ALOGE("Failed to get timestamp");
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700538 }
539}
540
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700541void NBLog::Writer::logFormat(const char *fmt, log_hash_t hash, ...)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800542{
543 if (!mEnabled) {
544 return;
545 }
546
547 va_list ap;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700548 va_start(ap, hash);
549 Writer::logVFormat(fmt, hash, ap);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800550 va_end(ap);
551}
552
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700553void NBLog::Writer::logVFormat(const char *fmt, log_hash_t hash, va_list argp)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800554{
555 if (!mEnabled) {
556 return;
557 }
558 Writer::logStart(fmt);
559 int i;
560 double f;
561 char* s;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700562 int64_t t;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800563 Writer::logTimestamp();
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700564 Writer::logHash(hash);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800565 for (const char *p = fmt; *p != '\0'; p++) {
566 // TODO: implement more complex formatting such as %.3f
567 if (*p != '%') {
568 continue;
569 }
570 switch(*++p) {
571 case 's': // string
572 s = va_arg(argp, char *);
573 Writer::log(s);
574 break;
575
576 case 't': // timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700577 t = va_arg(argp, int64_t);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800578 Writer::logTimestamp(t);
579 break;
580
581 case 'd': // integer
582 i = va_arg(argp, int);
583 Writer::logInteger(i);
584 break;
585
586 case 'f': // float
587 f = va_arg(argp, double); // float arguments are promoted to double in vararg lists
588 Writer::logFloat((float)f);
589 break;
590
591 case 'p': // pid
592 Writer::logPID();
593 break;
594
595 // the "%\0" case finishes parsing
596 case '\0':
597 --p;
598 break;
599
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800600 case '%':
601 break;
602
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800603 default:
604 ALOGW("NBLog Writer parsed invalid format specifier: %c", *p);
605 break;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800606 }
607 }
608 Writer::logEnd();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800609}
610
611void NBLog::Writer::log(Event event, const void *data, size_t length)
612{
613 if (!mEnabled) {
614 return;
615 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800616 if (data == NULL || length > Entry::kMaxLength) {
617 // TODO Perhaps it makes sense to display truncated data or at least a
618 // message that the data is too long? The current behavior can create
619 // a confusion for a programmer debugging their code.
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800620 return;
621 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700622 // Ignore if invalid event
623 if (event == EVENT_RESERVED || event >= EVENT_UPPER_BOUND) {
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800624 return;
625 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700626 Entry etr(event, data, length);
627 log(&etr, true /*trusted*/);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800628}
629
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700630void NBLog::Writer::log(const NBLog::Entry *etr, bool trusted)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800631{
632 if (!mEnabled) {
633 return;
634 }
635 if (!trusted) {
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700636 log(etr->mEvent, etr->mData, etr->mLength);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800637 return;
638 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700639 size_t need = etr->mLength + Entry::kOverhead; // mEvent, mLength, data[mLength], mLength
640 // need = number of bytes written to FIFO
Glenn Kasten535e1612016-12-05 12:19:36 -0800641
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800642 // FIXME optimize this using memcpy for the data part of the Entry.
643 // The Entry could have a method copyTo(ptr, offset, size) to optimize the copy.
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700644 // checks size of a single log Entry: type, length, data pointer and ending
Glenn Kasten535e1612016-12-05 12:19:36 -0800645 uint8_t temp[Entry::kMaxLength + Entry::kOverhead];
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700646 // write this data to temp array
Glenn Kasten535e1612016-12-05 12:19:36 -0800647 for (size_t i = 0; i < need; i++) {
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700648 temp[i] = etr->copyEntryDataAt(i);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800649 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700650 // write to circular buffer
Glenn Kasten535e1612016-12-05 12:19:36 -0800651 mFifoWriter->write(temp, need);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800652}
653
654bool NBLog::Writer::isEnabled() const
655{
656 return mEnabled;
657}
658
659bool NBLog::Writer::setEnabled(bool enabled)
660{
661 bool old = mEnabled;
662 mEnabled = enabled && mShared != NULL;
663 return old;
664}
665
666// ---------------------------------------------------------------------------
667
668NBLog::LockedWriter::LockedWriter()
669 : Writer()
670{
671}
672
Glenn Kasten535e1612016-12-05 12:19:36 -0800673NBLog::LockedWriter::LockedWriter(void *shared, size_t size)
674 : Writer(shared, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800675{
676}
677
678void NBLog::LockedWriter::log(const char *string)
679{
680 Mutex::Autolock _l(mLock);
681 Writer::log(string);
682}
683
684void NBLog::LockedWriter::logf(const char *fmt, ...)
685{
686 // FIXME should not take the lock until after formatting is done
687 Mutex::Autolock _l(mLock);
688 va_list ap;
689 va_start(ap, fmt);
690 Writer::logvf(fmt, ap);
691 va_end(ap);
692}
693
694void NBLog::LockedWriter::logvf(const char *fmt, va_list ap)
695{
696 // FIXME should not take the lock until after formatting is done
697 Mutex::Autolock _l(mLock);
698 Writer::logvf(fmt, ap);
699}
700
701void NBLog::LockedWriter::logTimestamp()
702{
703 // FIXME should not take the lock until after the clock_gettime() syscall
704 Mutex::Autolock _l(mLock);
705 Writer::logTimestamp();
706}
707
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700708void NBLog::LockedWriter::logTimestamp(const int64_t ts)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800709{
710 Mutex::Autolock _l(mLock);
711 Writer::logTimestamp(ts);
712}
713
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800714void NBLog::LockedWriter::logInteger(const int x)
715{
716 Mutex::Autolock _l(mLock);
717 Writer::logInteger(x);
718}
719
720void NBLog::LockedWriter::logFloat(const float x)
721{
722 Mutex::Autolock _l(mLock);
723 Writer::logFloat(x);
724}
725
726void NBLog::LockedWriter::logPID()
727{
728 Mutex::Autolock _l(mLock);
729 Writer::logPID();
730}
731
732void NBLog::LockedWriter::logStart(const char *fmt)
733{
734 Mutex::Autolock _l(mLock);
735 Writer::logStart(fmt);
736}
737
738
739void NBLog::LockedWriter::logEnd()
740{
741 Mutex::Autolock _l(mLock);
742 Writer::logEnd();
743}
744
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700745void NBLog::LockedWriter::logHash(log_hash_t hash)
746{
747 Mutex::Autolock _l(mLock);
748 Writer::logHash(hash);
749}
750
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800751bool NBLog::LockedWriter::isEnabled() const
752{
753 Mutex::Autolock _l(mLock);
754 return Writer::isEnabled();
755}
756
757bool NBLog::LockedWriter::setEnabled(bool enabled)
758{
759 Mutex::Autolock _l(mLock);
760 return Writer::setEnabled(enabled);
761}
762
763// ---------------------------------------------------------------------------
764
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700765const std::set<NBLog::Event> NBLog::Reader::startingTypes {NBLog::Event::EVENT_START_FMT,
766 NBLog::Event::EVENT_HISTOGRAM_ENTRY_TS};
767const std::set<NBLog::Event> NBLog::Reader::endingTypes {NBLog::Event::EVENT_END_FMT,
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700768 NBLog::Event::EVENT_HISTOGRAM_ENTRY_TS,
769 NBLog::Event::EVENT_AUDIO_STATE};
770
Glenn Kasten535e1612016-12-05 12:19:36 -0800771NBLog::Reader::Reader(const void *shared, size_t size)
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700772 : mFd(-1), mIndent(0), mLost(0),
773 mShared((/*const*/ Shared *) shared), /*mIMemory*/
Glenn Kasten535e1612016-12-05 12:19:36 -0800774 mFifo(mShared != NULL ?
775 new audio_utils_fifo(size, sizeof(uint8_t),
776 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700777 mFifoReader(mFifo != NULL ? new audio_utils_fifo_reader(*mFifo) : NULL)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800778{
779}
780
Glenn Kasten535e1612016-12-05 12:19:36 -0800781NBLog::Reader::Reader(const sp<IMemory>& iMemory, size_t size)
782 : Reader(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800783{
Glenn Kasten535e1612016-12-05 12:19:36 -0800784 mIMemory = iMemory;
785}
786
787NBLog::Reader::~Reader()
788{
789 delete mFifoReader;
790 delete mFifo;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800791}
792
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700793const uint8_t *NBLog::Reader::findLastEntryOfTypes(const uint8_t *front, const uint8_t *back,
794 const std::set<Event> &types) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800795 while (back + Entry::kPreviousLengthOffset >= front) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700796 const uint8_t *prev = back - back[Entry::kPreviousLengthOffset] - Entry::kOverhead;
797 if (prev < front || prev + prev[offsetof(entry, length)] +
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800798 Entry::kOverhead != back) {
799
800 // prev points to an out of limits or inconsistent entry
801 return nullptr;
802 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700803 if (types.find((const Event) prev[offsetof(entry, type)]) != types.end()) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800804 return prev;
805 }
806 back = prev;
807 }
808 return nullptr; // no entry found
809}
810
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700811// Copies content of a Reader FIFO into its Snapshot
812// The Snapshot has the same raw data, but represented as a sequence of entries
813// and an EntryIterator making it possible to process the data.
Nicolas Roulet40a44982017-02-03 13:39:57 -0800814std::unique_ptr<NBLog::Reader::Snapshot> NBLog::Reader::getSnapshot()
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800815{
Glenn Kasten535e1612016-12-05 12:19:36 -0800816 if (mFifoReader == NULL) {
Nicolas Roulet40a44982017-02-03 13:39:57 -0800817 return std::unique_ptr<NBLog::Reader::Snapshot>(new Snapshot());
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800818 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800819 // make a copy to avoid race condition with writer
Glenn Kasten535e1612016-12-05 12:19:36 -0800820 size_t capacity = mFifo->capacity();
821
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800822 // This emulates the behaviour of audio_utils_fifo_reader::read, but without incrementing the
823 // reader index. The index is incremented after handling corruption, to after the last complete
824 // entry of the buffer
825 size_t lost;
826 audio_utils_iovec iovec[2];
827 ssize_t availToRead = mFifoReader->obtain(iovec, capacity, NULL /*timeout*/, &lost);
828 if (availToRead <= 0) {
829 return std::unique_ptr<NBLog::Reader::Snapshot>(new Snapshot());
830 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800831
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800832 std::unique_ptr<Snapshot> snapshot(new Snapshot(availToRead));
833 memcpy(snapshot->mData, (const char *) mFifo->buffer() + iovec[0].mOffset, iovec[0].mLength);
834 if (iovec[1].mLength > 0) {
835 memcpy(snapshot->mData + (iovec[0].mLength),
836 (const char *) mFifo->buffer() + iovec[1].mOffset, iovec[1].mLength);
837 }
838
839 // Handle corrupted buffer
840 // Potentially, a buffer has corrupted data on both beginning (due to overflow) and end
841 // (due to incomplete format entry). But even if the end format entry is incomplete,
842 // it ends in a complete entry (which is not an END_FMT). So is safe to traverse backwards.
843 // TODO: handle client corruption (in the middle of a buffer)
844
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700845 const uint8_t *back = snapshot->mData + availToRead;
846 const uint8_t *front = snapshot->mData;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800847
848 // Find last END_FMT. <back> is sitting on an entry which might be the middle of a FormatEntry.
849 // We go backwards until we find an EVENT_END_FMT.
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700850 const uint8_t *lastEnd = findLastEntryOfTypes(front, back, endingTypes);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800851 if (lastEnd == nullptr) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700852 snapshot->mEnd = snapshot->mBegin = EntryIterator(front);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800853 } else {
854 // end of snapshot points to after last END_FMT entry
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700855 snapshot->mEnd = EntryIterator(lastEnd).next();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800856 // find first START_FMT
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700857 const uint8_t *firstStart = nullptr;
858 const uint8_t *firstStartTmp = snapshot->mEnd;
859 while ((firstStartTmp = findLastEntryOfTypes(front, firstStartTmp, startingTypes))
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800860 != nullptr) {
861 firstStart = firstStartTmp;
862 }
863 // firstStart is null if no START_FMT entry was found before lastEnd
864 if (firstStart == nullptr) {
865 snapshot->mBegin = snapshot->mEnd;
866 } else {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700867 snapshot->mBegin = EntryIterator(firstStart);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800868 }
869 }
870
871 // advance fifo reader index to after last entry read.
872 mFifoReader->release(snapshot->mEnd - front);
873
874 snapshot->mLost = lost;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800875 return snapshot;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800876
Nicolas Roulet40a44982017-02-03 13:39:57 -0800877}
878
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700879// Takes raw content of the local merger FIFO, processes log entries, and
880// writes the data to a map of class PerformanceAnalysis, based on their thread ID.
881void NBLog::MergeReader::getAndProcessSnapshot(NBLog::Reader::Snapshot &snapshot)
Nicolas Roulet40a44982017-02-03 13:39:57 -0800882{
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700883 String8 timestamp, body;
Sanna Catherine de Treville Wagerd0965172017-07-24 13:42:44 -0700884 // TODO: check: is this thread safe?
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700885 // TODO: add lost data information and notification to ReportPerformance
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700886 size_t lost = snapshot.lost() + (snapshot.begin() - EntryIterator(snapshot.data()));
Glenn Kastenc02c9612013-10-15 09:25:11 -0700887 if (lost > 0) {
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700888 // TODO: ultimately, this will be += and reset to 0. TODO: check that this is
889 // obsolete now that Merger::merge is called periodically. No data should be lost
890 mLost = lost;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800891 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700892
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800893 for (auto entry = snapshot.begin(); entry != snapshot.end();) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800894 switch (entry->type) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800895 case EVENT_START_FMT:
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800896 entry = handleFormat(FormatEntry(entry), &timestamp, &body);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800897 break;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700898 case EVENT_HISTOGRAM_ENTRY_TS: {
899 HistTsEntryWithAuthor *data = (HistTsEntryWithAuthor *) (entry->data);
900 // TODO This memcpies are here to avoid unaligned memory access crash.
901 // There's probably a more efficient way to do it
902 log_hash_t hash;
903 memcpy(&hash, &(data->hash), sizeof(hash));
Nicolas Rouletad82aa62017-04-03 19:15:20 -0700904 int64_t ts;
905 memcpy(&ts, &data->ts, sizeof(ts));
Sanna Catherine de Treville Wagerd0965172017-07-24 13:42:44 -0700906 mThreadPerformanceAnalysis[data->author][hash].logTsEntry(ts);
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700907 ++entry;
908 break;
909 }
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700910 case EVENT_AUDIO_STATE: {
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700911 HistTsEntryWithAuthor *data = (HistTsEntryWithAuthor *) (entry->data);
912 // TODO This memcpies are here to avoid unaligned memory access crash.
913 // There's probably a more efficient way to do it
Sanna Catherine de Treville Wagerd0965172017-07-24 13:42:44 -0700914 log_hash_t hash;
915 memcpy(&hash, &(data->hash), sizeof(hash));
916 mThreadPerformanceAnalysis[data->author][hash].handleStateChange();
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700917 ++entry;
918 break;
919 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800920 case EVENT_END_FMT:
921 body.appendFormat("warning: got to end format event");
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800922 ++entry;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800923 break;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800924 case EVENT_RESERVED:
925 default:
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800926 body.appendFormat("warning: unexpected event %d", entry->type);
927 ++entry;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800928 break;
929 }
Sanna Catherine de Treville Wager9484bae2017-06-15 14:39:44 -0700930 }
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700931 // FIXME: decide whether to print the warnings here or elsewhere
Sanna Catherine de Treville Wager9484bae2017-06-15 14:39:44 -0700932 if (!body.isEmpty()) {
933 dumpLine(timestamp, body);
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700934 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800935}
936
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700937void NBLog::MergeReader::getAndProcessSnapshot()
Nicolas Roulet40a44982017-02-03 13:39:57 -0800938{
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700939 // get a snapshot, process it
Nicolas Roulet40a44982017-02-03 13:39:57 -0800940 std::unique_ptr<Snapshot> snap = getSnapshot();
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700941 getAndProcessSnapshot(*snap);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800942}
943
Sanna Catherine de Treville Wagercf6c75a2017-07-21 17:05:25 -0700944void NBLog::MergeReader::dump(int fd, int indent) {
945 ReportPerformance::dump(fd, indent, mThreadPerformanceAnalysis);
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700946}
947
948// Writes a string to the console
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800949void NBLog::Reader::dumpLine(const String8 &timestamp, String8 &body)
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700950{
951 if (mFd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700952 dprintf(mFd, "%.*s%s %s\n", mIndent, "", timestamp.string(), body.string());
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700953 } else {
954 ALOGI("%.*s%s %s", mIndent, "", timestamp.string(), body.string());
955 }
956 body.clear();
957}
958
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800959bool NBLog::Reader::isIMemory(const sp<IMemory>& iMemory) const
960{
Glenn Kasten481fb672013-09-30 14:39:28 -0700961 return iMemory != 0 && mIMemory != 0 && iMemory->pointer() == mIMemory->pointer();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800962}
963
Glenn Kasten1c446272017-04-07 09:49:07 -0700964// ---------------------------------------------------------------------------
965
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800966void NBLog::appendTimestamp(String8 *body, const void *data) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700967 int64_t ts;
968 memcpy(&ts, data, sizeof(ts));
969 body->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
970 (int) ((ts / (1000 * 1000)) % 1000));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800971}
972
973void NBLog::appendInt(String8 *body, const void *data) {
974 int x = *((int*) data);
975 body->appendFormat("<%d>", x);
976}
977
978void NBLog::appendFloat(String8 *body, const void *data) {
979 float f;
980 memcpy(&f, data, sizeof(float));
981 body->appendFormat("<%f>", f);
982}
983
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800984void NBLog::appendPID(String8 *body, const void* data, size_t length) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800985 pid_t id = *((pid_t*) data);
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800986 char * name = &((char*) data)[sizeof(pid_t)];
987 body->appendFormat("<PID: %d, name: %.*s>", id, (int) (length - sizeof(pid_t)), name);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800988}
989
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700990String8 NBLog::bufferDump(const uint8_t *buffer, size_t size)
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700991{
992 String8 str;
993 str.append("[ ");
994 for(size_t i = 0; i < size; i++)
995 {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700996 str.appendFormat("%d ", buffer[i]);
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700997 }
998 str.append("]");
999 return str;
1000}
1001
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001002String8 NBLog::bufferDump(const EntryIterator &it)
Nicolas Roulet2aedf372017-03-29 11:27:03 -07001003{
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001004 return bufferDump(it, it->length + Entry::kOverhead);
Nicolas Roulet2aedf372017-03-29 11:27:03 -07001005}
1006
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001007NBLog::EntryIterator NBLog::Reader::handleFormat(const FormatEntry &fmtEntry,
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001008 String8 *timestamp,
1009 String8 *body) {
Nicolas Roulet40a44982017-02-03 13:39:57 -08001010 // log timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001011 int64_t ts = fmtEntry.timestamp();
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001012 timestamp->clear();
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001013 timestamp->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
1014 (int) ((ts / (1000 * 1000)) % 1000));
Nicolas Roulet40a44982017-02-03 13:39:57 -08001015
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -07001016 // log unique hash
1017 log_hash_t hash = fmtEntry.hash();
1018 // print only lower 16bit of hash as hex and line as int to reduce spam in the log
1019 body->appendFormat("%.4X-%d ", (int)(hash >> 16) & 0xFFFF, (int) hash & 0xFFFF);
1020
Nicolas Roulet40a44982017-02-03 13:39:57 -08001021 // log author (if present)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001022 handleAuthor(fmtEntry, body);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001023
1024 // log string
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001025 NBLog::EntryIterator arg = fmtEntry.args();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001026
1027 const char* fmt = fmtEntry.formatString();
1028 size_t fmt_length = fmtEntry.formatStringLength();
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001029
1030 for (size_t fmt_offset = 0; fmt_offset < fmt_length; ++fmt_offset) {
1031 if (fmt[fmt_offset] != '%') {
1032 body->append(&fmt[fmt_offset], 1); // TODO optimize to write consecutive strings at once
1033 continue;
1034 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001035 // case "%%""
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001036 if (fmt[++fmt_offset] == '%') {
1037 body->append("%");
1038 continue;
1039 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001040 // case "%\0"
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001041 if (fmt_offset == fmt_length) {
1042 continue;
1043 }
1044
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001045 NBLog::Event event = (NBLog::Event) arg->type;
1046 size_t length = arg->length;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001047
1048 // TODO check length for event type is correct
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001049
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001050 if (event == EVENT_END_FMT) {
1051 break;
1052 }
1053
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001054 // TODO: implement more complex formatting such as %.3f
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001055 const uint8_t *datum = arg->data; // pointer to the current event args
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001056 switch(fmt[fmt_offset])
1057 {
1058 case 's': // string
Nicolas Roulet4da78202017-02-03 12:53:39 -08001059 ALOGW_IF(event != EVENT_STRING,
1060 "NBLog Reader incompatible event for string specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001061 body->append((const char*) datum, length);
1062 break;
1063
1064 case 't': // timestamp
Nicolas Roulet4da78202017-02-03 12:53:39 -08001065 ALOGW_IF(event != EVENT_TIMESTAMP,
1066 "NBLog Reader incompatible event for timestamp specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001067 appendTimestamp(body, datum);
1068 break;
1069
1070 case 'd': // integer
Nicolas Roulet4da78202017-02-03 12:53:39 -08001071 ALOGW_IF(event != EVENT_INTEGER,
1072 "NBLog Reader incompatible event for integer specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001073 appendInt(body, datum);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001074 break;
1075
1076 case 'f': // float
Nicolas Roulet4da78202017-02-03 12:53:39 -08001077 ALOGW_IF(event != EVENT_FLOAT,
1078 "NBLog Reader incompatible event for float specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001079 appendFloat(body, datum);
1080 break;
1081
1082 case 'p': // pid
Nicolas Roulet4da78202017-02-03 12:53:39 -08001083 ALOGW_IF(event != EVENT_PID,
1084 "NBLog Reader incompatible event for pid specifier: %d", event);
Nicolas Rouletc20cb502017-02-01 12:35:24 -08001085 appendPID(body, datum, length);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001086 break;
1087
1088 default:
1089 ALOGW("NBLog Reader encountered unknown character %c", fmt[fmt_offset]);
1090 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001091 ++arg;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001092 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001093 ALOGW_IF(arg->type != EVENT_END_FMT, "Expected end of format, got %d", arg->type);
1094 ++arg;
1095 return arg;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001096}
1097
Nicolas Roulet40a44982017-02-03 13:39:57 -08001098NBLog::Merger::Merger(const void *shared, size_t size):
Nicolas Roulet40a44982017-02-03 13:39:57 -08001099 mShared((Shared *) shared),
1100 mFifo(mShared != NULL ?
1101 new audio_utils_fifo(size, sizeof(uint8_t),
1102 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
1103 mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL)
1104 {}
1105
1106void NBLog::Merger::addReader(const NBLog::NamedReader &reader) {
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001107
Glenn Kasten1c446272017-04-07 09:49:07 -07001108 // FIXME This is called by binder thread in MediaLogService::registerWriter
1109 // but the access to shared variable mNamedReaders is not yet protected by a lock.
Nicolas Roulet40a44982017-02-03 13:39:57 -08001110 mNamedReaders.push_back(reader);
1111}
1112
1113// items placed in priority queue during merge
1114// composed by a timestamp and the index of the snapshot where the timestamp came from
1115struct MergeItem
1116{
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001117 int64_t ts;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001118 int index;
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001119 MergeItem(int64_t ts, int index): ts(ts), index(index) {}
Nicolas Roulet40a44982017-02-03 13:39:57 -08001120};
1121
1122// operators needed for priority queue in merge
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001123// bool operator>(const int64_t &t1, const int64_t &t2) {
1124// return t1.tv_sec > t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec);
1125// }
Nicolas Roulet40a44982017-02-03 13:39:57 -08001126
1127bool operator>(const struct MergeItem &i1, const struct MergeItem &i2) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001128 return i1.ts > i2.ts || (i1.ts == i2.ts && i1.index > i2.index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001129}
1130
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001131// Merge registered readers, sorted by timestamp, and write data to a single FIFO in local memory
Nicolas Roulet40a44982017-02-03 13:39:57 -08001132void NBLog::Merger::merge() {
Glenn Kasten1c446272017-04-07 09:49:07 -07001133 // FIXME This is called by merge thread
1134 // but the access to shared variable mNamedReaders is not yet protected by a lock.
Nicolas Roulet40a44982017-02-03 13:39:57 -08001135 int nLogs = mNamedReaders.size();
1136 std::vector<std::unique_ptr<NBLog::Reader::Snapshot>> snapshots(nLogs);
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001137 std::vector<NBLog::EntryIterator> offsets(nLogs);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001138 for (int i = 0; i < nLogs; ++i) {
1139 snapshots[i] = mNamedReaders[i].reader()->getSnapshot();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001140 offsets[i] = snapshots[i]->begin();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001141 }
1142 // initialize offsets
Nicolas Roulet40a44982017-02-03 13:39:57 -08001143 // TODO custom heap implementation could allow to update top, improving performance
1144 // for bursty buffers
1145 std::priority_queue<MergeItem, std::vector<MergeItem>, std::greater<MergeItem>> timestamps;
1146 for (int i = 0; i < nLogs; ++i)
1147 {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001148 if (offsets[i] != snapshots[i]->end()) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001149 int64_t ts = AbstractEntry::buildEntry(offsets[i])->timestamp();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001150 timestamps.emplace(ts, i);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001151 }
1152 }
1153
1154 while (!timestamps.empty()) {
1155 // find minimum timestamp
1156 int index = timestamps.top().index;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001157 // copy it to the log, increasing offset
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001158 offsets[index] = AbstractEntry::buildEntry(offsets[index])->copyWithAuthor(mFifoWriter,
1159 index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001160 // update data structures
Nicolas Roulet40a44982017-02-03 13:39:57 -08001161 timestamps.pop();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001162 if (offsets[index] != snapshots[index]->end()) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001163 int64_t ts = AbstractEntry::buildEntry(offsets[index])->timestamp();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001164 timestamps.emplace(ts, index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001165 }
1166 }
1167}
1168
Glenn Kasten1c446272017-04-07 09:49:07 -07001169const std::vector<NBLog::NamedReader>& NBLog::Merger::getNamedReaders() const {
1170 // FIXME This is returning a reference to a shared variable that needs a lock
1171 return mNamedReaders;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001172}
1173
Glenn Kasten1c446272017-04-07 09:49:07 -07001174// ---------------------------------------------------------------------------
1175
Nicolas Roulet40a44982017-02-03 13:39:57 -08001176NBLog::MergeReader::MergeReader(const void *shared, size_t size, Merger &merger)
1177 : Reader(shared, size), mNamedReaders(merger.getNamedReaders()) {}
1178
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001179void NBLog::MergeReader::handleAuthor(const NBLog::AbstractEntry &entry, String8 *body) {
1180 int author = entry.author();
Glenn Kasten1c446272017-04-07 09:49:07 -07001181 // FIXME Needs a lock
1182 const char* name = mNamedReaders[author].name();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001183 body->appendFormat("%s: ", name);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001184}
1185
Glenn Kasten1c446272017-04-07 09:49:07 -07001186// ---------------------------------------------------------------------------
1187
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001188NBLog::MergeThread::MergeThread(NBLog::Merger &merger, NBLog::MergeReader &mergeReader)
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001189 : mMerger(merger),
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001190 mMergeReader(mergeReader),
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001191 mTimeoutUs(0) {}
1192
1193NBLog::MergeThread::~MergeThread() {
1194 // set exit flag, set timeout to 0 to force threadLoop to exit and wait for the thread to join
1195 requestExit();
1196 setTimeoutUs(0);
1197 join();
1198}
1199
1200bool NBLog::MergeThread::threadLoop() {
1201 bool doMerge;
1202 {
1203 AutoMutex _l(mMutex);
1204 // If mTimeoutUs is negative, wait on the condition variable until it's positive.
1205 // If it's positive, wait kThreadSleepPeriodUs and then merge
1206 nsecs_t waitTime = mTimeoutUs > 0 ? kThreadSleepPeriodUs * 1000 : LLONG_MAX;
1207 mCond.waitRelative(mMutex, waitTime);
1208 doMerge = mTimeoutUs > 0;
1209 mTimeoutUs -= kThreadSleepPeriodUs;
1210 }
1211 if (doMerge) {
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001212 // Merge data from all the readers
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001213 mMerger.merge();
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001214 // Process the data collected by mMerger and write it to PerformanceAnalysis
1215 // FIXME: decide whether to call getAndProcessSnapshot every time
1216 // or whether to have a separate thread that calls it with a lower frequency
1217 mMergeReader.getAndProcessSnapshot();
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001218 }
1219 return true;
1220}
1221
1222void NBLog::MergeThread::wakeup() {
1223 setTimeoutUs(kThreadWakeupPeriodUs);
1224}
1225
1226void NBLog::MergeThread::setTimeoutUs(int time) {
1227 AutoMutex _l(mMutex);
1228 mTimeoutUs = time;
1229 mCond.signal();
1230}
1231
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001232} // namespace android