blob: d4765964d8cba4c4c6e3ec5dda3816b36ceedb7b [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 Salyzyn202e1532015-02-09 08:21:05 -080020#include <errno.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080021#include <stdio.h>
22#include <string.h>
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070023#include <sys/cdefs.h>
Mark Salyzyn57a0af92014-05-09 17:44:18 -070024#include <sys/user.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080025#include <time.h>
26#include <unistd.h>
27
Mark Salyzyn511338d2015-05-19 09:12:30 -070028#include <unordered_map>
29
Mark Salyzyn671e3432014-05-06 07:34:59 -070030#include <cutils/properties.h>
Mark Salyzynf10e2732016-09-27 13:08:23 -070031#include <private/android_logger.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080032
33#include "LogBuffer.h"
Mark Salyzynb6bee332015-09-08 08:56:32 -070034#include "LogKlog.h"
Mark Salyzyn671e3432014-05-06 07:34:59 -070035#include "LogReader.h"
Mark Salyzyna2c02222016-12-13 10:31:29 -080036#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080037
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070038#ifndef __predict_false
39#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
40#endif
41
Mark Salyzyndfa7a072014-02-11 12:29:31 -080042// Default
Mark Salyzyndfa7a072014-02-11 12:29:31 -080043#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn671e3432014-05-06 07:34:59 -070044
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070045void LogBuffer::init() {
Mark Salyzyndfa7a072014-02-11 12:29:31 -080046 log_id_for_each(i) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -080047 mLastSet[i] = false;
48 mLast[i] = mLogElements.begin();
49
Mark Salyzynf10e2732016-09-27 13:08:23 -070050 if (setSize(i, __android_logger_get_buffer_size(i))) {
Mark Salyzyn57a0af92014-05-09 17:44:18 -070051 setSize(i, LOG_BUFFER_MIN_SIZE);
52 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -080053 }
Mark Salyzynb6bee332015-09-08 08:56:32 -070054 bool lastMonotonic = monotonic;
Mark Salyzynba7a9a02015-12-01 15:57:25 -080055 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzynb75cce02015-11-30 11:35:56 -080056 if (lastMonotonic != monotonic) {
57 //
58 // Fixup all timestamps, may not be 100% accurate, but better than
59 // throwing what we have away when we get 'surprised' by a change.
60 // In-place element fixup so no need to check reader-lock. Entries
61 // should already be in timestamp order, but we could end up with a
62 // few out-of-order entries if new monotonics come in before we
63 // are notified of the reinit change in status. A Typical example would
64 // be:
65 // --------- beginning of system
66 // 10.494082 184 201 D Cryptfs : Just triggered post_fs_data
67 // --------- beginning of kernel
68 // 0.000000 0 0 I : Initializing cgroup subsys
69 // as the act of mounting /data would trigger persist.logd.timestamp to
70 // be corrected. 1/30 corner case YMMV.
71 //
72 pthread_mutex_lock(&mLogElementsLock);
73 LogBufferElementCollection::iterator it = mLogElements.begin();
74 while((it != mLogElements.end())) {
75 LogBufferElement *e = *it;
76 if (monotonic) {
77 if (!android::isMonotonic(e->mRealTime)) {
78 LogKlog::convertRealToMonotonic(e->mRealTime);
79 }
80 } else {
81 if (android::isMonotonic(e->mRealTime)) {
82 LogKlog::convertMonotonicToReal(e->mRealTime);
83 }
84 }
85 ++it;
86 }
87 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzynb6bee332015-09-08 08:56:32 -070088 }
89
Mark Salyzynb75cce02015-11-30 11:35:56 -080090 // We may have been triggered by a SIGHUP. Release any sleeping reader
91 // threads to dump their current content.
Mark Salyzynb6bee332015-09-08 08:56:32 -070092 //
Mark Salyzynb75cce02015-11-30 11:35:56 -080093 // NB: this is _not_ performed in the context of a SIGHUP, it is
94 // performed during startup, and in context of reinit administrative thread
95 LogTimeEntry::lock();
96
97 LastLogTimes::iterator times = mTimes.begin();
98 while(times != mTimes.end()) {
99 LogTimeEntry *entry = (*times);
100 if (entry->owned_Locked()) {
101 entry->triggerReader_Locked();
Mark Salyzynb6bee332015-09-08 08:56:32 -0700102 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800103 times++;
Mark Salyzynb6bee332015-09-08 08:56:32 -0700104 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800105
106 LogTimeEntry::unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800107}
108
Mark Salyzynb6bee332015-09-08 08:56:32 -0700109LogBuffer::LogBuffer(LastLogTimes *times):
Mark Salyzynba7a9a02015-12-01 15:57:25 -0800110 monotonic(android_log_clockid() == CLOCK_MONOTONIC),
Mark Salyzynb6bee332015-09-08 08:56:32 -0700111 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
129static bool identical(LogBufferElement* elem, LogBufferElement* last) {
130 // is it mostly identical?
131// if (!elem) return false;
132 unsigned short lenl = elem->getMsgLen();
133 if (!lenl) return false;
134// if (!last) return false;
135 unsigned short lenr = last->getMsgLen();
136 if (!lenr) return false;
137// if (elem->getLogId() != last->getLogId()) return false;
138 if (elem->getUid() != last->getUid()) return false;
139 if (elem->getPid() != last->getPid()) return false;
140 if (elem->getTid() != last->getTid()) return false;
141
142 // last is more than a minute old, stop squashing identical messages
143 if (elem->getRealTime().nsec() >
144 (last->getRealTime().nsec() + 60 * NS_PER_SEC)) return false;
145
146 // Identical message
147 const char* msgl = elem->getMsg();
148 const char* msgr = last->getMsg();
149 if ((lenl == lenr) && !fastcmp<memcmp>(msgl, msgr, lenl)) return true;
150
151 // audit message (except sequence number) identical?
152 static const char avc[] = "): avc: ";
153
154 if (last->isBinary()) {
155 if (fastcmp<memcmp>(msgl, msgr,
156 sizeof(android_log_event_string_t) -
157 sizeof(int32_t))) return false;
158 msgl += sizeof(android_log_event_string_t);
159 lenl -= sizeof(android_log_event_string_t);
160 msgr += sizeof(android_log_event_string_t);
161 lenr -= sizeof(android_log_event_string_t);
162 }
163 const char *avcl = android::strnstr(msgl, lenl, avc);
164 if (!avcl) return false;
165 lenl -= avcl - msgl;
166 const char *avcr = android::strnstr(msgr, lenr, avc);
167 if (!avcr) return false;
168 lenr -= avcr - msgr;
169 if (lenl != lenr) return false;
170 return !fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc), lenl);
171}
172
Mark Salyzyn202e1532015-02-09 08:21:05 -0800173int LogBuffer::log(log_id_t log_id, log_time realtime,
174 uid_t uid, pid_t pid, pid_t tid,
175 const char *msg, unsigned short len) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800176 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800177 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800178 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700179
Mark Salyzyn0175b072014-02-26 09:50:16 -0800180 LogBufferElement *elem = new LogBufferElement(log_id, realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700181 uid, pid, tid, msg, len);
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000182 if (log_id != LOG_ID_SECURITY) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800183 int prio = ANDROID_LOG_INFO;
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000184 const char *tag = NULL;
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700185 size_t len = 0;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800186 if (log_id == LOG_ID_EVENTS) {
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700187 tag = android::tagToName(&len, elem->getTag());
Mark Salyzyn083b0372015-12-04 10:59:45 -0800188 } else {
189 prio = *msg;
190 tag = msg + 1;
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700191 len = strlen(tag);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800192 }
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700193 if (!__android_log_is_loggable_len(prio, tag, len, ANDROID_LOG_VERBOSE)) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800194 // Log traffic received to total
195 pthread_mutex_lock(&mLogElementsLock);
196 stats.add(elem);
197 stats.subtract(elem);
198 pthread_mutex_unlock(&mLogElementsLock);
199 delete elem;
200 return -EACCES;
201 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700202 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800203
204 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800205 LogBufferElement* currentLast = lastLoggedElements[log_id];
206 if (currentLast) {
207 LogBufferElement *dropped = droppedElements[log_id];
208 unsigned short count = dropped ? dropped->getDropped() : 0;
209 if (identical(elem, currentLast)) {
210 if (dropped) {
211 if (count == USHRT_MAX) {
212 log(dropped);
213 count = 1;
214 } else {
215 delete dropped;
216 ++count;
217 }
218 }
219 if (count) {
220 stats.add(currentLast);
221 stats.subtract(currentLast);
222 currentLast->setDropped(count);
223 }
224 droppedElements[log_id] = currentLast;
225 lastLoggedElements[log_id] = elem;
226 pthread_mutex_unlock(&mLogElementsLock);
227 return len;
228 }
229 if (dropped) {
230 log(dropped);
231 droppedElements[log_id] = NULL;
232 }
233 if (count) {
234 log(currentLast);
235 } else {
236 delete currentLast;
237 }
238 }
239 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800240
Mark Salyzyna2c02222016-12-13 10:31:29 -0800241 log(elem);
242 pthread_mutex_unlock(&mLogElementsLock);
243
244 return len;
245}
246
247// assumes mLogElementsLock held, owns elem, will look after garbage collection
248void LogBuffer::log(LogBufferElement* elem) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800249 // Insert elements in time sorted order if possible
250 // NB: if end is region locked, place element at end of list
251 LogBufferElementCollection::iterator it = mLogElements.end();
252 LogBufferElementCollection::iterator last = it;
Mark Salyzyneae155e2014-10-13 16:49:47 -0700253 while (last != mLogElements.begin()) {
254 --it;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800255 if ((*it)->getRealTime() <= elem->getRealTime()) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800256 break;
257 }
258 last = it;
259 }
Mark Salyzync03e72c2014-02-18 11:23:53 -0800260
Mark Salyzyn0175b072014-02-26 09:50:16 -0800261 if (last == mLogElements.end()) {
262 mLogElements.push_back(elem);
263 } else {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800264 uint64_t end = 1;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800265 bool end_set = false;
266 bool end_always = false;
267
268 LogTimeEntry::lock();
269
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700270 LastLogTimes::iterator times = mTimes.begin();
271 while(times != mTimes.end()) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800272 LogTimeEntry* entry = (*times);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800273 if (entry->owned_Locked()) {
274 if (!entry->mNonBlock) {
275 end_always = true;
276 break;
277 }
278 if (!end_set || (end <= entry->mEnd)) {
279 end = entry->mEnd;
280 end_set = true;
281 }
282 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700283 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800284 }
285
286 if (end_always
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800287 || (end_set && (end >= (*last)->getSequence()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800288 mLogElements.push_back(elem);
289 } else {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800290 mLogElements.insert(last, elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800291 }
292
293 LogTimeEntry::unlock();
294 }
295
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700296 stats.add(elem);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800297 maybePrune(elem->getLogId());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800298}
299
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700300// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800301//
302// mLogElementsLock must be held when this function is called.
303void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800304 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700305 unsigned long maxSize = log_buffer_size(id);
306 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700307 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700308 size_t elements = stats.realElements(id);
309 size_t minElements = elements / 100;
310 if (minElements < minPrune) {
311 minElements = minPrune;
312 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700313 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700314 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700315 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800316 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700317 if (pruneRows > maxPrune) {
318 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700319 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800320 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800321 }
322}
323
Mark Salyzyn831aa292015-09-03 16:08:50 -0700324LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700325 LogBufferElementCollection::iterator it, bool coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700326 LogBufferElement *element = *it;
327 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700328
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700329 // Remove iterator references in the various lists that will become stale
330 // after the element is erased from the main logging list.
331
Mark Salyzyn6a066942016-07-14 15:34:30 -0700332 { // start of scope for found iterator
333 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
334 element->getTag() : element->getUid();
335 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
336 if ((found != mLastWorst[id].end()) && (it == found->second)) {
337 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700338 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700339 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700340
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700341 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700342 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700343 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
344 // long term code stability, find() check should be fast for those ids.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700345 LogBufferPidIteratorMap::iterator found =
346 mLastWorstPidOfSystem[id].find(element->getPid());
347 if ((found != mLastWorstPidOfSystem[id].end())
348 && (it == found->second)) {
349 mLastWorstPidOfSystem[id].erase(found);
350 }
351 }
352
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800353 bool setLast[LOG_ID_MAX];
354 bool doSetLast = false;
355 log_id_for_each(i) {
356 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
357 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700358#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
359 LogBufferElementCollection::iterator bad = it;
360 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
361 element->getTag() : element->getUid();
362#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700363 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800364 if (doSetLast) {
365 log_id_for_each(i) {
366 if (setLast[i]) {
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700367 if (__predict_false(it == mLogElements.end())) { // impossible
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800368 mLastSet[i] = false;
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700369 mLast[i] = mLogElements.begin();
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800370 } else {
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700371 mLast[i] = it; // push down the road as next-best-watermark
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800372 }
373 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800374 }
375 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700376#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
377 log_id_for_each(i) {
378 for(auto b : mLastWorst[i]) {
379 if (bad == b.second) {
380 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n",
381 i, b.first, key);
382 }
383 }
384 for(auto b : mLastWorstPidOfSystem[i]) {
385 if (bad == b.second) {
386 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n",
387 i, b.first);
388 }
389 }
390 if (mLastSet[i] && (bad == mLast[i])) {
391 android::prdebug("stale mLast[%d]\n", i);
392 mLastSet[i] = false;
393 mLast[i] = mLogElements.begin();
394 }
395 }
396#endif
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700397 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700398 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700399 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700400 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700401 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700402 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700403
404 return it;
405}
406
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700407// Define a temporary mechanism to report the last LogBufferElement pointer
408// for the specified uid, pid and tid. Used below to help merge-sort when
409// pruning for worst UID.
410class LogBufferElementKey {
411 const union {
412 struct {
413 uint16_t uid;
414 uint16_t pid;
415 uint16_t tid;
416 uint16_t padding;
417 } __packed;
418 uint64_t value;
419 } __packed;
420
421public:
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700422 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid):
423 uid(uid),
424 pid(pid),
425 tid(tid),
426 padding(0) {
427 }
Chih-Hung Hsieh1cc82ce2016-04-25 13:49:46 -0700428 explicit LogBufferElementKey(uint64_t key):value(key) { }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700429
430 uint64_t getKey() { return value; }
431};
432
Mark Salyzyn511338d2015-05-19 09:12:30 -0700433class LogBufferElementLast {
434
435 typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap;
436 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700437
438public:
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700439
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700440 bool coalesce(LogBufferElement *element, unsigned short dropped) {
441 LogBufferElementKey key(element->getUid(),
442 element->getPid(),
443 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700444 LogBufferElementMap::iterator it = map.find(key.getKey());
445 if (it != map.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700446 LogBufferElement *found = it->second;
447 unsigned short moreDropped = found->getDropped();
448 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700449 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700450 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700451 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700452 return true;
453 }
454 }
455 return false;
456 }
457
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700458 void add(LogBufferElement *element) {
459 LogBufferElementKey key(element->getUid(),
460 element->getPid(),
461 element->getTid());
462 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700463 }
464
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700465 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700466 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700467 }
468
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700469 void clear(LogBufferElement *element) {
470 uint64_t current = element->getRealTime().nsec()
Mark Salyzyn047cc072015-06-04 13:35:30 -0700471 - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn511338d2015-05-19 09:12:30 -0700472 for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700473 LogBufferElement *mapElement = it->second;
474 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD)
475 && (current > mapElement->getRealTime().nsec())) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700476 it = map.erase(it);
477 } else {
478 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700479 }
480 }
481 }
482
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700483};
484
Mark Salyzyn0175b072014-02-26 09:50:16 -0800485// prune "pruneRows" of type "id" from the buffer.
486//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700487// This garbage collection task is used to expire log entries. It is called to
488// remove all logs (clear), all UID logs (unprivileged clear), or every
489// 256 or 10% of the total logs (whichever is less) to prune the logs.
490//
491// First there is a prep phase where we discover the reader region lock that
492// acts as a backstop to any pruning activity to stop there and go no further.
493//
494// There are three major pruning loops that follow. All expire from the oldest
495// entries. Since there are multiple log buffers, the Android logging facility
496// will appear to drop entries 'in the middle' when looking at multiple log
497// sources and buffers. This effect is slightly more prominent when we prune
498// the worst offender by logging source. Thus the logs slowly loose content
499// and value as you move back in time. This is preferred since chatty sources
500// invariably move the logs value down faster as less chatty sources would be
501// expired in the noise.
502//
503// The first loop performs blacklisting and worst offender pruning. Falling
504// through when there are no notable worst offenders and have not hit the
505// region lock preventing further worst offender pruning. This loop also looks
506// after managing the chatty log entries and merging to help provide
507// statistical basis for blame. The chatty entries are not a notification of
508// how much logs you may have, but instead represent how much logs you would
509// have had in a virtual log buffer that is extended to cover all the in-memory
510// logs without loss. They last much longer than the represented pruned logs
511// since they get multiplied by the gains in the non-chatty log sources.
512//
513// The second loop get complicated because an algorithm of watermarks and
514// history is maintained to reduce the order and keep processing time
515// down to a minimum at scale. These algorithms can be costly in the face
516// of larger log buffers, or severly limited processing time granted to a
517// background task at lowest priority.
518//
519// This second loop does straight-up expiration from the end of the logs
520// (again, remember for the specified log buffer id) but does some whitelist
521// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
522// spam filtration all take priority. This second loop also checks if a region
523// lock is causing us to buffer too much in the logs to help the reader(s),
524// and will tell the slowest reader thread to skip log entries, and if
525// persistent and hits a further threshold, kill the reader thread.
526//
527// The third thread is optional, and only gets hit if there was a whitelist
528// and more needs to be pruned against the backstop of the region lock.
529//
Mark Salyzyn0175b072014-02-26 09:50:16 -0800530// mLogElementsLock must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700531//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700532bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800533 LogTimeEntry *oldest = NULL;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700534 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700535 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800536
537 LogTimeEntry::lock();
538
539 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700540 LastLogTimes::iterator times = mTimes.begin();
541 while(times != mTimes.end()) {
542 LogTimeEntry *entry = (*times);
TraianX Schiauda6495d2014-12-17 10:53:41 +0200543 if (entry->owned_Locked() && entry->isWatching(id)
Mark Salyzynb75cce02015-11-30 11:35:56 -0800544 && (!oldest ||
545 (oldest->mStart > entry->mStart) ||
546 ((oldest->mStart == entry->mStart) &&
547 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800548 oldest = entry;
549 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700550 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800551 }
552
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800553 LogBufferElementCollection::iterator it;
554
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700555 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700556 // Only here if clear all request from non system source, so chatty
557 // filter logistics is not required.
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800558 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
559 while (it != mLogElements.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700560 LogBufferElement *element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700561
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700562 if ((element->getLogId() != id) || (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700563 ++it;
564 continue;
565 }
566
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800567 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
568 mLast[id] = it;
569 mLastSet[id] = true;
570 }
571
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700572 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700573 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800574 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
575 oldest->triggerReader_Locked();
576 } else {
577 oldest->triggerSkip_Locked(id, pruneRows);
578 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700579 break;
580 }
581
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700582 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700583 if (--pruneRows == 0) {
584 break;
585 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700586 }
587 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700588 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700589 }
590
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700591 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn083b0372015-12-04 10:59:45 -0800592 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700593 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800594 // recalculate the worst offender on every batched pass
Mark Salyzyn6a066942016-07-14 15:34:30 -0700595 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800596 size_t worst_sizes = 0;
597 size_t second_worst_sizes = 0;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700598 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800599
Mark Salyzynae769232015-03-17 17:17:25 -0700600 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700601 // Calculate threshold as 12.5% of available storage
602 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700603
Mark Salyzyn6a066942016-07-14 15:34:30 -0700604 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
605 stats.sortTags(AID_ROOT, (pid_t)0, 2, id).findWorst(
606 worst, worst_sizes, second_worst_sizes, threshold);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700607 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700608 } else {
609 stats.sort(AID_ROOT, (pid_t)0, 2, id).findWorst(
610 worst, worst_sizes, second_worst_sizes, threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700611
Mark Salyzyn6a066942016-07-14 15:34:30 -0700612 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
613 stats.sortPids(worst, (pid_t)0, 2, id).findWorst(
614 worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700615 }
616 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800617 }
618
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700619 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700620 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700621 break;
622 }
623
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800624 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700625 bool leading = true;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800626 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700627 // Perform at least one mandatory garbage collection cycle in following
628 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700629 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700630 // - check age-out of preserved logs
631 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700632 if (!gc && (worst != -1)) {
633 { // begin scope for worst found iterator
634 LogBufferIteratorMap::iterator found = mLastWorst[id].find(worst);
635 if ((found != mLastWorst[id].end())
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700636 && (found->second != mLogElements.end())) {
637 leading = false;
638 it = found->second;
639 }
640 }
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700641 if (worstPid) { // begin scope for pid worst found iterator
642 // FYI: worstPid only set if !LOG_ID_EVENTS and
643 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700644 LogBufferPidIteratorMap::iterator found
645 = mLastWorstPidOfSystem[id].find(worstPid);
646 if ((found != mLastWorstPidOfSystem[id].end())
647 && (found->second != mLogElements.end())) {
648 leading = false;
649 it = found->second;
650 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700651 }
652 }
Mark Salyzynccfe8442015-08-24 13:43:27 -0700653 static const timespec too_old = {
654 EXPIRE_HOUR_THRESHOLD * 60 * 60, 0
655 };
656 LogBufferElementCollection::iterator lastt;
657 lastt = mLogElements.end();
658 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700659 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700660 while (it != mLogElements.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700661 LogBufferElement *element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800662
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700663 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700664 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800665 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
666 oldest->triggerReader_Locked();
667 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800668 break;
669 }
670
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700671 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800672 ++it;
673 continue;
674 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700675 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800676
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800677 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
678 mLast[id] = it;
679 mLastSet[id] = true;
680 }
681
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700682 unsigned short dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800683
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700684 // remove any leading drops
685 if (leading && dropped) {
686 it = erase(it);
687 continue;
688 }
689
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700690 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700691 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700692 continue;
693 }
694
Mark Salyzyn6a066942016-07-14 15:34:30 -0700695 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
696 element->getTag() :
697 element->getUid();
698
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700699 if (hasBlacklist && mPrune.naughty(element)) {
700 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700701 it = erase(it);
702 if (dropped) {
703 continue;
704 }
705
706 pruneRows--;
707 if (pruneRows == 0) {
708 break;
709 }
710
Mark Salyzyn6a066942016-07-14 15:34:30 -0700711 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700712 kick = true;
713 if (worst_sizes < second_worst_sizes) {
714 break;
715 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700716 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700717 }
718 continue;
719 }
720
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700721 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old))
722 || (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700723 break;
724 }
725
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700726 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700727 last.add(element);
728 if (worstPid
729 && ((!gc && (element->getPid() == worstPid))
Mark Salyzyn6a066942016-07-14 15:34:30 -0700730 || (mLastWorstPidOfSystem[id].find(element->getPid())
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700731 == mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700732 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700733 // watermark if current one empty. id is not LOG_ID_EVENTS
734 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700735 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700736 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700737 if ((!gc && !worstPid && (key == worst))
738 || (mLastWorst[id].find(key) == mLastWorst[id].end())) {
739 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700740 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800741 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700742 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800743 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700744
Mark Salyzyn6a066942016-07-14 15:34:30 -0700745 if ((key != worst)
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700746 || (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700747 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700748 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700749 ++it;
750 continue;
751 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700752 // key == worst below here
753 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700754
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700755 pruneRows--;
756 if (pruneRows == 0) {
757 break;
758 }
759
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700760 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700761
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700762 unsigned short len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700763
764 // do not create any leading drops
765 if (leading) {
766 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700767 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700768 stats.drop(element);
769 element->setDropped(1);
770 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700771 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700772 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700773 last.add(element);
774 if (worstPid && (!gc
775 || (mLastWorstPidOfSystem[id].find(worstPid)
776 == mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700777 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700778 // watermark if current one empty. id is not
779 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700780 mLastWorstPidOfSystem[id][worstPid] = it;
781 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700782 if ((!gc && !worstPid) ||
783 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
784 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700785 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700786 ++it;
787 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700788 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700789 if (worst_sizes < second_worst_sizes) {
790 break;
791 }
792 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800793 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700794 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800795
Mark Salyzyn1c950472014-04-01 17:19:47 -0700796 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800797 break; // the following loop will ask bad clients to skip/drop
798 }
799 }
800
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800801 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800802 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800803 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800804 while((pruneRows > 0) && (it != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700805 LogBufferElement *element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700806
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700807 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700808 it++;
809 continue;
810 }
811
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800812 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
813 mLast[id] = it;
814 mLastSet[id] = true;
815 }
816
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700817 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700818 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700819 if (whitelist) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800820 break;
821 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700822
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700823 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
824 // kick a misbehaving log reader client off the island
825 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800826 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
827 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700828 } else {
829 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800830 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700831 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800832 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700833
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700834 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
835 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700836 whitelist = true;
837 it++;
838 continue;
839 }
840
841 it = erase(it);
842 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800843 }
844
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700845 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800846 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800847 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800848 while((it != mLogElements.end()) && (pruneRows > 0)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700849 LogBufferElement *element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700850
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700851 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700852 ++it;
853 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800854 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700855
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800856 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
857 mLast[id] = it;
858 mLastSet[id] = true;
859 }
860
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700861 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700862 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700863 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
864 // kick a misbehaving log reader client off the island
865 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800866 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
867 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700868 } else {
869 oldest->triggerSkip_Locked(id, pruneRows);
870 }
871 break;
872 }
873
874 it = erase(it);
875 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800876 }
877 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800878
Mark Salyzyn0175b072014-02-26 09:50:16 -0800879 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700880
881 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800882}
883
884// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -0700885bool LogBuffer::clear(log_id_t id, uid_t uid) {
886 bool busy = true;
887 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
888 for (int retry = 4;;) {
889 if (retry == 1) { // last pass
890 // Check if it is still busy after the sleep, we say prune
891 // one entry, not another clear run, so we are looking for
892 // the quick side effect of the return value to tell us if
893 // we have a _blocked_ reader.
894 pthread_mutex_lock(&mLogElementsLock);
895 busy = prune(id, 1, uid);
896 pthread_mutex_unlock(&mLogElementsLock);
897 // It is still busy, blocked reader(s), lets kill them all!
898 // otherwise, lets be a good citizen and preserve the slow
899 // readers and let the clear run (below) deal with determining
900 // if we are still blocked and return an error code to caller.
901 if (busy) {
902 LogTimeEntry::lock();
903 LastLogTimes::iterator times = mTimes.begin();
904 while (times != mTimes.end()) {
905 LogTimeEntry *entry = (*times);
906 // Killer punch
907 if (entry->owned_Locked() && entry->isWatching(id)) {
908 entry->release_Locked();
909 }
910 times++;
911 }
912 LogTimeEntry::unlock();
913 }
914 }
915 pthread_mutex_lock(&mLogElementsLock);
916 busy = prune(id, ULONG_MAX, uid);
917 pthread_mutex_unlock(&mLogElementsLock);
918 if (!busy || !--retry) {
919 break;
920 }
921 sleep (1); // Let reader(s) catch up after notification
922 }
923 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800924}
925
926// get the used space associated with "id".
927unsigned long LogBuffer::getSizeUsed(log_id_t id) {
928 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800929 size_t retval = stats.sizes(id);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800930 pthread_mutex_unlock(&mLogElementsLock);
931 return retval;
932}
933
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800934// set the total space allocated to "id"
935int LogBuffer::setSize(log_id_t id, unsigned long size) {
936 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -0700937 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800938 return -1;
939 }
940 pthread_mutex_lock(&mLogElementsLock);
941 log_buffer_size(id) = size;
942 pthread_mutex_unlock(&mLogElementsLock);
943 return 0;
944}
945
946// get the total space allocated to "id"
947unsigned long LogBuffer::getSize(log_id_t id) {
948 pthread_mutex_lock(&mLogElementsLock);
949 size_t retval = log_buffer_size(id);
950 pthread_mutex_unlock(&mLogElementsLock);
951 return retval;
952}
953
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800954uint64_t LogBuffer::flushTo(
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800955 SocketClient *reader, const uint64_t start,
956 bool privileged, bool security,
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800957 int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800958 LogBufferElementCollection::iterator it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800959 uint64_t max = start;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800960 uid_t uid = reader->getUid();
961
962 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600963
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800964 if (start <= 1) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600965 // client wants to start from the beginning
966 it = mLogElements.begin();
967 } else {
968 // Client wants to start from some specified time. Chances are
969 // we are better off starting from the end of the time sorted list.
970 for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
971 --it;
972 LogBufferElement *element = *it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800973 if (element->getSequence() <= start) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600974 it++;
975 break;
976 }
977 }
978 }
979
980 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800981 LogBufferElement *element = *it;
982
983 if (!privileged && (element->getUid() != uid)) {
984 continue;
985 }
986
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800987 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
988 continue;
989 }
990
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800991 if (element->getSequence() <= start) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800992 continue;
993 }
994
995 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800996 if (filter) {
997 int ret = (*filter)(element, arg);
998 if (ret == false) {
999 continue;
1000 }
1001 if (ret != true) {
1002 break;
1003 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001004 }
1005
1006 pthread_mutex_unlock(&mLogElementsLock);
1007
1008 // range locking in LastLogTimes looks after us
Mark Salyzyn7b873652015-12-03 15:38:35 -08001009 max = element->flushTo(reader, this, privileged);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001010
1011 if (max == element->FLUSH_ERROR) {
1012 return max;
1013 }
1014
1015 pthread_mutex_lock(&mLogElementsLock);
1016 }
1017 pthread_mutex_unlock(&mLogElementsLock);
1018
1019 return max;
1020}
Mark Salyzyn34facab2014-02-06 14:48:50 -08001021
Mark Salyzynee3b8382015-12-17 09:58:43 -08001022std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1023 unsigned int logMask) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001024 pthread_mutex_lock(&mLogElementsLock);
1025
Mark Salyzynee3b8382015-12-17 09:58:43 -08001026 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001027
1028 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn73160ac2015-08-20 10:01:44 -07001029
1030 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001031}