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