Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012-2014 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 | */ |
Mark Salyzyn | 21fc393 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 16 | // for manual checking of stale entries during LogBuffer::erase() |
| 17 | //#define DEBUG_CHECK_FOR_STALE_ENTRIES |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 18 | |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 19 | #include <ctype.h> |
Mark Salyzyn | edb18aa | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 20 | #include <endian.h> |
Mark Salyzyn | 88fe7cc | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 21 | #include <errno.h> |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <string.h> |
Mark Salyzyn | 2e86fa4 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 24 | #include <sys/cdefs.h> |
Mark Salyzyn | 7b85c59 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 25 | #include <sys/user.h> |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 26 | #include <time.h> |
| 27 | #include <unistd.h> |
| 28 | |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 29 | #include <unordered_map> |
Mark Salyzyn | 399d5b9 | 2017-03-03 10:21:23 -0800 | [diff] [blame^] | 30 | #include <utility> |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 31 | |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 32 | #include <cutils/properties.h> |
Mark Salyzyn | f4693f3 | 2016-09-27 13:08:23 -0700 | [diff] [blame] | 33 | #include <private/android_logger.h> |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 34 | |
| 35 | #include "LogBuffer.h" |
Mark Salyzyn | 2b4a763 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 36 | #include "LogKlog.h" |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 37 | #include "LogReader.h" |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 38 | #include "LogUtils.h" |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 39 | |
Mark Salyzyn | 2e86fa4 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 40 | #ifndef __predict_false |
| 41 | #define __predict_false(exp) __builtin_expect((exp) != 0, 0) |
| 42 | #endif |
| 43 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 44 | // Default |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 45 | #define log_buffer_size(id) mMaxSize[id] |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 46 | |
Mark Salyzyn | 3fe2593 | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 47 | void LogBuffer::init() { |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 48 | log_id_for_each(i) { |
Mark Salyzyn | 7a3c282 | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 49 | mLastSet[i] = false; |
| 50 | mLast[i] = mLogElements.begin(); |
| 51 | |
Mark Salyzyn | f4693f3 | 2016-09-27 13:08:23 -0700 | [diff] [blame] | 52 | if (setSize(i, __android_logger_get_buffer_size(i))) { |
Mark Salyzyn | 7b85c59 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 53 | setSize(i, LOG_BUFFER_MIN_SIZE); |
| 54 | } |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 55 | } |
Mark Salyzyn | 2b4a763 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 56 | bool lastMonotonic = monotonic; |
Mark Salyzyn | 2d4f9bc | 2015-12-01 15:57:25 -0800 | [diff] [blame] | 57 | monotonic = android_log_clockid() == CLOCK_MONOTONIC; |
Mark Salyzyn | 552b475 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 58 | if (lastMonotonic != monotonic) { |
| 59 | // |
| 60 | // Fixup all timestamps, may not be 100% accurate, but better than |
| 61 | // throwing what we have away when we get 'surprised' by a change. |
| 62 | // In-place element fixup so no need to check reader-lock. Entries |
| 63 | // should already be in timestamp order, but we could end up with a |
| 64 | // few out-of-order entries if new monotonics come in before we |
| 65 | // are notified of the reinit change in status. A Typical example would |
| 66 | // be: |
| 67 | // --------- beginning of system |
| 68 | // 10.494082 184 201 D Cryptfs : Just triggered post_fs_data |
| 69 | // --------- beginning of kernel |
| 70 | // 0.000000 0 0 I : Initializing cgroup subsys |
| 71 | // as the act of mounting /data would trigger persist.logd.timestamp to |
| 72 | // be corrected. 1/30 corner case YMMV. |
| 73 | // |
| 74 | pthread_mutex_lock(&mLogElementsLock); |
| 75 | LogBufferElementCollection::iterator it = mLogElements.begin(); |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 76 | while ((it != mLogElements.end())) { |
| 77 | LogBufferElement* e = *it; |
Mark Salyzyn | 552b475 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 78 | if (monotonic) { |
| 79 | if (!android::isMonotonic(e->mRealTime)) { |
| 80 | LogKlog::convertRealToMonotonic(e->mRealTime); |
| 81 | } |
| 82 | } else { |
| 83 | if (android::isMonotonic(e->mRealTime)) { |
| 84 | LogKlog::convertMonotonicToReal(e->mRealTime); |
| 85 | } |
| 86 | } |
| 87 | ++it; |
| 88 | } |
| 89 | pthread_mutex_unlock(&mLogElementsLock); |
Mark Salyzyn | 2b4a763 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Mark Salyzyn | 552b475 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 92 | // We may have been triggered by a SIGHUP. Release any sleeping reader |
| 93 | // threads to dump their current content. |
Mark Salyzyn | 2b4a763 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 94 | // |
Mark Salyzyn | 552b475 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 95 | // NB: this is _not_ performed in the context of a SIGHUP, it is |
| 96 | // performed during startup, and in context of reinit administrative thread |
| 97 | LogTimeEntry::lock(); |
| 98 | |
| 99 | LastLogTimes::iterator times = mTimes.begin(); |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 100 | while (times != mTimes.end()) { |
| 101 | LogTimeEntry* entry = (*times); |
Mark Salyzyn | 552b475 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 102 | if (entry->owned_Locked()) { |
| 103 | entry->triggerReader_Locked(); |
Mark Salyzyn | 2b4a763 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 104 | } |
Mark Salyzyn | 552b475 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 105 | times++; |
Mark Salyzyn | 2b4a763 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 106 | } |
Mark Salyzyn | 552b475 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 107 | |
| 108 | LogTimeEntry::unlock(); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 111 | LogBuffer::LogBuffer(LastLogTimes* times) |
| 112 | : monotonic(android_log_clockid() == CLOCK_MONOTONIC), mTimes(*times) { |
Mark Salyzyn | 3fe2593 | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 113 | pthread_mutex_init(&mLogElementsLock, NULL); |
| 114 | |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 115 | log_id_for_each(i) { |
| 116 | lastLoggedElements[i] = NULL; |
| 117 | droppedElements[i] = NULL; |
| 118 | } |
| 119 | |
Mark Salyzyn | 3fe2593 | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 120 | init(); |
| 121 | } |
| 122 | |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 123 | LogBuffer::~LogBuffer() { |
| 124 | log_id_for_each(i) { |
| 125 | delete lastLoggedElements[i]; |
| 126 | delete droppedElements[i]; |
| 127 | } |
| 128 | } |
| 129 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 130 | enum match_type { DIFFERENT, SAME, SAME_LIBLOG }; |
Mark Salyzyn | edb18aa | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 131 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 132 | static enum match_type identical(LogBufferElement* elem, |
| 133 | LogBufferElement* last) { |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 134 | // is it mostly identical? |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 135 | // if (!elem) return DIFFERENT; |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 136 | unsigned short lenl = elem->getMsgLen(); |
Mark Salyzyn | edb18aa | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 137 | if (!lenl) return DIFFERENT; |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 138 | // if (!last) return DIFFERENT; |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 139 | unsigned short lenr = last->getMsgLen(); |
Mark Salyzyn | edb18aa | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 140 | if (!lenr) return DIFFERENT; |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 141 | // if (elem->getLogId() != last->getLogId()) return DIFFERENT; |
Mark Salyzyn | edb18aa | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 142 | if (elem->getUid() != last->getUid()) return DIFFERENT; |
| 143 | if (elem->getPid() != last->getPid()) return DIFFERENT; |
| 144 | if (elem->getTid() != last->getTid()) return DIFFERENT; |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 145 | |
| 146 | // last is more than a minute old, stop squashing identical messages |
| 147 | if (elem->getRealTime().nsec() > |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 148 | (last->getRealTime().nsec() + 60 * NS_PER_SEC)) |
| 149 | return DIFFERENT; |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 150 | |
| 151 | // Identical message |
| 152 | const char* msgl = elem->getMsg(); |
| 153 | const char* msgr = last->getMsg(); |
Mark Salyzyn | edb18aa | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 154 | if (lenl == lenr) { |
| 155 | if (!fastcmp<memcmp>(msgl, msgr, lenl)) return SAME; |
| 156 | // liblog tagged messages (content gets summed) |
| 157 | if ((elem->getLogId() == LOG_ID_EVENTS) && |
| 158 | (lenl == sizeof(android_log_event_int_t)) && |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 159 | !fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_int_t) - |
| 160 | sizeof(int32_t)) && |
| 161 | (elem->getTag() == LIBLOG_LOG_TAG)) |
| 162 | return SAME_LIBLOG; |
Mark Salyzyn | edb18aa | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 163 | } |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 164 | |
| 165 | // audit message (except sequence number) identical? |
| 166 | static const char avc[] = "): avc: "; |
| 167 | |
| 168 | if (last->isBinary()) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 169 | if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) - |
| 170 | sizeof(int32_t))) |
| 171 | return DIFFERENT; |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 172 | msgl += sizeof(android_log_event_string_t); |
| 173 | lenl -= sizeof(android_log_event_string_t); |
| 174 | msgr += sizeof(android_log_event_string_t); |
| 175 | lenr -= sizeof(android_log_event_string_t); |
| 176 | } |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 177 | const char* avcl = android::strnstr(msgl, lenl, avc); |
Mark Salyzyn | edb18aa | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 178 | if (!avcl) return DIFFERENT; |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 179 | lenl -= avcl - msgl; |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 180 | const char* avcr = android::strnstr(msgr, lenr, avc); |
Mark Salyzyn | edb18aa | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 181 | if (!avcr) return DIFFERENT; |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 182 | lenr -= avcr - msgr; |
Mark Salyzyn | edb18aa | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 183 | if (lenl != lenr) return DIFFERENT; |
Evgenii Stepanov | 77e48b6 | 2017-03-14 14:47:25 -0700 | [diff] [blame] | 184 | // TODO: After b/35468874 is addressed, revisit "lenl > strlen(avc)" |
| 185 | // condition, it might become superflous. |
| 186 | if (lenl > strlen(avc) && |
| 187 | fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc), |
| 188 | lenl - strlen(avc))) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 189 | return DIFFERENT; |
Evgenii Stepanov | 77e48b6 | 2017-03-14 14:47:25 -0700 | [diff] [blame] | 190 | } |
Mark Salyzyn | edb18aa | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 191 | return SAME; |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 194 | int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, |
| 195 | pid_t tid, const char* msg, unsigned short len) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 196 | if ((log_id >= LOG_ID_MAX) || (log_id < 0)) { |
Mark Salyzyn | 88fe7cc | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 197 | return -EINVAL; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 198 | } |
Mark Salyzyn | 19c9111 | 2014-10-02 13:07:05 -0700 | [diff] [blame] | 199 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 200 | LogBufferElement* elem = |
| 201 | new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len); |
Mark Salyzyn | 4a8d250 | 2016-01-06 21:17:43 +0000 | [diff] [blame] | 202 | if (log_id != LOG_ID_SECURITY) { |
Mark Salyzyn | 8885daf | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 203 | int prio = ANDROID_LOG_INFO; |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 204 | const char* tag = NULL; |
Mark Salyzyn | 8885daf | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 205 | if (log_id == LOG_ID_EVENTS) { |
Mark Salyzyn | db9d0f6 | 2016-09-12 14:51:54 -0700 | [diff] [blame] | 206 | tag = tagToName(elem->getTag()); |
Mark Salyzyn | 8885daf | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 207 | } else { |
| 208 | prio = *msg; |
| 209 | tag = msg + 1; |
| 210 | } |
Mark Salyzyn | db9d0f6 | 2016-09-12 14:51:54 -0700 | [diff] [blame] | 211 | if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) { |
Mark Salyzyn | 8885daf | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 212 | // Log traffic received to total |
| 213 | pthread_mutex_lock(&mLogElementsLock); |
| 214 | stats.add(elem); |
| 215 | stats.subtract(elem); |
| 216 | pthread_mutex_unlock(&mLogElementsLock); |
| 217 | delete elem; |
| 218 | return -EACCES; |
| 219 | } |
Mark Salyzyn | 19c9111 | 2014-10-02 13:07:05 -0700 | [diff] [blame] | 220 | } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 221 | |
| 222 | pthread_mutex_lock(&mLogElementsLock); |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 223 | LogBufferElement* currentLast = lastLoggedElements[log_id]; |
| 224 | if (currentLast) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 225 | LogBufferElement* dropped = droppedElements[log_id]; |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 226 | unsigned short count = dropped ? dropped->getDropped() : 0; |
Mark Salyzyn | 9361c0b | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 227 | // |
| 228 | // State Init |
| 229 | // incoming: |
| 230 | // dropped = NULL |
| 231 | // currentLast = NULL; |
| 232 | // elem = incoming message |
| 233 | // outgoing: |
| 234 | // dropped = NULL -> State 0 |
| 235 | // currentLast = copy of elem |
| 236 | // log elem |
| 237 | // State 0 |
| 238 | // incoming: |
| 239 | // count = 0 |
| 240 | // dropped = NULL |
| 241 | // currentLast = copy of last message |
| 242 | // elem = incoming message |
Mark Salyzyn | edb18aa | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 243 | // outgoing: if match != DIFFERENT |
Mark Salyzyn | 9361c0b | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 244 | // dropped = copy of first identical message -> State 1 |
| 245 | // currentLast = reference to elem |
Mark Salyzyn | edb18aa | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 246 | // break: if match == DIFFERENT |
Mark Salyzyn | 9361c0b | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 247 | // dropped = NULL -> State 0 |
| 248 | // delete copy of last message (incoming currentLast) |
| 249 | // currentLast = copy of elem |
| 250 | // log elem |
| 251 | // State 1 |
| 252 | // incoming: |
| 253 | // count = 0 |
| 254 | // dropped = copy of first identical message |
| 255 | // currentLast = reference to last held-back incoming |
| 256 | // message |
| 257 | // elem = incoming message |
Mark Salyzyn | edb18aa | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 258 | // outgoing: if match == SAME |
Mark Salyzyn | 9361c0b | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 259 | // delete copy of first identical message (dropped) |
| 260 | // dropped = reference to last held-back incoming |
| 261 | // message set to chatty count of 1 -> State 2 |
| 262 | // currentLast = reference to elem |
Mark Salyzyn | edb18aa | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 263 | // outgoing: if match == SAME_LIBLOG |
| 264 | // dropped = copy of first identical message -> State 1 |
| 265 | // take sum of currentLast and elem |
| 266 | // if sum overflows: |
| 267 | // log currentLast |
| 268 | // currentLast = reference to elem |
| 269 | // else |
| 270 | // delete currentLast |
| 271 | // currentLast = reference to elem, sum liblog. |
| 272 | // break: if match == DIFFERENT |
Mark Salyzyn | 9361c0b | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 273 | // delete dropped |
| 274 | // dropped = NULL -> State 0 |
| 275 | // log reference to last held-back (currentLast) |
| 276 | // currentLast = copy of elem |
| 277 | // log elem |
| 278 | // State 2 |
| 279 | // incoming: |
| 280 | // count = chatty count |
| 281 | // dropped = chatty message holding count |
| 282 | // currentLast = reference to last held-back incoming |
| 283 | // message. |
| 284 | // dropped = chatty message holding count |
| 285 | // elem = incoming message |
Mark Salyzyn | edb18aa | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 286 | // outgoing: if match != DIFFERENT |
Mark Salyzyn | 9361c0b | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 287 | // delete chatty message holding count |
| 288 | // dropped = reference to last held-back incoming |
| 289 | // message, set to chatty count + 1 |
| 290 | // currentLast = reference to elem |
Mark Salyzyn | edb18aa | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 291 | // break: if match == DIFFERENT |
Mark Salyzyn | 9361c0b | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 292 | // log dropped (chatty message) |
| 293 | // dropped = NULL -> State 0 |
| 294 | // log reference to last held-back (currentLast) |
| 295 | // currentLast = copy of elem |
| 296 | // log elem |
| 297 | // |
Mark Salyzyn | edb18aa | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 298 | enum match_type match = identical(elem, currentLast); |
| 299 | if (match != DIFFERENT) { |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 300 | if (dropped) { |
Mark Salyzyn | edb18aa | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 301 | // Sum up liblog tag messages? |
| 302 | if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) { |
| 303 | android_log_event_int_t* event = |
| 304 | reinterpret_cast<android_log_event_int_t*>( |
| 305 | const_cast<char*>(currentLast->getMsg())); |
| 306 | // |
| 307 | // To unit test, differentiate with something like: |
| 308 | // event->header.tag = htole32(CHATTY_LOG_TAG); |
| 309 | // here, then instead of delete currentLast below, |
| 310 | // log(currentLast) to see the incremental sums form. |
| 311 | // |
| 312 | uint32_t swab = event->payload.data; |
| 313 | unsigned long long total = htole32(swab); |
| 314 | event = reinterpret_cast<android_log_event_int_t*>( |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 315 | const_cast<char*>(elem->getMsg())); |
Mark Salyzyn | edb18aa | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 316 | swab = event->payload.data; |
| 317 | |
| 318 | lastLoggedElements[LOG_ID_EVENTS] = elem; |
| 319 | total += htole32(swab); |
| 320 | // check for overflow |
| 321 | if (total >= UINT32_MAX) { |
| 322 | log(currentLast); |
| 323 | pthread_mutex_unlock(&mLogElementsLock); |
| 324 | return len; |
| 325 | } |
| 326 | stats.add(currentLast); |
| 327 | stats.subtract(currentLast); |
| 328 | delete currentLast; |
| 329 | swab = total; |
| 330 | event->payload.data = htole32(swab); |
| 331 | pthread_mutex_unlock(&mLogElementsLock); |
| 332 | return len; |
| 333 | } |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 334 | if (count == USHRT_MAX) { |
| 335 | log(dropped); |
| 336 | count = 1; |
| 337 | } else { |
| 338 | delete dropped; |
| 339 | ++count; |
| 340 | } |
| 341 | } |
| 342 | if (count) { |
| 343 | stats.add(currentLast); |
| 344 | stats.subtract(currentLast); |
| 345 | currentLast->setDropped(count); |
| 346 | } |
| 347 | droppedElements[log_id] = currentLast; |
| 348 | lastLoggedElements[log_id] = elem; |
| 349 | pthread_mutex_unlock(&mLogElementsLock); |
| 350 | return len; |
| 351 | } |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 352 | if (dropped) { // State 1 or 2 |
| 353 | if (count) { // State 2 |
| 354 | log(dropped); // report chatty |
| 355 | } else { // State 1 |
| 356 | delete dropped; |
Mark Salyzyn | 9361c0b | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 357 | } |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 358 | droppedElements[log_id] = NULL; |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 359 | log(currentLast); // report last message in the series |
| 360 | } else { // State 0 |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 361 | delete currentLast; |
| 362 | } |
| 363 | } |
| 364 | lastLoggedElements[log_id] = new LogBufferElement(*elem); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 365 | |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 366 | log(elem); |
| 367 | pthread_mutex_unlock(&mLogElementsLock); |
| 368 | |
| 369 | return len; |
| 370 | } |
| 371 | |
| 372 | // assumes mLogElementsLock held, owns elem, will look after garbage collection |
| 373 | void LogBuffer::log(LogBufferElement* elem) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 374 | // Insert elements in time sorted order if possible |
| 375 | // NB: if end is region locked, place element at end of list |
| 376 | LogBufferElementCollection::iterator it = mLogElements.end(); |
| 377 | LogBufferElementCollection::iterator last = it; |
Mark Salyzyn | 399d5b9 | 2017-03-03 10:21:23 -0800 | [diff] [blame^] | 378 | if (__predict_true(it != mLogElements.begin())) --it; |
| 379 | if (__predict_false(it == mLogElements.begin()) || |
| 380 | __predict_true((*it)->getRealTime() <= elem->getRealTime())) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 381 | mLogElements.push_back(elem); |
| 382 | } else { |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 383 | uint64_t end = 1; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 384 | bool end_set = false; |
| 385 | bool end_always = false; |
| 386 | |
| 387 | LogTimeEntry::lock(); |
| 388 | |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 389 | LastLogTimes::iterator times = mTimes.begin(); |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 390 | while (times != mTimes.end()) { |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 391 | LogTimeEntry* entry = (*times); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 392 | if (entry->owned_Locked()) { |
| 393 | if (!entry->mNonBlock) { |
| 394 | end_always = true; |
| 395 | break; |
| 396 | } |
Mark Salyzyn | 399d5b9 | 2017-03-03 10:21:23 -0800 | [diff] [blame^] | 397 | // it passing mEnd is blocked by the following checks. |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 398 | if (!end_set || (end <= entry->mEnd)) { |
| 399 | end = entry->mEnd; |
| 400 | end_set = true; |
| 401 | } |
| 402 | } |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 403 | times++; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Mark Salyzyn | 399d5b9 | 2017-03-03 10:21:23 -0800 | [diff] [blame^] | 406 | if (end_always || (end_set && (end > (*it)->getSequence()))) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 407 | mLogElements.push_back(elem); |
| 408 | } else { |
Mark Salyzyn | 399d5b9 | 2017-03-03 10:21:23 -0800 | [diff] [blame^] | 409 | // should be short as timestamps are localized near end() |
| 410 | do { |
| 411 | last = it; |
| 412 | if (__predict_false(it == mLogElements.begin())) { |
| 413 | break; |
| 414 | } |
| 415 | |
| 416 | std::swap((*it)->mSequence, elem->mSequence); |
| 417 | |
| 418 | --it; |
| 419 | } while (((*it)->getRealTime() > elem->getRealTime()) && |
| 420 | (!end_set || (end <= (*it)->getSequence()))); |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 421 | mLogElements.insert(last, elem); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 422 | } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 423 | LogTimeEntry::unlock(); |
| 424 | } |
| 425 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 426 | stats.add(elem); |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 427 | maybePrune(elem->getLogId()); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Mark Salyzyn | 95ff248 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 430 | // Prune at most 10% of the log entries or maxPrune, whichever is less. |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 431 | // |
| 432 | // mLogElementsLock must be held when this function is called. |
| 433 | void LogBuffer::maybePrune(log_id_t id) { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 434 | size_t sizes = stats.sizes(id); |
Mark Salyzyn | 4436d4c | 2015-08-10 10:23:56 -0700 | [diff] [blame] | 435 | unsigned long maxSize = log_buffer_size(id); |
| 436 | if (sizes > maxSize) { |
Mark Salyzyn | 6374572 | 2015-08-19 12:20:36 -0700 | [diff] [blame] | 437 | size_t sizeOver = sizes - ((maxSize * 9) / 10); |
Mark Salyzyn | d745c72 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 438 | size_t elements = stats.realElements(id); |
| 439 | size_t minElements = elements / 100; |
| 440 | if (minElements < minPrune) { |
| 441 | minElements = minPrune; |
| 442 | } |
Mark Salyzyn | 4436d4c | 2015-08-10 10:23:56 -0700 | [diff] [blame] | 443 | unsigned long pruneRows = elements * sizeOver / sizes; |
Mark Salyzyn | 95ff248 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 444 | if (pruneRows < minElements) { |
Mark Salyzyn | 4436d4c | 2015-08-10 10:23:56 -0700 | [diff] [blame] | 445 | pruneRows = minElements; |
Mark Salyzyn | 9d4e34e | 2014-01-13 16:37:51 -0800 | [diff] [blame] | 446 | } |
Mark Salyzyn | 95ff248 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 447 | if (pruneRows > maxPrune) { |
| 448 | pruneRows = maxPrune; |
Mark Salyzyn | 6374572 | 2015-08-19 12:20:36 -0700 | [diff] [blame] | 449 | } |
Mark Salyzyn | 9d4e34e | 2014-01-13 16:37:51 -0800 | [diff] [blame] | 450 | prune(id, pruneRows); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
Mark Salyzyn | 57b82a6 | 2015-09-03 16:08:50 -0700 | [diff] [blame] | 454 | LogBufferElementCollection::iterator LogBuffer::erase( |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 455 | LogBufferElementCollection::iterator it, bool coalesce) { |
| 456 | LogBufferElement* element = *it; |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 457 | log_id_t id = element->getLogId(); |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 458 | |
Mark Salyzyn | 628d937 | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 459 | // Remove iterator references in the various lists that will become stale |
| 460 | // after the element is erased from the main logging list. |
| 461 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 462 | { // start of scope for found iterator |
| 463 | int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) |
| 464 | ? element->getTag() |
| 465 | : element->getUid(); |
Mark Salyzyn | c572fc6 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 466 | LogBufferIteratorMap::iterator found = mLastWorst[id].find(key); |
| 467 | if ((found != mLastWorst[id].end()) && (it == found->second)) { |
| 468 | mLastWorst[id].erase(found); |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 469 | } |
Mark Salyzyn | cfcffdaf | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 470 | } |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 471 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 472 | { // start of scope for pid found iterator |
Mark Salyzyn | 628d937 | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 473 | // element->getUid() may not be AID_SYSTEM for next-best-watermark. |
Mark Salyzyn | 2e86fa4 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 474 | // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and |
| 475 | // long term code stability, find() check should be fast for those ids. |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 476 | LogBufferPidIteratorMap::iterator found = |
| 477 | mLastWorstPidOfSystem[id].find(element->getPid()); |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 478 | if ((found != mLastWorstPidOfSystem[id].end()) && |
| 479 | (it == found->second)) { |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 480 | mLastWorstPidOfSystem[id].erase(found); |
| 481 | } |
| 482 | } |
| 483 | |
Mark Salyzyn | d701b78 | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 484 | bool setLast[LOG_ID_MAX]; |
| 485 | bool doSetLast = false; |
| 486 | log_id_for_each(i) { |
| 487 | doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]); |
| 488 | } |
Mark Salyzyn | 21fc393 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 489 | #ifdef DEBUG_CHECK_FOR_STALE_ENTRIES |
| 490 | LogBufferElementCollection::iterator bad = it; |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 491 | int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) |
| 492 | ? element->getTag() |
| 493 | : element->getUid(); |
Mark Salyzyn | 21fc393 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 494 | #endif |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 495 | it = mLogElements.erase(it); |
Mark Salyzyn | d701b78 | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 496 | if (doSetLast) { |
| 497 | log_id_for_each(i) { |
| 498 | if (setLast[i]) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 499 | if (__predict_false(it == mLogElements.end())) { // impossible |
Mark Salyzyn | d701b78 | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 500 | mLastSet[i] = false; |
Mark Salyzyn | 2e86fa4 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 501 | mLast[i] = mLogElements.begin(); |
Mark Salyzyn | d701b78 | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 502 | } else { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 503 | mLast[i] = it; // push down the road as next-best-watermark |
Mark Salyzyn | d701b78 | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 504 | } |
| 505 | } |
Mark Salyzyn | 7a3c282 | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 506 | } |
| 507 | } |
Mark Salyzyn | 21fc393 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 508 | #ifdef DEBUG_CHECK_FOR_STALE_ENTRIES |
| 509 | log_id_for_each(i) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 510 | for (auto b : mLastWorst[i]) { |
Mark Salyzyn | 21fc393 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 511 | if (bad == b.second) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 512 | android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i, |
| 513 | b.first, key); |
Mark Salyzyn | 21fc393 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 514 | } |
| 515 | } |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 516 | for (auto b : mLastWorstPidOfSystem[i]) { |
Mark Salyzyn | 21fc393 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 517 | if (bad == b.second) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 518 | android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i, |
| 519 | b.first); |
Mark Salyzyn | 21fc393 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | if (mLastSet[i] && (bad == mLast[i])) { |
| 523 | android::prdebug("stale mLast[%d]\n", i); |
| 524 | mLastSet[i] = false; |
| 525 | mLast[i] = mLogElements.begin(); |
| 526 | } |
| 527 | } |
| 528 | #endif |
Mark Salyzyn | 95ff248 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 529 | if (coalesce) { |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 530 | stats.erase(element); |
Mark Salyzyn | 95ff248 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 531 | } else { |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 532 | stats.subtract(element); |
Mark Salyzyn | 57b82a6 | 2015-09-03 16:08:50 -0700 | [diff] [blame] | 533 | } |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 534 | delete element; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 535 | |
| 536 | return it; |
| 537 | } |
| 538 | |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 539 | // Define a temporary mechanism to report the last LogBufferElement pointer |
| 540 | // for the specified uid, pid and tid. Used below to help merge-sort when |
| 541 | // pruning for worst UID. |
| 542 | class LogBufferElementKey { |
| 543 | const union { |
| 544 | struct { |
Mark Salyzyn | c459560 | 2016-12-13 12:44:20 -0800 | [diff] [blame] | 545 | uint32_t uid; |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 546 | uint16_t pid; |
| 547 | uint16_t tid; |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 548 | } __packed; |
| 549 | uint64_t value; |
| 550 | } __packed; |
| 551 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 552 | public: |
| 553 | LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid) |
| 554 | : uid(uid), pid(pid), tid(tid) { |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 555 | } |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 556 | explicit LogBufferElementKey(uint64_t key) : value(key) { |
| 557 | } |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 558 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 559 | uint64_t getKey() { |
| 560 | return value; |
| 561 | } |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 562 | }; |
| 563 | |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 564 | class LogBufferElementLast { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 565 | typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap; |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 566 | LogBufferElementMap map; |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 567 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 568 | public: |
| 569 | bool coalesce(LogBufferElement* element, unsigned short dropped) { |
| 570 | LogBufferElementKey key(element->getUid(), element->getPid(), |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 571 | element->getTid()); |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 572 | LogBufferElementMap::iterator it = map.find(key.getKey()); |
| 573 | if (it != map.end()) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 574 | LogBufferElement* found = it->second; |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 575 | unsigned short moreDropped = found->getDropped(); |
| 576 | if ((dropped + moreDropped) > USHRT_MAX) { |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 577 | map.erase(it); |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 578 | } else { |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 579 | found->setDropped(dropped + moreDropped); |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 580 | return true; |
| 581 | } |
| 582 | } |
| 583 | return false; |
| 584 | } |
| 585 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 586 | void add(LogBufferElement* element) { |
| 587 | LogBufferElementKey key(element->getUid(), element->getPid(), |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 588 | element->getTid()); |
| 589 | map[key.getKey()] = element; |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Mark Salyzyn | be1a30c | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 592 | inline void clear() { |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 593 | map.clear(); |
Mark Salyzyn | be1a30c | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 596 | void clear(LogBufferElement* element) { |
| 597 | uint64_t current = |
| 598 | element->getRealTime().nsec() - (EXPIRE_RATELIMIT * NS_PER_SEC); |
| 599 | for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) { |
| 600 | LogBufferElement* mapElement = it->second; |
| 601 | if ((mapElement->getDropped() >= EXPIRE_THRESHOLD) && |
| 602 | (current > mapElement->getRealTime().nsec())) { |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 603 | it = map.erase(it); |
| 604 | } else { |
| 605 | ++it; |
Mark Salyzyn | be1a30c | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | } |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 609 | }; |
| 610 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 611 | // prune "pruneRows" of type "id" from the buffer. |
| 612 | // |
Mark Salyzyn | 50e2108 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 613 | // This garbage collection task is used to expire log entries. It is called to |
| 614 | // remove all logs (clear), all UID logs (unprivileged clear), or every |
| 615 | // 256 or 10% of the total logs (whichever is less) to prune the logs. |
| 616 | // |
| 617 | // First there is a prep phase where we discover the reader region lock that |
| 618 | // acts as a backstop to any pruning activity to stop there and go no further. |
| 619 | // |
| 620 | // There are three major pruning loops that follow. All expire from the oldest |
| 621 | // entries. Since there are multiple log buffers, the Android logging facility |
| 622 | // will appear to drop entries 'in the middle' when looking at multiple log |
| 623 | // sources and buffers. This effect is slightly more prominent when we prune |
| 624 | // the worst offender by logging source. Thus the logs slowly loose content |
| 625 | // and value as you move back in time. This is preferred since chatty sources |
| 626 | // invariably move the logs value down faster as less chatty sources would be |
| 627 | // expired in the noise. |
| 628 | // |
| 629 | // The first loop performs blacklisting and worst offender pruning. Falling |
| 630 | // through when there are no notable worst offenders and have not hit the |
| 631 | // region lock preventing further worst offender pruning. This loop also looks |
| 632 | // after managing the chatty log entries and merging to help provide |
| 633 | // statistical basis for blame. The chatty entries are not a notification of |
| 634 | // how much logs you may have, but instead represent how much logs you would |
| 635 | // have had in a virtual log buffer that is extended to cover all the in-memory |
| 636 | // logs without loss. They last much longer than the represented pruned logs |
| 637 | // since they get multiplied by the gains in the non-chatty log sources. |
| 638 | // |
| 639 | // The second loop get complicated because an algorithm of watermarks and |
| 640 | // history is maintained to reduce the order and keep processing time |
| 641 | // down to a minimum at scale. These algorithms can be costly in the face |
| 642 | // of larger log buffers, or severly limited processing time granted to a |
| 643 | // background task at lowest priority. |
| 644 | // |
| 645 | // This second loop does straight-up expiration from the end of the logs |
| 646 | // (again, remember for the specified log buffer id) but does some whitelist |
| 647 | // preservation. Thus whitelist is a Hail Mary low priority, blacklists and |
| 648 | // spam filtration all take priority. This second loop also checks if a region |
| 649 | // lock is causing us to buffer too much in the logs to help the reader(s), |
| 650 | // and will tell the slowest reader thread to skip log entries, and if |
| 651 | // persistent and hits a further threshold, kill the reader thread. |
| 652 | // |
| 653 | // The third thread is optional, and only gets hit if there was a whitelist |
| 654 | // and more needs to be pruned against the backstop of the region lock. |
| 655 | // |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 656 | // mLogElementsLock must be held when this function is called. |
Mark Salyzyn | 50e2108 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 657 | // |
Mark Salyzyn | 7702c3e | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 658 | bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 659 | LogTimeEntry* oldest = NULL; |
Mark Salyzyn | 7702c3e | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 660 | bool busy = false; |
Mark Salyzyn | d280d81 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 661 | bool clearAll = pruneRows == ULONG_MAX; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 662 | |
| 663 | LogTimeEntry::lock(); |
| 664 | |
| 665 | // Region locked? |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 666 | LastLogTimes::iterator times = mTimes.begin(); |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 667 | while (times != mTimes.end()) { |
| 668 | LogTimeEntry* entry = (*times); |
| 669 | if (entry->owned_Locked() && entry->isWatching(id) && |
| 670 | (!oldest || (oldest->mStart > entry->mStart) || |
| 671 | ((oldest->mStart == entry->mStart) && |
| 672 | (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 673 | oldest = entry; |
| 674 | } |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 675 | times++; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 676 | } |
| 677 | |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 678 | LogBufferElementCollection::iterator it; |
| 679 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 680 | if (__predict_false(caller_uid != AID_ROOT)) { // unlikely |
Mark Salyzyn | 4e23c6e | 2016-09-01 15:48:36 -0700 | [diff] [blame] | 681 | // Only here if clear all request from non system source, so chatty |
| 682 | // filter logistics is not required. |
Mark Salyzyn | 7a3c282 | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 683 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
| 684 | while (it != mLogElements.end()) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 685 | LogBufferElement* element = *it; |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 686 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 687 | if ((element->getLogId() != id) || |
| 688 | (element->getUid() != caller_uid)) { |
Mark Salyzyn | d280d81 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 689 | ++it; |
| 690 | continue; |
| 691 | } |
| 692 | |
Mark Salyzyn | 7a3c282 | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 693 | if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) { |
| 694 | mLast[id] = it; |
| 695 | mLastSet[id] = true; |
| 696 | } |
| 697 | |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 698 | if (oldest && (oldest->mStart <= element->getSequence())) { |
Mark Salyzyn | 7702c3e | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 699 | busy = true; |
Mark Salyzyn | 552b475 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 700 | if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) { |
| 701 | oldest->triggerReader_Locked(); |
| 702 | } else { |
| 703 | oldest->triggerSkip_Locked(id, pruneRows); |
| 704 | } |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 705 | break; |
| 706 | } |
| 707 | |
Mark Salyzyn | d280d81 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 708 | it = erase(it); |
Mark Salyzyn | 4e23c6e | 2016-09-01 15:48:36 -0700 | [diff] [blame] | 709 | if (--pruneRows == 0) { |
| 710 | break; |
| 711 | } |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 712 | } |
| 713 | LogTimeEntry::unlock(); |
Mark Salyzyn | 7702c3e | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 714 | return busy; |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 717 | // prune by worst offenders; by blacklist, UID, and by PID of system UID |
Mark Salyzyn | 8885daf | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 718 | bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty(); |
Mark Salyzyn | d280d81 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 719 | while (!clearAll && (pruneRows > 0)) { |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 720 | // recalculate the worst offender on every batched pass |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 721 | int worst = -1; // not valid for getUid() or getKey() |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 722 | size_t worst_sizes = 0; |
| 723 | size_t second_worst_sizes = 0; |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 724 | pid_t worstPid = 0; // POSIX guarantees PID != 0 |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 725 | |
Mark Salyzyn | 0e765c6 | 2015-03-17 17:17:25 -0700 | [diff] [blame] | 726 | if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) { |
Mark Salyzyn | c572fc6 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 727 | // Calculate threshold as 12.5% of available storage |
| 728 | size_t threshold = log_buffer_size(id) / 8; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 729 | |
Mark Salyzyn | c572fc6 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 730 | if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 731 | stats.sortTags(AID_ROOT, (pid_t)0, 2, id) |
| 732 | .findWorst(worst, worst_sizes, second_worst_sizes, |
| 733 | threshold); |
Mark Salyzyn | 2e86fa4 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 734 | // per-pid filter for AID_SYSTEM sources is too complex |
Mark Salyzyn | c572fc6 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 735 | } else { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 736 | stats.sort(AID_ROOT, (pid_t)0, 2, id) |
| 737 | .findWorst(worst, worst_sizes, second_worst_sizes, |
| 738 | threshold); |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 739 | |
Mark Salyzyn | c572fc6 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 740 | if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 741 | stats.sortPids(worst, (pid_t)0, 2, id) |
| 742 | .findWorst(worstPid, worst_sizes, second_worst_sizes); |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 743 | } |
| 744 | } |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 745 | } |
| 746 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 747 | // skip if we have neither worst nor naughty filters |
Mark Salyzyn | c572fc6 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 748 | if ((worst == -1) && !hasBlacklist) { |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 749 | break; |
| 750 | } |
| 751 | |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 752 | bool kick = false; |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 753 | bool leading = true; |
Mark Salyzyn | 7a3c282 | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 754 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
Mark Salyzyn | 50e2108 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 755 | // Perform at least one mandatory garbage collection cycle in following |
| 756 | // - clear leading chatty tags |
Mark Salyzyn | 95ff248 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 757 | // - coalesce chatty tags |
Mark Salyzyn | 50e2108 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 758 | // - check age-out of preserved logs |
| 759 | bool gc = pruneRows <= 1; |
Mark Salyzyn | c572fc6 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 760 | if (!gc && (worst != -1)) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 761 | { // begin scope for worst found iterator |
| 762 | LogBufferIteratorMap::iterator found = |
| 763 | mLastWorst[id].find(worst); |
| 764 | if ((found != mLastWorst[id].end()) && |
| 765 | (found->second != mLogElements.end())) { |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 766 | leading = false; |
| 767 | it = found->second; |
| 768 | } |
| 769 | } |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 770 | if (worstPid) { // begin scope for pid worst found iterator |
Mark Salyzyn | 2e86fa4 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 771 | // FYI: worstPid only set if !LOG_ID_EVENTS and |
| 772 | // !LOG_ID_SECURITY, not going to make that assumption ... |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 773 | LogBufferPidIteratorMap::iterator found = |
| 774 | mLastWorstPidOfSystem[id].find(worstPid); |
| 775 | if ((found != mLastWorstPidOfSystem[id].end()) && |
| 776 | (found->second != mLogElements.end())) { |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 777 | leading = false; |
| 778 | it = found->second; |
| 779 | } |
Mark Salyzyn | cfcffdaf | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 780 | } |
| 781 | } |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 782 | static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 }; |
Mark Salyzyn | 4e29e17 | 2015-08-24 13:43:27 -0700 | [diff] [blame] | 783 | LogBufferElementCollection::iterator lastt; |
| 784 | lastt = mLogElements.end(); |
| 785 | --lastt; |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 786 | LogBufferElementLast last; |
Mark Salyzyn | cfcffdaf | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 787 | while (it != mLogElements.end()) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 788 | LogBufferElement* element = *it; |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 789 | |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 790 | if (oldest && (oldest->mStart <= element->getSequence())) { |
Mark Salyzyn | 7702c3e | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 791 | busy = true; |
Mark Salyzyn | 552b475 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 792 | if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) { |
| 793 | oldest->triggerReader_Locked(); |
| 794 | } |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 795 | break; |
| 796 | } |
| 797 | |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 798 | if (element->getLogId() != id) { |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 799 | ++it; |
| 800 | continue; |
| 801 | } |
Mark Salyzyn | 628d937 | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 802 | // below this point element->getLogId() == id |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 803 | |
Mark Salyzyn | 7a3c282 | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 804 | if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) { |
| 805 | mLast[id] = it; |
| 806 | mLastSet[id] = true; |
| 807 | } |
| 808 | |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 809 | unsigned short dropped = element->getDropped(); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 810 | |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 811 | // remove any leading drops |
| 812 | if (leading && dropped) { |
| 813 | it = erase(it); |
| 814 | continue; |
| 815 | } |
| 816 | |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 817 | if (dropped && last.coalesce(element, dropped)) { |
Mark Salyzyn | 95ff248 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 818 | it = erase(it, true); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 819 | continue; |
| 820 | } |
| 821 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 822 | int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) |
| 823 | ? element->getTag() |
| 824 | : element->getUid(); |
Mark Salyzyn | c572fc6 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 825 | |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 826 | if (hasBlacklist && mPrune.naughty(element)) { |
| 827 | last.clear(element); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 828 | it = erase(it); |
| 829 | if (dropped) { |
| 830 | continue; |
| 831 | } |
| 832 | |
| 833 | pruneRows--; |
| 834 | if (pruneRows == 0) { |
| 835 | break; |
| 836 | } |
| 837 | |
Mark Salyzyn | c572fc6 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 838 | if (key == worst) { |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 839 | kick = true; |
| 840 | if (worst_sizes < second_worst_sizes) { |
| 841 | break; |
| 842 | } |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 843 | worst_sizes -= element->getMsgLen(); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 844 | } |
| 845 | continue; |
| 846 | } |
| 847 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 848 | if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) || |
| 849 | (element->getRealTime() > (*lastt)->getRealTime())) { |
Mark Salyzyn | 4e29e17 | 2015-08-24 13:43:27 -0700 | [diff] [blame] | 850 | break; |
| 851 | } |
| 852 | |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 853 | if (dropped) { |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 854 | last.add(element); |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 855 | if (worstPid && |
| 856 | ((!gc && (element->getPid() == worstPid)) || |
| 857 | (mLastWorstPidOfSystem[id].find(element->getPid()) == |
| 858 | mLastWorstPidOfSystem[id].end()))) { |
Mark Salyzyn | 628d937 | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 859 | // element->getUid() may not be AID_SYSTEM, next best |
Mark Salyzyn | 2e86fa4 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 860 | // watermark if current one empty. id is not LOG_ID_EVENTS |
| 861 | // or LOG_ID_SECURITY because of worstPid check. |
Mark Salyzyn | 9faf908 | 2016-09-01 07:28:44 -0700 | [diff] [blame] | 862 | mLastWorstPidOfSystem[id][element->getPid()] = it; |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 863 | } |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 864 | if ((!gc && !worstPid && (key == worst)) || |
| 865 | (mLastWorst[id].find(key) == mLastWorst[id].end())) { |
Mark Salyzyn | c572fc6 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 866 | mLastWorst[id][key] = it; |
Mark Salyzyn | cb15c7c | 2015-08-24 13:43:27 -0700 | [diff] [blame] | 867 | } |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 868 | ++it; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 869 | continue; |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 870 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 871 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 872 | if ((key != worst) || |
| 873 | (worstPid && (element->getPid() != worstPid))) { |
Mark Salyzyn | fd318a8 | 2015-06-01 09:41:19 -0700 | [diff] [blame] | 874 | leading = false; |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 875 | last.clear(element); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 876 | ++it; |
| 877 | continue; |
| 878 | } |
Mark Salyzyn | 628d937 | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 879 | // key == worst below here |
| 880 | // If worstPid set, then element->getPid() == worstPid below here |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 881 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 882 | pruneRows--; |
| 883 | if (pruneRows == 0) { |
| 884 | break; |
| 885 | } |
| 886 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 887 | kick = true; |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 888 | |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 889 | unsigned short len = element->getMsgLen(); |
Mark Salyzyn | 65ec860 | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 890 | |
| 891 | // do not create any leading drops |
| 892 | if (leading) { |
| 893 | it = erase(it); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 894 | } else { |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 895 | stats.drop(element); |
| 896 | element->setDropped(1); |
| 897 | if (last.coalesce(element, 1)) { |
Mark Salyzyn | 95ff248 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 898 | it = erase(it, true); |
Mark Salyzyn | 65ec860 | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 899 | } else { |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 900 | last.add(element); |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 901 | if (worstPid && |
| 902 | (!gc || (mLastWorstPidOfSystem[id].find(worstPid) == |
| 903 | mLastWorstPidOfSystem[id].end()))) { |
Mark Salyzyn | 628d937 | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 904 | // element->getUid() may not be AID_SYSTEM, next best |
Mark Salyzyn | 2e86fa4 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 905 | // watermark if current one empty. id is not |
| 906 | // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid. |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 907 | mLastWorstPidOfSystem[id][worstPid] = it; |
| 908 | } |
Mark Salyzyn | c572fc6 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 909 | if ((!gc && !worstPid) || |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 910 | (mLastWorst[id].find(worst) == mLastWorst[id].end())) { |
Mark Salyzyn | c572fc6 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 911 | mLastWorst[id][worst] = it; |
Mark Salyzyn | 50e2108 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 912 | } |
Mark Salyzyn | 65ec860 | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 913 | ++it; |
| 914 | } |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 915 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 916 | if (worst_sizes < second_worst_sizes) { |
| 917 | break; |
| 918 | } |
| 919 | worst_sizes -= len; |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 920 | } |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 921 | last.clear(); |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 922 | |
Mark Salyzyn | c402bc7 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 923 | if (!kick || !mPrune.worstUidEnabled()) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 924 | break; // the following loop will ask bad clients to skip/drop |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 928 | bool whitelist = false; |
Mark Salyzyn | 8885daf | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 929 | bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll; |
Mark Salyzyn | 7a3c282 | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 930 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 931 | while ((pruneRows > 0) && (it != mLogElements.end())) { |
| 932 | LogBufferElement* element = *it; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 933 | |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 934 | if (element->getLogId() != id) { |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 935 | it++; |
| 936 | continue; |
| 937 | } |
| 938 | |
Mark Salyzyn | 7a3c282 | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 939 | if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) { |
| 940 | mLast[id] = it; |
| 941 | mLastSet[id] = true; |
| 942 | } |
| 943 | |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 944 | if (oldest && (oldest->mStart <= element->getSequence())) { |
Mark Salyzyn | 7702c3e | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 945 | busy = true; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 946 | if (whitelist) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 947 | break; |
| 948 | } |
Mark Salyzyn | c402bc7 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 949 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 950 | if (stats.sizes(id) > (2 * log_buffer_size(id))) { |
| 951 | // kick a misbehaving log reader client off the island |
| 952 | oldest->release_Locked(); |
Mark Salyzyn | 552b475 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 953 | } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) { |
| 954 | oldest->triggerReader_Locked(); |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 955 | } else { |
| 956 | oldest->triggerSkip_Locked(id, pruneRows); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 957 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 958 | break; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 959 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 960 | |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 961 | if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) { |
| 962 | // WhiteListed |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 963 | whitelist = true; |
| 964 | it++; |
| 965 | continue; |
| 966 | } |
| 967 | |
| 968 | it = erase(it); |
| 969 | pruneRows--; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 970 | } |
| 971 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 972 | // Do not save the whitelist if we are reader range limited |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 973 | if (whitelist && (pruneRows > 0)) { |
Mark Salyzyn | 7a3c282 | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 974 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 975 | while ((it != mLogElements.end()) && (pruneRows > 0)) { |
| 976 | LogBufferElement* element = *it; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 977 | |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 978 | if (element->getLogId() != id) { |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 979 | ++it; |
| 980 | continue; |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 981 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 982 | |
Mark Salyzyn | 7a3c282 | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 983 | if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) { |
| 984 | mLast[id] = it; |
| 985 | mLastSet[id] = true; |
| 986 | } |
| 987 | |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 988 | if (oldest && (oldest->mStart <= element->getSequence())) { |
Mark Salyzyn | 7702c3e | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 989 | busy = true; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 990 | if (stats.sizes(id) > (2 * log_buffer_size(id))) { |
| 991 | // kick a misbehaving log reader client off the island |
| 992 | oldest->release_Locked(); |
Mark Salyzyn | 552b475 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 993 | } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) { |
| 994 | oldest->triggerReader_Locked(); |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 995 | } else { |
| 996 | oldest->triggerSkip_Locked(id, pruneRows); |
| 997 | } |
| 998 | break; |
| 999 | } |
| 1000 | |
| 1001 | it = erase(it); |
| 1002 | pruneRows--; |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1003 | } |
| 1004 | } |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1005 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1006 | LogTimeEntry::unlock(); |
Mark Salyzyn | 7702c3e | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1007 | |
| 1008 | return (pruneRows > 0) && busy; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | // clear all rows of type "id" from the buffer. |
Mark Salyzyn | 7702c3e | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1012 | bool LogBuffer::clear(log_id_t id, uid_t uid) { |
| 1013 | bool busy = true; |
| 1014 | // If it takes more than 4 tries (seconds) to clear, then kill reader(s) |
| 1015 | for (int retry = 4;;) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1016 | if (retry == 1) { // last pass |
Mark Salyzyn | 7702c3e | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1017 | // Check if it is still busy after the sleep, we say prune |
| 1018 | // one entry, not another clear run, so we are looking for |
| 1019 | // the quick side effect of the return value to tell us if |
| 1020 | // we have a _blocked_ reader. |
| 1021 | pthread_mutex_lock(&mLogElementsLock); |
| 1022 | busy = prune(id, 1, uid); |
| 1023 | pthread_mutex_unlock(&mLogElementsLock); |
| 1024 | // It is still busy, blocked reader(s), lets kill them all! |
| 1025 | // otherwise, lets be a good citizen and preserve the slow |
| 1026 | // readers and let the clear run (below) deal with determining |
| 1027 | // if we are still blocked and return an error code to caller. |
| 1028 | if (busy) { |
| 1029 | LogTimeEntry::lock(); |
| 1030 | LastLogTimes::iterator times = mTimes.begin(); |
| 1031 | while (times != mTimes.end()) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1032 | LogTimeEntry* entry = (*times); |
Mark Salyzyn | 7702c3e | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1033 | // Killer punch |
| 1034 | if (entry->owned_Locked() && entry->isWatching(id)) { |
| 1035 | entry->release_Locked(); |
| 1036 | } |
| 1037 | times++; |
| 1038 | } |
| 1039 | LogTimeEntry::unlock(); |
| 1040 | } |
| 1041 | } |
| 1042 | pthread_mutex_lock(&mLogElementsLock); |
| 1043 | busy = prune(id, ULONG_MAX, uid); |
| 1044 | pthread_mutex_unlock(&mLogElementsLock); |
| 1045 | if (!busy || !--retry) { |
| 1046 | break; |
| 1047 | } |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1048 | sleep(1); // Let reader(s) catch up after notification |
Mark Salyzyn | 7702c3e | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1049 | } |
| 1050 | return busy; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | // get the used space associated with "id". |
| 1054 | unsigned long LogBuffer::getSizeUsed(log_id_t id) { |
| 1055 | pthread_mutex_lock(&mLogElementsLock); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1056 | size_t retval = stats.sizes(id); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1057 | pthread_mutex_unlock(&mLogElementsLock); |
| 1058 | return retval; |
| 1059 | } |
| 1060 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1061 | // set the total space allocated to "id" |
| 1062 | int LogBuffer::setSize(log_id_t id, unsigned long size) { |
| 1063 | // Reasonable limits ... |
Mark Salyzyn | f4693f3 | 2016-09-27 13:08:23 -0700 | [diff] [blame] | 1064 | if (!__android_logger_valid_buffer_size(size)) { |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1065 | return -1; |
| 1066 | } |
| 1067 | pthread_mutex_lock(&mLogElementsLock); |
| 1068 | log_buffer_size(id) = size; |
| 1069 | pthread_mutex_unlock(&mLogElementsLock); |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
| 1073 | // get the total space allocated to "id" |
| 1074 | unsigned long LogBuffer::getSize(log_id_t id) { |
| 1075 | pthread_mutex_lock(&mLogElementsLock); |
| 1076 | size_t retval = log_buffer_size(id); |
| 1077 | pthread_mutex_unlock(&mLogElementsLock); |
| 1078 | return retval; |
| 1079 | } |
| 1080 | |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 1081 | uint64_t LogBuffer::flushTo( |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1082 | SocketClient* reader, const uint64_t start, bool privileged, bool security, |
| 1083 | int (*filter)(const LogBufferElement* element, void* arg), void* arg) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1084 | LogBufferElementCollection::iterator it; |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 1085 | uint64_t max = start; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1086 | uid_t uid = reader->getUid(); |
| 1087 | |
| 1088 | pthread_mutex_lock(&mLogElementsLock); |
Dragoslav Mitrinovic | 81624f2 | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1089 | |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 1090 | if (start <= 1) { |
Dragoslav Mitrinovic | 81624f2 | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1091 | // client wants to start from the beginning |
| 1092 | it = mLogElements.begin(); |
| 1093 | } else { |
| 1094 | // Client wants to start from some specified time. Chances are |
| 1095 | // we are better off starting from the end of the time sorted list. |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1096 | for (it = mLogElements.end(); it != mLogElements.begin(); |
| 1097 | /* do nothing */) { |
Dragoslav Mitrinovic | 81624f2 | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1098 | --it; |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1099 | LogBufferElement* element = *it; |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 1100 | if (element->getSequence() <= start) { |
Dragoslav Mitrinovic | 81624f2 | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1101 | it++; |
| 1102 | break; |
| 1103 | } |
| 1104 | } |
| 1105 | } |
| 1106 | |
Mark Salyzyn | 99a671a | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 1107 | // Help detect if the valid message before is from the same source so |
| 1108 | // we can differentiate chatty filter types. |
| 1109 | pid_t lastTid[LOG_ID_MAX] = { 0 }; |
| 1110 | |
Dragoslav Mitrinovic | 81624f2 | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1111 | for (; it != mLogElements.end(); ++it) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1112 | LogBufferElement* element = *it; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1113 | |
| 1114 | if (!privileged && (element->getUid() != uid)) { |
| 1115 | continue; |
| 1116 | } |
| 1117 | |
Mark Salyzyn | 924c0b3 | 2016-01-26 14:32:35 -0800 | [diff] [blame] | 1118 | if (!security && (element->getLogId() == LOG_ID_SECURITY)) { |
| 1119 | continue; |
| 1120 | } |
| 1121 | |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 1122 | if (element->getSequence() <= start) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1123 | continue; |
| 1124 | } |
| 1125 | |
| 1126 | // NB: calling out to another object with mLogElementsLock held (safe) |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 1127 | if (filter) { |
| 1128 | int ret = (*filter)(element, arg); |
| 1129 | if (ret == false) { |
| 1130 | continue; |
| 1131 | } |
| 1132 | if (ret != true) { |
| 1133 | break; |
| 1134 | } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1135 | } |
| 1136 | |
Mark Salyzyn | 99a671a | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 1137 | bool sameTid = lastTid[element->getLogId()] == element->getTid(); |
| 1138 | // Dropped (chatty) immediately following a valid log from the |
| 1139 | // same source in the same log buffer indicates we have a |
| 1140 | // multiple identical squash. chatty that differs source |
| 1141 | // is due to spam filter. chatty to chatty of different |
| 1142 | // source is also due to spam filter. |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1143 | lastTid[element->getLogId()] = |
| 1144 | (element->getDropped() && !sameTid) ? 0 : element->getTid(); |
Mark Salyzyn | 99a671a | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 1145 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1146 | pthread_mutex_unlock(&mLogElementsLock); |
| 1147 | |
| 1148 | // range locking in LastLogTimes looks after us |
Mark Salyzyn | 99a671a | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 1149 | max = element->flushTo(reader, this, privileged, sameTid); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1150 | |
| 1151 | if (max == element->FLUSH_ERROR) { |
| 1152 | return max; |
| 1153 | } |
| 1154 | |
| 1155 | pthread_mutex_lock(&mLogElementsLock); |
| 1156 | } |
| 1157 | pthread_mutex_unlock(&mLogElementsLock); |
| 1158 | |
| 1159 | return max; |
| 1160 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1161 | |
Mark Salyzyn | b921ab5 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 1162 | std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid, |
| 1163 | unsigned int logMask) { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1164 | pthread_mutex_lock(&mLogElementsLock); |
| 1165 | |
Mark Salyzyn | b921ab5 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 1166 | std::string ret = stats.format(uid, pid, logMask); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1167 | |
| 1168 | pthread_mutex_unlock(&mLogElementsLock); |
Mark Salyzyn | 1c7b6fb | 2015-08-20 10:01:44 -0700 | [diff] [blame] | 1169 | |
| 1170 | return ret; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1171 | } |