blob: 8b609cba2a0b425809dc8dc826002e927b19cbab [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)) &&
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700162 (elem->getTag() == LIBLOG_LOG_TAG)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800163 return SAME_LIBLOG;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700164 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800165 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800166
167 // audit message (except sequence number) identical?
Mark Salyzyna2c02222016-12-13 10:31:29 -0800168 if (last->isBinary()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800169 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) -
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700170 sizeof(int32_t))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800171 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700172 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800173 msgl += sizeof(android_log_event_string_t);
174 lenl -= sizeof(android_log_event_string_t);
175 msgr += sizeof(android_log_event_string_t);
176 lenr -= sizeof(android_log_event_string_t);
177 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700178 static const char avc[] = "): avc: ";
Mark Salyzyn501c3732017-03-10 14:31:54 -0800179 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800180 if (!avcl) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800181 lenl -= avcl - msgl;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800182 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800183 if (!avcr) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800184 lenr -= avcr - msgr;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800185 if (lenl != lenr) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700186 if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc),
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700187 lenl - strlen(avc))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800188 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700189 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800190 return SAME;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800191}
192
Mark Salyzyn501c3732017-03-10 14:31:54 -0800193int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
194 pid_t tid, const char* msg, unsigned short len) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800195 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800196 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800197 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700198
Mark Salyzyn501c3732017-03-10 14:31:54 -0800199 LogBufferElement* elem =
200 new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000201 if (log_id != LOG_ID_SECURITY) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800202 int prio = ANDROID_LOG_INFO;
Mark Salyzynae2abf12017-03-31 10:48:39 -0700203 const char* tag = nullptr;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800204 if (log_id == LOG_ID_EVENTS) {
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700205 tag = tagToName(elem->getTag());
Mark Salyzyn083b0372015-12-04 10:59:45 -0800206 } else {
207 prio = *msg;
208 tag = msg + 1;
209 }
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700210 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800211 // Log traffic received to total
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700212 wrlock();
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700213 stats.addTotal(elem);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700214 unlock();
Mark Salyzyn083b0372015-12-04 10:59:45 -0800215 delete elem;
216 return -EACCES;
217 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700218 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800219
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700220 wrlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800221 LogBufferElement* currentLast = lastLoggedElements[log_id];
222 if (currentLast) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800223 LogBufferElement* dropped = droppedElements[log_id];
Mark Salyzyna2c02222016-12-13 10:31:29 -0800224 unsigned short count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800225 //
226 // State Init
227 // incoming:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700228 // dropped = nullptr
229 // currentLast = nullptr;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800230 // elem = incoming message
231 // outgoing:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700232 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800233 // currentLast = copy of elem
234 // log elem
235 // State 0
236 // incoming:
237 // count = 0
Mark Salyzynae2abf12017-03-31 10:48:39 -0700238 // dropped = nullptr
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800239 // currentLast = copy of last message
240 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800241 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800242 // dropped = copy of first identical message -> State 1
243 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800244 // break: if match == DIFFERENT
Mark Salyzynae2abf12017-03-31 10:48:39 -0700245 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800246 // delete copy of last message (incoming currentLast)
247 // currentLast = copy of elem
248 // log elem
249 // State 1
250 // incoming:
251 // count = 0
252 // dropped = copy of first identical message
253 // currentLast = reference to last held-back incoming
254 // message
255 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800256 // outgoing: if match == SAME
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800257 // delete copy of first identical message (dropped)
258 // dropped = reference to last held-back incoming
259 // message set to chatty count of 1 -> State 2
260 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800261 // outgoing: if match == SAME_LIBLOG
262 // dropped = copy of first identical message -> State 1
263 // take sum of currentLast and elem
264 // if sum overflows:
265 // log currentLast
266 // currentLast = reference to elem
267 // else
268 // delete currentLast
269 // currentLast = reference to elem, sum liblog.
270 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800271 // delete dropped
Mark Salyzynae2abf12017-03-31 10:48:39 -0700272 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800273 // log reference to last held-back (currentLast)
274 // currentLast = copy of elem
275 // log elem
276 // State 2
277 // incoming:
278 // count = chatty count
279 // dropped = chatty message holding count
280 // currentLast = reference to last held-back incoming
281 // message.
282 // dropped = chatty message holding count
283 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800284 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800285 // delete chatty message holding count
286 // dropped = reference to last held-back incoming
287 // message, set to chatty count + 1
288 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800289 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800290 // log dropped (chatty message)
Mark Salyzynae2abf12017-03-31 10:48:39 -0700291 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800292 // log reference to last held-back (currentLast)
293 // currentLast = copy of elem
294 // log elem
295 //
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800296 enum match_type match = identical(elem, currentLast);
297 if (match != DIFFERENT) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800298 if (dropped) {
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800299 // Sum up liblog tag messages?
300 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
301 android_log_event_int_t* event =
302 reinterpret_cast<android_log_event_int_t*>(
303 const_cast<char*>(currentLast->getMsg()));
304 //
305 // To unit test, differentiate with something like:
306 // event->header.tag = htole32(CHATTY_LOG_TAG);
307 // here, then instead of delete currentLast below,
308 // log(currentLast) to see the incremental sums form.
309 //
310 uint32_t swab = event->payload.data;
311 unsigned long long total = htole32(swab);
312 event = reinterpret_cast<android_log_event_int_t*>(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800313 const_cast<char*>(elem->getMsg()));
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800314 swab = event->payload.data;
315
316 lastLoggedElements[LOG_ID_EVENTS] = elem;
317 total += htole32(swab);
318 // check for overflow
319 if (total >= UINT32_MAX) {
320 log(currentLast);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700321 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800322 return len;
323 }
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700324 stats.addTotal(currentLast);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800325 delete currentLast;
326 swab = total;
327 event->payload.data = htole32(swab);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700328 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800329 return len;
330 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800331 if (count == USHRT_MAX) {
332 log(dropped);
333 count = 1;
334 } else {
335 delete dropped;
336 ++count;
337 }
338 }
339 if (count) {
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700340 stats.addTotal(currentLast);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800341 currentLast->setDropped(count);
342 }
343 droppedElements[log_id] = currentLast;
344 lastLoggedElements[log_id] = elem;
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700345 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800346 return len;
347 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800348 if (dropped) { // State 1 or 2
349 if (count) { // State 2
350 log(dropped); // report chatty
351 } else { // State 1
352 delete dropped;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800353 }
Mark Salyzynae2abf12017-03-31 10:48:39 -0700354 droppedElements[log_id] = nullptr;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800355 log(currentLast); // report last message in the series
356 } else { // State 0
Mark Salyzyna2c02222016-12-13 10:31:29 -0800357 delete currentLast;
358 }
359 }
360 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800361
Mark Salyzyna2c02222016-12-13 10:31:29 -0800362 log(elem);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700363 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800364
365 return len;
366}
367
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700368// assumes LogBuffer::wrlock() held, owns elem, look after garbage collection
Mark Salyzyna2c02222016-12-13 10:31:29 -0800369void LogBuffer::log(LogBufferElement* elem) {
Mark Salyzyn09d66322017-03-14 13:11:12 -0700370 // cap on how far back we will sort in-place, otherwise append
371 static uint32_t too_far_back = 5; // five seconds
Mark Salyzyn0175b072014-02-26 09:50:16 -0800372 // Insert elements in time sorted order if possible
373 // NB: if end is region locked, place element at end of list
374 LogBufferElementCollection::iterator it = mLogElements.end();
375 LogBufferElementCollection::iterator last = it;
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800376 if (__predict_true(it != mLogElements.begin())) --it;
377 if (__predict_false(it == mLogElements.begin()) ||
Mark Salyzyn09d66322017-03-14 13:11:12 -0700378 __predict_true((*it)->getRealTime() <= elem->getRealTime()) ||
379 __predict_false((((*it)->getRealTime().tv_sec - too_far_back) >
380 elem->getRealTime().tv_sec) &&
381 (elem->getLogId() != LOG_ID_KERNEL) &&
382 ((*it)->getLogId() != LOG_ID_KERNEL))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800383 mLogElements.push_back(elem);
384 } else {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800385 log_time end = log_time::EPOCH;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800386 bool end_set = false;
387 bool end_always = false;
388
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700389 LogTimeEntry::rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800390
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700391 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800392 while (times != mTimes.end()) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800393 LogTimeEntry* entry = (*times);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800394 if (entry->owned_Locked()) {
395 if (!entry->mNonBlock) {
396 end_always = true;
397 break;
398 }
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800399 // it passing mEnd is blocked by the following checks.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800400 if (!end_set || (end <= entry->mEnd)) {
401 end = entry->mEnd;
402 end_set = true;
403 }
404 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700405 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800406 }
407
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800408 if (end_always || (end_set && (end > (*it)->getRealTime()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800409 mLogElements.push_back(elem);
410 } else {
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800411 // should be short as timestamps are localized near end()
412 do {
413 last = it;
414 if (__predict_false(it == mLogElements.begin())) {
415 break;
416 }
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800417 --it;
418 } while (((*it)->getRealTime() > elem->getRealTime()) &&
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800419 (!end_set || (end <= (*it)->getRealTime())));
Mark Salyzyna2c02222016-12-13 10:31:29 -0800420 mLogElements.insert(last, elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800421 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800422 LogTimeEntry::unlock();
423 }
424
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700425 stats.add(elem);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800426 maybePrune(elem->getLogId());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800427}
428
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700429// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800430//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700431// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800432void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800433 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700434 unsigned long maxSize = log_buffer_size(id);
435 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700436 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700437 size_t elements = stats.realElements(id);
438 size_t minElements = elements / 100;
439 if (minElements < minPrune) {
440 minElements = minPrune;
441 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700442 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700443 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700444 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800445 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700446 if (pruneRows > maxPrune) {
447 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700448 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800449 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800450 }
451}
452
Mark Salyzyn831aa292015-09-03 16:08:50 -0700453LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800454 LogBufferElementCollection::iterator it, bool coalesce) {
455 LogBufferElement* element = *it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700456 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700457
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700458 // Remove iterator references in the various lists that will become stale
459 // after the element is erased from the main logging list.
460
Mark Salyzyn501c3732017-03-10 14:31:54 -0800461 { // start of scope for found iterator
462 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
463 ? element->getTag()
464 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700465 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
466 if ((found != mLastWorst[id].end()) && (it == found->second)) {
467 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700468 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700469 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700470
Mark Salyzyn501c3732017-03-10 14:31:54 -0800471 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700472 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700473 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
474 // long term code stability, find() check should be fast for those ids.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700475 LogBufferPidIteratorMap::iterator found =
476 mLastWorstPidOfSystem[id].find(element->getPid());
Mark Salyzyn501c3732017-03-10 14:31:54 -0800477 if ((found != mLastWorstPidOfSystem[id].end()) &&
478 (it == found->second)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700479 mLastWorstPidOfSystem[id].erase(found);
480 }
481 }
482
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800483 bool setLast[LOG_ID_MAX];
484 bool doSetLast = false;
485 log_id_for_each(i) {
486 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
487 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700488#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
489 LogBufferElementCollection::iterator bad = it;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800490 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
491 ? element->getTag()
492 : element->getUid();
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700493#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700494 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800495 if (doSetLast) {
496 log_id_for_each(i) {
497 if (setLast[i]) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800498 if (__predict_false(it == mLogElements.end())) { // impossible
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800499 mLastSet[i] = false;
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700500 mLast[i] = mLogElements.begin();
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800501 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800502 mLast[i] = it; // push down the road as next-best-watermark
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800503 }
504 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800505 }
506 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700507#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
508 log_id_for_each(i) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800509 for (auto b : mLastWorst[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700510 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800511 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i,
512 b.first, key);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700513 }
514 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800515 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700516 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800517 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i,
518 b.first);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700519 }
520 }
521 if (mLastSet[i] && (bad == mLast[i])) {
522 android::prdebug("stale mLast[%d]\n", i);
523 mLastSet[i] = false;
524 mLast[i] = mLogElements.begin();
525 }
526 }
527#endif
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700528 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700529 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700530 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700531 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700532 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700533 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700534
535 return it;
536}
537
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700538// Define a temporary mechanism to report the last LogBufferElement pointer
539// for the specified uid, pid and tid. Used below to help merge-sort when
540// pruning for worst UID.
541class LogBufferElementKey {
542 const union {
543 struct {
Mark Salyzyn684bdb52016-12-13 12:44:20 -0800544 uint32_t uid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700545 uint16_t pid;
546 uint16_t tid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700547 } __packed;
548 uint64_t value;
549 } __packed;
550
Mark Salyzyn501c3732017-03-10 14:31:54 -0800551 public:
552 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid)
553 : uid(uid), pid(pid), tid(tid) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700554 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800555 explicit LogBufferElementKey(uint64_t key) : value(key) {
556 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700557
Mark Salyzyn501c3732017-03-10 14:31:54 -0800558 uint64_t getKey() {
559 return value;
560 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700561};
562
Mark Salyzyn511338d2015-05-19 09:12:30 -0700563class LogBufferElementLast {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800564 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn511338d2015-05-19 09:12:30 -0700565 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700566
Mark Salyzyn501c3732017-03-10 14:31:54 -0800567 public:
568 bool coalesce(LogBufferElement* element, unsigned short dropped) {
569 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700570 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700571 LogBufferElementMap::iterator it = map.find(key.getKey());
572 if (it != map.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800573 LogBufferElement* found = it->second;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700574 unsigned short moreDropped = found->getDropped();
575 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700576 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700577 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700578 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700579 return true;
580 }
581 }
582 return false;
583 }
584
Mark Salyzyn501c3732017-03-10 14:31:54 -0800585 void add(LogBufferElement* element) {
586 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700587 element->getTid());
588 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700589 }
590
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700591 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700592 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700593 }
594
Mark Salyzyn501c3732017-03-10 14:31:54 -0800595 void clear(LogBufferElement* element) {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800596 log_time current =
597 element->getRealTime() - log_time(EXPIRE_RATELIMIT, 0);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800598 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
599 LogBufferElement* mapElement = it->second;
600 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD) &&
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800601 (current > mapElement->getRealTime())) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700602 it = map.erase(it);
603 } else {
604 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700605 }
606 }
607 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700608};
609
Mark Salyzyn0175b072014-02-26 09:50:16 -0800610// prune "pruneRows" of type "id" from the buffer.
611//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700612// This garbage collection task is used to expire log entries. It is called to
613// remove all logs (clear), all UID logs (unprivileged clear), or every
614// 256 or 10% of the total logs (whichever is less) to prune the logs.
615//
616// First there is a prep phase where we discover the reader region lock that
617// acts as a backstop to any pruning activity to stop there and go no further.
618//
619// There are three major pruning loops that follow. All expire from the oldest
620// entries. Since there are multiple log buffers, the Android logging facility
621// will appear to drop entries 'in the middle' when looking at multiple log
622// sources and buffers. This effect is slightly more prominent when we prune
623// the worst offender by logging source. Thus the logs slowly loose content
624// and value as you move back in time. This is preferred since chatty sources
625// invariably move the logs value down faster as less chatty sources would be
626// expired in the noise.
627//
628// The first loop performs blacklisting and worst offender pruning. Falling
629// through when there are no notable worst offenders and have not hit the
630// region lock preventing further worst offender pruning. This loop also looks
631// after managing the chatty log entries and merging to help provide
632// statistical basis for blame. The chatty entries are not a notification of
633// how much logs you may have, but instead represent how much logs you would
634// have had in a virtual log buffer that is extended to cover all the in-memory
635// logs without loss. They last much longer than the represented pruned logs
636// since they get multiplied by the gains in the non-chatty log sources.
637//
638// The second loop get complicated because an algorithm of watermarks and
639// history is maintained to reduce the order and keep processing time
640// down to a minimum at scale. These algorithms can be costly in the face
641// of larger log buffers, or severly limited processing time granted to a
642// background task at lowest priority.
643//
644// This second loop does straight-up expiration from the end of the logs
645// (again, remember for the specified log buffer id) but does some whitelist
646// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
647// spam filtration all take priority. This second loop also checks if a region
648// lock is causing us to buffer too much in the logs to help the reader(s),
649// and will tell the slowest reader thread to skip log entries, and if
650// persistent and hits a further threshold, kill the reader thread.
651//
652// The third thread is optional, and only gets hit if there was a whitelist
653// and more needs to be pruned against the backstop of the region lock.
654//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700655// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700656//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700657bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzynae2abf12017-03-31 10:48:39 -0700658 LogTimeEntry* oldest = nullptr;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700659 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700660 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800661
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700662 LogTimeEntry::rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800663
664 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700665 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800666 while (times != mTimes.end()) {
667 LogTimeEntry* entry = (*times);
668 if (entry->owned_Locked() && entry->isWatching(id) &&
669 (!oldest || (oldest->mStart > entry->mStart) ||
670 ((oldest->mStart == entry->mStart) &&
671 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800672 oldest = entry;
673 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700674 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800675 }
Mark Salyzyn58363792017-04-17 12:46:12 -0700676 log_time watermark(log_time::tv_sec_max, log_time::tv_nsec_max);
677 if (oldest) watermark = oldest->mStart - pruneMargin;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800678
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800679 LogBufferElementCollection::iterator it;
680
Mark Salyzyn501c3732017-03-10 14:31:54 -0800681 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700682 // Only here if clear all request from non system source, so chatty
683 // filter logistics is not required.
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800684 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
685 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800686 LogBufferElement* element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700687
Mark Salyzyn501c3732017-03-10 14:31:54 -0800688 if ((element->getLogId() != id) ||
689 (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700690 ++it;
691 continue;
692 }
693
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800694 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
695 mLast[id] = it;
696 mLastSet[id] = true;
697 }
698
Mark Salyzyn58363792017-04-17 12:46:12 -0700699 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700700 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800701 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
702 oldest->triggerReader_Locked();
703 } else {
704 oldest->triggerSkip_Locked(id, pruneRows);
705 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700706 break;
707 }
708
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700709 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700710 if (--pruneRows == 0) {
711 break;
712 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700713 }
714 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700715 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700716 }
717
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700718 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn083b0372015-12-04 10:59:45 -0800719 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700720 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800721 // recalculate the worst offender on every batched pass
Mark Salyzyn501c3732017-03-10 14:31:54 -0800722 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800723 size_t worst_sizes = 0;
724 size_t second_worst_sizes = 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800725 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800726
Mark Salyzynae769232015-03-17 17:17:25 -0700727 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700728 // Calculate threshold as 12.5% of available storage
729 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700730
Mark Salyzyn6a066942016-07-14 15:34:30 -0700731 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800732 stats.sortTags(AID_ROOT, (pid_t)0, 2, id)
733 .findWorst(worst, worst_sizes, second_worst_sizes,
734 threshold);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700735 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700736 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800737 stats.sort(AID_ROOT, (pid_t)0, 2, id)
738 .findWorst(worst, worst_sizes, second_worst_sizes,
739 threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700740
Mark Salyzyn6a066942016-07-14 15:34:30 -0700741 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800742 stats.sortPids(worst, (pid_t)0, 2, id)
743 .findWorst(worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700744 }
745 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800746 }
747
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700748 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700749 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700750 break;
751 }
752
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800753 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700754 bool leading = true;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800755 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700756 // Perform at least one mandatory garbage collection cycle in following
757 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700758 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700759 // - check age-out of preserved logs
760 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700761 if (!gc && (worst != -1)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800762 { // begin scope for worst found iterator
763 LogBufferIteratorMap::iterator found =
764 mLastWorst[id].find(worst);
765 if ((found != mLastWorst[id].end()) &&
766 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700767 leading = false;
768 it = found->second;
769 }
770 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800771 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700772 // FYI: worstPid only set if !LOG_ID_EVENTS and
773 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzyn501c3732017-03-10 14:31:54 -0800774 LogBufferPidIteratorMap::iterator found =
775 mLastWorstPidOfSystem[id].find(worstPid);
776 if ((found != mLastWorstPidOfSystem[id].end()) &&
777 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700778 leading = false;
779 it = found->second;
780 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700781 }
782 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800783 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzynccfe8442015-08-24 13:43:27 -0700784 LogBufferElementCollection::iterator lastt;
785 lastt = mLogElements.end();
786 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700787 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700788 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800789 LogBufferElement* element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800790
Mark Salyzyn58363792017-04-17 12:46:12 -0700791 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700792 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800793 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
794 oldest->triggerReader_Locked();
795 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800796 break;
797 }
798
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700799 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800800 ++it;
801 continue;
802 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700803 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800804
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800805 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
806 mLast[id] = it;
807 mLastSet[id] = true;
808 }
809
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700810 unsigned short dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800811
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700812 // remove any leading drops
813 if (leading && dropped) {
814 it = erase(it);
815 continue;
816 }
817
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700818 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700819 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700820 continue;
821 }
822
Mark Salyzyn501c3732017-03-10 14:31:54 -0800823 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
824 ? element->getTag()
825 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700826
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700827 if (hasBlacklist && mPrune.naughty(element)) {
828 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700829 it = erase(it);
830 if (dropped) {
831 continue;
832 }
833
834 pruneRows--;
835 if (pruneRows == 0) {
836 break;
837 }
838
Mark Salyzyn6a066942016-07-14 15:34:30 -0700839 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700840 kick = true;
841 if (worst_sizes < second_worst_sizes) {
842 break;
843 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700844 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700845 }
846 continue;
847 }
848
Mark Salyzyn501c3732017-03-10 14:31:54 -0800849 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
850 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700851 break;
852 }
853
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700854 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700855 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800856 if (worstPid &&
857 ((!gc && (element->getPid() == worstPid)) ||
858 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
859 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700860 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700861 // watermark if current one empty. id is not LOG_ID_EVENTS
862 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700863 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700864 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800865 if ((!gc && !worstPid && (key == worst)) ||
866 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700867 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700868 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800869 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700870 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800871 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700872
Mark Salyzyn501c3732017-03-10 14:31:54 -0800873 if ((key != worst) ||
874 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700875 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700876 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700877 ++it;
878 continue;
879 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700880 // key == worst below here
881 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700882
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700883 pruneRows--;
884 if (pruneRows == 0) {
885 break;
886 }
887
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700888 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700889
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700890 unsigned short len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700891
892 // do not create any leading drops
893 if (leading) {
894 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700895 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700896 stats.drop(element);
897 element->setDropped(1);
898 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700899 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700900 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700901 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800902 if (worstPid &&
903 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
904 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700905 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700906 // watermark if current one empty. id is not
907 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700908 mLastWorstPidOfSystem[id][worstPid] = it;
909 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700910 if ((!gc && !worstPid) ||
Mark Salyzyn501c3732017-03-10 14:31:54 -0800911 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700912 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700913 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700914 ++it;
915 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700916 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700917 if (worst_sizes < second_worst_sizes) {
918 break;
919 }
920 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800921 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700922 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800923
Mark Salyzyn1c950472014-04-01 17:19:47 -0700924 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800925 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800926 }
927 }
928
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800929 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800930 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800931 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800932 while ((pruneRows > 0) && (it != mLogElements.end())) {
933 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700934
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700935 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700936 it++;
937 continue;
938 }
939
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800940 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
941 mLast[id] = it;
942 mLastSet[id] = true;
943 }
944
Mark Salyzyn58363792017-04-17 12:46:12 -0700945 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700946 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700947 if (whitelist) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800948 break;
949 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700950
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700951 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
952 // kick a misbehaving log reader client off the island
953 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800954 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
955 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700956 } else {
957 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800958 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700959 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800960 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700961
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700962 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
963 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700964 whitelist = true;
965 it++;
966 continue;
967 }
968
969 it = erase(it);
970 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800971 }
972
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700973 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800974 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800975 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800976 while ((it != mLogElements.end()) && (pruneRows > 0)) {
977 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700978
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700979 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700980 ++it;
981 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800982 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700983
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800984 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
985 mLast[id] = it;
986 mLastSet[id] = true;
987 }
988
Mark Salyzyn58363792017-04-17 12:46:12 -0700989 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700990 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700991 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
992 // kick a misbehaving log reader client off the island
993 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800994 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
995 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700996 } else {
997 oldest->triggerSkip_Locked(id, pruneRows);
998 }
999 break;
1000 }
1001
1002 it = erase(it);
1003 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001004 }
1005 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001006
Mark Salyzyn0175b072014-02-26 09:50:16 -08001007 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001008
1009 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001010}
1011
1012// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -07001013bool LogBuffer::clear(log_id_t id, uid_t uid) {
1014 bool busy = true;
1015 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
1016 for (int retry = 4;;) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001017 if (retry == 1) { // last pass
Mark Salyzync5dc9702015-09-16 15:34:00 -07001018 // Check if it is still busy after the sleep, we say prune
1019 // one entry, not another clear run, so we are looking for
1020 // the quick side effect of the return value to tell us if
1021 // we have a _blocked_ reader.
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001022 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001023 busy = prune(id, 1, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001024 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001025 // It is still busy, blocked reader(s), lets kill them all!
1026 // otherwise, lets be a good citizen and preserve the slow
1027 // readers and let the clear run (below) deal with determining
1028 // if we are still blocked and return an error code to caller.
1029 if (busy) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001030 LogTimeEntry::wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001031 LastLogTimes::iterator times = mTimes.begin();
1032 while (times != mTimes.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001033 LogTimeEntry* entry = (*times);
Mark Salyzync5dc9702015-09-16 15:34:00 -07001034 // Killer punch
1035 if (entry->owned_Locked() && entry->isWatching(id)) {
1036 entry->release_Locked();
1037 }
1038 times++;
1039 }
1040 LogTimeEntry::unlock();
1041 }
1042 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001043 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001044 busy = prune(id, ULONG_MAX, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001045 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001046 if (!busy || !--retry) {
1047 break;
1048 }
Mark Salyzyn501c3732017-03-10 14:31:54 -08001049 sleep(1); // Let reader(s) catch up after notification
Mark Salyzync5dc9702015-09-16 15:34:00 -07001050 }
1051 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001052}
1053
1054// get the used space associated with "id".
1055unsigned long LogBuffer::getSizeUsed(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001056 rdlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001057 size_t retval = stats.sizes(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001058 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001059 return retval;
1060}
1061
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001062// set the total space allocated to "id"
1063int LogBuffer::setSize(log_id_t id, unsigned long size) {
1064 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -07001065 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001066 return -1;
1067 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001068 wrlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001069 log_buffer_size(id) = size;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001070 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001071 return 0;
1072}
1073
1074// get the total space allocated to "id"
1075unsigned long LogBuffer::getSize(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001076 rdlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001077 size_t retval = log_buffer_size(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001078 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001079 return retval;
1080}
1081
Mark Salyzynae2abf12017-03-31 10:48:39 -07001082log_time LogBuffer::flushTo(SocketClient* reader, const log_time& start,
1083 pid_t* lastTid, bool privileged, bool security,
1084 int (*filter)(const LogBufferElement* element,
1085 void* arg),
1086 void* arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001087 LogBufferElementCollection::iterator it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001088 uid_t uid = reader->getUid();
1089
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001090 rdlock();
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001091
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -08001092 if (start == log_time::EPOCH) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001093 // client wants to start from the beginning
1094 it = mLogElements.begin();
1095 } else {
Mark Salyzyn0f92fdc2017-04-04 10:57:41 -07001096 // 3 second limit to continue search for out-of-order entries.
Mark Salyzyn58363792017-04-17 12:46:12 -07001097 log_time min = start - pruneMargin;
1098
Mark Salyzyn0f92fdc2017-04-04 10:57:41 -07001099 // Cap to 300 iterations we look back for out-of-order entries.
1100 size_t count = 300;
Mark Salyzyn58363792017-04-17 12:46:12 -07001101
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001102 // Client wants to start from some specified time. Chances are
1103 // we are better off starting from the end of the time sorted list.
Mark Salyzyn58363792017-04-17 12:46:12 -07001104 LogBufferElementCollection::iterator last;
Mark Salyzyn1f467162017-03-27 13:12:41 -07001105 for (last = it = mLogElements.end(); it != mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -08001106 /* do nothing */) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001107 --it;
Mark Salyzyn501c3732017-03-10 14:31:54 -08001108 LogBufferElement* element = *it;
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001109 if (element->getRealTime() > start) {
1110 last = it;
Mark Salyzyn0f92fdc2017-04-04 10:57:41 -07001111 } else if (!--count || (element->getRealTime() < min)) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001112 break;
1113 }
1114 }
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001115 it = last;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001116 }
1117
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001118 log_time max = start;
Mark Salyzynb5b87962017-01-23 14:20:31 -08001119
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001120 LogBufferElement* lastElement = nullptr; // iterator corruption paranoia
1121 static const size_t maxSkip = 4194304; // maximum entries to skip
1122 size_t skip = maxSkip;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001123 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001124 LogBufferElement* element = *it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001125
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001126 if (!--skip) {
1127 android::prdebug("reader.per: too many elements skipped");
1128 break;
1129 }
1130 if (element == lastElement) {
1131 android::prdebug("reader.per: identical elements");
1132 break;
1133 }
1134 lastElement = element;
1135
Mark Salyzyn0175b072014-02-26 09:50:16 -08001136 if (!privileged && (element->getUid() != uid)) {
1137 continue;
1138 }
1139
Mark Salyzyn8fa88962016-01-26 14:32:35 -08001140 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1141 continue;
1142 }
1143
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -08001144 if (element->getRealTime() <= start) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001145 continue;
1146 }
1147
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001148 // NB: calling out to another object with wrlock() held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001149 if (filter) {
1150 int ret = (*filter)(element, arg);
1151 if (ret == false) {
1152 continue;
1153 }
1154 if (ret != true) {
1155 break;
1156 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001157 }
1158
Mark Salyzynae2abf12017-03-31 10:48:39 -07001159 bool sameTid = false;
1160 if (lastTid) {
1161 sameTid = lastTid[element->getLogId()] == element->getTid();
1162 // Dropped (chatty) immediately following a valid log from the
1163 // same source in the same log buffer indicates we have a
1164 // multiple identical squash. chatty that differs source
1165 // is due to spam filter. chatty to chatty of different
1166 // source is also due to spam filter.
1167 lastTid[element->getLogId()] =
1168 (element->getDropped() && !sameTid) ? 0 : element->getTid();
1169 }
Mark Salyzynb5b87962017-01-23 14:20:31 -08001170
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001171 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001172
1173 // range locking in LastLogTimes looks after us
Mark Salyzynb5b87962017-01-23 14:20:31 -08001174 max = element->flushTo(reader, this, privileged, sameTid);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001175
1176 if (max == element->FLUSH_ERROR) {
1177 return max;
1178 }
1179
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001180 skip = maxSkip;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001181 rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001182 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001183 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001184
1185 return max;
1186}
Mark Salyzyn34facab2014-02-06 14:48:50 -08001187
Mark Salyzynee3b8382015-12-17 09:58:43 -08001188std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1189 unsigned int logMask) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001190 wrlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001191
Mark Salyzynee3b8382015-12-17 09:58:43 -08001192 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001193
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001194 unlock();
Mark Salyzyn73160ac2015-08-20 10:01:44 -07001195
1196 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001197}