blob: dd240ea05ce09417ac03f5501541987d8f90d106 [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
17// Non-blocking event logger intended for safe communication between processes via shared memory
18
19#ifndef ANDROID_MEDIA_NBLOG_H
20#define ANDROID_MEDIA_NBLOG_H
21
22#include <binder/IMemory.h>
Glenn Kasten535e1612016-12-05 12:19:36 -080023#include <audio_utils/fifo.h>
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -080024#include <utils/Mutex.h>
25#include <utils/threads.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080026
Nicolas Rouletad82aa62017-04-03 19:15:20 -070027#include <map>
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070028#include <set>
Nicolas Roulet40a44982017-02-03 13:39:57 -080029#include <vector>
30
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080031namespace android {
32
Glenn Kasten4e01ef62013-07-11 14:29:59 -070033class String8;
34
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080035class NBLog {
36
37public:
38
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -070039typedef uint64_t log_hash_t;
40
Glenn Kasten1c446272017-04-07 09:49:07 -070041// FIXME Everything needed for client (writer API and registration) should be isolated
42// from the rest of the implementation.
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080043class Writer;
44class Reader;
45
46private:
47
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070048enum Event : uint8_t {
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080049 EVENT_RESERVED,
50 EVENT_STRING, // ASCII string, not NUL-terminated
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -080051 // TODO: make timestamp optional
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080052 EVENT_TIMESTAMP, // clock_gettime(CLOCK_MONOTONIC)
Nicolas Roulet40a44982017-02-03 13:39:57 -080053 EVENT_INTEGER, // integer value entry
54 EVENT_FLOAT, // floating point value entry
55 EVENT_PID, // process ID and process name
56 EVENT_AUTHOR, // author index (present in merged logs) tracks entry's original log
Nicolas Rouletfe1e1442017-01-30 12:02:03 -080057 EVENT_START_FMT, // logFormat start event: entry includes format string, following
58 // entries contain format arguments
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -070059 EVENT_HASH, // unique HASH of log origin, originates from hash of file name
60 // and line number
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070061 EVENT_HISTOGRAM_ENTRY_TS, // single datum for timestamp histogram
62 EVENT_HISTOGRAM_FLUSH, // show histogram on log
Nicolas Rouletfe1e1442017-01-30 12:02:03 -080063 EVENT_END_FMT, // end of logFormat argument list
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070064
65 EVENT_UPPER_BOUND, // to check for invalid events
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080066};
67
Nicolas Rouletcd5dd012017-02-13 12:09:28 -080068
69// ---------------------------------------------------------------------------
70// API for handling format entry operations
71
72// a formatted entry has the following structure:
73// * START_FMT entry, containing the format string
Nicolas Rouletcd5dd012017-02-13 12:09:28 -080074// * TIMESTAMP entry
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -070075// * HASH entry
Nicolas Roulet1ca75122017-03-16 14:19:59 -070076// * author entry of the thread that generated it (optional, present in merged log)
Nicolas Rouletcd5dd012017-02-13 12:09:28 -080077// * format arg1
78// * format arg2
79// * ...
80// * END_FMT entry
81
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070082// entry representation in memory
83struct entry {
84 const uint8_t type;
85 const uint8_t length;
86 const uint8_t data[0];
87};
88
89// entry tail representation (after data)
90struct ending {
91 uint8_t length;
92 uint8_t next[0];
93};
94
95// entry iterator
96class EntryIterator {
Nicolas Rouletcd5dd012017-02-13 12:09:28 -080097public:
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070098 EntryIterator();
99 explicit EntryIterator(const uint8_t *entry);
100 EntryIterator(const EntryIterator &other);
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800101
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700102 // dereference underlying entry
103 const entry& operator*() const;
104 const entry* operator->() const;
105 // advance to next entry
106 EntryIterator& operator++(); // ++i
107 // back to previous entry
108 EntryIterator& operator--(); // --i
109 EntryIterator next() const;
110 EntryIterator prev() const;
111 bool operator!=(const EntryIterator &other) const;
112 int operator-(const EntryIterator &other) const;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800113
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700114 bool hasConsistentLength() const;
115 void copyTo(std::unique_ptr<audio_utils_fifo_writer> &dst) const;
116 void copyData(uint8_t *dst) const;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800117
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700118 template<typename T>
119 inline const T& payload() {
120 return *reinterpret_cast<const T *>(ptr + offsetof(entry, data));
121 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800122
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700123 inline operator const uint8_t*() const {
124 return ptr;
125 }
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800126
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700127private:
128 const uint8_t *ptr;
129};
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800130
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700131class AbstractEntry {
132public:
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800133
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700134 // Entry starting in the given pointer
135 explicit AbstractEntry(const uint8_t *entry);
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800136
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700137 // build concrete entry of appropriate class from pointer
138 static std::unique_ptr<AbstractEntry> buildEntry(const uint8_t *ptr);
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800139
140 // get format entry timestamp
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700141 // TODO consider changing to uint64_t
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700142 virtual int64_t timestamp() const = 0;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800143
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700144 // get format entry's unique id
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700145 virtual log_hash_t hash() const = 0;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700146
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800147 // entry's author index (-1 if none present)
148 // a Merger has a vector of Readers, author simply points to the index of the
149 // Reader that originated the entry
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700150 // TODO consider changing to uint32_t
151 virtual int author() const = 0;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800152
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700153 // copy entry, adding author before timestamp, returns iterator to end of entry
154 virtual EntryIterator copyWithAuthor(std::unique_ptr<audio_utils_fifo_writer> &dst,
155 int author) const = 0;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800156
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700157protected:
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800158 // copies ordinary entry from src to dst, and returns length of entry
159 // size_t copyEntry(audio_utils_fifo_writer *dst, const iterator &it);
160 const uint8_t *mEntry;
161};
162
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700163class FormatEntry : public AbstractEntry {
164public:
165 // explicit FormatEntry(const EntryIterator &it);
166 explicit FormatEntry(const uint8_t *ptr) : AbstractEntry(ptr) {}
167
168 // Entry's format string
169 const char* formatString() const;
170
171 // Enrty's format string length
172 size_t formatStringLength() const;
173
174 // Format arguments (excluding format string, timestamp and author)
175 EntryIterator args() const;
176
177 // get format entry timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700178 virtual int64_t timestamp() const override;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700179
180 // get format entry's unique id
181 virtual log_hash_t hash() const override;
182
183 // entry's author index (-1 if none present)
184 // a Merger has a vector of Readers, author simply points to the index of the
185 // Reader that originated the entry
186 virtual int author() const override;
187
188 // copy entry, adding author before timestamp, returns size of original entry
189 virtual EntryIterator copyWithAuthor(std::unique_ptr<audio_utils_fifo_writer> &dst,
190 int author) const override;
191
192 EntryIterator begin() const;
193};
194
195class HistogramEntry : public AbstractEntry {
196public:
197 explicit HistogramEntry(const uint8_t *ptr) : AbstractEntry(ptr) {
198 }
199
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700200 virtual int64_t timestamp() const override;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700201
202 virtual log_hash_t hash() const override;
203
204 virtual int author() const override;
205
206 virtual EntryIterator copyWithAuthor(std::unique_ptr<audio_utils_fifo_writer> &dst,
207 int author) const override;
208
209};
210
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800211// ---------------------------------------------------------------------------
212
213// representation of a single log entry in private memory
214struct Entry {
215 Entry(Event event, const void *data, size_t length)
216 : mEvent(event), mLength(length), mData(data) { }
217 /*virtual*/ ~Entry() { }
218
219 int readAt(size_t offset) const;
220
221private:
222 friend class Writer;
223 Event mEvent; // event type
Glenn Kasten535e1612016-12-05 12:19:36 -0800224 uint8_t mLength; // length of additional data, 0 <= mLength <= kMaxLength
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800225 const void *mData; // event type-specific data
Glenn Kasten535e1612016-12-05 12:19:36 -0800226 static const size_t kMaxLength = 255;
227public:
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800228 // mEvent, mLength, mData[...], duplicate mLength
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700229 static const size_t kOverhead = sizeof(entry) + sizeof(ending);
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800230 // endind length of previous entry
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700231 static const size_t kPreviousLengthOffset = - sizeof(ending) +
232 offsetof(ending, length);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800233};
234
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700235struct HistTsEntry {
236 log_hash_t hash;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700237 int64_t ts;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700238}; //TODO __attribute__((packed));
239
240struct HistTsEntryWithAuthor {
241 log_hash_t hash;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700242 int64_t ts;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700243 int author;
244}; //TODO __attribute__((packed));
245
246struct HistIntEntry {
247 log_hash_t hash;
248 int value;
249}; //TODO __attribute__((packed));
250
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800251// representation of a single log entry in shared memory
252// byte[0] mEvent
253// byte[1] mLength
254// byte[2] mData[0]
255// ...
256// byte[2+i] mData[i]
257// ...
258// byte[2+mLength-1] mData[mLength-1]
259// byte[2+mLength] duplicate copy of mLength to permit reverse scan
260// byte[3+mLength] start of next log entry
261
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800262 static void appendInt(String8 *body, const void *data);
263 static void appendFloat(String8 *body, const void *data);
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800264 static void appendPID(String8 *body, const void *data, size_t length);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800265 static void appendTimestamp(String8 *body, const void *data);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800266 static size_t fmtEntryLength(const uint8_t *data);
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700267 static String8 bufferDump(const uint8_t *buffer, size_t size);
268 static String8 bufferDump(const EntryIterator &it);
Glenn Kasten535e1612016-12-05 12:19:36 -0800269public:
270
271// Located in shared memory, must be POD.
272// Exactly one process must explicitly call the constructor or use placement new.
273// Since this is a POD, the destructor is empty and unnecessary to call it explicitly.
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800274struct Shared {
Glenn Kasten535e1612016-12-05 12:19:36 -0800275 Shared() /* mRear initialized via default constructor */ { }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800276 /*virtual*/ ~Shared() { }
277
Glenn Kasten535e1612016-12-05 12:19:36 -0800278 audio_utils_fifo_index mRear; // index one byte past the end of most recent Entry
279 char mBuffer[0]; // circular buffer for entries
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800280};
281
282public:
283
284// ---------------------------------------------------------------------------
285
286// FIXME Timeline was intended to wrap Writer and Reader, but isn't actually used yet.
287// For now it is just a namespace for sharedSize().
288class Timeline : public RefBase {
289public:
290#if 0
291 Timeline(size_t size, void *shared = NULL);
292 virtual ~Timeline();
293#endif
294
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700295 // Input parameter 'size' is the desired size of the timeline in byte units.
296 // Returns the size rounded up to a power-of-2, plus the constant size overhead for indices.
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800297 static size_t sharedSize(size_t size);
298
299#if 0
300private:
301 friend class Writer;
302 friend class Reader;
303
304 const size_t mSize; // circular buffer size in bytes, must be a power of 2
305 bool mOwn; // whether I own the memory at mShared
306 Shared* const mShared; // pointer to shared memory
307#endif
308};
309
310// ---------------------------------------------------------------------------
311
312// Writer is thread-safe with respect to Reader, but not with respect to multiple threads
313// calling Writer methods. If you need multi-thread safety for writing, use LockedWriter.
314class Writer : public RefBase {
315public:
316 Writer(); // dummy nop implementation without shared memory
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700317
318 // Input parameter 'size' is the desired size of the timeline in byte units.
319 // The size of the shared memory must be at least Timeline::sharedSize(size).
Glenn Kasten535e1612016-12-05 12:19:36 -0800320 Writer(void *shared, size_t size);
321 Writer(const sp<IMemory>& iMemory, size_t size);
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700322
Glenn Kasten535e1612016-12-05 12:19:36 -0800323 virtual ~Writer();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800324
Glenn Kasten1c446272017-04-07 09:49:07 -0700325 // FIXME needs comments, and some should be private
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800326 virtual void log(const char *string);
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800327 virtual void logf(const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800328 virtual void logvf(const char *fmt, va_list ap);
329 virtual void logTimestamp();
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700330 virtual void logTimestamp(const int64_t ts);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800331 virtual void logInteger(const int x);
332 virtual void logFloat(const float x);
333 virtual void logPID();
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700334 virtual void logFormat(const char *fmt, log_hash_t hash, ...);
335 virtual void logVFormat(const char *fmt, log_hash_t hash, va_list ap);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800336 virtual void logStart(const char *fmt);
337 virtual void logEnd();
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700338 virtual void logHash(log_hash_t hash);
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700339 virtual void logHistTS(log_hash_t hash);
340 virtual void logHistFlush(log_hash_t hash);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800341
342 virtual bool isEnabled() const;
343
344 // return value for all of these is the previous isEnabled()
345 virtual bool setEnabled(bool enabled); // but won't enable if no shared memory
346 bool enable() { return setEnabled(true); }
347 bool disable() { return setEnabled(false); }
348
349 sp<IMemory> getIMemory() const { return mIMemory; }
350
351private:
Glenn Kasten535e1612016-12-05 12:19:36 -0800352 // 0 <= length <= kMaxLength
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800353 void log(Event event, const void *data, size_t length);
354 void log(const Entry *entry, bool trusted = false);
355
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800356 Shared* const mShared; // raw pointer to shared memory
Glenn Kasten535e1612016-12-05 12:19:36 -0800357 sp<IMemory> mIMemory; // ref-counted version, initialized in constructor and then const
358 audio_utils_fifo * const mFifo; // FIFO itself,
359 // non-NULL unless constructor fails
360 audio_utils_fifo_writer * const mFifoWriter; // used to write to FIFO,
361 // non-NULL unless dummy constructor used
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800362 bool mEnabled; // whether to actually log
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800363
364 // cached pid and process name to use in %p format specifier
365 // total tag length is mPidTagSize and process name is not zero terminated
366 char *mPidTag;
367 size_t mPidTagSize;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800368};
369
370// ---------------------------------------------------------------------------
371
372// Similar to Writer, but safe for multiple threads to call concurrently
373class LockedWriter : public Writer {
374public:
375 LockedWriter();
Glenn Kasten535e1612016-12-05 12:19:36 -0800376 LockedWriter(void *shared, size_t size);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800377
378 virtual void log(const char *string);
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800379 virtual void logf(const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800380 virtual void logvf(const char *fmt, va_list ap);
381 virtual void logTimestamp();
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700382 virtual void logTimestamp(const int64_t ts);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800383 virtual void logInteger(const int x);
384 virtual void logFloat(const float x);
385 virtual void logPID();
386 virtual void logStart(const char *fmt);
387 virtual void logEnd();
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700388 virtual void logHash(log_hash_t hash);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800389
390 virtual bool isEnabled() const;
391 virtual bool setEnabled(bool enabled);
392
393private:
394 mutable Mutex mLock;
395};
396
397// ---------------------------------------------------------------------------
398
399class Reader : public RefBase {
400public:
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700401
Nicolas Roulet40a44982017-02-03 13:39:57 -0800402 // A snapshot of a readers buffer
403 class Snapshot {
404 public:
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800405 Snapshot() : mData(NULL), mLost(0) {}
Nicolas Roulet40a44982017-02-03 13:39:57 -0800406
407 Snapshot(size_t bufferSize) : mData(new uint8_t[bufferSize]) {}
408
409 ~Snapshot() { delete[] mData; }
410
411 // copy of the buffer
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800412 uint8_t *data() const { return mData; }
Nicolas Roulet40a44982017-02-03 13:39:57 -0800413
414 // amount of data lost (given by audio_utils_fifo_reader)
415 size_t lost() const { return mLost; }
416
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800417 // iterator to beginning of readable segment of snapshot
418 // data between begin and end has valid entries
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700419 EntryIterator begin() { return mBegin; }
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800420
421 // iterator to end of readable segment of snapshot
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700422 EntryIterator end() { return mEnd; }
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800423
424
Nicolas Roulet40a44982017-02-03 13:39:57 -0800425 private:
426 friend class Reader;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800427 uint8_t *mData;
428 size_t mLost;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700429 EntryIterator mBegin;
430 EntryIterator mEnd;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800431 };
432
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700433 // Input parameter 'size' is the desired size of the timeline in byte units.
434 // The size of the shared memory must be at least Timeline::sharedSize(size).
Glenn Kasten535e1612016-12-05 12:19:36 -0800435 Reader(const void *shared, size_t size);
436 Reader(const sp<IMemory>& iMemory, size_t size);
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700437
Glenn Kasten535e1612016-12-05 12:19:36 -0800438 virtual ~Reader();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800439
Nicolas Roulet40a44982017-02-03 13:39:57 -0800440 // get snapshot of readers fifo buffer, effectively consuming the buffer
441 std::unique_ptr<Snapshot> getSnapshot();
442 // dump a particular snapshot of the reader
443 void dump(int fd, size_t indent, Snapshot & snap);
444 // dump the current content of the reader's buffer
445 void dump(int fd, size_t indent = 0);
446 bool isIMemory(const sp<IMemory>& iMemory) const;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800447
448private:
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700449 static const std::set<Event> startingTypes;
450 static const std::set<Event> endingTypes;
Glenn Kasten535e1612016-12-05 12:19:36 -0800451 /*const*/ Shared* const mShared; // raw pointer to shared memory, actually const but not
452 // declared as const because audio_utils_fifo() constructor
453 sp<IMemory> mIMemory; // ref-counted version, assigned only in constructor
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700454 int mFd; // file descriptor
455 int mIndent; // indentation level
Glenn Kasten535e1612016-12-05 12:19:36 -0800456 audio_utils_fifo * const mFifo; // FIFO itself,
457 // non-NULL unless constructor fails
458 audio_utils_fifo_reader * const mFifoReader; // used to read from FIFO,
459 // non-NULL unless constructor fails
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700460
Nicolas Rouletad82aa62017-04-03 19:15:20 -0700461 std::map<std::pair<log_hash_t, int>, std::vector<int64_t>> mHists;
462
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700463 void dumpLine(const String8& timestamp, String8& body);
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800464
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700465 EntryIterator handleFormat(const FormatEntry &fmtEntry,
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800466 String8 *timestamp,
467 String8 *body);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800468 // dummy method for handling absent author entry
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700469 virtual void handleAuthor(const AbstractEntry &fmtEntry, String8 *body) {}
470
Nicolas Rouletad82aa62017-04-03 19:15:20 -0700471 static void drawHistogram(String8 *body, const std::vector<int64_t> &samples,
Nicolas Roulet4f033492017-04-03 19:17:03 -0700472 bool logScale, int indent = 0, int maxHeight = 10);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800473
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800474 // Searches for the last entry of type <type> in the range [front, back)
475 // back has to be entry-aligned. Returns nullptr if none enconuntered.
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700476 static const uint8_t *findLastEntryOfTypes(const uint8_t *front, const uint8_t *back,
477 const std::set<Event> &types);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800478
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800479 static const size_t kSquashTimestamp = 5; // squash this many or more adjacent timestamps
480};
481
Nicolas Roulet40a44982017-02-03 13:39:57 -0800482// Wrapper for a reader with a name. Contains a pointer to the reader and a pointer to the name
483class NamedReader {
484public:
485 NamedReader() { mName[0] = '\0'; } // for Vector
486 NamedReader(const sp<NBLog::Reader>& reader, const char *name) :
487 mReader(reader)
488 { strlcpy(mName, name, sizeof(mName)); }
489 ~NamedReader() { }
490 const sp<NBLog::Reader>& reader() const { return mReader; }
491 const char* name() const { return mName; }
492
493private:
494 sp<NBLog::Reader> mReader;
495 static const size_t kMaxName = 32;
496 char mName[kMaxName];
497};
498
499// ---------------------------------------------------------------------------
500
501class Merger : public RefBase {
502public:
503 Merger(const void *shared, size_t size);
504
505 virtual ~Merger() {}
506
507 void addReader(const NamedReader &reader);
508 // TODO add removeReader
509 void merge();
Glenn Kasten1c446272017-04-07 09:49:07 -0700510 // FIXME This is returning a reference to a shared variable that needs a lock
511 const std::vector<NamedReader>& getNamedReaders() const;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800512private:
513 // vector of the readers the merger is supposed to merge from.
514 // every reader reads from a writer's buffer
Glenn Kasten1c446272017-04-07 09:49:07 -0700515 // FIXME Needs to be protected by a lock
Nicolas Roulet40a44982017-02-03 13:39:57 -0800516 std::vector<NamedReader> mNamedReaders;
Glenn Kasten1c446272017-04-07 09:49:07 -0700517
518 // TODO Need comments on all of these
Nicolas Roulet40a44982017-02-03 13:39:57 -0800519 uint8_t *mBuffer;
520 Shared * const mShared;
521 std::unique_ptr<audio_utils_fifo> mFifo;
522 std::unique_ptr<audio_utils_fifo_writer> mFifoWriter;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800523};
524
525class MergeReader : public Reader {
526public:
527 MergeReader(const void *shared, size_t size, Merger &merger);
528private:
Glenn Kasten1c446272017-04-07 09:49:07 -0700529 // FIXME Needs to be protected by a lock,
530 // because even though our use of it is read-only there may be asynchronous updates
531 const std::vector<NamedReader>& mNamedReaders;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800532 // handle author entry by looking up the author's name and appending it to the body
533 // returns number of bytes read from fmtEntry
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700534 void handleAuthor(const AbstractEntry &fmtEntry, String8 *body);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800535};
536
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -0800537// MergeThread is a thread that contains a Merger. It works as a retriggerable one-shot:
538// when triggered, it awakes for a lapse of time, during which it periodically merges; if
539// retriggered, the timeout is reset.
540// The thread is triggered on AudioFlinger binder activity.
541class MergeThread : public Thread {
542public:
543 MergeThread(Merger &merger);
544 virtual ~MergeThread() override;
545
546 // Reset timeout and activate thread to merge periodically if it's idle
547 void wakeup();
548
549 // Set timeout period until the merging thread goes idle again
550 void setTimeoutUs(int time);
551
552private:
553 virtual bool threadLoop() override;
554
555 // the merger who actually does the work of merging the logs
556 Merger& mMerger;
557
558 // mutex for the condition variable
559 Mutex mMutex;
560
561 // condition variable to activate merging on timeout >= 0
562 Condition mCond;
563
564 // time left until the thread blocks again (in microseconds)
565 int mTimeoutUs;
566
567 // merging period when the thread is awake
568 static const int kThreadSleepPeriodUs = 1000000 /*1s*/;
569
570 // initial timeout value when triggered
571 static const int kThreadWakeupPeriodUs = 3000000 /*3s*/;
572};
573
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800574}; // class NBLog
575
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700576// TODO put somewhere else
577static inline int64_t get_monotonic_ns() {
578 timespec ts;
579 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
580 return (uint64_t) ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
581 }
582 return 0; // should not happen.
583}
584
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800585} // namespace android
586
587#endif // ANDROID_MEDIA_NBLOG_H