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