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 | b338b95 | 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 | 60b9992 | 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 | ab94771 | 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 | b004083 | 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 | 0e2fe42 | 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 | 24a57dc | 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 | ab94771 | 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 | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 48 | mLastSet[i] = false; |
| 49 | mLast[i] = mLogElements.begin(); |
| 50 | |
Mark Salyzyn | b004083 | 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 | 0e2fe42 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 55 | bool lastMonotonic = monotonic; |
Mark Salyzyn | 11a3e70 | 2015-12-01 15:57:25 -0800 | [diff] [blame] | 56 | monotonic = android_log_clockid() == CLOCK_MONOTONIC; |
Mark Salyzyn | c959c64 | 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 | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 75 | while ((it != mLogElements.end())) { |
| 76 | LogBufferElement* e = *it; |
Mark Salyzyn | c959c64 | 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 | 0e2fe42 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Mark Salyzyn | c959c64 | 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 | 0e2fe42 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 93 | // |
Mark Salyzyn | c959c64 | 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 | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 99 | while (times != mTimes.end()) { |
| 100 | LogTimeEntry* entry = (*times); |
Mark Salyzyn | c959c64 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 101 | if (entry->owned_Locked()) { |
| 102 | entry->triggerReader_Locked(); |
Mark Salyzyn | 0e2fe42 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 103 | } |
Mark Salyzyn | c959c64 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 104 | times++; |
Mark Salyzyn | 0e2fe42 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 105 | } |
Mark Salyzyn | c959c64 | 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 | 6505953 | 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 | 24a57dc | 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 | 24a57dc | 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 | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 129 | enum match_type { DIFFERENT, SAME, SAME_LIBLOG }; |
Mark Salyzyn | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 130 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 131 | static enum match_type identical(LogBufferElement* elem, |
| 132 | LogBufferElement* last) { |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 133 | // is it mostly identical? |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 134 | // if (!elem) return DIFFERENT; |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 135 | unsigned short lenl = elem->getMsgLen(); |
Mark Salyzyn | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 136 | if (!lenl) return DIFFERENT; |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 137 | // if (!last) return DIFFERENT; |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 138 | unsigned short lenr = last->getMsgLen(); |
Mark Salyzyn | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 139 | if (!lenr) return DIFFERENT; |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 140 | // if (elem->getLogId() != last->getLogId()) return DIFFERENT; |
Mark Salyzyn | 60b9992 | 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 | 24a57dc | 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 | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 147 | (last->getRealTime().nsec() + 60 * NS_PER_SEC)) |
| 148 | return DIFFERENT; |
Mark Salyzyn | 24a57dc | 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 | 60b9992 | 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 | 6505953 | 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 | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 162 | } |
Mark Salyzyn | 24a57dc | 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 | 6505953 | 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 | 24a57dc | 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 | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 176 | const char* avcl = android::strnstr(msgl, lenl, avc); |
Mark Salyzyn | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 177 | if (!avcl) return DIFFERENT; |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 178 | lenl -= avcl - msgl; |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 179 | const char* avcr = android::strnstr(msgr, lenr, avc); |
Mark Salyzyn | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 180 | if (!avcr) return DIFFERENT; |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 181 | lenr -= avcr - msgr; |
Mark Salyzyn | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 182 | if (lenl != lenr) return DIFFERENT; |
Evgenii Stepanov | 2d67330 | 2017-03-14 14:47:25 -0700 | [diff] [blame] | 183 | // TODO: After b/35468874 is addressed, revisit "lenl > strlen(avc)" |
Mark Salyzyn | d18255f | 2017-03-10 08:44:14 -0800 | [diff] [blame^] | 184 | // condition, it might become superfluous. |
Evgenii Stepanov | 2d67330 | 2017-03-14 14:47:25 -0700 | [diff] [blame] | 185 | if (lenl > strlen(avc) && |
| 186 | fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc), |
| 187 | lenl - strlen(avc))) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 188 | return DIFFERENT; |
Evgenii Stepanov | 2d67330 | 2017-03-14 14:47:25 -0700 | [diff] [blame] | 189 | } |
Mark Salyzyn | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 190 | return SAME; |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 193 | int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, |
| 194 | pid_t tid, const char* msg, unsigned short len) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 195 | if ((log_id >= LOG_ID_MAX) || (log_id < 0)) { |
Mark Salyzyn | 88fe7cc | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 196 | return -EINVAL; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 197 | } |
Mark Salyzyn | 0dcfae6 | 2014-10-02 13:07:05 -0700 | [diff] [blame] | 198 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 199 | LogBufferElement* elem = |
| 200 | new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len); |
Mark Salyzyn | c55cc11 | 2016-01-06 21:17:43 +0000 | [diff] [blame] | 201 | if (log_id != LOG_ID_SECURITY) { |
Mark Salyzyn | c969086 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 202 | int prio = ANDROID_LOG_INFO; |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 203 | const char* tag = NULL; |
Mark Salyzyn | c969086 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 204 | if (log_id == LOG_ID_EVENTS) { |
Mark Salyzyn | 9472b4d | 2016-09-12 14:51:54 -0700 | [diff] [blame] | 205 | tag = tagToName(elem->getTag()); |
Mark Salyzyn | c969086 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 206 | } else { |
| 207 | prio = *msg; |
| 208 | tag = msg + 1; |
| 209 | } |
Mark Salyzyn | 9472b4d | 2016-09-12 14:51:54 -0700 | [diff] [blame] | 210 | if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) { |
Mark Salyzyn | c969086 | 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 | 0dcfae6 | 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 | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 222 | LogBufferElement* currentLast = lastLoggedElements[log_id]; |
| 223 | if (currentLast) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 224 | LogBufferElement* dropped = droppedElements[log_id]; |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 225 | unsigned short count = dropped ? dropped->getDropped() : 0; |
Mark Salyzyn | 52ee5f5 | 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 | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 242 | // outgoing: if match != DIFFERENT |
Mark Salyzyn | 52ee5f5 | 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 | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 245 | // break: if match == DIFFERENT |
Mark Salyzyn | 52ee5f5 | 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 | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 257 | // outgoing: if match == SAME |
Mark Salyzyn | 52ee5f5 | 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 | 60b9992 | 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 | 52ee5f5 | 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 | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 285 | // outgoing: if match != DIFFERENT |
Mark Salyzyn | 52ee5f5 | 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 | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 290 | // break: if match == DIFFERENT |
Mark Salyzyn | 52ee5f5 | 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 | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 297 | enum match_type match = identical(elem, currentLast); |
| 298 | if (match != DIFFERENT) { |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 299 | if (dropped) { |
Mark Salyzyn | 60b9992 | 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*>( |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 314 | const_cast<char*>(elem->getMsg())); |
Mark Salyzyn | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 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 | 24a57dc | 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 | 6505953 | 2017-03-10 14:31:54 -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; |
Mark Salyzyn | 52ee5f5 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 356 | } |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 357 | droppedElements[log_id] = NULL; |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 358 | log(currentLast); // report last message in the series |
| 359 | } else { // State 0 |
Mark Salyzyn | 24a57dc | 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 | 24a57dc | 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 | 5119249 | 2017-03-03 10:21:23 -0800 | [diff] [blame] | 377 | if (__predict_true(it != mLogElements.begin())) --it; |
| 378 | if (__predict_false(it == mLogElements.begin()) || |
| 379 | __predict_true((*it)->getRealTime() <= elem->getRealTime())) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 380 | mLogElements.push_back(elem); |
| 381 | } else { |
Mark Salyzyn | d18255f | 2017-03-10 08:44:14 -0800 | [diff] [blame^] | 382 | log_time end = log_time::EPOCH; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 383 | bool end_set = false; |
| 384 | bool end_always = false; |
| 385 | |
| 386 | LogTimeEntry::lock(); |
| 387 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 388 | LastLogTimes::iterator times = mTimes.begin(); |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 389 | while (times != mTimes.end()) { |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 390 | LogTimeEntry* entry = (*times); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 391 | if (entry->owned_Locked()) { |
| 392 | if (!entry->mNonBlock) { |
| 393 | end_always = true; |
| 394 | break; |
| 395 | } |
Mark Salyzyn | 5119249 | 2017-03-03 10:21:23 -0800 | [diff] [blame] | 396 | // it passing mEnd is blocked by the following checks. |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 397 | if (!end_set || (end <= entry->mEnd)) { |
| 398 | end = entry->mEnd; |
| 399 | end_set = true; |
| 400 | } |
| 401 | } |
Mark Salyzyn | edcbdfa | 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 | d18255f | 2017-03-10 08:44:14 -0800 | [diff] [blame^] | 405 | if (end_always || (end_set && (end > (*it)->getRealTime()))) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 406 | mLogElements.push_back(elem); |
| 407 | } else { |
Mark Salyzyn | 5119249 | 2017-03-03 10:21:23 -0800 | [diff] [blame] | 408 | // should be short as timestamps are localized near end() |
| 409 | do { |
| 410 | last = it; |
| 411 | if (__predict_false(it == mLogElements.begin())) { |
| 412 | break; |
| 413 | } |
Mark Salyzyn | 5119249 | 2017-03-03 10:21:23 -0800 | [diff] [blame] | 414 | --it; |
| 415 | } while (((*it)->getRealTime() > elem->getRealTime()) && |
Mark Salyzyn | d18255f | 2017-03-10 08:44:14 -0800 | [diff] [blame^] | 416 | (!end_set || (end <= (*it)->getRealTime()))); |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 417 | mLogElements.insert(last, elem); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 418 | } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 419 | LogTimeEntry::unlock(); |
| 420 | } |
| 421 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 422 | stats.add(elem); |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 423 | maybePrune(elem->getLogId()); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Mark Salyzyn | 5c5189a | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 426 | // 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] | 427 | // |
| 428 | // mLogElementsLock must be held when this function is called. |
| 429 | void LogBuffer::maybePrune(log_id_t id) { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 430 | size_t sizes = stats.sizes(id); |
Mark Salyzyn | 5f19031 | 2015-08-10 10:23:56 -0700 | [diff] [blame] | 431 | unsigned long maxSize = log_buffer_size(id); |
| 432 | if (sizes > maxSize) { |
Mark Salyzyn | 185d8da | 2015-08-19 12:20:36 -0700 | [diff] [blame] | 433 | size_t sizeOver = sizes - ((maxSize * 9) / 10); |
Mark Salyzyn | 49858cc | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 434 | size_t elements = stats.realElements(id); |
| 435 | size_t minElements = elements / 100; |
| 436 | if (minElements < minPrune) { |
| 437 | minElements = minPrune; |
| 438 | } |
Mark Salyzyn | 5f19031 | 2015-08-10 10:23:56 -0700 | [diff] [blame] | 439 | unsigned long pruneRows = elements * sizeOver / sizes; |
Mark Salyzyn | 5c5189a | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 440 | if (pruneRows < minElements) { |
Mark Salyzyn | 5f19031 | 2015-08-10 10:23:56 -0700 | [diff] [blame] | 441 | pruneRows = minElements; |
Mark Salyzyn | 9d4e34e | 2014-01-13 16:37:51 -0800 | [diff] [blame] | 442 | } |
Mark Salyzyn | 5c5189a | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 443 | if (pruneRows > maxPrune) { |
| 444 | pruneRows = maxPrune; |
Mark Salyzyn | 185d8da | 2015-08-19 12:20:36 -0700 | [diff] [blame] | 445 | } |
Mark Salyzyn | 9d4e34e | 2014-01-13 16:37:51 -0800 | [diff] [blame] | 446 | prune(id, pruneRows); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
Mark Salyzyn | 1fdf71f | 2015-09-03 16:08:50 -0700 | [diff] [blame] | 450 | LogBufferElementCollection::iterator LogBuffer::erase( |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 451 | LogBufferElementCollection::iterator it, bool coalesce) { |
| 452 | LogBufferElement* element = *it; |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 453 | log_id_t id = element->getLogId(); |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 454 | |
Mark Salyzyn | 1117818 | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 455 | // Remove iterator references in the various lists that will become stale |
| 456 | // after the element is erased from the main logging list. |
| 457 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 458 | { // start of scope for found iterator |
| 459 | int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) |
| 460 | ? element->getTag() |
| 461 | : element->getUid(); |
Mark Salyzyn | 6fa0d03 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 462 | LogBufferIteratorMap::iterator found = mLastWorst[id].find(key); |
| 463 | if ((found != mLastWorst[id].end()) && (it == found->second)) { |
| 464 | mLastWorst[id].erase(found); |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 465 | } |
Mark Salyzyn | 5af07be | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 466 | } |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 467 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 468 | { // start of scope for pid found iterator |
Mark Salyzyn | 1117818 | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 469 | // element->getUid() may not be AID_SYSTEM for next-best-watermark. |
Mark Salyzyn | ab94771 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 470 | // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and |
| 471 | // long term code stability, find() check should be fast for those ids. |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 472 | LogBufferPidIteratorMap::iterator found = |
| 473 | mLastWorstPidOfSystem[id].find(element->getPid()); |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 474 | if ((found != mLastWorstPidOfSystem[id].end()) && |
| 475 | (it == found->second)) { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 476 | mLastWorstPidOfSystem[id].erase(found); |
| 477 | } |
| 478 | } |
| 479 | |
Mark Salyzyn | 75d5cf0 | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 480 | bool setLast[LOG_ID_MAX]; |
| 481 | bool doSetLast = false; |
| 482 | log_id_for_each(i) { |
| 483 | doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]); |
| 484 | } |
Mark Salyzyn | b338b95 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 485 | #ifdef DEBUG_CHECK_FOR_STALE_ENTRIES |
| 486 | LogBufferElementCollection::iterator bad = it; |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 487 | int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) |
| 488 | ? element->getTag() |
| 489 | : element->getUid(); |
Mark Salyzyn | b338b95 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 490 | #endif |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 491 | it = mLogElements.erase(it); |
Mark Salyzyn | 75d5cf0 | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 492 | if (doSetLast) { |
| 493 | log_id_for_each(i) { |
| 494 | if (setLast[i]) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 495 | if (__predict_false(it == mLogElements.end())) { // impossible |
Mark Salyzyn | 75d5cf0 | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 496 | mLastSet[i] = false; |
Mark Salyzyn | ab94771 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 497 | mLast[i] = mLogElements.begin(); |
Mark Salyzyn | 75d5cf0 | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 498 | } else { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 499 | mLast[i] = it; // push down the road as next-best-watermark |
Mark Salyzyn | 75d5cf0 | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 500 | } |
| 501 | } |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 502 | } |
| 503 | } |
Mark Salyzyn | b338b95 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 504 | #ifdef DEBUG_CHECK_FOR_STALE_ENTRIES |
| 505 | log_id_for_each(i) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 506 | for (auto b : mLastWorst[i]) { |
Mark Salyzyn | b338b95 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 507 | if (bad == b.second) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 508 | android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i, |
| 509 | b.first, key); |
Mark Salyzyn | b338b95 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 510 | } |
| 511 | } |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 512 | for (auto b : mLastWorstPidOfSystem[i]) { |
Mark Salyzyn | b338b95 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 513 | if (bad == b.second) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 514 | android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i, |
| 515 | b.first); |
Mark Salyzyn | b338b95 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | if (mLastSet[i] && (bad == mLast[i])) { |
| 519 | android::prdebug("stale mLast[%d]\n", i); |
| 520 | mLastSet[i] = false; |
| 521 | mLast[i] = mLogElements.begin(); |
| 522 | } |
| 523 | } |
| 524 | #endif |
Mark Salyzyn | 5c5189a | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 525 | if (coalesce) { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 526 | stats.erase(element); |
Mark Salyzyn | 5c5189a | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 527 | } else { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 528 | stats.subtract(element); |
Mark Salyzyn | 1fdf71f | 2015-09-03 16:08:50 -0700 | [diff] [blame] | 529 | } |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 530 | delete element; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 531 | |
| 532 | return it; |
| 533 | } |
| 534 | |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 535 | // Define a temporary mechanism to report the last LogBufferElement pointer |
| 536 | // for the specified uid, pid and tid. Used below to help merge-sort when |
| 537 | // pruning for worst UID. |
| 538 | class LogBufferElementKey { |
| 539 | const union { |
| 540 | struct { |
Mark Salyzyn | 16aacd3 | 2016-12-13 12:44:20 -0800 | [diff] [blame] | 541 | uint32_t uid; |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 542 | uint16_t pid; |
| 543 | uint16_t tid; |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 544 | } __packed; |
| 545 | uint64_t value; |
| 546 | } __packed; |
| 547 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 548 | public: |
| 549 | LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid) |
| 550 | : uid(uid), pid(pid), tid(tid) { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 551 | } |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 552 | explicit LogBufferElementKey(uint64_t key) : value(key) { |
| 553 | } |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 554 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 555 | uint64_t getKey() { |
| 556 | return value; |
| 557 | } |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 558 | }; |
| 559 | |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 560 | class LogBufferElementLast { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 561 | typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap; |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 562 | LogBufferElementMap map; |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 563 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 564 | public: |
| 565 | bool coalesce(LogBufferElement* element, unsigned short dropped) { |
| 566 | LogBufferElementKey key(element->getUid(), element->getPid(), |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 567 | element->getTid()); |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 568 | LogBufferElementMap::iterator it = map.find(key.getKey()); |
| 569 | if (it != map.end()) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 570 | LogBufferElement* found = it->second; |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 571 | unsigned short moreDropped = found->getDropped(); |
| 572 | if ((dropped + moreDropped) > USHRT_MAX) { |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 573 | map.erase(it); |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 574 | } else { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 575 | found->setDropped(dropped + moreDropped); |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 576 | return true; |
| 577 | } |
| 578 | } |
| 579 | return false; |
| 580 | } |
| 581 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 582 | void add(LogBufferElement* element) { |
| 583 | LogBufferElementKey key(element->getUid(), element->getPid(), |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 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 | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 592 | void clear(LogBufferElement* element) { |
Mark Salyzyn | d18255f | 2017-03-10 08:44:14 -0800 | [diff] [blame^] | 593 | log_time current = |
| 594 | element->getRealTime() - log_time(EXPIRE_RATELIMIT, 0); |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 595 | for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) { |
| 596 | LogBufferElement* mapElement = it->second; |
| 597 | if ((mapElement->getDropped() >= EXPIRE_THRESHOLD) && |
Mark Salyzyn | d18255f | 2017-03-10 08:44:14 -0800 | [diff] [blame^] | 598 | (current > mapElement->getRealTime())) { |
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 | } |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 605 | }; |
| 606 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 607 | // prune "pruneRows" of type "id" from the buffer. |
| 608 | // |
Mark Salyzyn | e2827d5 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 609 | // This garbage collection task is used to expire log entries. It is called to |
| 610 | // remove all logs (clear), all UID logs (unprivileged clear), or every |
| 611 | // 256 or 10% of the total logs (whichever is less) to prune the logs. |
| 612 | // |
| 613 | // First there is a prep phase where we discover the reader region lock that |
| 614 | // acts as a backstop to any pruning activity to stop there and go no further. |
| 615 | // |
| 616 | // There are three major pruning loops that follow. All expire from the oldest |
| 617 | // entries. Since there are multiple log buffers, the Android logging facility |
| 618 | // will appear to drop entries 'in the middle' when looking at multiple log |
| 619 | // sources and buffers. This effect is slightly more prominent when we prune |
| 620 | // the worst offender by logging source. Thus the logs slowly loose content |
| 621 | // and value as you move back in time. This is preferred since chatty sources |
| 622 | // invariably move the logs value down faster as less chatty sources would be |
| 623 | // expired in the noise. |
| 624 | // |
| 625 | // The first loop performs blacklisting and worst offender pruning. Falling |
| 626 | // through when there are no notable worst offenders and have not hit the |
| 627 | // region lock preventing further worst offender pruning. This loop also looks |
| 628 | // after managing the chatty log entries and merging to help provide |
| 629 | // statistical basis for blame. The chatty entries are not a notification of |
| 630 | // how much logs you may have, but instead represent how much logs you would |
| 631 | // have had in a virtual log buffer that is extended to cover all the in-memory |
| 632 | // logs without loss. They last much longer than the represented pruned logs |
| 633 | // since they get multiplied by the gains in the non-chatty log sources. |
| 634 | // |
| 635 | // The second loop get complicated because an algorithm of watermarks and |
| 636 | // history is maintained to reduce the order and keep processing time |
| 637 | // down to a minimum at scale. These algorithms can be costly in the face |
| 638 | // of larger log buffers, or severly limited processing time granted to a |
| 639 | // background task at lowest priority. |
| 640 | // |
| 641 | // This second loop does straight-up expiration from the end of the logs |
| 642 | // (again, remember for the specified log buffer id) but does some whitelist |
| 643 | // preservation. Thus whitelist is a Hail Mary low priority, blacklists and |
| 644 | // spam filtration all take priority. This second loop also checks if a region |
| 645 | // lock is causing us to buffer too much in the logs to help the reader(s), |
| 646 | // and will tell the slowest reader thread to skip log entries, and if |
| 647 | // persistent and hits a further threshold, kill the reader thread. |
| 648 | // |
| 649 | // The third thread is optional, and only gets hit if there was a whitelist |
| 650 | // and more needs to be pruned against the backstop of the region lock. |
| 651 | // |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 652 | // mLogElementsLock must be held when this function is called. |
Mark Salyzyn | e2827d5 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 653 | // |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 654 | bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 655 | LogTimeEntry* oldest = NULL; |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 656 | bool busy = false; |
Mark Salyzyn | 71541f3 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 657 | bool clearAll = pruneRows == ULONG_MAX; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 658 | |
| 659 | LogTimeEntry::lock(); |
| 660 | |
| 661 | // Region locked? |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 662 | LastLogTimes::iterator times = mTimes.begin(); |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 663 | while (times != mTimes.end()) { |
| 664 | LogTimeEntry* entry = (*times); |
| 665 | if (entry->owned_Locked() && entry->isWatching(id) && |
| 666 | (!oldest || (oldest->mStart > entry->mStart) || |
| 667 | ((oldest->mStart == entry->mStart) && |
| 668 | (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 669 | oldest = entry; |
| 670 | } |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 671 | times++; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 672 | } |
| 673 | |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 674 | LogBufferElementCollection::iterator it; |
| 675 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 676 | if (__predict_false(caller_uid != AID_ROOT)) { // unlikely |
Mark Salyzyn | 9e0c78e | 2016-09-01 15:48:36 -0700 | [diff] [blame] | 677 | // Only here if clear all request from non system source, so chatty |
| 678 | // filter logistics is not required. |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 679 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
| 680 | while (it != mLogElements.end()) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 681 | LogBufferElement* element = *it; |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 682 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 683 | if ((element->getLogId() != id) || |
| 684 | (element->getUid() != caller_uid)) { |
Mark Salyzyn | 71541f3 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 685 | ++it; |
| 686 | continue; |
| 687 | } |
| 688 | |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 689 | if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) { |
| 690 | mLast[id] = it; |
| 691 | mLastSet[id] = true; |
| 692 | } |
| 693 | |
Mark Salyzyn | d18255f | 2017-03-10 08:44:14 -0800 | [diff] [blame^] | 694 | if (oldest && (oldest->mStart <= element->getRealTime().nsec())) { |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 695 | busy = true; |
Mark Salyzyn | c959c64 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 696 | if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) { |
| 697 | oldest->triggerReader_Locked(); |
| 698 | } else { |
| 699 | oldest->triggerSkip_Locked(id, pruneRows); |
| 700 | } |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 701 | break; |
| 702 | } |
| 703 | |
Mark Salyzyn | 71541f3 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 704 | it = erase(it); |
Mark Salyzyn | 9e0c78e | 2016-09-01 15:48:36 -0700 | [diff] [blame] | 705 | if (--pruneRows == 0) { |
| 706 | break; |
| 707 | } |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 708 | } |
| 709 | LogTimeEntry::unlock(); |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 710 | return busy; |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 711 | } |
| 712 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 713 | // prune by worst offenders; by blacklist, UID, and by PID of system UID |
Mark Salyzyn | c969086 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 714 | bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty(); |
Mark Salyzyn | 71541f3 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 715 | while (!clearAll && (pruneRows > 0)) { |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 716 | // recalculate the worst offender on every batched pass |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 717 | int worst = -1; // not valid for getUid() or getKey() |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 718 | size_t worst_sizes = 0; |
| 719 | size_t second_worst_sizes = 0; |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 720 | pid_t worstPid = 0; // POSIX guarantees PID != 0 |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 721 | |
Mark Salyzyn | 0e765c6 | 2015-03-17 17:17:25 -0700 | [diff] [blame] | 722 | if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) { |
Mark Salyzyn | 6fa0d03 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 723 | // Calculate threshold as 12.5% of available storage |
| 724 | size_t threshold = log_buffer_size(id) / 8; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 725 | |
Mark Salyzyn | 6fa0d03 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 726 | if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 727 | stats.sortTags(AID_ROOT, (pid_t)0, 2, id) |
| 728 | .findWorst(worst, worst_sizes, second_worst_sizes, |
| 729 | threshold); |
Mark Salyzyn | ab94771 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 730 | // per-pid filter for AID_SYSTEM sources is too complex |
Mark Salyzyn | 6fa0d03 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 731 | } else { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 732 | stats.sort(AID_ROOT, (pid_t)0, 2, id) |
| 733 | .findWorst(worst, worst_sizes, second_worst_sizes, |
| 734 | threshold); |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 735 | |
Mark Salyzyn | 6fa0d03 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 736 | if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 737 | stats.sortPids(worst, (pid_t)0, 2, id) |
| 738 | .findWorst(worstPid, worst_sizes, second_worst_sizes); |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 739 | } |
| 740 | } |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 741 | } |
| 742 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 743 | // skip if we have neither worst nor naughty filters |
Mark Salyzyn | 6fa0d03 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 744 | if ((worst == -1) && !hasBlacklist) { |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 745 | break; |
| 746 | } |
| 747 | |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 748 | bool kick = false; |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 749 | bool leading = true; |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 750 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
Mark Salyzyn | e2827d5 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 751 | // Perform at least one mandatory garbage collection cycle in following |
| 752 | // - clear leading chatty tags |
Mark Salyzyn | 5c5189a | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 753 | // - coalesce chatty tags |
Mark Salyzyn | e2827d5 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 754 | // - check age-out of preserved logs |
| 755 | bool gc = pruneRows <= 1; |
Mark Salyzyn | 6fa0d03 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 756 | if (!gc && (worst != -1)) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 757 | { // begin scope for worst found iterator |
| 758 | LogBufferIteratorMap::iterator found = |
| 759 | mLastWorst[id].find(worst); |
| 760 | if ((found != mLastWorst[id].end()) && |
| 761 | (found->second != mLogElements.end())) { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 762 | leading = false; |
| 763 | it = found->second; |
| 764 | } |
| 765 | } |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 766 | if (worstPid) { // begin scope for pid worst found iterator |
Mark Salyzyn | ab94771 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 767 | // FYI: worstPid only set if !LOG_ID_EVENTS and |
| 768 | // !LOG_ID_SECURITY, not going to make that assumption ... |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 769 | LogBufferPidIteratorMap::iterator found = |
| 770 | mLastWorstPidOfSystem[id].find(worstPid); |
| 771 | if ((found != mLastWorstPidOfSystem[id].end()) && |
| 772 | (found->second != mLogElements.end())) { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 773 | leading = false; |
| 774 | it = found->second; |
| 775 | } |
Mark Salyzyn | 5af07be | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 776 | } |
| 777 | } |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 778 | static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 }; |
Mark Salyzyn | 7f62d6b | 2015-08-24 13:43:27 -0700 | [diff] [blame] | 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 | 5af07be | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 783 | while (it != mLogElements.end()) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 784 | LogBufferElement* element = *it; |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 785 | |
Mark Salyzyn | d18255f | 2017-03-10 08:44:14 -0800 | [diff] [blame^] | 786 | if (oldest && (oldest->mStart <= element->getRealTime().nsec())) { |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 787 | busy = true; |
Mark Salyzyn | c959c64 | 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 | edcbdfa | 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 | 1117818 | 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 | b7dd96e | 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 | edcbdfa | 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 | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 813 | if (dropped && last.coalesce(element, dropped)) { |
Mark Salyzyn | 5c5189a | 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 | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 818 | int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) |
| 819 | ? element->getTag() |
| 820 | : element->getUid(); |
Mark Salyzyn | 6fa0d03 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 821 | |
Mark Salyzyn | edcbdfa | 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 | 6fa0d03 | 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 | edcbdfa | 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 | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 844 | if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) || |
| 845 | (element->getRealTime() > (*lastt)->getRealTime())) { |
Mark Salyzyn | 7f62d6b | 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 | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 850 | last.add(element); |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 851 | if (worstPid && |
| 852 | ((!gc && (element->getPid() == worstPid)) || |
| 853 | (mLastWorstPidOfSystem[id].find(element->getPid()) == |
| 854 | mLastWorstPidOfSystem[id].end()))) { |
Mark Salyzyn | 1117818 | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 855 | // element->getUid() may not be AID_SYSTEM, next best |
Mark Salyzyn | ab94771 | 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 | ce50dec | 2016-09-01 07:28:44 -0700 | [diff] [blame] | 858 | mLastWorstPidOfSystem[id][element->getPid()] = it; |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 859 | } |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 860 | if ((!gc && !worstPid && (key == worst)) || |
| 861 | (mLastWorst[id].find(key) == mLastWorst[id].end())) { |
Mark Salyzyn | 6fa0d03 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 862 | mLastWorst[id][key] = it; |
Mark Salyzyn | 76aab49 | 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 | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 868 | if ((key != worst) || |
| 869 | (worstPid && (element->getPid() != worstPid))) { |
Mark Salyzyn | fd318a8 | 2015-06-01 09:41:19 -0700 | [diff] [blame] | 870 | leading = false; |
Mark Salyzyn | edcbdfa | 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 | 1117818 | 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 | edcbdfa | 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 | edcbdfa | 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 | 5c5189a | 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 | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 896 | last.add(element); |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 897 | if (worstPid && |
| 898 | (!gc || (mLastWorstPidOfSystem[id].find(worstPid) == |
| 899 | mLastWorstPidOfSystem[id].end()))) { |
Mark Salyzyn | 1117818 | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 900 | // element->getUid() may not be AID_SYSTEM, next best |
Mark Salyzyn | ab94771 | 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 | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 903 | mLastWorstPidOfSystem[id][worstPid] = it; |
| 904 | } |
Mark Salyzyn | 6fa0d03 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 905 | if ((!gc && !worstPid) || |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 906 | (mLastWorst[id].find(worst) == mLastWorst[id].end())) { |
Mark Salyzyn | 6fa0d03 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 907 | mLastWorst[id][worst] = it; |
Mark Salyzyn | e2827d5 | 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 | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 920 | break; // the following loop will ask bad clients to skip/drop |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 921 | } |
| 922 | } |
| 923 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 924 | bool whitelist = false; |
Mark Salyzyn | c969086 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 925 | bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll; |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 926 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 927 | while ((pruneRows > 0) && (it != mLogElements.end())) { |
| 928 | LogBufferElement* element = *it; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 929 | |
Mark Salyzyn | edcbdfa | 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 | b7dd96e | 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 | d18255f | 2017-03-10 08:44:14 -0800 | [diff] [blame^] | 940 | if (oldest && (oldest->mStart <= element->getRealTime().nsec())) { |
Mark Salyzyn | 8cf0d03 | 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 | c959c64 | 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 | edcbdfa | 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 | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 970 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 971 | while ((it != mLogElements.end()) && (pruneRows > 0)) { |
| 972 | LogBufferElement* element = *it; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 973 | |
Mark Salyzyn | edcbdfa | 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 | b7dd96e | 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 | d18255f | 2017-03-10 08:44:14 -0800 | [diff] [blame^] | 984 | if (oldest && (oldest->mStart <= element->getRealTime().nsec())) { |
Mark Salyzyn | 8cf0d03 | 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 | c959c64 | 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 | 8cf0d03 | 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 | 8cf0d03 | 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;;) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1012 | if (retry == 1) { // last pass |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 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()) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1028 | LogTimeEntry* entry = (*times); |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 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 | } |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1044 | sleep(1); // Let reader(s) catch up after notification |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 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 | b004083 | 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 | d18255f | 2017-03-10 08:44:14 -0800 | [diff] [blame^] | 1077 | log_time LogBuffer::flushTo( |
| 1078 | SocketClient* reader, const log_time& start, bool privileged, bool security, |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1079 | int (*filter)(const LogBufferElement* element, void* arg), void* arg) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1080 | LogBufferElementCollection::iterator it; |
Mark Salyzyn | d18255f | 2017-03-10 08:44:14 -0800 | [diff] [blame^] | 1081 | log_time max = start; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1082 | uid_t uid = reader->getUid(); |
| 1083 | |
| 1084 | pthread_mutex_lock(&mLogElementsLock); |
Dragoslav Mitrinovic | 81624f2 | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1085 | |
Mark Salyzyn | d18255f | 2017-03-10 08:44:14 -0800 | [diff] [blame^] | 1086 | if (start == log_time::EPOCH) { |
Dragoslav Mitrinovic | 81624f2 | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1087 | // client wants to start from the beginning |
| 1088 | it = mLogElements.begin(); |
| 1089 | } else { |
| 1090 | // Client wants to start from some specified time. Chances are |
| 1091 | // we are better off starting from the end of the time sorted list. |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1092 | for (it = mLogElements.end(); it != mLogElements.begin(); |
| 1093 | /* do nothing */) { |
Dragoslav Mitrinovic | 81624f2 | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1094 | --it; |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1095 | LogBufferElement* element = *it; |
Mark Salyzyn | d18255f | 2017-03-10 08:44:14 -0800 | [diff] [blame^] | 1096 | if (element->getRealTime() <= start) { |
Dragoslav Mitrinovic | 81624f2 | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1097 | it++; |
| 1098 | break; |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | |
Mark Salyzyn | 1d7b071 | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 1103 | // Help detect if the valid message before is from the same source so |
| 1104 | // we can differentiate chatty filter types. |
| 1105 | pid_t lastTid[LOG_ID_MAX] = { 0 }; |
| 1106 | |
Dragoslav Mitrinovic | 81624f2 | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1107 | for (; it != mLogElements.end(); ++it) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1108 | LogBufferElement* element = *it; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1109 | |
| 1110 | if (!privileged && (element->getUid() != uid)) { |
| 1111 | continue; |
| 1112 | } |
| 1113 | |
Mark Salyzyn | b10f737 | 2016-01-26 14:32:35 -0800 | [diff] [blame] | 1114 | if (!security && (element->getLogId() == LOG_ID_SECURITY)) { |
| 1115 | continue; |
| 1116 | } |
| 1117 | |
Mark Salyzyn | d18255f | 2017-03-10 08:44:14 -0800 | [diff] [blame^] | 1118 | if (element->getRealTime() <= start) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1119 | continue; |
| 1120 | } |
| 1121 | |
| 1122 | // NB: calling out to another object with mLogElementsLock held (safe) |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 1123 | if (filter) { |
| 1124 | int ret = (*filter)(element, arg); |
| 1125 | if (ret == false) { |
| 1126 | continue; |
| 1127 | } |
| 1128 | if (ret != true) { |
| 1129 | break; |
| 1130 | } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1131 | } |
| 1132 | |
Mark Salyzyn | 1d7b071 | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 1133 | bool sameTid = lastTid[element->getLogId()] == element->getTid(); |
| 1134 | // Dropped (chatty) immediately following a valid log from the |
| 1135 | // same source in the same log buffer indicates we have a |
| 1136 | // multiple identical squash. chatty that differs source |
| 1137 | // is due to spam filter. chatty to chatty of different |
| 1138 | // source is also due to spam filter. |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1139 | lastTid[element->getLogId()] = |
| 1140 | (element->getDropped() && !sameTid) ? 0 : element->getTid(); |
Mark Salyzyn | 1d7b071 | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 1141 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1142 | pthread_mutex_unlock(&mLogElementsLock); |
| 1143 | |
| 1144 | // range locking in LastLogTimes looks after us |
Mark Salyzyn | 1d7b071 | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 1145 | max = element->flushTo(reader, this, privileged, sameTid); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1146 | |
| 1147 | if (max == element->FLUSH_ERROR) { |
| 1148 | return max; |
| 1149 | } |
| 1150 | |
| 1151 | pthread_mutex_lock(&mLogElementsLock); |
| 1152 | } |
| 1153 | pthread_mutex_unlock(&mLogElementsLock); |
| 1154 | |
| 1155 | return max; |
| 1156 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1157 | |
Mark Salyzyn | 4a2fc1c | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 1158 | std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid, |
| 1159 | unsigned int logMask) { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1160 | pthread_mutex_lock(&mLogElementsLock); |
| 1161 | |
Mark Salyzyn | 4a2fc1c | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 1162 | std::string ret = stats.format(uid, pid, logMask); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1163 | |
| 1164 | pthread_mutex_unlock(&mLogElementsLock); |
Mark Salyzyn | 7c528d9 | 2015-08-20 10:01:44 -0700 | [diff] [blame] | 1165 | |
| 1166 | return ret; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1167 | } |