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. |
Sanna Catherine de Treville Wager | 1431644 | 2017-08-11 10:45:29 -0700 | [diff] [blame] | 15 | * |
| 16 | * |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #define LOG_TAG "NBLog" |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 20 | |
Sanna Catherine de Treville Wager | 697a8a5 | 2017-06-01 09:49:05 -0700 | [diff] [blame] | 21 | #include <algorithm> |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 22 | #include <climits> |
Sanna Catherine de Treville Wager | cced674 | 2017-05-10 14:42:54 -0700 | [diff] [blame] | 23 | #include <math.h> |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame^] | 24 | #include <memory> |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 25 | #include <unordered_set> |
Sanna Catherine de Treville Wager | 697a8a5 | 2017-06-01 09:49:05 -0700 | [diff] [blame] | 26 | #include <vector> |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 27 | #include <stdarg.h> |
| 28 | #include <stdint.h> |
| 29 | #include <stdio.h> |
| 30 | #include <string.h> |
Nicolas Roulet | c20cb50 | 2017-02-01 12:35:24 -0800 | [diff] [blame] | 31 | #include <sys/prctl.h> |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 32 | #include <time.h> |
| 33 | #include <new> |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 34 | #include <audio_utils/roundup.h> |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame^] | 35 | #include <json/json.h> |
Glenn Kasten | 8589ce7 | 2017-09-08 17:03:42 -0700 | [diff] [blame] | 36 | #include <media/nblog/NBLog.h> |
| 37 | #include <media/nblog/PerformanceAnalysis.h> |
| 38 | #include <media/nblog/ReportPerformance.h> |
Sanna Catherine de Treville Wager | e486526 | 2017-07-14 16:24:15 -0700 | [diff] [blame] | 39 | #include <utils/CallStack.h> |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 40 | #include <utils/Log.h> |
Glenn Kasten | 4e01ef6 | 2013-07-11 14:29:59 -0700 | [diff] [blame] | 41 | #include <utils/String8.h> |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame^] | 42 | #include <utils/Timers.h> |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 43 | |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 44 | #include <queue> |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 45 | #include <utility> |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 46 | |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 47 | namespace android { |
| 48 | |
Sanna Catherine de Treville Wager | 2d1631e | 2017-05-30 16:12:36 -0700 | [diff] [blame] | 49 | int NBLog::Entry::copyEntryDataAt(size_t offset) const |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 50 | { |
Sanna Catherine de Treville Wager | 2d1631e | 2017-05-30 16:12:36 -0700 | [diff] [blame] | 51 | // FIXME This is too slow |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 52 | if (offset == 0) |
| 53 | return mEvent; |
| 54 | else if (offset == 1) |
| 55 | return mLength; |
| 56 | else if (offset < (size_t) (mLength + 2)) |
| 57 | return ((char *) mData)[offset - 2]; |
| 58 | else if (offset == (size_t) (mLength + 2)) |
| 59 | return mLength; |
| 60 | else |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | // --------------------------------------------------------------------------- |
| 65 | |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 66 | /*static*/ |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 67 | std::unique_ptr<NBLog::AbstractEntry> NBLog::AbstractEntry::buildEntry(const uint8_t *ptr) |
| 68 | { |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 69 | if (ptr == nullptr) { |
| 70 | return nullptr; |
| 71 | } |
Sanna Catherine de Treville Wager | cced674 | 2017-05-10 14:42:54 -0700 | [diff] [blame] | 72 | const uint8_t type = EntryIterator(ptr)->type; |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 73 | switch (type) { |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 74 | case EVENT_FMT_START: |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 75 | return std::make_unique<FormatEntry>(FormatEntry(ptr)); |
Sanna Catherine de Treville Wager | a8a8a47 | 2017-07-11 09:41:25 -0700 | [diff] [blame] | 76 | case EVENT_AUDIO_STATE: |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 77 | case EVENT_HISTOGRAM_ENTRY_TS: |
| 78 | return std::make_unique<HistogramEntry>(HistogramEntry(ptr)); |
| 79 | default: |
| 80 | ALOGW("Tried to create AbstractEntry of type %d", type); |
| 81 | return nullptr; |
| 82 | } |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 85 | NBLog::AbstractEntry::AbstractEntry(const uint8_t *entry) : mEntry(entry) |
| 86 | { |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // --------------------------------------------------------------------------- |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 90 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 91 | NBLog::EntryIterator NBLog::FormatEntry::begin() const |
| 92 | { |
Sanna Catherine de Treville Wager | dd92d7e | 2017-05-15 14:56:53 -0700 | [diff] [blame] | 93 | return EntryIterator(mEntry); |
| 94 | } |
| 95 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 96 | const char *NBLog::FormatEntry::formatString() const |
| 97 | { |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 98 | return (const char*) mEntry + offsetof(entry, data); |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 101 | size_t NBLog::FormatEntry::formatStringLength() const |
| 102 | { |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 103 | return mEntry[offsetof(entry, length)]; |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 106 | NBLog::EntryIterator NBLog::FormatEntry::args() const |
| 107 | { |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 108 | auto it = begin(); |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 109 | ++it; // skip start fmt |
| 110 | ++it; // skip timestamp |
| 111 | ++it; // skip hash |
Nicolas Roulet | 1ca7512 | 2017-03-16 14:19:59 -0700 | [diff] [blame] | 112 | // Skip author if present |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 113 | if (it->type == EVENT_FMT_AUTHOR) { |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 114 | ++it; |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 115 | } |
Nicolas Roulet | 1ca7512 | 2017-03-16 14:19:59 -0700 | [diff] [blame] | 116 | return it; |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 119 | int64_t NBLog::FormatEntry::timestamp() const |
| 120 | { |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 121 | auto it = begin(); |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 122 | ++it; // skip start fmt |
Nicolas Roulet | f42f156 | 2017-03-30 19:16:22 -0700 | [diff] [blame] | 123 | return it.payload<int64_t>(); |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 126 | NBLog::log_hash_t NBLog::FormatEntry::hash() const |
| 127 | { |
Nicolas Roulet | bd0c6b4 | 2017-03-16 13:54:23 -0700 | [diff] [blame] | 128 | auto it = begin(); |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 129 | ++it; // skip start fmt |
| 130 | ++it; // skip timestamp |
Nicolas Roulet | bd0c6b4 | 2017-03-16 13:54:23 -0700 | [diff] [blame] | 131 | // unaligned 64-bit read not supported |
| 132 | log_hash_t hash; |
| 133 | memcpy(&hash, it->data, sizeof(hash)); |
| 134 | return hash; |
| 135 | } |
| 136 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 137 | int NBLog::FormatEntry::author() const |
| 138 | { |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 139 | auto it = begin(); |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 140 | ++it; // skip start fmt |
| 141 | ++it; // skip timestamp |
| 142 | ++it; // skip hash |
Nicolas Roulet | 1ca7512 | 2017-03-16 14:19:59 -0700 | [diff] [blame] | 143 | // if there is an author entry, return it, return -1 otherwise |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 144 | return it->type == EVENT_FMT_AUTHOR ? it.payload<int>() : -1; |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 147 | NBLog::EntryIterator NBLog::FormatEntry::copyWithAuthor( |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 148 | std::unique_ptr<audio_utils_fifo_writer> &dst, int author) const |
| 149 | { |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 150 | auto it = begin(); |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 151 | it.copyTo(dst); // copy fmt start entry |
| 152 | (++it).copyTo(dst); // copy timestamp |
| 153 | (++it).copyTo(dst); // copy hash |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 154 | // insert author entry |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 155 | size_t authorEntrySize = Entry::kOverhead + sizeof(author); |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 156 | uint8_t authorEntry[authorEntrySize]; |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 157 | authorEntry[offsetof(entry, type)] = EVENT_FMT_AUTHOR; |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 158 | authorEntry[offsetof(entry, length)] = |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 159 | authorEntry[authorEntrySize + Entry::kPreviousLengthOffset] = |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 160 | sizeof(author); |
| 161 | *(int*) (&authorEntry[offsetof(entry, data)]) = author; |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 162 | dst->write(authorEntry, authorEntrySize); |
| 163 | // copy rest of entries |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 164 | while ((++it)->type != EVENT_FMT_END) { |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 165 | it.copyTo(dst); |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 166 | } |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 167 | it.copyTo(dst); |
| 168 | ++it; |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 169 | return it; |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 172 | void NBLog::EntryIterator::copyTo(std::unique_ptr<audio_utils_fifo_writer> &dst) const |
| 173 | { |
| 174 | size_t length = mPtr[offsetof(entry, length)] + Entry::kOverhead; |
| 175 | dst->write(mPtr, length); |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 176 | } |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 177 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 178 | void NBLog::EntryIterator::copyData(uint8_t *dst) const |
| 179 | { |
| 180 | memcpy((void*) dst, mPtr + offsetof(entry, data), mPtr[offsetof(entry, length)]); |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 183 | NBLog::EntryIterator::EntryIterator() // Dummy initialization. |
| 184 | : mPtr(nullptr) |
| 185 | { |
| 186 | } |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 187 | |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 188 | NBLog::EntryIterator::EntryIterator(const uint8_t *entry) |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 189 | : mPtr(entry) |
| 190 | { |
| 191 | } |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 192 | |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 193 | NBLog::EntryIterator::EntryIterator(const NBLog::EntryIterator &other) |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 194 | : mPtr(other.mPtr) |
| 195 | { |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 198 | const NBLog::entry& NBLog::EntryIterator::operator*() const |
| 199 | { |
| 200 | return *(entry*) mPtr; |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 203 | const NBLog::entry* NBLog::EntryIterator::operator->() const |
| 204 | { |
| 205 | return (entry*) mPtr; |
| 206 | } |
| 207 | |
| 208 | NBLog::EntryIterator& NBLog::EntryIterator::operator++() |
| 209 | { |
| 210 | mPtr += mPtr[offsetof(entry, length)] + Entry::kOverhead; |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 211 | return *this; |
| 212 | } |
| 213 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 214 | NBLog::EntryIterator& NBLog::EntryIterator::operator--() |
| 215 | { |
| 216 | mPtr -= mPtr[Entry::kPreviousLengthOffset] + Entry::kOverhead; |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 217 | return *this; |
| 218 | } |
| 219 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 220 | NBLog::EntryIterator NBLog::EntryIterator::next() const |
| 221 | { |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 222 | EntryIterator aux(*this); |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 223 | return ++aux; |
| 224 | } |
| 225 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 226 | NBLog::EntryIterator NBLog::EntryIterator::prev() const |
| 227 | { |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 228 | EntryIterator aux(*this); |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 229 | return --aux; |
| 230 | } |
| 231 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 232 | int NBLog::EntryIterator::operator-(const NBLog::EntryIterator &other) const |
| 233 | { |
| 234 | return mPtr - other.mPtr; |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 237 | bool NBLog::EntryIterator::operator!=(const EntryIterator &other) const |
| 238 | { |
| 239 | return mPtr != other.mPtr; |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 242 | bool NBLog::EntryIterator::hasConsistentLength() const |
| 243 | { |
| 244 | return mPtr[offsetof(entry, length)] == mPtr[mPtr[offsetof(entry, length)] + |
| 245 | Entry::kOverhead + Entry::kPreviousLengthOffset]; |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | // --------------------------------------------------------------------------- |
| 249 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 250 | int64_t NBLog::HistogramEntry::timestamp() const |
| 251 | { |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 252 | return EntryIterator(mEntry).payload<HistTsEntry>().ts; |
| 253 | } |
| 254 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 255 | NBLog::log_hash_t NBLog::HistogramEntry::hash() const |
| 256 | { |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 257 | return EntryIterator(mEntry).payload<HistTsEntry>().hash; |
| 258 | } |
| 259 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 260 | int NBLog::HistogramEntry::author() const |
| 261 | { |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 262 | EntryIterator it(mEntry); |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 263 | return it->length == sizeof(HistTsEntryWithAuthor) |
| 264 | ? it.payload<HistTsEntryWithAuthor>().author : -1; |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | NBLog::EntryIterator NBLog::HistogramEntry::copyWithAuthor( |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 268 | std::unique_ptr<audio_utils_fifo_writer> &dst, int author) const |
| 269 | { |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 270 | // Current histogram entry has {type, length, struct HistTsEntry, length}. |
| 271 | // We now want {type, length, struct HistTsEntryWithAuthor, length} |
| 272 | uint8_t buffer[Entry::kOverhead + sizeof(HistTsEntryWithAuthor)]; |
| 273 | // Copy content until the point we want to add the author |
| 274 | memcpy(buffer, mEntry, sizeof(entry) + sizeof(HistTsEntry)); |
| 275 | // Copy the author |
| 276 | *(int*) (buffer + sizeof(entry) + sizeof(HistTsEntry)) = author; |
| 277 | // Update lengths |
| 278 | buffer[offsetof(entry, length)] = sizeof(HistTsEntryWithAuthor); |
Ivan Lozano | 9ef855d | 2018-01-08 15:19:09 -0800 | [diff] [blame] | 279 | buffer[offsetof(entry, data) + sizeof(HistTsEntryWithAuthor) + offsetof(ending, length)] |
| 280 | = sizeof(HistTsEntryWithAuthor); |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 281 | // Write new buffer into FIFO |
| 282 | dst->write(buffer, sizeof(buffer)); |
| 283 | return EntryIterator(mEntry).next(); |
| 284 | } |
| 285 | |
| 286 | // --------------------------------------------------------------------------- |
| 287 | |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 288 | #if 0 // FIXME see note in NBLog.h |
| 289 | NBLog::Timeline::Timeline(size_t size, void *shared) |
| 290 | : mSize(roundup(size)), mOwn(shared == NULL), |
| 291 | mShared((Shared *) (mOwn ? new char[sharedSize(size)] : shared)) |
| 292 | { |
| 293 | new (mShared) Shared; |
| 294 | } |
| 295 | |
| 296 | NBLog::Timeline::~Timeline() |
| 297 | { |
| 298 | mShared->~Shared(); |
| 299 | if (mOwn) { |
| 300 | delete[] (char *) mShared; |
| 301 | } |
| 302 | } |
| 303 | #endif |
| 304 | |
| 305 | /*static*/ |
| 306 | size_t NBLog::Timeline::sharedSize(size_t size) |
| 307 | { |
Glenn Kasten | ed99c2b | 2016-12-12 08:31:24 -0800 | [diff] [blame] | 308 | // TODO fifo now supports non-power-of-2 buffer sizes, so could remove the roundup |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 309 | return sizeof(Shared) + roundup(size); |
| 310 | } |
| 311 | |
| 312 | // --------------------------------------------------------------------------- |
| 313 | |
| 314 | NBLog::Writer::Writer() |
Nicolas Roulet | c20cb50 | 2017-02-01 12:35:24 -0800 | [diff] [blame] | 315 | : mShared(NULL), mFifo(NULL), mFifoWriter(NULL), mEnabled(false), mPidTag(NULL), mPidTagSize(0) |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 316 | { |
| 317 | } |
| 318 | |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 319 | NBLog::Writer::Writer(void *shared, size_t size) |
| 320 | : mShared((Shared *) shared), |
| 321 | mFifo(mShared != NULL ? |
| 322 | new audio_utils_fifo(size, sizeof(uint8_t), |
| 323 | mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL), |
| 324 | mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL), |
| 325 | mEnabled(mFifoWriter != NULL) |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 326 | { |
Nicolas Roulet | c20cb50 | 2017-02-01 12:35:24 -0800 | [diff] [blame] | 327 | // caching pid and process name |
| 328 | pid_t id = ::getpid(); |
| 329 | char procName[16]; |
| 330 | int status = prctl(PR_GET_NAME, procName); |
| 331 | if (status) { // error getting process name |
| 332 | procName[0] = '\0'; |
| 333 | } |
| 334 | size_t length = strlen(procName); |
| 335 | mPidTagSize = length + sizeof(pid_t); |
| 336 | mPidTag = new char[mPidTagSize]; |
| 337 | memcpy(mPidTag, &id, sizeof(pid_t)); |
| 338 | memcpy(mPidTag + sizeof(pid_t), procName, length); |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 339 | } |
| 340 | |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 341 | NBLog::Writer::Writer(const sp<IMemory>& iMemory, size_t size) |
| 342 | : Writer(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size) |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 343 | { |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 344 | mIMemory = iMemory; |
| 345 | } |
| 346 | |
| 347 | NBLog::Writer::~Writer() |
| 348 | { |
| 349 | delete mFifoWriter; |
| 350 | delete mFifo; |
Nicolas Roulet | c20cb50 | 2017-02-01 12:35:24 -0800 | [diff] [blame] | 351 | delete[] mPidTag; |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | void NBLog::Writer::log(const char *string) |
| 355 | { |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 356 | if (!mEnabled) { |
| 357 | return; |
| 358 | } |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 359 | LOG_ALWAYS_FATAL_IF(string == NULL, "Attempted to log NULL string"); |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 360 | size_t length = strlen(string); |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 361 | if (length > Entry::kMaxLength) { |
| 362 | length = Entry::kMaxLength; |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 363 | } |
| 364 | log(EVENT_STRING, string, length); |
| 365 | } |
| 366 | |
| 367 | void NBLog::Writer::logf(const char *fmt, ...) |
| 368 | { |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 369 | if (!mEnabled) { |
| 370 | return; |
| 371 | } |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 372 | va_list ap; |
| 373 | va_start(ap, fmt); |
| 374 | Writer::logvf(fmt, ap); // the Writer:: is needed to avoid virtual dispatch for LockedWriter |
| 375 | va_end(ap); |
| 376 | } |
| 377 | |
| 378 | void NBLog::Writer::logvf(const char *fmt, va_list ap) |
| 379 | { |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 380 | if (!mEnabled) { |
| 381 | return; |
| 382 | } |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 383 | char buffer[Entry::kMaxLength + 1 /*NUL*/]; |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 384 | int length = vsnprintf(buffer, sizeof(buffer), fmt, ap); |
| 385 | if (length >= (int) sizeof(buffer)) { |
| 386 | length = sizeof(buffer) - 1; |
| 387 | // NUL termination is not required |
| 388 | // buffer[length] = '\0'; |
| 389 | } |
| 390 | if (length >= 0) { |
| 391 | log(EVENT_STRING, buffer, length); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | void NBLog::Writer::logTimestamp() |
| 396 | { |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 397 | if (!mEnabled) { |
| 398 | return; |
| 399 | } |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 400 | struct timespec ts; |
| 401 | if (!clock_gettime(CLOCK_MONOTONIC, &ts)) { |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 402 | log(EVENT_TIMESTAMP, &ts, sizeof(ts)); |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 406 | void NBLog::Writer::logStart(const char *fmt) |
| 407 | { |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 408 | if (!mEnabled) { |
| 409 | return; |
| 410 | } |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 411 | size_t length = strlen(fmt); |
| 412 | if (length > Entry::kMaxLength) { |
| 413 | length = Entry::kMaxLength; |
| 414 | } |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 415 | log(EVENT_FMT_START, fmt, length); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 418 | void NBLog::Writer::logTimestampFormat() |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 419 | { |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 420 | if (!mEnabled) { |
| 421 | return; |
| 422 | } |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame^] | 423 | const nsecs_t ts = systemTime(); |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 424 | if (ts > 0) { |
| 425 | log(EVENT_FMT_TIMESTAMP, &ts, sizeof(ts)); |
| 426 | } else { |
| 427 | ALOGE("Failed to get timestamp"); |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 428 | } |
Nicolas Roulet | bd0c6b4 | 2017-03-16 13:54:23 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Sanna Catherine de Treville Wager | a8a8a47 | 2017-07-11 09:41:25 -0700 | [diff] [blame] | 431 | void NBLog::Writer::logEventHistTs(Event event, log_hash_t hash) |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 432 | { |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 433 | if (!mEnabled) { |
| 434 | return; |
| 435 | } |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 436 | HistTsEntry data; |
| 437 | data.hash = hash; |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame^] | 438 | data.ts = systemTime(); |
Nicolas Roulet | f42f156 | 2017-03-30 19:16:22 -0700 | [diff] [blame] | 439 | if (data.ts > 0) { |
Sanna Catherine de Treville Wager | a8a8a47 | 2017-07-11 09:41:25 -0700 | [diff] [blame] | 440 | log(event, &data, sizeof(data)); |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 441 | } else { |
Nicolas Roulet | f42f156 | 2017-03-30 19:16:22 -0700 | [diff] [blame] | 442 | ALOGE("Failed to get timestamp"); |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
Nicolas Roulet | bd0c6b4 | 2017-03-16 13:54:23 -0700 | [diff] [blame] | 446 | void NBLog::Writer::logFormat(const char *fmt, log_hash_t hash, ...) |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 447 | { |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 448 | if (!mEnabled) { |
| 449 | return; |
| 450 | } |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 451 | va_list ap; |
Nicolas Roulet | bd0c6b4 | 2017-03-16 13:54:23 -0700 | [diff] [blame] | 452 | va_start(ap, hash); |
| 453 | Writer::logVFormat(fmt, hash, ap); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 454 | va_end(ap); |
| 455 | } |
| 456 | |
Nicolas Roulet | bd0c6b4 | 2017-03-16 13:54:23 -0700 | [diff] [blame] | 457 | void NBLog::Writer::logVFormat(const char *fmt, log_hash_t hash, va_list argp) |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 458 | { |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 459 | if (!mEnabled) { |
| 460 | return; |
| 461 | } |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 462 | Writer::logStart(fmt); |
| 463 | int i; |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 464 | double d; |
| 465 | float f; |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 466 | char* s; |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 467 | size_t length; |
Nicolas Roulet | f42f156 | 2017-03-30 19:16:22 -0700 | [diff] [blame] | 468 | int64_t t; |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 469 | Writer::logTimestampFormat(); |
| 470 | log(EVENT_FMT_HASH, &hash, sizeof(hash)); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 471 | for (const char *p = fmt; *p != '\0'; p++) { |
| 472 | // TODO: implement more complex formatting such as %.3f |
| 473 | if (*p != '%') { |
| 474 | continue; |
| 475 | } |
| 476 | switch(*++p) { |
| 477 | case 's': // string |
| 478 | s = va_arg(argp, char *); |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 479 | length = strlen(s); |
| 480 | if (length > Entry::kMaxLength) { |
| 481 | length = Entry::kMaxLength; |
| 482 | } |
| 483 | log(EVENT_FMT_STRING, s, length); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 484 | break; |
| 485 | |
| 486 | case 't': // timestamp |
Nicolas Roulet | f42f156 | 2017-03-30 19:16:22 -0700 | [diff] [blame] | 487 | t = va_arg(argp, int64_t); |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 488 | log(EVENT_FMT_TIMESTAMP, &t, sizeof(t)); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 489 | break; |
| 490 | |
| 491 | case 'd': // integer |
| 492 | i = va_arg(argp, int); |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 493 | log(EVENT_FMT_INTEGER, &i, sizeof(i)); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 494 | break; |
| 495 | |
| 496 | case 'f': // float |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 497 | d = va_arg(argp, double); // float arguments are promoted to double in vararg lists |
| 498 | f = (float)d; |
| 499 | log(EVENT_FMT_FLOAT, &f, sizeof(f)); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 500 | break; |
| 501 | |
| 502 | case 'p': // pid |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 503 | log(EVENT_FMT_PID, mPidTag, mPidTagSize); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 504 | break; |
| 505 | |
| 506 | // the "%\0" case finishes parsing |
| 507 | case '\0': |
| 508 | --p; |
| 509 | break; |
| 510 | |
Nicolas Roulet | c20cb50 | 2017-02-01 12:35:24 -0800 | [diff] [blame] | 511 | case '%': |
| 512 | break; |
| 513 | |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 514 | default: |
| 515 | ALOGW("NBLog Writer parsed invalid format specifier: %c", *p); |
| 516 | break; |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 517 | } |
| 518 | } |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 519 | Entry etr(EVENT_FMT_END, nullptr, 0); |
| 520 | log(etr, true); |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | void NBLog::Writer::log(Event event, const void *data, size_t length) |
| 524 | { |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 525 | if (!mEnabled) { |
| 526 | return; |
| 527 | } |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 528 | if (data == NULL || length > Entry::kMaxLength) { |
| 529 | // TODO Perhaps it makes sense to display truncated data or at least a |
| 530 | // message that the data is too long? The current behavior can create |
| 531 | // a confusion for a programmer debugging their code. |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 532 | return; |
| 533 | } |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 534 | // Ignore if invalid event |
| 535 | if (event == EVENT_RESERVED || event >= EVENT_UPPER_BOUND) { |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 536 | return; |
| 537 | } |
Sanna Catherine de Treville Wager | 2d1631e | 2017-05-30 16:12:36 -0700 | [diff] [blame] | 538 | Entry etr(event, data, length); |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 539 | log(etr, true /*trusted*/); |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 540 | } |
| 541 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 542 | void NBLog::Writer::log(const NBLog::Entry &etr, bool trusted) |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 543 | { |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 544 | if (!mEnabled) { |
| 545 | return; |
| 546 | } |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 547 | if (!trusted) { |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 548 | log(etr.mEvent, etr.mData, etr.mLength); |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 549 | return; |
| 550 | } |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 551 | const size_t need = etr.mLength + Entry::kOverhead; // mEvent, mLength, data[mLength], mLength |
| 552 | // need = number of bytes written to FIFO |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 553 | |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 554 | // FIXME optimize this using memcpy for the data part of the Entry. |
| 555 | // The Entry could have a method copyTo(ptr, offset, size) to optimize the copy. |
Sanna Catherine de Treville Wager | 2d1631e | 2017-05-30 16:12:36 -0700 | [diff] [blame] | 556 | // checks size of a single log Entry: type, length, data pointer and ending |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 557 | uint8_t temp[Entry::kMaxLength + Entry::kOverhead]; |
Sanna Catherine de Treville Wager | 2d1631e | 2017-05-30 16:12:36 -0700 | [diff] [blame] | 558 | // write this data to temp array |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 559 | for (size_t i = 0; i < need; i++) { |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 560 | temp[i] = etr.copyEntryDataAt(i); |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 561 | } |
Sanna Catherine de Treville Wager | 2d1631e | 2017-05-30 16:12:36 -0700 | [diff] [blame] | 562 | // write to circular buffer |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 563 | mFifoWriter->write(temp, need); |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | bool NBLog::Writer::isEnabled() const |
| 567 | { |
| 568 | return mEnabled; |
| 569 | } |
| 570 | |
| 571 | bool NBLog::Writer::setEnabled(bool enabled) |
| 572 | { |
| 573 | bool old = mEnabled; |
| 574 | mEnabled = enabled && mShared != NULL; |
| 575 | return old; |
| 576 | } |
| 577 | |
| 578 | // --------------------------------------------------------------------------- |
| 579 | |
| 580 | NBLog::LockedWriter::LockedWriter() |
| 581 | : Writer() |
| 582 | { |
| 583 | } |
| 584 | |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 585 | NBLog::LockedWriter::LockedWriter(void *shared, size_t size) |
| 586 | : Writer(shared, size) |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 587 | { |
| 588 | } |
| 589 | |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 590 | bool NBLog::LockedWriter::isEnabled() const |
| 591 | { |
| 592 | Mutex::Autolock _l(mLock); |
| 593 | return Writer::isEnabled(); |
| 594 | } |
| 595 | |
| 596 | bool NBLog::LockedWriter::setEnabled(bool enabled) |
| 597 | { |
| 598 | Mutex::Autolock _l(mLock); |
| 599 | return Writer::setEnabled(enabled); |
| 600 | } |
| 601 | |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 602 | void NBLog::LockedWriter::log(const Entry &entry, bool trusted) { |
| 603 | Mutex::Autolock _l(mLock); |
| 604 | Writer::log(entry, trusted); |
| 605 | } |
| 606 | |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 607 | // --------------------------------------------------------------------------- |
| 608 | |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 609 | // We make a set of the invalid types rather than the valid types when aligning |
| 610 | // Snapshot EntryIterators to valid entries during log corruption checking. |
| 611 | // This is done in order to avoid the maintenance overhead of adding a new NBLog::Event |
| 612 | // type to the two sets below whenever a new NBLog::Event type is created, as it is |
| 613 | // very likely that new types added will be valid types. |
| 614 | // Currently, invalidBeginTypes and invalidEndTypes are used to handle the special |
| 615 | // case of a Format Entry, which consists of a variable number of simple log entries. |
| 616 | // If a new NBLog::Event is added that consists of a variable number of simple log entries, |
| 617 | // then these sets need to be updated. |
| 618 | |
| 619 | // We want the beginning of a Snapshot to point to an entry that is not in |
| 620 | // the middle of a formatted entry and not an FMT_END. |
| 621 | const std::unordered_set<NBLog::Event> NBLog::Reader::invalidBeginTypes { |
| 622 | NBLog::Event::EVENT_FMT_TIMESTAMP, |
| 623 | NBLog::Event::EVENT_FMT_HASH, |
| 624 | NBLog::Event::EVENT_FMT_STRING, |
| 625 | NBLog::Event::EVENT_FMT_INTEGER, |
| 626 | NBLog::Event::EVENT_FMT_FLOAT, |
| 627 | NBLog::Event::EVENT_FMT_PID, |
| 628 | NBLog::Event::EVENT_FMT_AUTHOR, |
| 629 | NBLog::Event::EVENT_FMT_END |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 630 | }; |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 631 | |
| 632 | // We want the end of a Snapshot to point to an entry that is not in |
| 633 | // the middle of a formatted entry and not a FMT_START. |
| 634 | const std::unordered_set<NBLog::Event> NBLog::Reader::invalidEndTypes { |
| 635 | NBLog::Event::EVENT_FMT_START, |
| 636 | NBLog::Event::EVENT_FMT_TIMESTAMP, |
| 637 | NBLog::Event::EVENT_FMT_HASH, |
| 638 | NBLog::Event::EVENT_FMT_STRING, |
| 639 | NBLog::Event::EVENT_FMT_INTEGER, |
| 640 | NBLog::Event::EVENT_FMT_FLOAT, |
| 641 | NBLog::Event::EVENT_FMT_PID, |
| 642 | NBLog::Event::EVENT_FMT_AUTHOR |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 643 | }; |
Sanna Catherine de Treville Wager | a8a8a47 | 2017-07-11 09:41:25 -0700 | [diff] [blame] | 644 | |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 645 | NBLog::Reader::Reader(const void *shared, size_t size, const std::string &name) |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 646 | : mName(name), |
Sanna Catherine de Treville Wager | e486526 | 2017-07-14 16:24:15 -0700 | [diff] [blame] | 647 | mShared((/*const*/ Shared *) shared), /*mIMemory*/ |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 648 | mFifo(mShared != NULL ? |
| 649 | new audio_utils_fifo(size, sizeof(uint8_t), |
| 650 | mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL), |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 651 | mFifoReader(mFifo != NULL ? new audio_utils_fifo_reader(*mFifo) : NULL) |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 652 | { |
| 653 | } |
| 654 | |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 655 | NBLog::Reader::Reader(const sp<IMemory>& iMemory, size_t size, const std::string &name) |
| 656 | : Reader(iMemory != 0 ? (Shared *) iMemory->pointer() : NULL, size, name) |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 657 | { |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 658 | mIMemory = iMemory; |
| 659 | } |
| 660 | |
| 661 | NBLog::Reader::~Reader() |
| 662 | { |
| 663 | delete mFifoReader; |
| 664 | delete mFifo; |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 665 | } |
| 666 | |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 667 | const uint8_t *NBLog::Reader::findLastValidEntry(const uint8_t *front, const uint8_t *back, |
| 668 | const std::unordered_set<Event> &invalidTypes) { |
| 669 | if (front == nullptr || back == nullptr) { |
| 670 | return nullptr; |
| 671 | } |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 672 | while (back + Entry::kPreviousLengthOffset >= front) { |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 673 | const uint8_t *prev = back - back[Entry::kPreviousLengthOffset] - Entry::kOverhead; |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 674 | const Event type = (const Event)prev[offsetof(entry, type)]; |
| 675 | if (prev < front |
| 676 | || prev + prev[offsetof(entry, length)] + Entry::kOverhead != back |
| 677 | || type <= NBLog::EVENT_RESERVED || type >= NBLog::EVENT_UPPER_BOUND) { |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 678 | // prev points to an out of limits or inconsistent entry |
| 679 | return nullptr; |
| 680 | } |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 681 | // if invalidTypes does not contain the type, then the type is valid. |
| 682 | if (invalidTypes.find(type) == invalidTypes.end()) { |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 683 | return prev; |
| 684 | } |
| 685 | back = prev; |
| 686 | } |
| 687 | return nullptr; // no entry found |
| 688 | } |
| 689 | |
Sanna Catherine de Treville Wager | e486526 | 2017-07-14 16:24:15 -0700 | [diff] [blame] | 690 | // Copies content of a Reader FIFO into its Snapshot |
| 691 | // The Snapshot has the same raw data, but represented as a sequence of entries |
| 692 | // and an EntryIterator making it possible to process the data. |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 693 | std::unique_ptr<NBLog::Snapshot> NBLog::Reader::getSnapshot() |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 694 | { |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 695 | if (mFifoReader == NULL) { |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 696 | return std::unique_ptr<Snapshot>(new Snapshot()); |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 697 | } |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 698 | |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 699 | // This emulates the behaviour of audio_utils_fifo_reader::read, but without incrementing the |
| 700 | // reader index. The index is incremented after handling corruption, to after the last complete |
| 701 | // entry of the buffer |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 702 | size_t lost = 0; |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 703 | audio_utils_iovec iovec[2]; |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 704 | const size_t capacity = mFifo->capacity(); |
| 705 | ssize_t availToRead; |
| 706 | // A call to audio_utils_fifo_reader::obtain() places the read pointer one buffer length |
| 707 | // before the writer's pointer (since mFifoReader was constructed with flush=false). The |
| 708 | // do while loop is an attempt to read all of the FIFO's contents regardless of how behind |
| 709 | // the reader is with respect to the writer. However, the following scheduling sequence is |
| 710 | // possible and can lead to a starvation situation: |
| 711 | // - Writer T1 writes, overrun with respect to Reader T2 |
| 712 | // - T2 calls obtain() and gets EOVERFLOW, T2 ptr placed one buffer size behind T1 ptr |
| 713 | // - T1 write, overrun |
| 714 | // - T2 obtain(), EOVERFLOW (and so on...) |
| 715 | // To address this issue, we limit the number of tries for the reader to catch up with |
| 716 | // the writer. |
| 717 | int tries = 0; |
| 718 | size_t lostTemp; |
| 719 | do { |
| 720 | availToRead = mFifoReader->obtain(iovec, capacity, NULL /*timeout*/, &lostTemp); |
| 721 | lost += lostTemp; |
| 722 | } while (availToRead < 0 || ++tries <= kMaxObtainTries); |
| 723 | |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 724 | if (availToRead <= 0) { |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 725 | ALOGW_IF(availToRead < 0, "NBLog Reader %s failed to catch up with Writer", mName.c_str()); |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 726 | return std::unique_ptr<Snapshot>(new Snapshot()); |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 727 | } |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 728 | |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 729 | // Change to #if 1 for debugging. This statement is useful for checking buffer fullness levels |
| 730 | // (as seen by reader) and how much data was lost. If you find that the fullness level is |
| 731 | // getting close to full, or that data loss is happening to often, then you should |
| 732 | // probably try some of the following: |
| 733 | // - log less data |
| 734 | // - log less often |
| 735 | // - increase the initial shared memory allocation for the buffer |
| 736 | #if 0 |
| 737 | ALOGD("getSnapshot name=%s, availToRead=%zd, capacity=%zu, fullness=%.3f, lost=%zu", |
| 738 | name().c_str(), availToRead, capacity, (double)availToRead / (double)capacity, lost); |
| 739 | #endif |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 740 | std::unique_ptr<Snapshot> snapshot(new Snapshot(availToRead)); |
| 741 | memcpy(snapshot->mData, (const char *) mFifo->buffer() + iovec[0].mOffset, iovec[0].mLength); |
| 742 | if (iovec[1].mLength > 0) { |
| 743 | memcpy(snapshot->mData + (iovec[0].mLength), |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 744 | (const char *) mFifo->buffer() + iovec[1].mOffset, iovec[1].mLength); |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | // Handle corrupted buffer |
| 748 | // Potentially, a buffer has corrupted data on both beginning (due to overflow) and end |
| 749 | // (due to incomplete format entry). But even if the end format entry is incomplete, |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 750 | // it ends in a complete entry (which is not an FMT_END). So is safe to traverse backwards. |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 751 | // TODO: handle client corruption (in the middle of a buffer) |
| 752 | |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 753 | const uint8_t *back = snapshot->mData + availToRead; |
| 754 | const uint8_t *front = snapshot->mData; |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 755 | |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 756 | // Find last FMT_END. <back> is sitting on an entry which might be the middle of a FormatEntry. |
| 757 | // We go backwards until we find an EVENT_FMT_END. |
| 758 | const uint8_t *lastEnd = findLastValidEntry(front, back, invalidEndTypes); |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 759 | if (lastEnd == nullptr) { |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 760 | snapshot->mEnd = snapshot->mBegin = EntryIterator(front); |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 761 | } else { |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 762 | // end of snapshot points to after last FMT_END entry |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 763 | snapshot->mEnd = EntryIterator(lastEnd).next(); |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 764 | // find first FMT_START |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 765 | const uint8_t *firstStart = nullptr; |
| 766 | const uint8_t *firstStartTmp = snapshot->mEnd; |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 767 | while ((firstStartTmp = findLastValidEntry(front, firstStartTmp, invalidBeginTypes)) |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 768 | != nullptr) { |
| 769 | firstStart = firstStartTmp; |
| 770 | } |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 771 | // firstStart is null if no FMT_START entry was found before lastEnd |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 772 | if (firstStart == nullptr) { |
| 773 | snapshot->mBegin = snapshot->mEnd; |
| 774 | } else { |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 775 | snapshot->mBegin = EntryIterator(firstStart); |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 776 | } |
| 777 | } |
| 778 | |
| 779 | // advance fifo reader index to after last entry read. |
| 780 | mFifoReader->release(snapshot->mEnd - front); |
| 781 | |
| 782 | snapshot->mLost = lost; |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 783 | return snapshot; |
| 784 | } |
| 785 | |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame^] | 786 | // TODO separate this method from the rest of NBLog |
Sanna Catherine de Treville Wager | e486526 | 2017-07-14 16:24:15 -0700 | [diff] [blame] | 787 | // Takes raw content of the local merger FIFO, processes log entries, and |
| 788 | // writes the data to a map of class PerformanceAnalysis, based on their thread ID. |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame^] | 789 | void NBLog::MergeReader::processSnapshot(NBLog::Snapshot &snapshot, int author) |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 790 | { |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame^] | 791 | PerformanceData& data = mThreadPerformanceData[author]; |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 792 | // We don't do "auto it" because it reduces readability in this case. |
| 793 | for (EntryIterator it = snapshot.begin(); it != snapshot.end(); ++it) { |
| 794 | switch (it->type) { |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 795 | case EVENT_HISTOGRAM_ENTRY_TS: { |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame^] | 796 | const HistTsEntry payload = it.payload<HistTsEntry>(); |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame] | 797 | // TODO: hash for histogram ts and audio state need to match |
| 798 | // and correspond to audio production source file location |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 799 | mThreadPerformanceAnalysis[author][0 /*hash*/].logTsEntry(payload.ts); |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 800 | } break; |
Sanna Catherine de Treville Wager | a8a8a47 | 2017-07-11 09:41:25 -0700 | [diff] [blame] | 801 | case EVENT_AUDIO_STATE: { |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 802 | mThreadPerformanceAnalysis[author][0 /*hash*/].handleStateChange(); |
| 803 | } break; |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame^] | 804 | case EVENT_THREAD_INFO: { |
| 805 | const thread_info_t info = it.payload<thread_info_t>(); |
| 806 | // TODO make PerformanceData hold a type of thread_info_t. |
| 807 | // Currently, thread_info_t is defined in NBLog.h, which includes |
| 808 | // PerformanceAnalysis.h. PerformanceData is defined in PerformanceAnalysis.h, |
| 809 | // where including NBLog.h would result in circular includes. The organization |
| 810 | // of files will need to change to avoid this problem. |
| 811 | data.type = info.type; |
| 812 | data.frameCount = info.frameCount; |
| 813 | data.sampleRate = info.sampleRate; |
| 814 | } break; |
Eric Tan | e98dd6f | 2018-08-22 18:23:50 -0700 | [diff] [blame] | 815 | case EVENT_LATENCY: { |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame^] | 816 | const double latencyMs = it.payload<double>(); |
| 817 | data.latencyHist.add(latencyMs); |
Eric Tan | e98dd6f | 2018-08-22 18:23:50 -0700 | [diff] [blame] | 818 | } break; |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 819 | case EVENT_WORK_TIME: { |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame^] | 820 | const int64_t monotonicNs = it.payload<int64_t>(); |
Eric Tan | e98dd6f | 2018-08-22 18:23:50 -0700 | [diff] [blame] | 821 | const double monotonicMs = monotonicNs * 1e-6; |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame^] | 822 | data.workHist.add(monotonicMs); |
| 823 | data.active += monotonicNs; |
Eric Tan | e98dd6f | 2018-08-22 18:23:50 -0700 | [diff] [blame] | 824 | } break; |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame^] | 825 | case EVENT_WARMUP_TIME: { |
| 826 | const double timeMs = it.payload<double>(); |
| 827 | data.warmupHist.add(timeMs); |
| 828 | } break; |
| 829 | case EVENT_UNDERRUN: |
| 830 | data.underruns++; |
| 831 | break; |
| 832 | case EVENT_OVERRUN: |
| 833 | data.overruns++; |
| 834 | break; |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 835 | case EVENT_RESERVED: |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 836 | case EVENT_UPPER_BOUND: |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 837 | ALOGW("warning: unexpected event %d", it->type); |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 838 | default: |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 839 | break; |
| 840 | } |
Sanna Catherine de Treville Wager | 9484bae | 2017-06-15 14:39:44 -0700 | [diff] [blame] | 841 | } |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 842 | } |
| 843 | |
Sanna Catherine de Treville Wager | e486526 | 2017-07-14 16:24:15 -0700 | [diff] [blame] | 844 | void NBLog::MergeReader::getAndProcessSnapshot() |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 845 | { |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 846 | // get a snapshot of each reader and process them |
| 847 | // TODO insert lock here |
| 848 | const size_t nLogs = mReaders.size(); |
| 849 | std::vector<std::unique_ptr<Snapshot>> snapshots(nLogs); |
| 850 | for (size_t i = 0; i < nLogs; i++) { |
| 851 | snapshots[i] = mReaders[i]->getSnapshot(); |
| 852 | } |
| 853 | // TODO unlock lock here |
| 854 | for (size_t i = 0; i < nLogs; i++) { |
| 855 | if (snapshots[i] != nullptr) { |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame^] | 856 | processSnapshot(*(snapshots[i]), i); |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 857 | } |
| 858 | } |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 859 | } |
| 860 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 861 | void NBLog::MergeReader::dump(int fd, int indent) |
| 862 | { |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame] | 863 | // TODO: add a mutex around media.log dump |
Sanna Catherine de Treville Wager | cf6c75a | 2017-07-21 17:05:25 -0700 | [diff] [blame] | 864 | ReportPerformance::dump(fd, indent, mThreadPerformanceAnalysis); |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame^] | 865 | Json::Value root(Json::arrayValue); |
| 866 | for (const auto& item : mThreadPerformanceData) { |
| 867 | const PerformanceData& data = item.second; |
| 868 | std::unique_ptr<Json::Value> threadData = dumpToJson(data); |
| 869 | if (threadData == nullptr) { |
| 870 | continue; |
| 871 | } |
| 872 | (*threadData)["threadNum"] = item.first; |
| 873 | root.append(*threadData); |
| 874 | } |
| 875 | Json::StyledWriter writer; |
| 876 | std::string rootStr = writer.write(root); |
| 877 | dprintf(fd, "%s", rootStr.c_str()); |
Sanna Catherine de Treville Wager | e486526 | 2017-07-14 16:24:15 -0700 | [diff] [blame] | 878 | } |
| 879 | |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 880 | // TODO for future compatibility, would prefer to have a dump() go to string, and then go |
| 881 | // to fd only when invoked through binder. |
| 882 | void NBLog::DumpReader::dump(int fd, size_t indent) |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 883 | { |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 884 | if (fd < 0) return; |
| 885 | std::unique_ptr<Snapshot> snapshot = getSnapshot(); |
| 886 | if (snapshot == nullptr) { |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 887 | return; |
| 888 | } |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 889 | String8 timestamp, body; |
| 890 | |
| 891 | // TODO all logged types should have a printable format. |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 892 | for (EntryIterator it = snapshot->begin(); it != snapshot->end(); ++it) { |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 893 | switch (it->type) { |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 894 | case EVENT_FMT_START: |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 895 | it = handleFormat(FormatEntry(it), ×tamp, &body); |
| 896 | break; |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 897 | case EVENT_WORK_TIME: { |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame^] | 898 | const int64_t monotonicNs = it.payload<int64_t>(); |
| 899 | body.appendFormat("Thread cycle: %ld ns", (long)monotonicNs); |
Eric Tan | e98dd6f | 2018-08-22 18:23:50 -0700 | [diff] [blame] | 900 | } break; |
| 901 | case EVENT_LATENCY: { |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame^] | 902 | const double latencyMs = it.payload<double>(); |
Eric Tan | e98dd6f | 2018-08-22 18:23:50 -0700 | [diff] [blame] | 903 | body.appendFormat("latency: %.3f ms", latencyMs); |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 904 | } break; |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame^] | 905 | case EVENT_WARMUP_TIME: { |
| 906 | const double timeMs = it.payload<double>(); |
| 907 | body.appendFormat("warmup time: %.3f ms", timeMs); |
| 908 | } break; |
| 909 | case EVENT_UNDERRUN: |
| 910 | body.appendFormat("underrun"); |
| 911 | break; |
| 912 | case EVENT_OVERRUN: |
| 913 | body.appendFormat("overrun"); |
| 914 | break; |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 915 | case EVENT_FMT_END: |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 916 | case EVENT_RESERVED: |
| 917 | case EVENT_UPPER_BOUND: |
| 918 | body.appendFormat("warning: unexpected event %d", it->type); |
| 919 | default: |
| 920 | break; |
| 921 | } |
| 922 | if (!body.isEmpty()) { |
| 923 | dprintf(fd, "%.*s%s %s\n", (int)indent, "", timestamp.string(), body.string()); |
| 924 | body.clear(); |
| 925 | } |
| 926 | timestamp.clear(); |
Glenn Kasten | 4e01ef6 | 2013-07-11 14:29:59 -0700 | [diff] [blame] | 927 | } |
Glenn Kasten | 4e01ef6 | 2013-07-11 14:29:59 -0700 | [diff] [blame] | 928 | } |
| 929 | |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 930 | bool NBLog::Reader::isIMemory(const sp<IMemory>& iMemory) const |
| 931 | { |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 932 | return iMemory != 0 && mIMemory != 0 && iMemory->pointer() == mIMemory->pointer(); |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 933 | } |
| 934 | |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 935 | void NBLog::DumpReader::appendTimestamp(String8 *body, const void *data) |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 936 | { |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 937 | if (body == nullptr || data == nullptr) { |
| 938 | return; |
| 939 | } |
Nicolas Roulet | f42f156 | 2017-03-30 19:16:22 -0700 | [diff] [blame] | 940 | int64_t ts; |
| 941 | memcpy(&ts, data, sizeof(ts)); |
| 942 | body->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)), |
| 943 | (int) ((ts / (1000 * 1000)) % 1000)); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 944 | } |
| 945 | |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 946 | void NBLog::DumpReader::appendInt(String8 *body, const void *data) |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 947 | { |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 948 | if (body == nullptr || data == nullptr) { |
| 949 | return; |
| 950 | } |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 951 | int x = *((int*) data); |
| 952 | body->appendFormat("<%d>", x); |
| 953 | } |
| 954 | |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 955 | void NBLog::DumpReader::appendFloat(String8 *body, const void *data) |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 956 | { |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 957 | if (body == nullptr || data == nullptr) { |
| 958 | return; |
| 959 | } |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 960 | float f; |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 961 | memcpy(&f, data, sizeof(f)); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 962 | body->appendFormat("<%f>", f); |
| 963 | } |
| 964 | |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 965 | void NBLog::DumpReader::appendPID(String8 *body, const void* data, size_t length) |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 966 | { |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 967 | if (body == nullptr || data == nullptr) { |
| 968 | return; |
| 969 | } |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 970 | pid_t id = *((pid_t*) data); |
Nicolas Roulet | c20cb50 | 2017-02-01 12:35:24 -0800 | [diff] [blame] | 971 | char * name = &((char*) data)[sizeof(pid_t)]; |
| 972 | body->appendFormat("<PID: %d, name: %.*s>", id, (int) (length - sizeof(pid_t)), name); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 973 | } |
| 974 | |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 975 | String8 NBLog::DumpReader::bufferDump(const uint8_t *buffer, size_t size) |
Nicolas Roulet | 2aedf37 | 2017-03-29 11:27:03 -0700 | [diff] [blame] | 976 | { |
| 977 | String8 str; |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 978 | if (buffer == nullptr) { |
| 979 | return str; |
| 980 | } |
Nicolas Roulet | 2aedf37 | 2017-03-29 11:27:03 -0700 | [diff] [blame] | 981 | str.append("[ "); |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 982 | for(size_t i = 0; i < size; i++) { |
Nicolas Roulet | f42f156 | 2017-03-30 19:16:22 -0700 | [diff] [blame] | 983 | str.appendFormat("%d ", buffer[i]); |
Nicolas Roulet | 2aedf37 | 2017-03-29 11:27:03 -0700 | [diff] [blame] | 984 | } |
| 985 | str.append("]"); |
| 986 | return str; |
| 987 | } |
| 988 | |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 989 | String8 NBLog::DumpReader::bufferDump(const EntryIterator &it) |
Nicolas Roulet | 2aedf37 | 2017-03-29 11:27:03 -0700 | [diff] [blame] | 990 | { |
Nicolas Roulet | f42f156 | 2017-03-30 19:16:22 -0700 | [diff] [blame] | 991 | return bufferDump(it, it->length + Entry::kOverhead); |
Nicolas Roulet | 2aedf37 | 2017-03-29 11:27:03 -0700 | [diff] [blame] | 992 | } |
| 993 | |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 994 | NBLog::EntryIterator NBLog::DumpReader::handleFormat(const FormatEntry &fmtEntry, |
| 995 | String8 *timestamp, |
| 996 | String8 *body) |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 997 | { |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 998 | // log timestamp |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame^] | 999 | const int64_t ts = fmtEntry.timestamp(); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 1000 | timestamp->clear(); |
Nicolas Roulet | f42f156 | 2017-03-30 19:16:22 -0700 | [diff] [blame] | 1001 | timestamp->appendFormat("[%d.%03d]", (int) (ts / (1000 * 1000 * 1000)), |
| 1002 | (int) ((ts / (1000 * 1000)) % 1000)); |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 1003 | |
Nicolas Roulet | bd0c6b4 | 2017-03-16 13:54:23 -0700 | [diff] [blame] | 1004 | // log unique hash |
| 1005 | log_hash_t hash = fmtEntry.hash(); |
| 1006 | // print only lower 16bit of hash as hex and line as int to reduce spam in the log |
| 1007 | body->appendFormat("%.4X-%d ", (int)(hash >> 16) & 0xFFFF, (int) hash & 0xFFFF); |
| 1008 | |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 1009 | // log author (if present) |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 1010 | handleAuthor(fmtEntry, body); |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 1011 | |
| 1012 | // log string |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 1013 | EntryIterator arg = fmtEntry.args(); |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 1014 | |
| 1015 | const char* fmt = fmtEntry.formatString(); |
| 1016 | size_t fmt_length = fmtEntry.formatStringLength(); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 1017 | |
| 1018 | for (size_t fmt_offset = 0; fmt_offset < fmt_length; ++fmt_offset) { |
| 1019 | if (fmt[fmt_offset] != '%') { |
| 1020 | body->append(&fmt[fmt_offset], 1); // TODO optimize to write consecutive strings at once |
| 1021 | continue; |
| 1022 | } |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 1023 | // case "%%"" |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 1024 | if (fmt[++fmt_offset] == '%') { |
| 1025 | body->append("%"); |
| 1026 | continue; |
| 1027 | } |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 1028 | // case "%\0" |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 1029 | if (fmt_offset == fmt_length) { |
| 1030 | continue; |
| 1031 | } |
| 1032 | |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 1033 | NBLog::Event event = (NBLog::Event) arg->type; |
| 1034 | size_t length = arg->length; |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 1035 | |
| 1036 | // TODO check length for event type is correct |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 1037 | |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 1038 | if (event == EVENT_FMT_END) { |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 1039 | break; |
| 1040 | } |
| 1041 | |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 1042 | // TODO: implement more complex formatting such as %.3f |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 1043 | const uint8_t *datum = arg->data; // pointer to the current event args |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 1044 | switch(fmt[fmt_offset]) |
| 1045 | { |
| 1046 | case 's': // string |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 1047 | ALOGW_IF(event != EVENT_FMT_STRING, |
Nicolas Roulet | 4da7820 | 2017-02-03 12:53:39 -0800 | [diff] [blame] | 1048 | "NBLog Reader incompatible event for string specifier: %d", event); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 1049 | body->append((const char*) datum, length); |
| 1050 | break; |
| 1051 | |
| 1052 | case 't': // timestamp |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 1053 | ALOGW_IF(event != EVENT_FMT_TIMESTAMP, |
Nicolas Roulet | 4da7820 | 2017-02-03 12:53:39 -0800 | [diff] [blame] | 1054 | "NBLog Reader incompatible event for timestamp specifier: %d", event); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 1055 | appendTimestamp(body, datum); |
| 1056 | break; |
| 1057 | |
| 1058 | case 'd': // integer |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 1059 | ALOGW_IF(event != EVENT_FMT_INTEGER, |
Nicolas Roulet | 4da7820 | 2017-02-03 12:53:39 -0800 | [diff] [blame] | 1060 | "NBLog Reader incompatible event for integer specifier: %d", event); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 1061 | appendInt(body, datum); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 1062 | break; |
| 1063 | |
| 1064 | case 'f': // float |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 1065 | ALOGW_IF(event != EVENT_FMT_FLOAT, |
Nicolas Roulet | 4da7820 | 2017-02-03 12:53:39 -0800 | [diff] [blame] | 1066 | "NBLog Reader incompatible event for float specifier: %d", event); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 1067 | appendFloat(body, datum); |
| 1068 | break; |
| 1069 | |
| 1070 | case 'p': // pid |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 1071 | ALOGW_IF(event != EVENT_FMT_PID, |
Nicolas Roulet | 4da7820 | 2017-02-03 12:53:39 -0800 | [diff] [blame] | 1072 | "NBLog Reader incompatible event for pid specifier: %d", event); |
Nicolas Roulet | c20cb50 | 2017-02-01 12:35:24 -0800 | [diff] [blame] | 1073 | appendPID(body, datum, length); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 1074 | break; |
| 1075 | |
| 1076 | default: |
| 1077 | ALOGW("NBLog Reader encountered unknown character %c", fmt[fmt_offset]); |
| 1078 | } |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 1079 | ++arg; |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 1080 | } |
Eric Tan | cf3d82c | 2018-09-04 15:44:45 -0700 | [diff] [blame] | 1081 | ALOGW_IF(arg->type != EVENT_FMT_END, "Expected end of format, got %d", arg->type); |
Nicolas Roulet | cd5dd01 | 2017-02-13 12:09:28 -0800 | [diff] [blame] | 1082 | return arg; |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 1083 | } |
| 1084 | |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 1085 | NBLog::Merger::Merger(const void *shared, size_t size): |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 1086 | mShared((Shared *) shared), |
| 1087 | mFifo(mShared != NULL ? |
| 1088 | new audio_utils_fifo(size, sizeof(uint8_t), |
| 1089 | mShared->mBuffer, mShared->mRear, NULL /*throttlesFront*/) : NULL), |
| 1090 | mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL) |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 1091 | { |
| 1092 | } |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 1093 | |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 1094 | void NBLog::Merger::addReader(const sp<NBLog::Reader> &reader) |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 1095 | { |
Glenn Kasten | 1c44627 | 2017-04-07 09:49:07 -0700 | [diff] [blame] | 1096 | // FIXME This is called by binder thread in MediaLogService::registerWriter |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 1097 | // but the access to shared variable mReaders is not yet protected by a lock. |
| 1098 | mReaders.push_back(reader); |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | // items placed in priority queue during merge |
| 1102 | // composed by a timestamp and the index of the snapshot where the timestamp came from |
| 1103 | struct MergeItem |
| 1104 | { |
Nicolas Roulet | f42f156 | 2017-03-30 19:16:22 -0700 | [diff] [blame] | 1105 | int64_t ts; |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 1106 | int index; |
Nicolas Roulet | f42f156 | 2017-03-30 19:16:22 -0700 | [diff] [blame] | 1107 | MergeItem(int64_t ts, int index): ts(ts), index(index) {} |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 1108 | }; |
| 1109 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 1110 | bool operator>(const struct MergeItem &i1, const struct MergeItem &i2) |
| 1111 | { |
Nicolas Roulet | f42f156 | 2017-03-30 19:16:22 -0700 | [diff] [blame] | 1112 | return i1.ts > i2.ts || (i1.ts == i2.ts && i1.index > i2.index); |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 1113 | } |
| 1114 | |
Sanna Catherine de Treville Wager | e486526 | 2017-07-14 16:24:15 -0700 | [diff] [blame] | 1115 | // Merge registered readers, sorted by timestamp, and write data to a single FIFO in local memory |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 1116 | void NBLog::Merger::merge() |
| 1117 | { |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 1118 | if (true) return; // Merging is not necessary at the moment, so this is to disable it |
| 1119 | // and bypass compiler warnings about member variables not being used. |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 1120 | const int nLogs = mReaders.size(); |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 1121 | std::vector<std::unique_ptr<Snapshot>> snapshots(nLogs); |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 1122 | std::vector<EntryIterator> offsets; |
| 1123 | offsets.reserve(nLogs); |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 1124 | for (int i = 0; i < nLogs; ++i) { |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 1125 | snapshots[i] = mReaders[i]->getSnapshot(); |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 1126 | offsets.push_back(snapshots[i]->begin()); |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 1127 | } |
| 1128 | // initialize offsets |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 1129 | // TODO custom heap implementation could allow to update top, improving performance |
| 1130 | // for bursty buffers |
| 1131 | std::priority_queue<MergeItem, std::vector<MergeItem>, std::greater<MergeItem>> timestamps; |
| 1132 | for (int i = 0; i < nLogs; ++i) |
| 1133 | { |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 1134 | if (offsets[i] != snapshots[i]->end()) { |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 1135 | std::unique_ptr<AbstractEntry> abstractEntry = AbstractEntry::buildEntry(offsets[i]); |
| 1136 | if (abstractEntry == nullptr) { |
| 1137 | continue; |
| 1138 | } |
| 1139 | timestamps.emplace(abstractEntry->timestamp(), i); |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | while (!timestamps.empty()) { |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 1144 | int index = timestamps.top().index; // find minimum timestamp |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 1145 | // copy it to the log, increasing offset |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 1146 | offsets[index] = AbstractEntry::buildEntry(offsets[index])-> |
| 1147 | copyWithAuthor(mFifoWriter, index); |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 1148 | // update data structures |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 1149 | timestamps.pop(); |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 1150 | if (offsets[index] != snapshots[index]->end()) { |
Nicolas Roulet | f42f156 | 2017-03-30 19:16:22 -0700 | [diff] [blame] | 1151 | int64_t ts = AbstractEntry::buildEntry(offsets[index])->timestamp(); |
Nicolas Roulet | 6ea1d7e | 2017-02-14 16:17:31 -0800 | [diff] [blame] | 1152 | timestamps.emplace(ts, index); |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 1153 | } |
| 1154 | } |
| 1155 | } |
| 1156 | |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 1157 | const std::vector<sp<NBLog::Reader>>& NBLog::Merger::getReaders() const |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 1158 | { |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 1159 | //AutoMutex _l(mLock); |
| 1160 | return mReaders; |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 1161 | } |
| 1162 | |
Glenn Kasten | 1c44627 | 2017-04-07 09:49:07 -0700 | [diff] [blame] | 1163 | // --------------------------------------------------------------------------- |
| 1164 | |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 1165 | NBLog::MergeReader::MergeReader(const void *shared, size_t size, Merger &merger) |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 1166 | : Reader(shared, size, "MergeReader"), mReaders(merger.getReaders()) |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 1167 | { |
| 1168 | } |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 1169 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 1170 | void NBLog::MergeReader::handleAuthor(const NBLog::AbstractEntry &entry, String8 *body) |
| 1171 | { |
Nicolas Roulet | 537ad7d | 2017-03-21 16:24:30 -0700 | [diff] [blame] | 1172 | int author = entry.author(); |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 1173 | if (author == -1) { |
| 1174 | return; |
| 1175 | } |
Glenn Kasten | 1c44627 | 2017-04-07 09:49:07 -0700 | [diff] [blame] | 1176 | // FIXME Needs a lock |
Eric Tan | 5786e01 | 2018-08-15 09:03:47 -0700 | [diff] [blame] | 1177 | const char* name = mReaders[author]->name().c_str(); |
Nicolas Roulet | 40a4498 | 2017-02-03 13:39:57 -0800 | [diff] [blame] | 1178 | body->appendFormat("%s: ", name); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 1179 | } |
| 1180 | |
Glenn Kasten | 1c44627 | 2017-04-07 09:49:07 -0700 | [diff] [blame] | 1181 | // --------------------------------------------------------------------------- |
| 1182 | |
Sanna Catherine de Treville Wager | e486526 | 2017-07-14 16:24:15 -0700 | [diff] [blame] | 1183 | NBLog::MergeThread::MergeThread(NBLog::Merger &merger, NBLog::MergeReader &mergeReader) |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 1184 | : mMerger(merger), |
Sanna Catherine de Treville Wager | e486526 | 2017-07-14 16:24:15 -0700 | [diff] [blame] | 1185 | mMergeReader(mergeReader), |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 1186 | mTimeoutUs(0) |
| 1187 | { |
| 1188 | } |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 1189 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 1190 | NBLog::MergeThread::~MergeThread() |
| 1191 | { |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 1192 | // set exit flag, set timeout to 0 to force threadLoop to exit and wait for the thread to join |
| 1193 | requestExit(); |
| 1194 | setTimeoutUs(0); |
| 1195 | join(); |
| 1196 | } |
| 1197 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 1198 | bool NBLog::MergeThread::threadLoop() |
| 1199 | { |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 1200 | bool doMerge; |
| 1201 | { |
| 1202 | AutoMutex _l(mMutex); |
| 1203 | // If mTimeoutUs is negative, wait on the condition variable until it's positive. |
Eric Tan | 6af1847 | 2018-08-20 09:27:50 -0700 | [diff] [blame] | 1204 | // If it's positive, merge. The minimum period between waking the condition variable |
| 1205 | // is handled in AudioFlinger::MediaLogNotifier::threadLoop(). |
| 1206 | mCond.wait(mMutex); |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 1207 | doMerge = mTimeoutUs > 0; |
| 1208 | mTimeoutUs -= kThreadSleepPeriodUs; |
| 1209 | } |
| 1210 | if (doMerge) { |
Sanna Catherine de Treville Wager | e486526 | 2017-07-14 16:24:15 -0700 | [diff] [blame] | 1211 | // Merge data from all the readers |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 1212 | mMerger.merge(); |
Sanna Catherine de Treville Wager | e486526 | 2017-07-14 16:24:15 -0700 | [diff] [blame] | 1213 | // Process the data collected by mMerger and write it to PerformanceAnalysis |
| 1214 | // FIXME: decide whether to call getAndProcessSnapshot every time |
| 1215 | // or whether to have a separate thread that calls it with a lower frequency |
| 1216 | mMergeReader.getAndProcessSnapshot(); |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 1217 | } |
| 1218 | return true; |
| 1219 | } |
| 1220 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 1221 | void NBLog::MergeThread::wakeup() |
| 1222 | { |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 1223 | setTimeoutUs(kThreadWakeupPeriodUs); |
| 1224 | } |
| 1225 | |
Eric Tan | 86be872 | 2018-08-10 10:25:47 -0700 | [diff] [blame] | 1226 | void NBLog::MergeThread::setTimeoutUs(int time) |
| 1227 | { |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 1228 | AutoMutex _l(mMutex); |
| 1229 | mTimeoutUs = time; |
| 1230 | mCond.signal(); |
| 1231 | } |
| 1232 | |
Glenn Kasten | 11d8dfc | 2013-01-14 14:53:13 -0800 | [diff] [blame] | 1233 | } // namespace android |