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