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