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