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