blob: dd78275f5eb8e33d830456b09e9cd46e659c4e87 [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();
Mark Salyzyn501c3732017-03-10 14:31:54 -080075 while ((it != mLogElements.end())) {
76 LogBufferElement* e = *it;
Mark Salyzynb75cce02015-11-30 11:35:56 -080077 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();
Mark Salyzyn501c3732017-03-10 14:31:54 -080099 while (times != mTimes.end()) {
100 LogTimeEntry* entry = (*times);
Mark Salyzynb75cce02015-11-30 11:35:56 -0800101 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 Salyzyn501c3732017-03-10 14:31:54 -0800110LogBuffer::LogBuffer(LastLogTimes* times)
111 : monotonic(android_log_clockid() == CLOCK_MONOTONIC), mTimes(*times) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700112 pthread_mutex_init(&mLogElementsLock, NULL);
113
Mark Salyzyna2c02222016-12-13 10:31:29 -0800114 log_id_for_each(i) {
115 lastLoggedElements[i] = NULL;
116 droppedElements[i] = NULL;
117 }
118
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700119 init();
120}
121
Mark Salyzyna2c02222016-12-13 10:31:29 -0800122LogBuffer::~LogBuffer() {
123 log_id_for_each(i) {
124 delete lastLoggedElements[i];
125 delete droppedElements[i];
126 }
127}
128
Mark Salyzyn501c3732017-03-10 14:31:54 -0800129enum match_type { DIFFERENT, SAME, SAME_LIBLOG };
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800130
Mark Salyzyn501c3732017-03-10 14:31:54 -0800131static enum match_type identical(LogBufferElement* elem,
132 LogBufferElement* last) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800133 // is it mostly identical?
Mark Salyzyn501c3732017-03-10 14:31:54 -0800134 // if (!elem) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800135 unsigned short lenl = elem->getMsgLen();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800136 if (!lenl) return DIFFERENT;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800137 // if (!last) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800138 unsigned short lenr = last->getMsgLen();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800139 if (!lenr) return DIFFERENT;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800140 // if (elem->getLogId() != last->getLogId()) return DIFFERENT;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800141 if (elem->getUid() != last->getUid()) return DIFFERENT;
142 if (elem->getPid() != last->getPid()) return DIFFERENT;
143 if (elem->getTid() != last->getTid()) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800144
145 // last is more than a minute old, stop squashing identical messages
146 if (elem->getRealTime().nsec() >
Mark Salyzyn501c3732017-03-10 14:31:54 -0800147 (last->getRealTime().nsec() + 60 * NS_PER_SEC))
148 return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800149
150 // Identical message
151 const char* msgl = elem->getMsg();
152 const char* msgr = last->getMsg();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800153 if (lenl == lenr) {
154 if (!fastcmp<memcmp>(msgl, msgr, lenl)) return SAME;
155 // liblog tagged messages (content gets summed)
156 if ((elem->getLogId() == LOG_ID_EVENTS) &&
157 (lenl == sizeof(android_log_event_int_t)) &&
Mark Salyzyn501c3732017-03-10 14:31:54 -0800158 !fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_int_t) -
159 sizeof(int32_t)) &&
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700160 (elem->getTag() == LIBLOG_LOG_TAG)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800161 return SAME_LIBLOG;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700162 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800163 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800164
165 // audit message (except sequence number) identical?
166 static const char avc[] = "): avc: ";
167
168 if (last->isBinary()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800169 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) -
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700170 sizeof(int32_t))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800171 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700172 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800173 msgl += sizeof(android_log_event_string_t);
174 lenl -= sizeof(android_log_event_string_t);
175 msgr += sizeof(android_log_event_string_t);
176 lenr -= sizeof(android_log_event_string_t);
177 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800178 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;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800181 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;
Alex Shlyapnikov589f4e72017-02-14 17:21:55 -0800185 // TODO: After b/35468874 is addressed, revisit "lenl > strlen(avc)"
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800186 // condition, it might become superfluous.
Alex Shlyapnikov589f4e72017-02-14 17:21:55 -0800187 if (lenl > strlen(avc) &&
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700188 fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc),
189 lenl - strlen(avc))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800190 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700191 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800192 return SAME;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800193}
194
Mark Salyzyn501c3732017-03-10 14:31:54 -0800195int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
196 pid_t tid, const char* msg, unsigned short len) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800197 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800198 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800199 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700200
Mark Salyzyn501c3732017-03-10 14:31:54 -0800201 LogBufferElement* elem =
202 new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000203 if (log_id != LOG_ID_SECURITY) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800204 int prio = ANDROID_LOG_INFO;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800205 const char* tag = NULL;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800206 if (log_id == LOG_ID_EVENTS) {
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700207 tag = tagToName(elem->getTag());
Mark Salyzyn083b0372015-12-04 10:59:45 -0800208 } else {
209 prio = *msg;
210 tag = msg + 1;
211 }
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700212 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800213 // Log traffic received to total
214 pthread_mutex_lock(&mLogElementsLock);
215 stats.add(elem);
216 stats.subtract(elem);
217 pthread_mutex_unlock(&mLogElementsLock);
218 delete elem;
219 return -EACCES;
220 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700221 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800222
223 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800224 LogBufferElement* currentLast = lastLoggedElements[log_id];
225 if (currentLast) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800226 LogBufferElement* dropped = droppedElements[log_id];
Mark Salyzyna2c02222016-12-13 10:31:29 -0800227 unsigned short count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800228 //
229 // State Init
230 // incoming:
231 // dropped = NULL
232 // currentLast = NULL;
233 // elem = incoming message
234 // outgoing:
235 // dropped = NULL -> State 0
236 // currentLast = copy of elem
237 // log elem
238 // State 0
239 // incoming:
240 // count = 0
241 // dropped = NULL
242 // currentLast = copy of last message
243 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800244 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800245 // dropped = copy of first identical message -> State 1
246 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800247 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800248 // dropped = NULL -> State 0
249 // delete copy of last message (incoming currentLast)
250 // currentLast = copy of elem
251 // log elem
252 // State 1
253 // incoming:
254 // count = 0
255 // dropped = copy of first identical message
256 // currentLast = reference to last held-back incoming
257 // message
258 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800259 // outgoing: if match == SAME
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800260 // delete copy of first identical message (dropped)
261 // dropped = reference to last held-back incoming
262 // message set to chatty count of 1 -> State 2
263 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800264 // outgoing: if match == SAME_LIBLOG
265 // dropped = copy of first identical message -> State 1
266 // take sum of currentLast and elem
267 // if sum overflows:
268 // log currentLast
269 // currentLast = reference to elem
270 // else
271 // delete currentLast
272 // currentLast = reference to elem, sum liblog.
273 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800274 // delete dropped
275 // dropped = NULL -> State 0
276 // log reference to last held-back (currentLast)
277 // currentLast = copy of elem
278 // log elem
279 // State 2
280 // incoming:
281 // count = chatty count
282 // dropped = chatty message holding count
283 // currentLast = reference to last held-back incoming
284 // message.
285 // dropped = chatty message holding count
286 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800287 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800288 // delete chatty message holding count
289 // dropped = reference to last held-back incoming
290 // message, set to chatty count + 1
291 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800292 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800293 // log dropped (chatty message)
294 // dropped = NULL -> State 0
295 // log reference to last held-back (currentLast)
296 // currentLast = copy of elem
297 // log elem
298 //
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800299 enum match_type match = identical(elem, currentLast);
300 if (match != DIFFERENT) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800301 if (dropped) {
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800302 // Sum up liblog tag messages?
303 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
304 android_log_event_int_t* event =
305 reinterpret_cast<android_log_event_int_t*>(
306 const_cast<char*>(currentLast->getMsg()));
307 //
308 // To unit test, differentiate with something like:
309 // event->header.tag = htole32(CHATTY_LOG_TAG);
310 // here, then instead of delete currentLast below,
311 // log(currentLast) to see the incremental sums form.
312 //
313 uint32_t swab = event->payload.data;
314 unsigned long long total = htole32(swab);
315 event = reinterpret_cast<android_log_event_int_t*>(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800316 const_cast<char*>(elem->getMsg()));
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800317 swab = event->payload.data;
318
319 lastLoggedElements[LOG_ID_EVENTS] = elem;
320 total += htole32(swab);
321 // check for overflow
322 if (total >= UINT32_MAX) {
323 log(currentLast);
324 pthread_mutex_unlock(&mLogElementsLock);
325 return len;
326 }
327 stats.add(currentLast);
328 stats.subtract(currentLast);
329 delete currentLast;
330 swab = total;
331 event->payload.data = htole32(swab);
332 pthread_mutex_unlock(&mLogElementsLock);
333 return len;
334 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800335 if (count == USHRT_MAX) {
336 log(dropped);
337 count = 1;
338 } else {
339 delete dropped;
340 ++count;
341 }
342 }
343 if (count) {
344 stats.add(currentLast);
345 stats.subtract(currentLast);
346 currentLast->setDropped(count);
347 }
348 droppedElements[log_id] = currentLast;
349 lastLoggedElements[log_id] = elem;
350 pthread_mutex_unlock(&mLogElementsLock);
351 return len;
352 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800353 if (dropped) { // State 1 or 2
354 if (count) { // State 2
355 log(dropped); // report chatty
356 } else { // State 1
357 delete dropped;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800358 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800359 droppedElements[log_id] = NULL;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800360 log(currentLast); // report last message in the series
361 } else { // State 0
Mark Salyzyna2c02222016-12-13 10:31:29 -0800362 delete currentLast;
363 }
364 }
365 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800366
Mark Salyzyna2c02222016-12-13 10:31:29 -0800367 log(elem);
368 pthread_mutex_unlock(&mLogElementsLock);
369
370 return len;
371}
372
373// assumes mLogElementsLock held, owns elem, will look after garbage collection
374void LogBuffer::log(LogBufferElement* elem) {
Mark Salyzyn09d66322017-03-14 13:11:12 -0700375 // cap on how far back we will sort in-place, otherwise append
376 static uint32_t too_far_back = 5; // five seconds
Mark Salyzyn0175b072014-02-26 09:50:16 -0800377 // Insert elements in time sorted order if possible
378 // NB: if end is region locked, place element at end of list
379 LogBufferElementCollection::iterator it = mLogElements.end();
380 LogBufferElementCollection::iterator last = it;
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800381 if (__predict_true(it != mLogElements.begin())) --it;
382 if (__predict_false(it == mLogElements.begin()) ||
Mark Salyzyn09d66322017-03-14 13:11:12 -0700383 __predict_true((*it)->getRealTime() <= elem->getRealTime()) ||
384 __predict_false((((*it)->getRealTime().tv_sec - too_far_back) >
385 elem->getRealTime().tv_sec) &&
386 (elem->getLogId() != LOG_ID_KERNEL) &&
387 ((*it)->getLogId() != LOG_ID_KERNEL))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800388 mLogElements.push_back(elem);
389 } else {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800390 log_time end = log_time::EPOCH;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800391 bool end_set = false;
392 bool end_always = false;
393
394 LogTimeEntry::lock();
395
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700396 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800397 while (times != mTimes.end()) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800398 LogTimeEntry* entry = (*times);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800399 if (entry->owned_Locked()) {
400 if (!entry->mNonBlock) {
401 end_always = true;
402 break;
403 }
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800404 // it passing mEnd is blocked by the following checks.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800405 if (!end_set || (end <= entry->mEnd)) {
406 end = entry->mEnd;
407 end_set = true;
408 }
409 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700410 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800411 }
412
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800413 if (end_always || (end_set && (end > (*it)->getRealTime()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800414 mLogElements.push_back(elem);
415 } else {
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800416 // should be short as timestamps are localized near end()
417 do {
418 last = it;
419 if (__predict_false(it == mLogElements.begin())) {
420 break;
421 }
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800422 --it;
423 } while (((*it)->getRealTime() > elem->getRealTime()) &&
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800424 (!end_set || (end <= (*it)->getRealTime())));
Mark Salyzyna2c02222016-12-13 10:31:29 -0800425 mLogElements.insert(last, elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800426 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800427 LogTimeEntry::unlock();
428 }
429
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700430 stats.add(elem);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800431 maybePrune(elem->getLogId());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800432}
433
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700434// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800435//
436// mLogElementsLock must be held when this function is called.
437void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800438 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700439 unsigned long maxSize = log_buffer_size(id);
440 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700441 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700442 size_t elements = stats.realElements(id);
443 size_t minElements = elements / 100;
444 if (minElements < minPrune) {
445 minElements = minPrune;
446 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700447 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700448 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700449 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800450 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700451 if (pruneRows > maxPrune) {
452 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700453 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800454 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800455 }
456}
457
Mark Salyzyn831aa292015-09-03 16:08:50 -0700458LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800459 LogBufferElementCollection::iterator it, bool coalesce) {
460 LogBufferElement* element = *it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700461 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700462
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700463 // Remove iterator references in the various lists that will become stale
464 // after the element is erased from the main logging list.
465
Mark Salyzyn501c3732017-03-10 14:31:54 -0800466 { // start of scope for found iterator
467 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
468 ? element->getTag()
469 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700470 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
471 if ((found != mLastWorst[id].end()) && (it == found->second)) {
472 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700473 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700474 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700475
Mark Salyzyn501c3732017-03-10 14:31:54 -0800476 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700477 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700478 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
479 // long term code stability, find() check should be fast for those ids.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700480 LogBufferPidIteratorMap::iterator found =
481 mLastWorstPidOfSystem[id].find(element->getPid());
Mark Salyzyn501c3732017-03-10 14:31:54 -0800482 if ((found != mLastWorstPidOfSystem[id].end()) &&
483 (it == found->second)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700484 mLastWorstPidOfSystem[id].erase(found);
485 }
486 }
487
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800488 bool setLast[LOG_ID_MAX];
489 bool doSetLast = false;
490 log_id_for_each(i) {
491 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
492 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700493#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
494 LogBufferElementCollection::iterator bad = it;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800495 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
496 ? element->getTag()
497 : element->getUid();
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700498#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700499 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800500 if (doSetLast) {
501 log_id_for_each(i) {
502 if (setLast[i]) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800503 if (__predict_false(it == mLogElements.end())) { // impossible
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800504 mLastSet[i] = false;
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700505 mLast[i] = mLogElements.begin();
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800506 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800507 mLast[i] = it; // push down the road as next-best-watermark
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800508 }
509 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800510 }
511 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700512#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
513 log_id_for_each(i) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800514 for (auto b : mLastWorst[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700515 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800516 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i,
517 b.first, key);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700518 }
519 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800520 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700521 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800522 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i,
523 b.first);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700524 }
525 }
526 if (mLastSet[i] && (bad == mLast[i])) {
527 android::prdebug("stale mLast[%d]\n", i);
528 mLastSet[i] = false;
529 mLast[i] = mLogElements.begin();
530 }
531 }
532#endif
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700533 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700534 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700535 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700536 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700537 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700538 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700539
540 return it;
541}
542
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700543// Define a temporary mechanism to report the last LogBufferElement pointer
544// for the specified uid, pid and tid. Used below to help merge-sort when
545// pruning for worst UID.
546class LogBufferElementKey {
547 const union {
548 struct {
Mark Salyzyn684bdb52016-12-13 12:44:20 -0800549 uint32_t uid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700550 uint16_t pid;
551 uint16_t tid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700552 } __packed;
553 uint64_t value;
554 } __packed;
555
Mark Salyzyn501c3732017-03-10 14:31:54 -0800556 public:
557 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid)
558 : uid(uid), pid(pid), tid(tid) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700559 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800560 explicit LogBufferElementKey(uint64_t key) : value(key) {
561 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700562
Mark Salyzyn501c3732017-03-10 14:31:54 -0800563 uint64_t getKey() {
564 return value;
565 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700566};
567
Mark Salyzyn511338d2015-05-19 09:12:30 -0700568class LogBufferElementLast {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800569 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn511338d2015-05-19 09:12:30 -0700570 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700571
Mark Salyzyn501c3732017-03-10 14:31:54 -0800572 public:
573 bool coalesce(LogBufferElement* element, unsigned short dropped) {
574 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700575 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700576 LogBufferElementMap::iterator it = map.find(key.getKey());
577 if (it != map.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800578 LogBufferElement* found = it->second;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700579 unsigned short moreDropped = found->getDropped();
580 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700581 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700582 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700583 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700584 return true;
585 }
586 }
587 return false;
588 }
589
Mark Salyzyn501c3732017-03-10 14:31:54 -0800590 void add(LogBufferElement* element) {
591 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700592 element->getTid());
593 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700594 }
595
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700596 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700597 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700598 }
599
Mark Salyzyn501c3732017-03-10 14:31:54 -0800600 void clear(LogBufferElement* element) {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800601 log_time current =
602 element->getRealTime() - log_time(EXPIRE_RATELIMIT, 0);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800603 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
604 LogBufferElement* mapElement = it->second;
605 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD) &&
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800606 (current > mapElement->getRealTime())) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700607 it = map.erase(it);
608 } else {
609 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700610 }
611 }
612 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700613};
614
Mark Salyzyn0175b072014-02-26 09:50:16 -0800615// prune "pruneRows" of type "id" from the buffer.
616//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700617// This garbage collection task is used to expire log entries. It is called to
618// remove all logs (clear), all UID logs (unprivileged clear), or every
619// 256 or 10% of the total logs (whichever is less) to prune the logs.
620//
621// First there is a prep phase where we discover the reader region lock that
622// acts as a backstop to any pruning activity to stop there and go no further.
623//
624// There are three major pruning loops that follow. All expire from the oldest
625// entries. Since there are multiple log buffers, the Android logging facility
626// will appear to drop entries 'in the middle' when looking at multiple log
627// sources and buffers. This effect is slightly more prominent when we prune
628// the worst offender by logging source. Thus the logs slowly loose content
629// and value as you move back in time. This is preferred since chatty sources
630// invariably move the logs value down faster as less chatty sources would be
631// expired in the noise.
632//
633// The first loop performs blacklisting and worst offender pruning. Falling
634// through when there are no notable worst offenders and have not hit the
635// region lock preventing further worst offender pruning. This loop also looks
636// after managing the chatty log entries and merging to help provide
637// statistical basis for blame. The chatty entries are not a notification of
638// how much logs you may have, but instead represent how much logs you would
639// have had in a virtual log buffer that is extended to cover all the in-memory
640// logs without loss. They last much longer than the represented pruned logs
641// since they get multiplied by the gains in the non-chatty log sources.
642//
643// The second loop get complicated because an algorithm of watermarks and
644// history is maintained to reduce the order and keep processing time
645// down to a minimum at scale. These algorithms can be costly in the face
646// of larger log buffers, or severly limited processing time granted to a
647// background task at lowest priority.
648//
649// This second loop does straight-up expiration from the end of the logs
650// (again, remember for the specified log buffer id) but does some whitelist
651// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
652// spam filtration all take priority. This second loop also checks if a region
653// lock is causing us to buffer too much in the logs to help the reader(s),
654// and will tell the slowest reader thread to skip log entries, and if
655// persistent and hits a further threshold, kill the reader thread.
656//
657// The third thread is optional, and only gets hit if there was a whitelist
658// and more needs to be pruned against the backstop of the region lock.
659//
Mark Salyzyn0175b072014-02-26 09:50:16 -0800660// mLogElementsLock must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700661//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700662bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800663 LogTimeEntry* oldest = NULL;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700664 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700665 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800666
667 LogTimeEntry::lock();
668
669 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700670 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800671 while (times != mTimes.end()) {
672 LogTimeEntry* entry = (*times);
673 if (entry->owned_Locked() && entry->isWatching(id) &&
674 (!oldest || (oldest->mStart > entry->mStart) ||
675 ((oldest->mStart == entry->mStart) &&
676 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800677 oldest = entry;
678 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700679 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800680 }
681
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800682 LogBufferElementCollection::iterator it;
683
Mark Salyzyn501c3732017-03-10 14:31:54 -0800684 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700685 // Only here if clear all request from non system source, so chatty
686 // filter logistics is not required.
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800687 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
688 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800689 LogBufferElement* element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700690
Mark Salyzyn501c3732017-03-10 14:31:54 -0800691 if ((element->getLogId() != id) ||
692 (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700693 ++it;
694 continue;
695 }
696
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800697 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
698 mLast[id] = it;
699 mLastSet[id] = true;
700 }
701
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800702 if (oldest && (oldest->mStart <= element->getRealTime().nsec())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700703 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800704 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
705 oldest->triggerReader_Locked();
706 } else {
707 oldest->triggerSkip_Locked(id, pruneRows);
708 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700709 break;
710 }
711
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700712 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700713 if (--pruneRows == 0) {
714 break;
715 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700716 }
717 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700718 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700719 }
720
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700721 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn083b0372015-12-04 10:59:45 -0800722 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700723 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800724 // recalculate the worst offender on every batched pass
Mark Salyzyn501c3732017-03-10 14:31:54 -0800725 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800726 size_t worst_sizes = 0;
727 size_t second_worst_sizes = 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800728 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800729
Mark Salyzynae769232015-03-17 17:17:25 -0700730 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700731 // Calculate threshold as 12.5% of available storage
732 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700733
Mark Salyzyn6a066942016-07-14 15:34:30 -0700734 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800735 stats.sortTags(AID_ROOT, (pid_t)0, 2, id)
736 .findWorst(worst, worst_sizes, second_worst_sizes,
737 threshold);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700738 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700739 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800740 stats.sort(AID_ROOT, (pid_t)0, 2, id)
741 .findWorst(worst, worst_sizes, second_worst_sizes,
742 threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700743
Mark Salyzyn6a066942016-07-14 15:34:30 -0700744 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800745 stats.sortPids(worst, (pid_t)0, 2, id)
746 .findWorst(worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700747 }
748 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800749 }
750
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700751 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700752 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700753 break;
754 }
755
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800756 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700757 bool leading = true;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800758 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700759 // Perform at least one mandatory garbage collection cycle in following
760 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700761 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700762 // - check age-out of preserved logs
763 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700764 if (!gc && (worst != -1)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800765 { // begin scope for worst found iterator
766 LogBufferIteratorMap::iterator found =
767 mLastWorst[id].find(worst);
768 if ((found != mLastWorst[id].end()) &&
769 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700770 leading = false;
771 it = found->second;
772 }
773 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800774 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700775 // FYI: worstPid only set if !LOG_ID_EVENTS and
776 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzyn501c3732017-03-10 14:31:54 -0800777 LogBufferPidIteratorMap::iterator found =
778 mLastWorstPidOfSystem[id].find(worstPid);
779 if ((found != mLastWorstPidOfSystem[id].end()) &&
780 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700781 leading = false;
782 it = found->second;
783 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700784 }
785 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800786 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzynccfe8442015-08-24 13:43:27 -0700787 LogBufferElementCollection::iterator lastt;
788 lastt = mLogElements.end();
789 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700790 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700791 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800792 LogBufferElement* element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800793
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800794 if (oldest && (oldest->mStart <= element->getRealTime().nsec())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700795 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800796 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
797 oldest->triggerReader_Locked();
798 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800799 break;
800 }
801
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700802 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800803 ++it;
804 continue;
805 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700806 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800807
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800808 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
809 mLast[id] = it;
810 mLastSet[id] = true;
811 }
812
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700813 unsigned short dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800814
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700815 // remove any leading drops
816 if (leading && dropped) {
817 it = erase(it);
818 continue;
819 }
820
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700821 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700822 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700823 continue;
824 }
825
Mark Salyzyn501c3732017-03-10 14:31:54 -0800826 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
827 ? element->getTag()
828 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700829
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700830 if (hasBlacklist && mPrune.naughty(element)) {
831 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700832 it = erase(it);
833 if (dropped) {
834 continue;
835 }
836
837 pruneRows--;
838 if (pruneRows == 0) {
839 break;
840 }
841
Mark Salyzyn6a066942016-07-14 15:34:30 -0700842 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700843 kick = true;
844 if (worst_sizes < second_worst_sizes) {
845 break;
846 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700847 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700848 }
849 continue;
850 }
851
Mark Salyzyn501c3732017-03-10 14:31:54 -0800852 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
853 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700854 break;
855 }
856
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700857 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700858 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800859 if (worstPid &&
860 ((!gc && (element->getPid() == worstPid)) ||
861 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
862 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700863 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700864 // watermark if current one empty. id is not LOG_ID_EVENTS
865 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700866 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700867 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800868 if ((!gc && !worstPid && (key == worst)) ||
869 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700870 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700871 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800872 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700873 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800874 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700875
Mark Salyzyn501c3732017-03-10 14:31:54 -0800876 if ((key != worst) ||
877 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700878 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700879 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700880 ++it;
881 continue;
882 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700883 // key == worst below here
884 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700885
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700886 pruneRows--;
887 if (pruneRows == 0) {
888 break;
889 }
890
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700891 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700892
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700893 unsigned short len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700894
895 // do not create any leading drops
896 if (leading) {
897 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700898 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700899 stats.drop(element);
900 element->setDropped(1);
901 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700902 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700903 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700904 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800905 if (worstPid &&
906 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
907 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700908 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700909 // watermark if current one empty. id is not
910 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700911 mLastWorstPidOfSystem[id][worstPid] = it;
912 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700913 if ((!gc && !worstPid) ||
Mark Salyzyn501c3732017-03-10 14:31:54 -0800914 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700915 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700916 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700917 ++it;
918 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700919 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700920 if (worst_sizes < second_worst_sizes) {
921 break;
922 }
923 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800924 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700925 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800926
Mark Salyzyn1c950472014-04-01 17:19:47 -0700927 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800928 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800929 }
930 }
931
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800932 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800933 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800934 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800935 while ((pruneRows > 0) && (it != mLogElements.end())) {
936 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700937
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700938 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700939 it++;
940 continue;
941 }
942
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800943 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
944 mLast[id] = it;
945 mLastSet[id] = true;
946 }
947
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800948 if (oldest && (oldest->mStart <= element->getRealTime().nsec())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700949 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700950 if (whitelist) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800951 break;
952 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700953
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700954 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
955 // kick a misbehaving log reader client off the island
956 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800957 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
958 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700959 } else {
960 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800961 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700962 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800963 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700964
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700965 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
966 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700967 whitelist = true;
968 it++;
969 continue;
970 }
971
972 it = erase(it);
973 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800974 }
975
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700976 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800977 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800978 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800979 while ((it != mLogElements.end()) && (pruneRows > 0)) {
980 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700981
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700982 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700983 ++it;
984 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800985 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700986
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800987 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
988 mLast[id] = it;
989 mLastSet[id] = true;
990 }
991
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800992 if (oldest && (oldest->mStart <= element->getRealTime().nsec())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700993 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700994 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
995 // kick a misbehaving log reader client off the island
996 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800997 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
998 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700999 } else {
1000 oldest->triggerSkip_Locked(id, pruneRows);
1001 }
1002 break;
1003 }
1004
1005 it = erase(it);
1006 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001007 }
1008 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001009
Mark Salyzyn0175b072014-02-26 09:50:16 -08001010 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001011
1012 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001013}
1014
1015// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -07001016bool LogBuffer::clear(log_id_t id, uid_t uid) {
1017 bool busy = true;
1018 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
1019 for (int retry = 4;;) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001020 if (retry == 1) { // last pass
Mark Salyzync5dc9702015-09-16 15:34:00 -07001021 // Check if it is still busy after the sleep, we say prune
1022 // one entry, not another clear run, so we are looking for
1023 // the quick side effect of the return value to tell us if
1024 // we have a _blocked_ reader.
1025 pthread_mutex_lock(&mLogElementsLock);
1026 busy = prune(id, 1, uid);
1027 pthread_mutex_unlock(&mLogElementsLock);
1028 // It is still busy, blocked reader(s), lets kill them all!
1029 // otherwise, lets be a good citizen and preserve the slow
1030 // readers and let the clear run (below) deal with determining
1031 // if we are still blocked and return an error code to caller.
1032 if (busy) {
1033 LogTimeEntry::lock();
1034 LastLogTimes::iterator times = mTimes.begin();
1035 while (times != mTimes.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001036 LogTimeEntry* entry = (*times);
Mark Salyzync5dc9702015-09-16 15:34:00 -07001037 // Killer punch
1038 if (entry->owned_Locked() && entry->isWatching(id)) {
1039 entry->release_Locked();
1040 }
1041 times++;
1042 }
1043 LogTimeEntry::unlock();
1044 }
1045 }
1046 pthread_mutex_lock(&mLogElementsLock);
1047 busy = prune(id, ULONG_MAX, uid);
1048 pthread_mutex_unlock(&mLogElementsLock);
1049 if (!busy || !--retry) {
1050 break;
1051 }
Mark Salyzyn501c3732017-03-10 14:31:54 -08001052 sleep(1); // Let reader(s) catch up after notification
Mark Salyzync5dc9702015-09-16 15:34:00 -07001053 }
1054 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001055}
1056
1057// get the used space associated with "id".
1058unsigned long LogBuffer::getSizeUsed(log_id_t id) {
1059 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001060 size_t retval = stats.sizes(id);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001061 pthread_mutex_unlock(&mLogElementsLock);
1062 return retval;
1063}
1064
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001065// set the total space allocated to "id"
1066int LogBuffer::setSize(log_id_t id, unsigned long size) {
1067 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -07001068 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001069 return -1;
1070 }
1071 pthread_mutex_lock(&mLogElementsLock);
1072 log_buffer_size(id) = size;
1073 pthread_mutex_unlock(&mLogElementsLock);
1074 return 0;
1075}
1076
1077// get the total space allocated to "id"
1078unsigned long LogBuffer::getSize(log_id_t id) {
1079 pthread_mutex_lock(&mLogElementsLock);
1080 size_t retval = log_buffer_size(id);
1081 pthread_mutex_unlock(&mLogElementsLock);
1082 return retval;
1083}
1084
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -08001085log_time LogBuffer::flushTo(
1086 SocketClient* reader, const log_time& start, bool privileged, bool security,
Mark Salyzyn501c3732017-03-10 14:31:54 -08001087 int (*filter)(const LogBufferElement* element, void* arg), void* arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001088 LogBufferElementCollection::iterator it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001089 uid_t uid = reader->getUid();
1090
1091 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001092
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -08001093 if (start == log_time::EPOCH) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001094 // client wants to start from the beginning
1095 it = mLogElements.begin();
1096 } else {
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001097 LogBufferElementCollection::iterator last = mLogElements.begin();
1098 // 30 second limit to continue search for out-of-order entries.
1099 log_time min = start - log_time(30, 0);
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001100 // Client wants to start from some specified time. Chances are
1101 // we are better off starting from the end of the time sorted list.
Mark Salyzyn501c3732017-03-10 14:31:54 -08001102 for (it = mLogElements.end(); it != mLogElements.begin();
1103 /* do nothing */) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001104 --it;
Mark Salyzyn501c3732017-03-10 14:31:54 -08001105 LogBufferElement* element = *it;
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001106 if (element->getRealTime() > start) {
1107 last = it;
1108 } else if (element->getRealTime() < min) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001109 break;
1110 }
1111 }
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001112 it = last;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001113 }
1114
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001115 log_time max = start;
Mark Salyzynb5b87962017-01-23 14:20:31 -08001116 // Help detect if the valid message before is from the same source so
1117 // we can differentiate chatty filter types.
1118 pid_t lastTid[LOG_ID_MAX] = { 0 };
1119
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001120 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001121 LogBufferElement* element = *it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001122
1123 if (!privileged && (element->getUid() != uid)) {
1124 continue;
1125 }
1126
Mark Salyzyn8fa88962016-01-26 14:32:35 -08001127 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1128 continue;
1129 }
1130
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -08001131 if (element->getRealTime() <= start) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001132 continue;
1133 }
1134
1135 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001136 if (filter) {
1137 int ret = (*filter)(element, arg);
1138 if (ret == false) {
1139 continue;
1140 }
1141 if (ret != true) {
1142 break;
1143 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001144 }
1145
Mark Salyzynb5b87962017-01-23 14:20:31 -08001146 bool sameTid = lastTid[element->getLogId()] == element->getTid();
1147 // Dropped (chatty) immediately following a valid log from the
1148 // same source in the same log buffer indicates we have a
1149 // multiple identical squash. chatty that differs source
1150 // is due to spam filter. chatty to chatty of different
1151 // source is also due to spam filter.
Mark Salyzyn501c3732017-03-10 14:31:54 -08001152 lastTid[element->getLogId()] =
1153 (element->getDropped() && !sameTid) ? 0 : element->getTid();
Mark Salyzynb5b87962017-01-23 14:20:31 -08001154
Mark Salyzyn0175b072014-02-26 09:50:16 -08001155 pthread_mutex_unlock(&mLogElementsLock);
1156
1157 // range locking in LastLogTimes looks after us
Mark Salyzynb5b87962017-01-23 14:20:31 -08001158 max = element->flushTo(reader, this, privileged, sameTid);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001159
1160 if (max == element->FLUSH_ERROR) {
1161 return max;
1162 }
1163
1164 pthread_mutex_lock(&mLogElementsLock);
1165 }
1166 pthread_mutex_unlock(&mLogElementsLock);
1167
1168 return max;
1169}
Mark Salyzyn34facab2014-02-06 14:48:50 -08001170
Mark Salyzynee3b8382015-12-17 09:58:43 -08001171std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1172 unsigned int logMask) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001173 pthread_mutex_lock(&mLogElementsLock);
1174
Mark Salyzynee3b8382015-12-17 09:58:43 -08001175 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001176
1177 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn73160ac2015-08-20 10:01:44 -07001178
1179 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001180}