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