blob: c73632c5636283cb30da75bdebc559dd0ff759ea [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 Wager85768942017-07-26 20:17:30 -070072* 3) Dumpsys media.log call to report the data
73* MediaLogService::dump in MediaLogService.cpp
74* calls NBLog::Reader::dump, which calls ReportPerformance::dump
75* ReportPerformance::dump
76* calls PerformanceAnalysis::ReportPerformance
77* and ReportPerformance::WriteToFile
78* PerformanceAnalysis::ReportPerformance
79* for each thread/source file location instance of PerformanceAnalysis data,
80* combines all histograms into a single one and prints it to the console
81* along with outlier data
82* ReportPerformance::WriteToFile
83* writes histogram, outlier, and peak information to file separately for each
84* instance of PerformanceAnalysis data.
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070085*/
86
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080087#define LOG_TAG "NBLog"
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080088
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070089#include <algorithm>
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -080090#include <climits>
Sanna Catherine de Treville Wager201079a2017-05-18 16:36:29 -070091#include <deque>
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070092#include <fstream>
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070093#include <iostream>
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -070094#include <math.h>
Sanna Catherine de Treville Wager201079a2017-05-18 16:36:29 -070095#include <numeric>
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070096#include <vector>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080097#include <stdarg.h>
98#include <stdint.h>
99#include <stdio.h>
100#include <string.h>
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800101#include <sys/prctl.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800102#include <time.h>
103#include <new>
Glenn Kasten535e1612016-12-05 12:19:36 -0800104#include <audio_utils/roundup.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800105#include <media/nbaio/NBLog.h>
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700106#include <media/nbaio/PerformanceAnalysis.h>
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -0700107#include <media/nbaio/ReportPerformance.h>
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700108#include <utils/CallStack.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800109#include <utils/Log.h>
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700110#include <utils/String8.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800111
Nicolas Roulet40a44982017-02-03 13:39:57 -0800112#include <queue>
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700113#include <utility>
Nicolas Roulet40a44982017-02-03 13:39:57 -0800114
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800115namespace android {
116
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700117int NBLog::Entry::copyEntryDataAt(size_t offset) const
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800118{
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700119 // FIXME This is too slow
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800120 if (offset == 0)
121 return mEvent;
122 else if (offset == 1)
123 return mLength;
124 else if (offset < (size_t) (mLength + 2))
125 return ((char *) mData)[offset - 2];
126 else if (offset == (size_t) (mLength + 2))
127 return mLength;
128 else
129 return 0;
130}
131
132// ---------------------------------------------------------------------------
133
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700134/*static*/
135std::unique_ptr<NBLog::AbstractEntry> NBLog::AbstractEntry::buildEntry(const uint8_t *ptr) {
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -0700136 const uint8_t type = EntryIterator(ptr)->type;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700137 switch (type) {
138 case EVENT_START_FMT:
139 return std::make_unique<FormatEntry>(FormatEntry(ptr));
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700140 case EVENT_AUDIO_STATE:
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700141 case EVENT_HISTOGRAM_ENTRY_TS:
142 return std::make_unique<HistogramEntry>(HistogramEntry(ptr));
143 default:
144 ALOGW("Tried to create AbstractEntry of type %d", type);
145 return nullptr;
146 }
Nicolas Roulet40a44982017-02-03 13:39:57 -0800147}
148
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700149NBLog::AbstractEntry::AbstractEntry(const uint8_t *entry) : mEntry(entry) {
150}
151
152// ---------------------------------------------------------------------------
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800153
Sanna Catherine de Treville Wagerdd92d7e2017-05-15 14:56:53 -0700154NBLog::EntryIterator NBLog::FormatEntry::begin() const {
155 return EntryIterator(mEntry);
156}
157
Nicolas Roulet40a44982017-02-03 13:39:57 -0800158const char *NBLog::FormatEntry::formatString() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800159 return (const char*) mEntry + offsetof(entry, data);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800160}
161
162size_t NBLog::FormatEntry::formatStringLength() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800163 return mEntry[offsetof(entry, length)];
Nicolas Roulet40a44982017-02-03 13:39:57 -0800164}
165
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700166NBLog::EntryIterator NBLog::FormatEntry::args() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800167 auto it = begin();
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700168 // skip start fmt
169 ++it;
170 // skip timestamp
171 ++it;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700172 // skip hash
173 ++it;
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700174 // Skip author if present
175 if (it->type == EVENT_AUTHOR) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800176 ++it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800177 }
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700178 return it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800179}
180
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700181int64_t NBLog::FormatEntry::timestamp() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800182 auto it = begin();
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700183 // skip start fmt
184 ++it;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700185 return it.payload<int64_t>();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800186}
187
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700188NBLog::log_hash_t NBLog::FormatEntry::hash() const {
189 auto it = begin();
190 // skip start fmt
191 ++it;
192 // skip timestamp
193 ++it;
194 // unaligned 64-bit read not supported
195 log_hash_t hash;
196 memcpy(&hash, it->data, sizeof(hash));
197 return hash;
198}
199
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700200int NBLog::FormatEntry::author() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800201 auto it = begin();
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700202 // skip start fmt
203 ++it;
204 // skip timestamp
205 ++it;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700206 // skip hash
207 ++it;
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700208 // if there is an author entry, return it, return -1 otherwise
209 if (it->type == EVENT_AUTHOR) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800210 return it.payload<int>();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800211 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800212 return -1;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800213}
214
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700215NBLog::EntryIterator NBLog::FormatEntry::copyWithAuthor(
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800216 std::unique_ptr<audio_utils_fifo_writer> &dst, int author) const {
217 auto it = begin();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800218 // copy fmt start entry
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800219 it.copyTo(dst);
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700220 // copy timestamp
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700221 (++it).copyTo(dst); // copy hash
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700222 (++it).copyTo(dst);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800223 // insert author entry
224 size_t authorEntrySize = NBLog::Entry::kOverhead + sizeof(author);
225 uint8_t authorEntry[authorEntrySize];
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800226 authorEntry[offsetof(entry, type)] = EVENT_AUTHOR;
227 authorEntry[offsetof(entry, length)] =
228 authorEntry[authorEntrySize + NBLog::Entry::kPreviousLengthOffset] =
229 sizeof(author);
230 *(int*) (&authorEntry[offsetof(entry, data)]) = author;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800231 dst->write(authorEntry, authorEntrySize);
232 // copy rest of entries
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800233 while ((++it)->type != EVENT_END_FMT) {
234 it.copyTo(dst);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800235 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800236 it.copyTo(dst);
237 ++it;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800238 return it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800239}
240
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700241void NBLog::EntryIterator::copyTo(std::unique_ptr<audio_utils_fifo_writer> &dst) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800242 size_t length = ptr[offsetof(entry, length)] + NBLog::Entry::kOverhead;
243 dst->write(ptr, length);
244}
Nicolas Roulet40a44982017-02-03 13:39:57 -0800245
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700246void NBLog::EntryIterator::copyData(uint8_t *dst) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800247 memcpy((void*) dst, ptr + offsetof(entry, data), ptr[offsetof(entry, length)]);
248}
249
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700250NBLog::EntryIterator::EntryIterator()
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800251 : ptr(nullptr) {}
252
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700253NBLog::EntryIterator::EntryIterator(const uint8_t *entry)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800254 : ptr(entry) {}
255
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700256NBLog::EntryIterator::EntryIterator(const NBLog::EntryIterator &other)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800257 : ptr(other.ptr) {}
258
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700259const NBLog::entry& NBLog::EntryIterator::operator*() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800260 return *(entry*) ptr;
261}
262
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700263const NBLog::entry* NBLog::EntryIterator::operator->() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800264 return (entry*) ptr;
265}
266
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700267NBLog::EntryIterator& NBLog::EntryIterator::operator++() {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800268 ptr += ptr[offsetof(entry, length)] + NBLog::Entry::kOverhead;
269 return *this;
270}
271
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700272NBLog::EntryIterator& NBLog::EntryIterator::operator--() {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800273 ptr -= ptr[NBLog::Entry::kPreviousLengthOffset] + NBLog::Entry::kOverhead;
274 return *this;
275}
276
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700277NBLog::EntryIterator NBLog::EntryIterator::next() const {
278 EntryIterator aux(*this);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800279 return ++aux;
280}
281
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700282NBLog::EntryIterator NBLog::EntryIterator::prev() const {
283 EntryIterator aux(*this);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800284 return --aux;
285}
286
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700287int NBLog::EntryIterator::operator-(const NBLog::EntryIterator &other) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800288 return ptr - other.ptr;
289}
290
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700291bool NBLog::EntryIterator::operator!=(const EntryIterator &other) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800292 return ptr != other.ptr;
293}
294
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700295bool NBLog::EntryIterator::hasConsistentLength() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800296 return ptr[offsetof(entry, length)] == ptr[ptr[offsetof(entry, length)] +
297 NBLog::Entry::kOverhead + NBLog::Entry::kPreviousLengthOffset];
Nicolas Roulet40a44982017-02-03 13:39:57 -0800298}
299
300// ---------------------------------------------------------------------------
301
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700302int64_t NBLog::HistogramEntry::timestamp() const {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700303 return EntryIterator(mEntry).payload<HistTsEntry>().ts;
304}
305
306NBLog::log_hash_t NBLog::HistogramEntry::hash() const {
307 return EntryIterator(mEntry).payload<HistTsEntry>().hash;
308}
309
310int NBLog::HistogramEntry::author() const {
311 EntryIterator it(mEntry);
312 if (it->length == sizeof(HistTsEntryWithAuthor)) {
313 return it.payload<HistTsEntryWithAuthor>().author;
314 } else {
315 return -1;
316 }
317}
318
319NBLog::EntryIterator NBLog::HistogramEntry::copyWithAuthor(
320 std::unique_ptr<audio_utils_fifo_writer> &dst, int author) const {
321 // Current histogram entry has {type, length, struct HistTsEntry, length}.
322 // We now want {type, length, struct HistTsEntryWithAuthor, length}
323 uint8_t buffer[Entry::kOverhead + sizeof(HistTsEntryWithAuthor)];
324 // Copy content until the point we want to add the author
325 memcpy(buffer, mEntry, sizeof(entry) + sizeof(HistTsEntry));
326 // Copy the author
327 *(int*) (buffer + sizeof(entry) + sizeof(HistTsEntry)) = author;
328 // Update lengths
329 buffer[offsetof(entry, length)] = sizeof(HistTsEntryWithAuthor);
330 buffer[sizeof(buffer) + Entry::kPreviousLengthOffset] = sizeof(HistTsEntryWithAuthor);
331 // Write new buffer into FIFO
332 dst->write(buffer, sizeof(buffer));
333 return EntryIterator(mEntry).next();
334}
335
336// ---------------------------------------------------------------------------
337
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800338#if 0 // FIXME see note in NBLog.h
339NBLog::Timeline::Timeline(size_t size, void *shared)
340 : mSize(roundup(size)), mOwn(shared == NULL),
341 mShared((Shared *) (mOwn ? new char[sharedSize(size)] : shared))
342{
343 new (mShared) Shared;
344}
345
346NBLog::Timeline::~Timeline()
347{
348 mShared->~Shared();
349 if (mOwn) {
350 delete[] (char *) mShared;
351 }
352}
353#endif
354
355/*static*/
356size_t NBLog::Timeline::sharedSize(size_t size)
357{
Glenn Kastened99c2b2016-12-12 08:31:24 -0800358 // TODO fifo now supports non-power-of-2 buffer sizes, so could remove the roundup
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800359 return sizeof(Shared) + roundup(size);
360}
361
362// ---------------------------------------------------------------------------
363
364NBLog::Writer::Writer()
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800365 : mShared(NULL), mFifo(NULL), mFifoWriter(NULL), mEnabled(false), mPidTag(NULL), mPidTagSize(0)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800366{
367}
368
Glenn Kasten535e1612016-12-05 12:19:36 -0800369NBLog::Writer::Writer(void *shared, size_t size)
370 : mShared((Shared *) shared),
371 mFifo(mShared != NULL ?
372 new audio_utils_fifo(size, sizeof(uint8_t),
373 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
374 mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL),
375 mEnabled(mFifoWriter != NULL)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800376{
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800377 // caching pid and process name
378 pid_t id = ::getpid();
379 char procName[16];
380 int status = prctl(PR_GET_NAME, procName);
381 if (status) { // error getting process name
382 procName[0] = '\0';
383 }
384 size_t length = strlen(procName);
385 mPidTagSize = length + sizeof(pid_t);
386 mPidTag = new char[mPidTagSize];
387 memcpy(mPidTag, &id, sizeof(pid_t));
388 memcpy(mPidTag + sizeof(pid_t), procName, length);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800389}
390
Glenn Kasten535e1612016-12-05 12:19:36 -0800391NBLog::Writer::Writer(const sp<IMemory>& iMemory, size_t size)
392 : Writer(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800393{
Glenn Kasten535e1612016-12-05 12:19:36 -0800394 mIMemory = iMemory;
395}
396
397NBLog::Writer::~Writer()
398{
399 delete mFifoWriter;
400 delete mFifo;
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800401 delete[] mPidTag;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800402}
403
404void NBLog::Writer::log(const char *string)
405{
406 if (!mEnabled) {
407 return;
408 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800409 LOG_ALWAYS_FATAL_IF(string == NULL, "Attempted to log NULL string");
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800410 size_t length = strlen(string);
Glenn Kasten535e1612016-12-05 12:19:36 -0800411 if (length > Entry::kMaxLength) {
412 length = Entry::kMaxLength;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800413 }
414 log(EVENT_STRING, string, length);
415}
416
417void NBLog::Writer::logf(const char *fmt, ...)
418{
419 if (!mEnabled) {
420 return;
421 }
422 va_list ap;
423 va_start(ap, fmt);
424 Writer::logvf(fmt, ap); // the Writer:: is needed to avoid virtual dispatch for LockedWriter
425 va_end(ap);
426}
427
428void NBLog::Writer::logvf(const char *fmt, va_list ap)
429{
430 if (!mEnabled) {
431 return;
432 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800433 char buffer[Entry::kMaxLength + 1 /*NUL*/];
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800434 int length = vsnprintf(buffer, sizeof(buffer), fmt, ap);
435 if (length >= (int) sizeof(buffer)) {
436 length = sizeof(buffer) - 1;
437 // NUL termination is not required
438 // buffer[length] = '\0';
439 }
440 if (length >= 0) {
441 log(EVENT_STRING, buffer, length);
442 }
443}
444
445void NBLog::Writer::logTimestamp()
446{
447 if (!mEnabled) {
448 return;
449 }
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700450 int64_t ts = get_monotonic_ns();
451 if (ts > 0) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800452 log(EVENT_TIMESTAMP, &ts, sizeof(ts));
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700453 } else {
454 ALOGE("Failed to get timestamp");
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800455 }
456}
457
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700458void NBLog::Writer::logTimestamp(const int64_t ts)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800459{
460 if (!mEnabled) {
461 return;
462 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800463 log(EVENT_TIMESTAMP, &ts, sizeof(ts));
464}
465
466void NBLog::Writer::logInteger(const int x)
467{
468 if (!mEnabled) {
469 return;
470 }
471 log(EVENT_INTEGER, &x, sizeof(x));
472}
473
474void NBLog::Writer::logFloat(const float x)
475{
476 if (!mEnabled) {
477 return;
478 }
479 log(EVENT_FLOAT, &x, sizeof(x));
480}
481
482void NBLog::Writer::logPID()
483{
484 if (!mEnabled) {
485 return;
486 }
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800487 log(EVENT_PID, mPidTag, mPidTagSize);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800488}
489
490void NBLog::Writer::logStart(const char *fmt)
491{
492 if (!mEnabled) {
493 return;
494 }
495 size_t length = strlen(fmt);
496 if (length > Entry::kMaxLength) {
497 length = Entry::kMaxLength;
498 }
499 log(EVENT_START_FMT, fmt, length);
500}
501
502void NBLog::Writer::logEnd()
503{
504 if (!mEnabled) {
505 return;
506 }
507 Entry entry = Entry(EVENT_END_FMT, NULL, 0);
508 log(&entry, true);
509}
510
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700511void NBLog::Writer::logHash(log_hash_t hash)
512{
513 if (!mEnabled) {
514 return;
515 }
516 log(EVENT_HASH, &hash, sizeof(hash));
517}
518
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700519void NBLog::Writer::logEventHistTs(Event event, log_hash_t hash)
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700520{
521 if (!mEnabled) {
522 return;
523 }
524 HistTsEntry data;
525 data.hash = hash;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700526 data.ts = get_monotonic_ns();
527 if (data.ts > 0) {
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700528 log(event, &data, sizeof(data));
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700529 } else {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700530 ALOGE("Failed to get timestamp");
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700531 }
532}
533
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700534void NBLog::Writer::logFormat(const char *fmt, log_hash_t hash, ...)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800535{
536 if (!mEnabled) {
537 return;
538 }
539
540 va_list ap;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700541 va_start(ap, hash);
542 Writer::logVFormat(fmt, hash, ap);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800543 va_end(ap);
544}
545
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700546void NBLog::Writer::logVFormat(const char *fmt, log_hash_t hash, va_list argp)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800547{
548 if (!mEnabled) {
549 return;
550 }
551 Writer::logStart(fmt);
552 int i;
553 double f;
554 char* s;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700555 int64_t t;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800556 Writer::logTimestamp();
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700557 Writer::logHash(hash);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800558 for (const char *p = fmt; *p != '\0'; p++) {
559 // TODO: implement more complex formatting such as %.3f
560 if (*p != '%') {
561 continue;
562 }
563 switch(*++p) {
564 case 's': // string
565 s = va_arg(argp, char *);
566 Writer::log(s);
567 break;
568
569 case 't': // timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700570 t = va_arg(argp, int64_t);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800571 Writer::logTimestamp(t);
572 break;
573
574 case 'd': // integer
575 i = va_arg(argp, int);
576 Writer::logInteger(i);
577 break;
578
579 case 'f': // float
580 f = va_arg(argp, double); // float arguments are promoted to double in vararg lists
581 Writer::logFloat((float)f);
582 break;
583
584 case 'p': // pid
585 Writer::logPID();
586 break;
587
588 // the "%\0" case finishes parsing
589 case '\0':
590 --p;
591 break;
592
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800593 case '%':
594 break;
595
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800596 default:
597 ALOGW("NBLog Writer parsed invalid format specifier: %c", *p);
598 break;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800599 }
600 }
601 Writer::logEnd();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800602}
603
604void NBLog::Writer::log(Event event, const void *data, size_t length)
605{
606 if (!mEnabled) {
607 return;
608 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800609 if (data == NULL || length > Entry::kMaxLength) {
610 // TODO Perhaps it makes sense to display truncated data or at least a
611 // message that the data is too long? The current behavior can create
612 // a confusion for a programmer debugging their code.
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800613 return;
614 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700615 // Ignore if invalid event
616 if (event == EVENT_RESERVED || event >= EVENT_UPPER_BOUND) {
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800617 return;
618 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700619 Entry etr(event, data, length);
620 log(&etr, true /*trusted*/);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800621}
622
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700623void NBLog::Writer::log(const NBLog::Entry *etr, bool trusted)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800624{
625 if (!mEnabled) {
626 return;
627 }
628 if (!trusted) {
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700629 log(etr->mEvent, etr->mData, etr->mLength);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800630 return;
631 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700632 size_t need = etr->mLength + Entry::kOverhead; // mEvent, mLength, data[mLength], mLength
633 // need = number of bytes written to FIFO
Glenn Kasten535e1612016-12-05 12:19:36 -0800634
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800635 // FIXME optimize this using memcpy for the data part of the Entry.
636 // The Entry could have a method copyTo(ptr, offset, size) to optimize the copy.
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700637 // checks size of a single log Entry: type, length, data pointer and ending
Glenn Kasten535e1612016-12-05 12:19:36 -0800638 uint8_t temp[Entry::kMaxLength + Entry::kOverhead];
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700639 // write this data to temp array
Glenn Kasten535e1612016-12-05 12:19:36 -0800640 for (size_t i = 0; i < need; i++) {
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700641 temp[i] = etr->copyEntryDataAt(i);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800642 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700643 // write to circular buffer
Glenn Kasten535e1612016-12-05 12:19:36 -0800644 mFifoWriter->write(temp, need);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800645}
646
647bool NBLog::Writer::isEnabled() const
648{
649 return mEnabled;
650}
651
652bool NBLog::Writer::setEnabled(bool enabled)
653{
654 bool old = mEnabled;
655 mEnabled = enabled && mShared != NULL;
656 return old;
657}
658
659// ---------------------------------------------------------------------------
660
661NBLog::LockedWriter::LockedWriter()
662 : Writer()
663{
664}
665
Glenn Kasten535e1612016-12-05 12:19:36 -0800666NBLog::LockedWriter::LockedWriter(void *shared, size_t size)
667 : Writer(shared, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800668{
669}
670
671void NBLog::LockedWriter::log(const char *string)
672{
673 Mutex::Autolock _l(mLock);
674 Writer::log(string);
675}
676
677void NBLog::LockedWriter::logf(const char *fmt, ...)
678{
679 // FIXME should not take the lock until after formatting is done
680 Mutex::Autolock _l(mLock);
681 va_list ap;
682 va_start(ap, fmt);
683 Writer::logvf(fmt, ap);
684 va_end(ap);
685}
686
687void NBLog::LockedWriter::logvf(const char *fmt, va_list ap)
688{
689 // FIXME should not take the lock until after formatting is done
690 Mutex::Autolock _l(mLock);
691 Writer::logvf(fmt, ap);
692}
693
694void NBLog::LockedWriter::logTimestamp()
695{
696 // FIXME should not take the lock until after the clock_gettime() syscall
697 Mutex::Autolock _l(mLock);
698 Writer::logTimestamp();
699}
700
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700701void NBLog::LockedWriter::logTimestamp(const int64_t ts)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800702{
703 Mutex::Autolock _l(mLock);
704 Writer::logTimestamp(ts);
705}
706
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800707void NBLog::LockedWriter::logInteger(const int x)
708{
709 Mutex::Autolock _l(mLock);
710 Writer::logInteger(x);
711}
712
713void NBLog::LockedWriter::logFloat(const float x)
714{
715 Mutex::Autolock _l(mLock);
716 Writer::logFloat(x);
717}
718
719void NBLog::LockedWriter::logPID()
720{
721 Mutex::Autolock _l(mLock);
722 Writer::logPID();
723}
724
725void NBLog::LockedWriter::logStart(const char *fmt)
726{
727 Mutex::Autolock _l(mLock);
728 Writer::logStart(fmt);
729}
730
731
732void NBLog::LockedWriter::logEnd()
733{
734 Mutex::Autolock _l(mLock);
735 Writer::logEnd();
736}
737
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700738void NBLog::LockedWriter::logHash(log_hash_t hash)
739{
740 Mutex::Autolock _l(mLock);
741 Writer::logHash(hash);
742}
743
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800744bool NBLog::LockedWriter::isEnabled() const
745{
746 Mutex::Autolock _l(mLock);
747 return Writer::isEnabled();
748}
749
750bool NBLog::LockedWriter::setEnabled(bool enabled)
751{
752 Mutex::Autolock _l(mLock);
753 return Writer::setEnabled(enabled);
754}
755
756// ---------------------------------------------------------------------------
757
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700758const std::set<NBLog::Event> NBLog::Reader::startingTypes {NBLog::Event::EVENT_START_FMT,
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700759 NBLog::Event::EVENT_HISTOGRAM_ENTRY_TS,
760 NBLog::Event::EVENT_AUDIO_STATE};
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700761const std::set<NBLog::Event> NBLog::Reader::endingTypes {NBLog::Event::EVENT_END_FMT,
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700762 NBLog::Event::EVENT_HISTOGRAM_ENTRY_TS,
763 NBLog::Event::EVENT_AUDIO_STATE};
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700764
Glenn Kasten535e1612016-12-05 12:19:36 -0800765NBLog::Reader::Reader(const void *shared, size_t size)
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700766 : mFd(-1), mIndent(0), mLost(0),
767 mShared((/*const*/ Shared *) shared), /*mIMemory*/
Glenn Kasten535e1612016-12-05 12:19:36 -0800768 mFifo(mShared != NULL ?
769 new audio_utils_fifo(size, sizeof(uint8_t),
770 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700771 mFifoReader(mFifo != NULL ? new audio_utils_fifo_reader(*mFifo) : NULL)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800772{
773}
774
Glenn Kasten535e1612016-12-05 12:19:36 -0800775NBLog::Reader::Reader(const sp<IMemory>& iMemory, size_t size)
776 : Reader(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800777{
Glenn Kasten535e1612016-12-05 12:19:36 -0800778 mIMemory = iMemory;
779}
780
781NBLog::Reader::~Reader()
782{
783 delete mFifoReader;
784 delete mFifo;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800785}
786
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700787const uint8_t *NBLog::Reader::findLastEntryOfTypes(const uint8_t *front, const uint8_t *back,
788 const std::set<Event> &types) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800789 while (back + Entry::kPreviousLengthOffset >= front) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700790 const uint8_t *prev = back - back[Entry::kPreviousLengthOffset] - Entry::kOverhead;
791 if (prev < front || prev + prev[offsetof(entry, length)] +
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800792 Entry::kOverhead != back) {
793
794 // prev points to an out of limits or inconsistent entry
795 return nullptr;
796 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700797 if (types.find((const Event) prev[offsetof(entry, type)]) != types.end()) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800798 return prev;
799 }
800 back = prev;
801 }
802 return nullptr; // no entry found
803}
804
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700805// Copies content of a Reader FIFO into its Snapshot
806// The Snapshot has the same raw data, but represented as a sequence of entries
807// and an EntryIterator making it possible to process the data.
Nicolas Roulet40a44982017-02-03 13:39:57 -0800808std::unique_ptr<NBLog::Reader::Snapshot> NBLog::Reader::getSnapshot()
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800809{
Glenn Kasten535e1612016-12-05 12:19:36 -0800810 if (mFifoReader == NULL) {
Nicolas Roulet40a44982017-02-03 13:39:57 -0800811 return std::unique_ptr<NBLog::Reader::Snapshot>(new Snapshot());
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800812 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800813 // make a copy to avoid race condition with writer
Glenn Kasten535e1612016-12-05 12:19:36 -0800814 size_t capacity = mFifo->capacity();
815
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800816 // This emulates the behaviour of audio_utils_fifo_reader::read, but without incrementing the
817 // reader index. The index is incremented after handling corruption, to after the last complete
818 // entry of the buffer
819 size_t lost;
820 audio_utils_iovec iovec[2];
821 ssize_t availToRead = mFifoReader->obtain(iovec, capacity, NULL /*timeout*/, &lost);
822 if (availToRead <= 0) {
823 return std::unique_ptr<NBLog::Reader::Snapshot>(new Snapshot());
824 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800825
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800826 std::unique_ptr<Snapshot> snapshot(new Snapshot(availToRead));
827 memcpy(snapshot->mData, (const char *) mFifo->buffer() + iovec[0].mOffset, iovec[0].mLength);
828 if (iovec[1].mLength > 0) {
829 memcpy(snapshot->mData + (iovec[0].mLength),
830 (const char *) mFifo->buffer() + iovec[1].mOffset, iovec[1].mLength);
831 }
832
833 // Handle corrupted buffer
834 // Potentially, a buffer has corrupted data on both beginning (due to overflow) and end
835 // (due to incomplete format entry). But even if the end format entry is incomplete,
836 // it ends in a complete entry (which is not an END_FMT). So is safe to traverse backwards.
837 // TODO: handle client corruption (in the middle of a buffer)
838
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700839 const uint8_t *back = snapshot->mData + availToRead;
840 const uint8_t *front = snapshot->mData;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800841
842 // Find last END_FMT. <back> is sitting on an entry which might be the middle of a FormatEntry.
843 // We go backwards until we find an EVENT_END_FMT.
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700844 const uint8_t *lastEnd = findLastEntryOfTypes(front, back, endingTypes);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800845 if (lastEnd == nullptr) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700846 snapshot->mEnd = snapshot->mBegin = EntryIterator(front);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800847 } else {
848 // end of snapshot points to after last END_FMT entry
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700849 snapshot->mEnd = EntryIterator(lastEnd).next();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800850 // find first START_FMT
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700851 const uint8_t *firstStart = nullptr;
852 const uint8_t *firstStartTmp = snapshot->mEnd;
853 while ((firstStartTmp = findLastEntryOfTypes(front, firstStartTmp, startingTypes))
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800854 != nullptr) {
855 firstStart = firstStartTmp;
856 }
857 // firstStart is null if no START_FMT entry was found before lastEnd
858 if (firstStart == nullptr) {
859 snapshot->mBegin = snapshot->mEnd;
860 } else {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700861 snapshot->mBegin = EntryIterator(firstStart);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800862 }
863 }
864
865 // advance fifo reader index to after last entry read.
866 mFifoReader->release(snapshot->mEnd - front);
867
868 snapshot->mLost = lost;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800869 return snapshot;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800870
Nicolas Roulet40a44982017-02-03 13:39:57 -0800871}
872
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700873// Takes raw content of the local merger FIFO, processes log entries, and
874// writes the data to a map of class PerformanceAnalysis, based on their thread ID.
875void NBLog::MergeReader::getAndProcessSnapshot(NBLog::Reader::Snapshot &snapshot)
Nicolas Roulet40a44982017-02-03 13:39:57 -0800876{
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700877 String8 timestamp, body;
Sanna Catherine de Treville Wagerd0965172017-07-24 13:42:44 -0700878 // TODO: check: is this thread safe?
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700879 // TODO: add lost data information and notification to ReportPerformance
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700880 size_t lost = snapshot.lost() + (snapshot.begin() - EntryIterator(snapshot.data()));
Glenn Kastenc02c9612013-10-15 09:25:11 -0700881 if (lost > 0) {
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700882 // TODO: ultimately, this will be += and reset to 0. TODO: check that this is
883 // obsolete now that Merger::merge is called periodically. No data should be lost
884 mLost = lost;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800885 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700886
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800887 for (auto entry = snapshot.begin(); entry != snapshot.end();) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800888 switch (entry->type) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800889 case EVENT_START_FMT:
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800890 entry = handleFormat(FormatEntry(entry), &timestamp, &body);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800891 break;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700892 case EVENT_HISTOGRAM_ENTRY_TS: {
893 HistTsEntryWithAuthor *data = (HistTsEntryWithAuthor *) (entry->data);
894 // TODO This memcpies are here to avoid unaligned memory access crash.
895 // There's probably a more efficient way to do it
896 log_hash_t hash;
897 memcpy(&hash, &(data->hash), sizeof(hash));
Nicolas Rouletad82aa62017-04-03 19:15:20 -0700898 int64_t ts;
899 memcpy(&ts, &data->ts, sizeof(ts));
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700900 // TODO: hash for histogram ts and audio state need to match
901 // and correspond to audio production source file location
902 mThreadPerformanceAnalysis[data->author][0 /*hash*/].logTsEntry(ts);
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700903 ++entry;
904 break;
905 }
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700906 case EVENT_AUDIO_STATE: {
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700907 HistTsEntryWithAuthor *data = (HistTsEntryWithAuthor *) (entry->data);
908 // TODO This memcpies are here to avoid unaligned memory access crash.
909 // There's probably a more efficient way to do it
Sanna Catherine de Treville Wagerd0965172017-07-24 13:42:44 -0700910 log_hash_t hash;
911 memcpy(&hash, &(data->hash), sizeof(hash));
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700912 // TODO: remove ts if unused
913 int64_t ts;
914 memcpy(&ts, &data->ts, sizeof(ts));
915 mThreadPerformanceAnalysis[data->author][0 /*hash*/].handleStateChange();
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700916 ++entry;
917 break;
918 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800919 case EVENT_END_FMT:
920 body.appendFormat("warning: got to end format event");
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800921 ++entry;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800922 break;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800923 case EVENT_RESERVED:
924 default:
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800925 body.appendFormat("warning: unexpected event %d", entry->type);
926 ++entry;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800927 break;
928 }
Sanna Catherine de Treville Wager9484bae2017-06-15 14:39:44 -0700929 }
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700930 // FIXME: decide whether to print the warnings here or elsewhere
Sanna Catherine de Treville Wager9484bae2017-06-15 14:39:44 -0700931 if (!body.isEmpty()) {
932 dumpLine(timestamp, body);
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700933 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800934}
935
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700936void NBLog::MergeReader::getAndProcessSnapshot()
Nicolas Roulet40a44982017-02-03 13:39:57 -0800937{
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700938 // get a snapshot, process it
Nicolas Roulet40a44982017-02-03 13:39:57 -0800939 std::unique_ptr<Snapshot> snap = getSnapshot();
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700940 getAndProcessSnapshot(*snap);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800941}
942
Sanna Catherine de Treville Wagercf6c75a2017-07-21 17:05:25 -0700943void NBLog::MergeReader::dump(int fd, int indent) {
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700944 // TODO: add a mutex around media.log dump
Sanna Catherine de Treville Wagercf6c75a2017-07-21 17:05:25 -0700945 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