blob: e03731b709bfcbbe877e9cca35497c6ec86f9e4f [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
2 * Copyright (C) 2012-2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Mark Salyzyn60636fa2016-10-24 16:22:17 -070016// for manual checking of stale entries during LogBuffer::erase()
17//#define DEBUG_CHECK_FOR_STALE_ENTRIES
Mark Salyzyn0175b072014-02-26 09:50:16 -080018
Mark Salyzyn671e3432014-05-06 07:34:59 -070019#include <ctype.h>
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -080020#include <endian.h>
Mark Salyzyn202e1532015-02-09 08:21:05 -080021#include <errno.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080022#include <stdio.h>
23#include <string.h>
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070024#include <sys/cdefs.h>
Mark Salyzyn57a0af92014-05-09 17:44:18 -070025#include <sys/user.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080026#include <time.h>
27#include <unistd.h>
28
Mark Salyzyn511338d2015-05-19 09:12:30 -070029#include <unordered_map>
30
Mark Salyzyn671e3432014-05-06 07:34:59 -070031#include <cutils/properties.h>
Mark Salyzynf10e2732016-09-27 13:08:23 -070032#include <private/android_logger.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080033
34#include "LogBuffer.h"
Mark Salyzynb6bee332015-09-08 08:56:32 -070035#include "LogKlog.h"
Mark Salyzyn671e3432014-05-06 07:34:59 -070036#include "LogReader.h"
Mark Salyzyna2c02222016-12-13 10:31:29 -080037#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080038
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070039#ifndef __predict_false
40#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
41#endif
42
Mark Salyzyndfa7a072014-02-11 12:29:31 -080043// Default
Mark Salyzyndfa7a072014-02-11 12:29:31 -080044#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn671e3432014-05-06 07:34:59 -070045
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070046void LogBuffer::init() {
Mark Salyzyndfa7a072014-02-11 12:29:31 -080047 log_id_for_each(i) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -080048 mLastSet[i] = false;
49 mLast[i] = mLogElements.begin();
50
Mark Salyzynf10e2732016-09-27 13:08:23 -070051 if (setSize(i, __android_logger_get_buffer_size(i))) {
Mark Salyzyn57a0af92014-05-09 17:44:18 -070052 setSize(i, LOG_BUFFER_MIN_SIZE);
53 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -080054 }
Mark Salyzynb6bee332015-09-08 08:56:32 -070055 bool lastMonotonic = monotonic;
Mark Salyzynba7a9a02015-12-01 15:57:25 -080056 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzynb75cce02015-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 Salyzynb6bee332015-09-08 08:56:32 -070089 }
90
Mark Salyzynb75cce02015-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 Salyzynb6bee332015-09-08 08:56:32 -070093 //
Mark Salyzynb75cce02015-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 Salyzynb6bee332015-09-08 08:56:32 -0700103 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800104 times++;
Mark Salyzynb6bee332015-09-08 08:56:32 -0700105 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800106
107 LogTimeEntry::unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800108}
109
Mark Salyzynb6bee332015-09-08 08:56:32 -0700110LogBuffer::LogBuffer(LastLogTimes *times):
Mark Salyzynba7a9a02015-12-01 15:57:25 -0800111 monotonic(android_log_clockid() == CLOCK_MONOTONIC),
Mark Salyzynb6bee332015-09-08 08:56:32 -0700112 mTimes(*times) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700113 pthread_mutex_init(&mLogElementsLock, NULL);
114
Mark Salyzyna2c02222016-12-13 10:31:29 -0800115 log_id_for_each(i) {
116 lastLoggedElements[i] = NULL;
117 droppedElements[i] = NULL;
118 }
119
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700120 init();
121}
122
Mark Salyzyna2c02222016-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 Salyzyn1dfb4de2016-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 Salyzyna2c02222016-12-13 10:31:29 -0800137 // is it mostly identical?
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800138// if (!elem) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800139 unsigned short lenl = elem->getMsgLen();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800140 if (!lenl) return DIFFERENT;
141// if (!last) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800142 unsigned short lenr = last->getMsgLen();
Mark Salyzyn1dfb4de2016-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 Salyzyna2c02222016-12-13 10:31:29 -0800148
149 // last is more than a minute old, stop squashing identical messages
150 if (elem->getRealTime().nsec() >
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800151 (last->getRealTime().nsec() + 60 * NS_PER_SEC)) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800152
153 // Identical message
154 const char* msgl = elem->getMsg();
155 const char* msgr = last->getMsg();
Mark Salyzyn1dfb4de2016-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 Salyzyna2c02222016-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 Salyzyn1dfb4de2016-12-16 16:09:15 -0800172 sizeof(int32_t))) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800173 msgl += sizeof(android_log_event_string_t);
174 lenl -= sizeof(android_log_event_string_t);
175 msgr += sizeof(android_log_event_string_t);
176 lenr -= sizeof(android_log_event_string_t);
177 }
178 const char *avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800179 if (!avcl) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800180 lenl -= avcl - msgl;
181 const char *avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800182 if (!avcr) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800183 lenr -= avcr - msgr;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800184 if (lenl != lenr) return DIFFERENT;
185 if (fastcmp<memcmp>(avcl + strlen(avc),
Greg Hartmana6754dd2017-02-15 21:43:30 -0800186 avcr + strlen(avc),
187 lenl - strlen(avc))) return DIFFERENT;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800188 return SAME;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800189}
190
Mark Salyzyn202e1532015-02-09 08:21:05 -0800191int LogBuffer::log(log_id_t log_id, log_time realtime,
192 uid_t uid, pid_t pid, pid_t tid,
193 const char *msg, unsigned short len) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800194 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800195 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800196 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700197
Mark Salyzyn0175b072014-02-26 09:50:16 -0800198 LogBufferElement *elem = new LogBufferElement(log_id, realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700199 uid, pid, tid, msg, len);
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000200 if (log_id != LOG_ID_SECURITY) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800201 int prio = ANDROID_LOG_INFO;
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000202 const char *tag = NULL;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800203 if (log_id == LOG_ID_EVENTS) {
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700204 tag = tagToName(elem->getTag());
Mark Salyzyn083b0372015-12-04 10:59:45 -0800205 } else {
206 prio = *msg;
207 tag = msg + 1;
208 }
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700209 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800210 // Log traffic received to total
211 pthread_mutex_lock(&mLogElementsLock);
212 stats.add(elem);
213 stats.subtract(elem);
214 pthread_mutex_unlock(&mLogElementsLock);
215 delete elem;
216 return -EACCES;
217 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700218 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800219
220 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800221 LogBufferElement* currentLast = lastLoggedElements[log_id];
222 if (currentLast) {
223 LogBufferElement *dropped = droppedElements[log_id];
224 unsigned short count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800225 //
226 // State Init
227 // incoming:
228 // dropped = NULL
229 // currentLast = NULL;
230 // elem = incoming message
231 // outgoing:
232 // dropped = NULL -> State 0
233 // currentLast = copy of elem
234 // log elem
235 // State 0
236 // incoming:
237 // count = 0
238 // dropped = NULL
239 // currentLast = copy of last message
240 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800241 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800242 // dropped = copy of first identical message -> State 1
243 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800244 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800245 // dropped = NULL -> State 0
246 // delete copy of last message (incoming currentLast)
247 // currentLast = copy of elem
248 // log elem
249 // State 1
250 // incoming:
251 // count = 0
252 // dropped = copy of first identical message
253 // currentLast = reference to last held-back incoming
254 // message
255 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800256 // outgoing: if match == SAME
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800257 // delete copy of first identical message (dropped)
258 // dropped = reference to last held-back incoming
259 // message set to chatty count of 1 -> State 2
260 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800261 // outgoing: if match == SAME_LIBLOG
262 // dropped = copy of first identical message -> State 1
263 // take sum of currentLast and elem
264 // if sum overflows:
265 // log currentLast
266 // currentLast = reference to elem
267 // else
268 // delete currentLast
269 // currentLast = reference to elem, sum liblog.
270 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800271 // delete dropped
272 // dropped = NULL -> State 0
273 // log reference to last held-back (currentLast)
274 // currentLast = copy of elem
275 // log elem
276 // State 2
277 // incoming:
278 // count = chatty count
279 // dropped = chatty message holding count
280 // currentLast = reference to last held-back incoming
281 // message.
282 // dropped = chatty message holding count
283 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800284 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800285 // delete chatty message holding count
286 // dropped = reference to last held-back incoming
287 // message, set to chatty count + 1
288 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800289 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800290 // log dropped (chatty message)
291 // dropped = NULL -> State 0
292 // log reference to last held-back (currentLast)
293 // currentLast = copy of elem
294 // log elem
295 //
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800296 enum match_type match = identical(elem, currentLast);
297 if (match != DIFFERENT) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800298 if (dropped) {
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800299 // Sum up liblog tag messages?
300 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
301 android_log_event_int_t* event =
302 reinterpret_cast<android_log_event_int_t*>(
303 const_cast<char*>(currentLast->getMsg()));
304 //
305 // To unit test, differentiate with something like:
306 // event->header.tag = htole32(CHATTY_LOG_TAG);
307 // here, then instead of delete currentLast below,
308 // log(currentLast) to see the incremental sums form.
309 //
310 uint32_t swab = event->payload.data;
311 unsigned long long total = htole32(swab);
312 event = reinterpret_cast<android_log_event_int_t*>(
313 const_cast<char*>(elem->getMsg()));
314 swab = event->payload.data;
315
316 lastLoggedElements[LOG_ID_EVENTS] = elem;
317 total += htole32(swab);
318 // check for overflow
319 if (total >= UINT32_MAX) {
320 log(currentLast);
321 pthread_mutex_unlock(&mLogElementsLock);
322 return len;
323 }
324 stats.add(currentLast);
325 stats.subtract(currentLast);
326 delete currentLast;
327 swab = total;
328 event->payload.data = htole32(swab);
329 pthread_mutex_unlock(&mLogElementsLock);
330 return len;
331 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800332 if (count == USHRT_MAX) {
333 log(dropped);
334 count = 1;
335 } else {
336 delete dropped;
337 ++count;
338 }
339 }
340 if (count) {
341 stats.add(currentLast);
342 stats.subtract(currentLast);
343 currentLast->setDropped(count);
344 }
345 droppedElements[log_id] = currentLast;
346 lastLoggedElements[log_id] = elem;
347 pthread_mutex_unlock(&mLogElementsLock);
348 return len;
349 }
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800350 if (dropped) { // State 1 or 2
351 if (count) { // State 2
352 log(dropped); // report chatty
353 } else { // State 1
354 delete dropped;
355 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800356 droppedElements[log_id] = NULL;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800357 log(currentLast); // report last message in the series
358 } else { // State 0
Mark Salyzyna2c02222016-12-13 10:31:29 -0800359 delete currentLast;
360 }
361 }
362 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800363
Mark Salyzyna2c02222016-12-13 10:31:29 -0800364 log(elem);
365 pthread_mutex_unlock(&mLogElementsLock);
366
367 return len;
368}
369
370// assumes mLogElementsLock held, owns elem, will look after garbage collection
371void LogBuffer::log(LogBufferElement* elem) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800372 // Insert elements in time sorted order if possible
373 // NB: if end is region locked, place element at end of list
374 LogBufferElementCollection::iterator it = mLogElements.end();
375 LogBufferElementCollection::iterator last = it;
Mark Salyzyneae155e2014-10-13 16:49:47 -0700376 while (last != mLogElements.begin()) {
377 --it;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800378 if ((*it)->getRealTime() <= elem->getRealTime()) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800379 break;
380 }
381 last = it;
382 }
Mark Salyzync03e72c2014-02-18 11:23:53 -0800383
Mark Salyzyn0175b072014-02-26 09:50:16 -0800384 if (last == mLogElements.end()) {
385 mLogElements.push_back(elem);
386 } else {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800387 uint64_t end = 1;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800388 bool end_set = false;
389 bool end_always = false;
390
391 LogTimeEntry::lock();
392
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700393 LastLogTimes::iterator times = mTimes.begin();
394 while(times != mTimes.end()) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800395 LogTimeEntry* entry = (*times);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800396 if (entry->owned_Locked()) {
397 if (!entry->mNonBlock) {
398 end_always = true;
399 break;
400 }
401 if (!end_set || (end <= entry->mEnd)) {
402 end = entry->mEnd;
403 end_set = true;
404 }
405 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700406 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800407 }
408
409 if (end_always
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800410 || (end_set && (end >= (*last)->getSequence()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800411 mLogElements.push_back(elem);
412 } else {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800413 mLogElements.insert(last, elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800414 }
415
416 LogTimeEntry::unlock();
417 }
418
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700419 stats.add(elem);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800420 maybePrune(elem->getLogId());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800421}
422
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700423// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800424//
425// mLogElementsLock must be held when this function is called.
426void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800427 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700428 unsigned long maxSize = log_buffer_size(id);
429 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700430 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700431 size_t elements = stats.realElements(id);
432 size_t minElements = elements / 100;
433 if (minElements < minPrune) {
434 minElements = minPrune;
435 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700436 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700437 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700438 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800439 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700440 if (pruneRows > maxPrune) {
441 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700442 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800443 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800444 }
445}
446
Mark Salyzyn831aa292015-09-03 16:08:50 -0700447LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700448 LogBufferElementCollection::iterator it, bool coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700449 LogBufferElement *element = *it;
450 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700451
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700452 // Remove iterator references in the various lists that will become stale
453 // after the element is erased from the main logging list.
454
Mark Salyzyn6a066942016-07-14 15:34:30 -0700455 { // start of scope for found iterator
456 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
457 element->getTag() : element->getUid();
458 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
459 if ((found != mLastWorst[id].end()) && (it == found->second)) {
460 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700461 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700462 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700463
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700464 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700465 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700466 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
467 // long term code stability, find() check should be fast for those ids.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700468 LogBufferPidIteratorMap::iterator found =
469 mLastWorstPidOfSystem[id].find(element->getPid());
470 if ((found != mLastWorstPidOfSystem[id].end())
471 && (it == found->second)) {
472 mLastWorstPidOfSystem[id].erase(found);
473 }
474 }
475
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800476 bool setLast[LOG_ID_MAX];
477 bool doSetLast = false;
478 log_id_for_each(i) {
479 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
480 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700481#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
482 LogBufferElementCollection::iterator bad = it;
483 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
484 element->getTag() : element->getUid();
485#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700486 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800487 if (doSetLast) {
488 log_id_for_each(i) {
489 if (setLast[i]) {
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700490 if (__predict_false(it == mLogElements.end())) { // impossible
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800491 mLastSet[i] = false;
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700492 mLast[i] = mLogElements.begin();
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800493 } else {
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700494 mLast[i] = it; // push down the road as next-best-watermark
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800495 }
496 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800497 }
498 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700499#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
500 log_id_for_each(i) {
501 for(auto b : mLastWorst[i]) {
502 if (bad == b.second) {
503 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n",
504 i, b.first, key);
505 }
506 }
507 for(auto b : mLastWorstPidOfSystem[i]) {
508 if (bad == b.second) {
509 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n",
510 i, b.first);
511 }
512 }
513 if (mLastSet[i] && (bad == mLast[i])) {
514 android::prdebug("stale mLast[%d]\n", i);
515 mLastSet[i] = false;
516 mLast[i] = mLogElements.begin();
517 }
518 }
519#endif
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700520 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700521 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700522 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700523 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700524 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700525 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700526
527 return it;
528}
529
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700530// Define a temporary mechanism to report the last LogBufferElement pointer
531// for the specified uid, pid and tid. Used below to help merge-sort when
532// pruning for worst UID.
533class LogBufferElementKey {
534 const union {
535 struct {
Mark Salyzyn684bdb52016-12-13 12:44:20 -0800536 uint32_t uid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700537 uint16_t pid;
538 uint16_t tid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700539 } __packed;
540 uint64_t value;
541 } __packed;
542
543public:
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700544 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid):
545 uid(uid),
546 pid(pid),
Mark Salyzyn684bdb52016-12-13 12:44:20 -0800547 tid(tid)
548 {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700549 }
Chih-Hung Hsieh1cc82ce2016-04-25 13:49:46 -0700550 explicit LogBufferElementKey(uint64_t key):value(key) { }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700551
552 uint64_t getKey() { return value; }
553};
554
Mark Salyzyn511338d2015-05-19 09:12:30 -0700555class LogBufferElementLast {
556
557 typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap;
558 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700559
560public:
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700561
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700562 bool coalesce(LogBufferElement *element, unsigned short dropped) {
563 LogBufferElementKey key(element->getUid(),
564 element->getPid(),
565 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700566 LogBufferElementMap::iterator it = map.find(key.getKey());
567 if (it != map.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700568 LogBufferElement *found = it->second;
569 unsigned short moreDropped = found->getDropped();
570 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700571 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700572 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700573 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700574 return true;
575 }
576 }
577 return false;
578 }
579
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700580 void add(LogBufferElement *element) {
581 LogBufferElementKey key(element->getUid(),
582 element->getPid(),
583 element->getTid());
584 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700585 }
586
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700587 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700588 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700589 }
590
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700591 void clear(LogBufferElement *element) {
592 uint64_t current = element->getRealTime().nsec()
Mark Salyzyn047cc072015-06-04 13:35:30 -0700593 - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn511338d2015-05-19 09:12:30 -0700594 for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700595 LogBufferElement *mapElement = it->second;
596 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD)
597 && (current > mapElement->getRealTime().nsec())) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700598 it = map.erase(it);
599 } else {
600 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700601 }
602 }
603 }
604
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700605};
606
Mark Salyzyn0175b072014-02-26 09:50:16 -0800607// prune "pruneRows" of type "id" from the buffer.
608//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700609// This garbage collection task is used to expire log entries. It is called to
610// remove all logs (clear), all UID logs (unprivileged clear), or every
611// 256 or 10% of the total logs (whichever is less) to prune the logs.
612//
613// First there is a prep phase where we discover the reader region lock that
614// acts as a backstop to any pruning activity to stop there and go no further.
615//
616// There are three major pruning loops that follow. All expire from the oldest
617// entries. Since there are multiple log buffers, the Android logging facility
618// will appear to drop entries 'in the middle' when looking at multiple log
619// sources and buffers. This effect is slightly more prominent when we prune
620// the worst offender by logging source. Thus the logs slowly loose content
621// and value as you move back in time. This is preferred since chatty sources
622// invariably move the logs value down faster as less chatty sources would be
623// expired in the noise.
624//
625// The first loop performs blacklisting and worst offender pruning. Falling
626// through when there are no notable worst offenders and have not hit the
627// region lock preventing further worst offender pruning. This loop also looks
628// after managing the chatty log entries and merging to help provide
629// statistical basis for blame. The chatty entries are not a notification of
630// how much logs you may have, but instead represent how much logs you would
631// have had in a virtual log buffer that is extended to cover all the in-memory
632// logs without loss. They last much longer than the represented pruned logs
633// since they get multiplied by the gains in the non-chatty log sources.
634//
635// The second loop get complicated because an algorithm of watermarks and
636// history is maintained to reduce the order and keep processing time
637// down to a minimum at scale. These algorithms can be costly in the face
638// of larger log buffers, or severly limited processing time granted to a
639// background task at lowest priority.
640//
641// This second loop does straight-up expiration from the end of the logs
642// (again, remember for the specified log buffer id) but does some whitelist
643// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
644// spam filtration all take priority. This second loop also checks if a region
645// lock is causing us to buffer too much in the logs to help the reader(s),
646// and will tell the slowest reader thread to skip log entries, and if
647// persistent and hits a further threshold, kill the reader thread.
648//
649// The third thread is optional, and only gets hit if there was a whitelist
650// and more needs to be pruned against the backstop of the region lock.
651//
Mark Salyzyn0175b072014-02-26 09:50:16 -0800652// mLogElementsLock must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700653//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700654bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800655 LogTimeEntry *oldest = NULL;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700656 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700657 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800658
659 LogTimeEntry::lock();
660
661 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700662 LastLogTimes::iterator times = mTimes.begin();
663 while(times != mTimes.end()) {
664 LogTimeEntry *entry = (*times);
TraianX Schiauda6495d2014-12-17 10:53:41 +0200665 if (entry->owned_Locked() && entry->isWatching(id)
Mark Salyzynb75cce02015-11-30 11:35:56 -0800666 && (!oldest ||
667 (oldest->mStart > entry->mStart) ||
668 ((oldest->mStart == entry->mStart) &&
669 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800670 oldest = entry;
671 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700672 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800673 }
674
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800675 LogBufferElementCollection::iterator it;
676
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700677 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700678 // Only here if clear all request from non system source, so chatty
679 // filter logistics is not required.
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800680 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
681 while (it != mLogElements.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700682 LogBufferElement *element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700683
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700684 if ((element->getLogId() != id) || (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700685 ++it;
686 continue;
687 }
688
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800689 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
690 mLast[id] = it;
691 mLastSet[id] = true;
692 }
693
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700694 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700695 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800696 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
697 oldest->triggerReader_Locked();
698 } else {
699 oldest->triggerSkip_Locked(id, pruneRows);
700 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700701 break;
702 }
703
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700704 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700705 if (--pruneRows == 0) {
706 break;
707 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700708 }
709 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700710 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700711 }
712
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700713 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn083b0372015-12-04 10:59:45 -0800714 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700715 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800716 // recalculate the worst offender on every batched pass
Mark Salyzyn6a066942016-07-14 15:34:30 -0700717 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800718 size_t worst_sizes = 0;
719 size_t second_worst_sizes = 0;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700720 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800721
Mark Salyzynae769232015-03-17 17:17:25 -0700722 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700723 // Calculate threshold as 12.5% of available storage
724 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700725
Mark Salyzyn6a066942016-07-14 15:34:30 -0700726 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
727 stats.sortTags(AID_ROOT, (pid_t)0, 2, id).findWorst(
728 worst, worst_sizes, second_worst_sizes, threshold);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700729 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700730 } else {
731 stats.sort(AID_ROOT, (pid_t)0, 2, id).findWorst(
732 worst, worst_sizes, second_worst_sizes, threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700733
Mark Salyzyn6a066942016-07-14 15:34:30 -0700734 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
735 stats.sortPids(worst, (pid_t)0, 2, id).findWorst(
736 worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700737 }
738 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800739 }
740
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700741 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700742 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700743 break;
744 }
745
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800746 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700747 bool leading = true;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800748 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700749 // Perform at least one mandatory garbage collection cycle in following
750 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700751 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700752 // - check age-out of preserved logs
753 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700754 if (!gc && (worst != -1)) {
755 { // begin scope for worst found iterator
756 LogBufferIteratorMap::iterator found = mLastWorst[id].find(worst);
757 if ((found != mLastWorst[id].end())
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700758 && (found->second != mLogElements.end())) {
759 leading = false;
760 it = found->second;
761 }
762 }
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700763 if (worstPid) { // begin scope for pid worst found iterator
764 // FYI: worstPid only set if !LOG_ID_EVENTS and
765 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700766 LogBufferPidIteratorMap::iterator found
767 = mLastWorstPidOfSystem[id].find(worstPid);
768 if ((found != mLastWorstPidOfSystem[id].end())
769 && (found->second != mLogElements.end())) {
770 leading = false;
771 it = found->second;
772 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700773 }
774 }
Mark Salyzynccfe8442015-08-24 13:43:27 -0700775 static const timespec too_old = {
776 EXPIRE_HOUR_THRESHOLD * 60 * 60, 0
777 };
778 LogBufferElementCollection::iterator lastt;
779 lastt = mLogElements.end();
780 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700781 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700782 while (it != mLogElements.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700783 LogBufferElement *element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800784
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700785 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700786 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800787 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
788 oldest->triggerReader_Locked();
789 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800790 break;
791 }
792
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700793 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800794 ++it;
795 continue;
796 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700797 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800798
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800799 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
800 mLast[id] = it;
801 mLastSet[id] = true;
802 }
803
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700804 unsigned short dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800805
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700806 // remove any leading drops
807 if (leading && dropped) {
808 it = erase(it);
809 continue;
810 }
811
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700812 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700813 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700814 continue;
815 }
816
Mark Salyzyn6a066942016-07-14 15:34:30 -0700817 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
818 element->getTag() :
819 element->getUid();
820
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700821 if (hasBlacklist && mPrune.naughty(element)) {
822 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700823 it = erase(it);
824 if (dropped) {
825 continue;
826 }
827
828 pruneRows--;
829 if (pruneRows == 0) {
830 break;
831 }
832
Mark Salyzyn6a066942016-07-14 15:34:30 -0700833 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700834 kick = true;
835 if (worst_sizes < second_worst_sizes) {
836 break;
837 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700838 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700839 }
840 continue;
841 }
842
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700843 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old))
844 || (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700845 break;
846 }
847
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700848 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700849 last.add(element);
850 if (worstPid
851 && ((!gc && (element->getPid() == worstPid))
Mark Salyzyn6a066942016-07-14 15:34:30 -0700852 || (mLastWorstPidOfSystem[id].find(element->getPid())
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700853 == mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700854 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700855 // watermark if current one empty. id is not LOG_ID_EVENTS
856 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700857 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700858 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700859 if ((!gc && !worstPid && (key == worst))
860 || (mLastWorst[id].find(key) == mLastWorst[id].end())) {
861 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700862 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800863 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700864 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800865 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700866
Mark Salyzyn6a066942016-07-14 15:34:30 -0700867 if ((key != worst)
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700868 || (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700869 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700870 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700871 ++it;
872 continue;
873 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700874 // key == worst below here
875 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700876
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700877 pruneRows--;
878 if (pruneRows == 0) {
879 break;
880 }
881
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700882 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700883
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700884 unsigned short len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700885
886 // do not create any leading drops
887 if (leading) {
888 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700889 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700890 stats.drop(element);
891 element->setDropped(1);
892 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700893 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700894 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700895 last.add(element);
896 if (worstPid && (!gc
897 || (mLastWorstPidOfSystem[id].find(worstPid)
898 == mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700899 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700900 // watermark if current one empty. id is not
901 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700902 mLastWorstPidOfSystem[id][worstPid] = it;
903 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700904 if ((!gc && !worstPid) ||
905 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
906 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700907 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700908 ++it;
909 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700910 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700911 if (worst_sizes < second_worst_sizes) {
912 break;
913 }
914 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800915 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700916 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800917
Mark Salyzyn1c950472014-04-01 17:19:47 -0700918 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800919 break; // the following loop will ask bad clients to skip/drop
920 }
921 }
922
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800923 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800924 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800925 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800926 while((pruneRows > 0) && (it != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700927 LogBufferElement *element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700928
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700929 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700930 it++;
931 continue;
932 }
933
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800934 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
935 mLast[id] = it;
936 mLastSet[id] = true;
937 }
938
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700939 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700940 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700941 if (whitelist) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800942 break;
943 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700944
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700945 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
946 // kick a misbehaving log reader client off the island
947 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800948 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
949 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700950 } else {
951 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800952 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700953 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800954 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700955
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700956 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
957 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700958 whitelist = true;
959 it++;
960 continue;
961 }
962
963 it = erase(it);
964 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800965 }
966
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700967 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800968 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800969 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800970 while((it != mLogElements.end()) && (pruneRows > 0)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700971 LogBufferElement *element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700972
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700973 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700974 ++it;
975 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800976 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700977
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800978 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
979 mLast[id] = it;
980 mLastSet[id] = true;
981 }
982
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700983 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700984 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700985 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
986 // kick a misbehaving log reader client off the island
987 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800988 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
989 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700990 } else {
991 oldest->triggerSkip_Locked(id, pruneRows);
992 }
993 break;
994 }
995
996 it = erase(it);
997 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800998 }
999 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001000
Mark Salyzyn0175b072014-02-26 09:50:16 -08001001 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001002
1003 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001004}
1005
1006// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -07001007bool LogBuffer::clear(log_id_t id, uid_t uid) {
1008 bool busy = true;
1009 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
1010 for (int retry = 4;;) {
1011 if (retry == 1) { // last pass
1012 // Check if it is still busy after the sleep, we say prune
1013 // one entry, not another clear run, so we are looking for
1014 // the quick side effect of the return value to tell us if
1015 // we have a _blocked_ reader.
1016 pthread_mutex_lock(&mLogElementsLock);
1017 busy = prune(id, 1, uid);
1018 pthread_mutex_unlock(&mLogElementsLock);
1019 // It is still busy, blocked reader(s), lets kill them all!
1020 // otherwise, lets be a good citizen and preserve the slow
1021 // readers and let the clear run (below) deal with determining
1022 // if we are still blocked and return an error code to caller.
1023 if (busy) {
1024 LogTimeEntry::lock();
1025 LastLogTimes::iterator times = mTimes.begin();
1026 while (times != mTimes.end()) {
1027 LogTimeEntry *entry = (*times);
1028 // Killer punch
1029 if (entry->owned_Locked() && entry->isWatching(id)) {
1030 entry->release_Locked();
1031 }
1032 times++;
1033 }
1034 LogTimeEntry::unlock();
1035 }
1036 }
1037 pthread_mutex_lock(&mLogElementsLock);
1038 busy = prune(id, ULONG_MAX, uid);
1039 pthread_mutex_unlock(&mLogElementsLock);
1040 if (!busy || !--retry) {
1041 break;
1042 }
1043 sleep (1); // Let reader(s) catch up after notification
1044 }
1045 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001046}
1047
1048// get the used space associated with "id".
1049unsigned long LogBuffer::getSizeUsed(log_id_t id) {
1050 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001051 size_t retval = stats.sizes(id);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001052 pthread_mutex_unlock(&mLogElementsLock);
1053 return retval;
1054}
1055
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001056// set the total space allocated to "id"
1057int LogBuffer::setSize(log_id_t id, unsigned long size) {
1058 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -07001059 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001060 return -1;
1061 }
1062 pthread_mutex_lock(&mLogElementsLock);
1063 log_buffer_size(id) = size;
1064 pthread_mutex_unlock(&mLogElementsLock);
1065 return 0;
1066}
1067
1068// get the total space allocated to "id"
1069unsigned long LogBuffer::getSize(log_id_t id) {
1070 pthread_mutex_lock(&mLogElementsLock);
1071 size_t retval = log_buffer_size(id);
1072 pthread_mutex_unlock(&mLogElementsLock);
1073 return retval;
1074}
1075
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001076uint64_t LogBuffer::flushTo(
Mark Salyzyn8fa88962016-01-26 14:32:35 -08001077 SocketClient *reader, const uint64_t start,
1078 bool privileged, bool security,
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001079 int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001080 LogBufferElementCollection::iterator it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001081 uint64_t max = start;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001082 uid_t uid = reader->getUid();
1083
1084 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001085
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001086 if (start <= 1) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001087 // client wants to start from the beginning
1088 it = mLogElements.begin();
1089 } else {
1090 // Client wants to start from some specified time. Chances are
1091 // we are better off starting from the end of the time sorted list.
1092 for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
1093 --it;
1094 LogBufferElement *element = *it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001095 if (element->getSequence() <= start) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001096 it++;
1097 break;
1098 }
1099 }
1100 }
1101
Mark Salyzynb5b87962017-01-23 14:20:31 -08001102 // Help detect if the valid message before is from the same source so
1103 // we can differentiate chatty filter types.
1104 pid_t lastTid[LOG_ID_MAX] = { 0 };
1105
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001106 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001107 LogBufferElement *element = *it;
1108
1109 if (!privileged && (element->getUid() != uid)) {
1110 continue;
1111 }
1112
Mark Salyzyn8fa88962016-01-26 14:32:35 -08001113 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1114 continue;
1115 }
1116
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001117 if (element->getSequence() <= start) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001118 continue;
1119 }
1120
1121 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001122 if (filter) {
1123 int ret = (*filter)(element, arg);
1124 if (ret == false) {
1125 continue;
1126 }
1127 if (ret != true) {
1128 break;
1129 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001130 }
1131
Mark Salyzynb5b87962017-01-23 14:20:31 -08001132 bool sameTid = lastTid[element->getLogId()] == element->getTid();
1133 // Dropped (chatty) immediately following a valid log from the
1134 // same source in the same log buffer indicates we have a
1135 // multiple identical squash. chatty that differs source
1136 // is due to spam filter. chatty to chatty of different
1137 // source is also due to spam filter.
1138 lastTid[element->getLogId()] = (element->getDropped() && !sameTid) ?
1139 0 : element->getTid();
1140
Mark Salyzyn0175b072014-02-26 09:50:16 -08001141 pthread_mutex_unlock(&mLogElementsLock);
1142
1143 // range locking in LastLogTimes looks after us
Mark Salyzynb5b87962017-01-23 14:20:31 -08001144 max = element->flushTo(reader, this, privileged, sameTid);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001145
1146 if (max == element->FLUSH_ERROR) {
1147 return max;
1148 }
1149
1150 pthread_mutex_lock(&mLogElementsLock);
1151 }
1152 pthread_mutex_unlock(&mLogElementsLock);
1153
1154 return max;
1155}
Mark Salyzyn34facab2014-02-06 14:48:50 -08001156
Mark Salyzynee3b8382015-12-17 09:58:43 -08001157std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1158 unsigned int logMask) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001159 pthread_mutex_lock(&mLogElementsLock);
1160
Mark Salyzynee3b8382015-12-17 09:58:43 -08001161 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001162
1163 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn73160ac2015-08-20 10:01:44 -07001164
1165 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001166}