blob: 7f5fe4f8f21e4e5fd5b69558ee1fc0d2cd875d9b [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 */
16
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070017#include <ctype.h>
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -080018#include <errno.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080019#include <stdio.h>
20#include <string.h>
Mark Salyzyn7b85c592014-05-09 17:44:18 -070021#include <sys/user.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080022#include <time.h>
23#include <unistd.h>
24
Mark Salyzyn6a404192015-05-19 09:12:30 -070025#include <unordered_map>
26
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070027#include <cutils/properties.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080028#include <log/logger.h>
29
30#include "LogBuffer.h"
Mark Salyzyn2b4a7632015-09-08 08:56:32 -070031#include "LogKlog.h"
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070032#include "LogReader.h"
Mark Salyzyn12bac902014-02-26 09:50:16 -080033
Mark Salyzync89839a2014-02-11 12:29:31 -080034// Default
Mark Salyzyn9a012a72016-01-05 08:49:44 -080035#define LOG_BUFFER_SIZE (256 * 1024) // Tuned with ro.logd.size per-platform
Mark Salyzync89839a2014-02-11 12:29:31 -080036#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn7b85c592014-05-09 17:44:18 -070037#define LOG_BUFFER_MIN_SIZE (64 * 1024UL)
38#define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL)
39
40static bool valid_size(unsigned long value) {
41 if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) {
42 return false;
43 }
44
45 long pages = sysconf(_SC_PHYS_PAGES);
46 if (pages < 1) {
47 return true;
48 }
49
50 long pagesize = sysconf(_SC_PAGESIZE);
51 if (pagesize <= 1) {
52 pagesize = PAGE_SIZE;
53 }
54
55 // maximum memory impact a somewhat arbitrary ~3%
56 pages = (pages + 31) / 32;
57 unsigned long maximum = pages * pagesize;
58
59 if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) {
60 return true;
61 }
62
63 return value <= maximum;
64}
Mark Salyzyn12bac902014-02-26 09:50:16 -080065
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070066static unsigned long property_get_size(const char *key) {
67 char property[PROPERTY_VALUE_MAX];
68 property_get(key, property, "");
69
70 char *cp;
71 unsigned long value = strtoul(property, &cp, 10);
72
73 switch(*cp) {
74 case 'm':
75 case 'M':
76 value *= 1024;
77 /* FALLTHRU */
78 case 'k':
79 case 'K':
80 value *= 1024;
81 /* FALLTHRU */
82 case '\0':
83 break;
84
85 default:
86 value = 0;
87 }
88
Mark Salyzyn7b85c592014-05-09 17:44:18 -070089 if (!valid_size(value)) {
90 value = 0;
91 }
92
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070093 return value;
94}
95
Mark Salyzyn3fe25932015-03-10 16:45:17 -070096void LogBuffer::init() {
Mark Salyzyn7b85c592014-05-09 17:44:18 -070097 static const char global_tuneable[] = "persist.logd.size"; // Settings App
98 static const char global_default[] = "ro.logd.size"; // BoardConfig.mk
99
100 unsigned long default_size = property_get_size(global_tuneable);
101 if (!default_size) {
102 default_size = property_get_size(global_default);
Mark Salyzyn973d18c2015-12-14 13:07:12 -0800103 if (!default_size) {
Mark Salyzyne19143e2015-12-14 16:40:12 -0800104 default_size = property_get_bool("ro.config.low_ram",
105 BOOL_DEFAULT_FALSE)
106 ? LOG_BUFFER_MIN_SIZE // 64K
107 : LOG_BUFFER_SIZE; // 256K
Mark Salyzyn973d18c2015-12-14 13:07:12 -0800108 }
Mark Salyzyn7b85c592014-05-09 17:44:18 -0700109 }
Mark Salyzyn4e756fb2014-05-06 07:34:59 -0700110
Mark Salyzync89839a2014-02-11 12:29:31 -0800111 log_id_for_each(i) {
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800112 mLastSet[i] = false;
113 mLast[i] = mLogElements.begin();
114
Mark Salyzyn4e756fb2014-05-06 07:34:59 -0700115 char key[PROP_NAME_MAX];
Mark Salyzyn4e756fb2014-05-06 07:34:59 -0700116
Mark Salyzyn7b85c592014-05-09 17:44:18 -0700117 snprintf(key, sizeof(key), "%s.%s",
118 global_tuneable, android_log_id_to_name(i));
119 unsigned long property_size = property_get_size(key);
120
121 if (!property_size) {
122 snprintf(key, sizeof(key), "%s.%s",
123 global_default, android_log_id_to_name(i));
124 property_size = property_get_size(key);
125 }
126
127 if (!property_size) {
128 property_size = default_size;
129 }
130
131 if (!property_size) {
132 property_size = LOG_BUFFER_SIZE;
133 }
134
135 if (setSize(i, property_size)) {
136 setSize(i, LOG_BUFFER_MIN_SIZE);
137 }
Mark Salyzync89839a2014-02-11 12:29:31 -0800138 }
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700139 bool lastMonotonic = monotonic;
Mark Salyzyn2d4f9bc2015-12-01 15:57:25 -0800140 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzyn552b4752015-11-30 11:35:56 -0800141 if (lastMonotonic != monotonic) {
142 //
143 // Fixup all timestamps, may not be 100% accurate, but better than
144 // throwing what we have away when we get 'surprised' by a change.
145 // In-place element fixup so no need to check reader-lock. Entries
146 // should already be in timestamp order, but we could end up with a
147 // few out-of-order entries if new monotonics come in before we
148 // are notified of the reinit change in status. A Typical example would
149 // be:
150 // --------- beginning of system
151 // 10.494082 184 201 D Cryptfs : Just triggered post_fs_data
152 // --------- beginning of kernel
153 // 0.000000 0 0 I : Initializing cgroup subsys
154 // as the act of mounting /data would trigger persist.logd.timestamp to
155 // be corrected. 1/30 corner case YMMV.
156 //
157 pthread_mutex_lock(&mLogElementsLock);
158 LogBufferElementCollection::iterator it = mLogElements.begin();
159 while((it != mLogElements.end())) {
160 LogBufferElement *e = *it;
161 if (monotonic) {
162 if (!android::isMonotonic(e->mRealTime)) {
163 LogKlog::convertRealToMonotonic(e->mRealTime);
164 }
165 } else {
166 if (android::isMonotonic(e->mRealTime)) {
167 LogKlog::convertMonotonicToReal(e->mRealTime);
168 }
169 }
170 ++it;
171 }
172 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700173 }
174
Mark Salyzyn552b4752015-11-30 11:35:56 -0800175 // We may have been triggered by a SIGHUP. Release any sleeping reader
176 // threads to dump their current content.
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700177 //
Mark Salyzyn552b4752015-11-30 11:35:56 -0800178 // NB: this is _not_ performed in the context of a SIGHUP, it is
179 // performed during startup, and in context of reinit administrative thread
180 LogTimeEntry::lock();
181
182 LastLogTimes::iterator times = mTimes.begin();
183 while(times != mTimes.end()) {
184 LogTimeEntry *entry = (*times);
185 if (entry->owned_Locked()) {
186 entry->triggerReader_Locked();
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700187 }
Mark Salyzyn552b4752015-11-30 11:35:56 -0800188 times++;
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700189 }
Mark Salyzyn552b4752015-11-30 11:35:56 -0800190
191 LogTimeEntry::unlock();
Mark Salyzyn12bac902014-02-26 09:50:16 -0800192}
193
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700194LogBuffer::LogBuffer(LastLogTimes *times):
Mark Salyzyn2d4f9bc2015-12-01 15:57:25 -0800195 monotonic(android_log_clockid() == CLOCK_MONOTONIC),
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700196 mTimes(*times) {
Mark Salyzyn3fe25932015-03-10 16:45:17 -0700197 pthread_mutex_init(&mLogElementsLock, NULL);
198
199 init();
200}
201
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800202int LogBuffer::log(log_id_t log_id, log_time realtime,
203 uid_t uid, pid_t pid, pid_t tid,
204 const char *msg, unsigned short len) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800205 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800206 return -EINVAL;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800207 }
Mark Salyzyn19c91112014-10-02 13:07:05 -0700208
Mark Salyzyn12bac902014-02-26 09:50:16 -0800209 LogBufferElement *elem = new LogBufferElement(log_id, realtime,
Mark Salyzyn3661dd02014-03-20 16:09:38 -0700210 uid, pid, tid, msg, len);
Mark Salyzyn4a8d2502016-01-06 21:17:43 +0000211 if (log_id != LOG_ID_SECURITY) {
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800212 int prio = ANDROID_LOG_INFO;
Mark Salyzyn4a8d2502016-01-06 21:17:43 +0000213 const char *tag = NULL;
Mark Salyzyn0da1b472016-09-22 09:56:51 -0700214 size_t len = 0;
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800215 if (log_id == LOG_ID_EVENTS) {
Mark Salyzyn0da1b472016-09-22 09:56:51 -0700216 tag = android::tagToName(&len, elem->getTag());
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800217 } else {
218 prio = *msg;
219 tag = msg + 1;
Mark Salyzyn0da1b472016-09-22 09:56:51 -0700220 len = strlen(tag);
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800221 }
Mark Salyzyn0da1b472016-09-22 09:56:51 -0700222 if (!__android_log_is_loggable_len(prio, tag, len, ANDROID_LOG_VERBOSE)) {
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800223 // Log traffic received to total
224 pthread_mutex_lock(&mLogElementsLock);
225 stats.add(elem);
226 stats.subtract(elem);
227 pthread_mutex_unlock(&mLogElementsLock);
228 delete elem;
229 return -EACCES;
230 }
Mark Salyzyn19c91112014-10-02 13:07:05 -0700231 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800232
233 pthread_mutex_lock(&mLogElementsLock);
234
235 // Insert elements in time sorted order if possible
236 // NB: if end is region locked, place element at end of list
237 LogBufferElementCollection::iterator it = mLogElements.end();
238 LogBufferElementCollection::iterator last = it;
Mark Salyzyna222a772014-10-13 16:49:47 -0700239 while (last != mLogElements.begin()) {
240 --it;
Mark Salyzyn5b7a8d82014-02-18 11:23:53 -0800241 if ((*it)->getRealTime() <= realtime) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800242 break;
243 }
244 last = it;
245 }
Mark Salyzyn5b7a8d82014-02-18 11:23:53 -0800246
Mark Salyzyn12bac902014-02-26 09:50:16 -0800247 if (last == mLogElements.end()) {
248 mLogElements.push_back(elem);
249 } else {
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800250 uint64_t end = 1;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800251 bool end_set = false;
252 bool end_always = false;
253
254 LogTimeEntry::lock();
255
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700256 LastLogTimes::iterator times = mTimes.begin();
257 while(times != mTimes.end()) {
258 LogTimeEntry *entry = (*times);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800259 if (entry->owned_Locked()) {
260 if (!entry->mNonBlock) {
261 end_always = true;
262 break;
263 }
264 if (!end_set || (end <= entry->mEnd)) {
265 end = entry->mEnd;
266 end_set = true;
267 }
268 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700269 times++;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800270 }
271
272 if (end_always
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800273 || (end_set && (end >= (*last)->getSequence()))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800274 mLogElements.push_back(elem);
275 } else {
276 mLogElements.insert(last,elem);
277 }
278
279 LogTimeEntry::unlock();
280 }
281
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700282 stats.add(elem);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800283 maybePrune(log_id);
284 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800285
286 return len;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800287}
288
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700289// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn12bac902014-02-26 09:50:16 -0800290//
291// mLogElementsLock must be held when this function is called.
292void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800293 size_t sizes = stats.sizes(id);
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700294 unsigned long maxSize = log_buffer_size(id);
295 if (sizes > maxSize) {
Mark Salyzyn63745722015-08-19 12:20:36 -0700296 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzynd745c722015-09-30 07:40:09 -0700297 size_t elements = stats.realElements(id);
298 size_t minElements = elements / 100;
299 if (minElements < minPrune) {
300 minElements = minPrune;
301 }
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700302 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700303 if (pruneRows < minElements) {
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700304 pruneRows = minElements;
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800305 }
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700306 if (pruneRows > maxPrune) {
307 pruneRows = maxPrune;
Mark Salyzyn63745722015-08-19 12:20:36 -0700308 }
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800309 prune(id, pruneRows);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800310 }
311}
312
Mark Salyzyn57b82a62015-09-03 16:08:50 -0700313LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700314 LogBufferElementCollection::iterator it, bool coalesce) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700315 LogBufferElement *element = *it;
316 log_id_t id = element->getLogId();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700317
Mark Salyzync572fc62016-07-14 15:34:30 -0700318 { // start of scope for found iterator
319 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
320 element->getTag() : element->getUid();
321 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
322 if ((found != mLastWorst[id].end()) && (it == found->second)) {
323 mLastWorst[id].erase(found);
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700324 }
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700325 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700326
Mark Salyzync572fc62016-07-14 15:34:30 -0700327 if ((id != LOG_ID_EVENTS) && (id != LOG_ID_SECURITY) && (element->getUid() == AID_SYSTEM)) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700328 // start of scope for pid found iterator
329 LogBufferPidIteratorMap::iterator found =
330 mLastWorstPidOfSystem[id].find(element->getPid());
331 if ((found != mLastWorstPidOfSystem[id].end())
332 && (it == found->second)) {
333 mLastWorstPidOfSystem[id].erase(found);
334 }
335 }
336
Mark Salyzynd701b782016-01-19 16:04:41 -0800337 bool setLast[LOG_ID_MAX];
338 bool doSetLast = false;
339 log_id_for_each(i) {
340 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
341 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700342 it = mLogElements.erase(it);
Mark Salyzynd701b782016-01-19 16:04:41 -0800343 if (doSetLast) {
344 log_id_for_each(i) {
345 if (setLast[i]) {
346 if (it == mLogElements.end()) { // unlikely
347 mLastSet[i] = false;
348 } else {
349 mLast[i] = it;
350 }
351 }
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800352 }
353 }
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700354 if (coalesce) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700355 stats.erase(element);
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700356 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700357 stats.subtract(element);
Mark Salyzyn57b82a62015-09-03 16:08:50 -0700358 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700359 delete element;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700360
361 return it;
362}
363
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700364// Define a temporary mechanism to report the last LogBufferElement pointer
365// for the specified uid, pid and tid. Used below to help merge-sort when
366// pruning for worst UID.
367class LogBufferElementKey {
368 const union {
369 struct {
370 uint16_t uid;
371 uint16_t pid;
372 uint16_t tid;
373 uint16_t padding;
374 } __packed;
375 uint64_t value;
376 } __packed;
377
378public:
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700379 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid):
380 uid(uid),
381 pid(pid),
382 tid(tid),
383 padding(0) {
384 }
Chih-Hung Hsieh697219b2016-04-25 13:49:46 -0700385 explicit LogBufferElementKey(uint64_t key):value(key) { }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700386
387 uint64_t getKey() { return value; }
388};
389
Mark Salyzyn6a404192015-05-19 09:12:30 -0700390class LogBufferElementLast {
391
392 typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap;
393 LogBufferElementMap map;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700394
395public:
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700396
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700397 bool coalesce(LogBufferElement *element, unsigned short dropped) {
398 LogBufferElementKey key(element->getUid(),
399 element->getPid(),
400 element->getTid());
Mark Salyzyn6a404192015-05-19 09:12:30 -0700401 LogBufferElementMap::iterator it = map.find(key.getKey());
402 if (it != map.end()) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700403 LogBufferElement *found = it->second;
404 unsigned short moreDropped = found->getDropped();
405 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700406 map.erase(it);
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700407 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700408 found->setDropped(dropped + moreDropped);
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700409 return true;
410 }
411 }
412 return false;
413 }
414
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700415 void add(LogBufferElement *element) {
416 LogBufferElementKey key(element->getUid(),
417 element->getPid(),
418 element->getTid());
419 map[key.getKey()] = element;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700420 }
421
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700422 inline void clear() {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700423 map.clear();
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700424 }
425
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700426 void clear(LogBufferElement *element) {
427 uint64_t current = element->getRealTime().nsec()
Mark Salyzyn80c1fc62015-06-04 13:35:30 -0700428 - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn6a404192015-05-19 09:12:30 -0700429 for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700430 LogBufferElement *mapElement = it->second;
431 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD)
432 && (current > mapElement->getRealTime().nsec())) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700433 it = map.erase(it);
434 } else {
435 ++it;
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700436 }
437 }
438 }
439
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700440};
441
Mark Salyzyn12bac902014-02-26 09:50:16 -0800442// prune "pruneRows" of type "id" from the buffer.
443//
Mark Salyzyn50e21082015-09-08 09:12:51 -0700444// This garbage collection task is used to expire log entries. It is called to
445// remove all logs (clear), all UID logs (unprivileged clear), or every
446// 256 or 10% of the total logs (whichever is less) to prune the logs.
447//
448// First there is a prep phase where we discover the reader region lock that
449// acts as a backstop to any pruning activity to stop there and go no further.
450//
451// There are three major pruning loops that follow. All expire from the oldest
452// entries. Since there are multiple log buffers, the Android logging facility
453// will appear to drop entries 'in the middle' when looking at multiple log
454// sources and buffers. This effect is slightly more prominent when we prune
455// the worst offender by logging source. Thus the logs slowly loose content
456// and value as you move back in time. This is preferred since chatty sources
457// invariably move the logs value down faster as less chatty sources would be
458// expired in the noise.
459//
460// The first loop performs blacklisting and worst offender pruning. Falling
461// through when there are no notable worst offenders and have not hit the
462// region lock preventing further worst offender pruning. This loop also looks
463// after managing the chatty log entries and merging to help provide
464// statistical basis for blame. The chatty entries are not a notification of
465// how much logs you may have, but instead represent how much logs you would
466// have had in a virtual log buffer that is extended to cover all the in-memory
467// logs without loss. They last much longer than the represented pruned logs
468// since they get multiplied by the gains in the non-chatty log sources.
469//
470// The second loop get complicated because an algorithm of watermarks and
471// history is maintained to reduce the order and keep processing time
472// down to a minimum at scale. These algorithms can be costly in the face
473// of larger log buffers, or severly limited processing time granted to a
474// background task at lowest priority.
475//
476// This second loop does straight-up expiration from the end of the logs
477// (again, remember for the specified log buffer id) but does some whitelist
478// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
479// spam filtration all take priority. This second loop also checks if a region
480// lock is causing us to buffer too much in the logs to help the reader(s),
481// and will tell the slowest reader thread to skip log entries, and if
482// persistent and hits a further threshold, kill the reader thread.
483//
484// The third thread is optional, and only gets hit if there was a whitelist
485// and more needs to be pruned against the backstop of the region lock.
486//
Mark Salyzyn12bac902014-02-26 09:50:16 -0800487// mLogElementsLock must be held when this function is called.
Mark Salyzyn50e21082015-09-08 09:12:51 -0700488//
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700489bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800490 LogTimeEntry *oldest = NULL;
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700491 bool busy = false;
Mark Salyzynd280d812015-09-16 15:34:00 -0700492 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800493
494 LogTimeEntry::lock();
495
496 // Region locked?
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700497 LastLogTimes::iterator times = mTimes.begin();
498 while(times != mTimes.end()) {
499 LogTimeEntry *entry = (*times);
TraianX Schiau6ba427e2014-12-17 10:53:41 +0200500 if (entry->owned_Locked() && entry->isWatching(id)
Mark Salyzyn552b4752015-11-30 11:35:56 -0800501 && (!oldest ||
502 (oldest->mStart > entry->mStart) ||
503 ((oldest->mStart == entry->mStart) &&
504 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800505 oldest = entry;
506 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700507 times++;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800508 }
509
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800510 LogBufferElementCollection::iterator it;
511
Mark Salyzyn276d5352014-06-12 11:16:16 -0700512 if (caller_uid != AID_ROOT) {
Mark Salyzyn4e23c6e2016-09-01 15:48:36 -0700513 // Only here if clear all request from non system source, so chatty
514 // filter logistics is not required.
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800515 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
516 while (it != mLogElements.end()) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700517 LogBufferElement *element = *it;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700518
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700519 if ((element->getLogId() != id) || (element->getUid() != caller_uid)) {
Mark Salyzynd280d812015-09-16 15:34:00 -0700520 ++it;
521 continue;
522 }
523
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800524 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
525 mLast[id] = it;
526 mLastSet[id] = true;
527 }
528
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700529 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700530 busy = true;
Mark Salyzyn552b4752015-11-30 11:35:56 -0800531 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
532 oldest->triggerReader_Locked();
533 } else {
534 oldest->triggerSkip_Locked(id, pruneRows);
535 }
Mark Salyzyn276d5352014-06-12 11:16:16 -0700536 break;
537 }
538
Mark Salyzynd280d812015-09-16 15:34:00 -0700539 it = erase(it);
Mark Salyzyn4e23c6e2016-09-01 15:48:36 -0700540 if (--pruneRows == 0) {
541 break;
542 }
Mark Salyzyn276d5352014-06-12 11:16:16 -0700543 }
544 LogTimeEntry::unlock();
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700545 return busy;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700546 }
547
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700548 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800549 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzynd280d812015-09-16 15:34:00 -0700550 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800551 // recalculate the worst offender on every batched pass
Mark Salyzync572fc62016-07-14 15:34:30 -0700552 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800553 size_t worst_sizes = 0;
554 size_t second_worst_sizes = 0;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700555 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800556
Mark Salyzyn0e765c62015-03-17 17:17:25 -0700557 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzync572fc62016-07-14 15:34:30 -0700558 // Calculate threshold as 12.5% of available storage
559 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700560
Mark Salyzync572fc62016-07-14 15:34:30 -0700561 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
562 stats.sortTags(AID_ROOT, (pid_t)0, 2, id).findWorst(
563 worst, worst_sizes, second_worst_sizes, threshold);
564 } else {
565 stats.sort(AID_ROOT, (pid_t)0, 2, id).findWorst(
566 worst, worst_sizes, second_worst_sizes, threshold);
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700567
Mark Salyzync572fc62016-07-14 15:34:30 -0700568 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
569 stats.sortPids(worst, (pid_t)0, 2, id).findWorst(
570 worstPid, worst_sizes, second_worst_sizes);
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700571 }
572 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800573 }
574
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700575 // skip if we have neither worst nor naughty filters
Mark Salyzync572fc62016-07-14 15:34:30 -0700576 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700577 break;
578 }
579
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800580 bool kick = false;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700581 bool leading = true;
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800582 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn50e21082015-09-08 09:12:51 -0700583 // Perform at least one mandatory garbage collection cycle in following
584 // - clear leading chatty tags
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700585 // - coalesce chatty tags
Mark Salyzyn50e21082015-09-08 09:12:51 -0700586 // - check age-out of preserved logs
587 bool gc = pruneRows <= 1;
Mark Salyzync572fc62016-07-14 15:34:30 -0700588 if (!gc && (worst != -1)) {
589 { // begin scope for worst found iterator
590 LogBufferIteratorMap::iterator found = mLastWorst[id].find(worst);
591 if ((found != mLastWorst[id].end())
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700592 && (found->second != mLogElements.end())) {
593 leading = false;
594 it = found->second;
595 }
596 }
597 if (worstPid) {
598 // begin scope for pid worst found iterator
599 LogBufferPidIteratorMap::iterator found
600 = mLastWorstPidOfSystem[id].find(worstPid);
601 if ((found != mLastWorstPidOfSystem[id].end())
602 && (found->second != mLogElements.end())) {
603 leading = false;
604 it = found->second;
605 }
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700606 }
607 }
Mark Salyzyn4e29e172015-08-24 13:43:27 -0700608 static const timespec too_old = {
609 EXPIRE_HOUR_THRESHOLD * 60 * 60, 0
610 };
611 LogBufferElementCollection::iterator lastt;
612 lastt = mLogElements.end();
613 --lastt;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700614 LogBufferElementLast last;
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700615 while (it != mLogElements.end()) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700616 LogBufferElement *element = *it;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800617
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700618 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700619 busy = true;
Mark Salyzyn552b4752015-11-30 11:35:56 -0800620 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
621 oldest->triggerReader_Locked();
622 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800623 break;
624 }
625
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700626 if (element->getLogId() != id) {
Mark Salyzync89839a2014-02-11 12:29:31 -0800627 ++it;
628 continue;
629 }
630
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800631 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
632 mLast[id] = it;
633 mLastSet[id] = true;
634 }
635
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700636 unsigned short dropped = element->getDropped();
Mark Salyzync89839a2014-02-11 12:29:31 -0800637
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700638 // remove any leading drops
639 if (leading && dropped) {
640 it = erase(it);
641 continue;
642 }
643
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700644 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700645 it = erase(it, true);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700646 continue;
647 }
648
Mark Salyzync572fc62016-07-14 15:34:30 -0700649 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
650 element->getTag() :
651 element->getUid();
652
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700653 if (hasBlacklist && mPrune.naughty(element)) {
654 last.clear(element);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700655 it = erase(it);
656 if (dropped) {
657 continue;
658 }
659
660 pruneRows--;
661 if (pruneRows == 0) {
662 break;
663 }
664
Mark Salyzync572fc62016-07-14 15:34:30 -0700665 if (key == worst) {
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700666 kick = true;
667 if (worst_sizes < second_worst_sizes) {
668 break;
669 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700670 worst_sizes -= element->getMsgLen();
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700671 }
672 continue;
673 }
674
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700675 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old))
676 || (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzyn4e29e172015-08-24 13:43:27 -0700677 break;
678 }
679
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700680 if (dropped) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700681 last.add(element);
682 if (worstPid
683 && ((!gc && (element->getPid() == worstPid))
Mark Salyzync572fc62016-07-14 15:34:30 -0700684 || (mLastWorstPidOfSystem[id].find(element->getPid())
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700685 == mLastWorstPidOfSystem[id].end()))) {
Mark Salyzyn9faf9082016-09-01 07:28:44 -0700686 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700687 }
Mark Salyzync572fc62016-07-14 15:34:30 -0700688 if ((!gc && !worstPid && (key == worst))
689 || (mLastWorst[id].find(key) == mLastWorst[id].end())) {
690 mLastWorst[id][key] = it;
Mark Salyzyncb15c7c2015-08-24 13:43:27 -0700691 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800692 ++it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700693 continue;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800694 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700695
Mark Salyzync572fc62016-07-14 15:34:30 -0700696 if ((key != worst)
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700697 || (worstPid && (element->getPid() != worstPid))) {
Mark Salyzynfd318a82015-06-01 09:41:19 -0700698 leading = false;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700699 last.clear(element);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700700 ++it;
701 continue;
702 }
703
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700704 pruneRows--;
705 if (pruneRows == 0) {
706 break;
707 }
708
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700709 kick = true;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700710
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700711 unsigned short len = element->getMsgLen();
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700712
713 // do not create any leading drops
714 if (leading) {
715 it = erase(it);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700716 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700717 stats.drop(element);
718 element->setDropped(1);
719 if (last.coalesce(element, 1)) {
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700720 it = erase(it, true);
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700721 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700722 last.add(element);
723 if (worstPid && (!gc
724 || (mLastWorstPidOfSystem[id].find(worstPid)
725 == mLastWorstPidOfSystem[id].end()))) {
726 mLastWorstPidOfSystem[id][worstPid] = it;
727 }
Mark Salyzync572fc62016-07-14 15:34:30 -0700728 if ((!gc && !worstPid) ||
729 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
730 mLastWorst[id][worst] = it;
Mark Salyzyn50e21082015-09-08 09:12:51 -0700731 }
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700732 ++it;
733 }
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700734 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700735 if (worst_sizes < second_worst_sizes) {
736 break;
737 }
738 worst_sizes -= len;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800739 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700740 last.clear();
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800741
Mark Salyzync402bc72014-04-01 17:19:47 -0700742 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800743 break; // the following loop will ask bad clients to skip/drop
744 }
745 }
746
Mark Salyzync89839a2014-02-11 12:29:31 -0800747 bool whitelist = false;
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800748 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800749 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn12bac902014-02-26 09:50:16 -0800750 while((pruneRows > 0) && (it != mLogElements.end())) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700751 LogBufferElement *element = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700752
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700753 if (element->getLogId() != id) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700754 it++;
755 continue;
756 }
757
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800758 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
759 mLast[id] = it;
760 mLastSet[id] = true;
761 }
762
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700763 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700764 busy = true;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700765 if (whitelist) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800766 break;
767 }
Mark Salyzync402bc72014-04-01 17:19:47 -0700768
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700769 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
770 // kick a misbehaving log reader client off the island
771 oldest->release_Locked();
Mark Salyzyn552b4752015-11-30 11:35:56 -0800772 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
773 oldest->triggerReader_Locked();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700774 } else {
775 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzync89839a2014-02-11 12:29:31 -0800776 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700777 break;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800778 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700779
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700780 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
781 // WhiteListed
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700782 whitelist = true;
783 it++;
784 continue;
785 }
786
787 it = erase(it);
788 pruneRows--;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800789 }
790
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700791 // Do not save the whitelist if we are reader range limited
Mark Salyzync89839a2014-02-11 12:29:31 -0800792 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800793 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzync89839a2014-02-11 12:29:31 -0800794 while((it != mLogElements.end()) && (pruneRows > 0)) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700795 LogBufferElement *element = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700796
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700797 if (element->getLogId() != id) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700798 ++it;
799 continue;
Mark Salyzync89839a2014-02-11 12:29:31 -0800800 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700801
Mark Salyzyn7a3c2822016-01-11 10:58:09 -0800802 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
803 mLast[id] = it;
804 mLastSet[id] = true;
805 }
806
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700807 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700808 busy = true;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700809 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
810 // kick a misbehaving log reader client off the island
811 oldest->release_Locked();
Mark Salyzyn552b4752015-11-30 11:35:56 -0800812 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
813 oldest->triggerReader_Locked();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700814 } else {
815 oldest->triggerSkip_Locked(id, pruneRows);
816 }
817 break;
818 }
819
820 it = erase(it);
821 pruneRows--;
Mark Salyzync89839a2014-02-11 12:29:31 -0800822 }
823 }
Mark Salyzync89839a2014-02-11 12:29:31 -0800824
Mark Salyzyn12bac902014-02-26 09:50:16 -0800825 LogTimeEntry::unlock();
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700826
827 return (pruneRows > 0) && busy;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800828}
829
830// clear all rows of type "id" from the buffer.
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700831bool LogBuffer::clear(log_id_t id, uid_t uid) {
832 bool busy = true;
833 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
834 for (int retry = 4;;) {
835 if (retry == 1) { // last pass
836 // Check if it is still busy after the sleep, we say prune
837 // one entry, not another clear run, so we are looking for
838 // the quick side effect of the return value to tell us if
839 // we have a _blocked_ reader.
840 pthread_mutex_lock(&mLogElementsLock);
841 busy = prune(id, 1, uid);
842 pthread_mutex_unlock(&mLogElementsLock);
843 // It is still busy, blocked reader(s), lets kill them all!
844 // otherwise, lets be a good citizen and preserve the slow
845 // readers and let the clear run (below) deal with determining
846 // if we are still blocked and return an error code to caller.
847 if (busy) {
848 LogTimeEntry::lock();
849 LastLogTimes::iterator times = mTimes.begin();
850 while (times != mTimes.end()) {
851 LogTimeEntry *entry = (*times);
852 // Killer punch
853 if (entry->owned_Locked() && entry->isWatching(id)) {
854 entry->release_Locked();
855 }
856 times++;
857 }
858 LogTimeEntry::unlock();
859 }
860 }
861 pthread_mutex_lock(&mLogElementsLock);
862 busy = prune(id, ULONG_MAX, uid);
863 pthread_mutex_unlock(&mLogElementsLock);
864 if (!busy || !--retry) {
865 break;
866 }
867 sleep (1); // Let reader(s) catch up after notification
868 }
869 return busy;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800870}
871
872// get the used space associated with "id".
873unsigned long LogBuffer::getSizeUsed(log_id_t id) {
874 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzynd774bce2014-02-06 14:48:50 -0800875 size_t retval = stats.sizes(id);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800876 pthread_mutex_unlock(&mLogElementsLock);
877 return retval;
878}
879
Mark Salyzync89839a2014-02-11 12:29:31 -0800880// set the total space allocated to "id"
881int LogBuffer::setSize(log_id_t id, unsigned long size) {
882 // Reasonable limits ...
Mark Salyzyn7b85c592014-05-09 17:44:18 -0700883 if (!valid_size(size)) {
Mark Salyzync89839a2014-02-11 12:29:31 -0800884 return -1;
885 }
886 pthread_mutex_lock(&mLogElementsLock);
887 log_buffer_size(id) = size;
888 pthread_mutex_unlock(&mLogElementsLock);
889 return 0;
890}
891
892// get the total space allocated to "id"
893unsigned long LogBuffer::getSize(log_id_t id) {
894 pthread_mutex_lock(&mLogElementsLock);
895 size_t retval = log_buffer_size(id);
896 pthread_mutex_unlock(&mLogElementsLock);
897 return retval;
898}
899
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800900uint64_t LogBuffer::flushTo(
Mark Salyzyn924c0b32016-01-26 14:32:35 -0800901 SocketClient *reader, const uint64_t start,
902 bool privileged, bool security,
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800903 int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800904 LogBufferElementCollection::iterator it;
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800905 uint64_t max = start;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800906 uid_t uid = reader->getUid();
907
908 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -0600909
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800910 if (start <= 1) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -0600911 // client wants to start from the beginning
912 it = mLogElements.begin();
913 } else {
914 // Client wants to start from some specified time. Chances are
915 // we are better off starting from the end of the time sorted list.
916 for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
917 --it;
918 LogBufferElement *element = *it;
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800919 if (element->getSequence() <= start) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -0600920 it++;
921 break;
922 }
923 }
924 }
925
926 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800927 LogBufferElement *element = *it;
928
929 if (!privileged && (element->getUid() != uid)) {
930 continue;
931 }
932
Mark Salyzyn924c0b32016-01-26 14:32:35 -0800933 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
934 continue;
935 }
936
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800937 if (element->getSequence() <= start) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800938 continue;
939 }
940
941 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800942 if (filter) {
943 int ret = (*filter)(element, arg);
944 if (ret == false) {
945 continue;
946 }
947 if (ret != true) {
948 break;
949 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800950 }
951
952 pthread_mutex_unlock(&mLogElementsLock);
953
954 // range locking in LastLogTimes looks after us
Mark Salyzyn3443fda2015-12-03 15:38:35 -0800955 max = element->flushTo(reader, this, privileged);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800956
957 if (max == element->FLUSH_ERROR) {
958 return max;
959 }
960
961 pthread_mutex_lock(&mLogElementsLock);
962 }
963 pthread_mutex_unlock(&mLogElementsLock);
964
965 return max;
966}
Mark Salyzynd774bce2014-02-06 14:48:50 -0800967
Mark Salyzynb921ab52015-12-17 09:58:43 -0800968std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
969 unsigned int logMask) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800970 pthread_mutex_lock(&mLogElementsLock);
971
Mark Salyzynb921ab52015-12-17 09:58:43 -0800972 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzynd774bce2014-02-06 14:48:50 -0800973
974 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn1c7b6fb2015-08-20 10:01:44 -0700975
976 return ret;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800977}