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