blob: 3552f7073a6708dcb6d2e6544757ee696edca365 [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 Salyzyn202e1532015-02-09 08:21:05 -080020#include <errno.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080021#include <stdio.h>
22#include <string.h>
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070023#include <sys/cdefs.h>
Mark Salyzyn57a0af92014-05-09 17:44:18 -070024#include <sys/user.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080025#include <time.h>
26#include <unistd.h>
27
Mark Salyzyn511338d2015-05-19 09:12:30 -070028#include <unordered_map>
29
Mark Salyzyn671e3432014-05-06 07:34:59 -070030#include <cutils/properties.h>
Mark Salyzynf10e2732016-09-27 13:08:23 -070031#include <private/android_logger.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080032
33#include "LogBuffer.h"
Mark Salyzynb6bee332015-09-08 08:56:32 -070034#include "LogKlog.h"
Mark Salyzyn671e3432014-05-06 07:34:59 -070035#include "LogReader.h"
Mark Salyzyna2c02222016-12-13 10:31:29 -080036#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080037
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070038#ifndef __predict_false
39#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
40#endif
41
Mark Salyzyndfa7a072014-02-11 12:29:31 -080042// Default
Mark Salyzyndfa7a072014-02-11 12:29:31 -080043#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn671e3432014-05-06 07:34:59 -070044
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070045void LogBuffer::init() {
Mark Salyzyndfa7a072014-02-11 12:29:31 -080046 log_id_for_each(i) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -080047 mLastSet[i] = false;
48 mLast[i] = mLogElements.begin();
49
Mark Salyzynf10e2732016-09-27 13:08:23 -070050 if (setSize(i, __android_logger_get_buffer_size(i))) {
Mark Salyzyn57a0af92014-05-09 17:44:18 -070051 setSize(i, LOG_BUFFER_MIN_SIZE);
52 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -080053 }
Mark Salyzynb6bee332015-09-08 08:56:32 -070054 bool lastMonotonic = monotonic;
Mark Salyzynba7a9a02015-12-01 15:57:25 -080055 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzynb75cce02015-11-30 11:35:56 -080056 if (lastMonotonic != monotonic) {
57 //
58 // Fixup all timestamps, may not be 100% accurate, but better than
59 // throwing what we have away when we get 'surprised' by a change.
60 // In-place element fixup so no need to check reader-lock. Entries
61 // should already be in timestamp order, but we could end up with a
62 // few out-of-order entries if new monotonics come in before we
63 // are notified of the reinit change in status. A Typical example would
64 // be:
65 // --------- beginning of system
66 // 10.494082 184 201 D Cryptfs : Just triggered post_fs_data
67 // --------- beginning of kernel
68 // 0.000000 0 0 I : Initializing cgroup subsys
69 // as the act of mounting /data would trigger persist.logd.timestamp to
70 // be corrected. 1/30 corner case YMMV.
71 //
72 pthread_mutex_lock(&mLogElementsLock);
73 LogBufferElementCollection::iterator it = mLogElements.begin();
74 while((it != mLogElements.end())) {
75 LogBufferElement *e = *it;
76 if (monotonic) {
77 if (!android::isMonotonic(e->mRealTime)) {
78 LogKlog::convertRealToMonotonic(e->mRealTime);
79 }
80 } else {
81 if (android::isMonotonic(e->mRealTime)) {
82 LogKlog::convertMonotonicToReal(e->mRealTime);
83 }
84 }
85 ++it;
86 }
87 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzynb6bee332015-09-08 08:56:32 -070088 }
89
Mark Salyzynb75cce02015-11-30 11:35:56 -080090 // We may have been triggered by a SIGHUP. Release any sleeping reader
91 // threads to dump their current content.
Mark Salyzynb6bee332015-09-08 08:56:32 -070092 //
Mark Salyzynb75cce02015-11-30 11:35:56 -080093 // NB: this is _not_ performed in the context of a SIGHUP, it is
94 // performed during startup, and in context of reinit administrative thread
95 LogTimeEntry::lock();
96
97 LastLogTimes::iterator times = mTimes.begin();
98 while(times != mTimes.end()) {
99 LogTimeEntry *entry = (*times);
100 if (entry->owned_Locked()) {
101 entry->triggerReader_Locked();
Mark Salyzynb6bee332015-09-08 08:56:32 -0700102 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800103 times++;
Mark Salyzynb6bee332015-09-08 08:56:32 -0700104 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800105
106 LogTimeEntry::unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800107}
108
Mark Salyzynb6bee332015-09-08 08:56:32 -0700109LogBuffer::LogBuffer(LastLogTimes *times):
Mark Salyzynba7a9a02015-12-01 15:57:25 -0800110 monotonic(android_log_clockid() == CLOCK_MONOTONIC),
Mark Salyzynb6bee332015-09-08 08:56:32 -0700111 mTimes(*times) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700112 pthread_mutex_init(&mLogElementsLock, NULL);
113
Mark Salyzyna2c02222016-12-13 10:31:29 -0800114 log_id_for_each(i) {
115 lastLoggedElements[i] = NULL;
116 droppedElements[i] = NULL;
117 }
118
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700119 init();
120}
121
Mark Salyzyna2c02222016-12-13 10:31:29 -0800122LogBuffer::~LogBuffer() {
123 log_id_for_each(i) {
124 delete lastLoggedElements[i];
125 delete droppedElements[i];
126 }
127}
128
129static bool identical(LogBufferElement* elem, LogBufferElement* last) {
130 // is it mostly identical?
131// if (!elem) return false;
132 unsigned short lenl = elem->getMsgLen();
133 if (!lenl) return false;
134// if (!last) return false;
135 unsigned short lenr = last->getMsgLen();
136 if (!lenr) return false;
137// if (elem->getLogId() != last->getLogId()) return false;
138 if (elem->getUid() != last->getUid()) return false;
139 if (elem->getPid() != last->getPid()) return false;
140 if (elem->getTid() != last->getTid()) return false;
141
142 // last is more than a minute old, stop squashing identical messages
143 if (elem->getRealTime().nsec() >
144 (last->getRealTime().nsec() + 60 * NS_PER_SEC)) return false;
145
146 // Identical message
147 const char* msgl = elem->getMsg();
148 const char* msgr = last->getMsg();
149 if ((lenl == lenr) && !fastcmp<memcmp>(msgl, msgr, lenl)) return true;
150
151 // audit message (except sequence number) identical?
152 static const char avc[] = "): avc: ";
153
154 if (last->isBinary()) {
155 if (fastcmp<memcmp>(msgl, msgr,
156 sizeof(android_log_event_string_t) -
157 sizeof(int32_t))) return false;
158 msgl += sizeof(android_log_event_string_t);
159 lenl -= sizeof(android_log_event_string_t);
160 msgr += sizeof(android_log_event_string_t);
161 lenr -= sizeof(android_log_event_string_t);
162 }
163 const char *avcl = android::strnstr(msgl, lenl, avc);
164 if (!avcl) return false;
165 lenl -= avcl - msgl;
166 const char *avcr = android::strnstr(msgr, lenr, avc);
167 if (!avcr) return false;
168 lenr -= avcr - msgr;
169 if (lenl != lenr) return false;
170 return !fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc), lenl);
171}
172
Mark Salyzyn202e1532015-02-09 08:21:05 -0800173int LogBuffer::log(log_id_t log_id, log_time realtime,
174 uid_t uid, pid_t pid, pid_t tid,
175 const char *msg, unsigned short len) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800176 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800177 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800178 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700179
Mark Salyzyn0175b072014-02-26 09:50:16 -0800180 LogBufferElement *elem = new LogBufferElement(log_id, realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700181 uid, pid, tid, msg, len);
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000182 if (log_id != LOG_ID_SECURITY) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800183 int prio = ANDROID_LOG_INFO;
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000184 const char *tag = NULL;
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700185 size_t len = 0;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800186 if (log_id == LOG_ID_EVENTS) {
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700187 tag = android::tagToName(&len, elem->getTag());
Mark Salyzyn083b0372015-12-04 10:59:45 -0800188 } else {
189 prio = *msg;
190 tag = msg + 1;
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700191 len = strlen(tag);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800192 }
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700193 if (!__android_log_is_loggable_len(prio, tag, len, ANDROID_LOG_VERBOSE)) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800194 // Log traffic received to total
195 pthread_mutex_lock(&mLogElementsLock);
196 stats.add(elem);
197 stats.subtract(elem);
198 pthread_mutex_unlock(&mLogElementsLock);
199 delete elem;
200 return -EACCES;
201 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700202 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800203
204 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800205 LogBufferElement* currentLast = lastLoggedElements[log_id];
206 if (currentLast) {
207 LogBufferElement *dropped = droppedElements[log_id];
208 unsigned short count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800209 //
210 // State Init
211 // incoming:
212 // dropped = NULL
213 // currentLast = NULL;
214 // elem = incoming message
215 // outgoing:
216 // dropped = NULL -> State 0
217 // currentLast = copy of elem
218 // log elem
219 // State 0
220 // incoming:
221 // count = 0
222 // dropped = NULL
223 // currentLast = copy of last message
224 // elem = incoming message
225 // outgoing: (if *elem == *currentLast)
226 // dropped = copy of first identical message -> State 1
227 // currentLast = reference to elem
228 // break: (if *elem != *currentLast)
229 // dropped = NULL -> State 0
230 // delete copy of last message (incoming currentLast)
231 // currentLast = copy of elem
232 // log elem
233 // State 1
234 // incoming:
235 // count = 0
236 // dropped = copy of first identical message
237 // currentLast = reference to last held-back incoming
238 // message
239 // elem = incoming message
240 // outgoing: (if *elem == *currentLast)
241 // delete copy of first identical message (dropped)
242 // dropped = reference to last held-back incoming
243 // message set to chatty count of 1 -> State 2
244 // currentLast = reference to elem
245 // break:
246 // delete dropped
247 // dropped = NULL -> State 0
248 // log reference to last held-back (currentLast)
249 // currentLast = copy of elem
250 // log elem
251 // State 2
252 // incoming:
253 // count = chatty count
254 // dropped = chatty message holding count
255 // currentLast = reference to last held-back incoming
256 // message.
257 // dropped = chatty message holding count
258 // elem = incoming message
259 // outgoing: (if *elem == *currentLast)
260 // delete chatty message holding count
261 // dropped = reference to last held-back incoming
262 // message, set to chatty count + 1
263 // currentLast = reference to elem
264 // break:
265 // log dropped (chatty message)
266 // dropped = NULL -> State 0
267 // log reference to last held-back (currentLast)
268 // currentLast = copy of elem
269 // log elem
270 //
Mark Salyzyna2c02222016-12-13 10:31:29 -0800271 if (identical(elem, currentLast)) {
272 if (dropped) {
273 if (count == USHRT_MAX) {
274 log(dropped);
275 count = 1;
276 } else {
277 delete dropped;
278 ++count;
279 }
280 }
281 if (count) {
282 stats.add(currentLast);
283 stats.subtract(currentLast);
284 currentLast->setDropped(count);
285 }
286 droppedElements[log_id] = currentLast;
287 lastLoggedElements[log_id] = elem;
288 pthread_mutex_unlock(&mLogElementsLock);
289 return len;
290 }
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800291 if (dropped) { // State 1 or 2
292 if (count) { // State 2
293 log(dropped); // report chatty
294 } else { // State 1
295 delete dropped;
296 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800297 droppedElements[log_id] = NULL;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800298 log(currentLast); // report last message in the series
299 } else { // State 0
Mark Salyzyna2c02222016-12-13 10:31:29 -0800300 delete currentLast;
301 }
302 }
303 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800304
Mark Salyzyna2c02222016-12-13 10:31:29 -0800305 log(elem);
306 pthread_mutex_unlock(&mLogElementsLock);
307
308 return len;
309}
310
311// assumes mLogElementsLock held, owns elem, will look after garbage collection
312void LogBuffer::log(LogBufferElement* elem) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800313 // Insert elements in time sorted order if possible
314 // NB: if end is region locked, place element at end of list
315 LogBufferElementCollection::iterator it = mLogElements.end();
316 LogBufferElementCollection::iterator last = it;
Mark Salyzyneae155e2014-10-13 16:49:47 -0700317 while (last != mLogElements.begin()) {
318 --it;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800319 if ((*it)->getRealTime() <= elem->getRealTime()) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800320 break;
321 }
322 last = it;
323 }
Mark Salyzync03e72c2014-02-18 11:23:53 -0800324
Mark Salyzyn0175b072014-02-26 09:50:16 -0800325 if (last == mLogElements.end()) {
326 mLogElements.push_back(elem);
327 } else {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800328 uint64_t end = 1;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800329 bool end_set = false;
330 bool end_always = false;
331
332 LogTimeEntry::lock();
333
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700334 LastLogTimes::iterator times = mTimes.begin();
335 while(times != mTimes.end()) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800336 LogTimeEntry* entry = (*times);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800337 if (entry->owned_Locked()) {
338 if (!entry->mNonBlock) {
339 end_always = true;
340 break;
341 }
342 if (!end_set || (end <= entry->mEnd)) {
343 end = entry->mEnd;
344 end_set = true;
345 }
346 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700347 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800348 }
349
350 if (end_always
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800351 || (end_set && (end >= (*last)->getSequence()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800352 mLogElements.push_back(elem);
353 } else {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800354 mLogElements.insert(last, elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800355 }
356
357 LogTimeEntry::unlock();
358 }
359
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700360 stats.add(elem);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800361 maybePrune(elem->getLogId());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800362}
363
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700364// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800365//
366// mLogElementsLock must be held when this function is called.
367void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800368 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700369 unsigned long maxSize = log_buffer_size(id);
370 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700371 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700372 size_t elements = stats.realElements(id);
373 size_t minElements = elements / 100;
374 if (minElements < minPrune) {
375 minElements = minPrune;
376 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700377 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700378 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700379 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800380 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700381 if (pruneRows > maxPrune) {
382 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700383 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800384 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800385 }
386}
387
Mark Salyzyn831aa292015-09-03 16:08:50 -0700388LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700389 LogBufferElementCollection::iterator it, bool coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700390 LogBufferElement *element = *it;
391 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700392
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700393 // Remove iterator references in the various lists that will become stale
394 // after the element is erased from the main logging list.
395
Mark Salyzyn6a066942016-07-14 15:34:30 -0700396 { // start of scope for found iterator
397 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
398 element->getTag() : element->getUid();
399 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
400 if ((found != mLastWorst[id].end()) && (it == found->second)) {
401 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700402 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700403 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700404
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700405 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700406 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700407 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
408 // long term code stability, find() check should be fast for those ids.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700409 LogBufferPidIteratorMap::iterator found =
410 mLastWorstPidOfSystem[id].find(element->getPid());
411 if ((found != mLastWorstPidOfSystem[id].end())
412 && (it == found->second)) {
413 mLastWorstPidOfSystem[id].erase(found);
414 }
415 }
416
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800417 bool setLast[LOG_ID_MAX];
418 bool doSetLast = false;
419 log_id_for_each(i) {
420 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
421 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700422#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
423 LogBufferElementCollection::iterator bad = it;
424 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
425 element->getTag() : element->getUid();
426#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700427 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800428 if (doSetLast) {
429 log_id_for_each(i) {
430 if (setLast[i]) {
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700431 if (__predict_false(it == mLogElements.end())) { // impossible
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800432 mLastSet[i] = false;
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700433 mLast[i] = mLogElements.begin();
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800434 } else {
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700435 mLast[i] = it; // push down the road as next-best-watermark
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800436 }
437 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800438 }
439 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700440#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
441 log_id_for_each(i) {
442 for(auto b : mLastWorst[i]) {
443 if (bad == b.second) {
444 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n",
445 i, b.first, key);
446 }
447 }
448 for(auto b : mLastWorstPidOfSystem[i]) {
449 if (bad == b.second) {
450 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n",
451 i, b.first);
452 }
453 }
454 if (mLastSet[i] && (bad == mLast[i])) {
455 android::prdebug("stale mLast[%d]\n", i);
456 mLastSet[i] = false;
457 mLast[i] = mLogElements.begin();
458 }
459 }
460#endif
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700461 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700462 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700463 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700464 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700465 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700466 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700467
468 return it;
469}
470
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700471// Define a temporary mechanism to report the last LogBufferElement pointer
472// for the specified uid, pid and tid. Used below to help merge-sort when
473// pruning for worst UID.
474class LogBufferElementKey {
475 const union {
476 struct {
477 uint16_t uid;
478 uint16_t pid;
479 uint16_t tid;
480 uint16_t padding;
481 } __packed;
482 uint64_t value;
483 } __packed;
484
485public:
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700486 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid):
487 uid(uid),
488 pid(pid),
489 tid(tid),
490 padding(0) {
491 }
Chih-Hung Hsieh1cc82ce2016-04-25 13:49:46 -0700492 explicit LogBufferElementKey(uint64_t key):value(key) { }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700493
494 uint64_t getKey() { return value; }
495};
496
Mark Salyzyn511338d2015-05-19 09:12:30 -0700497class LogBufferElementLast {
498
499 typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap;
500 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700501
502public:
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700503
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700504 bool coalesce(LogBufferElement *element, unsigned short dropped) {
505 LogBufferElementKey key(element->getUid(),
506 element->getPid(),
507 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700508 LogBufferElementMap::iterator it = map.find(key.getKey());
509 if (it != map.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700510 LogBufferElement *found = it->second;
511 unsigned short moreDropped = found->getDropped();
512 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700513 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700514 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700515 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700516 return true;
517 }
518 }
519 return false;
520 }
521
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700522 void add(LogBufferElement *element) {
523 LogBufferElementKey key(element->getUid(),
524 element->getPid(),
525 element->getTid());
526 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700527 }
528
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700529 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700530 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700531 }
532
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700533 void clear(LogBufferElement *element) {
534 uint64_t current = element->getRealTime().nsec()
Mark Salyzyn047cc072015-06-04 13:35:30 -0700535 - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn511338d2015-05-19 09:12:30 -0700536 for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700537 LogBufferElement *mapElement = it->second;
538 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD)
539 && (current > mapElement->getRealTime().nsec())) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700540 it = map.erase(it);
541 } else {
542 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700543 }
544 }
545 }
546
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700547};
548
Mark Salyzyn0175b072014-02-26 09:50:16 -0800549// prune "pruneRows" of type "id" from the buffer.
550//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700551// This garbage collection task is used to expire log entries. It is called to
552// remove all logs (clear), all UID logs (unprivileged clear), or every
553// 256 or 10% of the total logs (whichever is less) to prune the logs.
554//
555// First there is a prep phase where we discover the reader region lock that
556// acts as a backstop to any pruning activity to stop there and go no further.
557//
558// There are three major pruning loops that follow. All expire from the oldest
559// entries. Since there are multiple log buffers, the Android logging facility
560// will appear to drop entries 'in the middle' when looking at multiple log
561// sources and buffers. This effect is slightly more prominent when we prune
562// the worst offender by logging source. Thus the logs slowly loose content
563// and value as you move back in time. This is preferred since chatty sources
564// invariably move the logs value down faster as less chatty sources would be
565// expired in the noise.
566//
567// The first loop performs blacklisting and worst offender pruning. Falling
568// through when there are no notable worst offenders and have not hit the
569// region lock preventing further worst offender pruning. This loop also looks
570// after managing the chatty log entries and merging to help provide
571// statistical basis for blame. The chatty entries are not a notification of
572// how much logs you may have, but instead represent how much logs you would
573// have had in a virtual log buffer that is extended to cover all the in-memory
574// logs without loss. They last much longer than the represented pruned logs
575// since they get multiplied by the gains in the non-chatty log sources.
576//
577// The second loop get complicated because an algorithm of watermarks and
578// history is maintained to reduce the order and keep processing time
579// down to a minimum at scale. These algorithms can be costly in the face
580// of larger log buffers, or severly limited processing time granted to a
581// background task at lowest priority.
582//
583// This second loop does straight-up expiration from the end of the logs
584// (again, remember for the specified log buffer id) but does some whitelist
585// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
586// spam filtration all take priority. This second loop also checks if a region
587// lock is causing us to buffer too much in the logs to help the reader(s),
588// and will tell the slowest reader thread to skip log entries, and if
589// persistent and hits a further threshold, kill the reader thread.
590//
591// The third thread is optional, and only gets hit if there was a whitelist
592// and more needs to be pruned against the backstop of the region lock.
593//
Mark Salyzyn0175b072014-02-26 09:50:16 -0800594// mLogElementsLock must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700595//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700596bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800597 LogTimeEntry *oldest = NULL;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700598 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700599 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800600
601 LogTimeEntry::lock();
602
603 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700604 LastLogTimes::iterator times = mTimes.begin();
605 while(times != mTimes.end()) {
606 LogTimeEntry *entry = (*times);
TraianX Schiauda6495d2014-12-17 10:53:41 +0200607 if (entry->owned_Locked() && entry->isWatching(id)
Mark Salyzynb75cce02015-11-30 11:35:56 -0800608 && (!oldest ||
609 (oldest->mStart > entry->mStart) ||
610 ((oldest->mStart == entry->mStart) &&
611 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800612 oldest = entry;
613 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700614 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800615 }
616
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800617 LogBufferElementCollection::iterator it;
618
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700619 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700620 // Only here if clear all request from non system source, so chatty
621 // filter logistics is not required.
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800622 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
623 while (it != mLogElements.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700624 LogBufferElement *element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700625
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700626 if ((element->getLogId() != id) || (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700627 ++it;
628 continue;
629 }
630
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800631 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
632 mLast[id] = it;
633 mLastSet[id] = true;
634 }
635
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700636 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700637 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800638 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
639 oldest->triggerReader_Locked();
640 } else {
641 oldest->triggerSkip_Locked(id, pruneRows);
642 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700643 break;
644 }
645
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700646 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700647 if (--pruneRows == 0) {
648 break;
649 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700650 }
651 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700652 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700653 }
654
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700655 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn083b0372015-12-04 10:59:45 -0800656 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700657 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800658 // recalculate the worst offender on every batched pass
Mark Salyzyn6a066942016-07-14 15:34:30 -0700659 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800660 size_t worst_sizes = 0;
661 size_t second_worst_sizes = 0;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700662 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800663
Mark Salyzynae769232015-03-17 17:17:25 -0700664 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700665 // Calculate threshold as 12.5% of available storage
666 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700667
Mark Salyzyn6a066942016-07-14 15:34:30 -0700668 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
669 stats.sortTags(AID_ROOT, (pid_t)0, 2, id).findWorst(
670 worst, worst_sizes, second_worst_sizes, threshold);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700671 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700672 } else {
673 stats.sort(AID_ROOT, (pid_t)0, 2, id).findWorst(
674 worst, worst_sizes, second_worst_sizes, threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700675
Mark Salyzyn6a066942016-07-14 15:34:30 -0700676 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
677 stats.sortPids(worst, (pid_t)0, 2, id).findWorst(
678 worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700679 }
680 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800681 }
682
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700683 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700684 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700685 break;
686 }
687
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800688 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700689 bool leading = true;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800690 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700691 // Perform at least one mandatory garbage collection cycle in following
692 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700693 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700694 // - check age-out of preserved logs
695 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700696 if (!gc && (worst != -1)) {
697 { // begin scope for worst found iterator
698 LogBufferIteratorMap::iterator found = mLastWorst[id].find(worst);
699 if ((found != mLastWorst[id].end())
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700700 && (found->second != mLogElements.end())) {
701 leading = false;
702 it = found->second;
703 }
704 }
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700705 if (worstPid) { // begin scope for pid worst found iterator
706 // FYI: worstPid only set if !LOG_ID_EVENTS and
707 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700708 LogBufferPidIteratorMap::iterator found
709 = mLastWorstPidOfSystem[id].find(worstPid);
710 if ((found != mLastWorstPidOfSystem[id].end())
711 && (found->second != mLogElements.end())) {
712 leading = false;
713 it = found->second;
714 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700715 }
716 }
Mark Salyzynccfe8442015-08-24 13:43:27 -0700717 static const timespec too_old = {
718 EXPIRE_HOUR_THRESHOLD * 60 * 60, 0
719 };
720 LogBufferElementCollection::iterator lastt;
721 lastt = mLogElements.end();
722 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700723 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700724 while (it != mLogElements.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700725 LogBufferElement *element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800726
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700727 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700728 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800729 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
730 oldest->triggerReader_Locked();
731 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800732 break;
733 }
734
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700735 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800736 ++it;
737 continue;
738 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700739 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800740
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800741 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
742 mLast[id] = it;
743 mLastSet[id] = true;
744 }
745
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700746 unsigned short dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800747
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700748 // remove any leading drops
749 if (leading && dropped) {
750 it = erase(it);
751 continue;
752 }
753
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700754 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700755 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700756 continue;
757 }
758
Mark Salyzyn6a066942016-07-14 15:34:30 -0700759 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
760 element->getTag() :
761 element->getUid();
762
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700763 if (hasBlacklist && mPrune.naughty(element)) {
764 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700765 it = erase(it);
766 if (dropped) {
767 continue;
768 }
769
770 pruneRows--;
771 if (pruneRows == 0) {
772 break;
773 }
774
Mark Salyzyn6a066942016-07-14 15:34:30 -0700775 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700776 kick = true;
777 if (worst_sizes < second_worst_sizes) {
778 break;
779 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700780 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700781 }
782 continue;
783 }
784
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700785 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old))
786 || (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700787 break;
788 }
789
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700790 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700791 last.add(element);
792 if (worstPid
793 && ((!gc && (element->getPid() == worstPid))
Mark Salyzyn6a066942016-07-14 15:34:30 -0700794 || (mLastWorstPidOfSystem[id].find(element->getPid())
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700795 == mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700796 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700797 // watermark if current one empty. id is not LOG_ID_EVENTS
798 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700799 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700800 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700801 if ((!gc && !worstPid && (key == worst))
802 || (mLastWorst[id].find(key) == mLastWorst[id].end())) {
803 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700804 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800805 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700806 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800807 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700808
Mark Salyzyn6a066942016-07-14 15:34:30 -0700809 if ((key != worst)
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700810 || (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700811 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700812 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700813 ++it;
814 continue;
815 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700816 // key == worst below here
817 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700818
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700819 pruneRows--;
820 if (pruneRows == 0) {
821 break;
822 }
823
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700824 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700825
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700826 unsigned short len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700827
828 // do not create any leading drops
829 if (leading) {
830 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700831 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700832 stats.drop(element);
833 element->setDropped(1);
834 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700835 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700836 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700837 last.add(element);
838 if (worstPid && (!gc
839 || (mLastWorstPidOfSystem[id].find(worstPid)
840 == mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700841 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700842 // watermark if current one empty. id is not
843 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700844 mLastWorstPidOfSystem[id][worstPid] = it;
845 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700846 if ((!gc && !worstPid) ||
847 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
848 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700849 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700850 ++it;
851 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700852 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700853 if (worst_sizes < second_worst_sizes) {
854 break;
855 }
856 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800857 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700858 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800859
Mark Salyzyn1c950472014-04-01 17:19:47 -0700860 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800861 break; // the following loop will ask bad clients to skip/drop
862 }
863 }
864
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800865 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800866 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800867 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800868 while((pruneRows > 0) && (it != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700869 LogBufferElement *element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700870
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700871 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700872 it++;
873 continue;
874 }
875
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800876 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
877 mLast[id] = it;
878 mLastSet[id] = true;
879 }
880
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700881 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700882 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700883 if (whitelist) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800884 break;
885 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700886
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700887 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
888 // kick a misbehaving log reader client off the island
889 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800890 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
891 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700892 } else {
893 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800894 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700895 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800896 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700897
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700898 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
899 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700900 whitelist = true;
901 it++;
902 continue;
903 }
904
905 it = erase(it);
906 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800907 }
908
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700909 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800910 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800911 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800912 while((it != mLogElements.end()) && (pruneRows > 0)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700913 LogBufferElement *element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700914
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700915 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700916 ++it;
917 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800918 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700919
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800920 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
921 mLast[id] = it;
922 mLastSet[id] = true;
923 }
924
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700925 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700926 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700927 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
928 // kick a misbehaving log reader client off the island
929 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800930 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
931 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700932 } else {
933 oldest->triggerSkip_Locked(id, pruneRows);
934 }
935 break;
936 }
937
938 it = erase(it);
939 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800940 }
941 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800942
Mark Salyzyn0175b072014-02-26 09:50:16 -0800943 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700944
945 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800946}
947
948// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -0700949bool LogBuffer::clear(log_id_t id, uid_t uid) {
950 bool busy = true;
951 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
952 for (int retry = 4;;) {
953 if (retry == 1) { // last pass
954 // Check if it is still busy after the sleep, we say prune
955 // one entry, not another clear run, so we are looking for
956 // the quick side effect of the return value to tell us if
957 // we have a _blocked_ reader.
958 pthread_mutex_lock(&mLogElementsLock);
959 busy = prune(id, 1, uid);
960 pthread_mutex_unlock(&mLogElementsLock);
961 // It is still busy, blocked reader(s), lets kill them all!
962 // otherwise, lets be a good citizen and preserve the slow
963 // readers and let the clear run (below) deal with determining
964 // if we are still blocked and return an error code to caller.
965 if (busy) {
966 LogTimeEntry::lock();
967 LastLogTimes::iterator times = mTimes.begin();
968 while (times != mTimes.end()) {
969 LogTimeEntry *entry = (*times);
970 // Killer punch
971 if (entry->owned_Locked() && entry->isWatching(id)) {
972 entry->release_Locked();
973 }
974 times++;
975 }
976 LogTimeEntry::unlock();
977 }
978 }
979 pthread_mutex_lock(&mLogElementsLock);
980 busy = prune(id, ULONG_MAX, uid);
981 pthread_mutex_unlock(&mLogElementsLock);
982 if (!busy || !--retry) {
983 break;
984 }
985 sleep (1); // Let reader(s) catch up after notification
986 }
987 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800988}
989
990// get the used space associated with "id".
991unsigned long LogBuffer::getSizeUsed(log_id_t id) {
992 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800993 size_t retval = stats.sizes(id);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800994 pthread_mutex_unlock(&mLogElementsLock);
995 return retval;
996}
997
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800998// set the total space allocated to "id"
999int LogBuffer::setSize(log_id_t id, unsigned long size) {
1000 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -07001001 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001002 return -1;
1003 }
1004 pthread_mutex_lock(&mLogElementsLock);
1005 log_buffer_size(id) = size;
1006 pthread_mutex_unlock(&mLogElementsLock);
1007 return 0;
1008}
1009
1010// get the total space allocated to "id"
1011unsigned long LogBuffer::getSize(log_id_t id) {
1012 pthread_mutex_lock(&mLogElementsLock);
1013 size_t retval = log_buffer_size(id);
1014 pthread_mutex_unlock(&mLogElementsLock);
1015 return retval;
1016}
1017
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001018uint64_t LogBuffer::flushTo(
Mark Salyzyn8fa88962016-01-26 14:32:35 -08001019 SocketClient *reader, const uint64_t start,
1020 bool privileged, bool security,
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001021 int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001022 LogBufferElementCollection::iterator it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001023 uint64_t max = start;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001024 uid_t uid = reader->getUid();
1025
1026 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001027
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001028 if (start <= 1) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001029 // client wants to start from the beginning
1030 it = mLogElements.begin();
1031 } else {
1032 // Client wants to start from some specified time. Chances are
1033 // we are better off starting from the end of the time sorted list.
1034 for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
1035 --it;
1036 LogBufferElement *element = *it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001037 if (element->getSequence() <= start) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001038 it++;
1039 break;
1040 }
1041 }
1042 }
1043
1044 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001045 LogBufferElement *element = *it;
1046
1047 if (!privileged && (element->getUid() != uid)) {
1048 continue;
1049 }
1050
Mark Salyzyn8fa88962016-01-26 14:32:35 -08001051 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1052 continue;
1053 }
1054
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001055 if (element->getSequence() <= start) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001056 continue;
1057 }
1058
1059 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001060 if (filter) {
1061 int ret = (*filter)(element, arg);
1062 if (ret == false) {
1063 continue;
1064 }
1065 if (ret != true) {
1066 break;
1067 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001068 }
1069
1070 pthread_mutex_unlock(&mLogElementsLock);
1071
1072 // range locking in LastLogTimes looks after us
Mark Salyzyn7b873652015-12-03 15:38:35 -08001073 max = element->flushTo(reader, this, privileged);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001074
1075 if (max == element->FLUSH_ERROR) {
1076 return max;
1077 }
1078
1079 pthread_mutex_lock(&mLogElementsLock);
1080 }
1081 pthread_mutex_unlock(&mLogElementsLock);
1082
1083 return max;
1084}
Mark Salyzyn34facab2014-02-06 14:48:50 -08001085
Mark Salyzynee3b8382015-12-17 09:58:43 -08001086std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1087 unsigned int logMask) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001088 pthread_mutex_lock(&mLogElementsLock);
1089
Mark Salyzynee3b8382015-12-17 09:58:43 -08001090 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001091
1092 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn73160ac2015-08-20 10:01:44 -07001093
1094 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001095}