Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012-2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Mark Salyzyn | b338b95 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 16 | // for manual checking of stale entries during LogBuffer::erase() |
| 17 | //#define DEBUG_CHECK_FOR_STALE_ENTRIES |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 18 | |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 19 | #include <ctype.h> |
Mark Salyzyn | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 20 | #include <endian.h> |
Mark Salyzyn | 88fe7cc | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 21 | #include <errno.h> |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <string.h> |
Mark Salyzyn | ab94771 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 24 | #include <sys/cdefs.h> |
Mark Salyzyn | 7b85c59 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 25 | #include <sys/user.h> |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 26 | #include <time.h> |
| 27 | #include <unistd.h> |
| 28 | |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 29 | #include <unordered_map> |
| 30 | |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 31 | #include <cutils/properties.h> |
Mark Salyzyn | b004083 | 2016-09-27 13:08:23 -0700 | [diff] [blame] | 32 | #include <private/android_logger.h> |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 33 | |
| 34 | #include "LogBuffer.h" |
Mark Salyzyn | 0e2fe42 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 35 | #include "LogKlog.h" |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 36 | #include "LogReader.h" |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 37 | #include "LogUtils.h" |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 38 | |
Mark Salyzyn | ab94771 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 39 | #ifndef __predict_false |
| 40 | #define __predict_false(exp) __builtin_expect((exp) != 0, 0) |
| 41 | #endif |
| 42 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 43 | // Default |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 44 | #define log_buffer_size(id) mMaxSize[id] |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 45 | |
Mark Salyzyn | 782d4d2 | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 46 | const log_time LogBuffer::pruneMargin(3, 0); |
| 47 | |
Mark Salyzyn | 3fe2593 | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 48 | void LogBuffer::init() { |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 49 | log_id_for_each(i) { |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 50 | mLastSet[i] = false; |
| 51 | mLast[i] = mLogElements.begin(); |
| 52 | |
Mark Salyzyn | b004083 | 2016-09-27 13:08:23 -0700 | [diff] [blame] | 53 | if (setSize(i, __android_logger_get_buffer_size(i))) { |
Mark Salyzyn | 7b85c59 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 54 | setSize(i, LOG_BUFFER_MIN_SIZE); |
| 55 | } |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 56 | } |
Mark Salyzyn | 0e2fe42 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 57 | bool lastMonotonic = monotonic; |
Mark Salyzyn | 11a3e70 | 2015-12-01 15:57:25 -0800 | [diff] [blame] | 58 | monotonic = android_log_clockid() == CLOCK_MONOTONIC; |
Mark Salyzyn | c959c64 | 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 | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 75 | rdlock(); |
Mark Salyzyn | c959c64 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 76 | LogBufferElementCollection::iterator it = mLogElements.begin(); |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 77 | while ((it != mLogElements.end())) { |
| 78 | LogBufferElement* e = *it; |
Mark Salyzyn | c959c64 | 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 | cb722de | 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 | c959c64 | 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 | cb722de | 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 | c959c64 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | ++it; |
| 95 | } |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 96 | unlock(); |
Mark Salyzyn | 0e2fe42 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Mark Salyzyn | c959c64 | 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 | 0e2fe42 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 101 | // |
Mark Salyzyn | c959c64 | 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 | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 104 | LogTimeEntry::wrlock(); |
Mark Salyzyn | c959c64 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 105 | |
| 106 | LastLogTimes::iterator times = mTimes.begin(); |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 107 | while (times != mTimes.end()) { |
Tom Cherry | 06e478b | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 108 | LogTimeEntry* entry = times->get(); |
| 109 | entry->triggerReader_Locked(); |
Mark Salyzyn | c959c64 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 110 | times++; |
Mark Salyzyn | 0e2fe42 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 111 | } |
Mark Salyzyn | c959c64 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 112 | |
| 113 | LogTimeEntry::unlock(); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Mark Salyzyn | 6505953 | 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 | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 118 | pthread_rwlock_init(&mLogElementsLock, nullptr); |
Mark Salyzyn | 3fe2593 | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 119 | |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 120 | log_id_for_each(i) { |
Mark Salyzyn | bec7b2d | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 121 | lastLoggedElements[i] = nullptr; |
| 122 | droppedElements[i] = nullptr; |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Mark Salyzyn | 3fe2593 | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 125 | init(); |
| 126 | } |
| 127 | |
Mark Salyzyn | 24a57dc | 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 | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 135 | enum match_type { DIFFERENT, SAME, SAME_LIBLOG }; |
Mark Salyzyn | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 136 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 137 | static enum match_type identical(LogBufferElement* elem, |
| 138 | LogBufferElement* last) { |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 139 | // is it mostly identical? |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 140 | // if (!elem) return DIFFERENT; |
Mark Salyzyn | 530bd6c | 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 | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 143 | // if (!last) return DIFFERENT; |
Mark Salyzyn | 530bd6c | 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 | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 146 | // if (elem->getLogId() != last->getLogId()) return DIFFERENT; |
Mark Salyzyn | 60b9992 | 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 | 24a57dc | 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 | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 153 | (last->getRealTime().nsec() + 60 * NS_PER_SEC)) |
| 154 | return DIFFERENT; |
Mark Salyzyn | 24a57dc | 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 | 60b9992 | 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 | 6505953 | 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 | 6b344a8 | 2017-03-13 15:41:59 -0700 | [diff] [blame] | 166 | (elem->getTag() == LIBLOG_LOG_TAG)) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 167 | return SAME_LIBLOG; |
Mark Salyzyn | 6b344a8 | 2017-03-13 15:41:59 -0700 | [diff] [blame] | 168 | } |
Mark Salyzyn | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 169 | } |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 170 | |
| 171 | // audit message (except sequence number) identical? |
Mark Salyzyn | 34aad62 | 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 | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 175 | if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) - |
Mark Salyzyn | 6b344a8 | 2017-03-13 15:41:59 -0700 | [diff] [blame] | 176 | sizeof(int32_t))) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 177 | return DIFFERENT; |
Mark Salyzyn | 6b344a8 | 2017-03-13 15:41:59 -0700 | [diff] [blame] | 178 | } |
Mark Salyzyn | 24a57dc | 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 | 530bd6c | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 184 | static const char avc[] = "): avc: "; |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 185 | const char* avcl = android::strnstr(msgl, lenl, avc); |
Mark Salyzyn | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 186 | if (!avcl) return DIFFERENT; |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 187 | lenl -= avcl - msgl; |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 188 | const char* avcr = android::strnstr(msgr, lenr, avc); |
Mark Salyzyn | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 189 | if (!avcr) return DIFFERENT; |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 190 | lenr -= avcr - msgr; |
Mark Salyzyn | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 191 | if (lenl != lenr) return DIFFERENT; |
Mark Salyzyn | 530bd6c | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 192 | if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc), |
Mark Salyzyn | 6b344a8 | 2017-03-13 15:41:59 -0700 | [diff] [blame] | 193 | lenl - strlen(avc))) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 194 | return DIFFERENT; |
Mark Salyzyn | 6b344a8 | 2017-03-13 15:41:59 -0700 | [diff] [blame] | 195 | } |
Mark Salyzyn | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 196 | return SAME; |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Mark Salyzyn | 6505953 | 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 | 9ff8155 | 2018-08-13 14:22:56 -0700 | [diff] [blame] | 200 | pid_t tid, const char* msg, uint16_t len) { |
Yi Kong | 90bcc18 | 2018-03-01 17:48:53 -0500 | [diff] [blame] | 201 | if (log_id >= LOG_ID_MAX) { |
Mark Salyzyn | 88fe7cc | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 202 | return -EINVAL; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 203 | } |
Mark Salyzyn | 0dcfae6 | 2014-10-02 13:07:05 -0700 | [diff] [blame] | 204 | |
Mark Salyzyn | cb722de | 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 | 6505953 | 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 | c55cc11 | 2016-01-06 21:17:43 +0000 | [diff] [blame] | 212 | if (log_id != LOG_ID_SECURITY) { |
Mark Salyzyn | c969086 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 213 | int prio = ANDROID_LOG_INFO; |
Mark Salyzyn | bec7b2d | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 214 | const char* tag = nullptr; |
Paul Elliott | 043810b | 2017-11-07 11:11:41 +0000 | [diff] [blame] | 215 | size_t tag_len = 0; |
Yao Chen | 36eacf8 | 2017-12-01 15:48:19 -0800 | [diff] [blame] | 216 | if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) { |
Mark Salyzyn | 9472b4d | 2016-09-12 14:51:54 -0700 | [diff] [blame] | 217 | tag = tagToName(elem->getTag()); |
Paul Elliott | 043810b | 2017-11-07 11:11:41 +0000 | [diff] [blame] | 218 | if (tag) { |
| 219 | tag_len = strlen(tag); |
| 220 | } |
Mark Salyzyn | c969086 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 221 | } else { |
| 222 | prio = *msg; |
| 223 | tag = msg + 1; |
Paul Elliott | 043810b | 2017-11-07 11:11:41 +0000 | [diff] [blame] | 224 | tag_len = strnlen(tag, len - 1); |
Mark Salyzyn | c969086 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 225 | } |
Paul Elliott | 043810b | 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 | c969086 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 228 | // Log traffic received to total |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 229 | wrlock(); |
Mark Salyzyn | fb0967b | 2017-04-14 09:46:57 -0700 | [diff] [blame] | 230 | stats.addTotal(elem); |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 231 | unlock(); |
Mark Salyzyn | c969086 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 232 | delete elem; |
| 233 | return -EACCES; |
| 234 | } |
Mark Salyzyn | 0dcfae6 | 2014-10-02 13:07:05 -0700 | [diff] [blame] | 235 | } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 236 | |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 237 | wrlock(); |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 238 | LogBufferElement* currentLast = lastLoggedElements[log_id]; |
| 239 | if (currentLast) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 240 | LogBufferElement* dropped = droppedElements[log_id]; |
Chih-Hung Hsieh | 9ff8155 | 2018-08-13 14:22:56 -0700 | [diff] [blame] | 241 | uint16_t count = dropped ? dropped->getDropped() : 0; |
Mark Salyzyn | 52ee5f5 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 242 | // |
| 243 | // State Init |
| 244 | // incoming: |
Mark Salyzyn | bec7b2d | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 245 | // dropped = nullptr |
| 246 | // currentLast = nullptr; |
Mark Salyzyn | 52ee5f5 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 247 | // elem = incoming message |
| 248 | // outgoing: |
Mark Salyzyn | bec7b2d | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 249 | // dropped = nullptr -> State 0 |
Mark Salyzyn | 52ee5f5 | 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 | bec7b2d | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 255 | // dropped = nullptr |
Mark Salyzyn | 52ee5f5 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 256 | // currentLast = copy of last message |
| 257 | // elem = incoming message |
Mark Salyzyn | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 258 | // outgoing: if match != DIFFERENT |
Mark Salyzyn | 52ee5f5 | 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 | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 261 | // break: if match == DIFFERENT |
Mark Salyzyn | bec7b2d | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 262 | // dropped = nullptr -> State 0 |
Mark Salyzyn | 52ee5f5 | 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 | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 273 | // outgoing: if match == SAME |
Mark Salyzyn | 52ee5f5 | 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 | 60b9992 | 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 | 52ee5f5 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 288 | // delete dropped |
Mark Salyzyn | bec7b2d | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 289 | // dropped = nullptr -> State 0 |
Mark Salyzyn | 52ee5f5 | 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 | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 301 | // outgoing: if match != DIFFERENT |
Mark Salyzyn | 52ee5f5 | 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 | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 306 | // break: if match == DIFFERENT |
Mark Salyzyn | 52ee5f5 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 307 | // log dropped (chatty message) |
Mark Salyzyn | bec7b2d | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 308 | // dropped = nullptr -> State 0 |
Mark Salyzyn | 52ee5f5 | 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 | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 313 | enum match_type match = identical(elem, currentLast); |
| 314 | if (match != DIFFERENT) { |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 315 | if (dropped) { |
Mark Salyzyn | 60b9992 | 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 | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 330 | const_cast<char*>(elem->getMsg())); |
Mark Salyzyn | 60b9992 | 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 | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 338 | unlock(); |
Mark Salyzyn | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 339 | return len; |
| 340 | } |
Mark Salyzyn | fb0967b | 2017-04-14 09:46:57 -0700 | [diff] [blame] | 341 | stats.addTotal(currentLast); |
Mark Salyzyn | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 342 | delete currentLast; |
| 343 | swab = total; |
| 344 | event->payload.data = htole32(swab); |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 345 | unlock(); |
Mark Salyzyn | 60b9992 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 346 | return len; |
| 347 | } |
Mark Salyzyn | 24a57dc | 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 | fb0967b | 2017-04-14 09:46:57 -0700 | [diff] [blame] | 357 | stats.addTotal(currentLast); |
Mark Salyzyn | 24a57dc | 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 | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 362 | unlock(); |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 363 | return len; |
| 364 | } |
Mark Salyzyn | 6505953 | 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 | 52ee5f5 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 370 | } |
Mark Salyzyn | bec7b2d | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 371 | droppedElements[log_id] = nullptr; |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 372 | log(currentLast); // report last message in the series |
| 373 | } else { // State 0 |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 374 | delete currentLast; |
| 375 | } |
| 376 | } |
| 377 | lastLoggedElements[log_id] = new LogBufferElement(*elem); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 378 | |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 379 | log(elem); |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 380 | unlock(); |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 381 | |
| 382 | return len; |
| 383 | } |
| 384 | |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 385 | // assumes LogBuffer::wrlock() held, owns elem, look after garbage collection |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 386 | void LogBuffer::log(LogBufferElement* elem) { |
Mark Salyzyn | 55b8a4e | 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 | 12bac90 | 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 | 5119249 | 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 | 55b8a4e | 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 | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 400 | mLogElements.push_back(elem); |
| 401 | } else { |
Bernie Innocenti | 440e035 | 2019-01-16 15:25:18 +0900 | [diff] [blame] | 402 | log_time end(log_time::EPOCH); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 403 | bool end_set = false; |
| 404 | bool end_always = false; |
| 405 | |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 406 | LogTimeEntry::rdlock(); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 407 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 408 | LastLogTimes::iterator times = mTimes.begin(); |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 409 | while (times != mTimes.end()) { |
Tom Cherry | 06e478b | 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 | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 419 | } |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 420 | times++; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Mark Salyzyn | d18255f | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 423 | if (end_always || (end_set && (end > (*it)->getRealTime()))) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 424 | mLogElements.push_back(elem); |
| 425 | } else { |
Mark Salyzyn | 5119249 | 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 | 5119249 | 2017-03-03 10:21:23 -0800 | [diff] [blame] | 432 | --it; |
| 433 | } while (((*it)->getRealTime() > elem->getRealTime()) && |
Mark Salyzyn | d18255f | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 434 | (!end_set || (end <= (*it)->getRealTime()))); |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 435 | mLogElements.insert(last, elem); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 436 | } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 437 | LogTimeEntry::unlock(); |
| 438 | } |
| 439 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 440 | stats.add(elem); |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 441 | maybePrune(elem->getLogId()); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 442 | } |
| 443 | |
Mark Salyzyn | 5c5189a | 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 | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 445 | // |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 446 | // LogBuffer::wrlock() must be held when this function is called. |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 447 | void LogBuffer::maybePrune(log_id_t id) { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 448 | size_t sizes = stats.sizes(id); |
Mark Salyzyn | 5f19031 | 2015-08-10 10:23:56 -0700 | [diff] [blame] | 449 | unsigned long maxSize = log_buffer_size(id); |
| 450 | if (sizes > maxSize) { |
Mark Salyzyn | 185d8da | 2015-08-19 12:20:36 -0700 | [diff] [blame] | 451 | size_t sizeOver = sizes - ((maxSize * 9) / 10); |
Mark Salyzyn | 49858cc | 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 | 5f19031 | 2015-08-10 10:23:56 -0700 | [diff] [blame] | 457 | unsigned long pruneRows = elements * sizeOver / sizes; |
Mark Salyzyn | 5c5189a | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 458 | if (pruneRows < minElements) { |
Mark Salyzyn | 5f19031 | 2015-08-10 10:23:56 -0700 | [diff] [blame] | 459 | pruneRows = minElements; |
Mark Salyzyn | 9d4e34e | 2014-01-13 16:37:51 -0800 | [diff] [blame] | 460 | } |
Mark Salyzyn | 5c5189a | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 461 | if (pruneRows > maxPrune) { |
| 462 | pruneRows = maxPrune; |
Mark Salyzyn | 185d8da | 2015-08-19 12:20:36 -0700 | [diff] [blame] | 463 | } |
Mark Salyzyn | 9d4e34e | 2014-01-13 16:37:51 -0800 | [diff] [blame] | 464 | prune(id, pruneRows); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
Mark Salyzyn | 1fdf71f | 2015-09-03 16:08:50 -0700 | [diff] [blame] | 468 | LogBufferElementCollection::iterator LogBuffer::erase( |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 469 | LogBufferElementCollection::iterator it, bool coalesce) { |
| 470 | LogBufferElement* element = *it; |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 471 | log_id_t id = element->getLogId(); |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 472 | |
Mark Salyzyn | 1117818 | 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 | 6505953 | 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 | 6fa0d03 | 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 | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 483 | } |
Mark Salyzyn | 5af07be | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 484 | } |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 485 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 486 | { // start of scope for pid found iterator |
Mark Salyzyn | 1117818 | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 487 | // element->getUid() may not be AID_SYSTEM for next-best-watermark. |
Mark Salyzyn | ab94771 | 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 | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 490 | LogBufferPidIteratorMap::iterator found = |
| 491 | mLastWorstPidOfSystem[id].find(element->getPid()); |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 492 | if ((found != mLastWorstPidOfSystem[id].end()) && |
| 493 | (it == found->second)) { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 494 | mLastWorstPidOfSystem[id].erase(found); |
| 495 | } |
| 496 | } |
| 497 | |
Mark Salyzyn | 75d5cf0 | 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 | b338b95 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 503 | #ifdef DEBUG_CHECK_FOR_STALE_ENTRIES |
| 504 | LogBufferElementCollection::iterator bad = it; |
Mark Salyzyn | 6505953 | 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 | b338b95 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 508 | #endif |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 509 | it = mLogElements.erase(it); |
Mark Salyzyn | 75d5cf0 | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 510 | if (doSetLast) { |
| 511 | log_id_for_each(i) { |
| 512 | if (setLast[i]) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 513 | if (__predict_false(it == mLogElements.end())) { // impossible |
Mark Salyzyn | 75d5cf0 | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 514 | mLastSet[i] = false; |
Mark Salyzyn | ab94771 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 515 | mLast[i] = mLogElements.begin(); |
Mark Salyzyn | 75d5cf0 | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 516 | } else { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 517 | mLast[i] = it; // push down the road as next-best-watermark |
Mark Salyzyn | 75d5cf0 | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 518 | } |
| 519 | } |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 520 | } |
| 521 | } |
Mark Salyzyn | b338b95 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 522 | #ifdef DEBUG_CHECK_FOR_STALE_ENTRIES |
| 523 | log_id_for_each(i) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 524 | for (auto b : mLastWorst[i]) { |
Mark Salyzyn | b338b95 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 525 | if (bad == b.second) { |
Mark Salyzyn | 6505953 | 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 | b338b95 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 528 | } |
| 529 | } |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 530 | for (auto b : mLastWorstPidOfSystem[i]) { |
Mark Salyzyn | b338b95 | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 531 | if (bad == b.second) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 532 | android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i, |
| 533 | b.first); |
Mark Salyzyn | b338b95 | 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 | 5c5189a | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 543 | if (coalesce) { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 544 | stats.erase(element); |
Mark Salyzyn | 5c5189a | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 545 | } else { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 546 | stats.subtract(element); |
Mark Salyzyn | 1fdf71f | 2015-09-03 16:08:50 -0700 | [diff] [blame] | 547 | } |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 548 | delete element; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 549 | |
| 550 | return it; |
| 551 | } |
| 552 | |
Mark Salyzyn | d84b27e | 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 | 16aacd3 | 2016-12-13 12:44:20 -0800 | [diff] [blame] | 559 | uint32_t uid; |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 560 | uint16_t pid; |
| 561 | uint16_t tid; |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 562 | } __packed; |
| 563 | uint64_t value; |
| 564 | } __packed; |
| 565 | |
Mark Salyzyn | 6505953 | 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 | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 569 | } |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 570 | explicit LogBufferElementKey(uint64_t key) : value(key) { |
| 571 | } |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 572 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 573 | uint64_t getKey() { |
| 574 | return value; |
| 575 | } |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 576 | }; |
| 577 | |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 578 | class LogBufferElementLast { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 579 | typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap; |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 580 | LogBufferElementMap map; |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 581 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 582 | public: |
Chih-Hung Hsieh | 9ff8155 | 2018-08-13 14:22:56 -0700 | [diff] [blame] | 583 | bool coalesce(LogBufferElement* element, uint16_t dropped) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 584 | LogBufferElementKey key(element->getUid(), element->getPid(), |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 585 | element->getTid()); |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 586 | LogBufferElementMap::iterator it = map.find(key.getKey()); |
| 587 | if (it != map.end()) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 588 | LogBufferElement* found = it->second; |
Chih-Hung Hsieh | 9ff8155 | 2018-08-13 14:22:56 -0700 | [diff] [blame] | 589 | uint16_t moreDropped = found->getDropped(); |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 590 | if ((dropped + moreDropped) > USHRT_MAX) { |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 591 | map.erase(it); |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 592 | } else { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 593 | found->setDropped(dropped + moreDropped); |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 594 | return true; |
| 595 | } |
| 596 | } |
| 597 | return false; |
| 598 | } |
| 599 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 600 | void add(LogBufferElement* element) { |
| 601 | LogBufferElementKey key(element->getUid(), element->getPid(), |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 602 | element->getTid()); |
| 603 | map[key.getKey()] = element; |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Mark Salyzyn | be1a30c | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 606 | inline void clear() { |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 607 | map.clear(); |
Mark Salyzyn | be1a30c | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 610 | void clear(LogBufferElement* element) { |
Mark Salyzyn | d18255f | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 611 | log_time current = |
| 612 | element->getRealTime() - log_time(EXPIRE_RATELIMIT, 0); |
Mark Salyzyn | 6505953 | 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 | d18255f | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 616 | (current > mapElement->getRealTime())) { |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 617 | it = map.erase(it); |
| 618 | } else { |
| 619 | ++it; |
Mark Salyzyn | be1a30c | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | } |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 623 | }; |
| 624 | |
Mark Salyzyn | 434f374 | 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. |
Tom Cherry | 59fc119 | 2019-11-15 17:37:03 -0800 | [diff] [blame] | 641 | android::prdebug("Kicking blocked reader, pid %d, from LogBuffer::kickMe()\n", |
| 642 | me->mClient->getPid()); |
Mark Salyzyn | 434f374 | 2017-05-11 13:28:33 -0700 | [diff] [blame] | 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 |
Tom Cherry | 59fc119 | 2019-11-15 17:37:03 -0800 | [diff] [blame] | 650 | android::prdebug( |
| 651 | "Skipping %lu entries from slow reader, pid %d, from LogBuffer::kickMe()\n", |
| 652 | pruneRows, me->mClient->getPid()); |
Mark Salyzyn | 434f374 | 2017-05-11 13:28:33 -0700 | [diff] [blame] | 653 | me->triggerSkip_Locked(id, pruneRows); |
| 654 | } |
| 655 | } |
| 656 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 657 | // prune "pruneRows" of type "id" from the buffer. |
| 658 | // |
Mark Salyzyn | e2827d5 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 659 | // This garbage collection task is used to expire log entries. It is called to |
| 660 | // remove all logs (clear), all UID logs (unprivileged clear), or every |
| 661 | // 256 or 10% of the total logs (whichever is less) to prune the logs. |
| 662 | // |
| 663 | // First there is a prep phase where we discover the reader region lock that |
| 664 | // acts as a backstop to any pruning activity to stop there and go no further. |
| 665 | // |
| 666 | // There are three major pruning loops that follow. All expire from the oldest |
| 667 | // entries. Since there are multiple log buffers, the Android logging facility |
| 668 | // will appear to drop entries 'in the middle' when looking at multiple log |
| 669 | // sources and buffers. This effect is slightly more prominent when we prune |
| 670 | // the worst offender by logging source. Thus the logs slowly loose content |
| 671 | // and value as you move back in time. This is preferred since chatty sources |
| 672 | // invariably move the logs value down faster as less chatty sources would be |
| 673 | // expired in the noise. |
| 674 | // |
| 675 | // The first loop performs blacklisting and worst offender pruning. Falling |
| 676 | // through when there are no notable worst offenders and have not hit the |
| 677 | // region lock preventing further worst offender pruning. This loop also looks |
| 678 | // after managing the chatty log entries and merging to help provide |
| 679 | // statistical basis for blame. The chatty entries are not a notification of |
| 680 | // how much logs you may have, but instead represent how much logs you would |
| 681 | // have had in a virtual log buffer that is extended to cover all the in-memory |
| 682 | // logs without loss. They last much longer than the represented pruned logs |
| 683 | // since they get multiplied by the gains in the non-chatty log sources. |
| 684 | // |
| 685 | // The second loop get complicated because an algorithm of watermarks and |
| 686 | // history is maintained to reduce the order and keep processing time |
| 687 | // down to a minimum at scale. These algorithms can be costly in the face |
| 688 | // of larger log buffers, or severly limited processing time granted to a |
| 689 | // background task at lowest priority. |
| 690 | // |
| 691 | // This second loop does straight-up expiration from the end of the logs |
| 692 | // (again, remember for the specified log buffer id) but does some whitelist |
| 693 | // preservation. Thus whitelist is a Hail Mary low priority, blacklists and |
| 694 | // spam filtration all take priority. This second loop also checks if a region |
| 695 | // lock is causing us to buffer too much in the logs to help the reader(s), |
| 696 | // and will tell the slowest reader thread to skip log entries, and if |
| 697 | // persistent and hits a further threshold, kill the reader thread. |
| 698 | // |
| 699 | // The third thread is optional, and only gets hit if there was a whitelist |
| 700 | // and more needs to be pruned against the backstop of the region lock. |
| 701 | // |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 702 | // LogBuffer::wrlock() must be held when this function is called. |
Mark Salyzyn | e2827d5 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 703 | // |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 704 | bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) { |
Mark Salyzyn | bec7b2d | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 705 | LogTimeEntry* oldest = nullptr; |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 706 | bool busy = false; |
Mark Salyzyn | 71541f3 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 707 | bool clearAll = pruneRows == ULONG_MAX; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 708 | |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 709 | LogTimeEntry::rdlock(); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 710 | |
| 711 | // Region locked? |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 712 | LastLogTimes::iterator times = mTimes.begin(); |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 713 | while (times != mTimes.end()) { |
Tom Cherry | 06e478b | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 714 | LogTimeEntry* entry = times->get(); |
| 715 | if (entry->isWatching(id) && |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 716 | (!oldest || (oldest->mStart > entry->mStart) || |
| 717 | ((oldest->mStart == entry->mStart) && |
| 718 | (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 719 | oldest = entry; |
| 720 | } |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 721 | times++; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 722 | } |
Mark Salyzyn | 782d4d2 | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 723 | log_time watermark(log_time::tv_sec_max, log_time::tv_nsec_max); |
| 724 | if (oldest) watermark = oldest->mStart - pruneMargin; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 725 | |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 726 | LogBufferElementCollection::iterator it; |
| 727 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 728 | if (__predict_false(caller_uid != AID_ROOT)) { // unlikely |
Mark Salyzyn | 9e0c78e | 2016-09-01 15:48:36 -0700 | [diff] [blame] | 729 | // Only here if clear all request from non system source, so chatty |
| 730 | // filter logistics is not required. |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 731 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
| 732 | while (it != mLogElements.end()) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 733 | LogBufferElement* element = *it; |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 734 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 735 | if ((element->getLogId() != id) || |
| 736 | (element->getUid() != caller_uid)) { |
Mark Salyzyn | 71541f3 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 737 | ++it; |
| 738 | continue; |
| 739 | } |
| 740 | |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 741 | if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) { |
| 742 | mLast[id] = it; |
| 743 | mLastSet[id] = true; |
| 744 | } |
| 745 | |
Mark Salyzyn | 782d4d2 | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 746 | if (oldest && (watermark <= element->getRealTime())) { |
Mark Salyzyn | 434f374 | 2017-05-11 13:28:33 -0700 | [diff] [blame] | 747 | busy = isBusy(watermark); |
| 748 | if (busy) kickMe(oldest, id, pruneRows); |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 749 | break; |
| 750 | } |
| 751 | |
Mark Salyzyn | 71541f3 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 752 | it = erase(it); |
Mark Salyzyn | 9e0c78e | 2016-09-01 15:48:36 -0700 | [diff] [blame] | 753 | if (--pruneRows == 0) { |
| 754 | break; |
| 755 | } |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 756 | } |
| 757 | LogTimeEntry::unlock(); |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 758 | return busy; |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 761 | // prune by worst offenders; by blacklist, UID, and by PID of system UID |
Mark Salyzyn | c969086 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 762 | bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty(); |
Mark Salyzyn | 71541f3 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 763 | while (!clearAll && (pruneRows > 0)) { |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 764 | // recalculate the worst offender on every batched pass |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 765 | int worst = -1; // not valid for getUid() or getKey() |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 766 | size_t worst_sizes = 0; |
| 767 | size_t second_worst_sizes = 0; |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 768 | pid_t worstPid = 0; // POSIX guarantees PID != 0 |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 769 | |
Mark Salyzyn | 0e765c6 | 2015-03-17 17:17:25 -0700 | [diff] [blame] | 770 | if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) { |
Mark Salyzyn | 6fa0d03 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 771 | // Calculate threshold as 12.5% of available storage |
| 772 | size_t threshold = log_buffer_size(id) / 8; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 773 | |
Mark Salyzyn | 6fa0d03 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 774 | if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 775 | stats.sortTags(AID_ROOT, (pid_t)0, 2, id) |
| 776 | .findWorst(worst, worst_sizes, second_worst_sizes, |
| 777 | threshold); |
Mark Salyzyn | ab94771 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 778 | // per-pid filter for AID_SYSTEM sources is too complex |
Mark Salyzyn | 6fa0d03 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 779 | } else { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 780 | stats.sort(AID_ROOT, (pid_t)0, 2, id) |
| 781 | .findWorst(worst, worst_sizes, second_worst_sizes, |
| 782 | threshold); |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 783 | |
Mark Salyzyn | 6fa0d03 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 784 | if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 785 | stats.sortPids(worst, (pid_t)0, 2, id) |
| 786 | .findWorst(worstPid, worst_sizes, second_worst_sizes); |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 787 | } |
| 788 | } |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 789 | } |
| 790 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 791 | // skip if we have neither worst nor naughty filters |
Mark Salyzyn | 6fa0d03 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 792 | if ((worst == -1) && !hasBlacklist) { |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 793 | break; |
| 794 | } |
| 795 | |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 796 | bool kick = false; |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 797 | bool leading = true; |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 798 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
Mark Salyzyn | e2827d5 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 799 | // Perform at least one mandatory garbage collection cycle in following |
| 800 | // - clear leading chatty tags |
Mark Salyzyn | 5c5189a | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 801 | // - coalesce chatty tags |
Mark Salyzyn | e2827d5 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 802 | // - check age-out of preserved logs |
| 803 | bool gc = pruneRows <= 1; |
Mark Salyzyn | 6fa0d03 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 804 | if (!gc && (worst != -1)) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 805 | { // begin scope for worst found iterator |
| 806 | LogBufferIteratorMap::iterator found = |
| 807 | mLastWorst[id].find(worst); |
| 808 | if ((found != mLastWorst[id].end()) && |
| 809 | (found->second != mLogElements.end())) { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 810 | leading = false; |
| 811 | it = found->second; |
| 812 | } |
| 813 | } |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 814 | if (worstPid) { // begin scope for pid worst found iterator |
Mark Salyzyn | ab94771 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 815 | // FYI: worstPid only set if !LOG_ID_EVENTS and |
| 816 | // !LOG_ID_SECURITY, not going to make that assumption ... |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 817 | LogBufferPidIteratorMap::iterator found = |
| 818 | mLastWorstPidOfSystem[id].find(worstPid); |
| 819 | if ((found != mLastWorstPidOfSystem[id].end()) && |
| 820 | (found->second != mLogElements.end())) { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 821 | leading = false; |
| 822 | it = found->second; |
| 823 | } |
Mark Salyzyn | 5af07be | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 824 | } |
| 825 | } |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 826 | static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 }; |
Mark Salyzyn | 7f62d6b | 2015-08-24 13:43:27 -0700 | [diff] [blame] | 827 | LogBufferElementCollection::iterator lastt; |
| 828 | lastt = mLogElements.end(); |
| 829 | --lastt; |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 830 | LogBufferElementLast last; |
Mark Salyzyn | 5af07be | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 831 | while (it != mLogElements.end()) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 832 | LogBufferElement* element = *it; |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 833 | |
Mark Salyzyn | 782d4d2 | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 834 | if (oldest && (watermark <= element->getRealTime())) { |
Mark Salyzyn | 434f374 | 2017-05-11 13:28:33 -0700 | [diff] [blame] | 835 | busy = isBusy(watermark); |
| 836 | // Do not let chatty eliding trigger any reader mitigation |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 837 | break; |
| 838 | } |
| 839 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 840 | if (element->getLogId() != id) { |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 841 | ++it; |
| 842 | continue; |
| 843 | } |
Mark Salyzyn | 1117818 | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 844 | // below this point element->getLogId() == id |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 845 | |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 846 | if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) { |
| 847 | mLast[id] = it; |
| 848 | mLastSet[id] = true; |
| 849 | } |
| 850 | |
Chih-Hung Hsieh | 9ff8155 | 2018-08-13 14:22:56 -0700 | [diff] [blame] | 851 | uint16_t dropped = element->getDropped(); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 852 | |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 853 | // remove any leading drops |
| 854 | if (leading && dropped) { |
| 855 | it = erase(it); |
| 856 | continue; |
| 857 | } |
| 858 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 859 | if (dropped && last.coalesce(element, dropped)) { |
Mark Salyzyn | 5c5189a | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 860 | it = erase(it, true); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 861 | continue; |
| 862 | } |
| 863 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 864 | int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) |
| 865 | ? element->getTag() |
| 866 | : element->getUid(); |
Mark Salyzyn | 6fa0d03 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 867 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 868 | if (hasBlacklist && mPrune.naughty(element)) { |
| 869 | last.clear(element); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 870 | it = erase(it); |
| 871 | if (dropped) { |
| 872 | continue; |
| 873 | } |
| 874 | |
| 875 | pruneRows--; |
| 876 | if (pruneRows == 0) { |
| 877 | break; |
| 878 | } |
| 879 | |
Mark Salyzyn | 6fa0d03 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 880 | if (key == worst) { |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 881 | kick = true; |
| 882 | if (worst_sizes < second_worst_sizes) { |
| 883 | break; |
| 884 | } |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 885 | worst_sizes -= element->getMsgLen(); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 886 | } |
| 887 | continue; |
| 888 | } |
| 889 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 890 | if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) || |
| 891 | (element->getRealTime() > (*lastt)->getRealTime())) { |
Mark Salyzyn | 7f62d6b | 2015-08-24 13:43:27 -0700 | [diff] [blame] | 892 | break; |
| 893 | } |
| 894 | |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 895 | if (dropped) { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 896 | last.add(element); |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 897 | if (worstPid && |
| 898 | ((!gc && (element->getPid() == worstPid)) || |
| 899 | (mLastWorstPidOfSystem[id].find(element->getPid()) == |
| 900 | mLastWorstPidOfSystem[id].end()))) { |
Mark Salyzyn | 1117818 | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 901 | // element->getUid() may not be AID_SYSTEM, next best |
Mark Salyzyn | ab94771 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 902 | // watermark if current one empty. id is not LOG_ID_EVENTS |
| 903 | // or LOG_ID_SECURITY because of worstPid check. |
Mark Salyzyn | ce50dec | 2016-09-01 07:28:44 -0700 | [diff] [blame] | 904 | mLastWorstPidOfSystem[id][element->getPid()] = it; |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 905 | } |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 906 | if ((!gc && !worstPid && (key == worst)) || |
| 907 | (mLastWorst[id].find(key) == mLastWorst[id].end())) { |
Mark Salyzyn | 6fa0d03 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 908 | mLastWorst[id][key] = it; |
Mark Salyzyn | 76aab49 | 2015-08-24 13:43:27 -0700 | [diff] [blame] | 909 | } |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 910 | ++it; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 911 | continue; |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 912 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 913 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 914 | if ((key != worst) || |
| 915 | (worstPid && (element->getPid() != worstPid))) { |
Mark Salyzyn | fd318a8 | 2015-06-01 09:41:19 -0700 | [diff] [blame] | 916 | leading = false; |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 917 | last.clear(element); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 918 | ++it; |
| 919 | continue; |
| 920 | } |
Mark Salyzyn | 1117818 | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 921 | // key == worst below here |
| 922 | // If worstPid set, then element->getPid() == worstPid below here |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 923 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 924 | pruneRows--; |
| 925 | if (pruneRows == 0) { |
| 926 | break; |
| 927 | } |
| 928 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 929 | kick = true; |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 930 | |
Chih-Hung Hsieh | 9ff8155 | 2018-08-13 14:22:56 -0700 | [diff] [blame] | 931 | uint16_t len = element->getMsgLen(); |
Mark Salyzyn | 65ec860 | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 932 | |
| 933 | // do not create any leading drops |
| 934 | if (leading) { |
| 935 | it = erase(it); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 936 | } else { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 937 | stats.drop(element); |
| 938 | element->setDropped(1); |
| 939 | if (last.coalesce(element, 1)) { |
Mark Salyzyn | 5c5189a | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 940 | it = erase(it, true); |
Mark Salyzyn | 65ec860 | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 941 | } else { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 942 | last.add(element); |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 943 | if (worstPid && |
| 944 | (!gc || (mLastWorstPidOfSystem[id].find(worstPid) == |
| 945 | mLastWorstPidOfSystem[id].end()))) { |
Mark Salyzyn | 1117818 | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 946 | // element->getUid() may not be AID_SYSTEM, next best |
Mark Salyzyn | ab94771 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 947 | // watermark if current one empty. id is not |
| 948 | // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid. |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 949 | mLastWorstPidOfSystem[id][worstPid] = it; |
| 950 | } |
Mark Salyzyn | 6fa0d03 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 951 | if ((!gc && !worstPid) || |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 952 | (mLastWorst[id].find(worst) == mLastWorst[id].end())) { |
Mark Salyzyn | 6fa0d03 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 953 | mLastWorst[id][worst] = it; |
Mark Salyzyn | e2827d5 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 954 | } |
Mark Salyzyn | 65ec860 | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 955 | ++it; |
| 956 | } |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 957 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 958 | if (worst_sizes < second_worst_sizes) { |
| 959 | break; |
| 960 | } |
| 961 | worst_sizes -= len; |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 962 | } |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 963 | last.clear(); |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 964 | |
Mark Salyzyn | c402bc7 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 965 | if (!kick || !mPrune.worstUidEnabled()) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 966 | break; // the following loop will ask bad clients to skip/drop |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 970 | bool whitelist = false; |
Mark Salyzyn | c969086 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 971 | bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll; |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 972 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 973 | while ((pruneRows > 0) && (it != mLogElements.end())) { |
| 974 | LogBufferElement* element = *it; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 975 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 976 | if (element->getLogId() != id) { |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 977 | it++; |
| 978 | continue; |
| 979 | } |
| 980 | |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 981 | if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) { |
| 982 | mLast[id] = it; |
| 983 | mLastSet[id] = true; |
| 984 | } |
| 985 | |
Mark Salyzyn | 782d4d2 | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 986 | if (oldest && (watermark <= element->getRealTime())) { |
Mark Salyzyn | 434f374 | 2017-05-11 13:28:33 -0700 | [diff] [blame] | 987 | busy = isBusy(watermark); |
| 988 | if (!whitelist && busy) kickMe(oldest, id, pruneRows); |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 989 | break; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 990 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 991 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 992 | if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) { |
| 993 | // WhiteListed |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 994 | whitelist = true; |
| 995 | it++; |
| 996 | continue; |
| 997 | } |
| 998 | |
| 999 | it = erase(it); |
| 1000 | pruneRows--; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1001 | } |
| 1002 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 1003 | // Do not save the whitelist if we are reader range limited |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1004 | if (whitelist && (pruneRows > 0)) { |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 1005 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1006 | while ((it != mLogElements.end()) && (pruneRows > 0)) { |
| 1007 | LogBufferElement* element = *it; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 1008 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 1009 | if (element->getLogId() != id) { |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 1010 | ++it; |
| 1011 | continue; |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1012 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 1013 | |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 1014 | if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) { |
| 1015 | mLast[id] = it; |
| 1016 | mLastSet[id] = true; |
| 1017 | } |
| 1018 | |
Mark Salyzyn | 782d4d2 | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 1019 | if (oldest && (watermark <= element->getRealTime())) { |
Mark Salyzyn | 434f374 | 2017-05-11 13:28:33 -0700 | [diff] [blame] | 1020 | busy = isBusy(watermark); |
| 1021 | if (busy) kickMe(oldest, id, pruneRows); |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 1022 | break; |
| 1023 | } |
| 1024 | |
| 1025 | it = erase(it); |
| 1026 | pruneRows--; |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1027 | } |
| 1028 | } |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1029 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1030 | LogTimeEntry::unlock(); |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1031 | |
| 1032 | return (pruneRows > 0) && busy; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | // clear all rows of type "id" from the buffer. |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1036 | bool LogBuffer::clear(log_id_t id, uid_t uid) { |
| 1037 | bool busy = true; |
| 1038 | // If it takes more than 4 tries (seconds) to clear, then kill reader(s) |
| 1039 | for (int retry = 4;;) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1040 | if (retry == 1) { // last pass |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1041 | // Check if it is still busy after the sleep, we say prune |
| 1042 | // one entry, not another clear run, so we are looking for |
| 1043 | // the quick side effect of the return value to tell us if |
| 1044 | // we have a _blocked_ reader. |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1045 | wrlock(); |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1046 | busy = prune(id, 1, uid); |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1047 | unlock(); |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1048 | // It is still busy, blocked reader(s), lets kill them all! |
| 1049 | // otherwise, lets be a good citizen and preserve the slow |
| 1050 | // readers and let the clear run (below) deal with determining |
| 1051 | // if we are still blocked and return an error code to caller. |
| 1052 | if (busy) { |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1053 | LogTimeEntry::wrlock(); |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1054 | LastLogTimes::iterator times = mTimes.begin(); |
| 1055 | while (times != mTimes.end()) { |
Tom Cherry | 06e478b | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 1056 | LogTimeEntry* entry = times->get(); |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1057 | // Killer punch |
Tom Cherry | 06e478b | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 1058 | if (entry->isWatching(id)) { |
Tom Cherry | 59fc119 | 2019-11-15 17:37:03 -0800 | [diff] [blame] | 1059 | android::prdebug( |
| 1060 | "Kicking blocked reader, pid %d, from LogBuffer::clear()\n", |
| 1061 | entry->mClient->getPid()); |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1062 | entry->release_Locked(); |
| 1063 | } |
| 1064 | times++; |
| 1065 | } |
| 1066 | LogTimeEntry::unlock(); |
| 1067 | } |
| 1068 | } |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1069 | wrlock(); |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1070 | busy = prune(id, ULONG_MAX, uid); |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1071 | unlock(); |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1072 | if (!busy || !--retry) { |
| 1073 | break; |
| 1074 | } |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1075 | sleep(1); // Let reader(s) catch up after notification |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1076 | } |
| 1077 | return busy; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | // get the used space associated with "id". |
| 1081 | unsigned long LogBuffer::getSizeUsed(log_id_t id) { |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1082 | rdlock(); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1083 | size_t retval = stats.sizes(id); |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1084 | unlock(); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1085 | return retval; |
| 1086 | } |
| 1087 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1088 | // set the total space allocated to "id" |
| 1089 | int LogBuffer::setSize(log_id_t id, unsigned long size) { |
| 1090 | // Reasonable limits ... |
Mark Salyzyn | b004083 | 2016-09-27 13:08:23 -0700 | [diff] [blame] | 1091 | if (!__android_logger_valid_buffer_size(size)) { |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1092 | return -1; |
| 1093 | } |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1094 | wrlock(); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1095 | log_buffer_size(id) = size; |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1096 | unlock(); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1097 | return 0; |
| 1098 | } |
| 1099 | |
| 1100 | // get the total space allocated to "id" |
| 1101 | unsigned long LogBuffer::getSize(log_id_t id) { |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1102 | rdlock(); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1103 | size_t retval = log_buffer_size(id); |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1104 | unlock(); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1105 | return retval; |
| 1106 | } |
| 1107 | |
Mark Salyzyn | bec7b2d | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 1108 | log_time LogBuffer::flushTo(SocketClient* reader, const log_time& start, |
| 1109 | pid_t* lastTid, bool privileged, bool security, |
| 1110 | int (*filter)(const LogBufferElement* element, |
| 1111 | void* arg), |
| 1112 | void* arg) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1113 | LogBufferElementCollection::iterator it; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1114 | uid_t uid = reader->getUid(); |
| 1115 | |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1116 | rdlock(); |
Dragoslav Mitrinovic | 81624f2 | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1117 | |
Mark Salyzyn | d18255f | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 1118 | if (start == log_time::EPOCH) { |
Dragoslav Mitrinovic | 81624f2 | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1119 | // client wants to start from the beginning |
| 1120 | it = mLogElements.begin(); |
| 1121 | } else { |
Mark Salyzyn | 9a3e8a4 | 2017-04-04 10:57:41 -0700 | [diff] [blame] | 1122 | // Cap to 300 iterations we look back for out-of-order entries. |
| 1123 | size_t count = 300; |
Mark Salyzyn | 782d4d2 | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 1124 | |
Dragoslav Mitrinovic | 81624f2 | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1125 | // Client wants to start from some specified time. Chances are |
| 1126 | // we are better off starting from the end of the time sorted list. |
Mark Salyzyn | 782d4d2 | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 1127 | LogBufferElementCollection::iterator last; |
Mark Salyzyn | 46bd518 | 2017-03-27 13:12:41 -0700 | [diff] [blame] | 1128 | for (last = it = mLogElements.end(); it != mLogElements.begin(); |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1129 | /* do nothing */) { |
Dragoslav Mitrinovic | 81624f2 | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1130 | --it; |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1131 | LogBufferElement* element = *it; |
Mark Salyzyn | 242daa9 | 2017-03-10 10:31:48 -0800 | [diff] [blame] | 1132 | if (element->getRealTime() > start) { |
| 1133 | last = it; |
Mark Salyzyn | cb722de | 2017-05-18 10:06:00 -0700 | [diff] [blame] | 1134 | } else if (element->getRealTime() == start) { |
| 1135 | last = ++it; |
| 1136 | break; |
Joe Onorato | 8804f6b | 2018-04-04 14:35:34 -0700 | [diff] [blame] | 1137 | } else if (!--count) { |
Dragoslav Mitrinovic | 81624f2 | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1138 | break; |
| 1139 | } |
| 1140 | } |
Mark Salyzyn | 242daa9 | 2017-03-10 10:31:48 -0800 | [diff] [blame] | 1141 | it = last; |
Dragoslav Mitrinovic | 81624f2 | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1142 | } |
| 1143 | |
Mark Salyzyn | cb722de | 2017-05-18 10:06:00 -0700 | [diff] [blame] | 1144 | log_time curr = start; |
Mark Salyzyn | 1d7b071 | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 1145 | |
Mark Salyzyn | 1b1f061 | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 1146 | LogBufferElement* lastElement = nullptr; // iterator corruption paranoia |
| 1147 | static const size_t maxSkip = 4194304; // maximum entries to skip |
| 1148 | size_t skip = maxSkip; |
Dragoslav Mitrinovic | 81624f2 | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1149 | for (; it != mLogElements.end(); ++it) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1150 | LogBufferElement* element = *it; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1151 | |
Mark Salyzyn | 1b1f061 | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 1152 | if (!--skip) { |
| 1153 | android::prdebug("reader.per: too many elements skipped"); |
| 1154 | break; |
| 1155 | } |
| 1156 | if (element == lastElement) { |
| 1157 | android::prdebug("reader.per: identical elements"); |
| 1158 | break; |
| 1159 | } |
| 1160 | lastElement = element; |
| 1161 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1162 | if (!privileged && (element->getUid() != uid)) { |
| 1163 | continue; |
| 1164 | } |
| 1165 | |
Mark Salyzyn | b10f737 | 2016-01-26 14:32:35 -0800 | [diff] [blame] | 1166 | if (!security && (element->getLogId() == LOG_ID_SECURITY)) { |
| 1167 | continue; |
| 1168 | } |
| 1169 | |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1170 | // NB: calling out to another object with wrlock() held (safe) |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 1171 | if (filter) { |
| 1172 | int ret = (*filter)(element, arg); |
| 1173 | if (ret == false) { |
| 1174 | continue; |
| 1175 | } |
| 1176 | if (ret != true) { |
| 1177 | break; |
| 1178 | } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1179 | } |
| 1180 | |
Mark Salyzyn | bec7b2d | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 1181 | bool sameTid = false; |
| 1182 | if (lastTid) { |
| 1183 | sameTid = lastTid[element->getLogId()] == element->getTid(); |
| 1184 | // Dropped (chatty) immediately following a valid log from the |
| 1185 | // same source in the same log buffer indicates we have a |
| 1186 | // multiple identical squash. chatty that differs source |
| 1187 | // is due to spam filter. chatty to chatty of different |
| 1188 | // source is also due to spam filter. |
| 1189 | lastTid[element->getLogId()] = |
| 1190 | (element->getDropped() && !sameTid) ? 0 : element->getTid(); |
| 1191 | } |
Mark Salyzyn | 1d7b071 | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 1192 | |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1193 | unlock(); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1194 | |
| 1195 | // range locking in LastLogTimes looks after us |
Tom Cherry | e0ac158 | 2019-10-15 15:10:26 -0700 | [diff] [blame] | 1196 | curr = element->flushTo(reader, this, sameTid); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1197 | |
Mark Salyzyn | cb722de | 2017-05-18 10:06:00 -0700 | [diff] [blame] | 1198 | if (curr == element->FLUSH_ERROR) { |
| 1199 | return curr; |
Mark Salyzyn | e2c1ea4 | 2017-05-17 19:55:12 +0000 | [diff] [blame] | 1200 | } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1201 | |
Mark Salyzyn | 1b1f061 | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 1202 | skip = maxSkip; |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1203 | rdlock(); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1204 | } |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1205 | unlock(); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1206 | |
Mark Salyzyn | cb722de | 2017-05-18 10:06:00 -0700 | [diff] [blame] | 1207 | return curr; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1208 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1209 | |
Mark Salyzyn | 4a2fc1c | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 1210 | std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid, |
| 1211 | unsigned int logMask) { |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1212 | wrlock(); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1213 | |
Mark Salyzyn | 4a2fc1c | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 1214 | std::string ret = stats.format(uid, pid, logMask); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1215 | |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1216 | unlock(); |
Mark Salyzyn | 7c528d9 | 2015-08-20 10:01:44 -0700 | [diff] [blame] | 1217 | |
| 1218 | return ret; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1219 | } |