blob: 352fc187fb4d0353b0793a254b474ca2a3b2799a [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)) &&
160 (elem->getTag() == LIBLOG_LOG_TAG))
161 return SAME_LIBLOG;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800162 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800163
164 // audit message (except sequence number) identical?
165 static const char avc[] = "): avc: ";
166
167 if (last->isBinary()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800168 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) -
169 sizeof(int32_t)))
170 return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800171 msgl += sizeof(android_log_event_string_t);
172 lenl -= sizeof(android_log_event_string_t);
173 msgr += sizeof(android_log_event_string_t);
174 lenr -= sizeof(android_log_event_string_t);
175 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800176 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800177 if (!avcl) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800178 lenl -= avcl - msgl;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800179 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800180 if (!avcr) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800181 lenr -= avcr - msgr;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800182 if (lenl != lenr) return DIFFERENT;
Evgenii Stepanov03fc2fe2017-03-14 14:47:25 -0700183 // TODO: After b/35468874 is addressed, revisit "lenl > strlen(avc)"
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800184 // condition, it might become superfluous.
Evgenii Stepanov03fc2fe2017-03-14 14:47:25 -0700185 if (lenl > strlen(avc) &&
186 fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc),
187 lenl - strlen(avc))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800188 return DIFFERENT;
Evgenii Stepanov03fc2fe2017-03-14 14:47:25 -0700189 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800190 return SAME;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800191}
192
Mark Salyzyn501c3732017-03-10 14:31:54 -0800193int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
194 pid_t tid, const char* msg, unsigned short len) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800195 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800196 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800197 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700198
Mark Salyzyn501c3732017-03-10 14:31:54 -0800199 LogBufferElement* elem =
200 new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000201 if (log_id != LOG_ID_SECURITY) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800202 int prio = ANDROID_LOG_INFO;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800203 const char* tag = NULL;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800204 if (log_id == LOG_ID_EVENTS) {
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700205 tag = tagToName(elem->getTag());
Mark Salyzyn083b0372015-12-04 10:59:45 -0800206 } else {
207 prio = *msg;
208 tag = msg + 1;
209 }
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700210 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800211 // Log traffic received to total
212 pthread_mutex_lock(&mLogElementsLock);
213 stats.add(elem);
214 stats.subtract(elem);
215 pthread_mutex_unlock(&mLogElementsLock);
216 delete elem;
217 return -EACCES;
218 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700219 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800220
221 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800222 LogBufferElement* currentLast = lastLoggedElements[log_id];
223 if (currentLast) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800224 LogBufferElement* dropped = droppedElements[log_id];
Mark Salyzyna2c02222016-12-13 10:31:29 -0800225 unsigned short count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800226 //
227 // State Init
228 // incoming:
229 // dropped = NULL
230 // currentLast = NULL;
231 // elem = incoming message
232 // outgoing:
233 // dropped = NULL -> State 0
234 // currentLast = copy of elem
235 // log elem
236 // State 0
237 // incoming:
238 // count = 0
239 // dropped = NULL
240 // currentLast = copy of last message
241 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800242 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800243 // dropped = copy of first identical message -> State 1
244 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800245 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800246 // dropped = NULL -> State 0
247 // delete copy of last message (incoming currentLast)
248 // currentLast = copy of elem
249 // log elem
250 // State 1
251 // incoming:
252 // count = 0
253 // dropped = copy of first identical message
254 // currentLast = reference to last held-back incoming
255 // message
256 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800257 // outgoing: if match == SAME
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800258 // delete copy of first identical message (dropped)
259 // dropped = reference to last held-back incoming
260 // message set to chatty count of 1 -> State 2
261 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800262 // outgoing: if match == SAME_LIBLOG
263 // dropped = copy of first identical message -> State 1
264 // take sum of currentLast and elem
265 // if sum overflows:
266 // log currentLast
267 // currentLast = reference to elem
268 // else
269 // delete currentLast
270 // currentLast = reference to elem, sum liblog.
271 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800272 // delete dropped
273 // dropped = NULL -> State 0
274 // log reference to last held-back (currentLast)
275 // currentLast = copy of elem
276 // log elem
277 // State 2
278 // incoming:
279 // count = chatty count
280 // dropped = chatty message holding count
281 // currentLast = reference to last held-back incoming
282 // message.
283 // dropped = chatty message holding count
284 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800285 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800286 // delete chatty message holding count
287 // dropped = reference to last held-back incoming
288 // message, set to chatty count + 1
289 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800290 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800291 // log dropped (chatty message)
292 // dropped = NULL -> State 0
293 // log reference to last held-back (currentLast)
294 // currentLast = copy of elem
295 // log elem
296 //
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800297 enum match_type match = identical(elem, currentLast);
298 if (match != DIFFERENT) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800299 if (dropped) {
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800300 // Sum up liblog tag messages?
301 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
302 android_log_event_int_t* event =
303 reinterpret_cast<android_log_event_int_t*>(
304 const_cast<char*>(currentLast->getMsg()));
305 //
306 // To unit test, differentiate with something like:
307 // event->header.tag = htole32(CHATTY_LOG_TAG);
308 // here, then instead of delete currentLast below,
309 // log(currentLast) to see the incremental sums form.
310 //
311 uint32_t swab = event->payload.data;
312 unsigned long long total = htole32(swab);
313 event = reinterpret_cast<android_log_event_int_t*>(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800314 const_cast<char*>(elem->getMsg()));
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800315 swab = event->payload.data;
316
317 lastLoggedElements[LOG_ID_EVENTS] = elem;
318 total += htole32(swab);
319 // check for overflow
320 if (total >= UINT32_MAX) {
321 log(currentLast);
322 pthread_mutex_unlock(&mLogElementsLock);
323 return len;
324 }
325 stats.add(currentLast);
326 stats.subtract(currentLast);
327 delete currentLast;
328 swab = total;
329 event->payload.data = htole32(swab);
330 pthread_mutex_unlock(&mLogElementsLock);
331 return len;
332 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800333 if (count == USHRT_MAX) {
334 log(dropped);
335 count = 1;
336 } else {
337 delete dropped;
338 ++count;
339 }
340 }
341 if (count) {
342 stats.add(currentLast);
343 stats.subtract(currentLast);
344 currentLast->setDropped(count);
345 }
346 droppedElements[log_id] = currentLast;
347 lastLoggedElements[log_id] = elem;
348 pthread_mutex_unlock(&mLogElementsLock);
349 return len;
350 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800351 if (dropped) { // State 1 or 2
352 if (count) { // State 2
353 log(dropped); // report chatty
354 } else { // State 1
355 delete dropped;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800356 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800357 droppedElements[log_id] = NULL;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800358 log(currentLast); // report last message in the series
359 } else { // State 0
Mark Salyzyna2c02222016-12-13 10:31:29 -0800360 delete currentLast;
361 }
362 }
363 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800364
Mark Salyzyna2c02222016-12-13 10:31:29 -0800365 log(elem);
366 pthread_mutex_unlock(&mLogElementsLock);
367
368 return len;
369}
370
371// assumes mLogElementsLock held, owns elem, will look after garbage collection
372void LogBuffer::log(LogBufferElement* elem) {
Mark Salyzyn09d66322017-03-14 13:11:12 -0700373 // cap on how far back we will sort in-place, otherwise append
374 static uint32_t too_far_back = 5; // five seconds
Mark Salyzyn0175b072014-02-26 09:50:16 -0800375 // Insert elements in time sorted order if possible
376 // NB: if end is region locked, place element at end of list
377 LogBufferElementCollection::iterator it = mLogElements.end();
378 LogBufferElementCollection::iterator last = it;
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800379 if (__predict_true(it != mLogElements.begin())) --it;
380 if (__predict_false(it == mLogElements.begin()) ||
Mark Salyzyn09d66322017-03-14 13:11:12 -0700381 __predict_true((*it)->getRealTime() <= elem->getRealTime()) ||
382 __predict_false((((*it)->getRealTime().tv_sec - too_far_back) >
383 elem->getRealTime().tv_sec) &&
384 (elem->getLogId() != LOG_ID_KERNEL) &&
385 ((*it)->getLogId() != LOG_ID_KERNEL))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800386 mLogElements.push_back(elem);
387 } else {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800388 log_time end = log_time::EPOCH;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800389 bool end_set = false;
390 bool end_always = false;
391
392 LogTimeEntry::lock();
393
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700394 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800395 while (times != mTimes.end()) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800396 LogTimeEntry* entry = (*times);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800397 if (entry->owned_Locked()) {
398 if (!entry->mNonBlock) {
399 end_always = true;
400 break;
401 }
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800402 // it passing mEnd is blocked by the following checks.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800403 if (!end_set || (end <= entry->mEnd)) {
404 end = entry->mEnd;
405 end_set = true;
406 }
407 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700408 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800409 }
410
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800411 if (end_always || (end_set && (end > (*it)->getRealTime()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800412 mLogElements.push_back(elem);
413 } else {
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800414 // should be short as timestamps are localized near end()
415 do {
416 last = it;
417 if (__predict_false(it == mLogElements.begin())) {
418 break;
419 }
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800420 --it;
421 } while (((*it)->getRealTime() > elem->getRealTime()) &&
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800422 (!end_set || (end <= (*it)->getRealTime())));
Mark Salyzyna2c02222016-12-13 10:31:29 -0800423 mLogElements.insert(last, elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800424 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800425 LogTimeEntry::unlock();
426 }
427
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700428 stats.add(elem);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800429 maybePrune(elem->getLogId());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800430}
431
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700432// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800433//
434// mLogElementsLock must be held when this function is called.
435void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800436 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700437 unsigned long maxSize = log_buffer_size(id);
438 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700439 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700440 size_t elements = stats.realElements(id);
441 size_t minElements = elements / 100;
442 if (minElements < minPrune) {
443 minElements = minPrune;
444 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700445 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700446 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700447 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800448 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700449 if (pruneRows > maxPrune) {
450 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700451 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800452 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800453 }
454}
455
Mark Salyzyn831aa292015-09-03 16:08:50 -0700456LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800457 LogBufferElementCollection::iterator it, bool coalesce) {
458 LogBufferElement* element = *it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700459 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700460
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700461 // Remove iterator references in the various lists that will become stale
462 // after the element is erased from the main logging list.
463
Mark Salyzyn501c3732017-03-10 14:31:54 -0800464 { // start of scope for found iterator
465 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
466 ? element->getTag()
467 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700468 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
469 if ((found != mLastWorst[id].end()) && (it == found->second)) {
470 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700471 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700472 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700473
Mark Salyzyn501c3732017-03-10 14:31:54 -0800474 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700475 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700476 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
477 // long term code stability, find() check should be fast for those ids.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700478 LogBufferPidIteratorMap::iterator found =
479 mLastWorstPidOfSystem[id].find(element->getPid());
Mark Salyzyn501c3732017-03-10 14:31:54 -0800480 if ((found != mLastWorstPidOfSystem[id].end()) &&
481 (it == found->second)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700482 mLastWorstPidOfSystem[id].erase(found);
483 }
484 }
485
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800486 bool setLast[LOG_ID_MAX];
487 bool doSetLast = false;
488 log_id_for_each(i) {
489 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
490 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700491#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
492 LogBufferElementCollection::iterator bad = it;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800493 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
494 ? element->getTag()
495 : element->getUid();
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700496#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700497 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800498 if (doSetLast) {
499 log_id_for_each(i) {
500 if (setLast[i]) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800501 if (__predict_false(it == mLogElements.end())) { // impossible
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800502 mLastSet[i] = false;
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700503 mLast[i] = mLogElements.begin();
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800504 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800505 mLast[i] = it; // push down the road as next-best-watermark
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800506 }
507 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800508 }
509 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700510#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
511 log_id_for_each(i) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800512 for (auto b : mLastWorst[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700513 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800514 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i,
515 b.first, key);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700516 }
517 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800518 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700519 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800520 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i,
521 b.first);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700522 }
523 }
524 if (mLastSet[i] && (bad == mLast[i])) {
525 android::prdebug("stale mLast[%d]\n", i);
526 mLastSet[i] = false;
527 mLast[i] = mLogElements.begin();
528 }
529 }
530#endif
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700531 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700532 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700533 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700534 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700535 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700536 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700537
538 return it;
539}
540
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700541// Define a temporary mechanism to report the last LogBufferElement pointer
542// for the specified uid, pid and tid. Used below to help merge-sort when
543// pruning for worst UID.
544class LogBufferElementKey {
545 const union {
546 struct {
Mark Salyzyn684bdb52016-12-13 12:44:20 -0800547 uint32_t uid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700548 uint16_t pid;
549 uint16_t tid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700550 } __packed;
551 uint64_t value;
552 } __packed;
553
Mark Salyzyn501c3732017-03-10 14:31:54 -0800554 public:
555 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid)
556 : uid(uid), pid(pid), tid(tid) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700557 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800558 explicit LogBufferElementKey(uint64_t key) : value(key) {
559 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700560
Mark Salyzyn501c3732017-03-10 14:31:54 -0800561 uint64_t getKey() {
562 return value;
563 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700564};
565
Mark Salyzyn511338d2015-05-19 09:12:30 -0700566class LogBufferElementLast {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800567 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn511338d2015-05-19 09:12:30 -0700568 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700569
Mark Salyzyn501c3732017-03-10 14:31:54 -0800570 public:
571 bool coalesce(LogBufferElement* element, unsigned short dropped) {
572 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700573 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700574 LogBufferElementMap::iterator it = map.find(key.getKey());
575 if (it != map.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800576 LogBufferElement* found = it->second;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700577 unsigned short moreDropped = found->getDropped();
578 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700579 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700580 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700581 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700582 return true;
583 }
584 }
585 return false;
586 }
587
Mark Salyzyn501c3732017-03-10 14:31:54 -0800588 void add(LogBufferElement* element) {
589 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700590 element->getTid());
591 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700592 }
593
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700594 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700595 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700596 }
597
Mark Salyzyn501c3732017-03-10 14:31:54 -0800598 void clear(LogBufferElement* element) {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800599 log_time current =
600 element->getRealTime() - log_time(EXPIRE_RATELIMIT, 0);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800601 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
602 LogBufferElement* mapElement = it->second;
603 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD) &&
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800604 (current > mapElement->getRealTime())) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700605 it = map.erase(it);
606 } else {
607 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700608 }
609 }
610 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700611};
612
Mark Salyzyn0175b072014-02-26 09:50:16 -0800613// prune "pruneRows" of type "id" from the buffer.
614//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700615// This garbage collection task is used to expire log entries. It is called to
616// remove all logs (clear), all UID logs (unprivileged clear), or every
617// 256 or 10% of the total logs (whichever is less) to prune the logs.
618//
619// First there is a prep phase where we discover the reader region lock that
620// acts as a backstop to any pruning activity to stop there and go no further.
621//
622// There are three major pruning loops that follow. All expire from the oldest
623// entries. Since there are multiple log buffers, the Android logging facility
624// will appear to drop entries 'in the middle' when looking at multiple log
625// sources and buffers. This effect is slightly more prominent when we prune
626// the worst offender by logging source. Thus the logs slowly loose content
627// and value as you move back in time. This is preferred since chatty sources
628// invariably move the logs value down faster as less chatty sources would be
629// expired in the noise.
630//
631// The first loop performs blacklisting and worst offender pruning. Falling
632// through when there are no notable worst offenders and have not hit the
633// region lock preventing further worst offender pruning. This loop also looks
634// after managing the chatty log entries and merging to help provide
635// statistical basis for blame. The chatty entries are not a notification of
636// how much logs you may have, but instead represent how much logs you would
637// have had in a virtual log buffer that is extended to cover all the in-memory
638// logs without loss. They last much longer than the represented pruned logs
639// since they get multiplied by the gains in the non-chatty log sources.
640//
641// The second loop get complicated because an algorithm of watermarks and
642// history is maintained to reduce the order and keep processing time
643// down to a minimum at scale. These algorithms can be costly in the face
644// of larger log buffers, or severly limited processing time granted to a
645// background task at lowest priority.
646//
647// This second loop does straight-up expiration from the end of the logs
648// (again, remember for the specified log buffer id) but does some whitelist
649// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
650// spam filtration all take priority. This second loop also checks if a region
651// lock is causing us to buffer too much in the logs to help the reader(s),
652// and will tell the slowest reader thread to skip log entries, and if
653// persistent and hits a further threshold, kill the reader thread.
654//
655// The third thread is optional, and only gets hit if there was a whitelist
656// and more needs to be pruned against the backstop of the region lock.
657//
Mark Salyzyn0175b072014-02-26 09:50:16 -0800658// mLogElementsLock must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700659//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700660bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800661 LogTimeEntry* oldest = NULL;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700662 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700663 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800664
665 LogTimeEntry::lock();
666
667 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700668 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800669 while (times != mTimes.end()) {
670 LogTimeEntry* entry = (*times);
671 if (entry->owned_Locked() && entry->isWatching(id) &&
672 (!oldest || (oldest->mStart > entry->mStart) ||
673 ((oldest->mStart == entry->mStart) &&
674 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800675 oldest = entry;
676 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700677 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800678 }
679
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800680 LogBufferElementCollection::iterator it;
681
Mark Salyzyn501c3732017-03-10 14:31:54 -0800682 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700683 // Only here if clear all request from non system source, so chatty
684 // filter logistics is not required.
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800685 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
686 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800687 LogBufferElement* element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700688
Mark Salyzyn501c3732017-03-10 14:31:54 -0800689 if ((element->getLogId() != id) ||
690 (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700691 ++it;
692 continue;
693 }
694
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800695 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
696 mLast[id] = it;
697 mLastSet[id] = true;
698 }
699
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800700 if (oldest && (oldest->mStart <= element->getRealTime().nsec())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700701 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800702 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
703 oldest->triggerReader_Locked();
704 } else {
705 oldest->triggerSkip_Locked(id, pruneRows);
706 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700707 break;
708 }
709
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700710 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700711 if (--pruneRows == 0) {
712 break;
713 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700714 }
715 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700716 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700717 }
718
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700719 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn083b0372015-12-04 10:59:45 -0800720 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700721 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800722 // recalculate the worst offender on every batched pass
Mark Salyzyn501c3732017-03-10 14:31:54 -0800723 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800724 size_t worst_sizes = 0;
725 size_t second_worst_sizes = 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800726 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800727
Mark Salyzynae769232015-03-17 17:17:25 -0700728 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700729 // Calculate threshold as 12.5% of available storage
730 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700731
Mark Salyzyn6a066942016-07-14 15:34:30 -0700732 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800733 stats.sortTags(AID_ROOT, (pid_t)0, 2, id)
734 .findWorst(worst, worst_sizes, second_worst_sizes,
735 threshold);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700736 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700737 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800738 stats.sort(AID_ROOT, (pid_t)0, 2, id)
739 .findWorst(worst, worst_sizes, second_worst_sizes,
740 threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700741
Mark Salyzyn6a066942016-07-14 15:34:30 -0700742 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800743 stats.sortPids(worst, (pid_t)0, 2, id)
744 .findWorst(worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700745 }
746 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800747 }
748
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700749 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700750 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700751 break;
752 }
753
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800754 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700755 bool leading = true;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800756 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700757 // Perform at least one mandatory garbage collection cycle in following
758 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700759 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700760 // - check age-out of preserved logs
761 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700762 if (!gc && (worst != -1)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800763 { // begin scope for worst found iterator
764 LogBufferIteratorMap::iterator found =
765 mLastWorst[id].find(worst);
766 if ((found != mLastWorst[id].end()) &&
767 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700768 leading = false;
769 it = found->second;
770 }
771 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800772 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700773 // FYI: worstPid only set if !LOG_ID_EVENTS and
774 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzyn501c3732017-03-10 14:31:54 -0800775 LogBufferPidIteratorMap::iterator found =
776 mLastWorstPidOfSystem[id].find(worstPid);
777 if ((found != mLastWorstPidOfSystem[id].end()) &&
778 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700779 leading = false;
780 it = found->second;
781 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700782 }
783 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800784 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzynccfe8442015-08-24 13:43:27 -0700785 LogBufferElementCollection::iterator lastt;
786 lastt = mLogElements.end();
787 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700788 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700789 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800790 LogBufferElement* element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800791
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800792 if (oldest && (oldest->mStart <= element->getRealTime().nsec())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700793 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800794 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
795 oldest->triggerReader_Locked();
796 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800797 break;
798 }
799
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700800 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800801 ++it;
802 continue;
803 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700804 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800805
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800806 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
807 mLast[id] = it;
808 mLastSet[id] = true;
809 }
810
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700811 unsigned short dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800812
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700813 // remove any leading drops
814 if (leading && dropped) {
815 it = erase(it);
816 continue;
817 }
818
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700819 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700820 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700821 continue;
822 }
823
Mark Salyzyn501c3732017-03-10 14:31:54 -0800824 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
825 ? element->getTag()
826 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700827
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700828 if (hasBlacklist && mPrune.naughty(element)) {
829 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700830 it = erase(it);
831 if (dropped) {
832 continue;
833 }
834
835 pruneRows--;
836 if (pruneRows == 0) {
837 break;
838 }
839
Mark Salyzyn6a066942016-07-14 15:34:30 -0700840 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700841 kick = true;
842 if (worst_sizes < second_worst_sizes) {
843 break;
844 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700845 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700846 }
847 continue;
848 }
849
Mark Salyzyn501c3732017-03-10 14:31:54 -0800850 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
851 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700852 break;
853 }
854
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700855 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700856 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800857 if (worstPid &&
858 ((!gc && (element->getPid() == worstPid)) ||
859 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
860 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700861 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700862 // watermark if current one empty. id is not LOG_ID_EVENTS
863 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700864 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700865 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800866 if ((!gc && !worstPid && (key == worst)) ||
867 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700868 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700869 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800870 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700871 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800872 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700873
Mark Salyzyn501c3732017-03-10 14:31:54 -0800874 if ((key != worst) ||
875 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700876 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700877 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700878 ++it;
879 continue;
880 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700881 // key == worst below here
882 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700883
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700884 pruneRows--;
885 if (pruneRows == 0) {
886 break;
887 }
888
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700889 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700890
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700891 unsigned short len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700892
893 // do not create any leading drops
894 if (leading) {
895 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700896 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700897 stats.drop(element);
898 element->setDropped(1);
899 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700900 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700901 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700902 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800903 if (worstPid &&
904 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
905 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700906 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700907 // watermark if current one empty. id is not
908 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700909 mLastWorstPidOfSystem[id][worstPid] = it;
910 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700911 if ((!gc && !worstPid) ||
Mark Salyzyn501c3732017-03-10 14:31:54 -0800912 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700913 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700914 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700915 ++it;
916 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700917 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700918 if (worst_sizes < second_worst_sizes) {
919 break;
920 }
921 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800922 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700923 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800924
Mark Salyzyn1c950472014-04-01 17:19:47 -0700925 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800926 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800927 }
928 }
929
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800930 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800931 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800932 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800933 while ((pruneRows > 0) && (it != mLogElements.end())) {
934 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700935
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700936 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700937 it++;
938 continue;
939 }
940
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800941 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
942 mLast[id] = it;
943 mLastSet[id] = true;
944 }
945
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800946 if (oldest && (oldest->mStart <= element->getRealTime().nsec())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700947 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700948 if (whitelist) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800949 break;
950 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700951
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700952 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
953 // kick a misbehaving log reader client off the island
954 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800955 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
956 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700957 } else {
958 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800959 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700960 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800961 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700962
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700963 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
964 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700965 whitelist = true;
966 it++;
967 continue;
968 }
969
970 it = erase(it);
971 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800972 }
973
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700974 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800975 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800976 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800977 while ((it != mLogElements.end()) && (pruneRows > 0)) {
978 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700979
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700980 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700981 ++it;
982 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800983 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700984
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800985 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
986 mLast[id] = it;
987 mLastSet[id] = true;
988 }
989
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800990 if (oldest && (oldest->mStart <= element->getRealTime().nsec())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700991 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700992 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
993 // kick a misbehaving log reader client off the island
994 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800995 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
996 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700997 } else {
998 oldest->triggerSkip_Locked(id, pruneRows);
999 }
1000 break;
1001 }
1002
1003 it = erase(it);
1004 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001005 }
1006 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001007
Mark Salyzyn0175b072014-02-26 09:50:16 -08001008 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001009
1010 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001011}
1012
1013// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -07001014bool LogBuffer::clear(log_id_t id, uid_t uid) {
1015 bool busy = true;
1016 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
1017 for (int retry = 4;;) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001018 if (retry == 1) { // last pass
Mark Salyzync5dc9702015-09-16 15:34:00 -07001019 // Check if it is still busy after the sleep, we say prune
1020 // one entry, not another clear run, so we are looking for
1021 // the quick side effect of the return value to tell us if
1022 // we have a _blocked_ reader.
1023 pthread_mutex_lock(&mLogElementsLock);
1024 busy = prune(id, 1, uid);
1025 pthread_mutex_unlock(&mLogElementsLock);
1026 // It is still busy, blocked reader(s), lets kill them all!
1027 // otherwise, lets be a good citizen and preserve the slow
1028 // readers and let the clear run (below) deal with determining
1029 // if we are still blocked and return an error code to caller.
1030 if (busy) {
1031 LogTimeEntry::lock();
1032 LastLogTimes::iterator times = mTimes.begin();
1033 while (times != mTimes.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001034 LogTimeEntry* entry = (*times);
Mark Salyzync5dc9702015-09-16 15:34:00 -07001035 // Killer punch
1036 if (entry->owned_Locked() && entry->isWatching(id)) {
1037 entry->release_Locked();
1038 }
1039 times++;
1040 }
1041 LogTimeEntry::unlock();
1042 }
1043 }
1044 pthread_mutex_lock(&mLogElementsLock);
1045 busy = prune(id, ULONG_MAX, uid);
1046 pthread_mutex_unlock(&mLogElementsLock);
1047 if (!busy || !--retry) {
1048 break;
1049 }
Mark Salyzyn501c3732017-03-10 14:31:54 -08001050 sleep(1); // Let reader(s) catch up after notification
Mark Salyzync5dc9702015-09-16 15:34:00 -07001051 }
1052 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001053}
1054
1055// get the used space associated with "id".
1056unsigned long LogBuffer::getSizeUsed(log_id_t id) {
1057 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001058 size_t retval = stats.sizes(id);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001059 pthread_mutex_unlock(&mLogElementsLock);
1060 return retval;
1061}
1062
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001063// set the total space allocated to "id"
1064int LogBuffer::setSize(log_id_t id, unsigned long size) {
1065 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -07001066 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001067 return -1;
1068 }
1069 pthread_mutex_lock(&mLogElementsLock);
1070 log_buffer_size(id) = size;
1071 pthread_mutex_unlock(&mLogElementsLock);
1072 return 0;
1073}
1074
1075// get the total space allocated to "id"
1076unsigned long LogBuffer::getSize(log_id_t id) {
1077 pthread_mutex_lock(&mLogElementsLock);
1078 size_t retval = log_buffer_size(id);
1079 pthread_mutex_unlock(&mLogElementsLock);
1080 return retval;
1081}
1082
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -08001083log_time LogBuffer::flushTo(
1084 SocketClient* reader, const log_time& start, bool privileged, bool security,
Mark Salyzyn501c3732017-03-10 14:31:54 -08001085 int (*filter)(const LogBufferElement* element, void* arg), void* arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001086 LogBufferElementCollection::iterator it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001087 uid_t uid = reader->getUid();
1088
1089 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001090
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -08001091 if (start == log_time::EPOCH) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001092 // client wants to start from the beginning
1093 it = mLogElements.begin();
1094 } else {
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001095 LogBufferElementCollection::iterator last = mLogElements.begin();
1096 // 30 second limit to continue search for out-of-order entries.
1097 log_time min = start - log_time(30, 0);
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001098 // Client wants to start from some specified time. Chances are
1099 // we are better off starting from the end of the time sorted list.
Mark Salyzyn501c3732017-03-10 14:31:54 -08001100 for (it = mLogElements.end(); it != mLogElements.begin();
1101 /* do nothing */) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001102 --it;
Mark Salyzyn501c3732017-03-10 14:31:54 -08001103 LogBufferElement* element = *it;
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001104 if (element->getRealTime() > start) {
1105 last = it;
1106 } else if (element->getRealTime() < min) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001107 break;
1108 }
1109 }
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001110 it = last;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001111 }
1112
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001113 log_time max = start;
Mark Salyzynb5b87962017-01-23 14:20:31 -08001114 // Help detect if the valid message before is from the same source so
1115 // we can differentiate chatty filter types.
1116 pid_t lastTid[LOG_ID_MAX] = { 0 };
1117
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001118 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001119 LogBufferElement* element = *it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001120
1121 if (!privileged && (element->getUid() != uid)) {
1122 continue;
1123 }
1124
Mark Salyzyn8fa88962016-01-26 14:32:35 -08001125 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1126 continue;
1127 }
1128
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -08001129 if (element->getRealTime() <= start) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001130 continue;
1131 }
1132
1133 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001134 if (filter) {
1135 int ret = (*filter)(element, arg);
1136 if (ret == false) {
1137 continue;
1138 }
1139 if (ret != true) {
1140 break;
1141 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001142 }
1143
Mark Salyzynb5b87962017-01-23 14:20:31 -08001144 bool sameTid = lastTid[element->getLogId()] == element->getTid();
1145 // Dropped (chatty) immediately following a valid log from the
1146 // same source in the same log buffer indicates we have a
1147 // multiple identical squash. chatty that differs source
1148 // is due to spam filter. chatty to chatty of different
1149 // source is also due to spam filter.
Mark Salyzyn501c3732017-03-10 14:31:54 -08001150 lastTid[element->getLogId()] =
1151 (element->getDropped() && !sameTid) ? 0 : element->getTid();
Mark Salyzynb5b87962017-01-23 14:20:31 -08001152
Mark Salyzyn0175b072014-02-26 09:50:16 -08001153 pthread_mutex_unlock(&mLogElementsLock);
1154
1155 // range locking in LastLogTimes looks after us
Mark Salyzynb5b87962017-01-23 14:20:31 -08001156 max = element->flushTo(reader, this, privileged, sameTid);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001157
1158 if (max == element->FLUSH_ERROR) {
1159 return max;
1160 }
1161
1162 pthread_mutex_lock(&mLogElementsLock);
1163 }
1164 pthread_mutex_unlock(&mLogElementsLock);
1165
1166 return max;
1167}
Mark Salyzyn34facab2014-02-06 14:48:50 -08001168
Mark Salyzynee3b8382015-12-17 09:58:43 -08001169std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1170 unsigned int logMask) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001171 pthread_mutex_lock(&mLogElementsLock);
1172
Mark Salyzynee3b8382015-12-17 09:58:43 -08001173 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001174
1175 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn73160ac2015-08-20 10:01:44 -07001176
1177 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001178}