blob: 9c634f680967841df0d1d37771265994a9ca745f [file] [log] [blame]
Mark Salyzyn12bac902014-02-26 09:50:16 -08001/*
2 * Copyright (C) 2012-2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Mark Salyzyn21fc3932016-10-24 16:22:17 -070016// for manual checking of stale entries during LogBuffer::erase()
17//#define DEBUG_CHECK_FOR_STALE_ENTRIES
Mark Salyzyn12bac902014-02-26 09:50:16 -080018
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070019#include <ctype.h>
Mark Salyzynedb18aa2016-12-16 16:09:15 -080020#include <endian.h>
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -080021#include <errno.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080022#include <stdio.h>
23#include <string.h>
Mark Salyzyn2e86fa42016-10-24 08:20:26 -070024#include <sys/cdefs.h>
Mark Salyzyn7b85c592014-05-09 17:44:18 -070025#include <sys/user.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080026#include <time.h>
27#include <unistd.h>
28
Mark Salyzyn6a404192015-05-19 09:12:30 -070029#include <unordered_map>
30
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070031#include <cutils/properties.h>
Mark Salyzynf4693f32016-09-27 13:08:23 -070032#include <private/android_logger.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080033
34#include "LogBuffer.h"
Mark Salyzyn2b4a7632015-09-08 08:56:32 -070035#include "LogKlog.h"
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070036#include "LogReader.h"
Mark Salyzyn2456d042016-12-13 10:31:29 -080037#include "LogUtils.h"
Mark Salyzyn12bac902014-02-26 09:50:16 -080038
Mark Salyzyn2e86fa42016-10-24 08:20:26 -070039#ifndef __predict_false
40#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
41#endif
42
Mark Salyzync89839a2014-02-11 12:29:31 -080043// Default
Mark Salyzync89839a2014-02-11 12:29:31 -080044#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070045
Mark Salyzyn3fe25932015-03-10 16:45:17 -070046void LogBuffer::init() {
Mark Salyzync89839a2014-02-11 12:29:31 -080047 log_id_for_each(i) {
Mark Salyzyn7a3c2822016-01-11 10:58:09 -080048 mLastSet[i] = false;
49 mLast[i] = mLogElements.begin();
50
Mark Salyzynf4693f32016-09-27 13:08:23 -070051 if (setSize(i, __android_logger_get_buffer_size(i))) {
Mark Salyzyn7b85c592014-05-09 17:44:18 -070052 setSize(i, LOG_BUFFER_MIN_SIZE);
53 }
Mark Salyzync89839a2014-02-11 12:29:31 -080054 }
Mark Salyzyn2b4a7632015-09-08 08:56:32 -070055 bool lastMonotonic = monotonic;
Mark Salyzyn2d4f9bc2015-12-01 15:57:25 -080056 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzyn552b4752015-11-30 11:35:56 -080057 if (lastMonotonic != monotonic) {
58 //
59 // Fixup all timestamps, may not be 100% accurate, but better than
60 // throwing what we have away when we get 'surprised' by a change.
61 // In-place element fixup so no need to check reader-lock. Entries
62 // should already be in timestamp order, but we could end up with a
63 // few out-of-order entries if new monotonics come in before we
64 // are notified of the reinit change in status. A Typical example would
65 // be:
66 // --------- beginning of system
67 // 10.494082 184 201 D Cryptfs : Just triggered post_fs_data
68 // --------- beginning of kernel
69 // 0.000000 0 0 I : Initializing cgroup subsys
70 // as the act of mounting /data would trigger persist.logd.timestamp to
71 // be corrected. 1/30 corner case YMMV.
72 //
73 pthread_mutex_lock(&mLogElementsLock);
74 LogBufferElementCollection::iterator it = mLogElements.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -080075 while ((it != mLogElements.end())) {
76 LogBufferElement* e = *it;
Mark Salyzyn552b4752015-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 Salyzyn2b4a7632015-09-08 08:56:32 -070089 }
90
Mark Salyzyn552b4752015-11-30 11:35:56 -080091 // We may have been triggered by a SIGHUP. Release any sleeping reader
92 // threads to dump their current content.
Mark Salyzyn2b4a7632015-09-08 08:56:32 -070093 //
Mark Salyzyn552b4752015-11-30 11:35:56 -080094 // NB: this is _not_ performed in the context of a SIGHUP, it is
95 // performed during startup, and in context of reinit administrative thread
96 LogTimeEntry::lock();
97
98 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -080099 while (times != mTimes.end()) {
100 LogTimeEntry* entry = (*times);
Mark Salyzyn552b4752015-11-30 11:35:56 -0800101 if (entry->owned_Locked()) {
102 entry->triggerReader_Locked();
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700103 }
Mark Salyzyn552b4752015-11-30 11:35:56 -0800104 times++;
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700105 }
Mark Salyzyn552b4752015-11-30 11:35:56 -0800106
107 LogTimeEntry::unlock();
Mark Salyzyn12bac902014-02-26 09:50:16 -0800108}
109
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800110LogBuffer::LogBuffer(LastLogTimes* times)
111 : monotonic(android_log_clockid() == CLOCK_MONOTONIC), mTimes(*times) {
Mark Salyzyn3fe25932015-03-10 16:45:17 -0700112 pthread_mutex_init(&mLogElementsLock, NULL);
113
Mark Salyzyn2456d042016-12-13 10:31:29 -0800114 log_id_for_each(i) {
115 lastLoggedElements[i] = NULL;
116 droppedElements[i] = NULL;
117 }
118
Mark Salyzyn3fe25932015-03-10 16:45:17 -0700119 init();
120}
121
Mark Salyzyn2456d042016-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 Salyzynda65bcb2017-03-10 14:31:54 -0800129enum match_type { DIFFERENT, SAME, SAME_LIBLOG };
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800130
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800131static enum match_type identical(LogBufferElement* elem,
132 LogBufferElement* last) {
Mark Salyzyn2456d042016-12-13 10:31:29 -0800133 // is it mostly identical?
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800134 // if (!elem) return DIFFERENT;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800135 unsigned short lenl = elem->getMsgLen();
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800136 if (!lenl) return DIFFERENT;
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800137 // if (!last) return DIFFERENT;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800138 unsigned short lenr = last->getMsgLen();
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800139 if (!lenr) return DIFFERENT;
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800140 // if (elem->getLogId() != last->getLogId()) return DIFFERENT;
Mark Salyzynedb18aa2016-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 Salyzyn2456d042016-12-13 10:31:29 -0800144
145 // last is more than a minute old, stop squashing identical messages
146 if (elem->getRealTime().nsec() >
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800147 (last->getRealTime().nsec() + 60 * NS_PER_SEC))
148 return DIFFERENT;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800149
150 // Identical message
151 const char* msgl = elem->getMsg();
152 const char* msgr = last->getMsg();
Mark Salyzynedb18aa2016-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 Salyzynda65bcb2017-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 Salyzynedb18aa2016-12-16 16:09:15 -0800162 }
Mark Salyzyn2456d042016-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 Salyzynda65bcb2017-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 Salyzyn2456d042016-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 Salyzynda65bcb2017-03-10 14:31:54 -0800176 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800177 if (!avcl) return DIFFERENT;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800178 lenl -= avcl - msgl;
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800179 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800180 if (!avcr) return DIFFERENT;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800181 lenr -= avcr - msgr;
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800182 if (lenl != lenr) return DIFFERENT;
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800183 if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc), lenl))
184 return DIFFERENT;
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800185 return SAME;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800186}
187
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800188int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
189 pid_t tid, const char* msg, unsigned short len) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800190 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800191 return -EINVAL;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800192 }
Mark Salyzyn19c91112014-10-02 13:07:05 -0700193
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800194 LogBufferElement* elem =
195 new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
Mark Salyzyn4a8d2502016-01-06 21:17:43 +0000196 if (log_id != LOG_ID_SECURITY) {
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800197 int prio = ANDROID_LOG_INFO;
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800198 const char* tag = NULL;
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800199 if (log_id == LOG_ID_EVENTS) {
Mark Salyzyndb9d0f62016-09-12 14:51:54 -0700200 tag = tagToName(elem->getTag());
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800201 } else {
202 prio = *msg;
203 tag = msg + 1;
204 }
Mark Salyzyndb9d0f62016-09-12 14:51:54 -0700205 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800206 // Log traffic received to total
207 pthread_mutex_lock(&mLogElementsLock);
208 stats.add(elem);
209 stats.subtract(elem);
210 pthread_mutex_unlock(&mLogElementsLock);
211 delete elem;
212 return -EACCES;
213 }
Mark Salyzyn19c91112014-10-02 13:07:05 -0700214 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800215
216 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn2456d042016-12-13 10:31:29 -0800217 LogBufferElement* currentLast = lastLoggedElements[log_id];
218 if (currentLast) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800219 LogBufferElement* dropped = droppedElements[log_id];
Mark Salyzyn2456d042016-12-13 10:31:29 -0800220 unsigned short count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800221 //
222 // State Init
223 // incoming:
224 // dropped = NULL
225 // currentLast = NULL;
226 // elem = incoming message
227 // outgoing:
228 // dropped = NULL -> State 0
229 // currentLast = copy of elem
230 // log elem
231 // State 0
232 // incoming:
233 // count = 0
234 // dropped = NULL
235 // currentLast = copy of last message
236 // elem = incoming message
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800237 // outgoing: if match != DIFFERENT
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800238 // dropped = copy of first identical message -> State 1
239 // currentLast = reference to elem
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800240 // break: if match == DIFFERENT
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800241 // dropped = NULL -> State 0
242 // delete copy of last message (incoming currentLast)
243 // currentLast = copy of elem
244 // log elem
245 // State 1
246 // incoming:
247 // count = 0
248 // dropped = copy of first identical message
249 // currentLast = reference to last held-back incoming
250 // message
251 // elem = incoming message
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800252 // outgoing: if match == SAME
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800253 // delete copy of first identical message (dropped)
254 // dropped = reference to last held-back incoming
255 // message set to chatty count of 1 -> State 2
256 // currentLast = reference to elem
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800257 // outgoing: if match == SAME_LIBLOG
258 // dropped = copy of first identical message -> State 1
259 // take sum of currentLast and elem
260 // if sum overflows:
261 // log currentLast
262 // currentLast = reference to elem
263 // else
264 // delete currentLast
265 // currentLast = reference to elem, sum liblog.
266 // break: if match == DIFFERENT
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800267 // delete dropped
268 // dropped = NULL -> State 0
269 // log reference to last held-back (currentLast)
270 // currentLast = copy of elem
271 // log elem
272 // State 2
273 // incoming:
274 // count = chatty count
275 // dropped = chatty message holding count
276 // currentLast = reference to last held-back incoming
277 // message.
278 // dropped = chatty message holding count
279 // elem = incoming message
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800280 // outgoing: if match != DIFFERENT
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800281 // delete chatty message holding count
282 // dropped = reference to last held-back incoming
283 // message, set to chatty count + 1
284 // currentLast = reference to elem
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800285 // break: if match == DIFFERENT
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800286 // log dropped (chatty message)
287 // dropped = NULL -> State 0
288 // log reference to last held-back (currentLast)
289 // currentLast = copy of elem
290 // log elem
291 //
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800292 enum match_type match = identical(elem, currentLast);
293 if (match != DIFFERENT) {
Mark Salyzyn2456d042016-12-13 10:31:29 -0800294 if (dropped) {
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800295 // Sum up liblog tag messages?
296 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
297 android_log_event_int_t* event =
298 reinterpret_cast<android_log_event_int_t*>(
299 const_cast<char*>(currentLast->getMsg()));
300 //
301 // To unit test, differentiate with something like:
302 // event->header.tag = htole32(CHATTY_LOG_TAG);
303 // here, then instead of delete currentLast below,
304 // log(currentLast) to see the incremental sums form.
305 //
306 uint32_t swab = event->payload.data;
307 unsigned long long total = htole32(swab);
308 event = reinterpret_cast<android_log_event_int_t*>(
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800309 const_cast<char*>(elem->getMsg()));
Mark Salyzynedb18aa2016-12-16 16:09:15 -0800310 swab = event->payload.data;
311
312 lastLoggedElements[LOG_ID_EVENTS] = elem;
313 total += htole32(swab);
314 // check for overflow
315 if (total >= UINT32_MAX) {
316 log(currentLast);
317 pthread_mutex_unlock(&mLogElementsLock);
318 return len;
319 }
320 stats.add(currentLast);
321 stats.subtract(currentLast);
322 delete currentLast;
323 swab = total;
324 event->payload.data = htole32(swab);
325 pthread_mutex_unlock(&mLogElementsLock);
326 return len;
327 }
Mark Salyzyn2456d042016-12-13 10:31:29 -0800328 if (count == USHRT_MAX) {
329 log(dropped);
330 count = 1;
331 } else {
332 delete dropped;
333 ++count;
334 }
335 }
336 if (count) {
337 stats.add(currentLast);
338 stats.subtract(currentLast);
339 currentLast->setDropped(count);
340 }
341 droppedElements[log_id] = currentLast;
342 lastLoggedElements[log_id] = elem;
343 pthread_mutex_unlock(&mLogElementsLock);
344 return len;
345 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800346 if (dropped) { // State 1 or 2
347 if (count) { // State 2
348 log(dropped); // report chatty
349 } else { // State 1
350 delete dropped;
Mark Salyzyn9361c0b2016-12-16 16:09:15 -0800351 }
Mark Salyzyn2456d042016-12-13 10:31:29 -0800352 droppedElements[log_id] = NULL;
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800353 log(currentLast); // report last message in the series
354 } else { // State 0
Mark Salyzyn2456d042016-12-13 10:31:29 -0800355 delete currentLast;
356 }
357 }
358 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800359
Mark Salyzyn2456d042016-12-13 10:31:29 -0800360 log(elem);
361 pthread_mutex_unlock(&mLogElementsLock);
362
363 return len;
364}
365
366// assumes mLogElementsLock held, owns elem, will look after garbage collection
367void LogBuffer::log(LogBufferElement* elem) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800368 // Insert elements in time sorted order if possible
369 // NB: if end is region locked, place element at end of list
370 LogBufferElementCollection::iterator it = mLogElements.end();
371 LogBufferElementCollection::iterator last = it;
Mark Salyzyna222a772014-10-13 16:49:47 -0700372 while (last != mLogElements.begin()) {
373 --it;
Mark Salyzyn2456d042016-12-13 10:31:29 -0800374 if ((*it)->getRealTime() <= elem->getRealTime()) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800375 break;
376 }
377 last = it;
378 }
Mark Salyzyn5b7a8d82014-02-18 11:23:53 -0800379
Mark Salyzyn12bac902014-02-26 09:50:16 -0800380 if (last == mLogElements.end()) {
381 mLogElements.push_back(elem);
382 } else {
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800383 uint64_t end = 1;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800384 bool end_set = false;
385 bool end_always = false;
386
387 LogTimeEntry::lock();
388
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700389 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800390 while (times != mTimes.end()) {
Mark Salyzyn2456d042016-12-13 10:31:29 -0800391 LogTimeEntry* entry = (*times);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800392 if (entry->owned_Locked()) {
393 if (!entry->mNonBlock) {
394 end_always = true;
395 break;
396 }
397 if (!end_set || (end <= entry->mEnd)) {
398 end = entry->mEnd;
399 end_set = true;
400 }
401 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700402 times++;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800403 }
404
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800405 if (end_always || (end_set && (end >= (*last)->getSequence()))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800406 mLogElements.push_back(elem);
407 } else {
Mark Salyzyn2456d042016-12-13 10:31:29 -0800408 mLogElements.insert(last, elem);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800409 }
410
411 LogTimeEntry::unlock();
412 }
413
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700414 stats.add(elem);
Mark Salyzyn2456d042016-12-13 10:31:29 -0800415 maybePrune(elem->getLogId());
Mark Salyzyn12bac902014-02-26 09:50:16 -0800416}
417
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700418// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn12bac902014-02-26 09:50:16 -0800419//
420// mLogElementsLock must be held when this function is called.
421void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800422 size_t sizes = stats.sizes(id);
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700423 unsigned long maxSize = log_buffer_size(id);
424 if (sizes > maxSize) {
Mark Salyzyn63745722015-08-19 12:20:36 -0700425 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzynd745c722015-09-30 07:40:09 -0700426 size_t elements = stats.realElements(id);
427 size_t minElements = elements / 100;
428 if (minElements < minPrune) {
429 minElements = minPrune;
430 }
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700431 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700432 if (pruneRows < minElements) {
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700433 pruneRows = minElements;
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800434 }
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700435 if (pruneRows > maxPrune) {
436 pruneRows = maxPrune;
Mark Salyzyn63745722015-08-19 12:20:36 -0700437 }
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800438 prune(id, pruneRows);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800439 }
440}
441
Mark Salyzyn57b82a62015-09-03 16:08:50 -0700442LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800443 LogBufferElementCollection::iterator it, bool coalesce) {
444 LogBufferElement* element = *it;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700445 log_id_t id = element->getLogId();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700446
Mark Salyzyn628d9372016-10-21 09:46:42 -0700447 // Remove iterator references in the various lists that will become stale
448 // after the element is erased from the main logging list.
449
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800450 { // start of scope for found iterator
451 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
452 ? element->getTag()
453 : element->getUid();
Mark Salyzync572fc62016-07-14 15:34:30 -0700454 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
455 if ((found != mLastWorst[id].end()) && (it == found->second)) {
456 mLastWorst[id].erase(found);
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700457 }
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700458 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700459
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800460 { // start of scope for pid found iterator
Mark Salyzyn628d9372016-10-21 09:46:42 -0700461 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700462 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
463 // long term code stability, find() check should be fast for those ids.
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700464 LogBufferPidIteratorMap::iterator found =
465 mLastWorstPidOfSystem[id].find(element->getPid());
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800466 if ((found != mLastWorstPidOfSystem[id].end()) &&
467 (it == found->second)) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700468 mLastWorstPidOfSystem[id].erase(found);
469 }
470 }
471
Mark Salyzynd701b782016-01-19 16:04:41 -0800472 bool setLast[LOG_ID_MAX];
473 bool doSetLast = false;
474 log_id_for_each(i) {
475 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
476 }
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700477#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
478 LogBufferElementCollection::iterator bad = it;
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800479 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
480 ? element->getTag()
481 : element->getUid();
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700482#endif
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700483 it = mLogElements.erase(it);
Mark Salyzynd701b782016-01-19 16:04:41 -0800484 if (doSetLast) {
485 log_id_for_each(i) {
486 if (setLast[i]) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800487 if (__predict_false(it == mLogElements.end())) { // impossible
Mark Salyzynd701b782016-01-19 16:04:41 -0800488 mLastSet[i] = false;
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700489 mLast[i] = mLogElements.begin();
Mark Salyzynd701b782016-01-19 16:04:41 -0800490 } else {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800491 mLast[i] = it; // push down the road as next-best-watermark
Mark Salyzynd701b782016-01-19 16:04:41 -0800492 }
493 }
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800494 }
495 }
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700496#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
497 log_id_for_each(i) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800498 for (auto b : mLastWorst[i]) {
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700499 if (bad == b.second) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800500 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i,
501 b.first, key);
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700502 }
503 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800504 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700505 if (bad == b.second) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800506 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i,
507 b.first);
Mark Salyzyn21fc3932016-10-24 16:22:17 -0700508 }
509 }
510 if (mLastSet[i] && (bad == mLast[i])) {
511 android::prdebug("stale mLast[%d]\n", i);
512 mLastSet[i] = false;
513 mLast[i] = mLogElements.begin();
514 }
515 }
516#endif
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700517 if (coalesce) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700518 stats.erase(element);
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700519 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700520 stats.subtract(element);
Mark Salyzyn57b82a62015-09-03 16:08:50 -0700521 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700522 delete element;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700523
524 return it;
525}
526
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700527// Define a temporary mechanism to report the last LogBufferElement pointer
528// for the specified uid, pid and tid. Used below to help merge-sort when
529// pruning for worst UID.
530class LogBufferElementKey {
531 const union {
532 struct {
Mark Salyzync4595602016-12-13 12:44:20 -0800533 uint32_t uid;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700534 uint16_t pid;
535 uint16_t tid;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700536 } __packed;
537 uint64_t value;
538 } __packed;
539
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800540 public:
541 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid)
542 : uid(uid), pid(pid), tid(tid) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700543 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800544 explicit LogBufferElementKey(uint64_t key) : value(key) {
545 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700546
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800547 uint64_t getKey() {
548 return value;
549 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700550};
551
Mark Salyzyn6a404192015-05-19 09:12:30 -0700552class LogBufferElementLast {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800553 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn6a404192015-05-19 09:12:30 -0700554 LogBufferElementMap map;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700555
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800556 public:
557 bool coalesce(LogBufferElement* element, unsigned short dropped) {
558 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700559 element->getTid());
Mark Salyzyn6a404192015-05-19 09:12:30 -0700560 LogBufferElementMap::iterator it = map.find(key.getKey());
561 if (it != map.end()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800562 LogBufferElement* found = it->second;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700563 unsigned short moreDropped = found->getDropped();
564 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700565 map.erase(it);
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700566 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700567 found->setDropped(dropped + moreDropped);
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700568 return true;
569 }
570 }
571 return false;
572 }
573
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800574 void add(LogBufferElement* element) {
575 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700576 element->getTid());
577 map[key.getKey()] = element;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700578 }
579
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700580 inline void clear() {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700581 map.clear();
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700582 }
583
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800584 void clear(LogBufferElement* element) {
585 uint64_t current =
586 element->getRealTime().nsec() - (EXPIRE_RATELIMIT * NS_PER_SEC);
587 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
588 LogBufferElement* mapElement = it->second;
589 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD) &&
590 (current > mapElement->getRealTime().nsec())) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700591 it = map.erase(it);
592 } else {
593 ++it;
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700594 }
595 }
596 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700597};
598
Mark Salyzyn12bac902014-02-26 09:50:16 -0800599// prune "pruneRows" of type "id" from the buffer.
600//
Mark Salyzyn50e21082015-09-08 09:12:51 -0700601// This garbage collection task is used to expire log entries. It is called to
602// remove all logs (clear), all UID logs (unprivileged clear), or every
603// 256 or 10% of the total logs (whichever is less) to prune the logs.
604//
605// First there is a prep phase where we discover the reader region lock that
606// acts as a backstop to any pruning activity to stop there and go no further.
607//
608// There are three major pruning loops that follow. All expire from the oldest
609// entries. Since there are multiple log buffers, the Android logging facility
610// will appear to drop entries 'in the middle' when looking at multiple log
611// sources and buffers. This effect is slightly more prominent when we prune
612// the worst offender by logging source. Thus the logs slowly loose content
613// and value as you move back in time. This is preferred since chatty sources
614// invariably move the logs value down faster as less chatty sources would be
615// expired in the noise.
616//
617// The first loop performs blacklisting and worst offender pruning. Falling
618// through when there are no notable worst offenders and have not hit the
619// region lock preventing further worst offender pruning. This loop also looks
620// after managing the chatty log entries and merging to help provide
621// statistical basis for blame. The chatty entries are not a notification of
622// how much logs you may have, but instead represent how much logs you would
623// have had in a virtual log buffer that is extended to cover all the in-memory
624// logs without loss. They last much longer than the represented pruned logs
625// since they get multiplied by the gains in the non-chatty log sources.
626//
627// The second loop get complicated because an algorithm of watermarks and
628// history is maintained to reduce the order and keep processing time
629// down to a minimum at scale. These algorithms can be costly in the face
630// of larger log buffers, or severly limited processing time granted to a
631// background task at lowest priority.
632//
633// This second loop does straight-up expiration from the end of the logs
634// (again, remember for the specified log buffer id) but does some whitelist
635// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
636// spam filtration all take priority. This second loop also checks if a region
637// lock is causing us to buffer too much in the logs to help the reader(s),
638// and will tell the slowest reader thread to skip log entries, and if
639// persistent and hits a further threshold, kill the reader thread.
640//
641// The third thread is optional, and only gets hit if there was a whitelist
642// and more needs to be pruned against the backstop of the region lock.
643//
Mark Salyzyn12bac902014-02-26 09:50:16 -0800644// mLogElementsLock must be held when this function is called.
Mark Salyzyn50e21082015-09-08 09:12:51 -0700645//
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700646bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800647 LogTimeEntry* oldest = NULL;
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700648 bool busy = false;
Mark Salyzynd280d812015-09-16 15:34:00 -0700649 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800650
651 LogTimeEntry::lock();
652
653 // Region locked?
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700654 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800655 while (times != mTimes.end()) {
656 LogTimeEntry* entry = (*times);
657 if (entry->owned_Locked() && entry->isWatching(id) &&
658 (!oldest || (oldest->mStart > entry->mStart) ||
659 ((oldest->mStart == entry->mStart) &&
660 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800661 oldest = entry;
662 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700663 times++;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800664 }
665
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800666 LogBufferElementCollection::iterator it;
667
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800668 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn4e23c6e2016-09-01 15:48:36 -0700669 // Only here if clear all request from non system source, so chatty
670 // filter logistics is not required.
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800671 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
672 while (it != mLogElements.end()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800673 LogBufferElement* element = *it;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700674
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800675 if ((element->getLogId() != id) ||
676 (element->getUid() != caller_uid)) {
Mark Salyzynd280d812015-09-16 15:34:00 -0700677 ++it;
678 continue;
679 }
680
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800681 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
682 mLast[id] = it;
683 mLastSet[id] = true;
684 }
685
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700686 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700687 busy = true;
Mark Salyzyn552b4752015-11-30 11:35:56 -0800688 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
689 oldest->triggerReader_Locked();
690 } else {
691 oldest->triggerSkip_Locked(id, pruneRows);
692 }
Mark Salyzyn276d5352014-06-12 11:16:16 -0700693 break;
694 }
695
Mark Salyzynd280d812015-09-16 15:34:00 -0700696 it = erase(it);
Mark Salyzyn4e23c6e2016-09-01 15:48:36 -0700697 if (--pruneRows == 0) {
698 break;
699 }
Mark Salyzyn276d5352014-06-12 11:16:16 -0700700 }
701 LogTimeEntry::unlock();
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700702 return busy;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700703 }
704
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700705 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800706 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzynd280d812015-09-16 15:34:00 -0700707 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800708 // recalculate the worst offender on every batched pass
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800709 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800710 size_t worst_sizes = 0;
711 size_t second_worst_sizes = 0;
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800712 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800713
Mark Salyzyn0e765c62015-03-17 17:17:25 -0700714 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzync572fc62016-07-14 15:34:30 -0700715 // Calculate threshold as 12.5% of available storage
716 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700717
Mark Salyzync572fc62016-07-14 15:34:30 -0700718 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800719 stats.sortTags(AID_ROOT, (pid_t)0, 2, id)
720 .findWorst(worst, worst_sizes, second_worst_sizes,
721 threshold);
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700722 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzync572fc62016-07-14 15:34:30 -0700723 } else {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800724 stats.sort(AID_ROOT, (pid_t)0, 2, id)
725 .findWorst(worst, worst_sizes, second_worst_sizes,
726 threshold);
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700727
Mark Salyzync572fc62016-07-14 15:34:30 -0700728 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800729 stats.sortPids(worst, (pid_t)0, 2, id)
730 .findWorst(worstPid, worst_sizes, second_worst_sizes);
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700731 }
732 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800733 }
734
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700735 // skip if we have neither worst nor naughty filters
Mark Salyzync572fc62016-07-14 15:34:30 -0700736 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700737 break;
738 }
739
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800740 bool kick = false;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700741 bool leading = true;
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800742 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn50e21082015-09-08 09:12:51 -0700743 // Perform at least one mandatory garbage collection cycle in following
744 // - clear leading chatty tags
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700745 // - coalesce chatty tags
Mark Salyzyn50e21082015-09-08 09:12:51 -0700746 // - check age-out of preserved logs
747 bool gc = pruneRows <= 1;
Mark Salyzync572fc62016-07-14 15:34:30 -0700748 if (!gc && (worst != -1)) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800749 { // begin scope for worst found iterator
750 LogBufferIteratorMap::iterator found =
751 mLastWorst[id].find(worst);
752 if ((found != mLastWorst[id].end()) &&
753 (found->second != mLogElements.end())) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700754 leading = false;
755 it = found->second;
756 }
757 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800758 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700759 // FYI: worstPid only set if !LOG_ID_EVENTS and
760 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800761 LogBufferPidIteratorMap::iterator found =
762 mLastWorstPidOfSystem[id].find(worstPid);
763 if ((found != mLastWorstPidOfSystem[id].end()) &&
764 (found->second != mLogElements.end())) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700765 leading = false;
766 it = found->second;
767 }
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700768 }
769 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800770 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzyn4e29e172015-08-24 13:43:27 -0700771 LogBufferElementCollection::iterator lastt;
772 lastt = mLogElements.end();
773 --lastt;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700774 LogBufferElementLast last;
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700775 while (it != mLogElements.end()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800776 LogBufferElement* element = *it;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800777
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700778 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700779 busy = true;
Mark Salyzyn552b4752015-11-30 11:35:56 -0800780 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
781 oldest->triggerReader_Locked();
782 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800783 break;
784 }
785
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700786 if (element->getLogId() != id) {
Mark Salyzync89839a2014-02-11 12:29:31 -0800787 ++it;
788 continue;
789 }
Mark Salyzyn628d9372016-10-21 09:46:42 -0700790 // below this point element->getLogId() == id
Mark Salyzync89839a2014-02-11 12:29:31 -0800791
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800792 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
793 mLast[id] = it;
794 mLastSet[id] = true;
795 }
796
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700797 unsigned short dropped = element->getDropped();
Mark Salyzync89839a2014-02-11 12:29:31 -0800798
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700799 // remove any leading drops
800 if (leading && dropped) {
801 it = erase(it);
802 continue;
803 }
804
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700805 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700806 it = erase(it, true);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700807 continue;
808 }
809
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800810 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
811 ? element->getTag()
812 : element->getUid();
Mark Salyzync572fc62016-07-14 15:34:30 -0700813
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700814 if (hasBlacklist && mPrune.naughty(element)) {
815 last.clear(element);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700816 it = erase(it);
817 if (dropped) {
818 continue;
819 }
820
821 pruneRows--;
822 if (pruneRows == 0) {
823 break;
824 }
825
Mark Salyzync572fc62016-07-14 15:34:30 -0700826 if (key == worst) {
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700827 kick = true;
828 if (worst_sizes < second_worst_sizes) {
829 break;
830 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700831 worst_sizes -= element->getMsgLen();
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700832 }
833 continue;
834 }
835
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800836 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
837 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzyn4e29e172015-08-24 13:43:27 -0700838 break;
839 }
840
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700841 if (dropped) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700842 last.add(element);
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800843 if (worstPid &&
844 ((!gc && (element->getPid() == worstPid)) ||
845 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
846 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzyn628d9372016-10-21 09:46:42 -0700847 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700848 // watermark if current one empty. id is not LOG_ID_EVENTS
849 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn9faf9082016-09-01 07:28:44 -0700850 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700851 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800852 if ((!gc && !worstPid && (key == worst)) ||
853 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzync572fc62016-07-14 15:34:30 -0700854 mLastWorst[id][key] = it;
Mark Salyzyncb15c7c2015-08-24 13:43:27 -0700855 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800856 ++it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700857 continue;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800858 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700859
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800860 if ((key != worst) ||
861 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzynfd318a82015-06-01 09:41:19 -0700862 leading = false;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700863 last.clear(element);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700864 ++it;
865 continue;
866 }
Mark Salyzyn628d9372016-10-21 09:46:42 -0700867 // key == worst below here
868 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700869
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700870 pruneRows--;
871 if (pruneRows == 0) {
872 break;
873 }
874
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700875 kick = true;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700876
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700877 unsigned short len = element->getMsgLen();
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700878
879 // do not create any leading drops
880 if (leading) {
881 it = erase(it);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700882 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700883 stats.drop(element);
884 element->setDropped(1);
885 if (last.coalesce(element, 1)) {
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700886 it = erase(it, true);
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700887 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700888 last.add(element);
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800889 if (worstPid &&
890 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
891 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzyn628d9372016-10-21 09:46:42 -0700892 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn2e86fa42016-10-24 08:20:26 -0700893 // watermark if current one empty. id is not
894 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700895 mLastWorstPidOfSystem[id][worstPid] = it;
896 }
Mark Salyzync572fc62016-07-14 15:34:30 -0700897 if ((!gc && !worstPid) ||
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800898 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzync572fc62016-07-14 15:34:30 -0700899 mLastWorst[id][worst] = it;
Mark Salyzyn50e21082015-09-08 09:12:51 -0700900 }
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700901 ++it;
902 }
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700903 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700904 if (worst_sizes < second_worst_sizes) {
905 break;
906 }
907 worst_sizes -= len;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800908 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700909 last.clear();
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800910
Mark Salyzync402bc72014-04-01 17:19:47 -0700911 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800912 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800913 }
914 }
915
Mark Salyzync89839a2014-02-11 12:29:31 -0800916 bool whitelist = false;
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800917 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800918 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800919 while ((pruneRows > 0) && (it != mLogElements.end())) {
920 LogBufferElement* element = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700921
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700922 if (element->getLogId() != id) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700923 it++;
924 continue;
925 }
926
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800927 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
928 mLast[id] = it;
929 mLastSet[id] = true;
930 }
931
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700932 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700933 busy = true;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700934 if (whitelist) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800935 break;
936 }
Mark Salyzync402bc72014-04-01 17:19:47 -0700937
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700938 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
939 // kick a misbehaving log reader client off the island
940 oldest->release_Locked();
Mark Salyzyn552b4752015-11-30 11:35:56 -0800941 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
942 oldest->triggerReader_Locked();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700943 } else {
944 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzync89839a2014-02-11 12:29:31 -0800945 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700946 break;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800947 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700948
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700949 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
950 // WhiteListed
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700951 whitelist = true;
952 it++;
953 continue;
954 }
955
956 it = erase(it);
957 pruneRows--;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800958 }
959
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700960 // Do not save the whitelist if we are reader range limited
Mark Salyzync89839a2014-02-11 12:29:31 -0800961 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800962 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800963 while ((it != mLogElements.end()) && (pruneRows > 0)) {
964 LogBufferElement* element = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700965
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700966 if (element->getLogId() != id) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700967 ++it;
968 continue;
Mark Salyzync89839a2014-02-11 12:29:31 -0800969 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700970
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800971 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
972 mLast[id] = it;
973 mLastSet[id] = true;
974 }
975
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700976 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700977 busy = true;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700978 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
979 // kick a misbehaving log reader client off the island
980 oldest->release_Locked();
Mark Salyzyn552b4752015-11-30 11:35:56 -0800981 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
982 oldest->triggerReader_Locked();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700983 } else {
984 oldest->triggerSkip_Locked(id, pruneRows);
985 }
986 break;
987 }
988
989 it = erase(it);
990 pruneRows--;
Mark Salyzync89839a2014-02-11 12:29:31 -0800991 }
992 }
Mark Salyzync89839a2014-02-11 12:29:31 -0800993
Mark Salyzyn12bac902014-02-26 09:50:16 -0800994 LogTimeEntry::unlock();
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700995
996 return (pruneRows > 0) && busy;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800997}
998
999// clear all rows of type "id" from the buffer.
Mark Salyzyn7702c3e2015-09-16 15:34:00 -07001000bool LogBuffer::clear(log_id_t id, uid_t uid) {
1001 bool busy = true;
1002 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
1003 for (int retry = 4;;) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001004 if (retry == 1) { // last pass
Mark Salyzyn7702c3e2015-09-16 15:34:00 -07001005 // Check if it is still busy after the sleep, we say prune
1006 // one entry, not another clear run, so we are looking for
1007 // the quick side effect of the return value to tell us if
1008 // we have a _blocked_ reader.
1009 pthread_mutex_lock(&mLogElementsLock);
1010 busy = prune(id, 1, uid);
1011 pthread_mutex_unlock(&mLogElementsLock);
1012 // It is still busy, blocked reader(s), lets kill them all!
1013 // otherwise, lets be a good citizen and preserve the slow
1014 // readers and let the clear run (below) deal with determining
1015 // if we are still blocked and return an error code to caller.
1016 if (busy) {
1017 LogTimeEntry::lock();
1018 LastLogTimes::iterator times = mTimes.begin();
1019 while (times != mTimes.end()) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001020 LogTimeEntry* entry = (*times);
Mark Salyzyn7702c3e2015-09-16 15:34:00 -07001021 // Killer punch
1022 if (entry->owned_Locked() && entry->isWatching(id)) {
1023 entry->release_Locked();
1024 }
1025 times++;
1026 }
1027 LogTimeEntry::unlock();
1028 }
1029 }
1030 pthread_mutex_lock(&mLogElementsLock);
1031 busy = prune(id, ULONG_MAX, uid);
1032 pthread_mutex_unlock(&mLogElementsLock);
1033 if (!busy || !--retry) {
1034 break;
1035 }
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001036 sleep(1); // Let reader(s) catch up after notification
Mark Salyzyn7702c3e2015-09-16 15:34:00 -07001037 }
1038 return busy;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001039}
1040
1041// get the used space associated with "id".
1042unsigned long LogBuffer::getSizeUsed(log_id_t id) {
1043 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzynd774bce2014-02-06 14:48:50 -08001044 size_t retval = stats.sizes(id);
Mark Salyzyn12bac902014-02-26 09:50:16 -08001045 pthread_mutex_unlock(&mLogElementsLock);
1046 return retval;
1047}
1048
Mark Salyzync89839a2014-02-11 12:29:31 -08001049// set the total space allocated to "id"
1050int LogBuffer::setSize(log_id_t id, unsigned long size) {
1051 // Reasonable limits ...
Mark Salyzynf4693f32016-09-27 13:08:23 -07001052 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzync89839a2014-02-11 12:29:31 -08001053 return -1;
1054 }
1055 pthread_mutex_lock(&mLogElementsLock);
1056 log_buffer_size(id) = size;
1057 pthread_mutex_unlock(&mLogElementsLock);
1058 return 0;
1059}
1060
1061// get the total space allocated to "id"
1062unsigned long LogBuffer::getSize(log_id_t id) {
1063 pthread_mutex_lock(&mLogElementsLock);
1064 size_t retval = log_buffer_size(id);
1065 pthread_mutex_unlock(&mLogElementsLock);
1066 return retval;
1067}
1068
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -08001069uint64_t LogBuffer::flushTo(
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001070 SocketClient* reader, const uint64_t start, bool privileged, bool security,
1071 int (*filter)(const LogBufferElement* element, void* arg), void* arg) {
Mark Salyzyn12bac902014-02-26 09:50:16 -08001072 LogBufferElementCollection::iterator it;
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -08001073 uint64_t max = start;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001074 uid_t uid = reader->getUid();
1075
1076 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001077
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -08001078 if (start <= 1) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001079 // client wants to start from the beginning
1080 it = mLogElements.begin();
1081 } else {
1082 // Client wants to start from some specified time. Chances are
1083 // we are better off starting from the end of the time sorted list.
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001084 for (it = mLogElements.end(); it != mLogElements.begin();
1085 /* do nothing */) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001086 --it;
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001087 LogBufferElement* element = *it;
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -08001088 if (element->getSequence() <= start) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001089 it++;
1090 break;
1091 }
1092 }
1093 }
1094
Mark Salyzyn99a671a2017-01-23 14:20:31 -08001095 // Help detect if the valid message before is from the same source so
1096 // we can differentiate chatty filter types.
1097 pid_t lastTid[LOG_ID_MAX] = { 0 };
1098
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -06001099 for (; it != mLogElements.end(); ++it) {
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001100 LogBufferElement* element = *it;
Mark Salyzyn12bac902014-02-26 09:50:16 -08001101
1102 if (!privileged && (element->getUid() != uid)) {
1103 continue;
1104 }
1105
Mark Salyzyn924c0b32016-01-26 14:32:35 -08001106 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1107 continue;
1108 }
1109
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -08001110 if (element->getSequence() <= start) {
Mark Salyzyn12bac902014-02-26 09:50:16 -08001111 continue;
1112 }
1113
1114 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -08001115 if (filter) {
1116 int ret = (*filter)(element, arg);
1117 if (ret == false) {
1118 continue;
1119 }
1120 if (ret != true) {
1121 break;
1122 }
Mark Salyzyn12bac902014-02-26 09:50:16 -08001123 }
1124
Mark Salyzyn99a671a2017-01-23 14:20:31 -08001125 bool sameTid = lastTid[element->getLogId()] == element->getTid();
1126 // Dropped (chatty) immediately following a valid log from the
1127 // same source in the same log buffer indicates we have a
1128 // multiple identical squash. chatty that differs source
1129 // is due to spam filter. chatty to chatty of different
1130 // source is also due to spam filter.
Mark Salyzynda65bcb2017-03-10 14:31:54 -08001131 lastTid[element->getLogId()] =
1132 (element->getDropped() && !sameTid) ? 0 : element->getTid();
Mark Salyzyn99a671a2017-01-23 14:20:31 -08001133
Mark Salyzyn12bac902014-02-26 09:50:16 -08001134 pthread_mutex_unlock(&mLogElementsLock);
1135
1136 // range locking in LastLogTimes looks after us
Mark Salyzyn99a671a2017-01-23 14:20:31 -08001137 max = element->flushTo(reader, this, privileged, sameTid);
Mark Salyzyn12bac902014-02-26 09:50:16 -08001138
1139 if (max == element->FLUSH_ERROR) {
1140 return max;
1141 }
1142
1143 pthread_mutex_lock(&mLogElementsLock);
1144 }
1145 pthread_mutex_unlock(&mLogElementsLock);
1146
1147 return max;
1148}
Mark Salyzynd774bce2014-02-06 14:48:50 -08001149
Mark Salyzynb921ab52015-12-17 09:58:43 -08001150std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1151 unsigned int logMask) {
Mark Salyzynd774bce2014-02-06 14:48:50 -08001152 pthread_mutex_lock(&mLogElementsLock);
1153
Mark Salyzynb921ab52015-12-17 09:58:43 -08001154 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzynd774bce2014-02-06 14:48:50 -08001155
1156 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn1c7b6fb2015-08-20 10:01:44 -07001157
1158 return ret;
Mark Salyzynd774bce2014-02-06 14:48:50 -08001159}