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