blob: 0ba6025964cb23ee357181abf07a8a0e3f1fbaeb [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
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 Cherryd5b38382020-05-12 13:16:41 -070016// for manual checking of stale entries during ChattyLogBuffer::erase()
Mark Salyzyn60636fa2016-10-24 16:22:17 -070017//#define DEBUG_CHECK_FOR_STALE_ENTRIES
Mark Salyzyn0175b072014-02-26 09:50:16 -080018
Tom Cherryd5b38382020-05-12 13:16:41 -070019#include "ChattyLogBuffer.h"
20
Mark Salyzyn671e3432014-05-06 07:34:59 -070021#include <ctype.h>
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -080022#include <endian.h>
Mark Salyzyn202e1532015-02-09 08:21:05 -080023#include <errno.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080024#include <stdio.h>
25#include <string.h>
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070026#include <sys/cdefs.h>
Mark Salyzyn57a0af92014-05-09 17:44:18 -070027#include <sys/user.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080028#include <time.h>
29#include <unistd.h>
30
Tom Cherryb398a7c2020-05-20 12:09:22 -070031#include <limits>
Mark Salyzyn511338d2015-05-19 09:12:30 -070032#include <unordered_map>
Tom Cherry10d086e2019-08-21 14:16:34 -070033#include <utility>
Mark Salyzyn511338d2015-05-19 09:12:30 -070034
Mark Salyzynf10e2732016-09-27 13:08:23 -070035#include <private/android_logger.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080036
Mark Salyzyna2c02222016-12-13 10:31:29 -080037#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080038
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070039#ifndef __predict_false
40#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
41#endif
42
Tom Cherryd5b38382020-05-12 13:16:41 -070043ChattyLogBuffer::ChattyLogBuffer(LogReaderList* reader_list, LogTags* tags, PruneList* prune,
44 LogStatistics* stats)
Tom Cherry8f613462020-05-12 12:46:43 -070045 : SimpleLogBuffer(reader_list, tags, stats), prune_(prune) {}
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070046
Tom Cherrya26f7df2020-05-19 17:48:42 -070047ChattyLogBuffer::~ChattyLogBuffer() {}
Mark Salyzyna2c02222016-12-13 10:31:29 -080048
Mark Salyzyn501c3732017-03-10 14:31:54 -080049enum match_type { DIFFERENT, SAME, SAME_LIBLOG };
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -080050
Tom Cherry8f613462020-05-12 12:46:43 -070051static enum match_type Identical(LogBufferElement* elem, LogBufferElement* last) {
Mark Salyzyna2c02222016-12-13 10:31:29 -080052 // is it mostly identical?
Mark Salyzyn501c3732017-03-10 14:31:54 -080053 // if (!elem) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -070054 ssize_t lenl = elem->getMsgLen();
55 if (lenl <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -080056 // if (!last) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -070057 ssize_t lenr = last->getMsgLen();
58 if (lenr <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -080059 // if (elem->getLogId() != last->getLogId()) return DIFFERENT;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -080060 if (elem->getUid() != last->getUid()) return DIFFERENT;
61 if (elem->getPid() != last->getPid()) return DIFFERENT;
62 if (elem->getTid() != last->getTid()) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -080063
64 // last is more than a minute old, stop squashing identical messages
Tom Cherryd5b38382020-05-12 13:16:41 -070065 if (elem->getRealTime().nsec() > (last->getRealTime().nsec() + 60 * NS_PER_SEC))
Mark Salyzyn501c3732017-03-10 14:31:54 -080066 return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -080067
68 // Identical message
69 const char* msgl = elem->getMsg();
70 const char* msgr = last->getMsg();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -080071 if (lenl == lenr) {
72 if (!fastcmp<memcmp>(msgl, msgr, lenl)) return SAME;
73 // liblog tagged messages (content gets summed)
Tom Cherryd5b38382020-05-12 13:16:41 -070074 if (elem->getLogId() == LOG_ID_EVENTS && lenl == sizeof(android_log_event_int_t) &&
75 !fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_int_t) - sizeof(int32_t)) &&
76 elem->getTag() == LIBLOG_LOG_TAG) {
Mark Salyzyn501c3732017-03-10 14:31:54 -080077 return SAME_LIBLOG;
Mark Salyzyn1598fe02017-03-13 15:41:59 -070078 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -080079 }
Mark Salyzyna2c02222016-12-13 10:31:29 -080080
81 // audit message (except sequence number) identical?
Tom Cherryd5b38382020-05-12 13:16:41 -070082 if (last->isBinary() && lenl > static_cast<ssize_t>(sizeof(android_log_event_string_t)) &&
83 lenr > static_cast<ssize_t>(sizeof(android_log_event_string_t))) {
84 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) - sizeof(int32_t))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -080085 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -070086 }
Mark Salyzyna2c02222016-12-13 10:31:29 -080087 msgl += sizeof(android_log_event_string_t);
88 lenl -= sizeof(android_log_event_string_t);
89 msgr += sizeof(android_log_event_string_t);
90 lenr -= sizeof(android_log_event_string_t);
91 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -070092 static const char avc[] = "): avc: ";
Mark Salyzyn501c3732017-03-10 14:31:54 -080093 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -080094 if (!avcl) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -080095 lenl -= avcl - msgl;
Mark Salyzyn501c3732017-03-10 14:31:54 -080096 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -080097 if (!avcr) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -080098 lenr -= avcr - msgr;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -080099 if (lenl != lenr) return DIFFERENT;
Tom Cherryd5b38382020-05-12 13:16:41 -0700100 if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc), lenl - strlen(avc))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800101 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700102 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800103 return SAME;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800104}
105
Tom Cherry8f613462020-05-12 12:46:43 -0700106void ChattyLogBuffer::LogInternal(LogBufferElement&& elem) {
Tom Cherrya26f7df2020-05-19 17:48:42 -0700107 // b/137093665: don't coalesce security messages.
Tom Cherry8f613462020-05-12 12:46:43 -0700108 if (elem.getLogId() == LOG_ID_SECURITY) {
109 SimpleLogBuffer::LogInternal(std::move(elem));
110 return;
Tom Cherrya26f7df2020-05-19 17:48:42 -0700111 }
Tom Cherry8f613462020-05-12 12:46:43 -0700112 int log_id = elem.getLogId();
Tom Cherrya26f7df2020-05-19 17:48:42 -0700113
Tom Cherrya26f7df2020-05-19 17:48:42 -0700114 // Initialize last_logged_elements_ to a copy of elem if logging the first element for a log_id.
115 if (!last_logged_elements_[log_id]) {
Tom Cherry13224722020-05-19 18:02:00 -0700116 last_logged_elements_[log_id].emplace(elem);
Tom Cherry8f613462020-05-12 12:46:43 -0700117 SimpleLogBuffer::LogInternal(std::move(elem));
118 return;
Tom Cherrya26f7df2020-05-19 17:48:42 -0700119 }
120
Tom Cherry13224722020-05-19 18:02:00 -0700121 LogBufferElement& current_last = *last_logged_elements_[log_id];
Tom Cherry8f613462020-05-12 12:46:43 -0700122 enum match_type match = Identical(&elem, &current_last);
Tom Cherrya26f7df2020-05-19 17:48:42 -0700123
124 if (match == DIFFERENT) {
125 if (duplicate_elements_[log_id]) {
Tom Cherrya26f7df2020-05-19 17:48:42 -0700126 // If we previously had 3+ identical messages, log the chatty message.
Tom Cherry13224722020-05-19 18:02:00 -0700127 if (duplicate_elements_[log_id]->getDropped() > 0) {
Tom Cherry8f613462020-05-12 12:46:43 -0700128 SimpleLogBuffer::LogInternal(std::move(*duplicate_elements_[log_id]));
Mark Salyzyna2c02222016-12-13 10:31:29 -0800129 }
Tom Cherry13224722020-05-19 18:02:00 -0700130 duplicate_elements_[log_id].reset();
Tom Cherrya26f7df2020-05-19 17:48:42 -0700131 // Log the saved copy of the last identical message seen.
Tom Cherry8f613462020-05-12 12:46:43 -0700132 SimpleLogBuffer::LogInternal(std::move(current_last));
Tom Cherrya26f7df2020-05-19 17:48:42 -0700133 }
Tom Cherry13224722020-05-19 18:02:00 -0700134 last_logged_elements_[log_id].emplace(elem);
Tom Cherry8f613462020-05-12 12:46:43 -0700135 SimpleLogBuffer::LogInternal(std::move(elem));
136 return;
Tom Cherrya26f7df2020-05-19 17:48:42 -0700137 }
138
139 // 2 identical message: set duplicate_elements_ appropriately.
140 if (!duplicate_elements_[log_id]) {
Tom Cherry13224722020-05-19 18:02:00 -0700141 duplicate_elements_[log_id].emplace(std::move(current_last));
142 last_logged_elements_[log_id].emplace(std::move(elem));
Tom Cherry8f613462020-05-12 12:46:43 -0700143 return;
Tom Cherrya26f7df2020-05-19 17:48:42 -0700144 }
145
146 // 3+ identical LIBLOG event messages: coalesce them into last_logged_elements_.
147 if (match == SAME_LIBLOG) {
148 const android_log_event_int_t* current_last_event =
Tom Cherry13224722020-05-19 18:02:00 -0700149 reinterpret_cast<const android_log_event_int_t*>(current_last.getMsg());
Tom Cherrya26f7df2020-05-19 17:48:42 -0700150 int64_t current_last_count = current_last_event->payload.data;
151 android_log_event_int_t* elem_event =
Tom Cherry13224722020-05-19 18:02:00 -0700152 reinterpret_cast<android_log_event_int_t*>(const_cast<char*>(elem.getMsg()));
Tom Cherrya26f7df2020-05-19 17:48:42 -0700153 int64_t elem_count = elem_event->payload.data;
154
155 int64_t total = current_last_count + elem_count;
156 if (total > std::numeric_limits<int32_t>::max()) {
Tom Cherry8f613462020-05-12 12:46:43 -0700157 SimpleLogBuffer::LogInternal(std::move(current_last));
Tom Cherry13224722020-05-19 18:02:00 -0700158 last_logged_elements_[log_id].emplace(std::move(elem));
Tom Cherry8f613462020-05-12 12:46:43 -0700159 return;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800160 }
Tom Cherry8f613462020-05-12 12:46:43 -0700161 stats()->AddTotal(current_last.getLogId(), current_last.getMsgLen());
Tom Cherrya26f7df2020-05-19 17:48:42 -0700162 elem_event->payload.data = total;
Tom Cherry13224722020-05-19 18:02:00 -0700163 last_logged_elements_[log_id].emplace(std::move(elem));
Tom Cherry8f613462020-05-12 12:46:43 -0700164 return;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800165 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800166
Tom Cherrya26f7df2020-05-19 17:48:42 -0700167 // 3+ identical messages (not LIBLOG) messages: increase the drop count.
168 uint16_t dropped_count = duplicate_elements_[log_id]->getDropped();
169 if (dropped_count == std::numeric_limits<uint16_t>::max()) {
Tom Cherry8f613462020-05-12 12:46:43 -0700170 SimpleLogBuffer::LogInternal(std::move(*duplicate_elements_[log_id]));
Tom Cherrya26f7df2020-05-19 17:48:42 -0700171 dropped_count = 0;
172 }
173 // We're dropping the current_last log so add its stats to the total.
Tom Cherry8f613462020-05-12 12:46:43 -0700174 stats()->AddTotal(current_last.getLogId(), current_last.getMsgLen());
Tom Cherrya26f7df2020-05-19 17:48:42 -0700175 // Use current_last for tracking the dropped count to always use the latest timestamp.
Tom Cherry13224722020-05-19 18:02:00 -0700176 current_last.setDropped(dropped_count + 1);
177 duplicate_elements_[log_id].emplace(std::move(current_last));
178 last_logged_elements_[log_id].emplace(std::move(elem));
Mark Salyzyna2c02222016-12-13 10:31:29 -0800179}
180
Tom Cherry8f613462020-05-12 12:46:43 -0700181LogBufferElementCollection::iterator ChattyLogBuffer::Erase(LogBufferElementCollection::iterator it,
Tom Cherryd5b38382020-05-12 13:16:41 -0700182 bool coalesce) {
Tom Cherry13224722020-05-19 18:02:00 -0700183 LogBufferElement& element = *it;
184 log_id_t id = element.getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700185
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700186 // Remove iterator references in the various lists that will become stale
187 // after the element is erased from the main logging list.
188
Mark Salyzyn501c3732017-03-10 14:31:54 -0800189 { // start of scope for found iterator
Tom Cherry13224722020-05-19 18:02:00 -0700190 int key = (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) ? element.getTag()
191 : element.getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700192 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
193 if ((found != mLastWorst[id].end()) && (it == found->second)) {
194 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700195 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700196 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700197
Mark Salyzyn501c3732017-03-10 14:31:54 -0800198 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700199 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700200 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
201 // long term code stability, find() check should be fast for those ids.
Tom Cherry13224722020-05-19 18:02:00 -0700202 LogBufferPidIteratorMap::iterator found = mLastWorstPidOfSystem[id].find(element.getPid());
Tom Cherryd5b38382020-05-12 13:16:41 -0700203 if (found != mLastWorstPidOfSystem[id].end() && it == found->second) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700204 mLastWorstPidOfSystem[id].erase(found);
205 }
206 }
207
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700208#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
209 LogBufferElementCollection::iterator bad = it;
Tom Cherryd5b38382020-05-12 13:16:41 -0700210 int key =
211 (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) ? element->getTag() : element->getUid();
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700212#endif
Tom Cherry13224722020-05-19 18:02:00 -0700213
214 if (coalesce) {
Tom Cherry8f613462020-05-12 12:46:43 -0700215 stats()->Erase(&element);
Tom Cherry13224722020-05-19 18:02:00 -0700216 } else {
Tom Cherry8f613462020-05-12 12:46:43 -0700217 stats()->Subtract(&element);
Tom Cherry13224722020-05-19 18:02:00 -0700218 }
219
Tom Cherry8f613462020-05-12 12:46:43 -0700220 it = SimpleLogBuffer::Erase(it);
Tom Cherry13224722020-05-19 18:02:00 -0700221
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700222#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
223 log_id_for_each(i) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800224 for (auto b : mLastWorst[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700225 if (bad == b.second) {
Tom Cherryd5b38382020-05-12 13:16:41 -0700226 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i, b.first, key);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700227 }
228 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800229 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700230 if (bad == b.second) {
Tom Cherryd5b38382020-05-12 13:16:41 -0700231 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i, b.first);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700232 }
233 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700234 }
235#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700236 return it;
237}
238
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700239// Define a temporary mechanism to report the last LogBufferElement pointer
240// for the specified uid, pid and tid. Used below to help merge-sort when
241// pruning for worst UID.
Mark Salyzyn511338d2015-05-19 09:12:30 -0700242class LogBufferElementLast {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800243 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn511338d2015-05-19 09:12:30 -0700244 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700245
Tom Cherryd5b38382020-05-12 13:16:41 -0700246 public:
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700247 bool coalesce(LogBufferElement* element, uint16_t dropped) {
Tom Cherrya5151972020-05-15 11:39:58 -0700248 uint64_t key = LogBufferElementKey(element->getUid(), element->getPid(), element->getTid());
249 LogBufferElementMap::iterator it = map.find(key);
Mark Salyzyn511338d2015-05-19 09:12:30 -0700250 if (it != map.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800251 LogBufferElement* found = it->second;
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700252 uint16_t moreDropped = found->getDropped();
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700253 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700254 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700255 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700256 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700257 return true;
258 }
259 }
260 return false;
261 }
262
Mark Salyzyn501c3732017-03-10 14:31:54 -0800263 void add(LogBufferElement* element) {
Tom Cherrya5151972020-05-15 11:39:58 -0700264 uint64_t key = LogBufferElementKey(element->getUid(), element->getPid(), element->getTid());
265 map[key] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700266 }
267
Tom Cherryd5b38382020-05-12 13:16:41 -0700268 void clear() { map.clear(); }
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700269
Mark Salyzyn501c3732017-03-10 14:31:54 -0800270 void clear(LogBufferElement* element) {
Tom Cherry10d086e2019-08-21 14:16:34 -0700271 uint64_t current = element->getRealTime().nsec() - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800272 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
273 LogBufferElement* mapElement = it->second;
Tom Cherry10d086e2019-08-21 14:16:34 -0700274 if (mapElement->getDropped() >= EXPIRE_THRESHOLD &&
275 current > mapElement->getRealTime().nsec()) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700276 it = map.erase(it);
277 } else {
278 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700279 }
280 }
281 }
Tom Cherrya5151972020-05-15 11:39:58 -0700282
283 private:
284 uint64_t LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid) {
285 return uint64_t(uid) << 32 | uint64_t(pid) << 16 | uint64_t(tid);
286 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700287};
288
Mark Salyzyn0175b072014-02-26 09:50:16 -0800289// prune "pruneRows" of type "id" from the buffer.
290//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700291// This garbage collection task is used to expire log entries. It is called to
292// remove all logs (clear), all UID logs (unprivileged clear), or every
293// 256 or 10% of the total logs (whichever is less) to prune the logs.
294//
295// First there is a prep phase where we discover the reader region lock that
296// acts as a backstop to any pruning activity to stop there and go no further.
297//
298// There are three major pruning loops that follow. All expire from the oldest
299// entries. Since there are multiple log buffers, the Android logging facility
300// will appear to drop entries 'in the middle' when looking at multiple log
301// sources and buffers. This effect is slightly more prominent when we prune
302// the worst offender by logging source. Thus the logs slowly loose content
303// and value as you move back in time. This is preferred since chatty sources
304// invariably move the logs value down faster as less chatty sources would be
305// expired in the noise.
306//
307// The first loop performs blacklisting and worst offender pruning. Falling
308// through when there are no notable worst offenders and have not hit the
309// region lock preventing further worst offender pruning. This loop also looks
310// after managing the chatty log entries and merging to help provide
311// statistical basis for blame. The chatty entries are not a notification of
312// how much logs you may have, but instead represent how much logs you would
313// have had in a virtual log buffer that is extended to cover all the in-memory
314// logs without loss. They last much longer than the represented pruned logs
315// since they get multiplied by the gains in the non-chatty log sources.
316//
317// The second loop get complicated because an algorithm of watermarks and
318// history is maintained to reduce the order and keep processing time
319// down to a minimum at scale. These algorithms can be costly in the face
320// of larger log buffers, or severly limited processing time granted to a
321// background task at lowest priority.
322//
323// This second loop does straight-up expiration from the end of the logs
324// (again, remember for the specified log buffer id) but does some whitelist
325// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
326// spam filtration all take priority. This second loop also checks if a region
327// lock is causing us to buffer too much in the logs to help the reader(s),
328// and will tell the slowest reader thread to skip log entries, and if
329// persistent and hits a further threshold, kill the reader thread.
330//
331// The third thread is optional, and only gets hit if there was a whitelist
332// and more needs to be pruned against the backstop of the region lock.
333//
Tom Cherry8f613462020-05-12 12:46:43 -0700334bool ChattyLogBuffer::Prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Tom Cherry6ec71e92020-05-04 12:53:36 -0700335 LogReaderThread* oldest = nullptr;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700336 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700337 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800338
Tom Cherry8f613462020-05-12 12:46:43 -0700339 auto reader_threads_lock = std::lock_guard{reader_list()->reader_threads_lock()};
Mark Salyzyn0175b072014-02-26 09:50:16 -0800340
341 // Region locked?
Tom Cherry8f613462020-05-12 12:46:43 -0700342 for (const auto& reader_thread : reader_list()->reader_threads()) {
Tom Cherry68630a02020-05-11 16:29:29 -0700343 if (!reader_thread->IsWatching(id)) {
344 continue;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800345 }
Tom Cherry68630a02020-05-11 16:29:29 -0700346 if (!oldest || oldest->start() > reader_thread->start() ||
347 (oldest->start() == reader_thread->start() &&
348 reader_thread->deadline().time_since_epoch().count() != 0)) {
349 oldest = reader_thread.get();
350 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800351 }
352
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800353 LogBufferElementCollection::iterator it;
354
Mark Salyzyn501c3732017-03-10 14:31:54 -0800355 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700356 // Only here if clear all request from non system source, so chatty
357 // filter logistics is not required.
Tom Cherry385c2c92020-04-29 17:58:18 -0700358 it = GetOldest(id);
Tom Cherry8f613462020-05-12 12:46:43 -0700359 while (it != logs().end()) {
Tom Cherry13224722020-05-19 18:02:00 -0700360 LogBufferElement& element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700361
Tom Cherry13224722020-05-19 18:02:00 -0700362 if (element.getLogId() != id || element.getUid() != caller_uid) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700363 ++it;
364 continue;
365 }
366
Tom Cherry13224722020-05-19 18:02:00 -0700367 if (oldest && oldest->start() <= element.getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700368 busy = true;
Tom Cherry8f613462020-05-12 12:46:43 -0700369 KickReader(oldest, id, pruneRows);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700370 break;
371 }
372
Tom Cherry8f613462020-05-12 12:46:43 -0700373 it = Erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700374 if (--pruneRows == 0) {
375 break;
376 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700377 }
Mark Salyzync5dc9702015-09-16 15:34:00 -0700378 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700379 }
380
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700381 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Tom Cherry5a3db392020-05-01 17:03:20 -0700382 bool hasBlacklist = (id != LOG_ID_SECURITY) && prune_->naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700383 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800384 // recalculate the worst offender on every batched pass
Mark Salyzyn501c3732017-03-10 14:31:54 -0800385 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800386 size_t worst_sizes = 0;
387 size_t second_worst_sizes = 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800388 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800389
Tom Cherry5a3db392020-05-01 17:03:20 -0700390 if (worstUidEnabledForLogid(id) && prune_->worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700391 // Calculate threshold as 12.5% of available storage
Tom Cherry8f613462020-05-12 12:46:43 -0700392 size_t threshold = max_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700393
Tom Cherryb6b78e92020-05-07 09:13:12 -0700394 if (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) {
Tom Cherry8f613462020-05-12 12:46:43 -0700395 stats()->WorstTwoTags(threshold, &worst, &worst_sizes, &second_worst_sizes);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700396 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700397 } else {
Tom Cherry8f613462020-05-12 12:46:43 -0700398 stats()->WorstTwoUids(id, threshold, &worst, &worst_sizes, &second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700399
Tom Cherryb6b78e92020-05-07 09:13:12 -0700400 if (worst == AID_SYSTEM && prune_->worstPidOfSystemEnabled()) {
Tom Cherry8f613462020-05-12 12:46:43 -0700401 stats()->WorstTwoSystemPids(id, worst_sizes, &worstPid, &second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700402 }
403 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800404 }
405
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700406 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700407 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700408 break;
409 }
410
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800411 bool kick = false;
Tom Cherry385c2c92020-04-29 17:58:18 -0700412 bool leading = true; // true if starting from the oldest log entry, false if starting from
413 // a specific chatty entry.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700414 // Perform at least one mandatory garbage collection cycle in following
415 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700416 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700417 // - check age-out of preserved logs
418 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700419 if (!gc && (worst != -1)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800420 { // begin scope for worst found iterator
Tom Cherryd5b38382020-05-12 13:16:41 -0700421 LogBufferIteratorMap::iterator found = mLastWorst[id].find(worst);
Tom Cherry8f613462020-05-12 12:46:43 -0700422 if (found != mLastWorst[id].end() && found->second != logs().end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700423 leading = false;
424 it = found->second;
425 }
426 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800427 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700428 // FYI: worstPid only set if !LOG_ID_EVENTS and
429 // !LOG_ID_SECURITY, not going to make that assumption ...
Tom Cherryd5b38382020-05-12 13:16:41 -0700430 LogBufferPidIteratorMap::iterator found = mLastWorstPidOfSystem[id].find(worstPid);
Tom Cherry8f613462020-05-12 12:46:43 -0700431 if (found != mLastWorstPidOfSystem[id].end() && found->second != logs().end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700432 leading = false;
433 it = found->second;
434 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700435 }
436 }
Tom Cherry385c2c92020-04-29 17:58:18 -0700437 if (leading) {
438 it = GetOldest(id);
439 }
Tom Cherrybd80e562020-05-18 08:58:50 -0700440 static const log_time too_old{EXPIRE_HOUR_THRESHOLD * 60 * 60, 0};
Mark Salyzynccfe8442015-08-24 13:43:27 -0700441 LogBufferElementCollection::iterator lastt;
Tom Cherry8f613462020-05-12 12:46:43 -0700442 lastt = logs().end();
Mark Salyzynccfe8442015-08-24 13:43:27 -0700443 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700444 LogBufferElementLast last;
Tom Cherry8f613462020-05-12 12:46:43 -0700445 while (it != logs().end()) {
Tom Cherry13224722020-05-19 18:02:00 -0700446 LogBufferElement& element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800447
Tom Cherry13224722020-05-19 18:02:00 -0700448 if (oldest && oldest->start() <= element.getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700449 busy = true;
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700450 // Do not let chatty eliding trigger any reader mitigation
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800451 break;
452 }
453
Tom Cherry13224722020-05-19 18:02:00 -0700454 if (element.getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800455 ++it;
456 continue;
457 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700458 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800459
Tom Cherry13224722020-05-19 18:02:00 -0700460 uint16_t dropped = element.getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800461
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700462 // remove any leading drops
463 if (leading && dropped) {
Tom Cherry8f613462020-05-12 12:46:43 -0700464 it = Erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700465 continue;
466 }
467
Tom Cherry13224722020-05-19 18:02:00 -0700468 if (dropped && last.coalesce(&element, dropped)) {
Tom Cherry8f613462020-05-12 12:46:43 -0700469 it = Erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700470 continue;
471 }
472
Tom Cherry13224722020-05-19 18:02:00 -0700473 int key = (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) ? element.getTag()
474 : element.getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700475
Tom Cherry13224722020-05-19 18:02:00 -0700476 if (hasBlacklist && prune_->naughty(&element)) {
477 last.clear(&element);
Tom Cherry8f613462020-05-12 12:46:43 -0700478 it = Erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700479 if (dropped) {
480 continue;
481 }
482
483 pruneRows--;
484 if (pruneRows == 0) {
485 break;
486 }
487
Mark Salyzyn6a066942016-07-14 15:34:30 -0700488 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700489 kick = true;
490 if (worst_sizes < second_worst_sizes) {
491 break;
492 }
Tom Cherry13224722020-05-19 18:02:00 -0700493 worst_sizes -= element.getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700494 }
495 continue;
496 }
497
Tom Cherry13224722020-05-19 18:02:00 -0700498 if (element.getRealTime() < (lastt->getRealTime() - too_old) ||
499 element.getRealTime() > lastt->getRealTime()) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700500 break;
501 }
502
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700503 if (dropped) {
Tom Cherry13224722020-05-19 18:02:00 -0700504 last.add(&element);
505 if (worstPid && ((!gc && element.getPid() == worstPid) ||
506 mLastWorstPidOfSystem[id].find(element.getPid()) ==
Tom Cherryd5b38382020-05-12 13:16:41 -0700507 mLastWorstPidOfSystem[id].end())) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700508 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700509 // watermark if current one empty. id is not LOG_ID_EVENTS
510 // or LOG_ID_SECURITY because of worstPid check.
Tom Cherry13224722020-05-19 18:02:00 -0700511 mLastWorstPidOfSystem[id][element.getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700512 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800513 if ((!gc && !worstPid && (key == worst)) ||
514 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700515 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700516 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800517 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700518 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800519 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700520
Tom Cherry13224722020-05-19 18:02:00 -0700521 if (key != worst || (worstPid && element.getPid() != worstPid)) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700522 leading = false;
Tom Cherry13224722020-05-19 18:02:00 -0700523 last.clear(&element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700524 ++it;
525 continue;
526 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700527 // key == worst below here
528 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700529
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700530 pruneRows--;
531 if (pruneRows == 0) {
532 break;
533 }
534
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700535 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700536
Tom Cherry13224722020-05-19 18:02:00 -0700537 uint16_t len = element.getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700538
539 // do not create any leading drops
540 if (leading) {
Tom Cherry8f613462020-05-12 12:46:43 -0700541 it = Erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700542 } else {
Tom Cherry8f613462020-05-12 12:46:43 -0700543 stats()->Drop(&element);
Tom Cherry13224722020-05-19 18:02:00 -0700544 element.setDropped(1);
545 if (last.coalesce(&element, 1)) {
Tom Cherry8f613462020-05-12 12:46:43 -0700546 it = Erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700547 } else {
Tom Cherry13224722020-05-19 18:02:00 -0700548 last.add(&element);
Tom Cherryd5b38382020-05-12 13:16:41 -0700549 if (worstPid && (!gc || mLastWorstPidOfSystem[id].find(worstPid) ==
550 mLastWorstPidOfSystem[id].end())) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700551 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700552 // watermark if current one empty. id is not
553 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700554 mLastWorstPidOfSystem[id][worstPid] = it;
555 }
Tom Cherryd5b38382020-05-12 13:16:41 -0700556 if ((!gc && !worstPid) || mLastWorst[id].find(worst) == mLastWorst[id].end()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700557 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700558 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700559 ++it;
560 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700561 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700562 if (worst_sizes < second_worst_sizes) {
563 break;
564 }
565 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800566 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700567 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800568
Tom Cherry5a3db392020-05-01 17:03:20 -0700569 if (!kick || !prune_->worstUidEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800570 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800571 }
572 }
573
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800574 bool whitelist = false;
Tom Cherry5a3db392020-05-01 17:03:20 -0700575 bool hasWhitelist = (id != LOG_ID_SECURITY) && prune_->nice() && !clearAll;
Tom Cherry385c2c92020-04-29 17:58:18 -0700576 it = GetOldest(id);
Tom Cherry8f613462020-05-12 12:46:43 -0700577 while ((pruneRows > 0) && (it != logs().end())) {
Tom Cherry13224722020-05-19 18:02:00 -0700578 LogBufferElement& element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700579
Tom Cherry13224722020-05-19 18:02:00 -0700580 if (element.getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700581 it++;
582 continue;
583 }
584
Tom Cherry13224722020-05-19 18:02:00 -0700585 if (oldest && oldest->start() <= element.getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700586 busy = true;
Tom Cherry8f613462020-05-12 12:46:43 -0700587 if (!whitelist) KickReader(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700588 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800589 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700590
Tom Cherry13224722020-05-19 18:02:00 -0700591 if (hasWhitelist && !element.getDropped() && prune_->nice(&element)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700592 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700593 whitelist = true;
594 it++;
595 continue;
596 }
597
Tom Cherry8f613462020-05-12 12:46:43 -0700598 it = Erase(it);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700599 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800600 }
601
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700602 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800603 if (whitelist && (pruneRows > 0)) {
Tom Cherry385c2c92020-04-29 17:58:18 -0700604 it = GetOldest(id);
Tom Cherry8f613462020-05-12 12:46:43 -0700605 while ((it != logs().end()) && (pruneRows > 0)) {
Tom Cherry13224722020-05-19 18:02:00 -0700606 LogBufferElement& element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700607
Tom Cherry13224722020-05-19 18:02:00 -0700608 if (element.getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700609 ++it;
610 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800611 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700612
Tom Cherry13224722020-05-19 18:02:00 -0700613 if (oldest && oldest->start() <= element.getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700614 busy = true;
Tom Cherry8f613462020-05-12 12:46:43 -0700615 KickReader(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700616 break;
617 }
618
Tom Cherry8f613462020-05-12 12:46:43 -0700619 it = Erase(it);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700620 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800621 }
622 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800623
Mark Salyzync5dc9702015-09-16 15:34:00 -0700624 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800625}