blob: cc65d4758bc03e838a1e82dd20ff07542a1347d4 [file] [log] [blame]
Mark Salyzyn12bac902014-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 Salyzyn21fc3932016-10-24 16:22:17 -070016// for manual checking of stale entries during LogBuffer::erase()
17//#define DEBUG_CHECK_FOR_STALE_ENTRIES
Mark Salyzyn12bac902014-02-26 09:50:16 -080018
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070019#include <ctype.h>
Mark Salyzynedb18aa2016-12-16 16:09:15 -080020#include <endian.h>
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -080021#include <errno.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080022#include <stdio.h>
23#include <string.h>
Mark Salyzyn2e86fa42016-10-24 08:20:26 -070024#include <sys/cdefs.h>
Mark Salyzyn7b85c592014-05-09 17:44:18 -070025#include <sys/user.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080026#include <time.h>
27#include <unistd.h>
28
Mark Salyzyn6a404192015-05-19 09:12:30 -070029#include <unordered_map>
30
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070031#include <cutils/properties.h>
Mark Salyzynf4693f32016-09-27 13:08:23 -070032#include <private/android_logger.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080033
34#include "LogBuffer.h"
Mark Salyzyn2b4a7632015-09-08 08:56:32 -070035#include "LogKlog.h"
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070036#include "LogReader.h"
Mark Salyzyn2456d042016-12-13 10:31:29 -080037#include "LogUtils.h"
Mark Salyzyn12bac902014-02-26 09:50:16 -080038
Mark Salyzyn2e86fa42016-10-24 08:20:26 -070039#ifndef __predict_false
40#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
41#endif
42
Mark Salyzync89839a2014-02-11 12:29:31 -080043// Default
Mark Salyzync89839a2014-02-11 12:29:31 -080044#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070045
Mark Salyzyn3fe25932015-03-10 16:45:17 -070046void LogBuffer::init() {
Mark Salyzync89839a2014-02-11 12:29:31 -080047 log_id_for_each(i) {
Mark Salyzyn7a3c2822016-01-11 10:58:09 -080048 mLastSet[i] = false;
49 mLast[i] = mLogElements.begin();
50
Mark Salyzynf4693f32016-09-27 13:08:23 -070051 if (setSize(i, __android_logger_get_buffer_size(i))) {
Mark Salyzyn7b85c592014-05-09 17:44:18 -070052 setSize(i, LOG_BUFFER_MIN_SIZE);
53 }
Mark Salyzync89839a2014-02-11 12:29:31 -080054 }
Mark Salyzyn2b4a7632015-09-08 08:56:32 -070055 bool lastMonotonic = monotonic;
Mark Salyzyn2d4f9bc2015-12-01 15:57:25 -080056 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzyn552b4752015-11-30 11:35:56 -080057 if (lastMonotonic != monotonic) {
58 //
59 // Fixup all timestamps, may not be 100% accurate, but better than
60 // throwing what we have away when we get 'surprised' by a change.
61 // In-place element fixup so no need to check reader-lock. Entries
62 // should already be in timestamp order, but we could end up with a
63 // few out-of-order entries if new monotonics come in before we
64 // are notified of the reinit change in status. A Typical example would
65 // be:
66 // --------- beginning of system
67 // 10.494082 184 201 D Cryptfs : Just triggered post_fs_data
68 // --------- beginning of kernel
69 // 0.000000 0 0 I : Initializing cgroup subsys
70 // as the act of mounting /data would trigger persist.logd.timestamp to
71 // be corrected. 1/30 corner case YMMV.
72 //
73 pthread_mutex_lock(&mLogElementsLock);
74 LogBufferElementCollection::iterator it = mLogElements.begin();
75 while((it != mLogElements.end())) {
76 LogBufferElement *e = *it;
77 if (monotonic) {
78 if (!android::isMonotonic(e->mRealTime)) {
79 LogKlog::convertRealToMonotonic(e->mRealTime);
80 }
81 } else {
82 if (android::isMonotonic(e->mRealTime)) {
83 LogKlog::convertMonotonicToReal(e->mRealTime);
84 }
85 }
86 ++it;
87 }
88 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn2b4a7632015-09-08 08:56:32 -070089 }
90
Mark Salyzyn552b4752015-11-30 11:35:56 -080091 // We may have been triggered by a SIGHUP. Release any sleeping reader
92 // threads to dump their current content.
Mark Salyzyn2b4a7632015-09-08 08:56:32 -070093 //
Mark Salyzyn552b4752015-11-30 11:35:56 -080094 // NB: this is _not_ performed in the context of a SIGHUP, it is
95 // performed during startup, and in context of reinit administrative thread
96 LogTimeEntry::lock();
97
98 LastLogTimes::iterator times = mTimes.begin();
99 while(times != mTimes.end()) {
100 LogTimeEntry *entry = (*times);
101 if (entry->owned_Locked()) {
102 entry->triggerReader_Locked();
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700103 }
Mark Salyzyn552b4752015-11-30 11:35:56 -0800104 times++;
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700105 }
Mark Salyzyn552b4752015-11-30 11:35:56 -0800106
107 LogTimeEntry::unlock();
Mark Salyzyn12bac902014-02-26 09:50:16 -0800108}
109
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700110LogBuffer::LogBuffer(LastLogTimes *times):
Mark Salyzyn2d4f9bc2015-12-01 15:57:25 -0800111 monotonic(android_log_clockid() == CLOCK_MONOTONIC),
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700112 mTimes(*times) {
Mark Salyzyn3fe25932015-03-10 16:45:17 -0700113 pthread_mutex_init(&mLogElementsLock, NULL);
114
Mark Salyzyn2456d042016-12-13 10:31:29 -0800115 log_id_for_each(i) {
116 lastLoggedElements[i] = NULL;
117 droppedElements[i] = NULL;
118 }
119
Mark Salyzyn3fe25932015-03-10 16:45:17 -0700120 init();
121}
122
Mark Salyzyn2456d042016-12-13 10:31:29 -0800123LogBuffer::~LogBuffer() {
124 log_id_for_each(i) {
125 delete lastLoggedElements[i];
126 delete droppedElements[i];
127 }
128}
129
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800130enum match_type {
131 DIFFERENT,
132 SAME,
133 SAME_LIBLOG
134};
135
136static enum match_type identical(LogBufferElement* elem, LogBufferElement* last) {
Mark Salyzyn2456d042016-12-13 10:31:29 -0800137 // is it mostly identical?
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800138// if (!elem) return DIFFERENT;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800139 unsigned short lenl = elem->getMsgLen();
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800140 if (!lenl) return DIFFERENT;
141// if (!last) return DIFFERENT;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800142 unsigned short lenr = last->getMsgLen();
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800143 if (!lenr) return DIFFERENT;
144// if (elem->getLogId() != last->getLogId()) return DIFFERENT;
145 if (elem->getUid() != last->getUid()) return DIFFERENT;
146 if (elem->getPid() != last->getPid()) return DIFFERENT;
147 if (elem->getTid() != last->getTid()) return DIFFERENT;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800148
149 // last is more than a minute old, stop squashing identical messages
150 if (elem->getRealTime().nsec() >
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800151 (last->getRealTime().nsec() + 60 * NS_PER_SEC)) return DIFFERENT;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800152
153 // Identical message
154 const char* msgl = elem->getMsg();
155 const char* msgr = last->getMsg();
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800156 if (lenl == lenr) {
157 if (!fastcmp<memcmp>(msgl, msgr, lenl)) return SAME;
158 // liblog tagged messages (content gets summed)
159 if ((elem->getLogId() == LOG_ID_EVENTS) &&
160 (lenl == sizeof(android_log_event_int_t)) &&
161 !fastcmp<memcmp>(msgl, msgr,
162 sizeof(android_log_event_int_t) - sizeof(int32_t)) &&
163 (elem->getTag() == LIBLOG_LOG_TAG)) return SAME_LIBLOG;
164 }
Mark Salyzyn2456d042016-12-13 10:31:29 -0800165
166 // audit message (except sequence number) identical?
167 static const char avc[] = "): avc: ";
168
169 if (last->isBinary()) {
170 if (fastcmp<memcmp>(msgl, msgr,
171 sizeof(android_log_event_string_t) -
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800172 sizeof(int32_t))) return DIFFERENT;
Mark Salyzyn2456d042016-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 }
178 const char *avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800179 if (!avcl) return DIFFERENT;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800180 lenl -= avcl - msgl;
181 const char *avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800182 if (!avcr) return DIFFERENT;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800183 lenr -= avcr - msgr;
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800184 if (lenl != lenr) return DIFFERENT;
185 if (fastcmp<memcmp>(avcl + strlen(avc),
186 avcr + strlen(avc), lenl)) return DIFFERENT;
187 return SAME;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800188}
189
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800190int LogBuffer::log(log_id_t log_id, log_time realtime,
191 uid_t uid, pid_t pid, pid_t tid,
192 const char *msg, unsigned short len) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800193 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800194 return -EINVAL;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800195 }
Mark Salyzyn19c91112014-10-02 13:07:05 -0700196
Mark Salyzyn12bac902014-02-26 09:50:16 -0800197 LogBufferElement *elem = new LogBufferElement(log_id, realtime,
Mark Salyzyn3661dd02014-03-20 16:09:38 -0700198 uid, pid, tid, msg, len);
Mark Salyzyn4a8d2502016-01-06 21:17:43 +0000199 if (log_id != LOG_ID_SECURITY) {
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800200 int prio = ANDROID_LOG_INFO;
Mark Salyzyn4a8d2502016-01-06 21:17:43 +0000201 const char *tag = NULL;
Mark Salyzyn0da1b472016-09-22 09:56:51 -0700202 size_t len = 0;
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800203 if (log_id == LOG_ID_EVENTS) {
Mark Salyzyn0da1b472016-09-22 09:56:51 -0700204 tag = android::tagToName(&len, elem->getTag());
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800205 } else {
206 prio = *msg;
207 tag = msg + 1;
Mark Salyzyn0da1b472016-09-22 09:56:51 -0700208 len = strlen(tag);
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800209 }
Mark Salyzyn0da1b472016-09-22 09:56:51 -0700210 if (!__android_log_is_loggable_len(prio, tag, len, ANDROID_LOG_VERBOSE)) {
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800211 // Log traffic received to total
212 pthread_mutex_lock(&mLogElementsLock);
213 stats.add(elem);
214 stats.subtract(elem);
215 pthread_mutex_unlock(&mLogElementsLock);
216 delete elem;
217 return -EACCES;
218 }
Mark Salyzyn19c91112014-10-02 13:07:05 -0700219 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800220
221 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn2456d042016-12-13 10:31:29 -0800222 LogBufferElement* currentLast = lastLoggedElements[log_id];
223 if (currentLast) {
224 LogBufferElement *dropped = droppedElements[log_id];
225 unsigned short count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800226 //
227 // State Init
228 // incoming:
229 // dropped = NULL
230 // currentLast = NULL;
231 // elem = incoming message
232 // outgoing:
233 // dropped = NULL -> State 0
234 // currentLast = copy of elem
235 // log elem
236 // State 0
237 // incoming:
238 // count = 0
239 // dropped = NULL
240 // currentLast = copy of last message
241 // elem = incoming message
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800242 // outgoing: if match != DIFFERENT
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800243 // dropped = copy of first identical message -> State 1
244 // currentLast = reference to elem
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800245 // break: if match == DIFFERENT
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800246 // dropped = NULL -> State 0
247 // delete copy of last message (incoming currentLast)
248 // currentLast = copy of elem
249 // log elem
250 // State 1
251 // incoming:
252 // count = 0
253 // dropped = copy of first identical message
254 // currentLast = reference to last held-back incoming
255 // message
256 // elem = incoming message
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800257 // outgoing: if match == SAME
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800258 // delete copy of first identical message (dropped)
259 // dropped = reference to last held-back incoming
260 // message set to chatty count of 1 -> State 2
261 // currentLast = reference to elem
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800262 // outgoing: if match == SAME_LIBLOG
263 // dropped = copy of first identical message -> State 1
264 // take sum of currentLast and elem
265 // if sum overflows:
266 // log currentLast
267 // currentLast = reference to elem
268 // else
269 // delete currentLast
270 // currentLast = reference to elem, sum liblog.
271 // break: if match == DIFFERENT
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800272 // delete dropped
273 // dropped = NULL -> State 0
274 // log reference to last held-back (currentLast)
275 // currentLast = copy of elem
276 // log elem
277 // State 2
278 // incoming:
279 // count = chatty count
280 // dropped = chatty message holding count
281 // currentLast = reference to last held-back incoming
282 // message.
283 // dropped = chatty message holding count
284 // elem = incoming message
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800285 // outgoing: if match != DIFFERENT
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800286 // delete chatty message holding count
287 // dropped = reference to last held-back incoming
288 // message, set to chatty count + 1
289 // currentLast = reference to elem
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800290 // break: if match == DIFFERENT
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800291 // log dropped (chatty message)
292 // dropped = NULL -> State 0
293 // log reference to last held-back (currentLast)
294 // currentLast = copy of elem
295 // log elem
296 //
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800297 enum match_type match = identical(elem, currentLast);
298 if (match != DIFFERENT) {
Mark Salyzyn2456d042016-12-13 10:31:29 -0800299 if (dropped) {
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800300 // Sum up liblog tag messages?
301 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
302 android_log_event_int_t* event =
303 reinterpret_cast<android_log_event_int_t*>(
304 const_cast<char*>(currentLast->getMsg()));
305 //
306 // To unit test, differentiate with something like:
307 // event->header.tag = htole32(CHATTY_LOG_TAG);
308 // here, then instead of delete currentLast below,
309 // log(currentLast) to see the incremental sums form.
310 //
311 uint32_t swab = event->payload.data;
312 unsigned long long total = htole32(swab);
313 event = reinterpret_cast<android_log_event_int_t*>(
314 const_cast<char*>(elem->getMsg()));
315 swab = event->payload.data;
316
317 lastLoggedElements[LOG_ID_EVENTS] = elem;
318 total += htole32(swab);
319 // check for overflow
320 if (total >= UINT32_MAX) {
321 log(currentLast);
322 pthread_mutex_unlock(&mLogElementsLock);
323 return len;
324 }
325 stats.add(currentLast);
326 stats.subtract(currentLast);
327 delete currentLast;
328 swab = total;
329 event->payload.data = htole32(swab);
330 pthread_mutex_unlock(&mLogElementsLock);
331 return len;
332 }
Mark Salyzyn2456d042016-12-13 10:31:29 -0800333 if (count == USHRT_MAX) {
334 log(dropped);
335 count = 1;
336 } else {
337 delete dropped;
338 ++count;
339 }
340 }
341 if (count) {
342 stats.add(currentLast);
343 stats.subtract(currentLast);
344 currentLast->setDropped(count);
345 }
346 droppedElements[log_id] = currentLast;
347 lastLoggedElements[log_id] = elem;
348 pthread_mutex_unlock(&mLogElementsLock);
349 return len;
350 }
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800351 if (dropped) { // State 1 or 2
352 if (count) { // State 2
353 log(dropped); // report chatty
354 } else { // State 1
355 delete dropped;
356 }
Mark Salyzyn2456d042016-12-13 10:31:29 -0800357 droppedElements[log_id] = NULL;
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800358 log(currentLast); // report last message in the series
359 } else { // State 0
Mark Salyzyn2456d042016-12-13 10:31:29 -0800360 delete currentLast;
361 }
362 }
363 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800364
Mark Salyzyn2456d042016-12-13 10:31:29 -0800365 log(elem);
366 pthread_mutex_unlock(&mLogElementsLock);
367
368 return len;
369}
370
371// assumes mLogElementsLock held, owns elem, will look after garbage collection
372void LogBuffer::log(LogBufferElement* elem) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800373 // Insert elements in time sorted order if possible
374 // NB: if end is region locked, place element at end of list
375 LogBufferElementCollection::iterator it = mLogElements.end();
376 LogBufferElementCollection::iterator last = it;
Mark Salyzyna222a772014-10-13 16:49:47 -0700377 while (last != mLogElements.begin()) {
378 --it;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800379 if ((*it)->getRealTime() <= elem->getRealTime()) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800380 break;
381 }
382 last = it;
383 }
Mark Salyzyn5b7a8d82014-02-18 11:23:53 -0800384
Mark Salyzyn12bac902014-02-26 09:50:16 -0800385 if (last == mLogElements.end()) {
386 mLogElements.push_back(elem);
387 } else {
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800388 uint64_t end = 1;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800389 bool end_set = false;
390 bool end_always = false;
391
392 LogTimeEntry::lock();
393
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700394 LastLogTimes::iterator times = mTimes.begin();
395 while(times != mTimes.end()) {
Mark Salyzyn2456d042016-12-13 10:31:29 -0800396 LogTimeEntry* entry = (*times);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800397 if (entry->owned_Locked()) {
398 if (!entry->mNonBlock) {
399 end_always = true;
400 break;
401 }
402 if (!end_set || (end <= entry->mEnd)) {
403 end = entry->mEnd;
404 end_set = true;
405 }
406 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700407 times++;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800408 }
409
410 if (end_always
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800411 || (end_set && (end >= (*last)->getSequence()))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800412 mLogElements.push_back(elem);
413 } else {
Mark Salyzyn2456d042016-12-13 10:31:29 -0800414 mLogElements.insert(last, elem);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800415 }
416
417 LogTimeEntry::unlock();
418 }
419
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700420 stats.add(elem);
Mark Salyzyn2456d042016-12-13 10:31:29 -0800421 maybePrune(elem->getLogId());
Mark Salyzyn12bac902014-02-26 09:50:16 -0800422}
423
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700424// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn12bac902014-02-26 09:50:16 -0800425//
426// mLogElementsLock must be held when this function is called.
427void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800428 size_t sizes = stats.sizes(id);
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700429 unsigned long maxSize = log_buffer_size(id);
430 if (sizes > maxSize) {
Mark Salyzyn63745722015-08-19 12:20:36 -0700431 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzynd745c722015-09-30 07:40:09 -0700432 size_t elements = stats.realElements(id);
433 size_t minElements = elements / 100;
434 if (minElements < minPrune) {
435 minElements = minPrune;
436 }
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700437 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700438 if (pruneRows < minElements) {
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700439 pruneRows = minElements;
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800440 }
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700441 if (pruneRows > maxPrune) {
442 pruneRows = maxPrune;
Mark Salyzyn63745722015-08-19 12:20:36 -0700443 }
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800444 prune(id, pruneRows);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800445 }
446}
447
Mark Salyzyn57b82a62015-09-03 16:08:50 -0700448LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700449 LogBufferElementCollection::iterator it, bool coalesce) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700450 LogBufferElement *element = *it;
451 log_id_t id = element->getLogId();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700452
Mark Salyzyn628d9372016-10-21 09:46:42 -0700453 // Remove iterator references in the various lists that will become stale
454 // after the element is erased from the main logging list.
455
Mark Salyzync572fc62016-07-14 15:34:30 -0700456 { // start of scope for found iterator
457 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
458 element->getTag() : element->getUid();
459 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
460 if ((found != mLastWorst[id].end()) && (it == found->second)) {
461 mLastWorst[id].erase(found);
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700462 }
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700463 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700464
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700465 { // start of scope for pid found iterator
Mark Salyzyn628d9372016-10-21 09:46:42 -0700466 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700467 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
468 // long term code stability, find() check should be fast for those ids.
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700469 LogBufferPidIteratorMap::iterator found =
470 mLastWorstPidOfSystem[id].find(element->getPid());
471 if ((found != mLastWorstPidOfSystem[id].end())
472 && (it == found->second)) {
473 mLastWorstPidOfSystem[id].erase(found);
474 }
475 }
476
Mark Salyzynd701b782016-01-19 16:04:41 -0800477 bool setLast[LOG_ID_MAX];
478 bool doSetLast = false;
479 log_id_for_each(i) {
480 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
481 }
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700482#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
483 LogBufferElementCollection::iterator bad = it;
484 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
485 element->getTag() : element->getUid();
486#endif
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700487 it = mLogElements.erase(it);
Mark Salyzynd701b782016-01-19 16:04:41 -0800488 if (doSetLast) {
489 log_id_for_each(i) {
490 if (setLast[i]) {
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700491 if (__predict_false(it == mLogElements.end())) { // impossible
Mark Salyzynd701b782016-01-19 16:04:41 -0800492 mLastSet[i] = false;
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700493 mLast[i] = mLogElements.begin();
Mark Salyzynd701b782016-01-19 16:04:41 -0800494 } else {
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700495 mLast[i] = it; // push down the road as next-best-watermark
Mark Salyzynd701b782016-01-19 16:04:41 -0800496 }
497 }
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800498 }
499 }
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700500#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
501 log_id_for_each(i) {
502 for(auto b : mLastWorst[i]) {
503 if (bad == b.second) {
504 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n",
505 i, b.first, key);
506 }
507 }
508 for(auto b : mLastWorstPidOfSystem[i]) {
509 if (bad == b.second) {
510 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n",
511 i, b.first);
512 }
513 }
514 if (mLastSet[i] && (bad == mLast[i])) {
515 android::prdebug("stale mLast[%d]\n", i);
516 mLastSet[i] = false;
517 mLast[i] = mLogElements.begin();
518 }
519 }
520#endif
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700521 if (coalesce) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700522 stats.erase(element);
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700523 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700524 stats.subtract(element);
Mark Salyzyn57b82a62015-09-03 16:08:50 -0700525 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700526 delete element;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700527
528 return it;
529}
530
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700531// Define a temporary mechanism to report the last LogBufferElement pointer
532// for the specified uid, pid and tid. Used below to help merge-sort when
533// pruning for worst UID.
534class LogBufferElementKey {
535 const union {
536 struct {
Mark Salyzync4595602016-12-13 12:44:20 -0800537 uint32_t uid;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700538 uint16_t pid;
539 uint16_t tid;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700540 } __packed;
541 uint64_t value;
542 } __packed;
543
544public:
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700545 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid):
546 uid(uid),
547 pid(pid),
Mark Salyzync4595602016-12-13 12:44:20 -0800548 tid(tid)
549 {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700550 }
Chih-Hung Hsieh697219b2016-04-25 13:49:46 -0700551 explicit LogBufferElementKey(uint64_t key):value(key) { }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700552
553 uint64_t getKey() { return value; }
554};
555
Mark Salyzyn6a404192015-05-19 09:12:30 -0700556class LogBufferElementLast {
557
558 typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap;
559 LogBufferElementMap map;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700560
561public:
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700562
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700563 bool coalesce(LogBufferElement *element, unsigned short dropped) {
564 LogBufferElementKey key(element->getUid(),
565 element->getPid(),
566 element->getTid());
Mark Salyzyn6a404192015-05-19 09:12:30 -0700567 LogBufferElementMap::iterator it = map.find(key.getKey());
568 if (it != map.end()) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700569 LogBufferElement *found = it->second;
570 unsigned short moreDropped = found->getDropped();
571 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700572 map.erase(it);
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700573 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700574 found->setDropped(dropped + moreDropped);
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700575 return true;
576 }
577 }
578 return false;
579 }
580
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700581 void add(LogBufferElement *element) {
582 LogBufferElementKey key(element->getUid(),
583 element->getPid(),
584 element->getTid());
585 map[key.getKey()] = element;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700586 }
587
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700588 inline void clear() {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700589 map.clear();
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700590 }
591
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700592 void clear(LogBufferElement *element) {
593 uint64_t current = element->getRealTime().nsec()
Mark Salyzyn80c1fc62015-06-04 13:35:30 -0700594 - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn6a404192015-05-19 09:12:30 -0700595 for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700596 LogBufferElement *mapElement = it->second;
597 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD)
598 && (current > mapElement->getRealTime().nsec())) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700599 it = map.erase(it);
600 } else {
601 ++it;
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700602 }
603 }
604 }
605
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700606};
607
Mark Salyzyn12bac902014-02-26 09:50:16 -0800608// prune "pruneRows" of type "id" from the buffer.
609//
Mark Salyzyn50e21082015-09-08 09:12:51 -0700610// This garbage collection task is used to expire log entries. It is called to
611// remove all logs (clear), all UID logs (unprivileged clear), or every
612// 256 or 10% of the total logs (whichever is less) to prune the logs.
613//
614// First there is a prep phase where we discover the reader region lock that
615// acts as a backstop to any pruning activity to stop there and go no further.
616//
617// There are three major pruning loops that follow. All expire from the oldest
618// entries. Since there are multiple log buffers, the Android logging facility
619// will appear to drop entries 'in the middle' when looking at multiple log
620// sources and buffers. This effect is slightly more prominent when we prune
621// the worst offender by logging source. Thus the logs slowly loose content
622// and value as you move back in time. This is preferred since chatty sources
623// invariably move the logs value down faster as less chatty sources would be
624// expired in the noise.
625//
626// The first loop performs blacklisting and worst offender pruning. Falling
627// through when there are no notable worst offenders and have not hit the
628// region lock preventing further worst offender pruning. This loop also looks
629// after managing the chatty log entries and merging to help provide
630// statistical basis for blame. The chatty entries are not a notification of
631// how much logs you may have, but instead represent how much logs you would
632// have had in a virtual log buffer that is extended to cover all the in-memory
633// logs without loss. They last much longer than the represented pruned logs
634// since they get multiplied by the gains in the non-chatty log sources.
635//
636// The second loop get complicated because an algorithm of watermarks and
637// history is maintained to reduce the order and keep processing time
638// down to a minimum at scale. These algorithms can be costly in the face
639// of larger log buffers, or severly limited processing time granted to a
640// background task at lowest priority.
641//
642// This second loop does straight-up expiration from the end of the logs
643// (again, remember for the specified log buffer id) but does some whitelist
644// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
645// spam filtration all take priority. This second loop also checks if a region
646// lock is causing us to buffer too much in the logs to help the reader(s),
647// and will tell the slowest reader thread to skip log entries, and if
648// persistent and hits a further threshold, kill the reader thread.
649//
650// The third thread is optional, and only gets hit if there was a whitelist
651// and more needs to be pruned against the backstop of the region lock.
652//
Mark Salyzyn12bac902014-02-26 09:50:16 -0800653// mLogElementsLock must be held when this function is called.
Mark Salyzyn50e21082015-09-08 09:12:51 -0700654//
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700655bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800656 LogTimeEntry *oldest = NULL;
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700657 bool busy = false;
Mark Salyzynd280d812015-09-16 15:34:00 -0700658 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800659
660 LogTimeEntry::lock();
661
662 // Region locked?
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700663 LastLogTimes::iterator times = mTimes.begin();
664 while(times != mTimes.end()) {
665 LogTimeEntry *entry = (*times);
TraianX Schiau6ba427e2014-12-17 10:53:41 +0200666 if (entry->owned_Locked() && entry->isWatching(id)
Mark Salyzyn552b4752015-11-30 11:35:56 -0800667 && (!oldest ||
668 (oldest->mStart > entry->mStart) ||
669 ((oldest->mStart == entry->mStart) &&
670 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800671 oldest = entry;
672 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700673 times++;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800674 }
675
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800676 LogBufferElementCollection::iterator it;
677
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700678 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn4e23c6e2016-09-01 15:48:36 -0700679 // Only here if clear all request from non system source, so chatty
680 // filter logistics is not required.
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800681 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
682 while (it != mLogElements.end()) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700683 LogBufferElement *element = *it;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700684
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700685 if ((element->getLogId() != id) || (element->getUid() != caller_uid)) {
Mark Salyzynd280d812015-09-16 15:34:00 -0700686 ++it;
687 continue;
688 }
689
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800690 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
691 mLast[id] = it;
692 mLastSet[id] = true;
693 }
694
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700695 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700696 busy = true;
Mark Salyzyn552b4752015-11-30 11:35:56 -0800697 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
698 oldest->triggerReader_Locked();
699 } else {
700 oldest->triggerSkip_Locked(id, pruneRows);
701 }
Mark Salyzyn276d5352014-06-12 11:16:16 -0700702 break;
703 }
704
Mark Salyzynd280d812015-09-16 15:34:00 -0700705 it = erase(it);
Mark Salyzyn4e23c6e2016-09-01 15:48:36 -0700706 if (--pruneRows == 0) {
707 break;
708 }
Mark Salyzyn276d5352014-06-12 11:16:16 -0700709 }
710 LogTimeEntry::unlock();
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700711 return busy;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700712 }
713
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700714 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800715 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzynd280d812015-09-16 15:34:00 -0700716 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800717 // recalculate the worst offender on every batched pass
Mark Salyzync572fc62016-07-14 15:34:30 -0700718 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800719 size_t worst_sizes = 0;
720 size_t second_worst_sizes = 0;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700721 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800722
Mark Salyzyn0e765c62015-03-17 17:17:25 -0700723 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzync572fc62016-07-14 15:34:30 -0700724 // Calculate threshold as 12.5% of available storage
725 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700726
Mark Salyzync572fc62016-07-14 15:34:30 -0700727 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
728 stats.sortTags(AID_ROOT, (pid_t)0, 2, id).findWorst(
729 worst, worst_sizes, second_worst_sizes, threshold);
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700730 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzync572fc62016-07-14 15:34:30 -0700731 } else {
732 stats.sort(AID_ROOT, (pid_t)0, 2, id).findWorst(
733 worst, worst_sizes, second_worst_sizes, threshold);
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700734
Mark Salyzync572fc62016-07-14 15:34:30 -0700735 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
736 stats.sortPids(worst, (pid_t)0, 2, id).findWorst(
737 worstPid, worst_sizes, second_worst_sizes);
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700738 }
739 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800740 }
741
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700742 // skip if we have neither worst nor naughty filters
Mark Salyzync572fc62016-07-14 15:34:30 -0700743 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700744 break;
745 }
746
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800747 bool kick = false;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700748 bool leading = true;
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800749 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn50e21082015-09-08 09:12:51 -0700750 // Perform at least one mandatory garbage collection cycle in following
751 // - clear leading chatty tags
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700752 // - coalesce chatty tags
Mark Salyzyn50e21082015-09-08 09:12:51 -0700753 // - check age-out of preserved logs
754 bool gc = pruneRows <= 1;
Mark Salyzync572fc62016-07-14 15:34:30 -0700755 if (!gc && (worst != -1)) {
756 { // begin scope for worst found iterator
757 LogBufferIteratorMap::iterator found = mLastWorst[id].find(worst);
758 if ((found != mLastWorst[id].end())
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700759 && (found->second != mLogElements.end())) {
760 leading = false;
761 it = found->second;
762 }
763 }
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700764 if (worstPid) { // begin scope for pid worst found iterator
765 // FYI: worstPid only set if !LOG_ID_EVENTS and
766 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700767 LogBufferPidIteratorMap::iterator found
768 = mLastWorstPidOfSystem[id].find(worstPid);
769 if ((found != mLastWorstPidOfSystem[id].end())
770 && (found->second != mLogElements.end())) {
771 leading = false;
772 it = found->second;
773 }
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700774 }
775 }
Mark Salyzyn4e29e172015-08-24 13:43:27 -0700776 static const timespec too_old = {
777 EXPIRE_HOUR_THRESHOLD * 60 * 60, 0
778 };
779 LogBufferElementCollection::iterator lastt;
780 lastt = mLogElements.end();
781 --lastt;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700782 LogBufferElementLast last;
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700783 while (it != mLogElements.end()) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700784 LogBufferElement *element = *it;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800785
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700786 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700787 busy = true;
Mark Salyzyn552b4752015-11-30 11:35:56 -0800788 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
789 oldest->triggerReader_Locked();
790 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800791 break;
792 }
793
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700794 if (element->getLogId() != id) {
Mark Salyzync89839a2014-02-11 12:29:31 -0800795 ++it;
796 continue;
797 }
Mark Salyzyn628d9372016-10-21 09:46:42 -0700798 // below this point element->getLogId() == id
Mark Salyzync89839a2014-02-11 12:29:31 -0800799
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800800 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
801 mLast[id] = it;
802 mLastSet[id] = true;
803 }
804
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700805 unsigned short dropped = element->getDropped();
Mark Salyzync89839a2014-02-11 12:29:31 -0800806
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700807 // remove any leading drops
808 if (leading && dropped) {
809 it = erase(it);
810 continue;
811 }
812
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700813 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700814 it = erase(it, true);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700815 continue;
816 }
817
Mark Salyzync572fc62016-07-14 15:34:30 -0700818 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
819 element->getTag() :
820 element->getUid();
821
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700822 if (hasBlacklist && mPrune.naughty(element)) {
823 last.clear(element);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700824 it = erase(it);
825 if (dropped) {
826 continue;
827 }
828
829 pruneRows--;
830 if (pruneRows == 0) {
831 break;
832 }
833
Mark Salyzync572fc62016-07-14 15:34:30 -0700834 if (key == worst) {
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700835 kick = true;
836 if (worst_sizes < second_worst_sizes) {
837 break;
838 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700839 worst_sizes -= element->getMsgLen();
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700840 }
841 continue;
842 }
843
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700844 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old))
845 || (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzyn4e29e172015-08-24 13:43:27 -0700846 break;
847 }
848
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700849 if (dropped) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700850 last.add(element);
851 if (worstPid
852 && ((!gc && (element->getPid() == worstPid))
Mark Salyzync572fc62016-07-14 15:34:30 -0700853 || (mLastWorstPidOfSystem[id].find(element->getPid())
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700854 == mLastWorstPidOfSystem[id].end()))) {
Mark Salyzyn628d9372016-10-21 09:46:42 -0700855 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700856 // watermark if current one empty. id is not LOG_ID_EVENTS
857 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn9faf9082016-09-01 07:28:44 -0700858 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700859 }
Mark Salyzync572fc62016-07-14 15:34:30 -0700860 if ((!gc && !worstPid && (key == worst))
861 || (mLastWorst[id].find(key) == mLastWorst[id].end())) {
862 mLastWorst[id][key] = it;
Mark Salyzyncb15c7c2015-08-24 13:43:27 -0700863 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800864 ++it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700865 continue;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800866 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700867
Mark Salyzync572fc62016-07-14 15:34:30 -0700868 if ((key != worst)
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700869 || (worstPid && (element->getPid() != worstPid))) {
Mark Salyzynfd318a82015-06-01 09:41:19 -0700870 leading = false;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700871 last.clear(element);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700872 ++it;
873 continue;
874 }
Mark Salyzyn628d9372016-10-21 09:46:42 -0700875 // key == worst below here
876 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700877
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700878 pruneRows--;
879 if (pruneRows == 0) {
880 break;
881 }
882
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700883 kick = true;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700884
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700885 unsigned short len = element->getMsgLen();
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700886
887 // do not create any leading drops
888 if (leading) {
889 it = erase(it);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700890 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700891 stats.drop(element);
892 element->setDropped(1);
893 if (last.coalesce(element, 1)) {
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700894 it = erase(it, true);
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700895 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700896 last.add(element);
897 if (worstPid && (!gc
898 || (mLastWorstPidOfSystem[id].find(worstPid)
899 == mLastWorstPidOfSystem[id].end()))) {
Mark Salyzyn628d9372016-10-21 09:46:42 -0700900 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700901 // watermark if current one empty. id is not
902 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700903 mLastWorstPidOfSystem[id][worstPid] = it;
904 }
Mark Salyzync572fc62016-07-14 15:34:30 -0700905 if ((!gc && !worstPid) ||
906 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
907 mLastWorst[id][worst] = it;
Mark Salyzyn50e21082015-09-08 09:12:51 -0700908 }
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700909 ++it;
910 }
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700911 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700912 if (worst_sizes < second_worst_sizes) {
913 break;
914 }
915 worst_sizes -= len;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800916 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700917 last.clear();
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800918
Mark Salyzync402bc72014-04-01 17:19:47 -0700919 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800920 break; // the following loop will ask bad clients to skip/drop
921 }
922 }
923
Mark Salyzync89839a2014-02-11 12:29:31 -0800924 bool whitelist = false;
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800925 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800926 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn12bac902014-02-26 09:50:16 -0800927 while((pruneRows > 0) && (it != mLogElements.end())) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700928 LogBufferElement *element = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700929
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700930 if (element->getLogId() != id) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700931 it++;
932 continue;
933 }
934
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800935 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
936 mLast[id] = it;
937 mLastSet[id] = true;
938 }
939
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700940 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700941 busy = true;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700942 if (whitelist) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800943 break;
944 }
Mark Salyzync402bc72014-04-01 17:19:47 -0700945
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700946 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
947 // kick a misbehaving log reader client off the island
948 oldest->release_Locked();
Mark Salyzyn552b4752015-11-30 11:35:56 -0800949 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
950 oldest->triggerReader_Locked();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700951 } else {
952 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzync89839a2014-02-11 12:29:31 -0800953 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700954 break;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800955 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700956
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700957 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
958 // WhiteListed
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700959 whitelist = true;
960 it++;
961 continue;
962 }
963
964 it = erase(it);
965 pruneRows--;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800966 }
967
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700968 // Do not save the whitelist if we are reader range limited
Mark Salyzync89839a2014-02-11 12:29:31 -0800969 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800970 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzync89839a2014-02-11 12:29:31 -0800971 while((it != mLogElements.end()) && (pruneRows > 0)) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700972 LogBufferElement *element = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700973
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700974 if (element->getLogId() != id) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700975 ++it;
976 continue;
Mark Salyzync89839a2014-02-11 12:29:31 -0800977 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700978
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800979 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
980 mLast[id] = it;
981 mLastSet[id] = true;
982 }
983
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700984 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700985 busy = true;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700986 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
987 // kick a misbehaving log reader client off the island
988 oldest->release_Locked();
Mark Salyzyn552b4752015-11-30 11:35:56 -0800989 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
990 oldest->triggerReader_Locked();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700991 } else {
992 oldest->triggerSkip_Locked(id, pruneRows);
993 }
994 break;
995 }
996
997 it = erase(it);
998 pruneRows--;
Mark Salyzync89839a2014-02-11 12:29:31 -0800999 }
1000 }
Mark Salyzync89839a2014-02-11 12:29:31 -08001001
Mark Salyzyn12bac902014-02-26 09:50:16 -08001002 LogTimeEntry::unlock();
Mark Salyzyn7702c3e2015-09-16 15:34:00 -07001003
1004 return (pruneRows > 0) && busy;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001005}
1006
1007// clear all rows of type "id" from the buffer.
Mark Salyzyn7702c3e2015-09-16 15:34:00 -07001008bool LogBuffer::clear(log_id_t id, uid_t uid) {
1009 bool busy = true;
1010 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
1011 for (int retry = 4;;) {
1012 if (retry == 1) { // last pass
1013 // Check if it is still busy after the sleep, we say prune
1014 // one entry, not another clear run, so we are looking for
1015 // the quick side effect of the return value to tell us if
1016 // we have a _blocked_ reader.
1017 pthread_mutex_lock(&mLogElementsLock);
1018 busy = prune(id, 1, uid);
1019 pthread_mutex_unlock(&mLogElementsLock);
1020 // It is still busy, blocked reader(s), lets kill them all!
1021 // otherwise, lets be a good citizen and preserve the slow
1022 // readers and let the clear run (below) deal with determining
1023 // if we are still blocked and return an error code to caller.
1024 if (busy) {
1025 LogTimeEntry::lock();
1026 LastLogTimes::iterator times = mTimes.begin();
1027 while (times != mTimes.end()) {
1028 LogTimeEntry *entry = (*times);
1029 // Killer punch
1030 if (entry->owned_Locked() && entry->isWatching(id)) {
1031 entry->release_Locked();
1032 }
1033 times++;
1034 }
1035 LogTimeEntry::unlock();
1036 }
1037 }
1038 pthread_mutex_lock(&mLogElementsLock);
1039 busy = prune(id, ULONG_MAX, uid);
1040 pthread_mutex_unlock(&mLogElementsLock);
1041 if (!busy || !--retry) {
1042 break;
1043 }
1044 sleep (1); // Let reader(s) catch up after notification
1045 }
1046 return busy;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001047}
1048
1049// get the used space associated with "id".
1050unsigned long LogBuffer::getSizeUsed(log_id_t id) {
1051 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzynd774bce2014-02-06 14:48:50 -08001052 size_t retval = stats.sizes(id);
Mark Salyzyn12bac902014-02-26 09:50:16 -08001053 pthread_mutex_unlock(&mLogElementsLock);
1054 return retval;
1055}
1056
Mark Salyzync89839a2014-02-11 12:29:31 -08001057// set the total space allocated to "id"
1058int LogBuffer::setSize(log_id_t id, unsigned long size) {
1059 // Reasonable limits ...
Mark Salyzynf4693f32016-09-27 13:08:23 -07001060 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzync89839a2014-02-11 12:29:31 -08001061 return -1;
1062 }
1063 pthread_mutex_lock(&mLogElementsLock);
1064 log_buffer_size(id) = size;
1065 pthread_mutex_unlock(&mLogElementsLock);
1066 return 0;
1067}
1068
1069// get the total space allocated to "id"
1070unsigned long LogBuffer::getSize(log_id_t id) {
1071 pthread_mutex_lock(&mLogElementsLock);
1072 size_t retval = log_buffer_size(id);
1073 pthread_mutex_unlock(&mLogElementsLock);
1074 return retval;
1075}
1076
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -08001077uint64_t LogBuffer::flushTo(
Mark Salyzyn924c0b32016-01-26 14:32:35 -08001078 SocketClient *reader, const uint64_t start,
1079 bool privileged, bool security,
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -08001080 int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
Mark Salyzyn12bac902014-02-26 09:50:16 -08001081 LogBufferElementCollection::iterator it;
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -08001082 uint64_t max = start;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001083 uid_t uid = reader->getUid();
1084
1085 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001086
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -08001087 if (start <= 1) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001088 // client wants to start from the beginning
1089 it = mLogElements.begin();
1090 } else {
1091 // Client wants to start from some specified time. Chances are
1092 // we are better off starting from the end of the time sorted list.
1093 for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
1094 --it;
1095 LogBufferElement *element = *it;
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -08001096 if (element->getSequence() <= start) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001097 it++;
1098 break;
1099 }
1100 }
1101 }
1102
1103 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn12bac902014-02-26 09:50:16 -08001104 LogBufferElement *element = *it;
1105
1106 if (!privileged && (element->getUid() != uid)) {
1107 continue;
1108 }
1109
Mark Salyzyn924c0b32016-01-26 14:32:35 -08001110 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1111 continue;
1112 }
1113
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -08001114 if (element->getSequence() <= start) {
Mark Salyzyn12bac902014-02-26 09:50:16 -08001115 continue;
1116 }
1117
1118 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -08001119 if (filter) {
1120 int ret = (*filter)(element, arg);
1121 if (ret == false) {
1122 continue;
1123 }
1124 if (ret != true) {
1125 break;
1126 }
Mark Salyzyn12bac902014-02-26 09:50:16 -08001127 }
1128
1129 pthread_mutex_unlock(&mLogElementsLock);
1130
1131 // range locking in LastLogTimes looks after us
Mark Salyzyn3443fda2015-12-03 15:38:35 -08001132 max = element->flushTo(reader, this, privileged);
Mark Salyzyn12bac902014-02-26 09:50:16 -08001133
1134 if (max == element->FLUSH_ERROR) {
1135 return max;
1136 }
1137
1138 pthread_mutex_lock(&mLogElementsLock);
1139 }
1140 pthread_mutex_unlock(&mLogElementsLock);
1141
1142 return max;
1143}
Mark Salyzynd774bce2014-02-06 14:48:50 -08001144
Mark Salyzynb921ab52015-12-17 09:58:43 -08001145std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1146 unsigned int logMask) {
Mark Salyzynd774bce2014-02-06 14:48:50 -08001147 pthread_mutex_lock(&mLogElementsLock);
1148
Mark Salyzynb921ab52015-12-17 09:58:43 -08001149 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzynd774bce2014-02-06 14:48:50 -08001150
1151 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn1c7b6fb2015-08-20 10:01:44 -07001152
1153 return ret;
Mark Salyzynd774bce2014-02-06 14:48:50 -08001154}