blob: c2e89fcf2cdba06a88af2d04b10c750bbbf3fc95 [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 Cherry9787f9a2020-05-19 19:01:16 -070051static enum match_type Identical(const LogBufferElement& elem, const LogBufferElement& last) {
52 ssize_t lenl = elem.msg_len();
Mark Salyzyn0484b3b2016-08-11 08:02:06 -070053 if (lenl <= 0) return DIFFERENT; // value if this represents a chatty elem
Tom Cherry9787f9a2020-05-19 19:01:16 -070054 ssize_t lenr = last.msg_len();
Mark Salyzyn0484b3b2016-08-11 08:02:06 -070055 if (lenr <= 0) return DIFFERENT; // value if this represents a chatty elem
Tom Cherry9787f9a2020-05-19 19:01:16 -070056 if (elem.uid() != last.uid()) return DIFFERENT;
57 if (elem.pid() != last.pid()) return DIFFERENT;
58 if (elem.tid() != last.tid()) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -080059
60 // last is more than a minute old, stop squashing identical messages
Tom Cherry9787f9a2020-05-19 19:01:16 -070061 if (elem.realtime().nsec() > (last.realtime().nsec() + 60 * NS_PER_SEC)) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -080062
63 // Identical message
Tom Cherry9787f9a2020-05-19 19:01:16 -070064 const char* msgl = elem.msg();
65 const char* msgr = last.msg();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -080066 if (lenl == lenr) {
67 if (!fastcmp<memcmp>(msgl, msgr, lenl)) return SAME;
68 // liblog tagged messages (content gets summed)
Tom Cherry9787f9a2020-05-19 19:01:16 -070069 if (elem.log_id() == LOG_ID_EVENTS && lenl == sizeof(android_log_event_int_t) &&
Tom Cherryd5b38382020-05-12 13:16:41 -070070 !fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_int_t) - sizeof(int32_t)) &&
Tom Cherry9787f9a2020-05-19 19:01:16 -070071 elem.GetTag() == LIBLOG_LOG_TAG) {
Mark Salyzyn501c3732017-03-10 14:31:54 -080072 return SAME_LIBLOG;
Mark Salyzyn1598fe02017-03-13 15:41:59 -070073 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -080074 }
Mark Salyzyna2c02222016-12-13 10:31:29 -080075
76 // audit message (except sequence number) identical?
Tom Cherry9787f9a2020-05-19 19:01:16 -070077 if (last.IsBinary() && lenl > static_cast<ssize_t>(sizeof(android_log_event_string_t)) &&
Tom Cherryd5b38382020-05-12 13:16:41 -070078 lenr > static_cast<ssize_t>(sizeof(android_log_event_string_t))) {
79 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) - sizeof(int32_t))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -080080 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -070081 }
Mark Salyzyna2c02222016-12-13 10:31:29 -080082 msgl += sizeof(android_log_event_string_t);
83 lenl -= sizeof(android_log_event_string_t);
84 msgr += sizeof(android_log_event_string_t);
85 lenr -= sizeof(android_log_event_string_t);
86 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -070087 static const char avc[] = "): avc: ";
Mark Salyzyn501c3732017-03-10 14:31:54 -080088 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -080089 if (!avcl) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -080090 lenl -= avcl - msgl;
Mark Salyzyn501c3732017-03-10 14:31:54 -080091 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -080092 if (!avcr) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -080093 lenr -= avcr - msgr;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -080094 if (lenl != lenr) return DIFFERENT;
Tom Cherryd5b38382020-05-12 13:16:41 -070095 if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc), lenl - strlen(avc))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -080096 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -070097 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -080098 return SAME;
Mark Salyzyna2c02222016-12-13 10:31:29 -080099}
100
Tom Cherry8f613462020-05-12 12:46:43 -0700101void ChattyLogBuffer::LogInternal(LogBufferElement&& elem) {
Tom Cherrya26f7df2020-05-19 17:48:42 -0700102 // b/137093665: don't coalesce security messages.
Tom Cherry9787f9a2020-05-19 19:01:16 -0700103 if (elem.log_id() == LOG_ID_SECURITY) {
Tom Cherry8f613462020-05-12 12:46:43 -0700104 SimpleLogBuffer::LogInternal(std::move(elem));
105 return;
Tom Cherrya26f7df2020-05-19 17:48:42 -0700106 }
Tom Cherry9787f9a2020-05-19 19:01:16 -0700107 int log_id = elem.log_id();
Tom Cherrya26f7df2020-05-19 17:48:42 -0700108
Tom Cherrya26f7df2020-05-19 17:48:42 -0700109 // Initialize last_logged_elements_ to a copy of elem if logging the first element for a log_id.
110 if (!last_logged_elements_[log_id]) {
Tom Cherry13224722020-05-19 18:02:00 -0700111 last_logged_elements_[log_id].emplace(elem);
Tom Cherry8f613462020-05-12 12:46:43 -0700112 SimpleLogBuffer::LogInternal(std::move(elem));
113 return;
Tom Cherrya26f7df2020-05-19 17:48:42 -0700114 }
115
Tom Cherry13224722020-05-19 18:02:00 -0700116 LogBufferElement& current_last = *last_logged_elements_[log_id];
Tom Cherry9787f9a2020-05-19 19:01:16 -0700117 enum match_type match = Identical(elem, current_last);
Tom Cherrya26f7df2020-05-19 17:48:42 -0700118
119 if (match == DIFFERENT) {
120 if (duplicate_elements_[log_id]) {
Tom Cherrya26f7df2020-05-19 17:48:42 -0700121 // If we previously had 3+ identical messages, log the chatty message.
Tom Cherry9787f9a2020-05-19 19:01:16 -0700122 if (duplicate_elements_[log_id]->dropped_count() > 0) {
Tom Cherry8f613462020-05-12 12:46:43 -0700123 SimpleLogBuffer::LogInternal(std::move(*duplicate_elements_[log_id]));
Mark Salyzyna2c02222016-12-13 10:31:29 -0800124 }
Tom Cherry13224722020-05-19 18:02:00 -0700125 duplicate_elements_[log_id].reset();
Tom Cherrya26f7df2020-05-19 17:48:42 -0700126 // Log the saved copy of the last identical message seen.
Tom Cherry8f613462020-05-12 12:46:43 -0700127 SimpleLogBuffer::LogInternal(std::move(current_last));
Tom Cherrya26f7df2020-05-19 17:48:42 -0700128 }
Tom Cherry13224722020-05-19 18:02:00 -0700129 last_logged_elements_[log_id].emplace(elem);
Tom Cherry8f613462020-05-12 12:46:43 -0700130 SimpleLogBuffer::LogInternal(std::move(elem));
131 return;
Tom Cherrya26f7df2020-05-19 17:48:42 -0700132 }
133
134 // 2 identical message: set duplicate_elements_ appropriately.
135 if (!duplicate_elements_[log_id]) {
Tom Cherry13224722020-05-19 18:02:00 -0700136 duplicate_elements_[log_id].emplace(std::move(current_last));
137 last_logged_elements_[log_id].emplace(std::move(elem));
Tom Cherry8f613462020-05-12 12:46:43 -0700138 return;
Tom Cherrya26f7df2020-05-19 17:48:42 -0700139 }
140
141 // 3+ identical LIBLOG event messages: coalesce them into last_logged_elements_.
142 if (match == SAME_LIBLOG) {
143 const android_log_event_int_t* current_last_event =
Tom Cherry9787f9a2020-05-19 19:01:16 -0700144 reinterpret_cast<const android_log_event_int_t*>(current_last.msg());
Tom Cherrya26f7df2020-05-19 17:48:42 -0700145 int64_t current_last_count = current_last_event->payload.data;
146 android_log_event_int_t* elem_event =
Tom Cherry9787f9a2020-05-19 19:01:16 -0700147 reinterpret_cast<android_log_event_int_t*>(const_cast<char*>(elem.msg()));
Tom Cherrya26f7df2020-05-19 17:48:42 -0700148 int64_t elem_count = elem_event->payload.data;
149
150 int64_t total = current_last_count + elem_count;
151 if (total > std::numeric_limits<int32_t>::max()) {
Tom Cherry8f613462020-05-12 12:46:43 -0700152 SimpleLogBuffer::LogInternal(std::move(current_last));
Tom Cherry13224722020-05-19 18:02:00 -0700153 last_logged_elements_[log_id].emplace(std::move(elem));
Tom Cherry8f613462020-05-12 12:46:43 -0700154 return;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800155 }
Tom Cherry9787f9a2020-05-19 19:01:16 -0700156 stats()->AddTotal(current_last.log_id(), current_last.msg_len());
Tom Cherrya26f7df2020-05-19 17:48:42 -0700157 elem_event->payload.data = total;
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 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800161
Tom Cherrya26f7df2020-05-19 17:48:42 -0700162 // 3+ identical messages (not LIBLOG) messages: increase the drop count.
Tom Cherry9787f9a2020-05-19 19:01:16 -0700163 uint16_t dropped_count = duplicate_elements_[log_id]->dropped_count();
Tom Cherrya26f7df2020-05-19 17:48:42 -0700164 if (dropped_count == std::numeric_limits<uint16_t>::max()) {
Tom Cherry8f613462020-05-12 12:46:43 -0700165 SimpleLogBuffer::LogInternal(std::move(*duplicate_elements_[log_id]));
Tom Cherrya26f7df2020-05-19 17:48:42 -0700166 dropped_count = 0;
167 }
168 // We're dropping the current_last log so add its stats to the total.
Tom Cherry9787f9a2020-05-19 19:01:16 -0700169 stats()->AddTotal(current_last.log_id(), current_last.msg_len());
Tom Cherrya26f7df2020-05-19 17:48:42 -0700170 // Use current_last for tracking the dropped count to always use the latest timestamp.
Tom Cherry9787f9a2020-05-19 19:01:16 -0700171 current_last.SetDropped(dropped_count + 1);
Tom Cherry13224722020-05-19 18:02:00 -0700172 duplicate_elements_[log_id].emplace(std::move(current_last));
173 last_logged_elements_[log_id].emplace(std::move(elem));
Mark Salyzyna2c02222016-12-13 10:31:29 -0800174}
175
Tom Cherry8f613462020-05-12 12:46:43 -0700176LogBufferElementCollection::iterator ChattyLogBuffer::Erase(LogBufferElementCollection::iterator it,
Tom Cherryd5b38382020-05-12 13:16:41 -0700177 bool coalesce) {
Tom Cherry13224722020-05-19 18:02:00 -0700178 LogBufferElement& element = *it;
Tom Cherry9787f9a2020-05-19 19:01:16 -0700179 log_id_t id = element.log_id();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700180
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700181 // Remove iterator references in the various lists that will become stale
182 // after the element is erased from the main logging list.
183
Mark Salyzyn501c3732017-03-10 14:31:54 -0800184 { // start of scope for found iterator
Tom Cherry9787f9a2020-05-19 19:01:16 -0700185 int key = (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) ? element.GetTag() : element.uid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700186 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
187 if ((found != mLastWorst[id].end()) && (it == found->second)) {
188 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700189 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700190 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700191
Mark Salyzyn501c3732017-03-10 14:31:54 -0800192 { // start of scope for pid found iterator
Tom Cherry9787f9a2020-05-19 19:01:16 -0700193 // element->uid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700194 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
195 // long term code stability, find() check should be fast for those ids.
Tom Cherry9787f9a2020-05-19 19:01:16 -0700196 LogBufferPidIteratorMap::iterator found = mLastWorstPidOfSystem[id].find(element.pid());
Tom Cherryd5b38382020-05-12 13:16:41 -0700197 if (found != mLastWorstPidOfSystem[id].end() && it == found->second) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700198 mLastWorstPidOfSystem[id].erase(found);
199 }
200 }
201
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700202#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
203 LogBufferElementCollection::iterator bad = it;
Tom Cherry9787f9a2020-05-19 19:01:16 -0700204 int key = (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) ? element->GetTag() : element->uid();
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700205#endif
Tom Cherry13224722020-05-19 18:02:00 -0700206
207 if (coalesce) {
Tom Cherry9787f9a2020-05-19 19:01:16 -0700208 stats()->Erase(element);
Tom Cherry13224722020-05-19 18:02:00 -0700209 } else {
Tom Cherry9787f9a2020-05-19 19:01:16 -0700210 stats()->Subtract(element);
Tom Cherry13224722020-05-19 18:02:00 -0700211 }
212
Tom Cherry8f613462020-05-12 12:46:43 -0700213 it = SimpleLogBuffer::Erase(it);
Tom Cherry13224722020-05-19 18:02:00 -0700214
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700215#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
216 log_id_for_each(i) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800217 for (auto b : mLastWorst[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700218 if (bad == b.second) {
Tom Cherryd5b38382020-05-12 13:16:41 -0700219 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i, b.first, key);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700220 }
221 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800222 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700223 if (bad == b.second) {
Tom Cherryd5b38382020-05-12 13:16:41 -0700224 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i, b.first);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700225 }
226 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700227 }
228#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700229 return it;
230}
231
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700232// Define a temporary mechanism to report the last LogBufferElement pointer
233// for the specified uid, pid and tid. Used below to help merge-sort when
234// pruning for worst UID.
Mark Salyzyn511338d2015-05-19 09:12:30 -0700235class LogBufferElementLast {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800236 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn511338d2015-05-19 09:12:30 -0700237 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700238
Tom Cherryd5b38382020-05-12 13:16:41 -0700239 public:
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700240 bool coalesce(LogBufferElement* element, uint16_t dropped) {
Tom Cherry9787f9a2020-05-19 19:01:16 -0700241 uint64_t key = LogBufferElementKey(element->uid(), element->pid(), element->tid());
Tom Cherrya5151972020-05-15 11:39:58 -0700242 LogBufferElementMap::iterator it = map.find(key);
Mark Salyzyn511338d2015-05-19 09:12:30 -0700243 if (it != map.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800244 LogBufferElement* found = it->second;
Tom Cherry9787f9a2020-05-19 19:01:16 -0700245 uint16_t moreDropped = found->dropped_count();
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700246 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700247 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700248 } else {
Tom Cherry9787f9a2020-05-19 19:01:16 -0700249 found->SetDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700250 return true;
251 }
252 }
253 return false;
254 }
255
Mark Salyzyn501c3732017-03-10 14:31:54 -0800256 void add(LogBufferElement* element) {
Tom Cherry9787f9a2020-05-19 19:01:16 -0700257 uint64_t key = LogBufferElementKey(element->uid(), element->pid(), element->tid());
Tom Cherrya5151972020-05-15 11:39:58 -0700258 map[key] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700259 }
260
Tom Cherryd5b38382020-05-12 13:16:41 -0700261 void clear() { map.clear(); }
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700262
Mark Salyzyn501c3732017-03-10 14:31:54 -0800263 void clear(LogBufferElement* element) {
Tom Cherry9787f9a2020-05-19 19:01:16 -0700264 uint64_t current = element->realtime().nsec() - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800265 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
266 LogBufferElement* mapElement = it->second;
Tom Cherry9787f9a2020-05-19 19:01:16 -0700267 if (mapElement->dropped_count() >= EXPIRE_THRESHOLD &&
268 current > mapElement->realtime().nsec()) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700269 it = map.erase(it);
270 } else {
271 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700272 }
273 }
274 }
Tom Cherrya5151972020-05-15 11:39:58 -0700275
276 private:
277 uint64_t LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid) {
278 return uint64_t(uid) << 32 | uint64_t(pid) << 16 | uint64_t(tid);
279 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700280};
281
Mark Salyzyn0175b072014-02-26 09:50:16 -0800282// prune "pruneRows" of type "id" from the buffer.
283//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700284// This garbage collection task is used to expire log entries. It is called to
285// remove all logs (clear), all UID logs (unprivileged clear), or every
286// 256 or 10% of the total logs (whichever is less) to prune the logs.
287//
288// First there is a prep phase where we discover the reader region lock that
289// acts as a backstop to any pruning activity to stop there and go no further.
290//
291// There are three major pruning loops that follow. All expire from the oldest
292// entries. Since there are multiple log buffers, the Android logging facility
293// will appear to drop entries 'in the middle' when looking at multiple log
294// sources and buffers. This effect is slightly more prominent when we prune
295// the worst offender by logging source. Thus the logs slowly loose content
296// and value as you move back in time. This is preferred since chatty sources
297// invariably move the logs value down faster as less chatty sources would be
298// expired in the noise.
299//
300// The first loop performs blacklisting and worst offender pruning. Falling
301// through when there are no notable worst offenders and have not hit the
302// region lock preventing further worst offender pruning. This loop also looks
303// after managing the chatty log entries and merging to help provide
304// statistical basis for blame. The chatty entries are not a notification of
305// how much logs you may have, but instead represent how much logs you would
306// have had in a virtual log buffer that is extended to cover all the in-memory
307// logs without loss. They last much longer than the represented pruned logs
308// since they get multiplied by the gains in the non-chatty log sources.
309//
310// The second loop get complicated because an algorithm of watermarks and
311// history is maintained to reduce the order and keep processing time
312// down to a minimum at scale. These algorithms can be costly in the face
313// of larger log buffers, or severly limited processing time granted to a
314// background task at lowest priority.
315//
316// This second loop does straight-up expiration from the end of the logs
317// (again, remember for the specified log buffer id) but does some whitelist
318// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
319// spam filtration all take priority. This second loop also checks if a region
320// lock is causing us to buffer too much in the logs to help the reader(s),
321// and will tell the slowest reader thread to skip log entries, and if
322// persistent and hits a further threshold, kill the reader thread.
323//
324// The third thread is optional, and only gets hit if there was a whitelist
325// and more needs to be pruned against the backstop of the region lock.
326//
Tom Cherry8f613462020-05-12 12:46:43 -0700327bool ChattyLogBuffer::Prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Tom Cherry6ec71e92020-05-04 12:53:36 -0700328 LogReaderThread* oldest = nullptr;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700329 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700330 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800331
Tom Cherry8f613462020-05-12 12:46:43 -0700332 auto reader_threads_lock = std::lock_guard{reader_list()->reader_threads_lock()};
Mark Salyzyn0175b072014-02-26 09:50:16 -0800333
334 // Region locked?
Tom Cherry8f613462020-05-12 12:46:43 -0700335 for (const auto& reader_thread : reader_list()->reader_threads()) {
Tom Cherry68630a02020-05-11 16:29:29 -0700336 if (!reader_thread->IsWatching(id)) {
337 continue;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800338 }
Tom Cherry68630a02020-05-11 16:29:29 -0700339 if (!oldest || oldest->start() > reader_thread->start() ||
340 (oldest->start() == reader_thread->start() &&
341 reader_thread->deadline().time_since_epoch().count() != 0)) {
342 oldest = reader_thread.get();
343 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800344 }
345
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800346 LogBufferElementCollection::iterator it;
347
Mark Salyzyn501c3732017-03-10 14:31:54 -0800348 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700349 // Only here if clear all request from non system source, so chatty
350 // filter logistics is not required.
Tom Cherry385c2c92020-04-29 17:58:18 -0700351 it = GetOldest(id);
Tom Cherry8f613462020-05-12 12:46:43 -0700352 while (it != logs().end()) {
Tom Cherry13224722020-05-19 18:02:00 -0700353 LogBufferElement& element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700354
Tom Cherry9787f9a2020-05-19 19:01:16 -0700355 if (element.log_id() != id || element.uid() != caller_uid) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700356 ++it;
357 continue;
358 }
359
Tom Cherry9787f9a2020-05-19 19:01:16 -0700360 if (oldest && oldest->start() <= element.sequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700361 busy = true;
Tom Cherry8f613462020-05-12 12:46:43 -0700362 KickReader(oldest, id, pruneRows);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700363 break;
364 }
365
Tom Cherry8f613462020-05-12 12:46:43 -0700366 it = Erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700367 if (--pruneRows == 0) {
368 break;
369 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700370 }
Mark Salyzync5dc9702015-09-16 15:34:00 -0700371 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700372 }
373
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700374 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Tom Cherry5a3db392020-05-01 17:03:20 -0700375 bool hasBlacklist = (id != LOG_ID_SECURITY) && prune_->naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700376 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800377 // recalculate the worst offender on every batched pass
Tom Cherry9787f9a2020-05-19 19:01:16 -0700378 int worst = -1; // not valid for uid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800379 size_t worst_sizes = 0;
380 size_t second_worst_sizes = 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800381 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800382
Tom Cherry5a3db392020-05-01 17:03:20 -0700383 if (worstUidEnabledForLogid(id) && prune_->worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700384 // Calculate threshold as 12.5% of available storage
Tom Cherry8f613462020-05-12 12:46:43 -0700385 size_t threshold = max_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700386
Tom Cherryb6b78e92020-05-07 09:13:12 -0700387 if (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) {
Tom Cherry8f613462020-05-12 12:46:43 -0700388 stats()->WorstTwoTags(threshold, &worst, &worst_sizes, &second_worst_sizes);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700389 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700390 } else {
Tom Cherry8f613462020-05-12 12:46:43 -0700391 stats()->WorstTwoUids(id, threshold, &worst, &worst_sizes, &second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700392
Tom Cherryb6b78e92020-05-07 09:13:12 -0700393 if (worst == AID_SYSTEM && prune_->worstPidOfSystemEnabled()) {
Tom Cherry8f613462020-05-12 12:46:43 -0700394 stats()->WorstTwoSystemPids(id, worst_sizes, &worstPid, &second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700395 }
396 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800397 }
398
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700399 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700400 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700401 break;
402 }
403
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800404 bool kick = false;
Tom Cherry385c2c92020-04-29 17:58:18 -0700405 bool leading = true; // true if starting from the oldest log entry, false if starting from
406 // a specific chatty entry.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700407 // Perform at least one mandatory garbage collection cycle in following
408 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700409 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700410 // - check age-out of preserved logs
411 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700412 if (!gc && (worst != -1)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800413 { // begin scope for worst found iterator
Tom Cherryd5b38382020-05-12 13:16:41 -0700414 LogBufferIteratorMap::iterator found = mLastWorst[id].find(worst);
Tom Cherry8f613462020-05-12 12:46:43 -0700415 if (found != mLastWorst[id].end() && found->second != logs().end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700416 leading = false;
417 it = found->second;
418 }
419 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800420 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700421 // FYI: worstPid only set if !LOG_ID_EVENTS and
422 // !LOG_ID_SECURITY, not going to make that assumption ...
Tom Cherryd5b38382020-05-12 13:16:41 -0700423 LogBufferPidIteratorMap::iterator found = mLastWorstPidOfSystem[id].find(worstPid);
Tom Cherry8f613462020-05-12 12:46:43 -0700424 if (found != mLastWorstPidOfSystem[id].end() && found->second != logs().end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700425 leading = false;
426 it = found->second;
427 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700428 }
429 }
Tom Cherry385c2c92020-04-29 17:58:18 -0700430 if (leading) {
431 it = GetOldest(id);
432 }
Tom Cherrybd80e562020-05-18 08:58:50 -0700433 static const log_time too_old{EXPIRE_HOUR_THRESHOLD * 60 * 60, 0};
Mark Salyzynccfe8442015-08-24 13:43:27 -0700434 LogBufferElementCollection::iterator lastt;
Tom Cherry8f613462020-05-12 12:46:43 -0700435 lastt = logs().end();
Mark Salyzynccfe8442015-08-24 13:43:27 -0700436 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700437 LogBufferElementLast last;
Tom Cherry8f613462020-05-12 12:46:43 -0700438 while (it != logs().end()) {
Tom Cherry13224722020-05-19 18:02:00 -0700439 LogBufferElement& element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800440
Tom Cherry9787f9a2020-05-19 19:01:16 -0700441 if (oldest && oldest->start() <= element.sequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700442 busy = true;
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700443 // Do not let chatty eliding trigger any reader mitigation
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800444 break;
445 }
446
Tom Cherry9787f9a2020-05-19 19:01:16 -0700447 if (element.log_id() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800448 ++it;
449 continue;
450 }
Tom Cherry9787f9a2020-05-19 19:01:16 -0700451 // below this point element->log_id() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800452
Tom Cherry9787f9a2020-05-19 19:01:16 -0700453 uint16_t dropped = element.dropped_count();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800454
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700455 // remove any leading drops
456 if (leading && dropped) {
Tom Cherry8f613462020-05-12 12:46:43 -0700457 it = Erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700458 continue;
459 }
460
Tom Cherry13224722020-05-19 18:02:00 -0700461 if (dropped && last.coalesce(&element, dropped)) {
Tom Cherry8f613462020-05-12 12:46:43 -0700462 it = Erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700463 continue;
464 }
465
Tom Cherry9787f9a2020-05-19 19:01:16 -0700466 int key = (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) ? element.GetTag()
467 : element.uid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700468
Tom Cherry13224722020-05-19 18:02:00 -0700469 if (hasBlacklist && prune_->naughty(&element)) {
470 last.clear(&element);
Tom Cherry8f613462020-05-12 12:46:43 -0700471 it = Erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700472 if (dropped) {
473 continue;
474 }
475
476 pruneRows--;
477 if (pruneRows == 0) {
478 break;
479 }
480
Mark Salyzyn6a066942016-07-14 15:34:30 -0700481 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700482 kick = true;
483 if (worst_sizes < second_worst_sizes) {
484 break;
485 }
Tom Cherry9787f9a2020-05-19 19:01:16 -0700486 worst_sizes -= element.msg_len();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700487 }
488 continue;
489 }
490
Tom Cherry9787f9a2020-05-19 19:01:16 -0700491 if (element.realtime() < (lastt->realtime() - too_old) ||
492 element.realtime() > lastt->realtime()) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700493 break;
494 }
495
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700496 if (dropped) {
Tom Cherry13224722020-05-19 18:02:00 -0700497 last.add(&element);
Tom Cherry9787f9a2020-05-19 19:01:16 -0700498 if (worstPid && ((!gc && element.pid() == worstPid) ||
499 mLastWorstPidOfSystem[id].find(element.pid()) ==
Tom Cherryd5b38382020-05-12 13:16:41 -0700500 mLastWorstPidOfSystem[id].end())) {
Tom Cherry9787f9a2020-05-19 19:01:16 -0700501 // element->uid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700502 // watermark if current one empty. id is not LOG_ID_EVENTS
503 // or LOG_ID_SECURITY because of worstPid check.
Tom Cherry9787f9a2020-05-19 19:01:16 -0700504 mLastWorstPidOfSystem[id][element.pid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700505 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800506 if ((!gc && !worstPid && (key == worst)) ||
507 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700508 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700509 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800510 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700511 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800512 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700513
Tom Cherry9787f9a2020-05-19 19:01:16 -0700514 if (key != worst || (worstPid && element.pid() != worstPid)) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700515 leading = false;
Tom Cherry13224722020-05-19 18:02:00 -0700516 last.clear(&element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700517 ++it;
518 continue;
519 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700520 // key == worst below here
Tom Cherry9787f9a2020-05-19 19:01:16 -0700521 // If worstPid set, then element->pid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700522
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700523 pruneRows--;
524 if (pruneRows == 0) {
525 break;
526 }
527
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700528 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700529
Tom Cherry9787f9a2020-05-19 19:01:16 -0700530 uint16_t len = element.msg_len();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700531
532 // do not create any leading drops
533 if (leading) {
Tom Cherry8f613462020-05-12 12:46:43 -0700534 it = Erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700535 } else {
Tom Cherry9787f9a2020-05-19 19:01:16 -0700536 stats()->Drop(element);
537 element.SetDropped(1);
Tom Cherry13224722020-05-19 18:02:00 -0700538 if (last.coalesce(&element, 1)) {
Tom Cherry8f613462020-05-12 12:46:43 -0700539 it = Erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700540 } else {
Tom Cherry13224722020-05-19 18:02:00 -0700541 last.add(&element);
Tom Cherryd5b38382020-05-12 13:16:41 -0700542 if (worstPid && (!gc || mLastWorstPidOfSystem[id].find(worstPid) ==
543 mLastWorstPidOfSystem[id].end())) {
Tom Cherry9787f9a2020-05-19 19:01:16 -0700544 // element->uid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700545 // watermark if current one empty. id is not
546 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700547 mLastWorstPidOfSystem[id][worstPid] = it;
548 }
Tom Cherryd5b38382020-05-12 13:16:41 -0700549 if ((!gc && !worstPid) || mLastWorst[id].find(worst) == mLastWorst[id].end()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700550 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700551 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700552 ++it;
553 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700554 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700555 if (worst_sizes < second_worst_sizes) {
556 break;
557 }
558 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800559 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700560 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800561
Tom Cherry5a3db392020-05-01 17:03:20 -0700562 if (!kick || !prune_->worstUidEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800563 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800564 }
565 }
566
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800567 bool whitelist = false;
Tom Cherry5a3db392020-05-01 17:03:20 -0700568 bool hasWhitelist = (id != LOG_ID_SECURITY) && prune_->nice() && !clearAll;
Tom Cherry385c2c92020-04-29 17:58:18 -0700569 it = GetOldest(id);
Tom Cherry8f613462020-05-12 12:46:43 -0700570 while ((pruneRows > 0) && (it != logs().end())) {
Tom Cherry13224722020-05-19 18:02:00 -0700571 LogBufferElement& element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700572
Tom Cherry9787f9a2020-05-19 19:01:16 -0700573 if (element.log_id() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700574 it++;
575 continue;
576 }
577
Tom Cherry9787f9a2020-05-19 19:01:16 -0700578 if (oldest && oldest->start() <= element.sequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700579 busy = true;
Tom Cherry8f613462020-05-12 12:46:43 -0700580 if (!whitelist) KickReader(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700581 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800582 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700583
Tom Cherry9787f9a2020-05-19 19:01:16 -0700584 if (hasWhitelist && !element.dropped_count() && prune_->nice(&element)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700585 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700586 whitelist = true;
587 it++;
588 continue;
589 }
590
Tom Cherry8f613462020-05-12 12:46:43 -0700591 it = Erase(it);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700592 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800593 }
594
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700595 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800596 if (whitelist && (pruneRows > 0)) {
Tom Cherry385c2c92020-04-29 17:58:18 -0700597 it = GetOldest(id);
Tom Cherry8f613462020-05-12 12:46:43 -0700598 while ((it != logs().end()) && (pruneRows > 0)) {
Tom Cherry13224722020-05-19 18:02:00 -0700599 LogBufferElement& element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700600
Tom Cherry9787f9a2020-05-19 19:01:16 -0700601 if (element.log_id() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700602 ++it;
603 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800604 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700605
Tom Cherry9787f9a2020-05-19 19:01:16 -0700606 if (oldest && oldest->start() <= element.sequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700607 busy = true;
Tom Cherry8f613462020-05-12 12:46:43 -0700608 KickReader(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700609 break;
610 }
611
Tom Cherry8f613462020-05-12 12:46:43 -0700612 it = Erase(it);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700613 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800614 }
615 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800616
Mark Salyzync5dc9702015-09-16 15:34:00 -0700617 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800618}