Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 1 | /* |
| 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 Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 23 | #include <audio_utils/fifo.h> |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 24 | #include <utils/Mutex.h> |
| 25 | #include <utils/threads.h> |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 26 | |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 27 | #include <set> |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 30 | namespace android { |
| 31 | |
Glenn Kasten | 4e01ef6 | 2013-07-11 14:29:59 -0700 | [diff] [blame] | 32 | class String8; |
| 33 | |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 34 | class NBLog { |
| 35 | |
| 36 | public: |
| 37 | |
Nicolas Roulet | bd0c6b4 | 2017-03-16 13:54:23 -0700 | [diff] [blame] | 38 | typedef uint64_t log_hash_t; |
| 39 | |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 40 | class Writer; |
| 41 | class Reader; |
| 42 | |
| 43 | private: |
| 44 | |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 45 | enum Event : uint8_t { |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 46 | EVENT_RESERVED, |
| 47 | EVENT_STRING, // ASCII string, not NUL-terminated |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 48 | // TODO: make timestamp optional |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 49 | EVENT_TIMESTAMP, // clock_gettime(CLOCK_MONOTONIC) |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 50 | EVENT_INTEGER, // integer value entry |
| 51 | EVENT_FLOAT, // floating point value entry |
| 52 | EVENT_PID, // process ID and process name |
| 53 | EVENT_AUTHOR, // author index (present in merged logs) tracks entry's original log |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 54 | EVENT_START_FMT, // logFormat start event: entry includes format string, following |
| 55 | // entries contain format arguments |
Nicolas Roulet | bd0c6b4 | 2017-03-16 13:54:23 -0700 | [diff] [blame] | 56 | EVENT_HASH, // unique HASH of log origin, originates from hash of file name |
| 57 | // and line number |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 58 | EVENT_HISTOGRAM_ENTRY_TS, // single datum for timestamp histogram |
| 59 | EVENT_HISTOGRAM_FLUSH, // show histogram on log |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 60 | EVENT_END_FMT, // end of logFormat argument list |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 61 | |
| 62 | EVENT_UPPER_BOUND, // to check for invalid events |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 65 | |
| 66 | // --------------------------------------------------------------------------- |
| 67 | // API for handling format entry operations |
| 68 | |
| 69 | // a formatted entry has the following structure: |
| 70 | // * START_FMT entry, containing the format string |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 71 | // * TIMESTAMP entry |
Nicolas Roulet | bd0c6b4 | 2017-03-16 13:54:23 -0700 | [diff] [blame] | 72 | // * HASH entry |
Nicolas Roulet | 1ca7512 | 2017-03-16 14:19:59 -0700 | [diff] [blame] | 73 | // * author entry of the thread that generated it (optional, present in merged log) |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 74 | // * format arg1 |
| 75 | // * format arg2 |
| 76 | // * ... |
| 77 | // * END_FMT entry |
| 78 | |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 79 | // entry representation in memory |
| 80 | struct entry { |
| 81 | const uint8_t type; |
| 82 | const uint8_t length; |
| 83 | const uint8_t data[0]; |
| 84 | }; |
| 85 | |
| 86 | // entry tail representation (after data) |
| 87 | struct ending { |
| 88 | uint8_t length; |
| 89 | uint8_t next[0]; |
| 90 | }; |
| 91 | |
| 92 | // entry iterator |
| 93 | class EntryIterator { |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 94 | public: |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 95 | EntryIterator(); |
| 96 | explicit EntryIterator(const uint8_t *entry); |
| 97 | EntryIterator(const EntryIterator &other); |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 98 | |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 99 | // dereference underlying entry |
| 100 | const entry& operator*() const; |
| 101 | const entry* operator->() const; |
| 102 | // advance to next entry |
| 103 | EntryIterator& operator++(); // ++i |
| 104 | // back to previous entry |
| 105 | EntryIterator& operator--(); // --i |
| 106 | EntryIterator next() const; |
| 107 | EntryIterator prev() const; |
| 108 | bool operator!=(const EntryIterator &other) const; |
| 109 | int operator-(const EntryIterator &other) const; |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 110 | |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 111 | bool hasConsistentLength() const; |
| 112 | void copyTo(std::unique_ptr<audio_utils_fifo_writer> &dst) const; |
| 113 | void copyData(uint8_t *dst) const; |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 114 | |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 115 | template<typename T> |
| 116 | inline const T& payload() { |
| 117 | return *reinterpret_cast<const T *>(ptr + offsetof(entry, data)); |
| 118 | } |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 119 | |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 120 | inline operator const uint8_t*() const { |
| 121 | return ptr; |
| 122 | } |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 123 | |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 124 | private: |
| 125 | const uint8_t *ptr; |
| 126 | }; |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 127 | |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 128 | class AbstractEntry { |
| 129 | public: |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 130 | |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 131 | // Entry starting in the given pointer |
| 132 | explicit AbstractEntry(const uint8_t *entry); |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 133 | |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 134 | // build concrete entry of appropriate class from pointer |
| 135 | static std::unique_ptr<AbstractEntry> buildEntry(const uint8_t *ptr); |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 136 | |
| 137 | // get format entry timestamp |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 138 | // TODO consider changing to uint64_t |
Nicolas Roulet | f42f156 | 2017-03-30 19:16:22 -0700 | [diff] [blame^] | 139 | virtual int64_t timestamp() const = 0; |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 140 | |
Nicolas Roulet | bd0c6b4 | 2017-03-16 13:54:23 -0700 | [diff] [blame] | 141 | // get format entry's unique id |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 142 | virtual log_hash_t hash() const = 0; |
Nicolas Roulet | bd0c6b4 | 2017-03-16 13:54:23 -0700 | [diff] [blame] | 143 | |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 144 | // entry's author index (-1 if none present) |
| 145 | // a Merger has a vector of Readers, author simply points to the index of the |
| 146 | // Reader that originated the entry |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 147 | // TODO consider changing to uint32_t |
| 148 | virtual int author() const = 0; |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 149 | |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 150 | // copy entry, adding author before timestamp, returns iterator to end of entry |
| 151 | virtual EntryIterator copyWithAuthor(std::unique_ptr<audio_utils_fifo_writer> &dst, |
| 152 | int author) const = 0; |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 153 | |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 154 | protected: |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 155 | // copies ordinary entry from src to dst, and returns length of entry |
| 156 | // size_t copyEntry(audio_utils_fifo_writer *dst, const iterator &it); |
| 157 | const uint8_t *mEntry; |
| 158 | }; |
| 159 | |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 160 | class FormatEntry : public AbstractEntry { |
| 161 | public: |
| 162 | // explicit FormatEntry(const EntryIterator &it); |
| 163 | explicit FormatEntry(const uint8_t *ptr) : AbstractEntry(ptr) {} |
| 164 | |
| 165 | // Entry's format string |
| 166 | const char* formatString() const; |
| 167 | |
| 168 | // Enrty's format string length |
| 169 | size_t formatStringLength() const; |
| 170 | |
| 171 | // Format arguments (excluding format string, timestamp and author) |
| 172 | EntryIterator args() const; |
| 173 | |
| 174 | // get format entry timestamp |
Nicolas Roulet | f42f156 | 2017-03-30 19:16:22 -0700 | [diff] [blame^] | 175 | virtual int64_t timestamp() const override; |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 176 | |
| 177 | // get format entry's unique id |
| 178 | virtual log_hash_t hash() const override; |
| 179 | |
| 180 | // entry's author index (-1 if none present) |
| 181 | // a Merger has a vector of Readers, author simply points to the index of the |
| 182 | // Reader that originated the entry |
| 183 | virtual int author() const override; |
| 184 | |
| 185 | // copy entry, adding author before timestamp, returns size of original entry |
| 186 | virtual EntryIterator copyWithAuthor(std::unique_ptr<audio_utils_fifo_writer> &dst, |
| 187 | int author) const override; |
| 188 | |
| 189 | EntryIterator begin() const; |
| 190 | }; |
| 191 | |
| 192 | class HistogramEntry : public AbstractEntry { |
| 193 | public: |
| 194 | explicit HistogramEntry(const uint8_t *ptr) : AbstractEntry(ptr) { |
| 195 | } |
| 196 | |
Nicolas Roulet | f42f156 | 2017-03-30 19:16:22 -0700 | [diff] [blame^] | 197 | virtual int64_t timestamp() const override; |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 198 | |
| 199 | virtual log_hash_t hash() const override; |
| 200 | |
| 201 | virtual int author() const override; |
| 202 | |
| 203 | virtual EntryIterator copyWithAuthor(std::unique_ptr<audio_utils_fifo_writer> &dst, |
| 204 | int author) const override; |
| 205 | |
| 206 | }; |
| 207 | |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 208 | // --------------------------------------------------------------------------- |
| 209 | |
| 210 | // representation of a single log entry in private memory |
| 211 | struct Entry { |
| 212 | Entry(Event event, const void *data, size_t length) |
| 213 | : mEvent(event), mLength(length), mData(data) { } |
| 214 | /*virtual*/ ~Entry() { } |
| 215 | |
| 216 | int readAt(size_t offset) const; |
| 217 | |
| 218 | private: |
| 219 | friend class Writer; |
| 220 | Event mEvent; // event type |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 221 | uint8_t mLength; // length of additional data, 0 <= mLength <= kMaxLength |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 222 | const void *mData; // event type-specific data |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 223 | static const size_t kMaxLength = 255; |
| 224 | public: |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 225 | // mEvent, mLength, mData[...], duplicate mLength |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 226 | static const size_t kOverhead = sizeof(entry) + sizeof(ending); |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 227 | // endind length of previous entry |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 228 | static const size_t kPreviousLengthOffset = - sizeof(ending) + |
| 229 | offsetof(ending, length); |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 230 | }; |
| 231 | |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 232 | struct HistTsEntry { |
| 233 | log_hash_t hash; |
Nicolas Roulet | f42f156 | 2017-03-30 19:16:22 -0700 | [diff] [blame^] | 234 | int64_t ts; |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 235 | }; //TODO __attribute__((packed)); |
| 236 | |
| 237 | struct HistTsEntryWithAuthor { |
| 238 | log_hash_t hash; |
Nicolas Roulet | f42f156 | 2017-03-30 19:16:22 -0700 | [diff] [blame^] | 239 | int64_t ts; |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 240 | int author; |
| 241 | }; //TODO __attribute__((packed)); |
| 242 | |
| 243 | struct HistIntEntry { |
| 244 | log_hash_t hash; |
| 245 | int value; |
| 246 | }; //TODO __attribute__((packed)); |
| 247 | |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 248 | // representation of a single log entry in shared memory |
| 249 | // byte[0] mEvent |
| 250 | // byte[1] mLength |
| 251 | // byte[2] mData[0] |
| 252 | // ... |
| 253 | // byte[2+i] mData[i] |
| 254 | // ... |
| 255 | // byte[2+mLength-1] mData[mLength-1] |
| 256 | // byte[2+mLength] duplicate copy of mLength to permit reverse scan |
| 257 | // byte[3+mLength] start of next log entry |
| 258 | |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 259 | static void appendInt(String8 *body, const void *data); |
| 260 | static void appendFloat(String8 *body, const void *data); |
Nicolas Roulet | c20cb50 | 2017-02-01 12:35:24 -0800 | [diff] [blame] | 261 | static void appendPID(String8 *body, const void *data, size_t length); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 262 | static void appendTimestamp(String8 *body, const void *data); |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 263 | static size_t fmtEntryLength(const uint8_t *data); |
Nicolas Roulet | f42f156 | 2017-03-30 19:16:22 -0700 | [diff] [blame^] | 264 | static String8 bufferDump(const uint8_t *buffer, size_t size); |
| 265 | static String8 bufferDump(const EntryIterator &it); |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 266 | public: |
| 267 | |
| 268 | // Located in shared memory, must be POD. |
| 269 | // Exactly one process must explicitly call the constructor or use placement new. |
| 270 | // Since this is a POD, the destructor is empty and unnecessary to call it explicitly. |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 271 | struct Shared { |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 272 | Shared() /* mRear initialized via default constructor */ { } |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 273 | /*virtual*/ ~Shared() { } |
| 274 | |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 275 | audio_utils_fifo_index mRear; // index one byte past the end of most recent Entry |
| 276 | char mBuffer[0]; // circular buffer for entries |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 277 | }; |
| 278 | |
| 279 | public: |
| 280 | |
| 281 | // --------------------------------------------------------------------------- |
| 282 | |
| 283 | // FIXME Timeline was intended to wrap Writer and Reader, but isn't actually used yet. |
| 284 | // For now it is just a namespace for sharedSize(). |
| 285 | class Timeline : public RefBase { |
| 286 | public: |
| 287 | #if 0 |
| 288 | Timeline(size_t size, void *shared = NULL); |
| 289 | virtual ~Timeline(); |
| 290 | #endif |
| 291 | |
Glenn Kasten | fb1fdc9 | 2013-07-10 17:03:19 -0700 | [diff] [blame] | 292 | // Input parameter 'size' is the desired size of the timeline in byte units. |
| 293 | // Returns the size rounded up to a power-of-2, plus the constant size overhead for indices. |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 294 | static size_t sharedSize(size_t size); |
| 295 | |
| 296 | #if 0 |
| 297 | private: |
| 298 | friend class Writer; |
| 299 | friend class Reader; |
| 300 | |
| 301 | const size_t mSize; // circular buffer size in bytes, must be a power of 2 |
| 302 | bool mOwn; // whether I own the memory at mShared |
| 303 | Shared* const mShared; // pointer to shared memory |
| 304 | #endif |
| 305 | }; |
| 306 | |
| 307 | // --------------------------------------------------------------------------- |
| 308 | |
| 309 | // Writer is thread-safe with respect to Reader, but not with respect to multiple threads |
| 310 | // calling Writer methods. If you need multi-thread safety for writing, use LockedWriter. |
| 311 | class Writer : public RefBase { |
| 312 | public: |
| 313 | Writer(); // dummy nop implementation without shared memory |
Glenn Kasten | fb1fdc9 | 2013-07-10 17:03:19 -0700 | [diff] [blame] | 314 | |
| 315 | // Input parameter 'size' is the desired size of the timeline in byte units. |
| 316 | // The size of the shared memory must be at least Timeline::sharedSize(size). |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 317 | Writer(void *shared, size_t size); |
| 318 | Writer(const sp<IMemory>& iMemory, size_t size); |
Glenn Kasten | fb1fdc9 | 2013-07-10 17:03:19 -0700 | [diff] [blame] | 319 | |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 320 | virtual ~Writer(); |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 321 | |
| 322 | virtual void log(const char *string); |
Glenn Kasten | ab7d72f | 2013-02-27 09:05:28 -0800 | [diff] [blame] | 323 | virtual void logf(const char *fmt, ...) __attribute__ ((format (printf, 2, 3))); |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 324 | virtual void logvf(const char *fmt, va_list ap); |
| 325 | virtual void logTimestamp(); |
Nicolas Roulet | f42f156 | 2017-03-30 19:16:22 -0700 | [diff] [blame^] | 326 | virtual void logTimestamp(const int64_t ts); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 327 | virtual void logInteger(const int x); |
| 328 | virtual void logFloat(const float x); |
| 329 | virtual void logPID(); |
Nicolas Roulet | bd0c6b4 | 2017-03-16 13:54:23 -0700 | [diff] [blame] | 330 | virtual void logFormat(const char *fmt, log_hash_t hash, ...); |
| 331 | virtual void logVFormat(const char *fmt, log_hash_t hash, va_list ap); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 332 | virtual void logStart(const char *fmt); |
| 333 | virtual void logEnd(); |
Nicolas Roulet | bd0c6b4 | 2017-03-16 13:54:23 -0700 | [diff] [blame] | 334 | virtual void logHash(log_hash_t hash); |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 335 | virtual void logHistTS(log_hash_t hash); |
| 336 | virtual void logHistFlush(log_hash_t hash); |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 337 | |
| 338 | virtual bool isEnabled() const; |
| 339 | |
| 340 | // return value for all of these is the previous isEnabled() |
| 341 | virtual bool setEnabled(bool enabled); // but won't enable if no shared memory |
| 342 | bool enable() { return setEnabled(true); } |
| 343 | bool disable() { return setEnabled(false); } |
| 344 | |
| 345 | sp<IMemory> getIMemory() const { return mIMemory; } |
| 346 | |
| 347 | private: |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 348 | // 0 <= length <= kMaxLength |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 349 | void log(Event event, const void *data, size_t length); |
| 350 | void log(const Entry *entry, bool trusted = false); |
| 351 | |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 352 | Shared* const mShared; // raw pointer to shared memory |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 353 | sp<IMemory> mIMemory; // ref-counted version, initialized in constructor and then const |
| 354 | audio_utils_fifo * const mFifo; // FIFO itself, |
| 355 | // non-NULL unless constructor fails |
| 356 | audio_utils_fifo_writer * const mFifoWriter; // used to write to FIFO, |
| 357 | // non-NULL unless dummy constructor used |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 358 | bool mEnabled; // whether to actually log |
Nicolas Roulet | c20cb50 | 2017-02-01 12:35:24 -0800 | [diff] [blame] | 359 | |
| 360 | // cached pid and process name to use in %p format specifier |
| 361 | // total tag length is mPidTagSize and process name is not zero terminated |
| 362 | char *mPidTag; |
| 363 | size_t mPidTagSize; |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 364 | }; |
| 365 | |
| 366 | // --------------------------------------------------------------------------- |
| 367 | |
| 368 | // Similar to Writer, but safe for multiple threads to call concurrently |
| 369 | class LockedWriter : public Writer { |
| 370 | public: |
| 371 | LockedWriter(); |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 372 | LockedWriter(void *shared, size_t size); |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 373 | |
| 374 | virtual void log(const char *string); |
Glenn Kasten | ab7d72f | 2013-02-27 09:05:28 -0800 | [diff] [blame] | 375 | virtual void logf(const char *fmt, ...) __attribute__ ((format (printf, 2, 3))); |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 376 | virtual void logvf(const char *fmt, va_list ap); |
| 377 | virtual void logTimestamp(); |
Nicolas Roulet | f42f156 | 2017-03-30 19:16:22 -0700 | [diff] [blame^] | 378 | virtual void logTimestamp(const int64_t ts); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 379 | virtual void logInteger(const int x); |
| 380 | virtual void logFloat(const float x); |
| 381 | virtual void logPID(); |
| 382 | virtual void logStart(const char *fmt); |
| 383 | virtual void logEnd(); |
Nicolas Roulet | bd0c6b4 | 2017-03-16 13:54:23 -0700 | [diff] [blame] | 384 | virtual void logHash(log_hash_t hash); |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 385 | |
| 386 | virtual bool isEnabled() const; |
| 387 | virtual bool setEnabled(bool enabled); |
| 388 | |
| 389 | private: |
| 390 | mutable Mutex mLock; |
| 391 | }; |
| 392 | |
| 393 | // --------------------------------------------------------------------------- |
| 394 | |
| 395 | class Reader : public RefBase { |
| 396 | public: |
Glenn Kasten | fb1fdc9 | 2013-07-10 17:03:19 -0700 | [diff] [blame] | 397 | |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 398 | // A snapshot of a readers buffer |
| 399 | class Snapshot { |
| 400 | public: |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 401 | Snapshot() : mData(NULL), mLost(0) {} |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 402 | |
| 403 | Snapshot(size_t bufferSize) : mData(new uint8_t[bufferSize]) {} |
| 404 | |
| 405 | ~Snapshot() { delete[] mData; } |
| 406 | |
| 407 | // copy of the buffer |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 408 | uint8_t *data() const { return mData; } |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 409 | |
| 410 | // amount of data lost (given by audio_utils_fifo_reader) |
| 411 | size_t lost() const { return mLost; } |
| 412 | |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 413 | // iterator to beginning of readable segment of snapshot |
| 414 | // data between begin and end has valid entries |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 415 | EntryIterator begin() { return mBegin; } |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 416 | |
| 417 | // iterator to end of readable segment of snapshot |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 418 | EntryIterator end() { return mEnd; } |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 419 | |
| 420 | |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 421 | private: |
| 422 | friend class Reader; |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 423 | uint8_t *mData; |
| 424 | size_t mLost; |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 425 | EntryIterator mBegin; |
| 426 | EntryIterator mEnd; |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 427 | }; |
| 428 | |
Glenn Kasten | fb1fdc9 | 2013-07-10 17:03:19 -0700 | [diff] [blame] | 429 | // Input parameter 'size' is the desired size of the timeline in byte units. |
| 430 | // The size of the shared memory must be at least Timeline::sharedSize(size). |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 431 | Reader(const void *shared, size_t size); |
| 432 | Reader(const sp<IMemory>& iMemory, size_t size); |
Glenn Kasten | fb1fdc9 | 2013-07-10 17:03:19 -0700 | [diff] [blame] | 433 | |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 434 | virtual ~Reader(); |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 435 | |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 436 | // get snapshot of readers fifo buffer, effectively consuming the buffer |
| 437 | std::unique_ptr<Snapshot> getSnapshot(); |
| 438 | // dump a particular snapshot of the reader |
| 439 | void dump(int fd, size_t indent, Snapshot & snap); |
| 440 | // dump the current content of the reader's buffer |
| 441 | void dump(int fd, size_t indent = 0); |
| 442 | bool isIMemory(const sp<IMemory>& iMemory) const; |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 443 | |
| 444 | private: |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 445 | static const std::set<Event> startingTypes; |
| 446 | static const std::set<Event> endingTypes; |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 447 | /*const*/ Shared* const mShared; // raw pointer to shared memory, actually const but not |
| 448 | // declared as const because audio_utils_fifo() constructor |
| 449 | sp<IMemory> mIMemory; // ref-counted version, assigned only in constructor |
Glenn Kasten | 4e01ef6 | 2013-07-11 14:29:59 -0700 | [diff] [blame] | 450 | int mFd; // file descriptor |
| 451 | int mIndent; // indentation level |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 452 | audio_utils_fifo * const mFifo; // FIFO itself, |
| 453 | // non-NULL unless constructor fails |
| 454 | audio_utils_fifo_reader * const mFifoReader; // used to read from FIFO, |
| 455 | // non-NULL unless constructor fails |
Glenn Kasten | 4e01ef6 | 2013-07-11 14:29:59 -0700 | [diff] [blame] | 456 | |
| 457 | void dumpLine(const String8& timestamp, String8& body); |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 458 | |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 459 | EntryIterator handleFormat(const FormatEntry &fmtEntry, |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 460 | String8 *timestamp, |
| 461 | String8 *body); |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 462 | // dummy method for handling absent author entry |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 463 | virtual void handleAuthor(const AbstractEntry &fmtEntry, String8 *body) {} |
| 464 | |
| 465 | static void drawHistogram(String8 *body, const std::vector<int> &samples, int maxHeight = 10); |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 466 | |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 467 | // Searches for the last entry of type <type> in the range [front, back) |
| 468 | // back has to be entry-aligned. Returns nullptr if none enconuntered. |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 469 | static const uint8_t *findLastEntryOfTypes(const uint8_t *front, const uint8_t *back, |
| 470 | const std::set<Event> &types); |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 471 | |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 472 | static const size_t kSquashTimestamp = 5; // squash this many or more adjacent timestamps |
| 473 | }; |
| 474 | |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 475 | // Wrapper for a reader with a name. Contains a pointer to the reader and a pointer to the name |
| 476 | class NamedReader { |
| 477 | public: |
| 478 | NamedReader() { mName[0] = '\0'; } // for Vector |
| 479 | NamedReader(const sp<NBLog::Reader>& reader, const char *name) : |
| 480 | mReader(reader) |
| 481 | { strlcpy(mName, name, sizeof(mName)); } |
| 482 | ~NamedReader() { } |
| 483 | const sp<NBLog::Reader>& reader() const { return mReader; } |
| 484 | const char* name() const { return mName; } |
| 485 | |
| 486 | private: |
| 487 | sp<NBLog::Reader> mReader; |
| 488 | static const size_t kMaxName = 32; |
| 489 | char mName[kMaxName]; |
| 490 | }; |
| 491 | |
| 492 | // --------------------------------------------------------------------------- |
| 493 | |
| 494 | class Merger : public RefBase { |
| 495 | public: |
| 496 | Merger(const void *shared, size_t size); |
| 497 | |
| 498 | virtual ~Merger() {} |
| 499 | |
| 500 | void addReader(const NamedReader &reader); |
| 501 | // TODO add removeReader |
| 502 | void merge(); |
| 503 | const std::vector<NamedReader> *getNamedReaders() const; |
| 504 | private: |
| 505 | // vector of the readers the merger is supposed to merge from. |
| 506 | // every reader reads from a writer's buffer |
| 507 | std::vector<NamedReader> mNamedReaders; |
| 508 | uint8_t *mBuffer; |
| 509 | Shared * const mShared; |
| 510 | std::unique_ptr<audio_utils_fifo> mFifo; |
| 511 | std::unique_ptr<audio_utils_fifo_writer> mFifoWriter; |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 512 | }; |
| 513 | |
| 514 | class MergeReader : public Reader { |
| 515 | public: |
| 516 | MergeReader(const void *shared, size_t size, Merger &merger); |
| 517 | private: |
| 518 | const std::vector<NamedReader> *mNamedReaders; |
| 519 | // handle author entry by looking up the author's name and appending it to the body |
| 520 | // returns number of bytes read from fmtEntry |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 521 | void handleAuthor(const AbstractEntry &fmtEntry, String8 *body); |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 522 | }; |
| 523 | |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 524 | // MergeThread is a thread that contains a Merger. It works as a retriggerable one-shot: |
| 525 | // when triggered, it awakes for a lapse of time, during which it periodically merges; if |
| 526 | // retriggered, the timeout is reset. |
| 527 | // The thread is triggered on AudioFlinger binder activity. |
| 528 | class MergeThread : public Thread { |
| 529 | public: |
| 530 | MergeThread(Merger &merger); |
| 531 | virtual ~MergeThread() override; |
| 532 | |
| 533 | // Reset timeout and activate thread to merge periodically if it's idle |
| 534 | void wakeup(); |
| 535 | |
| 536 | // Set timeout period until the merging thread goes idle again |
| 537 | void setTimeoutUs(int time); |
| 538 | |
| 539 | private: |
| 540 | virtual bool threadLoop() override; |
| 541 | |
| 542 | // the merger who actually does the work of merging the logs |
| 543 | Merger& mMerger; |
| 544 | |
| 545 | // mutex for the condition variable |
| 546 | Mutex mMutex; |
| 547 | |
| 548 | // condition variable to activate merging on timeout >= 0 |
| 549 | Condition mCond; |
| 550 | |
| 551 | // time left until the thread blocks again (in microseconds) |
| 552 | int mTimeoutUs; |
| 553 | |
| 554 | // merging period when the thread is awake |
| 555 | static const int kThreadSleepPeriodUs = 1000000 /*1s*/; |
| 556 | |
| 557 | // initial timeout value when triggered |
| 558 | static const int kThreadWakeupPeriodUs = 3000000 /*3s*/; |
| 559 | }; |
| 560 | |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 561 | }; // class NBLog |
| 562 | |
Nicolas Roulet | f42f156 | 2017-03-30 19:16:22 -0700 | [diff] [blame^] | 563 | // TODO put somewhere else |
| 564 | static inline int64_t get_monotonic_ns() { |
| 565 | timespec ts; |
| 566 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
| 567 | return (uint64_t) ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec; |
| 568 | } |
| 569 | return 0; // should not happen. |
| 570 | } |
| 571 | |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 572 | } // namespace android |
| 573 | |
| 574 | #endif // ANDROID_MEDIA_NBLOG_H |