blob: 110395c9b748b33c5f4ff22a92a8c37669dda1cb [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 Salyzyn0175b072014-02-26 09:50:16 -080035#define LOG_BUFFER_SIZE (256 * 1024) // Tuned on a per-platform basis here?
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 Salyzyn671e3432014-05-06 07:34:59 -0700112 char key[PROP_NAME_MAX];
Mark Salyzyn671e3432014-05-06 07:34:59 -0700113
Mark Salyzyn57a0af92014-05-09 17:44:18 -0700114 snprintf(key, sizeof(key), "%s.%s",
115 global_tuneable, android_log_id_to_name(i));
116 unsigned long property_size = property_get_size(key);
117
118 if (!property_size) {
119 snprintf(key, sizeof(key), "%s.%s",
120 global_default, android_log_id_to_name(i));
121 property_size = property_get_size(key);
122 }
123
124 if (!property_size) {
125 property_size = default_size;
126 }
127
128 if (!property_size) {
129 property_size = LOG_BUFFER_SIZE;
130 }
131
132 if (setSize(i, property_size)) {
133 setSize(i, LOG_BUFFER_MIN_SIZE);
134 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800135 }
Mark Salyzynb6bee332015-09-08 08:56:32 -0700136 bool lastMonotonic = monotonic;
Mark Salyzynba7a9a02015-12-01 15:57:25 -0800137 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800138 if (lastMonotonic != monotonic) {
139 //
140 // Fixup all timestamps, may not be 100% accurate, but better than
141 // throwing what we have away when we get 'surprised' by a change.
142 // In-place element fixup so no need to check reader-lock. Entries
143 // should already be in timestamp order, but we could end up with a
144 // few out-of-order entries if new monotonics come in before we
145 // are notified of the reinit change in status. A Typical example would
146 // be:
147 // --------- beginning of system
148 // 10.494082 184 201 D Cryptfs : Just triggered post_fs_data
149 // --------- beginning of kernel
150 // 0.000000 0 0 I : Initializing cgroup subsys
151 // as the act of mounting /data would trigger persist.logd.timestamp to
152 // be corrected. 1/30 corner case YMMV.
153 //
154 pthread_mutex_lock(&mLogElementsLock);
155 LogBufferElementCollection::iterator it = mLogElements.begin();
156 while((it != mLogElements.end())) {
157 LogBufferElement *e = *it;
158 if (monotonic) {
159 if (!android::isMonotonic(e->mRealTime)) {
160 LogKlog::convertRealToMonotonic(e->mRealTime);
161 }
162 } else {
163 if (android::isMonotonic(e->mRealTime)) {
164 LogKlog::convertMonotonicToReal(e->mRealTime);
165 }
166 }
167 ++it;
168 }
169 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzynb6bee332015-09-08 08:56:32 -0700170 }
171
Mark Salyzynb75cce02015-11-30 11:35:56 -0800172 // We may have been triggered by a SIGHUP. Release any sleeping reader
173 // threads to dump their current content.
Mark Salyzynb6bee332015-09-08 08:56:32 -0700174 //
Mark Salyzynb75cce02015-11-30 11:35:56 -0800175 // NB: this is _not_ performed in the context of a SIGHUP, it is
176 // performed during startup, and in context of reinit administrative thread
177 LogTimeEntry::lock();
178
179 LastLogTimes::iterator times = mTimes.begin();
180 while(times != mTimes.end()) {
181 LogTimeEntry *entry = (*times);
182 if (entry->owned_Locked()) {
183 entry->triggerReader_Locked();
Mark Salyzynb6bee332015-09-08 08:56:32 -0700184 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800185 times++;
Mark Salyzynb6bee332015-09-08 08:56:32 -0700186 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800187
188 LogTimeEntry::unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800189}
190
Mark Salyzynb6bee332015-09-08 08:56:32 -0700191LogBuffer::LogBuffer(LastLogTimes *times):
Mark Salyzynba7a9a02015-12-01 15:57:25 -0800192 monotonic(android_log_clockid() == CLOCK_MONOTONIC),
Mark Salyzynb6bee332015-09-08 08:56:32 -0700193 mTimes(*times) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700194 pthread_mutex_init(&mLogElementsLock, NULL);
195
196 init();
197}
198
Mark Salyzyn202e1532015-02-09 08:21:05 -0800199int LogBuffer::log(log_id_t log_id, log_time realtime,
200 uid_t uid, pid_t pid, pid_t tid,
201 const char *msg, unsigned short len) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800202 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800203 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800204 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700205
Mark Salyzyn0175b072014-02-26 09:50:16 -0800206 LogBufferElement *elem = new LogBufferElement(log_id, realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700207 uid, pid, tid, msg, len);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800208 if (log_id != LOG_ID_SECURITY) {
209 int prio = ANDROID_LOG_INFO;
210 const char *tag = NULL;
211 if (log_id == LOG_ID_EVENTS) {
212 tag = android::tagToName(elem->getTag());
213 } else {
214 prio = *msg;
215 tag = msg + 1;
216 }
217 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
218 // Log traffic received to total
219 pthread_mutex_lock(&mLogElementsLock);
220 stats.add(elem);
221 stats.subtract(elem);
222 pthread_mutex_unlock(&mLogElementsLock);
223 delete elem;
224 return -EACCES;
225 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700226 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800227
228 pthread_mutex_lock(&mLogElementsLock);
229
230 // Insert elements in time sorted order if possible
231 // NB: if end is region locked, place element at end of list
232 LogBufferElementCollection::iterator it = mLogElements.end();
233 LogBufferElementCollection::iterator last = it;
Mark Salyzyneae155e2014-10-13 16:49:47 -0700234 while (last != mLogElements.begin()) {
235 --it;
Mark Salyzync03e72c2014-02-18 11:23:53 -0800236 if ((*it)->getRealTime() <= realtime) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800237 break;
238 }
239 last = it;
240 }
Mark Salyzync03e72c2014-02-18 11:23:53 -0800241
Mark Salyzyn0175b072014-02-26 09:50:16 -0800242 if (last == mLogElements.end()) {
243 mLogElements.push_back(elem);
244 } else {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800245 uint64_t end = 1;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800246 bool end_set = false;
247 bool end_always = false;
248
249 LogTimeEntry::lock();
250
251 LastLogTimes::iterator t = mTimes.begin();
252 while(t != mTimes.end()) {
253 LogTimeEntry *entry = (*t);
254 if (entry->owned_Locked()) {
255 if (!entry->mNonBlock) {
256 end_always = true;
257 break;
258 }
259 if (!end_set || (end <= entry->mEnd)) {
260 end = entry->mEnd;
261 end_set = true;
262 }
263 }
264 t++;
265 }
266
267 if (end_always
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800268 || (end_set && (end >= (*last)->getSequence()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800269 mLogElements.push_back(elem);
270 } else {
271 mLogElements.insert(last,elem);
272 }
273
274 LogTimeEntry::unlock();
275 }
276
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700277 stats.add(elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800278 maybePrune(log_id);
279 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn202e1532015-02-09 08:21:05 -0800280
281 return len;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800282}
283
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700284// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800285//
286// mLogElementsLock must be held when this function is called.
287void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800288 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700289 unsigned long maxSize = log_buffer_size(id);
290 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700291 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700292 size_t elements = stats.realElements(id);
293 size_t minElements = elements / 100;
294 if (minElements < minPrune) {
295 minElements = minPrune;
296 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700297 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700298 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700299 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800300 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700301 if (pruneRows > maxPrune) {
302 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700303 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800304 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800305 }
306}
307
Mark Salyzyn831aa292015-09-03 16:08:50 -0700308LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700309 LogBufferElementCollection::iterator it, bool coalesce) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700310 LogBufferElement *e = *it;
Mark Salyzync892ea32015-08-19 17:06:11 -0700311 log_id_t id = e->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700312
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700313 LogBufferIteratorMap::iterator f = mLastWorstUid[id].find(e->getUid());
Mark Salyzync892ea32015-08-19 17:06:11 -0700314 if ((f != mLastWorstUid[id].end()) && (it == f->second)) {
315 mLastWorstUid[id].erase(f);
316 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700317 it = mLogElements.erase(it);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700318 if (coalesce) {
Mark Salyzyn831aa292015-09-03 16:08:50 -0700319 stats.erase(e);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700320 } else {
321 stats.subtract(e);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700322 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700323 delete e;
324
325 return it;
326}
327
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700328// Define a temporary mechanism to report the last LogBufferElement pointer
329// for the specified uid, pid and tid. Used below to help merge-sort when
330// pruning for worst UID.
331class LogBufferElementKey {
332 const union {
333 struct {
334 uint16_t uid;
335 uint16_t pid;
336 uint16_t tid;
337 uint16_t padding;
338 } __packed;
339 uint64_t value;
340 } __packed;
341
342public:
343 LogBufferElementKey(uid_t u, pid_t p, pid_t t):uid(u),pid(p),tid(t),padding(0) { }
344 LogBufferElementKey(uint64_t k):value(k) { }
345
346 uint64_t getKey() { return value; }
347};
348
Mark Salyzyn511338d2015-05-19 09:12:30 -0700349class LogBufferElementLast {
350
351 typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap;
352 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700353
354public:
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700355
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700356 bool coalesce(LogBufferElement *e, unsigned short dropped) {
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700357 LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700358 LogBufferElementMap::iterator it = map.find(key.getKey());
359 if (it != map.end()) {
360 LogBufferElement *l = it->second;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700361 unsigned short d = l->getDropped();
362 if ((dropped + d) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700363 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700364 } else {
365 l->setDropped(dropped + d);
366 return true;
367 }
368 }
369 return false;
370 }
371
Mark Salyzyn511338d2015-05-19 09:12:30 -0700372 void add(LogBufferElement *e) {
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700373 LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700374 map[key.getKey()] = e;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700375 }
376
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700377 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700378 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700379 }
380
381 void clear(LogBufferElement *e) {
Mark Salyzyn047cc072015-06-04 13:35:30 -0700382 uint64_t current = e->getRealTime().nsec()
383 - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn511338d2015-05-19 09:12:30 -0700384 for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
385 LogBufferElement *l = it->second;
Mark Salyzyn833a9b12015-05-15 15:58:17 -0700386 if ((l->getDropped() >= EXPIRE_THRESHOLD)
387 && (current > l->getRealTime().nsec())) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700388 it = map.erase(it);
389 } else {
390 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700391 }
392 }
393 }
394
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700395};
396
Mark Salyzyn0175b072014-02-26 09:50:16 -0800397// prune "pruneRows" of type "id" from the buffer.
398//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700399// This garbage collection task is used to expire log entries. It is called to
400// remove all logs (clear), all UID logs (unprivileged clear), or every
401// 256 or 10% of the total logs (whichever is less) to prune the logs.
402//
403// First there is a prep phase where we discover the reader region lock that
404// acts as a backstop to any pruning activity to stop there and go no further.
405//
406// There are three major pruning loops that follow. All expire from the oldest
407// entries. Since there are multiple log buffers, the Android logging facility
408// will appear to drop entries 'in the middle' when looking at multiple log
409// sources and buffers. This effect is slightly more prominent when we prune
410// the worst offender by logging source. Thus the logs slowly loose content
411// and value as you move back in time. This is preferred since chatty sources
412// invariably move the logs value down faster as less chatty sources would be
413// expired in the noise.
414//
415// The first loop performs blacklisting and worst offender pruning. Falling
416// through when there are no notable worst offenders and have not hit the
417// region lock preventing further worst offender pruning. This loop also looks
418// after managing the chatty log entries and merging to help provide
419// statistical basis for blame. The chatty entries are not a notification of
420// how much logs you may have, but instead represent how much logs you would
421// have had in a virtual log buffer that is extended to cover all the in-memory
422// logs without loss. They last much longer than the represented pruned logs
423// since they get multiplied by the gains in the non-chatty log sources.
424//
425// The second loop get complicated because an algorithm of watermarks and
426// history is maintained to reduce the order and keep processing time
427// down to a minimum at scale. These algorithms can be costly in the face
428// of larger log buffers, or severly limited processing time granted to a
429// background task at lowest priority.
430//
431// This second loop does straight-up expiration from the end of the logs
432// (again, remember for the specified log buffer id) but does some whitelist
433// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
434// spam filtration all take priority. This second loop also checks if a region
435// lock is causing us to buffer too much in the logs to help the reader(s),
436// and will tell the slowest reader thread to skip log entries, and if
437// persistent and hits a further threshold, kill the reader thread.
438//
439// The third thread is optional, and only gets hit if there was a whitelist
440// and more needs to be pruned against the backstop of the region lock.
441//
Mark Salyzyn0175b072014-02-26 09:50:16 -0800442// mLogElementsLock must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700443//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700444bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800445 LogTimeEntry *oldest = NULL;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700446 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700447 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800448
449 LogTimeEntry::lock();
450
451 // Region locked?
452 LastLogTimes::iterator t = mTimes.begin();
453 while(t != mTimes.end()) {
454 LogTimeEntry *entry = (*t);
TraianX Schiauda6495d2014-12-17 10:53:41 +0200455 if (entry->owned_Locked() && entry->isWatching(id)
Mark Salyzynb75cce02015-11-30 11:35:56 -0800456 && (!oldest ||
457 (oldest->mStart > entry->mStart) ||
458 ((oldest->mStart == entry->mStart) &&
459 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800460 oldest = entry;
461 }
462 t++;
463 }
464
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800465 LogBufferElementCollection::iterator it;
466
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700467 if (caller_uid != AID_ROOT) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700468 // Only here if clearAll condition (pruneRows == ULONG_MAX)
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700469 for(it = mLogElements.begin(); it != mLogElements.end();) {
470 LogBufferElement *e = *it;
471
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700472 if ((e->getLogId() != id) || (e->getUid() != caller_uid)) {
473 ++it;
474 continue;
475 }
476
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800477 if (oldest && (oldest->mStart <= e->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700478 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800479 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
480 oldest->triggerReader_Locked();
481 } else {
482 oldest->triggerSkip_Locked(id, pruneRows);
483 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700484 break;
485 }
486
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700487 it = erase(it);
488 pruneRows--;
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 Salyzyn64d6fe92014-02-06 18:11:13 -0800494 // prune by worst offender by 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
498 uid_t worst = (uid_t) -1;
499 size_t worst_sizes = 0;
500 size_t second_worst_sizes = 0;
501
Mark Salyzynae769232015-03-17 17:17:25 -0700502 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzynee3b8382015-12-17 09:58:43 -0800503 std::unique_ptr<const UidEntry *[]> sorted = stats.sort(
504 AID_ROOT, (pid_t)0, 2, id);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700505
Mark Salyzyn720f6d12015-03-16 08:26:05 -0700506 if (sorted.get()) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700507 if (sorted[0] && sorted[1]) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700508 worst_sizes = sorted[0]->getSizes();
Mark Salyzynd717d802015-04-20 15:36:12 -0700509 // Calculate threshold as 12.5% of available storage
510 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn50122692015-10-12 13:45:51 -0700511 if ((worst_sizes > threshold)
512 // Allow time horizon to extend roughly tenfold, assume
513 // average entry length is 100 characters.
514 && (worst_sizes > (10 * sorted[0]->getDropped()))) {
Mark Salyzynd717d802015-04-20 15:36:12 -0700515 worst = sorted[0]->getKey();
516 second_worst_sizes = sorted[1]->getSizes();
517 if (second_worst_sizes < threshold) {
518 second_worst_sizes = threshold;
519 }
520 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800521 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800522 }
523 }
524
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700525 // skip if we have neither worst nor naughty filters
526 if ((worst == (uid_t) -1) && !hasBlacklist) {
527 break;
528 }
529
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800530 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700531 bool leading = true;
Mark Salyzync892ea32015-08-19 17:06:11 -0700532 it = mLogElements.begin();
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700533 // Perform at least one mandatory garbage collection cycle in following
534 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700535 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700536 // - check age-out of preserved logs
537 bool gc = pruneRows <= 1;
538 if (!gc && (worst != (uid_t) -1)) {
Mark Salyzync892ea32015-08-19 17:06:11 -0700539 LogBufferIteratorMap::iterator f = mLastWorstUid[id].find(worst);
540 if ((f != mLastWorstUid[id].end())
541 && (f->second != mLogElements.end())) {
542 leading = false;
543 it = f->second;
544 }
545 }
Mark Salyzynccfe8442015-08-24 13:43:27 -0700546 static const timespec too_old = {
547 EXPIRE_HOUR_THRESHOLD * 60 * 60, 0
548 };
549 LogBufferElementCollection::iterator lastt;
550 lastt = mLogElements.end();
551 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700552 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700553 while (it != mLogElements.end()) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800554 LogBufferElement *e = *it;
555
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800556 if (oldest && (oldest->mStart <= e->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700557 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800558 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
559 oldest->triggerReader_Locked();
560 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800561 break;
562 }
563
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800564 if (e->getLogId() != id) {
565 ++it;
566 continue;
567 }
568
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700569 unsigned short dropped = e->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800570
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700571 // remove any leading drops
572 if (leading && dropped) {
573 it = erase(it);
574 continue;
575 }
576
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700577 if (dropped && last.coalesce(e, dropped)) {
578 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700579 continue;
580 }
581
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700582 if (hasBlacklist && mPrune.naughty(e)) {
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700583 last.clear(e);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700584 it = erase(it);
585 if (dropped) {
586 continue;
587 }
588
589 pruneRows--;
590 if (pruneRows == 0) {
591 break;
592 }
593
594 if (e->getUid() == worst) {
595 kick = true;
596 if (worst_sizes < second_worst_sizes) {
597 break;
598 }
599 worst_sizes -= e->getMsgLen();
600 }
601 continue;
602 }
603
Mark Salyzynccfe8442015-08-24 13:43:27 -0700604 if ((e->getRealTime() < ((*lastt)->getRealTime() - too_old))
605 || (e->getRealTime() > (*lastt)->getRealTime())) {
606 break;
607 }
608
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700609 if (dropped) {
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700610 last.add(e);
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700611 if ((!gc && (e->getUid() == worst))
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700612 || (mLastWorstUid[id].find(e->getUid())
613 == mLastWorstUid[id].end())) {
614 mLastWorstUid[id][e->getUid()] = it;
615 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800616 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700617 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800618 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700619
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700620 if (e->getUid() != worst) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700621 leading = false;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700622 last.clear(e);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700623 ++it;
624 continue;
625 }
626
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700627 pruneRows--;
628 if (pruneRows == 0) {
629 break;
630 }
631
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700632 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700633
634 unsigned short len = e->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700635
636 // do not create any leading drops
637 if (leading) {
638 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700639 } else {
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700640 stats.drop(e);
641 e->setDropped(1);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700642 if (last.coalesce(e, 1)) {
643 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700644 } else {
645 last.add(e);
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700646 if (!gc || (mLastWorstUid[id].find(worst)
647 == mLastWorstUid[id].end())) {
648 mLastWorstUid[id][worst] = it;
649 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700650 ++it;
651 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700652 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700653 if (worst_sizes < second_worst_sizes) {
654 break;
655 }
656 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800657 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700658 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800659
Mark Salyzyn1c950472014-04-01 17:19:47 -0700660 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800661 break; // the following loop will ask bad clients to skip/drop
662 }
663 }
664
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800665 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800666 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800667 it = mLogElements.begin();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800668 while((pruneRows > 0) && (it != mLogElements.end())) {
669 LogBufferElement *e = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700670
671 if (e->getLogId() != id) {
672 it++;
673 continue;
674 }
675
676 if (oldest && (oldest->mStart <= e->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700677 busy = true;
678
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700679 if (whitelist) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800680 break;
681 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700682
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700683 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
684 // kick a misbehaving log reader client off the island
685 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800686 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
687 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700688 } else {
689 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800690 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700691 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800692 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700693
Mark Salyzync5bf3b82015-05-21 12:50:31 -0700694 if (hasWhitelist && !e->getDropped() && mPrune.nice(e)) { // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700695 whitelist = true;
696 it++;
697 continue;
698 }
699
700 it = erase(it);
701 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800702 }
703
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700704 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800705 if (whitelist && (pruneRows > 0)) {
706 it = mLogElements.begin();
707 while((it != mLogElements.end()) && (pruneRows > 0)) {
708 LogBufferElement *e = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700709
710 if (e->getLogId() != id) {
711 ++it;
712 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800713 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700714
715 if (oldest && (oldest->mStart <= e->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700716 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700717 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
718 // kick a misbehaving log reader client off the island
719 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800720 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
721 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700722 } else {
723 oldest->triggerSkip_Locked(id, pruneRows);
724 }
725 break;
726 }
727
728 it = erase(it);
729 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800730 }
731 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800732
Mark Salyzyn0175b072014-02-26 09:50:16 -0800733 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700734
735 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800736}
737
738// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -0700739bool LogBuffer::clear(log_id_t id, uid_t uid) {
740 bool busy = true;
741 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
742 for (int retry = 4;;) {
743 if (retry == 1) { // last pass
744 // Check if it is still busy after the sleep, we say prune
745 // one entry, not another clear run, so we are looking for
746 // the quick side effect of the return value to tell us if
747 // we have a _blocked_ reader.
748 pthread_mutex_lock(&mLogElementsLock);
749 busy = prune(id, 1, uid);
750 pthread_mutex_unlock(&mLogElementsLock);
751 // It is still busy, blocked reader(s), lets kill them all!
752 // otherwise, lets be a good citizen and preserve the slow
753 // readers and let the clear run (below) deal with determining
754 // if we are still blocked and return an error code to caller.
755 if (busy) {
756 LogTimeEntry::lock();
757 LastLogTimes::iterator times = mTimes.begin();
758 while (times != mTimes.end()) {
759 LogTimeEntry *entry = (*times);
760 // Killer punch
761 if (entry->owned_Locked() && entry->isWatching(id)) {
762 entry->release_Locked();
763 }
764 times++;
765 }
766 LogTimeEntry::unlock();
767 }
768 }
769 pthread_mutex_lock(&mLogElementsLock);
770 busy = prune(id, ULONG_MAX, uid);
771 pthread_mutex_unlock(&mLogElementsLock);
772 if (!busy || !--retry) {
773 break;
774 }
775 sleep (1); // Let reader(s) catch up after notification
776 }
777 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800778}
779
780// get the used space associated with "id".
781unsigned long LogBuffer::getSizeUsed(log_id_t id) {
782 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800783 size_t retval = stats.sizes(id);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800784 pthread_mutex_unlock(&mLogElementsLock);
785 return retval;
786}
787
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800788// set the total space allocated to "id"
789int LogBuffer::setSize(log_id_t id, unsigned long size) {
790 // Reasonable limits ...
Mark Salyzyn57a0af92014-05-09 17:44:18 -0700791 if (!valid_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800792 return -1;
793 }
794 pthread_mutex_lock(&mLogElementsLock);
795 log_buffer_size(id) = size;
796 pthread_mutex_unlock(&mLogElementsLock);
797 return 0;
798}
799
800// get the total space allocated to "id"
801unsigned long LogBuffer::getSize(log_id_t id) {
802 pthread_mutex_lock(&mLogElementsLock);
803 size_t retval = log_buffer_size(id);
804 pthread_mutex_unlock(&mLogElementsLock);
805 return retval;
806}
807
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800808uint64_t LogBuffer::flushTo(
809 SocketClient *reader, const uint64_t start, bool privileged,
810 int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800811 LogBufferElementCollection::iterator it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800812 uint64_t max = start;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800813 uid_t uid = reader->getUid();
814
815 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600816
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800817 if (start <= 1) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600818 // client wants to start from the beginning
819 it = mLogElements.begin();
820 } else {
821 // Client wants to start from some specified time. Chances are
822 // we are better off starting from the end of the time sorted list.
823 for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
824 --it;
825 LogBufferElement *element = *it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800826 if (element->getSequence() <= start) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600827 it++;
828 break;
829 }
830 }
831 }
832
833 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800834 LogBufferElement *element = *it;
835
836 if (!privileged && (element->getUid() != uid)) {
837 continue;
838 }
839
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800840 if (element->getSequence() <= start) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800841 continue;
842 }
843
844 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800845 if (filter) {
846 int ret = (*filter)(element, arg);
847 if (ret == false) {
848 continue;
849 }
850 if (ret != true) {
851 break;
852 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800853 }
854
855 pthread_mutex_unlock(&mLogElementsLock);
856
857 // range locking in LastLogTimes looks after us
Mark Salyzyn7b873652015-12-03 15:38:35 -0800858 max = element->flushTo(reader, this, privileged);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800859
860 if (max == element->FLUSH_ERROR) {
861 return max;
862 }
863
864 pthread_mutex_lock(&mLogElementsLock);
865 }
866 pthread_mutex_unlock(&mLogElementsLock);
867
868 return max;
869}
Mark Salyzyn34facab2014-02-06 14:48:50 -0800870
Mark Salyzynee3b8382015-12-17 09:58:43 -0800871std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
872 unsigned int logMask) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800873 pthread_mutex_lock(&mLogElementsLock);
874
Mark Salyzynee3b8382015-12-17 09:58:43 -0800875 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800876
877 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn73160ac2015-08-20 10:01:44 -0700878
879 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800880}