blob: a009433096b5b558e21fc27e665c85f5791b5080 [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 Salyzyn0175b072014-02-26 09:50:16 -080036
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070037#ifndef __predict_false
38#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
39#endif
40
Mark Salyzyndfa7a072014-02-11 12:29:31 -080041// Default
Mark Salyzyndfa7a072014-02-11 12:29:31 -080042#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn671e3432014-05-06 07:34:59 -070043
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070044void LogBuffer::init() {
Mark Salyzyndfa7a072014-02-11 12:29:31 -080045 log_id_for_each(i) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -080046 mLastSet[i] = false;
47 mLast[i] = mLogElements.begin();
48
Mark Salyzynf10e2732016-09-27 13:08:23 -070049 if (setSize(i, __android_logger_get_buffer_size(i))) {
Mark Salyzyn57a0af92014-05-09 17:44:18 -070050 setSize(i, LOG_BUFFER_MIN_SIZE);
51 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -080052 }
Mark Salyzynb6bee332015-09-08 08:56:32 -070053 bool lastMonotonic = monotonic;
Mark Salyzynba7a9a02015-12-01 15:57:25 -080054 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzynb75cce02015-11-30 11:35:56 -080055 if (lastMonotonic != monotonic) {
56 //
57 // Fixup all timestamps, may not be 100% accurate, but better than
58 // throwing what we have away when we get 'surprised' by a change.
59 // In-place element fixup so no need to check reader-lock. Entries
60 // should already be in timestamp order, but we could end up with a
61 // few out-of-order entries if new monotonics come in before we
62 // are notified of the reinit change in status. A Typical example would
63 // be:
64 // --------- beginning of system
65 // 10.494082 184 201 D Cryptfs : Just triggered post_fs_data
66 // --------- beginning of kernel
67 // 0.000000 0 0 I : Initializing cgroup subsys
68 // as the act of mounting /data would trigger persist.logd.timestamp to
69 // be corrected. 1/30 corner case YMMV.
70 //
71 pthread_mutex_lock(&mLogElementsLock);
72 LogBufferElementCollection::iterator it = mLogElements.begin();
73 while((it != mLogElements.end())) {
74 LogBufferElement *e = *it;
75 if (monotonic) {
76 if (!android::isMonotonic(e->mRealTime)) {
77 LogKlog::convertRealToMonotonic(e->mRealTime);
78 }
79 } else {
80 if (android::isMonotonic(e->mRealTime)) {
81 LogKlog::convertMonotonicToReal(e->mRealTime);
82 }
83 }
84 ++it;
85 }
86 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzynb6bee332015-09-08 08:56:32 -070087 }
88
Mark Salyzynb75cce02015-11-30 11:35:56 -080089 // We may have been triggered by a SIGHUP. Release any sleeping reader
90 // threads to dump their current content.
Mark Salyzynb6bee332015-09-08 08:56:32 -070091 //
Mark Salyzynb75cce02015-11-30 11:35:56 -080092 // NB: this is _not_ performed in the context of a SIGHUP, it is
93 // performed during startup, and in context of reinit administrative thread
94 LogTimeEntry::lock();
95
96 LastLogTimes::iterator times = mTimes.begin();
97 while(times != mTimes.end()) {
98 LogTimeEntry *entry = (*times);
99 if (entry->owned_Locked()) {
100 entry->triggerReader_Locked();
Mark Salyzynb6bee332015-09-08 08:56:32 -0700101 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800102 times++;
Mark Salyzynb6bee332015-09-08 08:56:32 -0700103 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800104
105 LogTimeEntry::unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800106}
107
Mark Salyzynb6bee332015-09-08 08:56:32 -0700108LogBuffer::LogBuffer(LastLogTimes *times):
Mark Salyzynba7a9a02015-12-01 15:57:25 -0800109 monotonic(android_log_clockid() == CLOCK_MONOTONIC),
Mark Salyzynb6bee332015-09-08 08:56:32 -0700110 mTimes(*times) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700111 pthread_mutex_init(&mLogElementsLock, NULL);
112
113 init();
114}
115
Mark Salyzyn202e1532015-02-09 08:21:05 -0800116int LogBuffer::log(log_id_t log_id, log_time realtime,
117 uid_t uid, pid_t pid, pid_t tid,
118 const char *msg, unsigned short len) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800119 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800120 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800121 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700122
Mark Salyzyn0175b072014-02-26 09:50:16 -0800123 LogBufferElement *elem = new LogBufferElement(log_id, realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700124 uid, pid, tid, msg, len);
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000125 if (log_id != LOG_ID_SECURITY) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800126 int prio = ANDROID_LOG_INFO;
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000127 const char *tag = NULL;
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700128 size_t len = 0;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800129 if (log_id == LOG_ID_EVENTS) {
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700130 tag = android::tagToName(&len, elem->getTag());
Mark Salyzyn083b0372015-12-04 10:59:45 -0800131 } else {
132 prio = *msg;
133 tag = msg + 1;
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700134 len = strlen(tag);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800135 }
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700136 if (!__android_log_is_loggable_len(prio, tag, len, ANDROID_LOG_VERBOSE)) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800137 // Log traffic received to total
138 pthread_mutex_lock(&mLogElementsLock);
139 stats.add(elem);
140 stats.subtract(elem);
141 pthread_mutex_unlock(&mLogElementsLock);
142 delete elem;
143 return -EACCES;
144 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700145 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800146
147 pthread_mutex_lock(&mLogElementsLock);
148
149 // Insert elements in time sorted order if possible
150 // NB: if end is region locked, place element at end of list
151 LogBufferElementCollection::iterator it = mLogElements.end();
152 LogBufferElementCollection::iterator last = it;
Mark Salyzyneae155e2014-10-13 16:49:47 -0700153 while (last != mLogElements.begin()) {
154 --it;
Mark Salyzync03e72c2014-02-18 11:23:53 -0800155 if ((*it)->getRealTime() <= realtime) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800156 break;
157 }
158 last = it;
159 }
Mark Salyzync03e72c2014-02-18 11:23:53 -0800160
Mark Salyzyn0175b072014-02-26 09:50:16 -0800161 if (last == mLogElements.end()) {
162 mLogElements.push_back(elem);
163 } else {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800164 uint64_t end = 1;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800165 bool end_set = false;
166 bool end_always = false;
167
168 LogTimeEntry::lock();
169
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700170 LastLogTimes::iterator times = mTimes.begin();
171 while(times != mTimes.end()) {
172 LogTimeEntry *entry = (*times);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800173 if (entry->owned_Locked()) {
174 if (!entry->mNonBlock) {
175 end_always = true;
176 break;
177 }
178 if (!end_set || (end <= entry->mEnd)) {
179 end = entry->mEnd;
180 end_set = true;
181 }
182 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700183 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800184 }
185
186 if (end_always
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800187 || (end_set && (end >= (*last)->getSequence()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800188 mLogElements.push_back(elem);
189 } else {
190 mLogElements.insert(last,elem);
191 }
192
193 LogTimeEntry::unlock();
194 }
195
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700196 stats.add(elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800197 maybePrune(log_id);
198 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn202e1532015-02-09 08:21:05 -0800199
200 return len;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800201}
202
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700203// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800204//
205// mLogElementsLock must be held when this function is called.
206void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800207 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700208 unsigned long maxSize = log_buffer_size(id);
209 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700210 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700211 size_t elements = stats.realElements(id);
212 size_t minElements = elements / 100;
213 if (minElements < minPrune) {
214 minElements = minPrune;
215 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700216 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700217 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700218 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800219 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700220 if (pruneRows > maxPrune) {
221 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700222 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800223 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800224 }
225}
226
Mark Salyzyn831aa292015-09-03 16:08:50 -0700227LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700228 LogBufferElementCollection::iterator it, bool coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700229 LogBufferElement *element = *it;
230 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700231
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700232 // Remove iterator references in the various lists that will become stale
233 // after the element is erased from the main logging list.
234
Mark Salyzyn6a066942016-07-14 15:34:30 -0700235 { // start of scope for found iterator
236 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
237 element->getTag() : element->getUid();
238 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
239 if ((found != mLastWorst[id].end()) && (it == found->second)) {
240 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700241 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700242 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700243
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700244 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700245 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700246 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
247 // long term code stability, find() check should be fast for those ids.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700248 LogBufferPidIteratorMap::iterator found =
249 mLastWorstPidOfSystem[id].find(element->getPid());
250 if ((found != mLastWorstPidOfSystem[id].end())
251 && (it == found->second)) {
252 mLastWorstPidOfSystem[id].erase(found);
253 }
254 }
255
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800256 bool setLast[LOG_ID_MAX];
257 bool doSetLast = false;
258 log_id_for_each(i) {
259 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
260 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700261#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
262 LogBufferElementCollection::iterator bad = it;
263 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
264 element->getTag() : element->getUid();
265#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700266 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800267 if (doSetLast) {
268 log_id_for_each(i) {
269 if (setLast[i]) {
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700270 if (__predict_false(it == mLogElements.end())) { // impossible
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800271 mLastSet[i] = false;
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700272 mLast[i] = mLogElements.begin();
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800273 } else {
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700274 mLast[i] = it; // push down the road as next-best-watermark
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800275 }
276 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800277 }
278 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700279#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
280 log_id_for_each(i) {
281 for(auto b : mLastWorst[i]) {
282 if (bad == b.second) {
283 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n",
284 i, b.first, key);
285 }
286 }
287 for(auto b : mLastWorstPidOfSystem[i]) {
288 if (bad == b.second) {
289 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n",
290 i, b.first);
291 }
292 }
293 if (mLastSet[i] && (bad == mLast[i])) {
294 android::prdebug("stale mLast[%d]\n", i);
295 mLastSet[i] = false;
296 mLast[i] = mLogElements.begin();
297 }
298 }
299#endif
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700300 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700301 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700302 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700303 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700304 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700305 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700306
307 return it;
308}
309
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700310// Define a temporary mechanism to report the last LogBufferElement pointer
311// for the specified uid, pid and tid. Used below to help merge-sort when
312// pruning for worst UID.
313class LogBufferElementKey {
314 const union {
315 struct {
316 uint16_t uid;
317 uint16_t pid;
318 uint16_t tid;
319 uint16_t padding;
320 } __packed;
321 uint64_t value;
322 } __packed;
323
324public:
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700325 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid):
326 uid(uid),
327 pid(pid),
328 tid(tid),
329 padding(0) {
330 }
Chih-Hung Hsieh1cc82ce2016-04-25 13:49:46 -0700331 explicit LogBufferElementKey(uint64_t key):value(key) { }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700332
333 uint64_t getKey() { return value; }
334};
335
Mark Salyzyn511338d2015-05-19 09:12:30 -0700336class LogBufferElementLast {
337
338 typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap;
339 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700340
341public:
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700342
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700343 bool coalesce(LogBufferElement *element, unsigned short dropped) {
344 LogBufferElementKey key(element->getUid(),
345 element->getPid(),
346 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700347 LogBufferElementMap::iterator it = map.find(key.getKey());
348 if (it != map.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700349 LogBufferElement *found = it->second;
350 unsigned short moreDropped = found->getDropped();
351 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700352 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700353 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700354 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700355 return true;
356 }
357 }
358 return false;
359 }
360
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700361 void add(LogBufferElement *element) {
362 LogBufferElementKey key(element->getUid(),
363 element->getPid(),
364 element->getTid());
365 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700366 }
367
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700368 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700369 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700370 }
371
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700372 void clear(LogBufferElement *element) {
373 uint64_t current = element->getRealTime().nsec()
Mark Salyzyn047cc072015-06-04 13:35:30 -0700374 - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn511338d2015-05-19 09:12:30 -0700375 for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700376 LogBufferElement *mapElement = it->second;
377 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD)
378 && (current > mapElement->getRealTime().nsec())) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700379 it = map.erase(it);
380 } else {
381 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700382 }
383 }
384 }
385
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700386};
387
Mark Salyzyn0175b072014-02-26 09:50:16 -0800388// prune "pruneRows" of type "id" from the buffer.
389//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700390// This garbage collection task is used to expire log entries. It is called to
391// remove all logs (clear), all UID logs (unprivileged clear), or every
392// 256 or 10% of the total logs (whichever is less) to prune the logs.
393//
394// First there is a prep phase where we discover the reader region lock that
395// acts as a backstop to any pruning activity to stop there and go no further.
396//
397// There are three major pruning loops that follow. All expire from the oldest
398// entries. Since there are multiple log buffers, the Android logging facility
399// will appear to drop entries 'in the middle' when looking at multiple log
400// sources and buffers. This effect is slightly more prominent when we prune
401// the worst offender by logging source. Thus the logs slowly loose content
402// and value as you move back in time. This is preferred since chatty sources
403// invariably move the logs value down faster as less chatty sources would be
404// expired in the noise.
405//
406// The first loop performs blacklisting and worst offender pruning. Falling
407// through when there are no notable worst offenders and have not hit the
408// region lock preventing further worst offender pruning. This loop also looks
409// after managing the chatty log entries and merging to help provide
410// statistical basis for blame. The chatty entries are not a notification of
411// how much logs you may have, but instead represent how much logs you would
412// have had in a virtual log buffer that is extended to cover all the in-memory
413// logs without loss. They last much longer than the represented pruned logs
414// since they get multiplied by the gains in the non-chatty log sources.
415//
416// The second loop get complicated because an algorithm of watermarks and
417// history is maintained to reduce the order and keep processing time
418// down to a minimum at scale. These algorithms can be costly in the face
419// of larger log buffers, or severly limited processing time granted to a
420// background task at lowest priority.
421//
422// This second loop does straight-up expiration from the end of the logs
423// (again, remember for the specified log buffer id) but does some whitelist
424// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
425// spam filtration all take priority. This second loop also checks if a region
426// lock is causing us to buffer too much in the logs to help the reader(s),
427// and will tell the slowest reader thread to skip log entries, and if
428// persistent and hits a further threshold, kill the reader thread.
429//
430// The third thread is optional, and only gets hit if there was a whitelist
431// and more needs to be pruned against the backstop of the region lock.
432//
Mark Salyzyn0175b072014-02-26 09:50:16 -0800433// mLogElementsLock must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700434//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700435bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800436 LogTimeEntry *oldest = NULL;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700437 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700438 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800439
440 LogTimeEntry::lock();
441
442 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700443 LastLogTimes::iterator times = mTimes.begin();
444 while(times != mTimes.end()) {
445 LogTimeEntry *entry = (*times);
TraianX Schiauda6495d2014-12-17 10:53:41 +0200446 if (entry->owned_Locked() && entry->isWatching(id)
Mark Salyzynb75cce02015-11-30 11:35:56 -0800447 && (!oldest ||
448 (oldest->mStart > entry->mStart) ||
449 ((oldest->mStart == entry->mStart) &&
450 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800451 oldest = entry;
452 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700453 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800454 }
455
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800456 LogBufferElementCollection::iterator it;
457
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700458 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700459 // Only here if clear all request from non system source, so chatty
460 // filter logistics is not required.
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800461 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
462 while (it != mLogElements.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700463 LogBufferElement *element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700464
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700465 if ((element->getLogId() != id) || (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700466 ++it;
467 continue;
468 }
469
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800470 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
471 mLast[id] = it;
472 mLastSet[id] = true;
473 }
474
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700475 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700476 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800477 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
478 oldest->triggerReader_Locked();
479 } else {
480 oldest->triggerSkip_Locked(id, pruneRows);
481 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700482 break;
483 }
484
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700485 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700486 if (--pruneRows == 0) {
487 break;
488 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700489 }
490 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700491 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700492 }
493
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700494 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn083b0372015-12-04 10:59:45 -0800495 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700496 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800497 // recalculate the worst offender on every batched pass
Mark Salyzyn6a066942016-07-14 15:34:30 -0700498 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800499 size_t worst_sizes = 0;
500 size_t second_worst_sizes = 0;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700501 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800502
Mark Salyzynae769232015-03-17 17:17:25 -0700503 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700504 // Calculate threshold as 12.5% of available storage
505 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700506
Mark Salyzyn6a066942016-07-14 15:34:30 -0700507 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
508 stats.sortTags(AID_ROOT, (pid_t)0, 2, id).findWorst(
509 worst, worst_sizes, second_worst_sizes, threshold);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700510 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700511 } else {
512 stats.sort(AID_ROOT, (pid_t)0, 2, id).findWorst(
513 worst, worst_sizes, second_worst_sizes, threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700514
Mark Salyzyn6a066942016-07-14 15:34:30 -0700515 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
516 stats.sortPids(worst, (pid_t)0, 2, id).findWorst(
517 worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700518 }
519 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800520 }
521
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700522 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700523 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700524 break;
525 }
526
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800527 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700528 bool leading = true;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800529 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700530 // Perform at least one mandatory garbage collection cycle in following
531 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700532 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700533 // - check age-out of preserved logs
534 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700535 if (!gc && (worst != -1)) {
536 { // begin scope for worst found iterator
537 LogBufferIteratorMap::iterator found = mLastWorst[id].find(worst);
538 if ((found != mLastWorst[id].end())
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700539 && (found->second != mLogElements.end())) {
540 leading = false;
541 it = found->second;
542 }
543 }
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700544 if (worstPid) { // begin scope for pid worst found iterator
545 // FYI: worstPid only set if !LOG_ID_EVENTS and
546 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700547 LogBufferPidIteratorMap::iterator found
548 = mLastWorstPidOfSystem[id].find(worstPid);
549 if ((found != mLastWorstPidOfSystem[id].end())
550 && (found->second != mLogElements.end())) {
551 leading = false;
552 it = found->second;
553 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700554 }
555 }
Mark Salyzynccfe8442015-08-24 13:43:27 -0700556 static const timespec too_old = {
557 EXPIRE_HOUR_THRESHOLD * 60 * 60, 0
558 };
559 LogBufferElementCollection::iterator lastt;
560 lastt = mLogElements.end();
561 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700562 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700563 while (it != mLogElements.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700564 LogBufferElement *element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800565
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700566 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700567 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800568 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
569 oldest->triggerReader_Locked();
570 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800571 break;
572 }
573
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700574 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800575 ++it;
576 continue;
577 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700578 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800579
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800580 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
581 mLast[id] = it;
582 mLastSet[id] = true;
583 }
584
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700585 unsigned short dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800586
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700587 // remove any leading drops
588 if (leading && dropped) {
589 it = erase(it);
590 continue;
591 }
592
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700593 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700594 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700595 continue;
596 }
597
Mark Salyzyn6a066942016-07-14 15:34:30 -0700598 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
599 element->getTag() :
600 element->getUid();
601
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700602 if (hasBlacklist && mPrune.naughty(element)) {
603 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700604 it = erase(it);
605 if (dropped) {
606 continue;
607 }
608
609 pruneRows--;
610 if (pruneRows == 0) {
611 break;
612 }
613
Mark Salyzyn6a066942016-07-14 15:34:30 -0700614 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700615 kick = true;
616 if (worst_sizes < second_worst_sizes) {
617 break;
618 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700619 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700620 }
621 continue;
622 }
623
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700624 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old))
625 || (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700626 break;
627 }
628
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700629 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700630 last.add(element);
631 if (worstPid
632 && ((!gc && (element->getPid() == worstPid))
Mark Salyzyn6a066942016-07-14 15:34:30 -0700633 || (mLastWorstPidOfSystem[id].find(element->getPid())
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700634 == mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700635 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700636 // watermark if current one empty. id is not LOG_ID_EVENTS
637 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700638 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700639 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700640 if ((!gc && !worstPid && (key == worst))
641 || (mLastWorst[id].find(key) == mLastWorst[id].end())) {
642 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700643 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800644 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700645 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800646 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700647
Mark Salyzyn6a066942016-07-14 15:34:30 -0700648 if ((key != worst)
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700649 || (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700650 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700651 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700652 ++it;
653 continue;
654 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700655 // key == worst below here
656 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700657
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700658 pruneRows--;
659 if (pruneRows == 0) {
660 break;
661 }
662
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700663 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700664
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700665 unsigned short len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700666
667 // do not create any leading drops
668 if (leading) {
669 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700670 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700671 stats.drop(element);
672 element->setDropped(1);
673 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700674 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700675 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700676 last.add(element);
677 if (worstPid && (!gc
678 || (mLastWorstPidOfSystem[id].find(worstPid)
679 == mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700680 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700681 // watermark if current one empty. id is not
682 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700683 mLastWorstPidOfSystem[id][worstPid] = it;
684 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700685 if ((!gc && !worstPid) ||
686 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
687 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700688 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700689 ++it;
690 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700691 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700692 if (worst_sizes < second_worst_sizes) {
693 break;
694 }
695 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800696 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700697 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800698
Mark Salyzyn1c950472014-04-01 17:19:47 -0700699 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800700 break; // the following loop will ask bad clients to skip/drop
701 }
702 }
703
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800704 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800705 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800706 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800707 while((pruneRows > 0) && (it != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700708 LogBufferElement *element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700709
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700710 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700711 it++;
712 continue;
713 }
714
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800715 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
716 mLast[id] = it;
717 mLastSet[id] = true;
718 }
719
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700720 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700721 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700722 if (whitelist) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800723 break;
724 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700725
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700726 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
727 // kick a misbehaving log reader client off the island
728 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800729 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
730 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700731 } else {
732 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800733 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700734 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800735 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700736
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700737 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
738 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700739 whitelist = true;
740 it++;
741 continue;
742 }
743
744 it = erase(it);
745 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800746 }
747
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700748 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800749 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800750 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800751 while((it != mLogElements.end()) && (pruneRows > 0)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700752 LogBufferElement *element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700753
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700754 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700755 ++it;
756 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800757 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700758
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800759 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
760 mLast[id] = it;
761 mLastSet[id] = true;
762 }
763
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700764 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700765 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700766 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
767 // kick a misbehaving log reader client off the island
768 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800769 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
770 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700771 } else {
772 oldest->triggerSkip_Locked(id, pruneRows);
773 }
774 break;
775 }
776
777 it = erase(it);
778 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800779 }
780 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800781
Mark Salyzyn0175b072014-02-26 09:50:16 -0800782 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700783
784 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800785}
786
787// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -0700788bool LogBuffer::clear(log_id_t id, uid_t uid) {
789 bool busy = true;
790 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
791 for (int retry = 4;;) {
792 if (retry == 1) { // last pass
793 // Check if it is still busy after the sleep, we say prune
794 // one entry, not another clear run, so we are looking for
795 // the quick side effect of the return value to tell us if
796 // we have a _blocked_ reader.
797 pthread_mutex_lock(&mLogElementsLock);
798 busy = prune(id, 1, uid);
799 pthread_mutex_unlock(&mLogElementsLock);
800 // It is still busy, blocked reader(s), lets kill them all!
801 // otherwise, lets be a good citizen and preserve the slow
802 // readers and let the clear run (below) deal with determining
803 // if we are still blocked and return an error code to caller.
804 if (busy) {
805 LogTimeEntry::lock();
806 LastLogTimes::iterator times = mTimes.begin();
807 while (times != mTimes.end()) {
808 LogTimeEntry *entry = (*times);
809 // Killer punch
810 if (entry->owned_Locked() && entry->isWatching(id)) {
811 entry->release_Locked();
812 }
813 times++;
814 }
815 LogTimeEntry::unlock();
816 }
817 }
818 pthread_mutex_lock(&mLogElementsLock);
819 busy = prune(id, ULONG_MAX, uid);
820 pthread_mutex_unlock(&mLogElementsLock);
821 if (!busy || !--retry) {
822 break;
823 }
824 sleep (1); // Let reader(s) catch up after notification
825 }
826 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800827}
828
829// get the used space associated with "id".
830unsigned long LogBuffer::getSizeUsed(log_id_t id) {
831 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800832 size_t retval = stats.sizes(id);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800833 pthread_mutex_unlock(&mLogElementsLock);
834 return retval;
835}
836
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800837// set the total space allocated to "id"
838int LogBuffer::setSize(log_id_t id, unsigned long size) {
839 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -0700840 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800841 return -1;
842 }
843 pthread_mutex_lock(&mLogElementsLock);
844 log_buffer_size(id) = size;
845 pthread_mutex_unlock(&mLogElementsLock);
846 return 0;
847}
848
849// get the total space allocated to "id"
850unsigned long LogBuffer::getSize(log_id_t id) {
851 pthread_mutex_lock(&mLogElementsLock);
852 size_t retval = log_buffer_size(id);
853 pthread_mutex_unlock(&mLogElementsLock);
854 return retval;
855}
856
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800857uint64_t LogBuffer::flushTo(
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800858 SocketClient *reader, const uint64_t start,
859 bool privileged, bool security,
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800860 int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800861 LogBufferElementCollection::iterator it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800862 uint64_t max = start;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800863 uid_t uid = reader->getUid();
864
865 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600866
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800867 if (start <= 1) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600868 // client wants to start from the beginning
869 it = mLogElements.begin();
870 } else {
871 // Client wants to start from some specified time. Chances are
872 // we are better off starting from the end of the time sorted list.
873 for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
874 --it;
875 LogBufferElement *element = *it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800876 if (element->getSequence() <= start) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600877 it++;
878 break;
879 }
880 }
881 }
882
883 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800884 LogBufferElement *element = *it;
885
886 if (!privileged && (element->getUid() != uid)) {
887 continue;
888 }
889
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800890 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
891 continue;
892 }
893
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800894 if (element->getSequence() <= start) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800895 continue;
896 }
897
898 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800899 if (filter) {
900 int ret = (*filter)(element, arg);
901 if (ret == false) {
902 continue;
903 }
904 if (ret != true) {
905 break;
906 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800907 }
908
909 pthread_mutex_unlock(&mLogElementsLock);
910
911 // range locking in LastLogTimes looks after us
Mark Salyzyn7b873652015-12-03 15:38:35 -0800912 max = element->flushTo(reader, this, privileged);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800913
914 if (max == element->FLUSH_ERROR) {
915 return max;
916 }
917
918 pthread_mutex_lock(&mLogElementsLock);
919 }
920 pthread_mutex_unlock(&mLogElementsLock);
921
922 return max;
923}
Mark Salyzyn34facab2014-02-06 14:48:50 -0800924
Mark Salyzynee3b8382015-12-17 09:58:43 -0800925std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
926 unsigned int logMask) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800927 pthread_mutex_lock(&mLogElementsLock);
928
Mark Salyzynee3b8382015-12-17 09:58:43 -0800929 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800930
931 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn73160ac2015-08-20 10:01:44 -0700932
933 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800934}