blob: 0adeb46bc8f54aca255ca3e03902072142543f07 [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()
52* TODO: add description?
53* NBLog::MergeThread::threadLoop()
54* calls NBLog::Merger::merge
55* NBLog::Merger::merge
Sanna Catherine de Treville Wager1bb68622017-06-14 14:18:31 -070056* Merges snapshots sorted by timestamp
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070057* for each reader in vector of class NamedReader,
58* callsNamedReader::reader()->getSnapshot
59* TODO: check whether the rest of this function is relevant
60* NBLog::Reader::getSnapshot
61* copies snapshot of reader's fifo buffer into its own buffer
62* calls mFifoReader->obtain to find readable data
63* sets snapshot.begin() and .end() iterators to boundaries of valid entries
64* moves the fifo reader index to after the last entry read
Sanna Catherine de Treville Wager1bb68622017-06-14 14:18:31 -070065* in this case, the buffer is in shared memory. in (4), the buffer is private
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070066*
Sanna Catherine de Treville Wagerd4a68f62017-06-23 16:09:41 -070067* 3) reading the data from private buffer
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070068* MediaLogService::dump
Sanna Catherine de Treville Wager9484bae2017-06-15 14:39:44 -070069* calls NBLog::Reader::dump(CONSOLE)
70* The private buffer contains all logs for all readers in shared memory
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070071* NBLog::Reader::dump(int)
72* calls getSnapshot on the current reader
73* calls dump(int, size_t, Snapshot)
74* NBLog::Reader::dump(int, size, snapshot)
75* iterates through snapshot's events and switches based on their type
76* (string, timestamp, etc...)
77* In the case of EVENT_HISTOGRAM_ENTRY_TS, adds a list of timestamp sequences
78* (histogram entry) to NBLog::mHists
Sanna Catherine de Treville Wagerd4a68f62017-06-23 16:09:41 -070079* TODO: add every HISTOGRAM_ENTRY_TS to two
Sanna Catherine de Treville Wager1bb68622017-06-14 14:18:31 -070080* circular buffers: one short-term and one long-term (can add even longer-term
81* structures in the future). When dump is called, print everything currently
82* in the buffer.
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -070083* NBLog::drawHistogram
84* input: timestamp array
85* buckets this to a histogram and prints
86*
87*/
88
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080089#define LOG_TAG "NBLog"
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080090
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070091#include <algorithm>
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -080092#include <climits>
Sanna Catherine de Treville Wager201079a2017-05-18 16:36:29 -070093#include <deque>
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070094#include <fstream>
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070095#include <iostream>
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -070096#include <math.h>
Sanna Catherine de Treville Wager201079a2017-05-18 16:36:29 -070097#include <numeric>
Sanna Catherine de Treville Wager697a8a52017-06-01 09:49:05 -070098#include <vector>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080099#include <stdarg.h>
100#include <stdint.h>
101#include <stdio.h>
102#include <string.h>
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800103#include <sys/prctl.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800104#include <time.h>
105#include <new>
Glenn Kasten535e1612016-12-05 12:19:36 -0800106#include <audio_utils/roundup.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800107#include <media/nbaio/NBLog.h>
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700108#include <media/nbaio/PerformanceAnalysis.h>
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -0700109#include <media/nbaio/ReportPerformance.h>
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700110#include <utils/CallStack.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800111#include <utils/Log.h>
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700112#include <utils/String8.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800113
Nicolas Roulet40a44982017-02-03 13:39:57 -0800114#include <queue>
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700115#include <utility>
Nicolas Roulet40a44982017-02-03 13:39:57 -0800116
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800117namespace android {
118
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700119int NBLog::Entry::copyEntryDataAt(size_t offset) const
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800120{
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700121 // FIXME This is too slow
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800122 if (offset == 0)
123 return mEvent;
124 else if (offset == 1)
125 return mLength;
126 else if (offset < (size_t) (mLength + 2))
127 return ((char *) mData)[offset - 2];
128 else if (offset == (size_t) (mLength + 2))
129 return mLength;
130 else
131 return 0;
132}
133
134// ---------------------------------------------------------------------------
135
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700136/*static*/
137std::unique_ptr<NBLog::AbstractEntry> NBLog::AbstractEntry::buildEntry(const uint8_t *ptr) {
Sanna Catherine de Treville Wagercced6742017-05-10 14:42:54 -0700138 const uint8_t type = EntryIterator(ptr)->type;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700139 switch (type) {
140 case EVENT_START_FMT:
141 return std::make_unique<FormatEntry>(FormatEntry(ptr));
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700142 case EVENT_AUDIO_STATE:
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700143 case EVENT_HISTOGRAM_ENTRY_TS:
144 return std::make_unique<HistogramEntry>(HistogramEntry(ptr));
145 default:
146 ALOGW("Tried to create AbstractEntry of type %d", type);
147 return nullptr;
148 }
Nicolas Roulet40a44982017-02-03 13:39:57 -0800149}
150
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700151NBLog::AbstractEntry::AbstractEntry(const uint8_t *entry) : mEntry(entry) {
152}
153
154// ---------------------------------------------------------------------------
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800155
Sanna Catherine de Treville Wagerdd92d7e2017-05-15 14:56:53 -0700156NBLog::EntryIterator NBLog::FormatEntry::begin() const {
157 return EntryIterator(mEntry);
158}
159
Nicolas Roulet40a44982017-02-03 13:39:57 -0800160const char *NBLog::FormatEntry::formatString() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800161 return (const char*) mEntry + offsetof(entry, data);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800162}
163
164size_t NBLog::FormatEntry::formatStringLength() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800165 return mEntry[offsetof(entry, length)];
Nicolas Roulet40a44982017-02-03 13:39:57 -0800166}
167
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700168NBLog::EntryIterator NBLog::FormatEntry::args() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800169 auto it = begin();
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700170 // skip start fmt
171 ++it;
172 // skip timestamp
173 ++it;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700174 // skip hash
175 ++it;
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700176 // Skip author if present
177 if (it->type == EVENT_AUTHOR) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800178 ++it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800179 }
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700180 return it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800181}
182
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700183int64_t NBLog::FormatEntry::timestamp() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800184 auto it = begin();
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700185 // skip start fmt
186 ++it;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700187 return it.payload<int64_t>();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800188}
189
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700190NBLog::log_hash_t NBLog::FormatEntry::hash() const {
191 auto it = begin();
192 // skip start fmt
193 ++it;
194 // skip timestamp
195 ++it;
196 // unaligned 64-bit read not supported
197 log_hash_t hash;
198 memcpy(&hash, it->data, sizeof(hash));
199 return hash;
200}
201
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700202int NBLog::FormatEntry::author() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800203 auto it = begin();
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700204 // skip start fmt
205 ++it;
206 // skip timestamp
207 ++it;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700208 // skip hash
209 ++it;
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700210 // if there is an author entry, return it, return -1 otherwise
211 if (it->type == EVENT_AUTHOR) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800212 return it.payload<int>();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800213 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800214 return -1;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800215}
216
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700217NBLog::EntryIterator NBLog::FormatEntry::copyWithAuthor(
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800218 std::unique_ptr<audio_utils_fifo_writer> &dst, int author) const {
219 auto it = begin();
Nicolas Roulet40a44982017-02-03 13:39:57 -0800220 // copy fmt start entry
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800221 it.copyTo(dst);
Nicolas Roulet1ca75122017-03-16 14:19:59 -0700222 // copy timestamp
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700223 (++it).copyTo(dst); // copy hash
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700224 (++it).copyTo(dst);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800225 // insert author entry
226 size_t authorEntrySize = NBLog::Entry::kOverhead + sizeof(author);
227 uint8_t authorEntry[authorEntrySize];
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800228 authorEntry[offsetof(entry, type)] = EVENT_AUTHOR;
229 authorEntry[offsetof(entry, length)] =
230 authorEntry[authorEntrySize + NBLog::Entry::kPreviousLengthOffset] =
231 sizeof(author);
232 *(int*) (&authorEntry[offsetof(entry, data)]) = author;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800233 dst->write(authorEntry, authorEntrySize);
234 // copy rest of entries
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800235 while ((++it)->type != EVENT_END_FMT) {
236 it.copyTo(dst);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800237 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800238 it.copyTo(dst);
239 ++it;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800240 return it;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800241}
242
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700243void NBLog::EntryIterator::copyTo(std::unique_ptr<audio_utils_fifo_writer> &dst) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800244 size_t length = ptr[offsetof(entry, length)] + NBLog::Entry::kOverhead;
245 dst->write(ptr, length);
246}
Nicolas Roulet40a44982017-02-03 13:39:57 -0800247
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700248void NBLog::EntryIterator::copyData(uint8_t *dst) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800249 memcpy((void*) dst, ptr + offsetof(entry, data), ptr[offsetof(entry, length)]);
250}
251
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700252NBLog::EntryIterator::EntryIterator()
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800253 : ptr(nullptr) {}
254
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700255NBLog::EntryIterator::EntryIterator(const uint8_t *entry)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800256 : ptr(entry) {}
257
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700258NBLog::EntryIterator::EntryIterator(const NBLog::EntryIterator &other)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800259 : ptr(other.ptr) {}
260
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700261const NBLog::entry& NBLog::EntryIterator::operator*() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800262 return *(entry*) ptr;
263}
264
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700265const NBLog::entry* NBLog::EntryIterator::operator->() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800266 return (entry*) ptr;
267}
268
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700269NBLog::EntryIterator& NBLog::EntryIterator::operator++() {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800270 ptr += ptr[offsetof(entry, length)] + NBLog::Entry::kOverhead;
271 return *this;
272}
273
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700274NBLog::EntryIterator& NBLog::EntryIterator::operator--() {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800275 ptr -= ptr[NBLog::Entry::kPreviousLengthOffset] + NBLog::Entry::kOverhead;
276 return *this;
277}
278
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700279NBLog::EntryIterator NBLog::EntryIterator::next() const {
280 EntryIterator aux(*this);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800281 return ++aux;
282}
283
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700284NBLog::EntryIterator NBLog::EntryIterator::prev() const {
285 EntryIterator aux(*this);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800286 return --aux;
287}
288
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700289int NBLog::EntryIterator::operator-(const NBLog::EntryIterator &other) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800290 return ptr - other.ptr;
291}
292
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700293bool NBLog::EntryIterator::operator!=(const EntryIterator &other) const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800294 return ptr != other.ptr;
295}
296
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700297bool NBLog::EntryIterator::hasConsistentLength() const {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800298 return ptr[offsetof(entry, length)] == ptr[ptr[offsetof(entry, length)] +
299 NBLog::Entry::kOverhead + NBLog::Entry::kPreviousLengthOffset];
Nicolas Roulet40a44982017-02-03 13:39:57 -0800300}
301
302// ---------------------------------------------------------------------------
303
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700304int64_t NBLog::HistogramEntry::timestamp() const {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700305 return EntryIterator(mEntry).payload<HistTsEntry>().ts;
306}
307
308NBLog::log_hash_t NBLog::HistogramEntry::hash() const {
309 return EntryIterator(mEntry).payload<HistTsEntry>().hash;
310}
311
312int NBLog::HistogramEntry::author() const {
313 EntryIterator it(mEntry);
314 if (it->length == sizeof(HistTsEntryWithAuthor)) {
315 return it.payload<HistTsEntryWithAuthor>().author;
316 } else {
317 return -1;
318 }
319}
320
321NBLog::EntryIterator NBLog::HistogramEntry::copyWithAuthor(
322 std::unique_ptr<audio_utils_fifo_writer> &dst, int author) const {
323 // Current histogram entry has {type, length, struct HistTsEntry, length}.
324 // We now want {type, length, struct HistTsEntryWithAuthor, length}
325 uint8_t buffer[Entry::kOverhead + sizeof(HistTsEntryWithAuthor)];
326 // Copy content until the point we want to add the author
327 memcpy(buffer, mEntry, sizeof(entry) + sizeof(HistTsEntry));
328 // Copy the author
329 *(int*) (buffer + sizeof(entry) + sizeof(HistTsEntry)) = author;
330 // Update lengths
331 buffer[offsetof(entry, length)] = sizeof(HistTsEntryWithAuthor);
332 buffer[sizeof(buffer) + Entry::kPreviousLengthOffset] = sizeof(HistTsEntryWithAuthor);
333 // Write new buffer into FIFO
334 dst->write(buffer, sizeof(buffer));
335 return EntryIterator(mEntry).next();
336}
337
338// ---------------------------------------------------------------------------
339
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800340#if 0 // FIXME see note in NBLog.h
341NBLog::Timeline::Timeline(size_t size, void *shared)
342 : mSize(roundup(size)), mOwn(shared == NULL),
343 mShared((Shared *) (mOwn ? new char[sharedSize(size)] : shared))
344{
345 new (mShared) Shared;
346}
347
348NBLog::Timeline::~Timeline()
349{
350 mShared->~Shared();
351 if (mOwn) {
352 delete[] (char *) mShared;
353 }
354}
355#endif
356
357/*static*/
358size_t NBLog::Timeline::sharedSize(size_t size)
359{
Glenn Kastened99c2b2016-12-12 08:31:24 -0800360 // TODO fifo now supports non-power-of-2 buffer sizes, so could remove the roundup
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800361 return sizeof(Shared) + roundup(size);
362}
363
364// ---------------------------------------------------------------------------
365
366NBLog::Writer::Writer()
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800367 : mShared(NULL), mFifo(NULL), mFifoWriter(NULL), mEnabled(false), mPidTag(NULL), mPidTagSize(0)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800368{
369}
370
Glenn Kasten535e1612016-12-05 12:19:36 -0800371NBLog::Writer::Writer(void *shared, size_t size)
372 : mShared((Shared *) shared),
373 mFifo(mShared != NULL ?
374 new audio_utils_fifo(size, sizeof(uint8_t),
375 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
376 mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL),
377 mEnabled(mFifoWriter != NULL)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800378{
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800379 // caching pid and process name
380 pid_t id = ::getpid();
381 char procName[16];
382 int status = prctl(PR_GET_NAME, procName);
383 if (status) { // error getting process name
384 procName[0] = '\0';
385 }
386 size_t length = strlen(procName);
387 mPidTagSize = length + sizeof(pid_t);
388 mPidTag = new char[mPidTagSize];
389 memcpy(mPidTag, &id, sizeof(pid_t));
390 memcpy(mPidTag + sizeof(pid_t), procName, length);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800391}
392
Glenn Kasten535e1612016-12-05 12:19:36 -0800393NBLog::Writer::Writer(const sp<IMemory>& iMemory, size_t size)
394 : Writer(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800395{
Glenn Kasten535e1612016-12-05 12:19:36 -0800396 mIMemory = iMemory;
397}
398
399NBLog::Writer::~Writer()
400{
401 delete mFifoWriter;
402 delete mFifo;
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800403 delete[] mPidTag;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800404}
405
406void NBLog::Writer::log(const char *string)
407{
408 if (!mEnabled) {
409 return;
410 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800411 LOG_ALWAYS_FATAL_IF(string == NULL, "Attempted to log NULL string");
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800412 size_t length = strlen(string);
Glenn Kasten535e1612016-12-05 12:19:36 -0800413 if (length > Entry::kMaxLength) {
414 length = Entry::kMaxLength;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800415 }
416 log(EVENT_STRING, string, length);
417}
418
419void NBLog::Writer::logf(const char *fmt, ...)
420{
421 if (!mEnabled) {
422 return;
423 }
424 va_list ap;
425 va_start(ap, fmt);
426 Writer::logvf(fmt, ap); // the Writer:: is needed to avoid virtual dispatch for LockedWriter
427 va_end(ap);
428}
429
430void NBLog::Writer::logvf(const char *fmt, va_list ap)
431{
432 if (!mEnabled) {
433 return;
434 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800435 char buffer[Entry::kMaxLength + 1 /*NUL*/];
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800436 int length = vsnprintf(buffer, sizeof(buffer), fmt, ap);
437 if (length >= (int) sizeof(buffer)) {
438 length = sizeof(buffer) - 1;
439 // NUL termination is not required
440 // buffer[length] = '\0';
441 }
442 if (length >= 0) {
443 log(EVENT_STRING, buffer, length);
444 }
445}
446
447void NBLog::Writer::logTimestamp()
448{
449 if (!mEnabled) {
450 return;
451 }
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700452 int64_t ts = get_monotonic_ns();
453 if (ts > 0) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800454 log(EVENT_TIMESTAMP, &ts, sizeof(ts));
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700455 } else {
456 ALOGE("Failed to get timestamp");
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800457 }
458}
459
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700460void NBLog::Writer::logTimestamp(const int64_t ts)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800461{
462 if (!mEnabled) {
463 return;
464 }
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800465 log(EVENT_TIMESTAMP, &ts, sizeof(ts));
466}
467
468void NBLog::Writer::logInteger(const int x)
469{
470 if (!mEnabled) {
471 return;
472 }
473 log(EVENT_INTEGER, &x, sizeof(x));
474}
475
476void NBLog::Writer::logFloat(const float x)
477{
478 if (!mEnabled) {
479 return;
480 }
481 log(EVENT_FLOAT, &x, sizeof(x));
482}
483
484void NBLog::Writer::logPID()
485{
486 if (!mEnabled) {
487 return;
488 }
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800489 log(EVENT_PID, mPidTag, mPidTagSize);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800490}
491
492void NBLog::Writer::logStart(const char *fmt)
493{
494 if (!mEnabled) {
495 return;
496 }
497 size_t length = strlen(fmt);
498 if (length > Entry::kMaxLength) {
499 length = Entry::kMaxLength;
500 }
501 log(EVENT_START_FMT, fmt, length);
502}
503
504void NBLog::Writer::logEnd()
505{
506 if (!mEnabled) {
507 return;
508 }
509 Entry entry = Entry(EVENT_END_FMT, NULL, 0);
510 log(&entry, true);
511}
512
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700513void NBLog::Writer::logHash(log_hash_t hash)
514{
515 if (!mEnabled) {
516 return;
517 }
518 log(EVENT_HASH, &hash, sizeof(hash));
519}
520
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700521void NBLog::Writer::logEventHistTs(Event event, log_hash_t hash)
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700522{
523 if (!mEnabled) {
524 return;
525 }
526 HistTsEntry data;
527 data.hash = hash;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700528 data.ts = get_monotonic_ns();
529 if (data.ts > 0) {
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700530 log(event, &data, sizeof(data));
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700531 } else {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700532 ALOGE("Failed to get timestamp");
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700533 }
534}
535
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700536void NBLog::Writer::logFormat(const char *fmt, log_hash_t hash, ...)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800537{
538 if (!mEnabled) {
539 return;
540 }
541
542 va_list ap;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700543 va_start(ap, hash);
544 Writer::logVFormat(fmt, hash, ap);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800545 va_end(ap);
546}
547
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700548void NBLog::Writer::logVFormat(const char *fmt, log_hash_t hash, va_list argp)
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800549{
550 if (!mEnabled) {
551 return;
552 }
553 Writer::logStart(fmt);
554 int i;
555 double f;
556 char* s;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700557 int64_t t;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800558 Writer::logTimestamp();
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700559 Writer::logHash(hash);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800560 for (const char *p = fmt; *p != '\0'; p++) {
561 // TODO: implement more complex formatting such as %.3f
562 if (*p != '%') {
563 continue;
564 }
565 switch(*++p) {
566 case 's': // string
567 s = va_arg(argp, char *);
568 Writer::log(s);
569 break;
570
571 case 't': // timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700572 t = va_arg(argp, int64_t);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800573 Writer::logTimestamp(t);
574 break;
575
576 case 'd': // integer
577 i = va_arg(argp, int);
578 Writer::logInteger(i);
579 break;
580
581 case 'f': // float
582 f = va_arg(argp, double); // float arguments are promoted to double in vararg lists
583 Writer::logFloat((float)f);
584 break;
585
586 case 'p': // pid
587 Writer::logPID();
588 break;
589
590 // the "%\0" case finishes parsing
591 case '\0':
592 --p;
593 break;
594
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800595 case '%':
596 break;
597
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800598 default:
599 ALOGW("NBLog Writer parsed invalid format specifier: %c", *p);
600 break;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800601 }
602 }
603 Writer::logEnd();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800604}
605
606void NBLog::Writer::log(Event event, const void *data, size_t length)
607{
608 if (!mEnabled) {
609 return;
610 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800611 if (data == NULL || length > Entry::kMaxLength) {
612 // TODO Perhaps it makes sense to display truncated data or at least a
613 // message that the data is too long? The current behavior can create
614 // a confusion for a programmer debugging their code.
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800615 return;
616 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700617 // Ignore if invalid event
618 if (event == EVENT_RESERVED || event >= EVENT_UPPER_BOUND) {
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800619 return;
620 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700621 Entry etr(event, data, length);
622 log(&etr, true /*trusted*/);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800623}
624
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700625void NBLog::Writer::log(const NBLog::Entry *etr, bool trusted)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800626{
627 if (!mEnabled) {
628 return;
629 }
630 if (!trusted) {
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700631 log(etr->mEvent, etr->mData, etr->mLength);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800632 return;
633 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700634 size_t need = etr->mLength + Entry::kOverhead; // mEvent, mLength, data[mLength], mLength
635 // need = number of bytes written to FIFO
Glenn Kasten535e1612016-12-05 12:19:36 -0800636
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800637 // FIXME optimize this using memcpy for the data part of the Entry.
638 // The Entry could have a method copyTo(ptr, offset, size) to optimize the copy.
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700639 // checks size of a single log Entry: type, length, data pointer and ending
Glenn Kasten535e1612016-12-05 12:19:36 -0800640 uint8_t temp[Entry::kMaxLength + Entry::kOverhead];
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700641 // write this data to temp array
Glenn Kasten535e1612016-12-05 12:19:36 -0800642 for (size_t i = 0; i < need; i++) {
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700643 temp[i] = etr->copyEntryDataAt(i);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800644 }
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700645 // write to circular buffer
Glenn Kasten535e1612016-12-05 12:19:36 -0800646 mFifoWriter->write(temp, need);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800647}
648
649bool NBLog::Writer::isEnabled() const
650{
651 return mEnabled;
652}
653
654bool NBLog::Writer::setEnabled(bool enabled)
655{
656 bool old = mEnabled;
657 mEnabled = enabled && mShared != NULL;
658 return old;
659}
660
661// ---------------------------------------------------------------------------
662
663NBLog::LockedWriter::LockedWriter()
664 : Writer()
665{
666}
667
Glenn Kasten535e1612016-12-05 12:19:36 -0800668NBLog::LockedWriter::LockedWriter(void *shared, size_t size)
669 : Writer(shared, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800670{
671}
672
673void NBLog::LockedWriter::log(const char *string)
674{
675 Mutex::Autolock _l(mLock);
676 Writer::log(string);
677}
678
679void NBLog::LockedWriter::logf(const char *fmt, ...)
680{
681 // FIXME should not take the lock until after formatting is done
682 Mutex::Autolock _l(mLock);
683 va_list ap;
684 va_start(ap, fmt);
685 Writer::logvf(fmt, ap);
686 va_end(ap);
687}
688
689void NBLog::LockedWriter::logvf(const char *fmt, va_list ap)
690{
691 // FIXME should not take the lock until after formatting is done
692 Mutex::Autolock _l(mLock);
693 Writer::logvf(fmt, ap);
694}
695
696void NBLog::LockedWriter::logTimestamp()
697{
698 // FIXME should not take the lock until after the clock_gettime() syscall
699 Mutex::Autolock _l(mLock);
700 Writer::logTimestamp();
701}
702
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700703void NBLog::LockedWriter::logTimestamp(const int64_t ts)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800704{
705 Mutex::Autolock _l(mLock);
706 Writer::logTimestamp(ts);
707}
708
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800709void NBLog::LockedWriter::logInteger(const int x)
710{
711 Mutex::Autolock _l(mLock);
712 Writer::logInteger(x);
713}
714
715void NBLog::LockedWriter::logFloat(const float x)
716{
717 Mutex::Autolock _l(mLock);
718 Writer::logFloat(x);
719}
720
721void NBLog::LockedWriter::logPID()
722{
723 Mutex::Autolock _l(mLock);
724 Writer::logPID();
725}
726
727void NBLog::LockedWriter::logStart(const char *fmt)
728{
729 Mutex::Autolock _l(mLock);
730 Writer::logStart(fmt);
731}
732
733
734void NBLog::LockedWriter::logEnd()
735{
736 Mutex::Autolock _l(mLock);
737 Writer::logEnd();
738}
739
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700740void NBLog::LockedWriter::logHash(log_hash_t hash)
741{
742 Mutex::Autolock _l(mLock);
743 Writer::logHash(hash);
744}
745
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800746bool NBLog::LockedWriter::isEnabled() const
747{
748 Mutex::Autolock _l(mLock);
749 return Writer::isEnabled();
750}
751
752bool NBLog::LockedWriter::setEnabled(bool enabled)
753{
754 Mutex::Autolock _l(mLock);
755 return Writer::setEnabled(enabled);
756}
757
758// ---------------------------------------------------------------------------
759
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700760const std::set<NBLog::Event> NBLog::Reader::startingTypes {NBLog::Event::EVENT_START_FMT,
761 NBLog::Event::EVENT_HISTOGRAM_ENTRY_TS};
762const std::set<NBLog::Event> NBLog::Reader::endingTypes {NBLog::Event::EVENT_END_FMT,
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700763 NBLog::Event::EVENT_HISTOGRAM_ENTRY_TS,
764 NBLog::Event::EVENT_AUDIO_STATE};
765
Glenn Kasten535e1612016-12-05 12:19:36 -0800766NBLog::Reader::Reader(const void *shared, size_t size)
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700767 : mFd(-1), mIndent(0), mLost(0),
768 mShared((/*const*/ Shared *) shared), /*mIMemory*/
Glenn Kasten535e1612016-12-05 12:19:36 -0800769 mFifo(mShared != NULL ?
770 new audio_utils_fifo(size, sizeof(uint8_t),
771 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700772 mFifoReader(mFifo != NULL ? new audio_utils_fifo_reader(*mFifo) : NULL)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800773{
774}
775
Glenn Kasten535e1612016-12-05 12:19:36 -0800776NBLog::Reader::Reader(const sp<IMemory>& iMemory, size_t size)
777 : Reader(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size)
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800778{
Glenn Kasten535e1612016-12-05 12:19:36 -0800779 mIMemory = iMemory;
780}
781
782NBLog::Reader::~Reader()
783{
784 delete mFifoReader;
785 delete mFifo;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800786}
787
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700788const uint8_t *NBLog::Reader::findLastEntryOfTypes(const uint8_t *front, const uint8_t *back,
789 const std::set<Event> &types) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800790 while (back + Entry::kPreviousLengthOffset >= front) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700791 const uint8_t *prev = back - back[Entry::kPreviousLengthOffset] - Entry::kOverhead;
792 if (prev < front || prev + prev[offsetof(entry, length)] +
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800793 Entry::kOverhead != back) {
794
795 // prev points to an out of limits or inconsistent entry
796 return nullptr;
797 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700798 if (types.find((const Event) prev[offsetof(entry, type)]) != types.end()) {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800799 return prev;
800 }
801 back = prev;
802 }
803 return nullptr; // no entry found
804}
805
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700806// Copies content of a Reader FIFO into its Snapshot
807// The Snapshot has the same raw data, but represented as a sequence of entries
808// and an EntryIterator making it possible to process the data.
Nicolas Roulet40a44982017-02-03 13:39:57 -0800809std::unique_ptr<NBLog::Reader::Snapshot> NBLog::Reader::getSnapshot()
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800810{
Glenn Kasten535e1612016-12-05 12:19:36 -0800811 if (mFifoReader == NULL) {
Nicolas Roulet40a44982017-02-03 13:39:57 -0800812 return std::unique_ptr<NBLog::Reader::Snapshot>(new Snapshot());
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800813 }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800814 // make a copy to avoid race condition with writer
Glenn Kasten535e1612016-12-05 12:19:36 -0800815 size_t capacity = mFifo->capacity();
816
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800817 // This emulates the behaviour of audio_utils_fifo_reader::read, but without incrementing the
818 // reader index. The index is incremented after handling corruption, to after the last complete
819 // entry of the buffer
820 size_t lost;
821 audio_utils_iovec iovec[2];
822 ssize_t availToRead = mFifoReader->obtain(iovec, capacity, NULL /*timeout*/, &lost);
823 if (availToRead <= 0) {
824 return std::unique_ptr<NBLog::Reader::Snapshot>(new Snapshot());
825 }
Glenn Kasten535e1612016-12-05 12:19:36 -0800826
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800827 std::unique_ptr<Snapshot> snapshot(new Snapshot(availToRead));
828 memcpy(snapshot->mData, (const char *) mFifo->buffer() + iovec[0].mOffset, iovec[0].mLength);
829 if (iovec[1].mLength > 0) {
830 memcpy(snapshot->mData + (iovec[0].mLength),
831 (const char *) mFifo->buffer() + iovec[1].mOffset, iovec[1].mLength);
832 }
833
834 // Handle corrupted buffer
835 // Potentially, a buffer has corrupted data on both beginning (due to overflow) and end
836 // (due to incomplete format entry). But even if the end format entry is incomplete,
837 // it ends in a complete entry (which is not an END_FMT). So is safe to traverse backwards.
838 // TODO: handle client corruption (in the middle of a buffer)
839
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700840 const uint8_t *back = snapshot->mData + availToRead;
841 const uint8_t *front = snapshot->mData;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800842
843 // Find last END_FMT. <back> is sitting on an entry which might be the middle of a FormatEntry.
844 // We go backwards until we find an EVENT_END_FMT.
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700845 const uint8_t *lastEnd = findLastEntryOfTypes(front, back, endingTypes);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800846 if (lastEnd == nullptr) {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700847 snapshot->mEnd = snapshot->mBegin = EntryIterator(front);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800848 } else {
849 // end of snapshot points to after last END_FMT entry
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700850 snapshot->mEnd = EntryIterator(lastEnd).next();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800851 // find first START_FMT
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700852 const uint8_t *firstStart = nullptr;
853 const uint8_t *firstStartTmp = snapshot->mEnd;
854 while ((firstStartTmp = findLastEntryOfTypes(front, firstStartTmp, startingTypes))
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800855 != nullptr) {
856 firstStart = firstStartTmp;
857 }
858 // firstStart is null if no START_FMT entry was found before lastEnd
859 if (firstStart == nullptr) {
860 snapshot->mBegin = snapshot->mEnd;
861 } else {
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700862 snapshot->mBegin = EntryIterator(firstStart);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800863 }
864 }
865
866 // advance fifo reader index to after last entry read.
867 mFifoReader->release(snapshot->mEnd - front);
868
869 snapshot->mLost = lost;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800870 return snapshot;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800871
Nicolas Roulet40a44982017-02-03 13:39:57 -0800872}
873
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700874// Takes raw content of the local merger FIFO, processes log entries, and
875// writes the data to a map of class PerformanceAnalysis, based on their thread ID.
876void NBLog::MergeReader::getAndProcessSnapshot(NBLog::Reader::Snapshot &snapshot)
Nicolas Roulet40a44982017-02-03 13:39:57 -0800877{
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700878 String8 timestamp, body;
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700879 // TODO: check: is the FIXME below still a problem?
Sanna Catherine de Treville Wager41cad592017-06-29 14:57:59 -0700880 // FIXME: this is not thread safe
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700881 // TODO: add lost data information and notification to ReportPerformance
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700882 size_t lost = snapshot.lost() + (snapshot.begin() - EntryIterator(snapshot.data()));
Glenn Kastenc02c9612013-10-15 09:25:11 -0700883 if (lost > 0) {
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700884 // TODO: ultimately, this will be += and reset to 0. TODO: check that this is
885 // obsolete now that Merger::merge is called periodically. No data should be lost
886 mLost = lost;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800887 }
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700888
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800889 for (auto entry = snapshot.begin(); entry != snapshot.end();) {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800890 switch (entry->type) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800891 case EVENT_START_FMT:
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800892 entry = handleFormat(FormatEntry(entry), &timestamp, &body);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800893 break;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700894 case EVENT_HISTOGRAM_ENTRY_TS: {
895 HistTsEntryWithAuthor *data = (HistTsEntryWithAuthor *) (entry->data);
896 // TODO This memcpies are here to avoid unaligned memory access crash.
897 // There's probably a more efficient way to do it
898 log_hash_t hash;
899 memcpy(&hash, &(data->hash), sizeof(hash));
Nicolas Rouletad82aa62017-04-03 19:15:20 -0700900 int64_t ts;
901 memcpy(&ts, &data->ts, sizeof(ts));
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700902 mThreadPerformanceAnalysis[data->author].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
910 // TODO: incorporate hash information in mThreadPerformanceAnalysis
911 // log_hash_t hash;
912 // memcpy(&hash, &(data->hash), sizeof(hash));
913 // int64_t ts;
914 // memcpy(&ts, &data->ts, sizeof(ts));
915 mThreadPerformanceAnalysis[data->author].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) {
944 ReportPerformance::dump(fd, indent, mThreadPerformanceAnalysis);
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -0700945}
946
947// Writes a string to the console
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800948void NBLog::Reader::dumpLine(const String8 &timestamp, String8 &body)
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700949{
950 if (mFd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700951 dprintf(mFd, "%.*s%s %s\n", mIndent, "", timestamp.string(), body.string());
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700952 } else {
953 ALOGI("%.*s%s %s", mIndent, "", timestamp.string(), body.string());
954 }
955 body.clear();
956}
957
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800958bool NBLog::Reader::isIMemory(const sp<IMemory>& iMemory) const
959{
Glenn Kasten481fb672013-09-30 14:39:28 -0700960 return iMemory != 0 && mIMemory != 0 && iMemory->pointer() == mIMemory->pointer();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800961}
962
Glenn Kasten1c446272017-04-07 09:49:07 -0700963// ---------------------------------------------------------------------------
964
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800965void NBLog::appendTimestamp(String8 *body, const void *data) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700966 int64_t ts;
967 memcpy(&ts, data, sizeof(ts));
968 body->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
969 (int) ((ts / (1000 * 1000)) % 1000));
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800970}
971
972void NBLog::appendInt(String8 *body, const void *data) {
973 int x = *((int*) data);
974 body->appendFormat("<%d>", x);
975}
976
977void NBLog::appendFloat(String8 *body, const void *data) {
978 float f;
979 memcpy(&f, data, sizeof(float));
980 body->appendFormat("<%f>", f);
981}
982
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800983void NBLog::appendPID(String8 *body, const void* data, size_t length) {
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800984 pid_t id = *((pid_t*) data);
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800985 char * name = &((char*) data)[sizeof(pid_t)];
986 body->appendFormat("<PID: %d, name: %.*s>", id, (int) (length - sizeof(pid_t)), name);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800987}
988
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700989String8 NBLog::bufferDump(const uint8_t *buffer, size_t size)
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700990{
991 String8 str;
992 str.append("[ ");
993 for(size_t i = 0; i < size; i++)
994 {
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700995 str.appendFormat("%d ", buffer[i]);
Nicolas Roulet2aedf372017-03-29 11:27:03 -0700996 }
997 str.append("]");
998 return str;
999}
1000
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001001String8 NBLog::bufferDump(const EntryIterator &it)
Nicolas Roulet2aedf372017-03-29 11:27:03 -07001002{
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001003 return bufferDump(it, it->length + Entry::kOverhead);
Nicolas Roulet2aedf372017-03-29 11:27:03 -07001004}
1005
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001006NBLog::EntryIterator NBLog::Reader::handleFormat(const FormatEntry &fmtEntry,
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001007 String8 *timestamp,
1008 String8 *body) {
Nicolas Roulet40a44982017-02-03 13:39:57 -08001009 // log timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001010 int64_t ts = fmtEntry.timestamp();
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001011 timestamp->clear();
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001012 timestamp->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
1013 (int) ((ts / (1000 * 1000)) % 1000));
Nicolas Roulet40a44982017-02-03 13:39:57 -08001014
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -07001015 // log unique hash
1016 log_hash_t hash = fmtEntry.hash();
1017 // print only lower 16bit of hash as hex and line as int to reduce spam in the log
1018 body->appendFormat("%.4X-%d ", (int)(hash >> 16) & 0xFFFF, (int) hash & 0xFFFF);
1019
Nicolas Roulet40a44982017-02-03 13:39:57 -08001020 // log author (if present)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001021 handleAuthor(fmtEntry, body);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001022
1023 // log string
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001024 NBLog::EntryIterator arg = fmtEntry.args();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001025
1026 const char* fmt = fmtEntry.formatString();
1027 size_t fmt_length = fmtEntry.formatStringLength();
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001028
1029 for (size_t fmt_offset = 0; fmt_offset < fmt_length; ++fmt_offset) {
1030 if (fmt[fmt_offset] != '%') {
1031 body->append(&fmt[fmt_offset], 1); // TODO optimize to write consecutive strings at once
1032 continue;
1033 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001034 // case "%%""
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001035 if (fmt[++fmt_offset] == '%') {
1036 body->append("%");
1037 continue;
1038 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001039 // case "%\0"
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001040 if (fmt_offset == fmt_length) {
1041 continue;
1042 }
1043
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001044 NBLog::Event event = (NBLog::Event) arg->type;
1045 size_t length = arg->length;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001046
1047 // TODO check length for event type is correct
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001048
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001049 if (event == EVENT_END_FMT) {
1050 break;
1051 }
1052
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001053 // TODO: implement more complex formatting such as %.3f
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001054 const uint8_t *datum = arg->data; // pointer to the current event args
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001055 switch(fmt[fmt_offset])
1056 {
1057 case 's': // string
Nicolas Roulet4da78202017-02-03 12:53:39 -08001058 ALOGW_IF(event != EVENT_STRING,
1059 "NBLog Reader incompatible event for string specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001060 body->append((const char*) datum, length);
1061 break;
1062
1063 case 't': // timestamp
Nicolas Roulet4da78202017-02-03 12:53:39 -08001064 ALOGW_IF(event != EVENT_TIMESTAMP,
1065 "NBLog Reader incompatible event for timestamp specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001066 appendTimestamp(body, datum);
1067 break;
1068
1069 case 'd': // integer
Nicolas Roulet4da78202017-02-03 12:53:39 -08001070 ALOGW_IF(event != EVENT_INTEGER,
1071 "NBLog Reader incompatible event for integer specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001072 appendInt(body, datum);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001073 break;
1074
1075 case 'f': // float
Nicolas Roulet4da78202017-02-03 12:53:39 -08001076 ALOGW_IF(event != EVENT_FLOAT,
1077 "NBLog Reader incompatible event for float specifier: %d", event);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001078 appendFloat(body, datum);
1079 break;
1080
1081 case 'p': // pid
Nicolas Roulet4da78202017-02-03 12:53:39 -08001082 ALOGW_IF(event != EVENT_PID,
1083 "NBLog Reader incompatible event for pid specifier: %d", event);
Nicolas Rouletc20cb502017-02-01 12:35:24 -08001084 appendPID(body, datum, length);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001085 break;
1086
1087 default:
1088 ALOGW("NBLog Reader encountered unknown character %c", fmt[fmt_offset]);
1089 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001090 ++arg;
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001091 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -08001092 ALOGW_IF(arg->type != EVENT_END_FMT, "Expected end of format, got %d", arg->type);
1093 ++arg;
1094 return arg;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001095}
1096
Nicolas Roulet40a44982017-02-03 13:39:57 -08001097NBLog::Merger::Merger(const void *shared, size_t size):
Nicolas Roulet40a44982017-02-03 13:39:57 -08001098 mShared((Shared *) shared),
1099 mFifo(mShared != NULL ?
1100 new audio_utils_fifo(size, sizeof(uint8_t),
1101 mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
1102 mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL)
1103 {}
1104
1105void NBLog::Merger::addReader(const NBLog::NamedReader &reader) {
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001106
Glenn Kasten1c446272017-04-07 09:49:07 -07001107 // FIXME This is called by binder thread in MediaLogService::registerWriter
1108 // but the access to shared variable mNamedReaders is not yet protected by a lock.
Nicolas Roulet40a44982017-02-03 13:39:57 -08001109 mNamedReaders.push_back(reader);
1110}
1111
1112// items placed in priority queue during merge
1113// composed by a timestamp and the index of the snapshot where the timestamp came from
1114struct MergeItem
1115{
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001116 int64_t ts;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001117 int index;
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001118 MergeItem(int64_t ts, int index): ts(ts), index(index) {}
Nicolas Roulet40a44982017-02-03 13:39:57 -08001119};
1120
1121// operators needed for priority queue in merge
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001122// bool operator>(const int64_t &t1, const int64_t &t2) {
1123// return t1.tv_sec > t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec);
1124// }
Nicolas Roulet40a44982017-02-03 13:39:57 -08001125
1126bool operator>(const struct MergeItem &i1, const struct MergeItem &i2) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001127 return i1.ts > i2.ts || (i1.ts == i2.ts && i1.index > i2.index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001128}
1129
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001130// Merge registered readers, sorted by timestamp, and write data to a single FIFO in local memory
Nicolas Roulet40a44982017-02-03 13:39:57 -08001131void NBLog::Merger::merge() {
Glenn Kasten1c446272017-04-07 09:49:07 -07001132 // FIXME This is called by merge thread
1133 // but the access to shared variable mNamedReaders is not yet protected by a lock.
Nicolas Roulet40a44982017-02-03 13:39:57 -08001134 int nLogs = mNamedReaders.size();
1135 std::vector<std::unique_ptr<NBLog::Reader::Snapshot>> snapshots(nLogs);
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001136 std::vector<NBLog::EntryIterator> offsets(nLogs);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001137 for (int i = 0; i < nLogs; ++i) {
1138 snapshots[i] = mNamedReaders[i].reader()->getSnapshot();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001139 offsets[i] = snapshots[i]->begin();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001140 }
1141 // initialize offsets
Nicolas Roulet40a44982017-02-03 13:39:57 -08001142 // TODO custom heap implementation could allow to update top, improving performance
1143 // for bursty buffers
1144 std::priority_queue<MergeItem, std::vector<MergeItem>, std::greater<MergeItem>> timestamps;
1145 for (int i = 0; i < nLogs; ++i)
1146 {
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001147 if (offsets[i] != snapshots[i]->end()) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001148 int64_t ts = AbstractEntry::buildEntry(offsets[i])->timestamp();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001149 timestamps.emplace(ts, i);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001150 }
1151 }
1152
1153 while (!timestamps.empty()) {
1154 // find minimum timestamp
1155 int index = timestamps.top().index;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001156 // copy it to the log, increasing offset
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001157 offsets[index] = AbstractEntry::buildEntry(offsets[index])->copyWithAuthor(mFifoWriter,
1158 index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001159 // update data structures
Nicolas Roulet40a44982017-02-03 13:39:57 -08001160 timestamps.pop();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001161 if (offsets[index] != snapshots[index]->end()) {
Nicolas Rouletf42f1562017-03-30 19:16:22 -07001162 int64_t ts = AbstractEntry::buildEntry(offsets[index])->timestamp();
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -08001163 timestamps.emplace(ts, index);
Nicolas Roulet40a44982017-02-03 13:39:57 -08001164 }
1165 }
1166}
1167
Glenn Kasten1c446272017-04-07 09:49:07 -07001168const std::vector<NBLog::NamedReader>& NBLog::Merger::getNamedReaders() const {
1169 // FIXME This is returning a reference to a shared variable that needs a lock
1170 return mNamedReaders;
Nicolas Roulet40a44982017-02-03 13:39:57 -08001171}
1172
Glenn Kasten1c446272017-04-07 09:49:07 -07001173// ---------------------------------------------------------------------------
1174
Nicolas Roulet40a44982017-02-03 13:39:57 -08001175NBLog::MergeReader::MergeReader(const void *shared, size_t size, Merger &merger)
1176 : Reader(shared, size), mNamedReaders(merger.getNamedReaders()) {}
1177
Nicolas Roulet537ad7d2017-03-21 16:24:30 -07001178void NBLog::MergeReader::handleAuthor(const NBLog::AbstractEntry &entry, String8 *body) {
1179 int author = entry.author();
Glenn Kasten1c446272017-04-07 09:49:07 -07001180 // FIXME Needs a lock
1181 const char* name = mNamedReaders[author].name();
Nicolas Roulet40a44982017-02-03 13:39:57 -08001182 body->appendFormat("%s: ", name);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -08001183}
1184
Glenn Kasten1c446272017-04-07 09:49:07 -07001185// ---------------------------------------------------------------------------
1186
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001187NBLog::MergeThread::MergeThread(NBLog::Merger &merger, NBLog::MergeReader &mergeReader)
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001188 : mMerger(merger),
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001189 mMergeReader(mergeReader),
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001190 mTimeoutUs(0) {}
1191
1192NBLog::MergeThread::~MergeThread() {
1193 // set exit flag, set timeout to 0 to force threadLoop to exit and wait for the thread to join
1194 requestExit();
1195 setTimeoutUs(0);
1196 join();
1197}
1198
1199bool NBLog::MergeThread::threadLoop() {
1200 bool doMerge;
1201 {
1202 AutoMutex _l(mMutex);
1203 // If mTimeoutUs is negative, wait on the condition variable until it's positive.
1204 // If it's positive, wait kThreadSleepPeriodUs and then merge
1205 nsecs_t waitTime = mTimeoutUs > 0 ? kThreadSleepPeriodUs * 1000 : LLONG_MAX;
1206 mCond.waitRelative(mMutex, waitTime);
1207 doMerge = mTimeoutUs > 0;
1208 mTimeoutUs -= kThreadSleepPeriodUs;
1209 }
1210 if (doMerge) {
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001211 // Merge data from all the readers
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001212 mMerger.merge();
Sanna Catherine de Treville Wagere4865262017-07-14 16:24:15 -07001213 // Process the data collected by mMerger and write it to PerformanceAnalysis
1214 // FIXME: decide whether to call getAndProcessSnapshot every time
1215 // or whether to have a separate thread that calls it with a lower frequency
1216 mMergeReader.getAndProcessSnapshot();
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001217 }
1218 return true;
1219}
1220
1221void NBLog::MergeThread::wakeup() {
1222 setTimeoutUs(kThreadWakeupPeriodUs);
1223}
1224
1225void NBLog::MergeThread::setTimeoutUs(int time) {
1226 AutoMutex _l(mMutex);
1227 mTimeoutUs = time;
1228 mCond.signal();
1229}
1230
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001231} // namespace android