blob: 1ecf99c98e3cb6d64ba580685a3201c1c6140dde [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 */
16
Mark Salyzyn671e3432014-05-06 07:34:59 -070017#include <ctype.h>
Mark Salyzyn202e1532015-02-09 08:21:05 -080018#include <errno.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080019#include <stdio.h>
20#include <string.h>
Mark Salyzyn57a0af92014-05-09 17:44:18 -070021#include <sys/user.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080022#include <time.h>
23#include <unistd.h>
24
Mark Salyzyn511338d2015-05-19 09:12:30 -070025#include <unordered_map>
26
Mark Salyzyn671e3432014-05-06 07:34:59 -070027#include <cutils/properties.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080028#include <log/logger.h>
29
30#include "LogBuffer.h"
Mark Salyzynb6bee332015-09-08 08:56:32 -070031#include "LogKlog.h"
Mark Salyzyn671e3432014-05-06 07:34:59 -070032#include "LogReader.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080033
Mark Salyzyndfa7a072014-02-11 12:29:31 -080034// Default
Mark Salyzyn66607eb2016-01-05 08:49:44 -080035#define LOG_BUFFER_SIZE (256 * 1024) // Tuned with ro.logd.size per-platform
Mark Salyzyndfa7a072014-02-11 12:29:31 -080036#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn57a0af92014-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 Salyzyn0175b072014-02-26 09:50:16 -080065
Mark Salyzyn671e3432014-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 Salyzyn57a0af92014-05-09 17:44:18 -070089 if (!valid_size(value)) {
90 value = 0;
91 }
92
Mark Salyzyn671e3432014-05-06 07:34:59 -070093 return value;
94}
95
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070096void LogBuffer::init() {
Mark Salyzyn57a0af92014-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 Salyzyncdda62b2015-12-14 13:07:12 -0800103 if (!default_size) {
Mark Salyzyn9c66a582015-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 Salyzyncdda62b2015-12-14 13:07:12 -0800108 }
Mark Salyzyn57a0af92014-05-09 17:44:18 -0700109 }
Mark Salyzyn671e3432014-05-06 07:34:59 -0700110
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800111 log_id_for_each(i) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800112 mLastSet[i] = false;
113 mLast[i] = mLogElements.begin();
114
Mark Salyzyn671e3432014-05-06 07:34:59 -0700115 char key[PROP_NAME_MAX];
Mark Salyzyn671e3432014-05-06 07:34:59 -0700116
Mark Salyzyn57a0af92014-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 Salyzyndfa7a072014-02-11 12:29:31 -0800138 }
Mark Salyzynb6bee332015-09-08 08:56:32 -0700139 bool lastMonotonic = monotonic;
Mark Salyzynba7a9a02015-12-01 15:57:25 -0800140 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzynb75cce02015-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 Salyzynb6bee332015-09-08 08:56:32 -0700173 }
174
Mark Salyzynb75cce02015-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 Salyzynb6bee332015-09-08 08:56:32 -0700177 //
Mark Salyzynb75cce02015-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 Salyzynb6bee332015-09-08 08:56:32 -0700187 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800188 times++;
Mark Salyzynb6bee332015-09-08 08:56:32 -0700189 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800190
191 LogTimeEntry::unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800192}
193
Mark Salyzynb6bee332015-09-08 08:56:32 -0700194LogBuffer::LogBuffer(LastLogTimes *times):
Mark Salyzynba7a9a02015-12-01 15:57:25 -0800195 monotonic(android_log_clockid() == CLOCK_MONOTONIC),
Mark Salyzynb6bee332015-09-08 08:56:32 -0700196 mTimes(*times) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700197 pthread_mutex_init(&mLogElementsLock, NULL);
198
199 init();
200}
201
Mark Salyzyn202e1532015-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 Salyzyn0175b072014-02-26 09:50:16 -0800205 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800206 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800207 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700208
Mark Salyzyn0175b072014-02-26 09:50:16 -0800209 LogBufferElement *elem = new LogBufferElement(log_id, realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700210 uid, pid, tid, msg, len);
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000211 if (log_id != LOG_ID_SECURITY) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800212 int prio = ANDROID_LOG_INFO;
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000213 const char *tag = NULL;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800214 if (log_id == LOG_ID_EVENTS) {
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000215 tag = android::tagToName(elem->getTag());
Mark Salyzyn083b0372015-12-04 10:59:45 -0800216 } else {
217 prio = *msg;
218 tag = msg + 1;
219 }
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000220 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800221 // Log traffic received to total
222 pthread_mutex_lock(&mLogElementsLock);
223 stats.add(elem);
224 stats.subtract(elem);
225 pthread_mutex_unlock(&mLogElementsLock);
226 delete elem;
227 return -EACCES;
228 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700229 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800230
231 pthread_mutex_lock(&mLogElementsLock);
232
233 // Insert elements in time sorted order if possible
234 // NB: if end is region locked, place element at end of list
235 LogBufferElementCollection::iterator it = mLogElements.end();
236 LogBufferElementCollection::iterator last = it;
Mark Salyzyneae155e2014-10-13 16:49:47 -0700237 while (last != mLogElements.begin()) {
238 --it;
Mark Salyzync03e72c2014-02-18 11:23:53 -0800239 if ((*it)->getRealTime() <= realtime) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800240 break;
241 }
242 last = it;
243 }
Mark Salyzync03e72c2014-02-18 11:23:53 -0800244
Mark Salyzyn0175b072014-02-26 09:50:16 -0800245 if (last == mLogElements.end()) {
246 mLogElements.push_back(elem);
247 } else {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800248 uint64_t end = 1;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800249 bool end_set = false;
250 bool end_always = false;
251
252 LogTimeEntry::lock();
253
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700254 LastLogTimes::iterator times = mTimes.begin();
255 while(times != mTimes.end()) {
256 LogTimeEntry *entry = (*times);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800257 if (entry->owned_Locked()) {
258 if (!entry->mNonBlock) {
259 end_always = true;
260 break;
261 }
262 if (!end_set || (end <= entry->mEnd)) {
263 end = entry->mEnd;
264 end_set = true;
265 }
266 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700267 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800268 }
269
270 if (end_always
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800271 || (end_set && (end >= (*last)->getSequence()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800272 mLogElements.push_back(elem);
273 } else {
274 mLogElements.insert(last,elem);
275 }
276
277 LogTimeEntry::unlock();
278 }
279
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700280 stats.add(elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800281 maybePrune(log_id);
282 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn202e1532015-02-09 08:21:05 -0800283
284 return len;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800285}
286
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700287// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800288//
289// mLogElementsLock must be held when this function is called.
290void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800291 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700292 unsigned long maxSize = log_buffer_size(id);
293 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700294 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700295 size_t elements = stats.realElements(id);
296 size_t minElements = elements / 100;
297 if (minElements < minPrune) {
298 minElements = minPrune;
299 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700300 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700301 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700302 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800303 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700304 if (pruneRows > maxPrune) {
305 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700306 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800307 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800308 }
309}
310
Mark Salyzyn831aa292015-09-03 16:08:50 -0700311LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700312 LogBufferElementCollection::iterator it, bool coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700313 LogBufferElement *element = *it;
314 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700315
Mark Salyzyn6a066942016-07-14 15:34:30 -0700316 { // start of scope for found iterator
317 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
318 element->getTag() : element->getUid();
319 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
320 if ((found != mLastWorst[id].end()) && (it == found->second)) {
321 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700322 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700323 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700324
Mark Salyzyn6a066942016-07-14 15:34:30 -0700325 if ((id != LOG_ID_EVENTS) && (id != LOG_ID_SECURITY) && (element->getUid() == AID_SYSTEM)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700326 // start of scope for pid found iterator
327 LogBufferPidIteratorMap::iterator found =
328 mLastWorstPidOfSystem[id].find(element->getPid());
329 if ((found != mLastWorstPidOfSystem[id].end())
330 && (it == found->second)) {
331 mLastWorstPidOfSystem[id].erase(found);
332 }
333 }
334
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800335 bool setLast[LOG_ID_MAX];
336 bool doSetLast = false;
337 log_id_for_each(i) {
338 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
339 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700340 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800341 if (doSetLast) {
342 log_id_for_each(i) {
343 if (setLast[i]) {
344 if (it == mLogElements.end()) { // unlikely
345 mLastSet[i] = false;
346 } else {
347 mLast[i] = it;
348 }
349 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800350 }
351 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700352 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700353 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700354 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700355 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700356 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700357 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700358
359 return it;
360}
361
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700362// Define a temporary mechanism to report the last LogBufferElement pointer
363// for the specified uid, pid and tid. Used below to help merge-sort when
364// pruning for worst UID.
365class LogBufferElementKey {
366 const union {
367 struct {
368 uint16_t uid;
369 uint16_t pid;
370 uint16_t tid;
371 uint16_t padding;
372 } __packed;
373 uint64_t value;
374 } __packed;
375
376public:
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700377 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid):
378 uid(uid),
379 pid(pid),
380 tid(tid),
381 padding(0) {
382 }
Chih-Hung Hsieh1cc82ce2016-04-25 13:49:46 -0700383 explicit LogBufferElementKey(uint64_t key):value(key) { }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700384
385 uint64_t getKey() { return value; }
386};
387
Mark Salyzyn511338d2015-05-19 09:12:30 -0700388class LogBufferElementLast {
389
390 typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap;
391 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700392
393public:
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700394
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700395 bool coalesce(LogBufferElement *element, unsigned short dropped) {
396 LogBufferElementKey key(element->getUid(),
397 element->getPid(),
398 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700399 LogBufferElementMap::iterator it = map.find(key.getKey());
400 if (it != map.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700401 LogBufferElement *found = it->second;
402 unsigned short moreDropped = found->getDropped();
403 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700404 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700405 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700406 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700407 return true;
408 }
409 }
410 return false;
411 }
412
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700413 void add(LogBufferElement *element) {
414 LogBufferElementKey key(element->getUid(),
415 element->getPid(),
416 element->getTid());
417 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700418 }
419
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700420 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700421 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700422 }
423
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700424 void clear(LogBufferElement *element) {
425 uint64_t current = element->getRealTime().nsec()
Mark Salyzyn047cc072015-06-04 13:35:30 -0700426 - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn511338d2015-05-19 09:12:30 -0700427 for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700428 LogBufferElement *mapElement = it->second;
429 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD)
430 && (current > mapElement->getRealTime().nsec())) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700431 it = map.erase(it);
432 } else {
433 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700434 }
435 }
436 }
437
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700438};
439
Mark Salyzyn0175b072014-02-26 09:50:16 -0800440// prune "pruneRows" of type "id" from the buffer.
441//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700442// This garbage collection task is used to expire log entries. It is called to
443// remove all logs (clear), all UID logs (unprivileged clear), or every
444// 256 or 10% of the total logs (whichever is less) to prune the logs.
445//
446// First there is a prep phase where we discover the reader region lock that
447// acts as a backstop to any pruning activity to stop there and go no further.
448//
449// There are three major pruning loops that follow. All expire from the oldest
450// entries. Since there are multiple log buffers, the Android logging facility
451// will appear to drop entries 'in the middle' when looking at multiple log
452// sources and buffers. This effect is slightly more prominent when we prune
453// the worst offender by logging source. Thus the logs slowly loose content
454// and value as you move back in time. This is preferred since chatty sources
455// invariably move the logs value down faster as less chatty sources would be
456// expired in the noise.
457//
458// The first loop performs blacklisting and worst offender pruning. Falling
459// through when there are no notable worst offenders and have not hit the
460// region lock preventing further worst offender pruning. This loop also looks
461// after managing the chatty log entries and merging to help provide
462// statistical basis for blame. The chatty entries are not a notification of
463// how much logs you may have, but instead represent how much logs you would
464// have had in a virtual log buffer that is extended to cover all the in-memory
465// logs without loss. They last much longer than the represented pruned logs
466// since they get multiplied by the gains in the non-chatty log sources.
467//
468// The second loop get complicated because an algorithm of watermarks and
469// history is maintained to reduce the order and keep processing time
470// down to a minimum at scale. These algorithms can be costly in the face
471// of larger log buffers, or severly limited processing time granted to a
472// background task at lowest priority.
473//
474// This second loop does straight-up expiration from the end of the logs
475// (again, remember for the specified log buffer id) but does some whitelist
476// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
477// spam filtration all take priority. This second loop also checks if a region
478// lock is causing us to buffer too much in the logs to help the reader(s),
479// and will tell the slowest reader thread to skip log entries, and if
480// persistent and hits a further threshold, kill the reader thread.
481//
482// The third thread is optional, and only gets hit if there was a whitelist
483// and more needs to be pruned against the backstop of the region lock.
484//
Mark Salyzyn0175b072014-02-26 09:50:16 -0800485// mLogElementsLock must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700486//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700487bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800488 LogTimeEntry *oldest = NULL;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700489 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700490 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800491
492 LogTimeEntry::lock();
493
494 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700495 LastLogTimes::iterator times = mTimes.begin();
496 while(times != mTimes.end()) {
497 LogTimeEntry *entry = (*times);
TraianX Schiauda6495d2014-12-17 10:53:41 +0200498 if (entry->owned_Locked() && entry->isWatching(id)
Mark Salyzynb75cce02015-11-30 11:35:56 -0800499 && (!oldest ||
500 (oldest->mStart > entry->mStart) ||
501 ((oldest->mStart == entry->mStart) &&
502 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800503 oldest = entry;
504 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700505 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800506 }
507
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800508 LogBufferElementCollection::iterator it;
509
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700510 if (caller_uid != AID_ROOT) {
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700511 // Only here if clear all request from non system source, so chatty
512 // filter logistics is not required.
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800513 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
514 while (it != mLogElements.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700515 LogBufferElement *element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700516
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700517 if ((element->getLogId() != id) || (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700518 ++it;
519 continue;
520 }
521
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800522 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
523 mLast[id] = it;
524 mLastSet[id] = true;
525 }
526
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700527 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700528 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800529 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
530 oldest->triggerReader_Locked();
531 } else {
532 oldest->triggerSkip_Locked(id, pruneRows);
533 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700534 break;
535 }
536
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700537 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700538 if (--pruneRows == 0) {
539 break;
540 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700541 }
542 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700543 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700544 }
545
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700546 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn083b0372015-12-04 10:59:45 -0800547 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700548 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800549 // recalculate the worst offender on every batched pass
Mark Salyzyn6a066942016-07-14 15:34:30 -0700550 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800551 size_t worst_sizes = 0;
552 size_t second_worst_sizes = 0;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700553 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800554
Mark Salyzynae769232015-03-17 17:17:25 -0700555 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700556 // Calculate threshold as 12.5% of available storage
557 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700558
Mark Salyzyn6a066942016-07-14 15:34:30 -0700559 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
560 stats.sortTags(AID_ROOT, (pid_t)0, 2, id).findWorst(
561 worst, worst_sizes, second_worst_sizes, threshold);
562 } else {
563 stats.sort(AID_ROOT, (pid_t)0, 2, id).findWorst(
564 worst, worst_sizes, second_worst_sizes, threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700565
Mark Salyzyn6a066942016-07-14 15:34:30 -0700566 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
567 stats.sortPids(worst, (pid_t)0, 2, id).findWorst(
568 worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700569 }
570 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800571 }
572
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700573 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700574 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700575 break;
576 }
577
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800578 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700579 bool leading = true;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800580 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700581 // Perform at least one mandatory garbage collection cycle in following
582 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700583 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700584 // - check age-out of preserved logs
585 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700586 if (!gc && (worst != -1)) {
587 { // begin scope for worst found iterator
588 LogBufferIteratorMap::iterator found = mLastWorst[id].find(worst);
589 if ((found != mLastWorst[id].end())
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700590 && (found->second != mLogElements.end())) {
591 leading = false;
592 it = found->second;
593 }
594 }
595 if (worstPid) {
596 // begin scope for pid worst found iterator
597 LogBufferPidIteratorMap::iterator found
598 = mLastWorstPidOfSystem[id].find(worstPid);
599 if ((found != mLastWorstPidOfSystem[id].end())
600 && (found->second != mLogElements.end())) {
601 leading = false;
602 it = found->second;
603 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700604 }
605 }
Mark Salyzynccfe8442015-08-24 13:43:27 -0700606 static const timespec too_old = {
607 EXPIRE_HOUR_THRESHOLD * 60 * 60, 0
608 };
609 LogBufferElementCollection::iterator lastt;
610 lastt = mLogElements.end();
611 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700612 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700613 while (it != mLogElements.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700614 LogBufferElement *element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800615
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700616 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700617 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800618 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
619 oldest->triggerReader_Locked();
620 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800621 break;
622 }
623
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700624 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800625 ++it;
626 continue;
627 }
628
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800629 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
630 mLast[id] = it;
631 mLastSet[id] = true;
632 }
633
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700634 unsigned short dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800635
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700636 // remove any leading drops
637 if (leading && dropped) {
638 it = erase(it);
639 continue;
640 }
641
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700642 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700643 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700644 continue;
645 }
646
Mark Salyzyn6a066942016-07-14 15:34:30 -0700647 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
648 element->getTag() :
649 element->getUid();
650
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700651 if (hasBlacklist && mPrune.naughty(element)) {
652 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700653 it = erase(it);
654 if (dropped) {
655 continue;
656 }
657
658 pruneRows--;
659 if (pruneRows == 0) {
660 break;
661 }
662
Mark Salyzyn6a066942016-07-14 15:34:30 -0700663 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700664 kick = true;
665 if (worst_sizes < second_worst_sizes) {
666 break;
667 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700668 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700669 }
670 continue;
671 }
672
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700673 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old))
674 || (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700675 break;
676 }
677
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700678 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700679 last.add(element);
680 if (worstPid
681 && ((!gc && (element->getPid() == worstPid))
Mark Salyzyn6a066942016-07-14 15:34:30 -0700682 || (mLastWorstPidOfSystem[id].find(element->getPid())
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700683 == mLastWorstPidOfSystem[id].end()))) {
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700684 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700685 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700686 if ((!gc && !worstPid && (key == worst))
687 || (mLastWorst[id].find(key) == mLastWorst[id].end())) {
688 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700689 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800690 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700691 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800692 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700693
Mark Salyzyn6a066942016-07-14 15:34:30 -0700694 if ((key != worst)
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700695 || (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700696 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700697 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700698 ++it;
699 continue;
700 }
701
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700702 pruneRows--;
703 if (pruneRows == 0) {
704 break;
705 }
706
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700707 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700708
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700709 unsigned short len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700710
711 // do not create any leading drops
712 if (leading) {
713 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700714 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700715 stats.drop(element);
716 element->setDropped(1);
717 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700718 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700719 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700720 last.add(element);
721 if (worstPid && (!gc
722 || (mLastWorstPidOfSystem[id].find(worstPid)
723 == mLastWorstPidOfSystem[id].end()))) {
724 mLastWorstPidOfSystem[id][worstPid] = it;
725 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700726 if ((!gc && !worstPid) ||
727 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
728 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700729 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700730 ++it;
731 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700732 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700733 if (worst_sizes < second_worst_sizes) {
734 break;
735 }
736 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800737 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700738 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800739
Mark Salyzyn1c950472014-04-01 17:19:47 -0700740 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800741 break; // the following loop will ask bad clients to skip/drop
742 }
743 }
744
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800745 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800746 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800747 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800748 while((pruneRows > 0) && (it != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700749 LogBufferElement *element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700750
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700751 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700752 it++;
753 continue;
754 }
755
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800756 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
757 mLast[id] = it;
758 mLastSet[id] = true;
759 }
760
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700761 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700762 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700763 if (whitelist) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800764 break;
765 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700766
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700767 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
768 // kick a misbehaving log reader client off the island
769 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800770 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
771 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700772 } else {
773 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800774 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700775 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800776 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700777
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700778 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
779 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700780 whitelist = true;
781 it++;
782 continue;
783 }
784
785 it = erase(it);
786 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800787 }
788
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700789 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800790 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800791 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800792 while((it != mLogElements.end()) && (pruneRows > 0)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700793 LogBufferElement *element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700794
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700795 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700796 ++it;
797 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800798 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700799
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800800 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
801 mLast[id] = it;
802 mLastSet[id] = true;
803 }
804
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700805 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700806 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700807 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
808 // kick a misbehaving log reader client off the island
809 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800810 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
811 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700812 } else {
813 oldest->triggerSkip_Locked(id, pruneRows);
814 }
815 break;
816 }
817
818 it = erase(it);
819 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800820 }
821 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800822
Mark Salyzyn0175b072014-02-26 09:50:16 -0800823 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700824
825 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800826}
827
828// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -0700829bool LogBuffer::clear(log_id_t id, uid_t uid) {
830 bool busy = true;
831 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
832 for (int retry = 4;;) {
833 if (retry == 1) { // last pass
834 // Check if it is still busy after the sleep, we say prune
835 // one entry, not another clear run, so we are looking for
836 // the quick side effect of the return value to tell us if
837 // we have a _blocked_ reader.
838 pthread_mutex_lock(&mLogElementsLock);
839 busy = prune(id, 1, uid);
840 pthread_mutex_unlock(&mLogElementsLock);
841 // It is still busy, blocked reader(s), lets kill them all!
842 // otherwise, lets be a good citizen and preserve the slow
843 // readers and let the clear run (below) deal with determining
844 // if we are still blocked and return an error code to caller.
845 if (busy) {
846 LogTimeEntry::lock();
847 LastLogTimes::iterator times = mTimes.begin();
848 while (times != mTimes.end()) {
849 LogTimeEntry *entry = (*times);
850 // Killer punch
851 if (entry->owned_Locked() && entry->isWatching(id)) {
852 entry->release_Locked();
853 }
854 times++;
855 }
856 LogTimeEntry::unlock();
857 }
858 }
859 pthread_mutex_lock(&mLogElementsLock);
860 busy = prune(id, ULONG_MAX, uid);
861 pthread_mutex_unlock(&mLogElementsLock);
862 if (!busy || !--retry) {
863 break;
864 }
865 sleep (1); // Let reader(s) catch up after notification
866 }
867 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800868}
869
870// get the used space associated with "id".
871unsigned long LogBuffer::getSizeUsed(log_id_t id) {
872 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800873 size_t retval = stats.sizes(id);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800874 pthread_mutex_unlock(&mLogElementsLock);
875 return retval;
876}
877
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800878// set the total space allocated to "id"
879int LogBuffer::setSize(log_id_t id, unsigned long size) {
880 // Reasonable limits ...
Mark Salyzyn57a0af92014-05-09 17:44:18 -0700881 if (!valid_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800882 return -1;
883 }
884 pthread_mutex_lock(&mLogElementsLock);
885 log_buffer_size(id) = size;
886 pthread_mutex_unlock(&mLogElementsLock);
887 return 0;
888}
889
890// get the total space allocated to "id"
891unsigned long LogBuffer::getSize(log_id_t id) {
892 pthread_mutex_lock(&mLogElementsLock);
893 size_t retval = log_buffer_size(id);
894 pthread_mutex_unlock(&mLogElementsLock);
895 return retval;
896}
897
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800898uint64_t LogBuffer::flushTo(
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800899 SocketClient *reader, const uint64_t start,
900 bool privileged, bool security,
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800901 int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800902 LogBufferElementCollection::iterator it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800903 uint64_t max = start;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800904 uid_t uid = reader->getUid();
905
906 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600907
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800908 if (start <= 1) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600909 // client wants to start from the beginning
910 it = mLogElements.begin();
911 } else {
912 // Client wants to start from some specified time. Chances are
913 // we are better off starting from the end of the time sorted list.
914 for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
915 --it;
916 LogBufferElement *element = *it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800917 if (element->getSequence() <= start) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600918 it++;
919 break;
920 }
921 }
922 }
923
924 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800925 LogBufferElement *element = *it;
926
927 if (!privileged && (element->getUid() != uid)) {
928 continue;
929 }
930
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800931 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
932 continue;
933 }
934
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800935 if (element->getSequence() <= start) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800936 continue;
937 }
938
939 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800940 if (filter) {
941 int ret = (*filter)(element, arg);
942 if (ret == false) {
943 continue;
944 }
945 if (ret != true) {
946 break;
947 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800948 }
949
950 pthread_mutex_unlock(&mLogElementsLock);
951
952 // range locking in LastLogTimes looks after us
Mark Salyzyn7b873652015-12-03 15:38:35 -0800953 max = element->flushTo(reader, this, privileged);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800954
955 if (max == element->FLUSH_ERROR) {
956 return max;
957 }
958
959 pthread_mutex_lock(&mLogElementsLock);
960 }
961 pthread_mutex_unlock(&mLogElementsLock);
962
963 return max;
964}
Mark Salyzyn34facab2014-02-06 14:48:50 -0800965
Mark Salyzynee3b8382015-12-17 09:58:43 -0800966std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
967 unsigned int logMask) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800968 pthread_mutex_lock(&mLogElementsLock);
969
Mark Salyzynee3b8382015-12-17 09:58:43 -0800970 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800971
972 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn73160ac2015-08-20 10:01:44 -0700973
974 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800975}