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