blob: ded6c8cc76258ea04965b6d6f62cf3e34887b31b [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 */
Mark Salyzyn60636fa2016-10-24 16:22:17 -070016// for manual checking of stale entries during LogBuffer::erase()
17//#define DEBUG_CHECK_FOR_STALE_ENTRIES
Mark Salyzyn0175b072014-02-26 09:50:16 -080018
Mark Salyzyn671e3432014-05-06 07:34:59 -070019#include <ctype.h>
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -080020#include <endian.h>
Mark Salyzyn202e1532015-02-09 08:21:05 -080021#include <errno.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080022#include <stdio.h>
23#include <string.h>
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070024#include <sys/cdefs.h>
Mark Salyzyn57a0af92014-05-09 17:44:18 -070025#include <sys/user.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080026#include <time.h>
27#include <unistd.h>
28
Mark Salyzyn511338d2015-05-19 09:12:30 -070029#include <unordered_map>
30
Mark Salyzyn671e3432014-05-06 07:34:59 -070031#include <cutils/properties.h>
Mark Salyzynf10e2732016-09-27 13:08:23 -070032#include <private/android_logger.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080033
34#include "LogBuffer.h"
Mark Salyzynb6bee332015-09-08 08:56:32 -070035#include "LogKlog.h"
Mark Salyzyn671e3432014-05-06 07:34:59 -070036#include "LogReader.h"
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
Mark Salyzyndfa7a072014-02-11 12:29:31 -080043// Default
Mark Salyzyndfa7a072014-02-11 12:29:31 -080044#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn671e3432014-05-06 07:34:59 -070045
Mark Salyzyn58363792017-04-17 12:46:12 -070046const log_time LogBuffer::pruneMargin(3, 0);
47
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070048void LogBuffer::init() {
Mark Salyzyndfa7a072014-02-11 12:29:31 -080049 log_id_for_each(i) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -080050 mLastSet[i] = false;
51 mLast[i] = mLogElements.begin();
52
Mark Salyzynf10e2732016-09-27 13:08:23 -070053 if (setSize(i, __android_logger_get_buffer_size(i))) {
Mark Salyzyn57a0af92014-05-09 17:44:18 -070054 setSize(i, LOG_BUFFER_MIN_SIZE);
55 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -080056 }
Mark Salyzynb6bee332015-09-08 08:56:32 -070057 bool lastMonotonic = monotonic;
Mark Salyzynba7a9a02015-12-01 15:57:25 -080058 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzynb75cce02015-11-30 11:35:56 -080059 if (lastMonotonic != monotonic) {
60 //
61 // Fixup all timestamps, may not be 100% accurate, but better than
62 // throwing what we have away when we get 'surprised' by a change.
63 // In-place element fixup so no need to check reader-lock. Entries
64 // should already be in timestamp order, but we could end up with a
65 // few out-of-order entries if new monotonics come in before we
66 // are notified of the reinit change in status. A Typical example would
67 // be:
68 // --------- beginning of system
69 // 10.494082 184 201 D Cryptfs : Just triggered post_fs_data
70 // --------- beginning of kernel
71 // 0.000000 0 0 I : Initializing cgroup subsys
72 // as the act of mounting /data would trigger persist.logd.timestamp to
73 // be corrected. 1/30 corner case YMMV.
74 //
Mark Salyzyn3c501b52017-04-18 14:09:45 -070075 rdlock();
Mark Salyzynb75cce02015-11-30 11:35:56 -080076 LogBufferElementCollection::iterator it = mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -080077 while ((it != mLogElements.end())) {
78 LogBufferElement* e = *it;
Mark Salyzynb75cce02015-11-30 11:35:56 -080079 if (monotonic) {
80 if (!android::isMonotonic(e->mRealTime)) {
81 LogKlog::convertRealToMonotonic(e->mRealTime);
82 }
83 } else {
84 if (android::isMonotonic(e->mRealTime)) {
85 LogKlog::convertMonotonicToReal(e->mRealTime);
86 }
87 }
88 ++it;
89 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -070090 unlock();
Mark Salyzynb6bee332015-09-08 08:56:32 -070091 }
92
Mark Salyzynb75cce02015-11-30 11:35:56 -080093 // We may have been triggered by a SIGHUP. Release any sleeping reader
94 // threads to dump their current content.
Mark Salyzynb6bee332015-09-08 08:56:32 -070095 //
Mark Salyzynb75cce02015-11-30 11:35:56 -080096 // NB: this is _not_ performed in the context of a SIGHUP, it is
97 // performed during startup, and in context of reinit administrative thread
Mark Salyzyn3c501b52017-04-18 14:09:45 -070098 LogTimeEntry::wrlock();
Mark Salyzynb75cce02015-11-30 11:35:56 -080099
100 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800101 while (times != mTimes.end()) {
102 LogTimeEntry* entry = (*times);
Mark Salyzynb75cce02015-11-30 11:35:56 -0800103 if (entry->owned_Locked()) {
104 entry->triggerReader_Locked();
Mark Salyzynb6bee332015-09-08 08:56:32 -0700105 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800106 times++;
Mark Salyzynb6bee332015-09-08 08:56:32 -0700107 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800108
109 LogTimeEntry::unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800110}
111
Mark Salyzyn501c3732017-03-10 14:31:54 -0800112LogBuffer::LogBuffer(LastLogTimes* times)
113 : monotonic(android_log_clockid() == CLOCK_MONOTONIC), mTimes(*times) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700114 pthread_rwlock_init(&mLogElementsLock, nullptr);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700115
Mark Salyzyna2c02222016-12-13 10:31:29 -0800116 log_id_for_each(i) {
Mark Salyzynae2abf12017-03-31 10:48:39 -0700117 lastLoggedElements[i] = nullptr;
118 droppedElements[i] = nullptr;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800119 }
120
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700121 init();
122}
123
Mark Salyzyna2c02222016-12-13 10:31:29 -0800124LogBuffer::~LogBuffer() {
125 log_id_for_each(i) {
126 delete lastLoggedElements[i];
127 delete droppedElements[i];
128 }
129}
130
Mark Salyzyn501c3732017-03-10 14:31:54 -0800131enum match_type { DIFFERENT, SAME, SAME_LIBLOG };
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800132
Mark Salyzyn501c3732017-03-10 14:31:54 -0800133static enum match_type identical(LogBufferElement* elem,
134 LogBufferElement* last) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800135 // is it mostly identical?
Mark Salyzyn501c3732017-03-10 14:31:54 -0800136 // if (!elem) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700137 ssize_t lenl = elem->getMsgLen();
138 if (lenl <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800139 // if (!last) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700140 ssize_t lenr = last->getMsgLen();
141 if (lenr <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800142 // if (elem->getLogId() != last->getLogId()) return DIFFERENT;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800143 if (elem->getUid() != last->getUid()) return DIFFERENT;
144 if (elem->getPid() != last->getPid()) return DIFFERENT;
145 if (elem->getTid() != last->getTid()) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800146
147 // last is more than a minute old, stop squashing identical messages
148 if (elem->getRealTime().nsec() >
Mark Salyzyn501c3732017-03-10 14:31:54 -0800149 (last->getRealTime().nsec() + 60 * NS_PER_SEC))
150 return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800151
152 // Identical message
153 const char* msgl = elem->getMsg();
154 const char* msgr = last->getMsg();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800155 if (lenl == lenr) {
156 if (!fastcmp<memcmp>(msgl, msgr, lenl)) return SAME;
157 // liblog tagged messages (content gets summed)
158 if ((elem->getLogId() == LOG_ID_EVENTS) &&
159 (lenl == sizeof(android_log_event_int_t)) &&
Mark Salyzyn501c3732017-03-10 14:31:54 -0800160 !fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_int_t) -
161 sizeof(int32_t)) &&
162 (elem->getTag() == LIBLOG_LOG_TAG))
163 return SAME_LIBLOG;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800164 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800165
166 // audit message (except sequence number) identical?
Mark Salyzyna2c02222016-12-13 10:31:29 -0800167 if (last->isBinary()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800168 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) -
169 sizeof(int32_t)))
170 return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800171 msgl += sizeof(android_log_event_string_t);
172 lenl -= sizeof(android_log_event_string_t);
173 msgr += sizeof(android_log_event_string_t);
174 lenr -= sizeof(android_log_event_string_t);
175 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700176 static const char avc[] = "): avc: ";
Mark Salyzyn501c3732017-03-10 14:31:54 -0800177 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800178 if (!avcl) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800179 lenl -= avcl - msgl;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800180 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800181 if (!avcr) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800182 lenr -= avcr - msgr;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800183 if (lenl != lenr) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700184 if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc),
Evgenii Stepanov03fc2fe2017-03-14 14:47:25 -0700185 lenl - strlen(avc))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800186 return DIFFERENT;
Evgenii Stepanov03fc2fe2017-03-14 14:47:25 -0700187 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800188 return SAME;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800189}
190
Mark Salyzyn501c3732017-03-10 14:31:54 -0800191int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
192 pid_t tid, const char* msg, unsigned short len) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800193 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800194 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800195 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700196
Mark Salyzyn501c3732017-03-10 14:31:54 -0800197 LogBufferElement* elem =
198 new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000199 if (log_id != LOG_ID_SECURITY) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800200 int prio = ANDROID_LOG_INFO;
Mark Salyzynae2abf12017-03-31 10:48:39 -0700201 const char* tag = nullptr;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800202 if (log_id == LOG_ID_EVENTS) {
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700203 tag = tagToName(elem->getTag());
Mark Salyzyn083b0372015-12-04 10:59:45 -0800204 } else {
205 prio = *msg;
206 tag = msg + 1;
207 }
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700208 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800209 // Log traffic received to total
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700210 wrlock();
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700211 stats.addTotal(elem);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700212 unlock();
Mark Salyzyn083b0372015-12-04 10:59:45 -0800213 delete elem;
214 return -EACCES;
215 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700216 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800217
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700218 wrlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800219 LogBufferElement* currentLast = lastLoggedElements[log_id];
220 if (currentLast) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800221 LogBufferElement* dropped = droppedElements[log_id];
Mark Salyzyna2c02222016-12-13 10:31:29 -0800222 unsigned short count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800223 //
224 // State Init
225 // incoming:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700226 // dropped = nullptr
227 // currentLast = nullptr;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800228 // elem = incoming message
229 // outgoing:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700230 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800231 // currentLast = copy of elem
232 // log elem
233 // State 0
234 // incoming:
235 // count = 0
Mark Salyzynae2abf12017-03-31 10:48:39 -0700236 // dropped = nullptr
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800237 // currentLast = copy of last message
238 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800239 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800240 // dropped = copy of first identical message -> State 1
241 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800242 // break: if match == DIFFERENT
Mark Salyzynae2abf12017-03-31 10:48:39 -0700243 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800244 // delete copy of last message (incoming currentLast)
245 // currentLast = copy of elem
246 // log elem
247 // State 1
248 // incoming:
249 // count = 0
250 // dropped = copy of first identical message
251 // currentLast = reference to last held-back incoming
252 // message
253 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800254 // outgoing: if match == SAME
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800255 // delete copy of first identical message (dropped)
256 // dropped = reference to last held-back incoming
257 // message set to chatty count of 1 -> State 2
258 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800259 // outgoing: if match == SAME_LIBLOG
260 // dropped = copy of first identical message -> State 1
261 // take sum of currentLast and elem
262 // if sum overflows:
263 // log currentLast
264 // currentLast = reference to elem
265 // else
266 // delete currentLast
267 // currentLast = reference to elem, sum liblog.
268 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800269 // delete dropped
Mark Salyzynae2abf12017-03-31 10:48:39 -0700270 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800271 // log reference to last held-back (currentLast)
272 // currentLast = copy of elem
273 // log elem
274 // State 2
275 // incoming:
276 // count = chatty count
277 // dropped = chatty message holding count
278 // currentLast = reference to last held-back incoming
279 // message.
280 // dropped = chatty message holding count
281 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800282 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800283 // delete chatty message holding count
284 // dropped = reference to last held-back incoming
285 // message, set to chatty count + 1
286 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800287 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800288 // log dropped (chatty message)
Mark Salyzynae2abf12017-03-31 10:48:39 -0700289 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800290 // log reference to last held-back (currentLast)
291 // currentLast = copy of elem
292 // log elem
293 //
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800294 enum match_type match = identical(elem, currentLast);
295 if (match != DIFFERENT) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800296 if (dropped) {
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800297 // Sum up liblog tag messages?
298 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
299 android_log_event_int_t* event =
300 reinterpret_cast<android_log_event_int_t*>(
301 const_cast<char*>(currentLast->getMsg()));
302 //
303 // To unit test, differentiate with something like:
304 // event->header.tag = htole32(CHATTY_LOG_TAG);
305 // here, then instead of delete currentLast below,
306 // log(currentLast) to see the incremental sums form.
307 //
308 uint32_t swab = event->payload.data;
309 unsigned long long total = htole32(swab);
310 event = reinterpret_cast<android_log_event_int_t*>(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800311 const_cast<char*>(elem->getMsg()));
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800312 swab = event->payload.data;
313
314 lastLoggedElements[LOG_ID_EVENTS] = elem;
315 total += htole32(swab);
316 // check for overflow
317 if (total >= UINT32_MAX) {
318 log(currentLast);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700319 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800320 return len;
321 }
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700322 stats.addTotal(currentLast);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800323 delete currentLast;
324 swab = total;
325 event->payload.data = htole32(swab);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700326 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800327 return len;
328 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800329 if (count == USHRT_MAX) {
330 log(dropped);
331 count = 1;
332 } else {
333 delete dropped;
334 ++count;
335 }
336 }
337 if (count) {
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700338 stats.addTotal(currentLast);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800339 currentLast->setDropped(count);
340 }
341 droppedElements[log_id] = currentLast;
342 lastLoggedElements[log_id] = elem;
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700343 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800344 return len;
345 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800346 if (dropped) { // State 1 or 2
347 if (count) { // State 2
348 log(dropped); // report chatty
349 } else { // State 1
350 delete dropped;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800351 }
Mark Salyzynae2abf12017-03-31 10:48:39 -0700352 droppedElements[log_id] = nullptr;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800353 log(currentLast); // report last message in the series
354 } else { // State 0
Mark Salyzyna2c02222016-12-13 10:31:29 -0800355 delete currentLast;
356 }
357 }
358 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800359
Mark Salyzyna2c02222016-12-13 10:31:29 -0800360 log(elem);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700361 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800362
363 return len;
364}
365
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700366// assumes LogBuffer::wrlock() held, owns elem, look after garbage collection
Mark Salyzyna2c02222016-12-13 10:31:29 -0800367void LogBuffer::log(LogBufferElement* elem) {
Mark Salyzyn09d66322017-03-14 13:11:12 -0700368 // cap on how far back we will sort in-place, otherwise append
369 static uint32_t too_far_back = 5; // five seconds
Mark Salyzyn0175b072014-02-26 09:50:16 -0800370 // Insert elements in time sorted order if possible
371 // NB: if end is region locked, place element at end of list
372 LogBufferElementCollection::iterator it = mLogElements.end();
373 LogBufferElementCollection::iterator last = it;
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800374 if (__predict_true(it != mLogElements.begin())) --it;
375 if (__predict_false(it == mLogElements.begin()) ||
Mark Salyzyn09d66322017-03-14 13:11:12 -0700376 __predict_true((*it)->getRealTime() <= elem->getRealTime()) ||
377 __predict_false((((*it)->getRealTime().tv_sec - too_far_back) >
378 elem->getRealTime().tv_sec) &&
379 (elem->getLogId() != LOG_ID_KERNEL) &&
380 ((*it)->getLogId() != LOG_ID_KERNEL))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800381 mLogElements.push_back(elem);
382 } else {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800383 log_time end = log_time::EPOCH;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800384 bool end_set = false;
385 bool end_always = false;
386
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700387 LogTimeEntry::rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800388
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700389 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800390 while (times != mTimes.end()) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800391 LogTimeEntry* entry = (*times);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800392 if (entry->owned_Locked()) {
393 if (!entry->mNonBlock) {
394 end_always = true;
395 break;
396 }
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800397 // it passing mEnd is blocked by the following checks.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800398 if (!end_set || (end <= entry->mEnd)) {
399 end = entry->mEnd;
400 end_set = true;
401 }
402 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700403 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800404 }
405
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800406 if (end_always || (end_set && (end > (*it)->getRealTime()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800407 mLogElements.push_back(elem);
408 } else {
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800409 // should be short as timestamps are localized near end()
410 do {
411 last = it;
412 if (__predict_false(it == mLogElements.begin())) {
413 break;
414 }
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800415 --it;
416 } while (((*it)->getRealTime() > elem->getRealTime()) &&
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800417 (!end_set || (end <= (*it)->getRealTime())));
Mark Salyzyna2c02222016-12-13 10:31:29 -0800418 mLogElements.insert(last, elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800419 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800420 LogTimeEntry::unlock();
421 }
422
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700423 stats.add(elem);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800424 maybePrune(elem->getLogId());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800425}
426
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700427// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800428//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700429// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800430void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800431 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700432 unsigned long maxSize = log_buffer_size(id);
433 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700434 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700435 size_t elements = stats.realElements(id);
436 size_t minElements = elements / 100;
437 if (minElements < minPrune) {
438 minElements = minPrune;
439 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700440 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700441 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700442 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800443 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700444 if (pruneRows > maxPrune) {
445 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700446 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800447 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800448 }
449}
450
Mark Salyzyn831aa292015-09-03 16:08:50 -0700451LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800452 LogBufferElementCollection::iterator it, bool coalesce) {
453 LogBufferElement* element = *it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700454 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700455
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700456 // Remove iterator references in the various lists that will become stale
457 // after the element is erased from the main logging list.
458
Mark Salyzyn501c3732017-03-10 14:31:54 -0800459 { // start of scope for found iterator
460 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
461 ? element->getTag()
462 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700463 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
464 if ((found != mLastWorst[id].end()) && (it == found->second)) {
465 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700466 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700467 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700468
Mark Salyzyn501c3732017-03-10 14:31:54 -0800469 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700470 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700471 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
472 // long term code stability, find() check should be fast for those ids.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700473 LogBufferPidIteratorMap::iterator found =
474 mLastWorstPidOfSystem[id].find(element->getPid());
Mark Salyzyn501c3732017-03-10 14:31:54 -0800475 if ((found != mLastWorstPidOfSystem[id].end()) &&
476 (it == found->second)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700477 mLastWorstPidOfSystem[id].erase(found);
478 }
479 }
480
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800481 bool setLast[LOG_ID_MAX];
482 bool doSetLast = false;
483 log_id_for_each(i) {
484 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
485 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700486#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
487 LogBufferElementCollection::iterator bad = it;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800488 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
489 ? element->getTag()
490 : element->getUid();
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700491#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700492 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800493 if (doSetLast) {
494 log_id_for_each(i) {
495 if (setLast[i]) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800496 if (__predict_false(it == mLogElements.end())) { // impossible
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800497 mLastSet[i] = false;
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700498 mLast[i] = mLogElements.begin();
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800499 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800500 mLast[i] = it; // push down the road as next-best-watermark
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800501 }
502 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800503 }
504 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700505#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
506 log_id_for_each(i) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800507 for (auto b : mLastWorst[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700508 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800509 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i,
510 b.first, key);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700511 }
512 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800513 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700514 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800515 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i,
516 b.first);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700517 }
518 }
519 if (mLastSet[i] && (bad == mLast[i])) {
520 android::prdebug("stale mLast[%d]\n", i);
521 mLastSet[i] = false;
522 mLast[i] = mLogElements.begin();
523 }
524 }
525#endif
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700526 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700527 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700528 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700529 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700530 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700531 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700532
533 return it;
534}
535
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700536// Define a temporary mechanism to report the last LogBufferElement pointer
537// for the specified uid, pid and tid. Used below to help merge-sort when
538// pruning for worst UID.
539class LogBufferElementKey {
540 const union {
541 struct {
Mark Salyzyn684bdb52016-12-13 12:44:20 -0800542 uint32_t uid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700543 uint16_t pid;
544 uint16_t tid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700545 } __packed;
546 uint64_t value;
547 } __packed;
548
Mark Salyzyn501c3732017-03-10 14:31:54 -0800549 public:
550 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid)
551 : uid(uid), pid(pid), tid(tid) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700552 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800553 explicit LogBufferElementKey(uint64_t key) : value(key) {
554 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700555
Mark Salyzyn501c3732017-03-10 14:31:54 -0800556 uint64_t getKey() {
557 return value;
558 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700559};
560
Mark Salyzyn511338d2015-05-19 09:12:30 -0700561class LogBufferElementLast {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800562 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn511338d2015-05-19 09:12:30 -0700563 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700564
Mark Salyzyn501c3732017-03-10 14:31:54 -0800565 public:
566 bool coalesce(LogBufferElement* element, unsigned short dropped) {
567 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700568 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700569 LogBufferElementMap::iterator it = map.find(key.getKey());
570 if (it != map.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800571 LogBufferElement* found = it->second;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700572 unsigned short moreDropped = found->getDropped();
573 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700574 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700575 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700576 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700577 return true;
578 }
579 }
580 return false;
581 }
582
Mark Salyzyn501c3732017-03-10 14:31:54 -0800583 void add(LogBufferElement* element) {
584 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700585 element->getTid());
586 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700587 }
588
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700589 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700590 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700591 }
592
Mark Salyzyn501c3732017-03-10 14:31:54 -0800593 void clear(LogBufferElement* element) {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800594 log_time current =
595 element->getRealTime() - log_time(EXPIRE_RATELIMIT, 0);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800596 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
597 LogBufferElement* mapElement = it->second;
598 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD) &&
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800599 (current > mapElement->getRealTime())) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700600 it = map.erase(it);
601 } else {
602 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700603 }
604 }
605 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700606};
607
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700608// Determine if watermark is within pruneMargin + 1s from the end of the list,
609// the caller will use this result to set an internal busy flag indicating
610// the prune operation could not be completed because a reader is blocking
611// the request.
612bool LogBuffer::isBusy(log_time watermark) {
613 LogBufferElementCollection::iterator ei = mLogElements.end();
614 --ei;
615 return watermark < ((*ei)->getRealTime() - pruneMargin - log_time(1, 0));
616}
617
618// If the selected reader is blocking our pruning progress, decide on
619// what kind of mitigation is necessary to unblock the situation.
620void LogBuffer::kickMe(LogTimeEntry* me, log_id_t id, unsigned long pruneRows) {
621 if (stats.sizes(id) > (2 * log_buffer_size(id))) { // +100%
622 // A misbehaving or slow reader has its connection
623 // dropped if we hit too much memory pressure.
624 me->release_Locked();
625 } else if (me->mTimeout.tv_sec || me->mTimeout.tv_nsec) {
626 // Allow a blocked WRAP timeout reader to
627 // trigger and start reporting the log data.
628 me->triggerReader_Locked();
629 } else {
630 // tell slow reader to skip entries to catch up
631 me->triggerSkip_Locked(id, pruneRows);
632 }
633}
634
Mark Salyzyn0175b072014-02-26 09:50:16 -0800635// prune "pruneRows" of type "id" from the buffer.
636//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700637// This garbage collection task is used to expire log entries. It is called to
638// remove all logs (clear), all UID logs (unprivileged clear), or every
639// 256 or 10% of the total logs (whichever is less) to prune the logs.
640//
641// First there is a prep phase where we discover the reader region lock that
642// acts as a backstop to any pruning activity to stop there and go no further.
643//
644// There are three major pruning loops that follow. All expire from the oldest
645// entries. Since there are multiple log buffers, the Android logging facility
646// will appear to drop entries 'in the middle' when looking at multiple log
647// sources and buffers. This effect is slightly more prominent when we prune
648// the worst offender by logging source. Thus the logs slowly loose content
649// and value as you move back in time. This is preferred since chatty sources
650// invariably move the logs value down faster as less chatty sources would be
651// expired in the noise.
652//
653// The first loop performs blacklisting and worst offender pruning. Falling
654// through when there are no notable worst offenders and have not hit the
655// region lock preventing further worst offender pruning. This loop also looks
656// after managing the chatty log entries and merging to help provide
657// statistical basis for blame. The chatty entries are not a notification of
658// how much logs you may have, but instead represent how much logs you would
659// have had in a virtual log buffer that is extended to cover all the in-memory
660// logs without loss. They last much longer than the represented pruned logs
661// since they get multiplied by the gains in the non-chatty log sources.
662//
663// The second loop get complicated because an algorithm of watermarks and
664// history is maintained to reduce the order and keep processing time
665// down to a minimum at scale. These algorithms can be costly in the face
666// of larger log buffers, or severly limited processing time granted to a
667// background task at lowest priority.
668//
669// This second loop does straight-up expiration from the end of the logs
670// (again, remember for the specified log buffer id) but does some whitelist
671// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
672// spam filtration all take priority. This second loop also checks if a region
673// lock is causing us to buffer too much in the logs to help the reader(s),
674// and will tell the slowest reader thread to skip log entries, and if
675// persistent and hits a further threshold, kill the reader thread.
676//
677// The third thread is optional, and only gets hit if there was a whitelist
678// and more needs to be pruned against the backstop of the region lock.
679//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700680// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700681//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700682bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzynae2abf12017-03-31 10:48:39 -0700683 LogTimeEntry* oldest = nullptr;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700684 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700685 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800686
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700687 LogTimeEntry::rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800688
689 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700690 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800691 while (times != mTimes.end()) {
692 LogTimeEntry* entry = (*times);
693 if (entry->owned_Locked() && entry->isWatching(id) &&
694 (!oldest || (oldest->mStart > entry->mStart) ||
695 ((oldest->mStart == entry->mStart) &&
696 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800697 oldest = entry;
698 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700699 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800700 }
Mark Salyzyn58363792017-04-17 12:46:12 -0700701 log_time watermark(log_time::tv_sec_max, log_time::tv_nsec_max);
702 if (oldest) watermark = oldest->mStart - pruneMargin;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800703
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800704 LogBufferElementCollection::iterator it;
705
Mark Salyzyn501c3732017-03-10 14:31:54 -0800706 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700707 // Only here if clear all request from non system source, so chatty
708 // filter logistics is not required.
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800709 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
710 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800711 LogBufferElement* element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700712
Mark Salyzyn501c3732017-03-10 14:31:54 -0800713 if ((element->getLogId() != id) ||
714 (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700715 ++it;
716 continue;
717 }
718
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800719 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
720 mLast[id] = it;
721 mLastSet[id] = true;
722 }
723
Mark Salyzyn58363792017-04-17 12:46:12 -0700724 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700725 busy = isBusy(watermark);
726 if (busy) kickMe(oldest, id, pruneRows);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700727 break;
728 }
729
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700730 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700731 if (--pruneRows == 0) {
732 break;
733 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700734 }
735 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700736 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700737 }
738
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700739 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn083b0372015-12-04 10:59:45 -0800740 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700741 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800742 // recalculate the worst offender on every batched pass
Mark Salyzyn501c3732017-03-10 14:31:54 -0800743 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800744 size_t worst_sizes = 0;
745 size_t second_worst_sizes = 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800746 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800747
Mark Salyzynae769232015-03-17 17:17:25 -0700748 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700749 // Calculate threshold as 12.5% of available storage
750 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700751
Mark Salyzyn6a066942016-07-14 15:34:30 -0700752 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800753 stats.sortTags(AID_ROOT, (pid_t)0, 2, id)
754 .findWorst(worst, worst_sizes, second_worst_sizes,
755 threshold);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700756 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700757 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800758 stats.sort(AID_ROOT, (pid_t)0, 2, id)
759 .findWorst(worst, worst_sizes, second_worst_sizes,
760 threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700761
Mark Salyzyn6a066942016-07-14 15:34:30 -0700762 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800763 stats.sortPids(worst, (pid_t)0, 2, id)
764 .findWorst(worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700765 }
766 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800767 }
768
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700769 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700770 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700771 break;
772 }
773
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800774 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700775 bool leading = true;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800776 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700777 // Perform at least one mandatory garbage collection cycle in following
778 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700779 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700780 // - check age-out of preserved logs
781 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700782 if (!gc && (worst != -1)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800783 { // begin scope for worst found iterator
784 LogBufferIteratorMap::iterator found =
785 mLastWorst[id].find(worst);
786 if ((found != mLastWorst[id].end()) &&
787 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700788 leading = false;
789 it = found->second;
790 }
791 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800792 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700793 // FYI: worstPid only set if !LOG_ID_EVENTS and
794 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzyn501c3732017-03-10 14:31:54 -0800795 LogBufferPidIteratorMap::iterator found =
796 mLastWorstPidOfSystem[id].find(worstPid);
797 if ((found != mLastWorstPidOfSystem[id].end()) &&
798 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700799 leading = false;
800 it = found->second;
801 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700802 }
803 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800804 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzynccfe8442015-08-24 13:43:27 -0700805 LogBufferElementCollection::iterator lastt;
806 lastt = mLogElements.end();
807 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700808 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700809 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800810 LogBufferElement* element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800811
Mark Salyzyn58363792017-04-17 12:46:12 -0700812 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700813 busy = isBusy(watermark);
814 // Do not let chatty eliding trigger any reader mitigation
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800815 break;
816 }
817
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700818 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800819 ++it;
820 continue;
821 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700822 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800823
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800824 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
825 mLast[id] = it;
826 mLastSet[id] = true;
827 }
828
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700829 unsigned short dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800830
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700831 // remove any leading drops
832 if (leading && dropped) {
833 it = erase(it);
834 continue;
835 }
836
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700837 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700838 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700839 continue;
840 }
841
Mark Salyzyn501c3732017-03-10 14:31:54 -0800842 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
843 ? element->getTag()
844 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700845
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700846 if (hasBlacklist && mPrune.naughty(element)) {
847 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700848 it = erase(it);
849 if (dropped) {
850 continue;
851 }
852
853 pruneRows--;
854 if (pruneRows == 0) {
855 break;
856 }
857
Mark Salyzyn6a066942016-07-14 15:34:30 -0700858 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700859 kick = true;
860 if (worst_sizes < second_worst_sizes) {
861 break;
862 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700863 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700864 }
865 continue;
866 }
867
Mark Salyzyn501c3732017-03-10 14:31:54 -0800868 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
869 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700870 break;
871 }
872
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700873 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700874 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800875 if (worstPid &&
876 ((!gc && (element->getPid() == worstPid)) ||
877 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
878 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700879 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700880 // watermark if current one empty. id is not LOG_ID_EVENTS
881 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700882 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700883 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800884 if ((!gc && !worstPid && (key == worst)) ||
885 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700886 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700887 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800888 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700889 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800890 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700891
Mark Salyzyn501c3732017-03-10 14:31:54 -0800892 if ((key != worst) ||
893 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700894 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700895 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700896 ++it;
897 continue;
898 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700899 // key == worst below here
900 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700901
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700902 pruneRows--;
903 if (pruneRows == 0) {
904 break;
905 }
906
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700907 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700908
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700909 unsigned short len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700910
911 // do not create any leading drops
912 if (leading) {
913 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700914 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700915 stats.drop(element);
916 element->setDropped(1);
917 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700918 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700919 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700920 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800921 if (worstPid &&
922 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
923 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700924 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700925 // watermark if current one empty. id is not
926 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700927 mLastWorstPidOfSystem[id][worstPid] = it;
928 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700929 if ((!gc && !worstPid) ||
Mark Salyzyn501c3732017-03-10 14:31:54 -0800930 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700931 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700932 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700933 ++it;
934 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700935 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700936 if (worst_sizes < second_worst_sizes) {
937 break;
938 }
939 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800940 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700941 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800942
Mark Salyzyn1c950472014-04-01 17:19:47 -0700943 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800944 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800945 }
946 }
947
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800948 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800949 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800950 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800951 while ((pruneRows > 0) && (it != mLogElements.end())) {
952 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700953
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700954 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700955 it++;
956 continue;
957 }
958
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800959 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
960 mLast[id] = it;
961 mLastSet[id] = true;
962 }
963
Mark Salyzyn58363792017-04-17 12:46:12 -0700964 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700965 busy = isBusy(watermark);
966 if (!whitelist && busy) kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700967 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800968 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700969
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700970 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
971 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700972 whitelist = true;
973 it++;
974 continue;
975 }
976
977 it = erase(it);
978 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800979 }
980
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700981 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800982 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800983 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800984 while ((it != mLogElements.end()) && (pruneRows > 0)) {
985 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700986
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700987 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700988 ++it;
989 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800990 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700991
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800992 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
993 mLast[id] = it;
994 mLastSet[id] = true;
995 }
996
Mark Salyzyn58363792017-04-17 12:46:12 -0700997 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700998 busy = isBusy(watermark);
999 if (busy) kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -07001000 break;
1001 }
1002
1003 it = erase(it);
1004 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001005 }
1006 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001007
Mark Salyzyn0175b072014-02-26 09:50:16 -08001008 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001009
1010 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001011}
1012
1013// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -07001014bool LogBuffer::clear(log_id_t id, uid_t uid) {
1015 bool busy = true;
1016 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
1017 for (int retry = 4;;) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001018 if (retry == 1) { // last pass
Mark Salyzync5dc9702015-09-16 15:34:00 -07001019 // Check if it is still busy after the sleep, we say prune
1020 // one entry, not another clear run, so we are looking for
1021 // the quick side effect of the return value to tell us if
1022 // we have a _blocked_ reader.
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001023 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001024 busy = prune(id, 1, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001025 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001026 // It is still busy, blocked reader(s), lets kill them all!
1027 // otherwise, lets be a good citizen and preserve the slow
1028 // readers and let the clear run (below) deal with determining
1029 // if we are still blocked and return an error code to caller.
1030 if (busy) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001031 LogTimeEntry::wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001032 LastLogTimes::iterator times = mTimes.begin();
1033 while (times != mTimes.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001034 LogTimeEntry* entry = (*times);
Mark Salyzync5dc9702015-09-16 15:34:00 -07001035 // Killer punch
1036 if (entry->owned_Locked() && entry->isWatching(id)) {
1037 entry->release_Locked();
1038 }
1039 times++;
1040 }
1041 LogTimeEntry::unlock();
1042 }
1043 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001044 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001045 busy = prune(id, ULONG_MAX, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001046 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001047 if (!busy || !--retry) {
1048 break;
1049 }
Mark Salyzyn501c3732017-03-10 14:31:54 -08001050 sleep(1); // Let reader(s) catch up after notification
Mark Salyzync5dc9702015-09-16 15:34:00 -07001051 }
1052 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001053}
1054
1055// get the used space associated with "id".
1056unsigned long LogBuffer::getSizeUsed(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001057 rdlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001058 size_t retval = stats.sizes(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001059 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001060 return retval;
1061}
1062
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001063// set the total space allocated to "id"
1064int LogBuffer::setSize(log_id_t id, unsigned long size) {
1065 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -07001066 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001067 return -1;
1068 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001069 wrlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001070 log_buffer_size(id) = size;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001071 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001072 return 0;
1073}
1074
1075// get the total space allocated to "id"
1076unsigned long LogBuffer::getSize(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001077 rdlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001078 size_t retval = log_buffer_size(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001079 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001080 return retval;
1081}
1082
Mark Salyzynae2abf12017-03-31 10:48:39 -07001083log_time LogBuffer::flushTo(SocketClient* reader, const log_time& start,
1084 pid_t* lastTid, bool privileged, bool security,
1085 int (*filter)(const LogBufferElement* element,
1086 void* arg),
1087 void* arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001088 LogBufferElementCollection::iterator it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001089 uid_t uid = reader->getUid();
1090
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001091 rdlock();
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001092
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -08001093 if (start == log_time::EPOCH) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001094 // client wants to start from the beginning
1095 it = mLogElements.begin();
1096 } else {
Mark Salyzyn0f92fdc2017-04-04 10:57:41 -07001097 // 3 second limit to continue search for out-of-order entries.
Mark Salyzyn58363792017-04-17 12:46:12 -07001098 log_time min = start - pruneMargin;
1099
Mark Salyzyn0f92fdc2017-04-04 10:57:41 -07001100 // Cap to 300 iterations we look back for out-of-order entries.
1101 size_t count = 300;
Mark Salyzyn58363792017-04-17 12:46:12 -07001102
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001103 // Client wants to start from some specified time. Chances are
1104 // we are better off starting from the end of the time sorted list.
Mark Salyzyn58363792017-04-17 12:46:12 -07001105 LogBufferElementCollection::iterator last;
Mark Salyzyn1f467162017-03-27 13:12:41 -07001106 for (last = it = mLogElements.end(); it != mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -08001107 /* do nothing */) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001108 --it;
Mark Salyzyn501c3732017-03-10 14:31:54 -08001109 LogBufferElement* element = *it;
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001110 if (element->getRealTime() > start) {
1111 last = it;
Mark Salyzyn0f92fdc2017-04-04 10:57:41 -07001112 } else if (!--count || (element->getRealTime() < min)) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001113 break;
1114 }
1115 }
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001116 it = last;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001117 }
1118
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001119 log_time max = start;
Mark Salyzynb5b87962017-01-23 14:20:31 -08001120
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001121 LogBufferElement* lastElement = nullptr; // iterator corruption paranoia
1122 static const size_t maxSkip = 4194304; // maximum entries to skip
1123 size_t skip = maxSkip;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001124 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001125 LogBufferElement* element = *it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001126
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001127 if (!--skip) {
1128 android::prdebug("reader.per: too many elements skipped");
1129 break;
1130 }
1131 if (element == lastElement) {
1132 android::prdebug("reader.per: identical elements");
1133 break;
1134 }
1135 lastElement = element;
1136
Mark Salyzyn0175b072014-02-26 09:50:16 -08001137 if (!privileged && (element->getUid() != uid)) {
1138 continue;
1139 }
1140
Mark Salyzyn8fa88962016-01-26 14:32:35 -08001141 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1142 continue;
1143 }
1144
Mark Salyzyneb45db22017-05-17 19:55:12 +00001145 if (element->getRealTime() <= start) {
1146 continue;
1147 }
1148
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001149 // NB: calling out to another object with wrlock() held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001150 if (filter) {
1151 int ret = (*filter)(element, arg);
1152 if (ret == false) {
1153 continue;
1154 }
1155 if (ret != true) {
1156 break;
1157 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001158 }
1159
Mark Salyzynae2abf12017-03-31 10:48:39 -07001160 bool sameTid = false;
1161 if (lastTid) {
1162 sameTid = lastTid[element->getLogId()] == element->getTid();
1163 // Dropped (chatty) immediately following a valid log from the
1164 // same source in the same log buffer indicates we have a
1165 // multiple identical squash. chatty that differs source
1166 // is due to spam filter. chatty to chatty of different
1167 // source is also due to spam filter.
1168 lastTid[element->getLogId()] =
1169 (element->getDropped() && !sameTid) ? 0 : element->getTid();
1170 }
Mark Salyzynb5b87962017-01-23 14:20:31 -08001171
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001172 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001173
1174 // range locking in LastLogTimes looks after us
Mark Salyzyneb45db22017-05-17 19:55:12 +00001175 max = element->flushTo(reader, this, privileged, sameTid);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001176
Mark Salyzyneb45db22017-05-17 19:55:12 +00001177 if (max == element->FLUSH_ERROR) {
1178 return max;
1179 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001180
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001181 skip = maxSkip;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001182 rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001183 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001184 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001185
1186 return max;
1187}
Mark Salyzyn34facab2014-02-06 14:48:50 -08001188
Mark Salyzynee3b8382015-12-17 09:58:43 -08001189std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1190 unsigned int logMask) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001191 wrlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001192
Mark Salyzynee3b8382015-12-17 09:58:43 -08001193 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001194
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001195 unlock();
Mark Salyzyn73160ac2015-08-20 10:01:44 -07001196
1197 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001198}