xref: /aosp_15_r20/frameworks/av/media/libnblog/Reader.cpp (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1*ec779b8eSAndroid Build Coastguard Worker /*
2*ec779b8eSAndroid Build Coastguard Worker  * Copyright (C) 2018 The Android Open Source Project
3*ec779b8eSAndroid Build Coastguard Worker  *
4*ec779b8eSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*ec779b8eSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*ec779b8eSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*ec779b8eSAndroid Build Coastguard Worker  *
8*ec779b8eSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*ec779b8eSAndroid Build Coastguard Worker  *
10*ec779b8eSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*ec779b8eSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*ec779b8eSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*ec779b8eSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*ec779b8eSAndroid Build Coastguard Worker  * limitations under the License.
15*ec779b8eSAndroid Build Coastguard Worker  */
16*ec779b8eSAndroid Build Coastguard Worker 
17*ec779b8eSAndroid Build Coastguard Worker #define LOG_TAG "NBLog"
18*ec779b8eSAndroid Build Coastguard Worker //#define LOG_NDEBUG 0
19*ec779b8eSAndroid Build Coastguard Worker 
20*ec779b8eSAndroid Build Coastguard Worker #include <memory>
21*ec779b8eSAndroid Build Coastguard Worker #include <stddef.h>
22*ec779b8eSAndroid Build Coastguard Worker #include <string>
23*ec779b8eSAndroid Build Coastguard Worker #include <unordered_set>
24*ec779b8eSAndroid Build Coastguard Worker 
25*ec779b8eSAndroid Build Coastguard Worker #include <audio_utils/fifo.h>
26*ec779b8eSAndroid Build Coastguard Worker #include <binder/IMemory.h>
27*ec779b8eSAndroid Build Coastguard Worker #include <media/nblog/Entry.h>
28*ec779b8eSAndroid Build Coastguard Worker #include <media/nblog/Events.h>
29*ec779b8eSAndroid Build Coastguard Worker #include <media/nblog/Reader.h>
30*ec779b8eSAndroid Build Coastguard Worker #include <media/nblog/Timeline.h>
31*ec779b8eSAndroid Build Coastguard Worker #include <utils/Log.h>
32*ec779b8eSAndroid Build Coastguard Worker #include <utils/String8.h>
33*ec779b8eSAndroid Build Coastguard Worker 
34*ec779b8eSAndroid Build Coastguard Worker namespace android {
35*ec779b8eSAndroid Build Coastguard Worker namespace NBLog {
36*ec779b8eSAndroid Build Coastguard Worker 
Reader(const void * shared,size_t size,const std::string & name)37*ec779b8eSAndroid Build Coastguard Worker Reader::Reader(const void *shared, size_t size, const std::string &name)
38*ec779b8eSAndroid Build Coastguard Worker     : mName(name),
39*ec779b8eSAndroid Build Coastguard Worker       mShared((/*const*/ Shared *) shared), /*mIMemory*/
40*ec779b8eSAndroid Build Coastguard Worker       mFifo(mShared != NULL ?
41*ec779b8eSAndroid Build Coastguard Worker         new audio_utils_fifo(size, sizeof(uint8_t),
42*ec779b8eSAndroid Build Coastguard Worker             mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL),
43*ec779b8eSAndroid Build Coastguard Worker       mFifoReader(mFifo != NULL ? new audio_utils_fifo_reader(*mFifo) : NULL)
44*ec779b8eSAndroid Build Coastguard Worker {
45*ec779b8eSAndroid Build Coastguard Worker }
46*ec779b8eSAndroid Build Coastguard Worker 
Reader(const sp<IMemory> & iMemory,size_t size,const std::string & name)47*ec779b8eSAndroid Build Coastguard Worker Reader::Reader(const sp<IMemory>& iMemory, size_t size, const std::string &name)
48*ec779b8eSAndroid Build Coastguard Worker     // TODO: Using unsecurePointer() has some associated security pitfalls
49*ec779b8eSAndroid Build Coastguard Worker     //       (see declaration for details).
50*ec779b8eSAndroid Build Coastguard Worker     //       Either document why it is safe in this case or address the
51*ec779b8eSAndroid Build Coastguard Worker     //       issue (e.g. by copying).
52*ec779b8eSAndroid Build Coastguard Worker     : Reader(iMemory != 0 ? (Shared *) iMemory->unsecurePointer() : NULL, size,
53*ec779b8eSAndroid Build Coastguard Worker              name)
54*ec779b8eSAndroid Build Coastguard Worker {
55*ec779b8eSAndroid Build Coastguard Worker     mIMemory = iMemory;
56*ec779b8eSAndroid Build Coastguard Worker }
57*ec779b8eSAndroid Build Coastguard Worker 
~Reader()58*ec779b8eSAndroid Build Coastguard Worker Reader::~Reader()
59*ec779b8eSAndroid Build Coastguard Worker {
60*ec779b8eSAndroid Build Coastguard Worker     delete mFifoReader;
61*ec779b8eSAndroid Build Coastguard Worker     delete mFifo;
62*ec779b8eSAndroid Build Coastguard Worker }
63*ec779b8eSAndroid Build Coastguard Worker 
64*ec779b8eSAndroid Build Coastguard Worker // Copies content of a Reader FIFO into its Snapshot
65*ec779b8eSAndroid Build Coastguard Worker // The Snapshot has the same raw data, but represented as a sequence of entries
66*ec779b8eSAndroid Build Coastguard Worker // and an EntryIterator making it possible to process the data.
getSnapshot(bool flush)67*ec779b8eSAndroid Build Coastguard Worker std::unique_ptr<Snapshot> Reader::getSnapshot(bool flush)
68*ec779b8eSAndroid Build Coastguard Worker {
69*ec779b8eSAndroid Build Coastguard Worker     if (mFifoReader == NULL) {
70*ec779b8eSAndroid Build Coastguard Worker         return std::unique_ptr<Snapshot>(new Snapshot());
71*ec779b8eSAndroid Build Coastguard Worker     }
72*ec779b8eSAndroid Build Coastguard Worker 
73*ec779b8eSAndroid Build Coastguard Worker     // This emulates the behaviour of audio_utils_fifo_reader::read, but without incrementing the
74*ec779b8eSAndroid Build Coastguard Worker     // reader index. The index is incremented after handling corruption, to after the last complete
75*ec779b8eSAndroid Build Coastguard Worker     // entry of the buffer
76*ec779b8eSAndroid Build Coastguard Worker     size_t lost = 0;
77*ec779b8eSAndroid Build Coastguard Worker     audio_utils_iovec iovec[2];
78*ec779b8eSAndroid Build Coastguard Worker     const size_t capacity = mFifo->capacity();
79*ec779b8eSAndroid Build Coastguard Worker     ssize_t availToRead;
80*ec779b8eSAndroid Build Coastguard Worker     // A call to audio_utils_fifo_reader::obtain() places the read pointer one buffer length
81*ec779b8eSAndroid Build Coastguard Worker     // before the writer's pointer (since mFifoReader was constructed with flush=false). The
82*ec779b8eSAndroid Build Coastguard Worker     // do while loop is an attempt to read all of the FIFO's contents regardless of how behind
83*ec779b8eSAndroid Build Coastguard Worker     // the reader is with respect to the writer. However, the following scheduling sequence is
84*ec779b8eSAndroid Build Coastguard Worker     // possible and can lead to a starvation situation:
85*ec779b8eSAndroid Build Coastguard Worker     // - Writer T1 writes, overrun with respect to Reader T2
86*ec779b8eSAndroid Build Coastguard Worker     // - T2 calls obtain() and gets EOVERFLOW, T2 ptr placed one buffer size behind T1 ptr
87*ec779b8eSAndroid Build Coastguard Worker     // - T1 write, overrun
88*ec779b8eSAndroid Build Coastguard Worker     // - T2 obtain(), EOVERFLOW (and so on...)
89*ec779b8eSAndroid Build Coastguard Worker     // To address this issue, we limit the number of tries for the reader to catch up with
90*ec779b8eSAndroid Build Coastguard Worker     // the writer.
91*ec779b8eSAndroid Build Coastguard Worker     int tries = 0;
92*ec779b8eSAndroid Build Coastguard Worker     size_t lostTemp;
93*ec779b8eSAndroid Build Coastguard Worker     do {
94*ec779b8eSAndroid Build Coastguard Worker         availToRead = mFifoReader->obtain(iovec, capacity, NULL /*timeout*/, &lostTemp);
95*ec779b8eSAndroid Build Coastguard Worker         lost += lostTemp;
96*ec779b8eSAndroid Build Coastguard Worker     } while (availToRead < 0 && ++tries <= kMaxObtainTries);
97*ec779b8eSAndroid Build Coastguard Worker 
98*ec779b8eSAndroid Build Coastguard Worker     if (availToRead <= 0) {
99*ec779b8eSAndroid Build Coastguard Worker         ALOGW_IF(availToRead < 0, "NBLog Reader %s failed to catch up with Writer", mName.c_str());
100*ec779b8eSAndroid Build Coastguard Worker         return std::unique_ptr<Snapshot>(new Snapshot());
101*ec779b8eSAndroid Build Coastguard Worker     }
102*ec779b8eSAndroid Build Coastguard Worker 
103*ec779b8eSAndroid Build Coastguard Worker     // Change to #if 1 for debugging. This statement is useful for checking buffer fullness levels
104*ec779b8eSAndroid Build Coastguard Worker     // (as seen by reader) and how much data was lost. If you find that the fullness level is
105*ec779b8eSAndroid Build Coastguard Worker     // getting close to full, or that data loss is happening to often, then you should
106*ec779b8eSAndroid Build Coastguard Worker     // probably try some of the following:
107*ec779b8eSAndroid Build Coastguard Worker     // - log less data
108*ec779b8eSAndroid Build Coastguard Worker     // - log less often
109*ec779b8eSAndroid Build Coastguard Worker     // - increase the initial shared memory allocation for the buffer
110*ec779b8eSAndroid Build Coastguard Worker #if 0
111*ec779b8eSAndroid Build Coastguard Worker     ALOGD("getSnapshot name=%s, availToRead=%zd, capacity=%zu, fullness=%.3f, lost=%zu",
112*ec779b8eSAndroid Build Coastguard Worker             name().c_str(), availToRead, capacity, (double)availToRead / (double)capacity, lost);
113*ec779b8eSAndroid Build Coastguard Worker #endif
114*ec779b8eSAndroid Build Coastguard Worker     std::unique_ptr<Snapshot> snapshot(new Snapshot(availToRead));
115*ec779b8eSAndroid Build Coastguard Worker     memcpy(snapshot->mData, (const char *) mFifo->buffer() + iovec[0].mOffset, iovec[0].mLength);
116*ec779b8eSAndroid Build Coastguard Worker     if (iovec[1].mLength > 0) {
117*ec779b8eSAndroid Build Coastguard Worker         memcpy(snapshot->mData + (iovec[0].mLength),
118*ec779b8eSAndroid Build Coastguard Worker                 (const char *) mFifo->buffer() + iovec[1].mOffset, iovec[1].mLength);
119*ec779b8eSAndroid Build Coastguard Worker     }
120*ec779b8eSAndroid Build Coastguard Worker 
121*ec779b8eSAndroid Build Coastguard Worker     // Handle corrupted buffer
122*ec779b8eSAndroid Build Coastguard Worker     // Potentially, a buffer has corrupted data on both beginning (due to overflow) and end
123*ec779b8eSAndroid Build Coastguard Worker     // (due to incomplete format entry). But even if the end format entry is incomplete,
124*ec779b8eSAndroid Build Coastguard Worker     // it ends in a complete entry (which is not an FMT_END). So is safe to traverse backwards.
125*ec779b8eSAndroid Build Coastguard Worker     // TODO: handle client corruption (in the middle of a buffer)
126*ec779b8eSAndroid Build Coastguard Worker 
127*ec779b8eSAndroid Build Coastguard Worker     const uint8_t *back = snapshot->mData + availToRead;
128*ec779b8eSAndroid Build Coastguard Worker     const uint8_t *front = snapshot->mData;
129*ec779b8eSAndroid Build Coastguard Worker 
130*ec779b8eSAndroid Build Coastguard Worker     // Find last FMT_END. <back> is sitting on an entry which might be the middle of a FormatEntry.
131*ec779b8eSAndroid Build Coastguard Worker     // We go backwards until we find an EVENT_FMT_END.
132*ec779b8eSAndroid Build Coastguard Worker     const uint8_t *lastEnd = findLastValidEntry(front, back, invalidEndTypes);
133*ec779b8eSAndroid Build Coastguard Worker     if (lastEnd == nullptr) {
134*ec779b8eSAndroid Build Coastguard Worker         snapshot->mEnd = snapshot->mBegin = EntryIterator(front);
135*ec779b8eSAndroid Build Coastguard Worker     } else {
136*ec779b8eSAndroid Build Coastguard Worker         // end of snapshot points to after last FMT_END entry
137*ec779b8eSAndroid Build Coastguard Worker         snapshot->mEnd = EntryIterator(lastEnd).next();
138*ec779b8eSAndroid Build Coastguard Worker         // find first FMT_START
139*ec779b8eSAndroid Build Coastguard Worker         const uint8_t *firstStart = nullptr;
140*ec779b8eSAndroid Build Coastguard Worker         const uint8_t *firstStartTmp = snapshot->mEnd;
141*ec779b8eSAndroid Build Coastguard Worker         while ((firstStartTmp = findLastValidEntry(front, firstStartTmp, invalidBeginTypes))
142*ec779b8eSAndroid Build Coastguard Worker                 != nullptr) {
143*ec779b8eSAndroid Build Coastguard Worker             firstStart = firstStartTmp;
144*ec779b8eSAndroid Build Coastguard Worker         }
145*ec779b8eSAndroid Build Coastguard Worker         // firstStart is null if no FMT_START entry was found before lastEnd
146*ec779b8eSAndroid Build Coastguard Worker         if (firstStart == nullptr) {
147*ec779b8eSAndroid Build Coastguard Worker             snapshot->mBegin = snapshot->mEnd;
148*ec779b8eSAndroid Build Coastguard Worker         } else {
149*ec779b8eSAndroid Build Coastguard Worker             snapshot->mBegin = EntryIterator(firstStart);
150*ec779b8eSAndroid Build Coastguard Worker         }
151*ec779b8eSAndroid Build Coastguard Worker     }
152*ec779b8eSAndroid Build Coastguard Worker 
153*ec779b8eSAndroid Build Coastguard Worker     // advance fifo reader index to after last entry read.
154*ec779b8eSAndroid Build Coastguard Worker     if (flush) {
155*ec779b8eSAndroid Build Coastguard Worker         mFifoReader->release(snapshot->mEnd - front);
156*ec779b8eSAndroid Build Coastguard Worker     }
157*ec779b8eSAndroid Build Coastguard Worker 
158*ec779b8eSAndroid Build Coastguard Worker     snapshot->mLost = lost;
159*ec779b8eSAndroid Build Coastguard Worker     return snapshot;
160*ec779b8eSAndroid Build Coastguard Worker }
161*ec779b8eSAndroid Build Coastguard Worker 
isIMemory(const sp<IMemory> & iMemory) const162*ec779b8eSAndroid Build Coastguard Worker bool Reader::isIMemory(const sp<IMemory>& iMemory) const
163*ec779b8eSAndroid Build Coastguard Worker {
164*ec779b8eSAndroid Build Coastguard Worker     return iMemory != 0 && mIMemory != 0 &&
165*ec779b8eSAndroid Build Coastguard Worker            iMemory->unsecurePointer() == mIMemory->unsecurePointer();
166*ec779b8eSAndroid Build Coastguard Worker }
167*ec779b8eSAndroid Build Coastguard Worker 
168*ec779b8eSAndroid Build Coastguard Worker // We make a set of the invalid types rather than the valid types when aligning
169*ec779b8eSAndroid Build Coastguard Worker // Snapshot EntryIterators to valid entries during log corruption checking.
170*ec779b8eSAndroid Build Coastguard Worker // This is done in order to avoid the maintenance overhead of adding a new Event
171*ec779b8eSAndroid Build Coastguard Worker // type to the two sets below whenever a new Event type is created, as it is
172*ec779b8eSAndroid Build Coastguard Worker // very likely that new types added will be valid types.
173*ec779b8eSAndroid Build Coastguard Worker // Currently, invalidBeginTypes and invalidEndTypes are used to handle the special
174*ec779b8eSAndroid Build Coastguard Worker // case of a Format Entry, which consists of a variable number of simple log entries.
175*ec779b8eSAndroid Build Coastguard Worker // If a new Event is added that consists of a variable number of simple log entries,
176*ec779b8eSAndroid Build Coastguard Worker // then these sets need to be updated.
177*ec779b8eSAndroid Build Coastguard Worker 
178*ec779b8eSAndroid Build Coastguard Worker // We want the beginning of a Snapshot to point to an entry that is not in
179*ec779b8eSAndroid Build Coastguard Worker // the middle of a formatted entry and not an FMT_END.
180*ec779b8eSAndroid Build Coastguard Worker const std::unordered_set<Event> Reader::invalidBeginTypes {
181*ec779b8eSAndroid Build Coastguard Worker     EVENT_FMT_AUTHOR,
182*ec779b8eSAndroid Build Coastguard Worker     EVENT_FMT_END,
183*ec779b8eSAndroid Build Coastguard Worker     EVENT_FMT_FLOAT,
184*ec779b8eSAndroid Build Coastguard Worker     EVENT_FMT_HASH,
185*ec779b8eSAndroid Build Coastguard Worker     EVENT_FMT_INTEGER,
186*ec779b8eSAndroid Build Coastguard Worker     EVENT_FMT_PID,
187*ec779b8eSAndroid Build Coastguard Worker     EVENT_FMT_STRING,
188*ec779b8eSAndroid Build Coastguard Worker     EVENT_FMT_TIMESTAMP,
189*ec779b8eSAndroid Build Coastguard Worker };
190*ec779b8eSAndroid Build Coastguard Worker 
191*ec779b8eSAndroid Build Coastguard Worker // We want the end of a Snapshot to point to an entry that is not in
192*ec779b8eSAndroid Build Coastguard Worker // the middle of a formatted entry and not a FMT_START.
193*ec779b8eSAndroid Build Coastguard Worker const std::unordered_set<Event> Reader::invalidEndTypes {
194*ec779b8eSAndroid Build Coastguard Worker     EVENT_FMT_AUTHOR,
195*ec779b8eSAndroid Build Coastguard Worker     EVENT_FMT_FLOAT,
196*ec779b8eSAndroid Build Coastguard Worker     EVENT_FMT_HASH,
197*ec779b8eSAndroid Build Coastguard Worker     EVENT_FMT_INTEGER,
198*ec779b8eSAndroid Build Coastguard Worker     EVENT_FMT_PID,
199*ec779b8eSAndroid Build Coastguard Worker     EVENT_FMT_START,
200*ec779b8eSAndroid Build Coastguard Worker     EVENT_FMT_STRING,
201*ec779b8eSAndroid Build Coastguard Worker     EVENT_FMT_TIMESTAMP,
202*ec779b8eSAndroid Build Coastguard Worker };
203*ec779b8eSAndroid Build Coastguard Worker 
findLastValidEntry(const uint8_t * front,const uint8_t * back,const std::unordered_set<Event> & invalidTypes)204*ec779b8eSAndroid Build Coastguard Worker const uint8_t *Reader::findLastValidEntry(const uint8_t *front, const uint8_t *back,
205*ec779b8eSAndroid Build Coastguard Worker                                           const std::unordered_set<Event> &invalidTypes) {
206*ec779b8eSAndroid Build Coastguard Worker     if (front == nullptr || back == nullptr) {
207*ec779b8eSAndroid Build Coastguard Worker         return nullptr;
208*ec779b8eSAndroid Build Coastguard Worker     }
209*ec779b8eSAndroid Build Coastguard Worker     while (back + Entry::kPreviousLengthOffset >= front) {
210*ec779b8eSAndroid Build Coastguard Worker         const uint8_t *prev = back - back[Entry::kPreviousLengthOffset] - Entry::kOverhead;
211*ec779b8eSAndroid Build Coastguard Worker         if (prev < front
212*ec779b8eSAndroid Build Coastguard Worker                 || prev + prev[offsetof(entry, length)] + Entry::kOverhead != back) {
213*ec779b8eSAndroid Build Coastguard Worker             // prev points to an out of limits entry
214*ec779b8eSAndroid Build Coastguard Worker             return nullptr;
215*ec779b8eSAndroid Build Coastguard Worker         }
216*ec779b8eSAndroid Build Coastguard Worker         const Event type = (const Event)prev[offsetof(entry, type)];
217*ec779b8eSAndroid Build Coastguard Worker         if (type <= EVENT_RESERVED || type >= EVENT_UPPER_BOUND) {
218*ec779b8eSAndroid Build Coastguard Worker             // prev points to an inconsistent entry
219*ec779b8eSAndroid Build Coastguard Worker             return nullptr;
220*ec779b8eSAndroid Build Coastguard Worker         }
221*ec779b8eSAndroid Build Coastguard Worker         // if invalidTypes does not contain the type, then the type is valid.
222*ec779b8eSAndroid Build Coastguard Worker         if (invalidTypes.find(type) == invalidTypes.end()) {
223*ec779b8eSAndroid Build Coastguard Worker             return prev;
224*ec779b8eSAndroid Build Coastguard Worker         }
225*ec779b8eSAndroid Build Coastguard Worker         back = prev;
226*ec779b8eSAndroid Build Coastguard Worker     }
227*ec779b8eSAndroid Build Coastguard Worker     return nullptr; // no entry found
228*ec779b8eSAndroid Build Coastguard Worker }
229*ec779b8eSAndroid Build Coastguard Worker 
230*ec779b8eSAndroid Build Coastguard Worker // TODO for future compatibility, would prefer to have a dump() go to string, and then go
231*ec779b8eSAndroid Build Coastguard Worker // to fd only when invoked through binder.
dump(int fd,size_t indent)232*ec779b8eSAndroid Build Coastguard Worker void DumpReader::dump(int fd, size_t indent)
233*ec779b8eSAndroid Build Coastguard Worker {
234*ec779b8eSAndroid Build Coastguard Worker     if (fd < 0) return;
235*ec779b8eSAndroid Build Coastguard Worker     std::unique_ptr<Snapshot> snapshot = getSnapshot(false /*flush*/);
236*ec779b8eSAndroid Build Coastguard Worker     if (snapshot == nullptr) {
237*ec779b8eSAndroid Build Coastguard Worker         return;
238*ec779b8eSAndroid Build Coastguard Worker     }
239*ec779b8eSAndroid Build Coastguard Worker     String8 timestamp, body;
240*ec779b8eSAndroid Build Coastguard Worker 
241*ec779b8eSAndroid Build Coastguard Worker     // TODO all logged types should have a printable format.
242*ec779b8eSAndroid Build Coastguard Worker     // TODO can we make the printing generic?
243*ec779b8eSAndroid Build Coastguard Worker     for (EntryIterator it = snapshot->begin(); it != snapshot->end(); ++it) {
244*ec779b8eSAndroid Build Coastguard Worker         switch (it->type) {
245*ec779b8eSAndroid Build Coastguard Worker         case EVENT_FMT_START:
246*ec779b8eSAndroid Build Coastguard Worker             it = handleFormat(FormatEntry(it), &timestamp, &body);
247*ec779b8eSAndroid Build Coastguard Worker             break;
248*ec779b8eSAndroid Build Coastguard Worker         case EVENT_LATENCY: {
249*ec779b8eSAndroid Build Coastguard Worker             const double latencyMs = it.payload<double>();
250*ec779b8eSAndroid Build Coastguard Worker             body.appendFormat("EVENT_LATENCY,%.3f", latencyMs);
251*ec779b8eSAndroid Build Coastguard Worker         } break;
252*ec779b8eSAndroid Build Coastguard Worker         case EVENT_OVERRUN: {
253*ec779b8eSAndroid Build Coastguard Worker             const int64_t ts = it.payload<int64_t>();
254*ec779b8eSAndroid Build Coastguard Worker             body.appendFormat("EVENT_OVERRUN,%lld", static_cast<long long>(ts));
255*ec779b8eSAndroid Build Coastguard Worker         } break;
256*ec779b8eSAndroid Build Coastguard Worker         case EVENT_THREAD_INFO: {
257*ec779b8eSAndroid Build Coastguard Worker             const thread_info_t info = it.payload<thread_info_t>();
258*ec779b8eSAndroid Build Coastguard Worker             body.appendFormat("EVENT_THREAD_INFO,%d,%s", static_cast<int>(info.id),
259*ec779b8eSAndroid Build Coastguard Worker                     threadTypeToString(info.type));
260*ec779b8eSAndroid Build Coastguard Worker         } break;
261*ec779b8eSAndroid Build Coastguard Worker         case EVENT_UNDERRUN: {
262*ec779b8eSAndroid Build Coastguard Worker             const int64_t ts = it.payload<int64_t>();
263*ec779b8eSAndroid Build Coastguard Worker             body.appendFormat("EVENT_UNDERRUN,%lld", static_cast<long long>(ts));
264*ec779b8eSAndroid Build Coastguard Worker         } break;
265*ec779b8eSAndroid Build Coastguard Worker         case EVENT_WARMUP_TIME: {
266*ec779b8eSAndroid Build Coastguard Worker             const double timeMs = it.payload<double>();
267*ec779b8eSAndroid Build Coastguard Worker             body.appendFormat("EVENT_WARMUP_TIME,%.3f", timeMs);
268*ec779b8eSAndroid Build Coastguard Worker         } break;
269*ec779b8eSAndroid Build Coastguard Worker         case EVENT_WORK_TIME: {
270*ec779b8eSAndroid Build Coastguard Worker             const int64_t monotonicNs = it.payload<int64_t>();
271*ec779b8eSAndroid Build Coastguard Worker             body.appendFormat("EVENT_WORK_TIME,%lld", static_cast<long long>(monotonicNs));
272*ec779b8eSAndroid Build Coastguard Worker         } break;
273*ec779b8eSAndroid Build Coastguard Worker         case EVENT_THREAD_PARAMS: {
274*ec779b8eSAndroid Build Coastguard Worker             const thread_params_t params = it.payload<thread_params_t>();
275*ec779b8eSAndroid Build Coastguard Worker             body.appendFormat("EVENT_THREAD_PARAMS,%zu,%u", params.frameCount, params.sampleRate);
276*ec779b8eSAndroid Build Coastguard Worker         } break;
277*ec779b8eSAndroid Build Coastguard Worker         case EVENT_FMT_END:
278*ec779b8eSAndroid Build Coastguard Worker         case EVENT_RESERVED:
279*ec779b8eSAndroid Build Coastguard Worker         case EVENT_UPPER_BOUND:
280*ec779b8eSAndroid Build Coastguard Worker             body.appendFormat("warning: unexpected event %d", it->type);
281*ec779b8eSAndroid Build Coastguard Worker             break;
282*ec779b8eSAndroid Build Coastguard Worker         default:
283*ec779b8eSAndroid Build Coastguard Worker             break;
284*ec779b8eSAndroid Build Coastguard Worker         }
285*ec779b8eSAndroid Build Coastguard Worker         if (!body.empty()) {
286*ec779b8eSAndroid Build Coastguard Worker             dprintf(fd, "%.*s%s %s\n", (int)indent, "", timestamp.c_str(), body.c_str());
287*ec779b8eSAndroid Build Coastguard Worker             body.clear();
288*ec779b8eSAndroid Build Coastguard Worker         }
289*ec779b8eSAndroid Build Coastguard Worker         timestamp.clear();
290*ec779b8eSAndroid Build Coastguard Worker     }
291*ec779b8eSAndroid Build Coastguard Worker }
292*ec779b8eSAndroid Build Coastguard Worker 
handleFormat(const FormatEntry & fmtEntry,String8 * timestamp,String8 * body)293*ec779b8eSAndroid Build Coastguard Worker EntryIterator DumpReader::handleFormat(const FormatEntry &fmtEntry,
294*ec779b8eSAndroid Build Coastguard Worker         String8 *timestamp, String8 *body)
295*ec779b8eSAndroid Build Coastguard Worker {
296*ec779b8eSAndroid Build Coastguard Worker     String8 timestampLocal;
297*ec779b8eSAndroid Build Coastguard Worker     String8 bodyLocal;
298*ec779b8eSAndroid Build Coastguard Worker     if (timestamp == nullptr) {
299*ec779b8eSAndroid Build Coastguard Worker         timestamp = &timestampLocal;
300*ec779b8eSAndroid Build Coastguard Worker     }
301*ec779b8eSAndroid Build Coastguard Worker     if (body == nullptr) {
302*ec779b8eSAndroid Build Coastguard Worker         body = &bodyLocal;
303*ec779b8eSAndroid Build Coastguard Worker     }
304*ec779b8eSAndroid Build Coastguard Worker 
305*ec779b8eSAndroid Build Coastguard Worker     // log timestamp
306*ec779b8eSAndroid Build Coastguard Worker     const int64_t ts = fmtEntry.timestamp();
307*ec779b8eSAndroid Build Coastguard Worker     timestamp->clear();
308*ec779b8eSAndroid Build Coastguard Worker     timestamp->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
309*ec779b8eSAndroid Build Coastguard Worker                     (int) ((ts / (1000 * 1000)) % 1000));
310*ec779b8eSAndroid Build Coastguard Worker 
311*ec779b8eSAndroid Build Coastguard Worker     // log unique hash
312*ec779b8eSAndroid Build Coastguard Worker     log_hash_t hash = fmtEntry.hash();
313*ec779b8eSAndroid Build Coastguard Worker     // print only lower 16bit of hash as hex and line as int to reduce spam in the log
314*ec779b8eSAndroid Build Coastguard Worker     body->appendFormat("%.4X-%d ", (int)(hash >> 16) & 0xFFFF, (int) hash & 0xFFFF);
315*ec779b8eSAndroid Build Coastguard Worker 
316*ec779b8eSAndroid Build Coastguard Worker     // log author (if present)
317*ec779b8eSAndroid Build Coastguard Worker     handleAuthor(fmtEntry, body);
318*ec779b8eSAndroid Build Coastguard Worker 
319*ec779b8eSAndroid Build Coastguard Worker     // log string
320*ec779b8eSAndroid Build Coastguard Worker     EntryIterator arg = fmtEntry.args();
321*ec779b8eSAndroid Build Coastguard Worker 
322*ec779b8eSAndroid Build Coastguard Worker     const char* fmt = fmtEntry.formatString();
323*ec779b8eSAndroid Build Coastguard Worker     size_t fmt_length = fmtEntry.formatStringLength();
324*ec779b8eSAndroid Build Coastguard Worker 
325*ec779b8eSAndroid Build Coastguard Worker     for (size_t fmt_offset = 0; fmt_offset < fmt_length; ++fmt_offset) {
326*ec779b8eSAndroid Build Coastguard Worker         if (fmt[fmt_offset] != '%') {
327*ec779b8eSAndroid Build Coastguard Worker             body->append(&fmt[fmt_offset], 1); // TODO optimize to write consecutive strings at once
328*ec779b8eSAndroid Build Coastguard Worker             continue;
329*ec779b8eSAndroid Build Coastguard Worker         }
330*ec779b8eSAndroid Build Coastguard Worker         // case "%%""
331*ec779b8eSAndroid Build Coastguard Worker         if (fmt[++fmt_offset] == '%') {
332*ec779b8eSAndroid Build Coastguard Worker             body->append("%");
333*ec779b8eSAndroid Build Coastguard Worker             continue;
334*ec779b8eSAndroid Build Coastguard Worker         }
335*ec779b8eSAndroid Build Coastguard Worker         // case "%\0"
336*ec779b8eSAndroid Build Coastguard Worker         if (fmt_offset == fmt_length) {
337*ec779b8eSAndroid Build Coastguard Worker             continue;
338*ec779b8eSAndroid Build Coastguard Worker         }
339*ec779b8eSAndroid Build Coastguard Worker 
340*ec779b8eSAndroid Build Coastguard Worker         Event event = (Event) arg->type;
341*ec779b8eSAndroid Build Coastguard Worker         size_t length = arg->length;
342*ec779b8eSAndroid Build Coastguard Worker 
343*ec779b8eSAndroid Build Coastguard Worker         // TODO check length for event type is correct
344*ec779b8eSAndroid Build Coastguard Worker 
345*ec779b8eSAndroid Build Coastguard Worker         if (event == EVENT_FMT_END) {
346*ec779b8eSAndroid Build Coastguard Worker             break;
347*ec779b8eSAndroid Build Coastguard Worker         }
348*ec779b8eSAndroid Build Coastguard Worker 
349*ec779b8eSAndroid Build Coastguard Worker         // TODO: implement more complex formatting such as %.3f
350*ec779b8eSAndroid Build Coastguard Worker         const uint8_t *datum = arg->data; // pointer to the current event args
351*ec779b8eSAndroid Build Coastguard Worker         switch(fmt[fmt_offset])
352*ec779b8eSAndroid Build Coastguard Worker         {
353*ec779b8eSAndroid Build Coastguard Worker         case 's': // string
354*ec779b8eSAndroid Build Coastguard Worker             ALOGW_IF(event != EVENT_FMT_STRING,
355*ec779b8eSAndroid Build Coastguard Worker                 "NBLog Reader incompatible event for string specifier: %d", event);
356*ec779b8eSAndroid Build Coastguard Worker             body->append((const char*) datum, length);
357*ec779b8eSAndroid Build Coastguard Worker             break;
358*ec779b8eSAndroid Build Coastguard Worker 
359*ec779b8eSAndroid Build Coastguard Worker         case 't': // timestamp
360*ec779b8eSAndroid Build Coastguard Worker             ALOGW_IF(event != EVENT_FMT_TIMESTAMP,
361*ec779b8eSAndroid Build Coastguard Worker                 "NBLog Reader incompatible event for timestamp specifier: %d", event);
362*ec779b8eSAndroid Build Coastguard Worker             appendTimestamp(body, datum);
363*ec779b8eSAndroid Build Coastguard Worker             break;
364*ec779b8eSAndroid Build Coastguard Worker 
365*ec779b8eSAndroid Build Coastguard Worker         case 'd': // integer
366*ec779b8eSAndroid Build Coastguard Worker             ALOGW_IF(event != EVENT_FMT_INTEGER,
367*ec779b8eSAndroid Build Coastguard Worker                 "NBLog Reader incompatible event for integer specifier: %d", event);
368*ec779b8eSAndroid Build Coastguard Worker             appendInt(body, datum);
369*ec779b8eSAndroid Build Coastguard Worker             break;
370*ec779b8eSAndroid Build Coastguard Worker 
371*ec779b8eSAndroid Build Coastguard Worker         case 'f': // float
372*ec779b8eSAndroid Build Coastguard Worker             ALOGW_IF(event != EVENT_FMT_FLOAT,
373*ec779b8eSAndroid Build Coastguard Worker                 "NBLog Reader incompatible event for float specifier: %d", event);
374*ec779b8eSAndroid Build Coastguard Worker             appendFloat(body, datum);
375*ec779b8eSAndroid Build Coastguard Worker             break;
376*ec779b8eSAndroid Build Coastguard Worker 
377*ec779b8eSAndroid Build Coastguard Worker         case 'p': // pid
378*ec779b8eSAndroid Build Coastguard Worker             ALOGW_IF(event != EVENT_FMT_PID,
379*ec779b8eSAndroid Build Coastguard Worker                 "NBLog Reader incompatible event for pid specifier: %d", event);
380*ec779b8eSAndroid Build Coastguard Worker             appendPID(body, datum, length);
381*ec779b8eSAndroid Build Coastguard Worker             break;
382*ec779b8eSAndroid Build Coastguard Worker 
383*ec779b8eSAndroid Build Coastguard Worker         default:
384*ec779b8eSAndroid Build Coastguard Worker             ALOGW("NBLog Reader encountered unknown character %c", fmt[fmt_offset]);
385*ec779b8eSAndroid Build Coastguard Worker         }
386*ec779b8eSAndroid Build Coastguard Worker         ++arg;
387*ec779b8eSAndroid Build Coastguard Worker     }
388*ec779b8eSAndroid Build Coastguard Worker     ALOGW_IF(arg->type != EVENT_FMT_END, "Expected end of format, got %d", arg->type);
389*ec779b8eSAndroid Build Coastguard Worker     return arg;
390*ec779b8eSAndroid Build Coastguard Worker }
391*ec779b8eSAndroid Build Coastguard Worker 
appendInt(String8 * body,const void * data)392*ec779b8eSAndroid Build Coastguard Worker void DumpReader::appendInt(String8 *body, const void *data)
393*ec779b8eSAndroid Build Coastguard Worker {
394*ec779b8eSAndroid Build Coastguard Worker     if (body == nullptr || data == nullptr) {
395*ec779b8eSAndroid Build Coastguard Worker         return;
396*ec779b8eSAndroid Build Coastguard Worker     }
397*ec779b8eSAndroid Build Coastguard Worker     //int x = *((int*) data);
398*ec779b8eSAndroid Build Coastguard Worker     int x;
399*ec779b8eSAndroid Build Coastguard Worker     memcpy(&x, data, sizeof(x));
400*ec779b8eSAndroid Build Coastguard Worker     body->appendFormat("<%d>", x);
401*ec779b8eSAndroid Build Coastguard Worker }
402*ec779b8eSAndroid Build Coastguard Worker 
appendFloat(String8 * body,const void * data)403*ec779b8eSAndroid Build Coastguard Worker void DumpReader::appendFloat(String8 *body, const void *data)
404*ec779b8eSAndroid Build Coastguard Worker {
405*ec779b8eSAndroid Build Coastguard Worker     if (body == nullptr || data == nullptr) {
406*ec779b8eSAndroid Build Coastguard Worker         return;
407*ec779b8eSAndroid Build Coastguard Worker     }
408*ec779b8eSAndroid Build Coastguard Worker     float f;
409*ec779b8eSAndroid Build Coastguard Worker     memcpy(&f, data, sizeof(f));
410*ec779b8eSAndroid Build Coastguard Worker     body->appendFormat("<%f>", f);
411*ec779b8eSAndroid Build Coastguard Worker }
412*ec779b8eSAndroid Build Coastguard Worker 
appendPID(String8 * body,const void * data,size_t length)413*ec779b8eSAndroid Build Coastguard Worker void DumpReader::appendPID(String8 *body, const void* data, size_t length)
414*ec779b8eSAndroid Build Coastguard Worker {
415*ec779b8eSAndroid Build Coastguard Worker     if (body == nullptr || data == nullptr) {
416*ec779b8eSAndroid Build Coastguard Worker         return;
417*ec779b8eSAndroid Build Coastguard Worker     }
418*ec779b8eSAndroid Build Coastguard Worker     pid_t id = *((pid_t*) data);
419*ec779b8eSAndroid Build Coastguard Worker     char * name = &((char*) data)[sizeof(pid_t)];
420*ec779b8eSAndroid Build Coastguard Worker     body->appendFormat("<PID: %d, name: %.*s>", id, (int) (length - sizeof(pid_t)), name);
421*ec779b8eSAndroid Build Coastguard Worker }
422*ec779b8eSAndroid Build Coastguard Worker 
appendTimestamp(String8 * body,const void * data)423*ec779b8eSAndroid Build Coastguard Worker void DumpReader::appendTimestamp(String8 *body, const void *data)
424*ec779b8eSAndroid Build Coastguard Worker {
425*ec779b8eSAndroid Build Coastguard Worker     if (body == nullptr || data == nullptr) {
426*ec779b8eSAndroid Build Coastguard Worker         return;
427*ec779b8eSAndroid Build Coastguard Worker     }
428*ec779b8eSAndroid Build Coastguard Worker     int64_t ts;
429*ec779b8eSAndroid Build Coastguard Worker     memcpy(&ts, data, sizeof(ts));
430*ec779b8eSAndroid Build Coastguard Worker     body->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)),
431*ec779b8eSAndroid Build Coastguard Worker                     (int) ((ts / (1000 * 1000)) % 1000));
432*ec779b8eSAndroid Build Coastguard Worker }
433*ec779b8eSAndroid Build Coastguard Worker 
bufferDump(const uint8_t * buffer,size_t size)434*ec779b8eSAndroid Build Coastguard Worker String8 DumpReader::bufferDump(const uint8_t *buffer, size_t size)
435*ec779b8eSAndroid Build Coastguard Worker {
436*ec779b8eSAndroid Build Coastguard Worker     String8 str;
437*ec779b8eSAndroid Build Coastguard Worker     if (buffer == nullptr) {
438*ec779b8eSAndroid Build Coastguard Worker         return str;
439*ec779b8eSAndroid Build Coastguard Worker     }
440*ec779b8eSAndroid Build Coastguard Worker     str.append("[ ");
441*ec779b8eSAndroid Build Coastguard Worker     for(size_t i = 0; i < size; i++) {
442*ec779b8eSAndroid Build Coastguard Worker         str.appendFormat("%d ", buffer[i]);
443*ec779b8eSAndroid Build Coastguard Worker     }
444*ec779b8eSAndroid Build Coastguard Worker     str.append("]");
445*ec779b8eSAndroid Build Coastguard Worker     return str;
446*ec779b8eSAndroid Build Coastguard Worker }
447*ec779b8eSAndroid Build Coastguard Worker 
bufferDump(const EntryIterator & it)448*ec779b8eSAndroid Build Coastguard Worker String8 DumpReader::bufferDump(const EntryIterator &it)
449*ec779b8eSAndroid Build Coastguard Worker {
450*ec779b8eSAndroid Build Coastguard Worker     return bufferDump(it, it->length + Entry::kOverhead);
451*ec779b8eSAndroid Build Coastguard Worker }
452*ec779b8eSAndroid Build Coastguard Worker 
453*ec779b8eSAndroid Build Coastguard Worker }   // namespace NBLog
454*ec779b8eSAndroid Build Coastguard Worker }   // namespace android
455