blob: 6b08e94159c3d6ecf7925797ee46d4c4f80a28b7 [file] [log] [blame]
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001/*
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -07002 * Copyright (C) 2017 The Android Open Source Project
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08003 *
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>
Sanna Catherine de Treville Wager9484bae2017-06-15 14:39:44 -070028#include <deque>
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070029#include <set>
Nicolas Roulet40a44982017-02-03 13:39:57 -080030#include <vector>
31
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080032namespace android {
33
Glenn Kasten4e01ef62013-07-11 14:29:59 -070034class String8;
35
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080036class NBLog {
37
38public:
39
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -070040typedef uint64_t log_hash_t;
41
Glenn Kasten1c446272017-04-07 09:49:07 -070042// FIXME Everything needed for client (writer API and registration) should be isolated
43// from the rest of the implementation.
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080044class Writer;
45class Reader;
46
47private:
48
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070049enum Event : uint8_t {
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080050 EVENT_RESERVED,
51 EVENT_STRING, // ASCII string, not NUL-terminated
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -080052 // TODO: make timestamp optional
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080053 EVENT_TIMESTAMP, // clock_gettime(CLOCK_MONOTONIC)
Nicolas Roulet40a44982017-02-03 13:39:57 -080054 EVENT_INTEGER, // integer value entry
55 EVENT_FLOAT, // floating point value entry
56 EVENT_PID, // process ID and process name
57 EVENT_AUTHOR, // author index (present in merged logs) tracks entry's original log
Nicolas Rouletfe1e1442017-01-30 12:02:03 -080058 EVENT_START_FMT, // logFormat start event: entry includes format string, following
59 // entries contain format arguments
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -070060 EVENT_HASH, // unique HASH of log origin, originates from hash of file name
61 // and line number
Nicolas Roulet537ad7d2017-03-21 16:24:30 -070062 EVENT_HISTOGRAM_ENTRY_TS, // single datum for timestamp histogram
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);
Colin Cross6f51c152017-04-28 12:46:17 -0700136 virtual ~AbstractEntry() {}
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800137
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700138 // build concrete entry of appropriate class from pointer
139 static std::unique_ptr<AbstractEntry> buildEntry(const uint8_t *ptr);
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800140
141 // get format entry timestamp
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700142 // TODO consider changing to uint64_t
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700143 virtual int64_t timestamp() const = 0;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800144
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700145 // get format entry's unique id
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700146 virtual log_hash_t hash() const = 0;
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700147
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800148 // entry's author index (-1 if none present)
149 // a Merger has a vector of Readers, author simply points to the index of the
150 // Reader that originated the entry
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700151 // TODO consider changing to uint32_t
152 virtual int author() const = 0;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800153
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700154 // copy entry, adding author before timestamp, returns iterator to end of entry
155 virtual EntryIterator copyWithAuthor(std::unique_ptr<audio_utils_fifo_writer> &dst,
156 int author) const = 0;
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800157
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700158protected:
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800159 // copies ordinary entry from src to dst, and returns length of entry
160 // size_t copyEntry(audio_utils_fifo_writer *dst, const iterator &it);
161 const uint8_t *mEntry;
162};
163
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700164class FormatEntry : public AbstractEntry {
165public:
166 // explicit FormatEntry(const EntryIterator &it);
167 explicit FormatEntry(const uint8_t *ptr) : AbstractEntry(ptr) {}
Colin Cross6f51c152017-04-28 12:46:17 -0700168 virtual ~FormatEntry() {}
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700169
Sanna Catherine de Treville Wagerdd92d7e2017-05-15 14:56:53 -0700170 EntryIterator begin() const;
171
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700172 // Entry's format string
173 const char* formatString() const;
174
175 // Enrty's format string length
176 size_t formatStringLength() const;
177
178 // Format arguments (excluding format string, timestamp and author)
179 EntryIterator args() const;
180
181 // get format entry timestamp
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700182 virtual int64_t timestamp() const override;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700183
184 // get format entry's unique id
185 virtual log_hash_t hash() const override;
186
187 // entry's author index (-1 if none present)
188 // a Merger has a vector of Readers, author simply points to the index of the
189 // Reader that originated the entry
190 virtual int author() const override;
191
192 // copy entry, adding author before timestamp, returns size of original entry
193 virtual EntryIterator copyWithAuthor(std::unique_ptr<audio_utils_fifo_writer> &dst,
194 int author) const override;
195
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700196};
197
198class HistogramEntry : public AbstractEntry {
199public:
200 explicit HistogramEntry(const uint8_t *ptr) : AbstractEntry(ptr) {
201 }
Colin Cross6f51c152017-04-28 12:46:17 -0700202 virtual ~HistogramEntry() {}
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700203
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700204 virtual int64_t timestamp() const override;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700205
206 virtual log_hash_t hash() const override;
207
208 virtual int author() const override;
209
210 virtual EntryIterator copyWithAuthor(std::unique_ptr<audio_utils_fifo_writer> &dst,
211 int author) const override;
212
213};
214
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800215// ---------------------------------------------------------------------------
216
217// representation of a single log entry in private memory
218struct Entry {
219 Entry(Event event, const void *data, size_t length)
220 : mEvent(event), mLength(length), mData(data) { }
221 /*virtual*/ ~Entry() { }
222
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700223 // used during writing to format Entry information as follows: [type][length][data ... ][length]
224 int copyEntryDataAt(size_t offset) const;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800225
226private:
227 friend class Writer;
228 Event mEvent; // event type
Glenn Kasten535e1612016-12-05 12:19:36 -0800229 uint8_t mLength; // length of additional data, 0 <= mLength <= kMaxLength
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800230 const void *mData; // event type-specific data
Glenn Kasten535e1612016-12-05 12:19:36 -0800231 static const size_t kMaxLength = 255;
232public:
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800233 // mEvent, mLength, mData[...], duplicate mLength
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700234 static const size_t kOverhead = sizeof(entry) + sizeof(ending);
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800235 // endind length of previous entry
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700236 static const size_t kPreviousLengthOffset = - sizeof(ending) +
237 offsetof(ending, length);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800238};
239
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700240struct HistTsEntry {
241 log_hash_t hash;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700242 int64_t ts;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700243}; //TODO __attribute__((packed));
244
245struct HistTsEntryWithAuthor {
246 log_hash_t hash;
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700247 int64_t ts;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700248 int author;
249}; //TODO __attribute__((packed));
250
251struct HistIntEntry {
252 log_hash_t hash;
253 int value;
254}; //TODO __attribute__((packed));
255
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800256// representation of a single log entry in shared memory
257// byte[0] mEvent
258// byte[1] mLength
259// byte[2] mData[0]
260// ...
261// byte[2+i] mData[i]
262// ...
263// byte[2+mLength-1] mData[mLength-1]
264// byte[2+mLength] duplicate copy of mLength to permit reverse scan
265// byte[3+mLength] start of next log entry
266
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800267 static void appendInt(String8 *body, const void *data);
268 static void appendFloat(String8 *body, const void *data);
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800269 static void appendPID(String8 *body, const void *data, size_t length);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800270 static void appendTimestamp(String8 *body, const void *data);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800271 static size_t fmtEntryLength(const uint8_t *data);
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700272 static String8 bufferDump(const uint8_t *buffer, size_t size);
273 static String8 bufferDump(const EntryIterator &it);
Glenn Kasten535e1612016-12-05 12:19:36 -0800274public:
275
276// Located in shared memory, must be POD.
277// Exactly one process must explicitly call the constructor or use placement new.
278// Since this is a POD, the destructor is empty and unnecessary to call it explicitly.
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800279struct Shared {
Glenn Kasten535e1612016-12-05 12:19:36 -0800280 Shared() /* mRear initialized via default constructor */ { }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800281 /*virtual*/ ~Shared() { }
282
Glenn Kasten535e1612016-12-05 12:19:36 -0800283 audio_utils_fifo_index mRear; // index one byte past the end of most recent Entry
284 char mBuffer[0]; // circular buffer for entries
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800285};
286
287public:
288
289// ---------------------------------------------------------------------------
290
291// FIXME Timeline was intended to wrap Writer and Reader, but isn't actually used yet.
292// For now it is just a namespace for sharedSize().
293class Timeline : public RefBase {
294public:
295#if 0
296 Timeline(size_t size, void *shared = NULL);
297 virtual ~Timeline();
298#endif
299
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700300 // Input parameter 'size' is the desired size of the timeline in byte units.
301 // Returns the size rounded up to a power-of-2, plus the constant size overhead for indices.
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800302 static size_t sharedSize(size_t size);
303
304#if 0
305private:
306 friend class Writer;
307 friend class Reader;
308
309 const size_t mSize; // circular buffer size in bytes, must be a power of 2
310 bool mOwn; // whether I own the memory at mShared
311 Shared* const mShared; // pointer to shared memory
312#endif
313};
314
315// ---------------------------------------------------------------------------
316
317// Writer is thread-safe with respect to Reader, but not with respect to multiple threads
318// calling Writer methods. If you need multi-thread safety for writing, use LockedWriter.
319class Writer : public RefBase {
320public:
321 Writer(); // dummy nop implementation without shared memory
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700322
323 // Input parameter 'size' is the desired size of the timeline in byte units.
324 // The size of the shared memory must be at least Timeline::sharedSize(size).
Glenn Kasten535e1612016-12-05 12:19:36 -0800325 Writer(void *shared, size_t size);
326 Writer(const sp<IMemory>& iMemory, size_t size);
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700327
Glenn Kasten535e1612016-12-05 12:19:36 -0800328 virtual ~Writer();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800329
Glenn Kasten1c446272017-04-07 09:49:07 -0700330 // FIXME needs comments, and some should be private
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800331 virtual void log(const char *string);
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800332 virtual void logf(const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800333 virtual void logvf(const char *fmt, va_list ap);
334 virtual void logTimestamp();
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700335 virtual void logTimestamp(const int64_t ts);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800336 virtual void logInteger(const int x);
337 virtual void logFloat(const float x);
338 virtual void logPID();
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700339 virtual void logFormat(const char *fmt, log_hash_t hash, ...);
340 virtual void logVFormat(const char *fmt, log_hash_t hash, va_list ap);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800341 virtual void logStart(const char *fmt);
342 virtual void logEnd();
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700343 virtual void logHash(log_hash_t hash);
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700344 virtual void logHistTS(log_hash_t hash);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800345
346 virtual bool isEnabled() const;
347
348 // return value for all of these is the previous isEnabled()
349 virtual bool setEnabled(bool enabled); // but won't enable if no shared memory
350 bool enable() { return setEnabled(true); }
351 bool disable() { return setEnabled(false); }
352
353 sp<IMemory> getIMemory() const { return mIMemory; }
354
355private:
Glenn Kasten535e1612016-12-05 12:19:36 -0800356 // 0 <= length <= kMaxLength
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700357 // writes a single Entry to the FIFO
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800358 void log(Event event, const void *data, size_t length);
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700359 // checks validity of an event before calling log above this one
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800360 void log(const Entry *entry, bool trusted = false);
361
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800362 Shared* const mShared; // raw pointer to shared memory
Glenn Kasten535e1612016-12-05 12:19:36 -0800363 sp<IMemory> mIMemory; // ref-counted version, initialized in constructor and then const
364 audio_utils_fifo * const mFifo; // FIFO itself,
365 // non-NULL unless constructor fails
366 audio_utils_fifo_writer * const mFifoWriter; // used to write to FIFO,
367 // non-NULL unless dummy constructor used
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800368 bool mEnabled; // whether to actually log
Nicolas Rouletc20cb502017-02-01 12:35:24 -0800369
370 // cached pid and process name to use in %p format specifier
371 // total tag length is mPidTagSize and process name is not zero terminated
372 char *mPidTag;
373 size_t mPidTagSize;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800374};
375
376// ---------------------------------------------------------------------------
377
378// Similar to Writer, but safe for multiple threads to call concurrently
379class LockedWriter : public Writer {
380public:
381 LockedWriter();
Glenn Kasten535e1612016-12-05 12:19:36 -0800382 LockedWriter(void *shared, size_t size);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800383
384 virtual void log(const char *string);
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800385 virtual void logf(const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800386 virtual void logvf(const char *fmt, va_list ap);
387 virtual void logTimestamp();
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700388 virtual void logTimestamp(const int64_t ts);
Nicolas Rouletfe1e1442017-01-30 12:02:03 -0800389 virtual void logInteger(const int x);
390 virtual void logFloat(const float x);
391 virtual void logPID();
392 virtual void logStart(const char *fmt);
393 virtual void logEnd();
Nicolas Rouletbd0c6b42017-03-16 13:54:23 -0700394 virtual void logHash(log_hash_t hash);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800395
396 virtual bool isEnabled() const;
397 virtual bool setEnabled(bool enabled);
398
399private:
400 mutable Mutex mLock;
401};
402
403// ---------------------------------------------------------------------------
404
405class Reader : public RefBase {
406public:
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700407
Nicolas Roulet40a44982017-02-03 13:39:57 -0800408 // A snapshot of a readers buffer
409 class Snapshot {
410 public:
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800411 Snapshot() : mData(NULL), mLost(0) {}
Nicolas Roulet40a44982017-02-03 13:39:57 -0800412
413 Snapshot(size_t bufferSize) : mData(new uint8_t[bufferSize]) {}
414
415 ~Snapshot() { delete[] mData; }
416
417 // copy of the buffer
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800418 uint8_t *data() const { return mData; }
Nicolas Roulet40a44982017-02-03 13:39:57 -0800419
420 // amount of data lost (given by audio_utils_fifo_reader)
421 size_t lost() const { return mLost; }
422
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800423 // iterator to beginning of readable segment of snapshot
424 // data between begin and end has valid entries
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700425 EntryIterator begin() { return mBegin; }
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800426
427 // iterator to end of readable segment of snapshot
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700428 EntryIterator end() { return mEnd; }
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800429
Nicolas Roulet40a44982017-02-03 13:39:57 -0800430 private:
431 friend class Reader;
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800432 uint8_t *mData;
433 size_t mLost;
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700434 EntryIterator mBegin;
435 EntryIterator mEnd;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800436 };
437
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700438 // Input parameter 'size' is the desired size of the timeline in byte units.
439 // The size of the shared memory must be at least Timeline::sharedSize(size).
Glenn Kasten535e1612016-12-05 12:19:36 -0800440 Reader(const void *shared, size_t size);
441 Reader(const sp<IMemory>& iMemory, size_t size);
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700442
Glenn Kasten535e1612016-12-05 12:19:36 -0800443 virtual ~Reader();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800444
Nicolas Roulet40a44982017-02-03 13:39:57 -0800445 // get snapshot of readers fifo buffer, effectively consuming the buffer
446 std::unique_ptr<Snapshot> getSnapshot();
447 // dump a particular snapshot of the reader
448 void dump(int fd, size_t indent, Snapshot & snap);
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700449 // dump the current content of the reader's buffer (call getSnapshot() and previous dump())
Nicolas Roulet40a44982017-02-03 13:39:57 -0800450 void dump(int fd, size_t indent = 0);
451 bool isIMemory(const sp<IMemory>& iMemory) const;
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800452
453private:
Sanna Catherine de Treville Wager9484bae2017-06-15 14:39:44 -0700454
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700455 // TODO: decide whether these belong in NBLog::Reader or in PerformanceAnalysis
Sanna Catherine de Treville Wager9484bae2017-06-15 14:39:44 -0700456 static const int kShortHistSize = 50; // number of samples in a short-term histogram
457 static const int kRecentHistsCapacity = 100; // number of short-term histograms stored in memory
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700458 static const std::set<Event> startingTypes;
459 static const std::set<Event> endingTypes;
Glenn Kasten535e1612016-12-05 12:19:36 -0800460 /*const*/ Shared* const mShared; // raw pointer to shared memory, actually const but not
461 // declared as const because audio_utils_fifo() constructor
462 sp<IMemory> mIMemory; // ref-counted version, assigned only in constructor
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700463 int mFd; // file descriptor
464 int mIndent; // indentation level
Glenn Kasten535e1612016-12-05 12:19:36 -0800465 audio_utils_fifo * const mFifo; // FIFO itself,
466 // non-NULL unless constructor fails
467 audio_utils_fifo_reader * const mFifoReader; // used to read from FIFO,
468 // non-NULL unless constructor fails
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700469
Sanna Catherine de Treville Wager9484bae2017-06-15 14:39:44 -0700470 // stores a short-term histogram of size determined by kShortHistSize
471 // TODO: unsigned, unsigned
472 using short_histogram = std::map<int, int>;
473
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700474 // each pair contains a sequence of timestamps (one histogram's worth)
475 // pair's log_hash_t is the hash of the source code location where the timestamp was taken
476 // pair's int points to the Reader that originated the entry
Nicolas Rouletad82aa62017-04-03 19:15:20 -0700477 std::map<std::pair<log_hash_t, int>, std::vector<int64_t>> mHists;
Sanna Catherine de Treville Wager9484bae2017-06-15 14:39:44 -0700478
479 // mHistsCopy stores timestamp vectors whose key is the reader thread index.
480 // TODO remove old mHists after changing the code
481 std::map<int, std::vector<int64_t>> mTimeStampSeries;
482
483 // stores fixed-size short buffer period histograms with hash and thread data
484 // TODO: Turn it into a circular buffer for better data flow
485 std::deque<std::pair<int, short_histogram>> mRecentHists;
486
Sanna Catherine de Treville Wager2d1631e2017-05-30 16:12:36 -0700487 // TODO: it might be clearer, instead of a direct map from source location to vector of
488 // timestamps, if we instead first mapped from source location to an object that
489 // represented that location. And one_of its fields would be a vector of timestamps.
490 // That would allow us to record other information about the source location beyond timestamps.
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700491 void dumpLine(const String8& timestamp, String8& body);
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800492
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700493 EntryIterator handleFormat(const FormatEntry &fmtEntry,
Nicolas Rouletcd5dd012017-02-13 12:09:28 -0800494 String8 *timestamp,
495 String8 *body);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800496 // dummy method for handling absent author entry
Colin Crossb8c35f92017-04-27 16:15:51 -0700497 virtual void handleAuthor(const AbstractEntry& /*fmtEntry*/, String8* /*body*/) {}
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700498
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800499 // Searches for the last entry of type <type> in the range [front, back)
500 // back has to be entry-aligned. Returns nullptr if none enconuntered.
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700501 static const uint8_t *findLastEntryOfTypes(const uint8_t *front, const uint8_t *back,
502 const std::set<Event> &types);
Nicolas Roulet6ea1d7e2017-02-14 16:17:31 -0800503
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800504 static const size_t kSquashTimestamp = 5; // squash this many or more adjacent timestamps
505};
506
Nicolas Roulet40a44982017-02-03 13:39:57 -0800507// Wrapper for a reader with a name. Contains a pointer to the reader and a pointer to the name
508class NamedReader {
509public:
510 NamedReader() { mName[0] = '\0'; } // for Vector
511 NamedReader(const sp<NBLog::Reader>& reader, const char *name) :
512 mReader(reader)
513 { strlcpy(mName, name, sizeof(mName)); }
514 ~NamedReader() { }
515 const sp<NBLog::Reader>& reader() const { return mReader; }
516 const char* name() const { return mName; }
517
518private:
519 sp<NBLog::Reader> mReader;
520 static const size_t kMaxName = 32;
521 char mName[kMaxName];
522};
523
524// ---------------------------------------------------------------------------
525
526class Merger : public RefBase {
527public:
528 Merger(const void *shared, size_t size);
529
530 virtual ~Merger() {}
531
532 void addReader(const NamedReader &reader);
533 // TODO add removeReader
534 void merge();
Glenn Kasten1c446272017-04-07 09:49:07 -0700535 // FIXME This is returning a reference to a shared variable that needs a lock
536 const std::vector<NamedReader>& getNamedReaders() const;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800537private:
538 // vector of the readers the merger is supposed to merge from.
539 // every reader reads from a writer's buffer
Glenn Kasten1c446272017-04-07 09:49:07 -0700540 // FIXME Needs to be protected by a lock
Nicolas Roulet40a44982017-02-03 13:39:57 -0800541 std::vector<NamedReader> mNamedReaders;
Glenn Kasten1c446272017-04-07 09:49:07 -0700542
543 // TODO Need comments on all of these
Nicolas Roulet40a44982017-02-03 13:39:57 -0800544 Shared * const mShared;
545 std::unique_ptr<audio_utils_fifo> mFifo;
546 std::unique_ptr<audio_utils_fifo_writer> mFifoWriter;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800547};
548
549class MergeReader : public Reader {
550public:
551 MergeReader(const void *shared, size_t size, Merger &merger);
552private:
Glenn Kasten1c446272017-04-07 09:49:07 -0700553 // FIXME Needs to be protected by a lock,
554 // because even though our use of it is read-only there may be asynchronous updates
555 const std::vector<NamedReader>& mNamedReaders;
Nicolas Roulet40a44982017-02-03 13:39:57 -0800556 // handle author entry by looking up the author's name and appending it to the body
557 // returns number of bytes read from fmtEntry
Nicolas Roulet537ad7d2017-03-21 16:24:30 -0700558 void handleAuthor(const AbstractEntry &fmtEntry, String8 *body);
Nicolas Roulet40a44982017-02-03 13:39:57 -0800559};
560
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -0800561// MergeThread is a thread that contains a Merger. It works as a retriggerable one-shot:
562// when triggered, it awakes for a lapse of time, during which it periodically merges; if
563// retriggered, the timeout is reset.
564// The thread is triggered on AudioFlinger binder activity.
565class MergeThread : public Thread {
566public:
567 MergeThread(Merger &merger);
568 virtual ~MergeThread() override;
569
570 // Reset timeout and activate thread to merge periodically if it's idle
571 void wakeup();
572
573 // Set timeout period until the merging thread goes idle again
574 void setTimeoutUs(int time);
575
576private:
577 virtual bool threadLoop() override;
578
579 // the merger who actually does the work of merging the logs
580 Merger& mMerger;
581
582 // mutex for the condition variable
583 Mutex mMutex;
584
585 // condition variable to activate merging on timeout >= 0
586 Condition mCond;
587
588 // time left until the thread blocks again (in microseconds)
589 int mTimeoutUs;
590
591 // merging period when the thread is awake
592 static const int kThreadSleepPeriodUs = 1000000 /*1s*/;
593
594 // initial timeout value when triggered
595 static const int kThreadWakeupPeriodUs = 3000000 /*3s*/;
596};
597
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800598}; // class NBLog
599
Nicolas Rouletf42f1562017-03-30 19:16:22 -0700600// TODO put somewhere else
601static inline int64_t get_monotonic_ns() {
602 timespec ts;
603 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
604 return (uint64_t) ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
605 }
606 return 0; // should not happen.
607}
608
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800609} // namespace android
610
611#endif // ANDROID_MEDIA_NBLOG_H