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 | */ |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 16 | // for manual checking of stale entries during ChattyLogBuffer::erase() |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 17 | //#define DEBUG_CHECK_FOR_STALE_ENTRIES |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 18 | |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 19 | #include "ChattyLogBuffer.h" |
| 20 | |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 21 | #include <ctype.h> |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 22 | #include <endian.h> |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 23 | #include <errno.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <string.h> |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 26 | #include <sys/cdefs.h> |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 27 | #include <sys/user.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 28 | #include <time.h> |
| 29 | #include <unistd.h> |
| 30 | |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 31 | #include <limits> |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 32 | #include <unordered_map> |
Tom Cherry | 10d086e | 2019-08-21 14:16:34 -0700 | [diff] [blame] | 33 | #include <utility> |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 34 | |
Mark Salyzyn | f10e273 | 2016-09-27 13:08:23 -0700 | [diff] [blame] | 35 | #include <private/android_logger.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 36 | |
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 | |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 46 | void ChattyLogBuffer::Init() { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 47 | log_id_for_each(i) { |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 48 | if (SetSize(i, __android_logger_get_buffer_size(i))) { |
| 49 | SetSize(i, LOG_BUFFER_MIN_SIZE); |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 50 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 51 | } |
Tom Cherry | e170d1a | 2020-05-01 16:45:25 -0700 | [diff] [blame] | 52 | // Release any sleeping reader threads to dump their current content. |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 53 | auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()}; |
| 54 | for (const auto& reader_thread : reader_list_->reader_threads()) { |
| 55 | reader_thread->triggerReader_Locked(); |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 56 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 59 | ChattyLogBuffer::ChattyLogBuffer(LogReaderList* reader_list, LogTags* tags, PruneList* prune, |
| 60 | LogStatistics* stats) |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 61 | : reader_list_(reader_list), tags_(tags), prune_(prune), stats_(stats) { |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 62 | Init(); |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Tom Cherry | a26f7df | 2020-05-19 17:48:42 -0700 | [diff] [blame^] | 65 | ChattyLogBuffer::~ChattyLogBuffer() {} |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 66 | |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 67 | LogBufferElementCollection::iterator ChattyLogBuffer::GetOldest(log_id_t log_id) { |
Tom Cherry | 385c2c9 | 2020-04-29 17:58:18 -0700 | [diff] [blame] | 68 | auto it = mLogElements.begin(); |
Tom Cherry | 20118ee | 2020-05-04 10:17:42 -0700 | [diff] [blame] | 69 | if (oldest_[log_id]) { |
| 70 | it = *oldest_[log_id]; |
Tom Cherry | 385c2c9 | 2020-04-29 17:58:18 -0700 | [diff] [blame] | 71 | } |
| 72 | while (it != mLogElements.end() && (*it)->getLogId() != log_id) { |
| 73 | it++; |
| 74 | } |
| 75 | if (it != mLogElements.end()) { |
Tom Cherry | 20118ee | 2020-05-04 10:17:42 -0700 | [diff] [blame] | 76 | oldest_[log_id] = it; |
Tom Cherry | 385c2c9 | 2020-04-29 17:58:18 -0700 | [diff] [blame] | 77 | } |
| 78 | return it; |
| 79 | } |
| 80 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 81 | enum match_type { DIFFERENT, SAME, SAME_LIBLOG }; |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 82 | |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 83 | static enum match_type identical(LogBufferElement* elem, LogBufferElement* last) { |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 84 | // is it mostly identical? |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 85 | // if (!elem) return DIFFERENT; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 86 | ssize_t lenl = elem->getMsgLen(); |
| 87 | if (lenl <= 0) return DIFFERENT; // value if this represents a chatty elem |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 88 | // if (!last) return DIFFERENT; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 89 | ssize_t lenr = last->getMsgLen(); |
| 90 | if (lenr <= 0) return DIFFERENT; // value if this represents a chatty elem |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 91 | // if (elem->getLogId() != last->getLogId()) return DIFFERENT; |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 92 | if (elem->getUid() != last->getUid()) return DIFFERENT; |
| 93 | if (elem->getPid() != last->getPid()) return DIFFERENT; |
| 94 | if (elem->getTid() != last->getTid()) return DIFFERENT; |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 95 | |
| 96 | // last is more than a minute old, stop squashing identical messages |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 97 | if (elem->getRealTime().nsec() > (last->getRealTime().nsec() + 60 * NS_PER_SEC)) |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 98 | return DIFFERENT; |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 99 | |
| 100 | // Identical message |
| 101 | const char* msgl = elem->getMsg(); |
| 102 | const char* msgr = last->getMsg(); |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 103 | if (lenl == lenr) { |
| 104 | if (!fastcmp<memcmp>(msgl, msgr, lenl)) return SAME; |
| 105 | // liblog tagged messages (content gets summed) |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 106 | if (elem->getLogId() == LOG_ID_EVENTS && lenl == sizeof(android_log_event_int_t) && |
| 107 | !fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_int_t) - sizeof(int32_t)) && |
| 108 | elem->getTag() == LIBLOG_LOG_TAG) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 109 | return SAME_LIBLOG; |
Mark Salyzyn | 1598fe0 | 2017-03-13 15:41:59 -0700 | [diff] [blame] | 110 | } |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 111 | } |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 112 | |
| 113 | // audit message (except sequence number) identical? |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 114 | if (last->isBinary() && lenl > static_cast<ssize_t>(sizeof(android_log_event_string_t)) && |
| 115 | lenr > static_cast<ssize_t>(sizeof(android_log_event_string_t))) { |
| 116 | if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) - sizeof(int32_t))) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 117 | return DIFFERENT; |
Mark Salyzyn | 1598fe0 | 2017-03-13 15:41:59 -0700 | [diff] [blame] | 118 | } |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 119 | msgl += sizeof(android_log_event_string_t); |
| 120 | lenl -= sizeof(android_log_event_string_t); |
| 121 | msgr += sizeof(android_log_event_string_t); |
| 122 | lenr -= sizeof(android_log_event_string_t); |
| 123 | } |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 124 | static const char avc[] = "): avc: "; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 125 | const char* avcl = android::strnstr(msgl, lenl, avc); |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 126 | if (!avcl) return DIFFERENT; |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 127 | lenl -= avcl - msgl; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 128 | const char* avcr = android::strnstr(msgr, lenr, avc); |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 129 | if (!avcr) return DIFFERENT; |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 130 | lenr -= avcr - msgr; |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 131 | if (lenl != lenr) return DIFFERENT; |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 132 | if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc), lenl - strlen(avc))) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 133 | return DIFFERENT; |
Mark Salyzyn | 1598fe0 | 2017-03-13 15:41:59 -0700 | [diff] [blame] | 134 | } |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 135 | return SAME; |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Tom Cherry | a26f7df | 2020-05-19 17:48:42 -0700 | [diff] [blame^] | 138 | bool ChattyLogBuffer::ShouldLog(log_id_t log_id, const char* msg, uint16_t len) { |
Tom Cherry | 2ac86de | 2020-02-20 13:21:51 -0800 | [diff] [blame] | 139 | if (log_id == LOG_ID_SECURITY) { |
Tom Cherry | a26f7df | 2020-05-19 17:48:42 -0700 | [diff] [blame^] | 140 | return true; |
Tom Cherry | 2ac86de | 2020-02-20 13:21:51 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | int prio = ANDROID_LOG_INFO; |
| 144 | const char* tag = nullptr; |
| 145 | size_t tag_len = 0; |
| 146 | if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) { |
Tom Cherry | a26f7df | 2020-05-19 17:48:42 -0700 | [diff] [blame^] | 147 | if (len < sizeof(android_event_header_t)) { |
| 148 | return false; |
| 149 | } |
| 150 | int32_t numeric_tag = reinterpret_cast<const android_event_header_t*>(msg)->tag; |
| 151 | tag = tags_->tagToName(numeric_tag); |
Tom Cherry | 2ac86de | 2020-02-20 13:21:51 -0800 | [diff] [blame] | 152 | if (tag) { |
| 153 | tag_len = strlen(tag); |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 154 | } |
Tom Cherry | 2ac86de | 2020-02-20 13:21:51 -0800 | [diff] [blame] | 155 | } else { |
| 156 | prio = *msg; |
| 157 | tag = msg + 1; |
| 158 | tag_len = strnlen(tag, len - 1); |
| 159 | } |
Tom Cherry | a26f7df | 2020-05-19 17:48:42 -0700 | [diff] [blame^] | 160 | return __android_log_is_loggable_len(prio, tag, tag_len, ANDROID_LOG_VERBOSE); |
| 161 | } |
| 162 | |
| 163 | int ChattyLogBuffer::Log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid, |
| 164 | const char* msg, uint16_t len) { |
| 165 | if (log_id >= LOG_ID_MAX) { |
| 166 | return -EINVAL; |
| 167 | } |
| 168 | |
| 169 | if (!ShouldLog(log_id, msg, len)) { |
Tom Cherry | 2ac86de | 2020-02-20 13:21:51 -0800 | [diff] [blame] | 170 | // Log traffic received to total |
Tom Cherry | a26f7df | 2020-05-19 17:48:42 -0700 | [diff] [blame^] | 171 | stats_->AddTotal(log_id, len); |
Tom Cherry | 2ac86de | 2020-02-20 13:21:51 -0800 | [diff] [blame] | 172 | return -EACCES; |
Mark Salyzyn | e59c469 | 2014-10-02 13:07:05 -0700 | [diff] [blame] | 173 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 174 | |
Tom Cherry | a26f7df | 2020-05-19 17:48:42 -0700 | [diff] [blame^] | 175 | // Slip the time by 1 nsec if the incoming lands on xxxxxx000 ns. |
| 176 | // This prevents any chance that an outside source can request an |
| 177 | // exact entry with time specified in ms or us precision. |
| 178 | if ((realtime.tv_nsec % 1000) == 0) ++realtime.tv_nsec; |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 179 | |
Tom Cherry | a26f7df | 2020-05-19 17:48:42 -0700 | [diff] [blame^] | 180 | std::unique_ptr<LogBufferElement> elem( |
| 181 | new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len)); |
| 182 | |
| 183 | // b/137093665: don't coalesce security messages. |
| 184 | if (log_id == LOG_ID_SECURITY) { |
| 185 | auto lock = std::lock_guard{lock_}; |
| 186 | Log(std::move(elem)); |
| 187 | return len; |
| 188 | } |
| 189 | |
| 190 | auto lock = std::lock_guard{lock_}; |
| 191 | // Initialize last_logged_elements_ to a copy of elem if logging the first element for a log_id. |
| 192 | if (!last_logged_elements_[log_id]) { |
| 193 | last_logged_elements_[log_id].reset(new LogBufferElement(*elem)); |
| 194 | Log(std::move(elem)); |
| 195 | return len; |
| 196 | } |
| 197 | |
| 198 | std::unique_ptr<LogBufferElement> current_last = std::move(last_logged_elements_[log_id]); |
| 199 | enum match_type match = identical(elem.get(), current_last.get()); |
| 200 | |
| 201 | if (match == DIFFERENT) { |
| 202 | if (duplicate_elements_[log_id]) { |
| 203 | auto dropped_element = std::move(duplicate_elements_[log_id]); |
| 204 | // If we previously had 3+ identical messages, log the chatty message. |
| 205 | if (dropped_element && dropped_element->getDropped() > 0) { |
| 206 | Log(std::move(dropped_element)); |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 207 | } |
Tom Cherry | a26f7df | 2020-05-19 17:48:42 -0700 | [diff] [blame^] | 208 | // Log the saved copy of the last identical message seen. |
| 209 | Log(std::move(current_last)); |
| 210 | } |
| 211 | last_logged_elements_[log_id].reset(new LogBufferElement(*elem)); |
| 212 | Log(std::move(elem)); |
| 213 | return len; |
| 214 | } |
| 215 | |
| 216 | // 2 identical message: set duplicate_elements_ appropriately. |
| 217 | if (!duplicate_elements_[log_id]) { |
| 218 | duplicate_elements_[log_id] = std::move(current_last); |
| 219 | last_logged_elements_[log_id] = std::move(elem); |
| 220 | return len; |
| 221 | } |
| 222 | |
| 223 | // 3+ identical LIBLOG event messages: coalesce them into last_logged_elements_. |
| 224 | if (match == SAME_LIBLOG) { |
| 225 | const android_log_event_int_t* current_last_event = |
| 226 | reinterpret_cast<const android_log_event_int_t*>(current_last->getMsg()); |
| 227 | int64_t current_last_count = current_last_event->payload.data; |
| 228 | android_log_event_int_t* elem_event = |
| 229 | reinterpret_cast<android_log_event_int_t*>(const_cast<char*>(elem->getMsg())); |
| 230 | int64_t elem_count = elem_event->payload.data; |
| 231 | |
| 232 | int64_t total = current_last_count + elem_count; |
| 233 | if (total > std::numeric_limits<int32_t>::max()) { |
| 234 | last_logged_elements_[log_id] = std::move(elem); |
| 235 | Log(std::move(current_last)); |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 236 | return len; |
| 237 | } |
Tom Cherry | a26f7df | 2020-05-19 17:48:42 -0700 | [diff] [blame^] | 238 | stats_->AddTotal(current_last->getLogId(), current_last->getMsgLen()); |
| 239 | elem_event->payload.data = total; |
| 240 | last_logged_elements_[log_id] = std::move(elem); |
| 241 | return len; |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 242 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 243 | |
Tom Cherry | a26f7df | 2020-05-19 17:48:42 -0700 | [diff] [blame^] | 244 | // 3+ identical messages (not LIBLOG) messages: increase the drop count. |
| 245 | uint16_t dropped_count = duplicate_elements_[log_id]->getDropped(); |
| 246 | if (dropped_count == std::numeric_limits<uint16_t>::max()) { |
| 247 | Log(std::move(duplicate_elements_[log_id])); |
| 248 | dropped_count = 0; |
| 249 | } |
| 250 | // We're dropping the current_last log so add its stats to the total. |
| 251 | stats_->AddTotal(current_last->getLogId(), current_last->getMsgLen()); |
| 252 | // Use current_last for tracking the dropped count to always use the latest timestamp. |
| 253 | current_last->setDropped(dropped_count + 1); |
| 254 | duplicate_elements_[log_id] = std::move(current_last); |
| 255 | last_logged_elements_[log_id] = std::move(elem); |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 256 | return len; |
| 257 | } |
| 258 | |
Tom Cherry | a26f7df | 2020-05-19 17:48:42 -0700 | [diff] [blame^] | 259 | void ChattyLogBuffer::Log(std::unique_ptr<LogBufferElement> elem) { |
| 260 | log_id_t log_id = elem->getLogId(); |
| 261 | mLogElements.push_back(elem.release()); |
| 262 | stats_->Add(mLogElements.back()); |
| 263 | maybePrune(log_id); |
| 264 | reader_list_->NotifyNewLog(1 << log_id); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 267 | void ChattyLogBuffer::maybePrune(log_id_t id) { |
Tom Cherry | 64e9016 | 2020-05-07 14:44:43 -0700 | [diff] [blame] | 268 | unsigned long prune_rows; |
| 269 | if (stats_->ShouldPrune(id, log_buffer_size(id), &prune_rows)) { |
| 270 | prune(id, prune_rows); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 274 | LogBufferElementCollection::iterator ChattyLogBuffer::erase(LogBufferElementCollection::iterator it, |
| 275 | bool coalesce) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 276 | LogBufferElement* element = *it; |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 277 | log_id_t id = element->getLogId(); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 278 | |
Mark Salyzyn | fa07f9d | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 279 | // Remove iterator references in the various lists that will become stale |
| 280 | // after the element is erased from the main logging list. |
| 281 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 282 | { // start of scope for found iterator |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 283 | int key = (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) ? element->getTag() |
| 284 | : element->getUid(); |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 285 | LogBufferIteratorMap::iterator found = mLastWorst[id].find(key); |
| 286 | if ((found != mLastWorst[id].end()) && (it == found->second)) { |
| 287 | mLastWorst[id].erase(found); |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 288 | } |
Mark Salyzyn | c892ea3 | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 289 | } |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 290 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 291 | { // start of scope for pid found iterator |
Mark Salyzyn | fa07f9d | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 292 | // element->getUid() may not be AID_SYSTEM for next-best-watermark. |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 293 | // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and |
| 294 | // long term code stability, find() check should be fast for those ids. |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 295 | LogBufferPidIteratorMap::iterator found = mLastWorstPidOfSystem[id].find(element->getPid()); |
| 296 | if (found != mLastWorstPidOfSystem[id].end() && it == found->second) { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 297 | mLastWorstPidOfSystem[id].erase(found); |
| 298 | } |
| 299 | } |
| 300 | |
Mark Salyzyn | 7fd6c5c | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 301 | bool setLast[LOG_ID_MAX]; |
| 302 | bool doSetLast = false; |
Tom Cherry | 20118ee | 2020-05-04 10:17:42 -0700 | [diff] [blame] | 303 | log_id_for_each(i) { doSetLast |= setLast[i] = oldest_[i] && it == *oldest_[i]; } |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 304 | #ifdef DEBUG_CHECK_FOR_STALE_ENTRIES |
| 305 | LogBufferElementCollection::iterator bad = it; |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 306 | int key = |
| 307 | (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) ? element->getTag() : element->getUid(); |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 308 | #endif |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 309 | it = mLogElements.erase(it); |
Mark Salyzyn | 7fd6c5c | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 310 | if (doSetLast) { |
| 311 | log_id_for_each(i) { |
| 312 | if (setLast[i]) { |
Tom Cherry | 385c2c9 | 2020-04-29 17:58:18 -0700 | [diff] [blame] | 313 | if (__predict_false(it == mLogElements.end())) { |
Tom Cherry | 20118ee | 2020-05-04 10:17:42 -0700 | [diff] [blame] | 314 | oldest_[i] = std::nullopt; |
Mark Salyzyn | 7fd6c5c | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 315 | } else { |
Tom Cherry | 20118ee | 2020-05-04 10:17:42 -0700 | [diff] [blame] | 316 | oldest_[i] = it; // Store the next iterator even if it does not correspond to |
Tom Cherry | 385c2c9 | 2020-04-29 17:58:18 -0700 | [diff] [blame] | 317 | // the same log_id, as a starting point for GetOldest(). |
Mark Salyzyn | 7fd6c5c | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 318 | } |
| 319 | } |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 320 | } |
| 321 | } |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 322 | #ifdef DEBUG_CHECK_FOR_STALE_ENTRIES |
| 323 | log_id_for_each(i) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 324 | for (auto b : mLastWorst[i]) { |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 325 | if (bad == b.second) { |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 326 | android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i, b.first, key); |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 329 | for (auto b : mLastWorstPidOfSystem[i]) { |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 330 | if (bad == b.second) { |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 331 | android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i, b.first); |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 332 | } |
| 333 | } |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 334 | } |
| 335 | #endif |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 336 | if (coalesce) { |
Tom Cherry | 64e9016 | 2020-05-07 14:44:43 -0700 | [diff] [blame] | 337 | stats_->Erase(element); |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 338 | } else { |
Tom Cherry | 64e9016 | 2020-05-07 14:44:43 -0700 | [diff] [blame] | 339 | stats_->Subtract(element); |
Mark Salyzyn | 831aa29 | 2015-09-03 16:08:50 -0700 | [diff] [blame] | 340 | } |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 341 | delete element; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 342 | |
| 343 | return it; |
| 344 | } |
| 345 | |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 346 | // Define a temporary mechanism to report the last LogBufferElement pointer |
| 347 | // for the specified uid, pid and tid. Used below to help merge-sort when |
| 348 | // pruning for worst UID. |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 349 | class LogBufferElementLast { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 350 | typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap; |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 351 | LogBufferElementMap map; |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 352 | |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 353 | public: |
Chih-Hung Hsieh | 08d470b | 2018-08-13 14:22:56 -0700 | [diff] [blame] | 354 | bool coalesce(LogBufferElement* element, uint16_t dropped) { |
Tom Cherry | a515197 | 2020-05-15 11:39:58 -0700 | [diff] [blame] | 355 | uint64_t key = LogBufferElementKey(element->getUid(), element->getPid(), element->getTid()); |
| 356 | LogBufferElementMap::iterator it = map.find(key); |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 357 | if (it != map.end()) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 358 | LogBufferElement* found = it->second; |
Chih-Hung Hsieh | 08d470b | 2018-08-13 14:22:56 -0700 | [diff] [blame] | 359 | uint16_t moreDropped = found->getDropped(); |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 360 | if ((dropped + moreDropped) > USHRT_MAX) { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 361 | map.erase(it); |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 362 | } else { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 363 | found->setDropped(dropped + moreDropped); |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 364 | return true; |
| 365 | } |
| 366 | } |
| 367 | return false; |
| 368 | } |
| 369 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 370 | void add(LogBufferElement* element) { |
Tom Cherry | a515197 | 2020-05-15 11:39:58 -0700 | [diff] [blame] | 371 | uint64_t key = LogBufferElementKey(element->getUid(), element->getPid(), element->getTid()); |
| 372 | map[key] = element; |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 375 | void clear() { map.clear(); } |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 376 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 377 | void clear(LogBufferElement* element) { |
Tom Cherry | 10d086e | 2019-08-21 14:16:34 -0700 | [diff] [blame] | 378 | uint64_t current = element->getRealTime().nsec() - (EXPIRE_RATELIMIT * NS_PER_SEC); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 379 | for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) { |
| 380 | LogBufferElement* mapElement = it->second; |
Tom Cherry | 10d086e | 2019-08-21 14:16:34 -0700 | [diff] [blame] | 381 | if (mapElement->getDropped() >= EXPIRE_THRESHOLD && |
| 382 | current > mapElement->getRealTime().nsec()) { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 383 | it = map.erase(it); |
| 384 | } else { |
| 385 | ++it; |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | } |
Tom Cherry | a515197 | 2020-05-15 11:39:58 -0700 | [diff] [blame] | 389 | |
| 390 | private: |
| 391 | uint64_t LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid) { |
| 392 | return uint64_t(uid) << 32 | uint64_t(pid) << 16 | uint64_t(tid); |
| 393 | } |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 394 | }; |
| 395 | |
Mark Salyzyn | 0878a7c | 2017-05-11 13:28:33 -0700 | [diff] [blame] | 396 | // If the selected reader is blocking our pruning progress, decide on |
| 397 | // what kind of mitigation is necessary to unblock the situation. |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 398 | void ChattyLogBuffer::kickMe(LogReaderThread* me, log_id_t id, unsigned long pruneRows) { |
Tom Cherry | 64e9016 | 2020-05-07 14:44:43 -0700 | [diff] [blame] | 399 | if (stats_->Sizes(id) > (2 * log_buffer_size(id))) { // +100% |
Mark Salyzyn | 0878a7c | 2017-05-11 13:28:33 -0700 | [diff] [blame] | 400 | // A misbehaving or slow reader has its connection |
| 401 | // dropped if we hit too much memory pressure. |
Tom Cherry | 283c9a1 | 2020-05-14 19:25:05 -0700 | [diff] [blame] | 402 | android::prdebug("Kicking blocked reader, %s, from ChattyLogBuffer::kickMe()\n", |
| 403 | me->name().c_str()); |
Mark Salyzyn | 0878a7c | 2017-05-11 13:28:33 -0700 | [diff] [blame] | 404 | me->release_Locked(); |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 405 | } else if (me->deadline().time_since_epoch().count() != 0) { |
| 406 | // Allow a blocked WRAP deadline reader to trigger and start reporting the log data. |
Mark Salyzyn | 0878a7c | 2017-05-11 13:28:33 -0700 | [diff] [blame] | 407 | me->triggerReader_Locked(); |
| 408 | } else { |
| 409 | // tell slow reader to skip entries to catch up |
Tom Cherry | 21f16a0 | 2019-11-15 17:37:03 -0800 | [diff] [blame] | 410 | android::prdebug( |
Tom Cherry | 283c9a1 | 2020-05-14 19:25:05 -0700 | [diff] [blame] | 411 | "Skipping %lu entries from slow reader, %s, from ChattyLogBuffer::kickMe()\n", |
| 412 | pruneRows, me->name().c_str()); |
Mark Salyzyn | 0878a7c | 2017-05-11 13:28:33 -0700 | [diff] [blame] | 413 | me->triggerSkip_Locked(id, pruneRows); |
| 414 | } |
| 415 | } |
| 416 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 417 | // prune "pruneRows" of type "id" from the buffer. |
| 418 | // |
Mark Salyzyn | 5bb2972 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 419 | // This garbage collection task is used to expire log entries. It is called to |
| 420 | // remove all logs (clear), all UID logs (unprivileged clear), or every |
| 421 | // 256 or 10% of the total logs (whichever is less) to prune the logs. |
| 422 | // |
| 423 | // First there is a prep phase where we discover the reader region lock that |
| 424 | // acts as a backstop to any pruning activity to stop there and go no further. |
| 425 | // |
| 426 | // There are three major pruning loops that follow. All expire from the oldest |
| 427 | // entries. Since there are multiple log buffers, the Android logging facility |
| 428 | // will appear to drop entries 'in the middle' when looking at multiple log |
| 429 | // sources and buffers. This effect is slightly more prominent when we prune |
| 430 | // the worst offender by logging source. Thus the logs slowly loose content |
| 431 | // and value as you move back in time. This is preferred since chatty sources |
| 432 | // invariably move the logs value down faster as less chatty sources would be |
| 433 | // expired in the noise. |
| 434 | // |
| 435 | // The first loop performs blacklisting and worst offender pruning. Falling |
| 436 | // through when there are no notable worst offenders and have not hit the |
| 437 | // region lock preventing further worst offender pruning. This loop also looks |
| 438 | // after managing the chatty log entries and merging to help provide |
| 439 | // statistical basis for blame. The chatty entries are not a notification of |
| 440 | // how much logs you may have, but instead represent how much logs you would |
| 441 | // have had in a virtual log buffer that is extended to cover all the in-memory |
| 442 | // logs without loss. They last much longer than the represented pruned logs |
| 443 | // since they get multiplied by the gains in the non-chatty log sources. |
| 444 | // |
| 445 | // The second loop get complicated because an algorithm of watermarks and |
| 446 | // history is maintained to reduce the order and keep processing time |
| 447 | // down to a minimum at scale. These algorithms can be costly in the face |
| 448 | // of larger log buffers, or severly limited processing time granted to a |
| 449 | // background task at lowest priority. |
| 450 | // |
| 451 | // This second loop does straight-up expiration from the end of the logs |
| 452 | // (again, remember for the specified log buffer id) but does some whitelist |
| 453 | // preservation. Thus whitelist is a Hail Mary low priority, blacklists and |
| 454 | // spam filtration all take priority. This second loop also checks if a region |
| 455 | // lock is causing us to buffer too much in the logs to help the reader(s), |
| 456 | // and will tell the slowest reader thread to skip log entries, and if |
| 457 | // persistent and hits a further threshold, kill the reader thread. |
| 458 | // |
| 459 | // The third thread is optional, and only gets hit if there was a whitelist |
| 460 | // and more needs to be pruned against the backstop of the region lock. |
| 461 | // |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 462 | bool ChattyLogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) { |
Tom Cherry | 6ec71e9 | 2020-05-04 12:53:36 -0700 | [diff] [blame] | 463 | LogReaderThread* oldest = nullptr; |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 464 | bool busy = false; |
Mark Salyzyn | 2b25c66 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 465 | bool clearAll = pruneRows == ULONG_MAX; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 466 | |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 467 | auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()}; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 468 | |
| 469 | // Region locked? |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 470 | for (const auto& reader_thread : reader_list_->reader_threads()) { |
| 471 | if (!reader_thread->IsWatching(id)) { |
| 472 | continue; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 473 | } |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 474 | if (!oldest || oldest->start() > reader_thread->start() || |
| 475 | (oldest->start() == reader_thread->start() && |
| 476 | reader_thread->deadline().time_since_epoch().count() != 0)) { |
| 477 | oldest = reader_thread.get(); |
| 478 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 479 | } |
| 480 | |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 481 | LogBufferElementCollection::iterator it; |
| 482 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 483 | if (__predict_false(caller_uid != AID_ROOT)) { // unlikely |
Mark Salyzyn | 43a5f31 | 2016-09-01 15:48:36 -0700 | [diff] [blame] | 484 | // Only here if clear all request from non system source, so chatty |
| 485 | // filter logistics is not required. |
Tom Cherry | 385c2c9 | 2020-04-29 17:58:18 -0700 | [diff] [blame] | 486 | it = GetOldest(id); |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 487 | while (it != mLogElements.end()) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 488 | LogBufferElement* element = *it; |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 489 | |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 490 | if (element->getLogId() != id || element->getUid() != caller_uid) { |
Mark Salyzyn | 2b25c66 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 491 | ++it; |
| 492 | continue; |
| 493 | } |
| 494 | |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 495 | if (oldest && oldest->start() <= element->getSequence()) { |
Tom Cherry | 5e26655 | 2020-04-08 10:47:26 -0700 | [diff] [blame] | 496 | busy = true; |
| 497 | kickMe(oldest, id, pruneRows); |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 498 | break; |
| 499 | } |
| 500 | |
Mark Salyzyn | 2b25c66 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 501 | it = erase(it); |
Mark Salyzyn | 43a5f31 | 2016-09-01 15:48:36 -0700 | [diff] [blame] | 502 | if (--pruneRows == 0) { |
| 503 | break; |
| 504 | } |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 505 | } |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 506 | return busy; |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 509 | // prune by worst offenders; by blacklist, UID, and by PID of system UID |
Tom Cherry | 5a3db39 | 2020-05-01 17:03:20 -0700 | [diff] [blame] | 510 | bool hasBlacklist = (id != LOG_ID_SECURITY) && prune_->naughty(); |
Mark Salyzyn | 2b25c66 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 511 | while (!clearAll && (pruneRows > 0)) { |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 512 | // recalculate the worst offender on every batched pass |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 513 | int worst = -1; // not valid for getUid() or getKey() |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 514 | size_t worst_sizes = 0; |
| 515 | size_t second_worst_sizes = 0; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 516 | pid_t worstPid = 0; // POSIX guarantees PID != 0 |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 517 | |
Tom Cherry | 5a3db39 | 2020-05-01 17:03:20 -0700 | [diff] [blame] | 518 | if (worstUidEnabledForLogid(id) && prune_->worstUidEnabled()) { |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 519 | // Calculate threshold as 12.5% of available storage |
| 520 | size_t threshold = log_buffer_size(id) / 8; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 521 | |
Tom Cherry | b6b78e9 | 2020-05-07 09:13:12 -0700 | [diff] [blame] | 522 | if (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) { |
Tom Cherry | 64e9016 | 2020-05-07 14:44:43 -0700 | [diff] [blame] | 523 | stats_->WorstTwoTags(threshold, &worst, &worst_sizes, &second_worst_sizes); |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 524 | // per-pid filter for AID_SYSTEM sources is too complex |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 525 | } else { |
Tom Cherry | 64e9016 | 2020-05-07 14:44:43 -0700 | [diff] [blame] | 526 | stats_->WorstTwoUids(id, threshold, &worst, &worst_sizes, &second_worst_sizes); |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 527 | |
Tom Cherry | b6b78e9 | 2020-05-07 09:13:12 -0700 | [diff] [blame] | 528 | if (worst == AID_SYSTEM && prune_->worstPidOfSystemEnabled()) { |
Tom Cherry | 64e9016 | 2020-05-07 14:44:43 -0700 | [diff] [blame] | 529 | stats_->WorstTwoSystemPids(id, worst_sizes, &worstPid, &second_worst_sizes); |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 530 | } |
| 531 | } |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 532 | } |
| 533 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 534 | // skip if we have neither worst nor naughty filters |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 535 | if ((worst == -1) && !hasBlacklist) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 536 | break; |
| 537 | } |
| 538 | |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 539 | bool kick = false; |
Tom Cherry | 385c2c9 | 2020-04-29 17:58:18 -0700 | [diff] [blame] | 540 | bool leading = true; // true if starting from the oldest log entry, false if starting from |
| 541 | // a specific chatty entry. |
Mark Salyzyn | 5bb2972 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 542 | // Perform at least one mandatory garbage collection cycle in following |
| 543 | // - clear leading chatty tags |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 544 | // - coalesce chatty tags |
Mark Salyzyn | 5bb2972 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 545 | // - check age-out of preserved logs |
| 546 | bool gc = pruneRows <= 1; |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 547 | if (!gc && (worst != -1)) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 548 | { // begin scope for worst found iterator |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 549 | LogBufferIteratorMap::iterator found = mLastWorst[id].find(worst); |
| 550 | if (found != mLastWorst[id].end() && found->second != mLogElements.end()) { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 551 | leading = false; |
| 552 | it = found->second; |
| 553 | } |
| 554 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 555 | if (worstPid) { // begin scope for pid worst found iterator |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 556 | // FYI: worstPid only set if !LOG_ID_EVENTS and |
| 557 | // !LOG_ID_SECURITY, not going to make that assumption ... |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 558 | LogBufferPidIteratorMap::iterator found = mLastWorstPidOfSystem[id].find(worstPid); |
| 559 | if (found != mLastWorstPidOfSystem[id].end() && |
| 560 | found->second != mLogElements.end()) { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 561 | leading = false; |
| 562 | it = found->second; |
| 563 | } |
Mark Salyzyn | c892ea3 | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 564 | } |
| 565 | } |
Tom Cherry | 385c2c9 | 2020-04-29 17:58:18 -0700 | [diff] [blame] | 566 | if (leading) { |
| 567 | it = GetOldest(id); |
| 568 | } |
Tom Cherry | bd80e56 | 2020-05-18 08:58:50 -0700 | [diff] [blame] | 569 | static const log_time too_old{EXPIRE_HOUR_THRESHOLD * 60 * 60, 0}; |
Mark Salyzyn | ccfe844 | 2015-08-24 13:43:27 -0700 | [diff] [blame] | 570 | LogBufferElementCollection::iterator lastt; |
| 571 | lastt = mLogElements.end(); |
| 572 | --lastt; |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 573 | LogBufferElementLast last; |
Mark Salyzyn | c892ea3 | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 574 | while (it != mLogElements.end()) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 575 | LogBufferElement* element = *it; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 576 | |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 577 | if (oldest && oldest->start() <= element->getSequence()) { |
Tom Cherry | 5e26655 | 2020-04-08 10:47:26 -0700 | [diff] [blame] | 578 | busy = true; |
Mark Salyzyn | 0878a7c | 2017-05-11 13:28:33 -0700 | [diff] [blame] | 579 | // Do not let chatty eliding trigger any reader mitigation |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 580 | break; |
| 581 | } |
| 582 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 583 | if (element->getLogId() != id) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 584 | ++it; |
| 585 | continue; |
| 586 | } |
Mark Salyzyn | fa07f9d | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 587 | // below this point element->getLogId() == id |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 588 | |
Chih-Hung Hsieh | 08d470b | 2018-08-13 14:22:56 -0700 | [diff] [blame] | 589 | uint16_t dropped = element->getDropped(); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 590 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 591 | // remove any leading drops |
| 592 | if (leading && dropped) { |
| 593 | it = erase(it); |
| 594 | continue; |
| 595 | } |
| 596 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 597 | if (dropped && last.coalesce(element, dropped)) { |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 598 | it = erase(it, true); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 599 | continue; |
| 600 | } |
| 601 | |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 602 | int key = (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) ? element->getTag() |
| 603 | : element->getUid(); |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 604 | |
Tom Cherry | 5a3db39 | 2020-05-01 17:03:20 -0700 | [diff] [blame] | 605 | if (hasBlacklist && prune_->naughty(element)) { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 606 | last.clear(element); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 607 | it = erase(it); |
| 608 | if (dropped) { |
| 609 | continue; |
| 610 | } |
| 611 | |
| 612 | pruneRows--; |
| 613 | if (pruneRows == 0) { |
| 614 | break; |
| 615 | } |
| 616 | |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 617 | if (key == worst) { |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 618 | kick = true; |
| 619 | if (worst_sizes < second_worst_sizes) { |
| 620 | break; |
| 621 | } |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 622 | worst_sizes -= element->getMsgLen(); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 623 | } |
| 624 | continue; |
| 625 | } |
| 626 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 627 | if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) || |
| 628 | (element->getRealTime() > (*lastt)->getRealTime())) { |
Mark Salyzyn | ccfe844 | 2015-08-24 13:43:27 -0700 | [diff] [blame] | 629 | break; |
| 630 | } |
| 631 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 632 | if (dropped) { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 633 | last.add(element); |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 634 | if (worstPid && ((!gc && element->getPid() == worstPid) || |
| 635 | mLastWorstPidOfSystem[id].find(element->getPid()) == |
| 636 | mLastWorstPidOfSystem[id].end())) { |
Mark Salyzyn | fa07f9d | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 637 | // element->getUid() may not be AID_SYSTEM, next best |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 638 | // watermark if current one empty. id is not LOG_ID_EVENTS |
| 639 | // or LOG_ID_SECURITY because of worstPid check. |
Mark Salyzyn | 1eefca2 | 2016-09-01 07:28:44 -0700 | [diff] [blame] | 640 | mLastWorstPidOfSystem[id][element->getPid()] = it; |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 641 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 642 | if ((!gc && !worstPid && (key == worst)) || |
| 643 | (mLastWorst[id].find(key) == mLastWorst[id].end())) { |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 644 | mLastWorst[id][key] = it; |
Mark Salyzyn | 49afe0d | 2015-08-24 13:43:27 -0700 | [diff] [blame] | 645 | } |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 646 | ++it; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 647 | continue; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 648 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 649 | |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 650 | if (key != worst || (worstPid && element->getPid() != worstPid)) { |
Mark Salyzyn | 5921276 | 2015-06-01 09:41:19 -0700 | [diff] [blame] | 651 | leading = false; |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 652 | last.clear(element); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 653 | ++it; |
| 654 | continue; |
| 655 | } |
Mark Salyzyn | fa07f9d | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 656 | // key == worst below here |
| 657 | // If worstPid set, then element->getPid() == worstPid below here |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 658 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 659 | pruneRows--; |
| 660 | if (pruneRows == 0) { |
| 661 | break; |
| 662 | } |
| 663 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 664 | kick = true; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 665 | |
Chih-Hung Hsieh | 08d470b | 2018-08-13 14:22:56 -0700 | [diff] [blame] | 666 | uint16_t len = element->getMsgLen(); |
Mark Salyzyn | 5392aac | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 667 | |
| 668 | // do not create any leading drops |
| 669 | if (leading) { |
| 670 | it = erase(it); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 671 | } else { |
Tom Cherry | 64e9016 | 2020-05-07 14:44:43 -0700 | [diff] [blame] | 672 | stats_->Drop(element); |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 673 | element->setDropped(1); |
| 674 | if (last.coalesce(element, 1)) { |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 675 | it = erase(it, true); |
Mark Salyzyn | 5392aac | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 676 | } else { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 677 | last.add(element); |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 678 | if (worstPid && (!gc || mLastWorstPidOfSystem[id].find(worstPid) == |
| 679 | mLastWorstPidOfSystem[id].end())) { |
Mark Salyzyn | fa07f9d | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 680 | // element->getUid() may not be AID_SYSTEM, next best |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 681 | // watermark if current one empty. id is not |
| 682 | // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid. |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 683 | mLastWorstPidOfSystem[id][worstPid] = it; |
| 684 | } |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 685 | if ((!gc && !worstPid) || mLastWorst[id].find(worst) == mLastWorst[id].end()) { |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 686 | mLastWorst[id][worst] = it; |
Mark Salyzyn | 5bb2972 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 687 | } |
Mark Salyzyn | 5392aac | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 688 | ++it; |
| 689 | } |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 690 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 691 | if (worst_sizes < second_worst_sizes) { |
| 692 | break; |
| 693 | } |
| 694 | worst_sizes -= len; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 695 | } |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 696 | last.clear(); |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 697 | |
Tom Cherry | 5a3db39 | 2020-05-01 17:03:20 -0700 | [diff] [blame] | 698 | if (!kick || !prune_->worstUidEnabled()) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 699 | break; // the following loop will ask bad clients to skip/drop |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 703 | bool whitelist = false; |
Tom Cherry | 5a3db39 | 2020-05-01 17:03:20 -0700 | [diff] [blame] | 704 | bool hasWhitelist = (id != LOG_ID_SECURITY) && prune_->nice() && !clearAll; |
Tom Cherry | 385c2c9 | 2020-04-29 17:58:18 -0700 | [diff] [blame] | 705 | it = GetOldest(id); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 706 | while ((pruneRows > 0) && (it != mLogElements.end())) { |
| 707 | LogBufferElement* element = *it; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 708 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 709 | if (element->getLogId() != id) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 710 | it++; |
| 711 | continue; |
| 712 | } |
| 713 | |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 714 | if (oldest && oldest->start() <= element->getSequence()) { |
Tom Cherry | 5e26655 | 2020-04-08 10:47:26 -0700 | [diff] [blame] | 715 | busy = true; |
| 716 | if (!whitelist) kickMe(oldest, id, pruneRows); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 717 | break; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 718 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 719 | |
Tom Cherry | 5a3db39 | 2020-05-01 17:03:20 -0700 | [diff] [blame] | 720 | if (hasWhitelist && !element->getDropped() && prune_->nice(element)) { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 721 | // WhiteListed |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 722 | whitelist = true; |
| 723 | it++; |
| 724 | continue; |
| 725 | } |
| 726 | |
| 727 | it = erase(it); |
| 728 | pruneRows--; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 729 | } |
| 730 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 731 | // Do not save the whitelist if we are reader range limited |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 732 | if (whitelist && (pruneRows > 0)) { |
Tom Cherry | 385c2c9 | 2020-04-29 17:58:18 -0700 | [diff] [blame] | 733 | it = GetOldest(id); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 734 | while ((it != mLogElements.end()) && (pruneRows > 0)) { |
| 735 | LogBufferElement* element = *it; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 736 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 737 | if (element->getLogId() != id) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 738 | ++it; |
| 739 | continue; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 740 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 741 | |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 742 | if (oldest && oldest->start() <= element->getSequence()) { |
Tom Cherry | 5e26655 | 2020-04-08 10:47:26 -0700 | [diff] [blame] | 743 | busy = true; |
| 744 | kickMe(oldest, id, pruneRows); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 745 | break; |
| 746 | } |
| 747 | |
| 748 | it = erase(it); |
| 749 | pruneRows--; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 750 | } |
| 751 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 752 | |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 753 | return (pruneRows > 0) && busy; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | // clear all rows of type "id" from the buffer. |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 757 | bool ChattyLogBuffer::Clear(log_id_t id, uid_t uid) { |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 758 | bool busy = true; |
| 759 | // If it takes more than 4 tries (seconds) to clear, then kill reader(s) |
| 760 | for (int retry = 4;;) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 761 | if (retry == 1) { // last pass |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 762 | // Check if it is still busy after the sleep, we say prune |
| 763 | // one entry, not another clear run, so we are looking for |
| 764 | // the quick side effect of the return value to tell us if |
| 765 | // we have a _blocked_ reader. |
Tom Cherry | 0b01ff0 | 2020-05-21 10:37:22 -0700 | [diff] [blame] | 766 | { |
| 767 | auto lock = std::lock_guard{lock_}; |
| 768 | busy = prune(id, 1, uid); |
| 769 | } |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 770 | // It is still busy, blocked reader(s), lets kill them all! |
| 771 | // otherwise, lets be a good citizen and preserve the slow |
| 772 | // readers and let the clear run (below) deal with determining |
| 773 | // if we are still blocked and return an error code to caller. |
| 774 | if (busy) { |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 775 | auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()}; |
| 776 | for (const auto& reader_thread : reader_list_->reader_threads()) { |
| 777 | if (reader_thread->IsWatching(id)) { |
Tom Cherry | 21f16a0 | 2019-11-15 17:37:03 -0800 | [diff] [blame] | 778 | android::prdebug( |
Tom Cherry | 283c9a1 | 2020-05-14 19:25:05 -0700 | [diff] [blame] | 779 | "Kicking blocked reader, %s, from ChattyLogBuffer::clear()\n", |
| 780 | reader_thread->name().c_str()); |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 781 | reader_thread->release_Locked(); |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 782 | } |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 783 | } |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 784 | } |
| 785 | } |
Tom Cherry | 0b01ff0 | 2020-05-21 10:37:22 -0700 | [diff] [blame] | 786 | { |
| 787 | auto lock = std::lock_guard{lock_}; |
| 788 | busy = prune(id, ULONG_MAX, uid); |
| 789 | } |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 790 | if (!busy || !--retry) { |
| 791 | break; |
| 792 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 793 | sleep(1); // Let reader(s) catch up after notification |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 794 | } |
| 795 | return busy; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 796 | } |
| 797 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 798 | // set the total space allocated to "id" |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 799 | int ChattyLogBuffer::SetSize(log_id_t id, unsigned long size) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 800 | // Reasonable limits ... |
Mark Salyzyn | f10e273 | 2016-09-27 13:08:23 -0700 | [diff] [blame] | 801 | if (!__android_logger_valid_buffer_size(size)) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 802 | return -1; |
| 803 | } |
Tom Cherry | 0b01ff0 | 2020-05-21 10:37:22 -0700 | [diff] [blame] | 804 | auto lock = std::lock_guard{lock_}; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 805 | log_buffer_size(id) = size; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 806 | return 0; |
| 807 | } |
| 808 | |
| 809 | // get the total space allocated to "id" |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 810 | unsigned long ChattyLogBuffer::GetSize(log_id_t id) { |
Tom Cherry | 0b01ff0 | 2020-05-21 10:37:22 -0700 | [diff] [blame] | 811 | auto shared_lock = SharedLock{lock_}; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 812 | size_t retval = log_buffer_size(id); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 813 | return retval; |
| 814 | } |
| 815 | |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 816 | uint64_t ChattyLogBuffer::FlushTo( |
Tom Cherry | 283c9a1 | 2020-05-14 19:25:05 -0700 | [diff] [blame] | 817 | LogWriter* writer, uint64_t start, pid_t* lastTid, |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 818 | const std::function<FlushToResult(const LogBufferElement* element)>& filter) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 819 | LogBufferElementCollection::iterator it; |
Tom Cherry | 283c9a1 | 2020-05-14 19:25:05 -0700 | [diff] [blame] | 820 | uid_t uid = writer->uid(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 821 | |
Tom Cherry | 0b01ff0 | 2020-05-21 10:37:22 -0700 | [diff] [blame] | 822 | auto shared_lock = SharedLock{lock_}; |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 823 | |
Tom Cherry | 10d086e | 2019-08-21 14:16:34 -0700 | [diff] [blame] | 824 | if (start <= 1) { |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 825 | // client wants to start from the beginning |
| 826 | it = mLogElements.begin(); |
| 827 | } else { |
| 828 | // Client wants to start from some specified time. Chances are |
| 829 | // we are better off starting from the end of the time sorted list. |
Tom Cherry | 10d086e | 2019-08-21 14:16:34 -0700 | [diff] [blame] | 830 | for (it = mLogElements.end(); it != mLogElements.begin(); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 831 | /* do nothing */) { |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 832 | --it; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 833 | LogBufferElement* element = *it; |
Tom Cherry | 10d086e | 2019-08-21 14:16:34 -0700 | [diff] [blame] | 834 | if (element->getSequence() <= start) { |
| 835 | it++; |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 836 | break; |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | |
Tom Cherry | 10d086e | 2019-08-21 14:16:34 -0700 | [diff] [blame] | 841 | uint64_t curr = start; |
Mark Salyzyn | b5b8796 | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 842 | |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 843 | for (; it != mLogElements.end(); ++it) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 844 | LogBufferElement* element = *it; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 845 | |
Tom Cherry | 283c9a1 | 2020-05-14 19:25:05 -0700 | [diff] [blame] | 846 | if (!writer->privileged() && element->getUid() != uid) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 847 | continue; |
| 848 | } |
| 849 | |
Tom Cherry | 283c9a1 | 2020-05-14 19:25:05 -0700 | [diff] [blame] | 850 | if (!writer->can_read_security_logs() && element->getLogId() == LOG_ID_SECURITY) { |
Mark Salyzyn | 8fa8896 | 2016-01-26 14:32:35 -0800 | [diff] [blame] | 851 | continue; |
| 852 | } |
| 853 | |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 854 | // NB: calling out to another object with wrlock() held (safe) |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 855 | if (filter) { |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 856 | FlushToResult ret = filter(element); |
| 857 | if (ret == FlushToResult::kSkip) { |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 858 | continue; |
| 859 | } |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 860 | if (ret == FlushToResult::kStop) { |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 861 | break; |
| 862 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 863 | } |
| 864 | |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 865 | bool sameTid = false; |
| 866 | if (lastTid) { |
| 867 | sameTid = lastTid[element->getLogId()] == element->getTid(); |
| 868 | // Dropped (chatty) immediately following a valid log from the |
| 869 | // same source in the same log buffer indicates we have a |
| 870 | // multiple identical squash. chatty that differs source |
| 871 | // is due to spam filter. chatty to chatty of different |
| 872 | // source is also due to spam filter. |
| 873 | lastTid[element->getLogId()] = |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 874 | (element->getDropped() && !sameTid) ? 0 : element->getTid(); |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 875 | } |
Mark Salyzyn | b5b8796 | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 876 | |
Tom Cherry | 0b01ff0 | 2020-05-21 10:37:22 -0700 | [diff] [blame] | 877 | shared_lock.unlock(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 878 | |
Tom Cherry | 283c9a1 | 2020-05-14 19:25:05 -0700 | [diff] [blame] | 879 | curr = element->getSequence(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 880 | // range locking in LastLogTimes looks after us |
Tom Cherry | 283c9a1 | 2020-05-14 19:25:05 -0700 | [diff] [blame] | 881 | if (!element->FlushTo(writer, stats_, sameTid)) { |
| 882 | return FLUSH_ERROR; |
Mark Salyzyn | eb45db2 | 2017-05-17 19:55:12 +0000 | [diff] [blame] | 883 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 884 | |
Tom Cherry | 0b01ff0 | 2020-05-21 10:37:22 -0700 | [diff] [blame] | 885 | shared_lock.lock_shared(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 886 | } |
Mark Salyzyn | 206ed8e | 2017-05-18 10:06:00 -0700 | [diff] [blame] | 887 | return curr; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 888 | } |