blob: 45b9861bff1711c44dc9c90677278a668553be44 [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 Salyzyn12bac902014-02-26 09:50:16 -080035#define LOG_BUFFER_SIZE (256 * 1024) // Tuned on a per-platform basis here?
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);
103 }
Mark Salyzyn4e756fb2014-05-06 07:34:59 -0700104
Mark Salyzync89839a2014-02-11 12:29:31 -0800105 log_id_for_each(i) {
Mark Salyzyn4e756fb2014-05-06 07:34:59 -0700106 char key[PROP_NAME_MAX];
Mark Salyzyn4e756fb2014-05-06 07:34:59 -0700107
Mark Salyzyn7b85c592014-05-09 17:44:18 -0700108 snprintf(key, sizeof(key), "%s.%s",
109 global_tuneable, android_log_id_to_name(i));
110 unsigned long property_size = property_get_size(key);
111
112 if (!property_size) {
113 snprintf(key, sizeof(key), "%s.%s",
114 global_default, android_log_id_to_name(i));
115 property_size = property_get_size(key);
116 }
117
118 if (!property_size) {
119 property_size = default_size;
120 }
121
122 if (!property_size) {
123 property_size = LOG_BUFFER_SIZE;
124 }
125
126 if (setSize(i, property_size)) {
127 setSize(i, LOG_BUFFER_MIN_SIZE);
128 }
Mark Salyzync89839a2014-02-11 12:29:31 -0800129 }
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700130 bool lastMonotonic = monotonic;
Mark Salyzyn2d4f9bc2015-12-01 15:57:25 -0800131 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzyn552b4752015-11-30 11:35:56 -0800132 if (lastMonotonic != monotonic) {
133 //
134 // Fixup all timestamps, may not be 100% accurate, but better than
135 // throwing what we have away when we get 'surprised' by a change.
136 // In-place element fixup so no need to check reader-lock. Entries
137 // should already be in timestamp order, but we could end up with a
138 // few out-of-order entries if new monotonics come in before we
139 // are notified of the reinit change in status. A Typical example would
140 // be:
141 // --------- beginning of system
142 // 10.494082 184 201 D Cryptfs : Just triggered post_fs_data
143 // --------- beginning of kernel
144 // 0.000000 0 0 I : Initializing cgroup subsys
145 // as the act of mounting /data would trigger persist.logd.timestamp to
146 // be corrected. 1/30 corner case YMMV.
147 //
148 pthread_mutex_lock(&mLogElementsLock);
149 LogBufferElementCollection::iterator it = mLogElements.begin();
150 while((it != mLogElements.end())) {
151 LogBufferElement *e = *it;
152 if (monotonic) {
153 if (!android::isMonotonic(e->mRealTime)) {
154 LogKlog::convertRealToMonotonic(e->mRealTime);
155 }
156 } else {
157 if (android::isMonotonic(e->mRealTime)) {
158 LogKlog::convertMonotonicToReal(e->mRealTime);
159 }
160 }
161 ++it;
162 }
163 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700164 }
165
Mark Salyzyn552b4752015-11-30 11:35:56 -0800166 // We may have been triggered by a SIGHUP. Release any sleeping reader
167 // threads to dump their current content.
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700168 //
Mark Salyzyn552b4752015-11-30 11:35:56 -0800169 // NB: this is _not_ performed in the context of a SIGHUP, it is
170 // performed during startup, and in context of reinit administrative thread
171 LogTimeEntry::lock();
172
173 LastLogTimes::iterator times = mTimes.begin();
174 while(times != mTimes.end()) {
175 LogTimeEntry *entry = (*times);
176 if (entry->owned_Locked()) {
177 entry->triggerReader_Locked();
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700178 }
Mark Salyzyn552b4752015-11-30 11:35:56 -0800179 times++;
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700180 }
Mark Salyzyn552b4752015-11-30 11:35:56 -0800181
182 LogTimeEntry::unlock();
Mark Salyzyn12bac902014-02-26 09:50:16 -0800183}
184
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700185LogBuffer::LogBuffer(LastLogTimes *times):
Mark Salyzyn2d4f9bc2015-12-01 15:57:25 -0800186 monotonic(android_log_clockid() == CLOCK_MONOTONIC),
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700187 mTimes(*times) {
Mark Salyzyn3fe25932015-03-10 16:45:17 -0700188 pthread_mutex_init(&mLogElementsLock, NULL);
189
190 init();
191}
192
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800193int LogBuffer::log(log_id_t log_id, log_time realtime,
194 uid_t uid, pid_t pid, pid_t tid,
195 const char *msg, unsigned short len) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800196 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800197 return -EINVAL;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800198 }
Mark Salyzyn19c91112014-10-02 13:07:05 -0700199
Mark Salyzyn12bac902014-02-26 09:50:16 -0800200 LogBufferElement *elem = new LogBufferElement(log_id, realtime,
Mark Salyzyn3661dd02014-03-20 16:09:38 -0700201 uid, pid, tid, msg, len);
Mark Salyzyn19c91112014-10-02 13:07:05 -0700202 int prio = ANDROID_LOG_INFO;
203 const char *tag = NULL;
204 if (log_id == LOG_ID_EVENTS) {
205 tag = android::tagToName(elem->getTag());
206 } else {
207 prio = *msg;
208 tag = msg + 1;
209 }
Mark Salyzyn27e10b92015-11-16 19:00:41 +0000210 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Mark Salyzyn19c91112014-10-02 13:07:05 -0700211 // Log traffic received to total
212 pthread_mutex_lock(&mLogElementsLock);
213 stats.add(elem);
214 stats.subtract(elem);
215 pthread_mutex_unlock(&mLogElementsLock);
216 delete elem;
217 return -EACCES;
218 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800219
220 pthread_mutex_lock(&mLogElementsLock);
221
222 // Insert elements in time sorted order if possible
223 // NB: if end is region locked, place element at end of list
224 LogBufferElementCollection::iterator it = mLogElements.end();
225 LogBufferElementCollection::iterator last = it;
Mark Salyzyna222a772014-10-13 16:49:47 -0700226 while (last != mLogElements.begin()) {
227 --it;
Mark Salyzyn5b7a8d82014-02-18 11:23:53 -0800228 if ((*it)->getRealTime() <= realtime) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800229 break;
230 }
231 last = it;
232 }
Mark Salyzyn5b7a8d82014-02-18 11:23:53 -0800233
Mark Salyzyn12bac902014-02-26 09:50:16 -0800234 if (last == mLogElements.end()) {
235 mLogElements.push_back(elem);
236 } else {
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800237 uint64_t end = 1;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800238 bool end_set = false;
239 bool end_always = false;
240
241 LogTimeEntry::lock();
242
243 LastLogTimes::iterator t = mTimes.begin();
244 while(t != mTimes.end()) {
245 LogTimeEntry *entry = (*t);
246 if (entry->owned_Locked()) {
247 if (!entry->mNonBlock) {
248 end_always = true;
249 break;
250 }
251 if (!end_set || (end <= entry->mEnd)) {
252 end = entry->mEnd;
253 end_set = true;
254 }
255 }
256 t++;
257 }
258
259 if (end_always
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800260 || (end_set && (end >= (*last)->getSequence()))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800261 mLogElements.push_back(elem);
262 } else {
263 mLogElements.insert(last,elem);
264 }
265
266 LogTimeEntry::unlock();
267 }
268
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700269 stats.add(elem);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800270 maybePrune(log_id);
271 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800272
273 return len;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800274}
275
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700276// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn12bac902014-02-26 09:50:16 -0800277//
278// mLogElementsLock must be held when this function is called.
279void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800280 size_t sizes = stats.sizes(id);
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700281 unsigned long maxSize = log_buffer_size(id);
282 if (sizes > maxSize) {
Mark Salyzyn63745722015-08-19 12:20:36 -0700283 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzynd745c722015-09-30 07:40:09 -0700284 size_t elements = stats.realElements(id);
285 size_t minElements = elements / 100;
286 if (minElements < minPrune) {
287 minElements = minPrune;
288 }
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700289 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700290 if (pruneRows < minElements) {
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700291 pruneRows = minElements;
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800292 }
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700293 if (pruneRows > maxPrune) {
294 pruneRows = maxPrune;
Mark Salyzyn63745722015-08-19 12:20:36 -0700295 }
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800296 prune(id, pruneRows);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800297 }
298}
299
Mark Salyzyn57b82a62015-09-03 16:08:50 -0700300LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700301 LogBufferElementCollection::iterator it, bool coalesce) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700302 LogBufferElement *e = *it;
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700303 log_id_t id = e->getLogId();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700304
Mark Salyzyn50e21082015-09-08 09:12:51 -0700305 LogBufferIteratorMap::iterator f = mLastWorstUid[id].find(e->getUid());
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700306 if ((f != mLastWorstUid[id].end()) && (it == f->second)) {
307 mLastWorstUid[id].erase(f);
308 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700309 it = mLogElements.erase(it);
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700310 if (coalesce) {
Mark Salyzyn57b82a62015-09-03 16:08:50 -0700311 stats.erase(e);
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700312 } else {
313 stats.subtract(e);
Mark Salyzyn57b82a62015-09-03 16:08:50 -0700314 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700315 delete e;
316
317 return it;
318}
319
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700320// Define a temporary mechanism to report the last LogBufferElement pointer
321// for the specified uid, pid and tid. Used below to help merge-sort when
322// pruning for worst UID.
323class LogBufferElementKey {
324 const union {
325 struct {
326 uint16_t uid;
327 uint16_t pid;
328 uint16_t tid;
329 uint16_t padding;
330 } __packed;
331 uint64_t value;
332 } __packed;
333
334public:
335 LogBufferElementKey(uid_t u, pid_t p, pid_t t):uid(u),pid(p),tid(t),padding(0) { }
336 LogBufferElementKey(uint64_t k):value(k) { }
337
338 uint64_t getKey() { return value; }
339};
340
Mark Salyzyn6a404192015-05-19 09:12:30 -0700341class LogBufferElementLast {
342
343 typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap;
344 LogBufferElementMap map;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700345
346public:
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700347
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700348 bool coalesce(LogBufferElement *e, unsigned short dropped) {
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700349 LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid());
Mark Salyzyn6a404192015-05-19 09:12:30 -0700350 LogBufferElementMap::iterator it = map.find(key.getKey());
351 if (it != map.end()) {
352 LogBufferElement *l = it->second;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700353 unsigned short d = l->getDropped();
354 if ((dropped + d) > USHRT_MAX) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700355 map.erase(it);
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700356 } else {
357 l->setDropped(dropped + d);
358 return true;
359 }
360 }
361 return false;
362 }
363
Mark Salyzyn6a404192015-05-19 09:12:30 -0700364 void add(LogBufferElement *e) {
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700365 LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid());
Mark Salyzyn6a404192015-05-19 09:12:30 -0700366 map[key.getKey()] = e;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700367 }
368
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700369 inline void clear() {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700370 map.clear();
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700371 }
372
373 void clear(LogBufferElement *e) {
Mark Salyzyn80c1fc62015-06-04 13:35:30 -0700374 uint64_t current = e->getRealTime().nsec()
375 - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn6a404192015-05-19 09:12:30 -0700376 for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
377 LogBufferElement *l = it->second;
Mark Salyzyn4f199f32015-05-15 15:58:17 -0700378 if ((l->getDropped() >= EXPIRE_THRESHOLD)
379 && (current > l->getRealTime().nsec())) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700380 it = map.erase(it);
381 } else {
382 ++it;
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700383 }
384 }
385 }
386
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700387};
388
Mark Salyzyn12bac902014-02-26 09:50:16 -0800389// prune "pruneRows" of type "id" from the buffer.
390//
Mark Salyzyn50e21082015-09-08 09:12:51 -0700391// This garbage collection task is used to expire log entries. It is called to
392// remove all logs (clear), all UID logs (unprivileged clear), or every
393// 256 or 10% of the total logs (whichever is less) to prune the logs.
394//
395// First there is a prep phase where we discover the reader region lock that
396// acts as a backstop to any pruning activity to stop there and go no further.
397//
398// There are three major pruning loops that follow. All expire from the oldest
399// entries. Since there are multiple log buffers, the Android logging facility
400// will appear to drop entries 'in the middle' when looking at multiple log
401// sources and buffers. This effect is slightly more prominent when we prune
402// the worst offender by logging source. Thus the logs slowly loose content
403// and value as you move back in time. This is preferred since chatty sources
404// invariably move the logs value down faster as less chatty sources would be
405// expired in the noise.
406//
407// The first loop performs blacklisting and worst offender pruning. Falling
408// through when there are no notable worst offenders and have not hit the
409// region lock preventing further worst offender pruning. This loop also looks
410// after managing the chatty log entries and merging to help provide
411// statistical basis for blame. The chatty entries are not a notification of
412// how much logs you may have, but instead represent how much logs you would
413// have had in a virtual log buffer that is extended to cover all the in-memory
414// logs without loss. They last much longer than the represented pruned logs
415// since they get multiplied by the gains in the non-chatty log sources.
416//
417// The second loop get complicated because an algorithm of watermarks and
418// history is maintained to reduce the order and keep processing time
419// down to a minimum at scale. These algorithms can be costly in the face
420// of larger log buffers, or severly limited processing time granted to a
421// background task at lowest priority.
422//
423// This second loop does straight-up expiration from the end of the logs
424// (again, remember for the specified log buffer id) but does some whitelist
425// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
426// spam filtration all take priority. This second loop also checks if a region
427// lock is causing us to buffer too much in the logs to help the reader(s),
428// and will tell the slowest reader thread to skip log entries, and if
429// persistent and hits a further threshold, kill the reader thread.
430//
431// The third thread is optional, and only gets hit if there was a whitelist
432// and more needs to be pruned against the backstop of the region lock.
433//
Mark Salyzyn12bac902014-02-26 09:50:16 -0800434// mLogElementsLock must be held when this function is called.
Mark Salyzyn50e21082015-09-08 09:12:51 -0700435//
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700436bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800437 LogTimeEntry *oldest = NULL;
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700438 bool busy = false;
Mark Salyzynd280d812015-09-16 15:34:00 -0700439 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800440
441 LogTimeEntry::lock();
442
443 // Region locked?
444 LastLogTimes::iterator t = mTimes.begin();
445 while(t != mTimes.end()) {
446 LogTimeEntry *entry = (*t);
TraianX Schiau6ba427e2014-12-17 10:53:41 +0200447 if (entry->owned_Locked() && entry->isWatching(id)
Mark Salyzyn552b4752015-11-30 11:35:56 -0800448 && (!oldest ||
449 (oldest->mStart > entry->mStart) ||
450 ((oldest->mStart == entry->mStart) &&
451 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800452 oldest = entry;
453 }
454 t++;
455 }
456
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800457 LogBufferElementCollection::iterator it;
458
Mark Salyzyn276d5352014-06-12 11:16:16 -0700459 if (caller_uid != AID_ROOT) {
Mark Salyzynd280d812015-09-16 15:34:00 -0700460 // Only here if clearAll condition (pruneRows == ULONG_MAX)
Mark Salyzyn276d5352014-06-12 11:16:16 -0700461 for(it = mLogElements.begin(); it != mLogElements.end();) {
462 LogBufferElement *e = *it;
463
Mark Salyzynd280d812015-09-16 15:34:00 -0700464 if ((e->getLogId() != id) || (e->getUid() != caller_uid)) {
465 ++it;
466 continue;
467 }
468
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800469 if (oldest && (oldest->mStart <= e->getSequence())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700470 busy = true;
Mark Salyzyn552b4752015-11-30 11:35:56 -0800471 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
472 oldest->triggerReader_Locked();
473 } else {
474 oldest->triggerSkip_Locked(id, pruneRows);
475 }
Mark Salyzyn276d5352014-06-12 11:16:16 -0700476 break;
477 }
478
Mark Salyzynd280d812015-09-16 15:34:00 -0700479 it = erase(it);
480 pruneRows--;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700481 }
482 LogTimeEntry::unlock();
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700483 return busy;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700484 }
485
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800486 // prune by worst offender by uid
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700487 bool hasBlacklist = mPrune.naughty();
Mark Salyzynd280d812015-09-16 15:34:00 -0700488 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800489 // recalculate the worst offender on every batched pass
490 uid_t worst = (uid_t) -1;
491 size_t worst_sizes = 0;
492 size_t second_worst_sizes = 0;
493
Mark Salyzyn0e765c62015-03-17 17:17:25 -0700494 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn1435b472015-03-16 08:26:05 -0700495 std::unique_ptr<const UidEntry *[]> sorted = stats.sort(2, id);
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700496
Mark Salyzyn1435b472015-03-16 08:26:05 -0700497 if (sorted.get()) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700498 if (sorted[0] && sorted[1]) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700499 worst_sizes = sorted[0]->getSizes();
Mark Salyzyn84a83982015-04-20 15:36:12 -0700500 // Calculate threshold as 12.5% of available storage
501 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn2b9465b2015-10-12 13:45:51 -0700502 if ((worst_sizes > threshold)
503 // Allow time horizon to extend roughly tenfold, assume
504 // average entry length is 100 characters.
505 && (worst_sizes > (10 * sorted[0]->getDropped()))) {
Mark Salyzyn84a83982015-04-20 15:36:12 -0700506 worst = sorted[0]->getKey();
507 second_worst_sizes = sorted[1]->getSizes();
508 if (second_worst_sizes < threshold) {
509 second_worst_sizes = threshold;
510 }
511 }
Mark Salyzync89839a2014-02-11 12:29:31 -0800512 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800513 }
514 }
515
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700516 // skip if we have neither worst nor naughty filters
517 if ((worst == (uid_t) -1) && !hasBlacklist) {
518 break;
519 }
520
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800521 bool kick = false;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700522 bool leading = true;
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700523 it = mLogElements.begin();
Mark Salyzyn50e21082015-09-08 09:12:51 -0700524 // Perform at least one mandatory garbage collection cycle in following
525 // - clear leading chatty tags
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700526 // - coalesce chatty tags
Mark Salyzyn50e21082015-09-08 09:12:51 -0700527 // - check age-out of preserved logs
528 bool gc = pruneRows <= 1;
529 if (!gc && (worst != (uid_t) -1)) {
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700530 LogBufferIteratorMap::iterator f = mLastWorstUid[id].find(worst);
531 if ((f != mLastWorstUid[id].end())
532 && (f->second != mLogElements.end())) {
533 leading = false;
534 it = f->second;
535 }
536 }
Mark Salyzyn4e29e172015-08-24 13:43:27 -0700537 static const timespec too_old = {
538 EXPIRE_HOUR_THRESHOLD * 60 * 60, 0
539 };
540 LogBufferElementCollection::iterator lastt;
541 lastt = mLogElements.end();
542 --lastt;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700543 LogBufferElementLast last;
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700544 while (it != mLogElements.end()) {
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800545 LogBufferElement *e = *it;
546
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800547 if (oldest && (oldest->mStart <= e->getSequence())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700548 busy = true;
Mark Salyzyn552b4752015-11-30 11:35:56 -0800549 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
550 oldest->triggerReader_Locked();
551 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800552 break;
553 }
554
Mark Salyzync89839a2014-02-11 12:29:31 -0800555 if (e->getLogId() != id) {
556 ++it;
557 continue;
558 }
559
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700560 unsigned short dropped = e->getDropped();
Mark Salyzync89839a2014-02-11 12:29:31 -0800561
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700562 // remove any leading drops
563 if (leading && dropped) {
564 it = erase(it);
565 continue;
566 }
567
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700568 if (dropped && last.coalesce(e, dropped)) {
569 it = erase(it, true);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700570 continue;
571 }
572
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700573 if (hasBlacklist && mPrune.naughty(e)) {
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700574 last.clear(e);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700575 it = erase(it);
576 if (dropped) {
577 continue;
578 }
579
580 pruneRows--;
581 if (pruneRows == 0) {
582 break;
583 }
584
585 if (e->getUid() == worst) {
586 kick = true;
587 if (worst_sizes < second_worst_sizes) {
588 break;
589 }
590 worst_sizes -= e->getMsgLen();
591 }
592 continue;
593 }
594
Mark Salyzyn4e29e172015-08-24 13:43:27 -0700595 if ((e->getRealTime() < ((*lastt)->getRealTime() - too_old))
596 || (e->getRealTime() > (*lastt)->getRealTime())) {
597 break;
598 }
599
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700600 if (dropped) {
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700601 last.add(e);
Mark Salyzyn50e21082015-09-08 09:12:51 -0700602 if ((!gc && (e->getUid() == worst))
Mark Salyzyncb15c7c2015-08-24 13:43:27 -0700603 || (mLastWorstUid[id].find(e->getUid())
604 == mLastWorstUid[id].end())) {
605 mLastWorstUid[id][e->getUid()] = it;
606 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800607 ++it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700608 continue;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800609 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700610
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700611 if (e->getUid() != worst) {
Mark Salyzynfd318a82015-06-01 09:41:19 -0700612 leading = false;
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700613 last.clear(e);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700614 ++it;
615 continue;
616 }
617
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700618 pruneRows--;
619 if (pruneRows == 0) {
620 break;
621 }
622
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700623 kick = true;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700624
625 unsigned short len = e->getMsgLen();
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700626
627 // do not create any leading drops
628 if (leading) {
629 it = erase(it);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700630 } else {
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700631 stats.drop(e);
632 e->setDropped(1);
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700633 if (last.coalesce(e, 1)) {
634 it = erase(it, true);
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700635 } else {
636 last.add(e);
Mark Salyzyn50e21082015-09-08 09:12:51 -0700637 if (!gc || (mLastWorstUid[id].find(worst)
638 == mLastWorstUid[id].end())) {
639 mLastWorstUid[id][worst] = it;
640 }
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700641 ++it;
642 }
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700643 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700644 if (worst_sizes < second_worst_sizes) {
645 break;
646 }
647 worst_sizes -= len;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800648 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700649 last.clear();
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800650
Mark Salyzync402bc72014-04-01 17:19:47 -0700651 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800652 break; // the following loop will ask bad clients to skip/drop
653 }
654 }
655
Mark Salyzync89839a2014-02-11 12:29:31 -0800656 bool whitelist = false;
Mark Salyzynd280d812015-09-16 15:34:00 -0700657 bool hasWhitelist = mPrune.nice() && !clearAll;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800658 it = mLogElements.begin();
Mark Salyzyn12bac902014-02-26 09:50:16 -0800659 while((pruneRows > 0) && (it != mLogElements.end())) {
660 LogBufferElement *e = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700661
662 if (e->getLogId() != id) {
663 it++;
664 continue;
665 }
666
667 if (oldest && (oldest->mStart <= e->getSequence())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700668 busy = true;
669
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700670 if (whitelist) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800671 break;
672 }
Mark Salyzync402bc72014-04-01 17:19:47 -0700673
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700674 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
675 // kick a misbehaving log reader client off the island
676 oldest->release_Locked();
Mark Salyzyn552b4752015-11-30 11:35:56 -0800677 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
678 oldest->triggerReader_Locked();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700679 } else {
680 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzync89839a2014-02-11 12:29:31 -0800681 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700682 break;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800683 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700684
Mark Salyzynd910ca92015-05-21 12:50:31 -0700685 if (hasWhitelist && !e->getDropped() && mPrune.nice(e)) { // WhiteListed
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700686 whitelist = true;
687 it++;
688 continue;
689 }
690
691 it = erase(it);
692 pruneRows--;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800693 }
694
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700695 // Do not save the whitelist if we are reader range limited
Mark Salyzync89839a2014-02-11 12:29:31 -0800696 if (whitelist && (pruneRows > 0)) {
697 it = mLogElements.begin();
698 while((it != mLogElements.end()) && (pruneRows > 0)) {
699 LogBufferElement *e = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700700
701 if (e->getLogId() != id) {
702 ++it;
703 continue;
Mark Salyzync89839a2014-02-11 12:29:31 -0800704 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700705
706 if (oldest && (oldest->mStart <= e->getSequence())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700707 busy = true;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700708 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
709 // kick a misbehaving log reader client off the island
710 oldest->release_Locked();
Mark Salyzyn552b4752015-11-30 11:35:56 -0800711 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
712 oldest->triggerReader_Locked();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700713 } else {
714 oldest->triggerSkip_Locked(id, pruneRows);
715 }
716 break;
717 }
718
719 it = erase(it);
720 pruneRows--;
Mark Salyzync89839a2014-02-11 12:29:31 -0800721 }
722 }
Mark Salyzync89839a2014-02-11 12:29:31 -0800723
Mark Salyzyn12bac902014-02-26 09:50:16 -0800724 LogTimeEntry::unlock();
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700725
726 return (pruneRows > 0) && busy;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800727}
728
729// clear all rows of type "id" from the buffer.
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700730bool LogBuffer::clear(log_id_t id, uid_t uid) {
731 bool busy = true;
732 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
733 for (int retry = 4;;) {
734 if (retry == 1) { // last pass
735 // Check if it is still busy after the sleep, we say prune
736 // one entry, not another clear run, so we are looking for
737 // the quick side effect of the return value to tell us if
738 // we have a _blocked_ reader.
739 pthread_mutex_lock(&mLogElementsLock);
740 busy = prune(id, 1, uid);
741 pthread_mutex_unlock(&mLogElementsLock);
742 // It is still busy, blocked reader(s), lets kill them all!
743 // otherwise, lets be a good citizen and preserve the slow
744 // readers and let the clear run (below) deal with determining
745 // if we are still blocked and return an error code to caller.
746 if (busy) {
747 LogTimeEntry::lock();
748 LastLogTimes::iterator times = mTimes.begin();
749 while (times != mTimes.end()) {
750 LogTimeEntry *entry = (*times);
751 // Killer punch
752 if (entry->owned_Locked() && entry->isWatching(id)) {
753 entry->release_Locked();
754 }
755 times++;
756 }
757 LogTimeEntry::unlock();
758 }
759 }
760 pthread_mutex_lock(&mLogElementsLock);
761 busy = prune(id, ULONG_MAX, uid);
762 pthread_mutex_unlock(&mLogElementsLock);
763 if (!busy || !--retry) {
764 break;
765 }
766 sleep (1); // Let reader(s) catch up after notification
767 }
768 return busy;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800769}
770
771// get the used space associated with "id".
772unsigned long LogBuffer::getSizeUsed(log_id_t id) {
773 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzynd774bce2014-02-06 14:48:50 -0800774 size_t retval = stats.sizes(id);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800775 pthread_mutex_unlock(&mLogElementsLock);
776 return retval;
777}
778
Mark Salyzync89839a2014-02-11 12:29:31 -0800779// set the total space allocated to "id"
780int LogBuffer::setSize(log_id_t id, unsigned long size) {
781 // Reasonable limits ...
Mark Salyzyn7b85c592014-05-09 17:44:18 -0700782 if (!valid_size(size)) {
Mark Salyzync89839a2014-02-11 12:29:31 -0800783 return -1;
784 }
785 pthread_mutex_lock(&mLogElementsLock);
786 log_buffer_size(id) = size;
787 pthread_mutex_unlock(&mLogElementsLock);
788 return 0;
789}
790
791// get the total space allocated to "id"
792unsigned long LogBuffer::getSize(log_id_t id) {
793 pthread_mutex_lock(&mLogElementsLock);
794 size_t retval = log_buffer_size(id);
795 pthread_mutex_unlock(&mLogElementsLock);
796 return retval;
797}
798
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800799uint64_t LogBuffer::flushTo(
800 SocketClient *reader, const uint64_t start, bool privileged,
801 int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800802 LogBufferElementCollection::iterator it;
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800803 uint64_t max = start;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800804 uid_t uid = reader->getUid();
805
806 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -0600807
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800808 if (start <= 1) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -0600809 // client wants to start from the beginning
810 it = mLogElements.begin();
811 } else {
812 // Client wants to start from some specified time. Chances are
813 // we are better off starting from the end of the time sorted list.
814 for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
815 --it;
816 LogBufferElement *element = *it;
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800817 if (element->getSequence() <= start) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -0600818 it++;
819 break;
820 }
821 }
822 }
823
824 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800825 LogBufferElement *element = *it;
826
827 if (!privileged && (element->getUid() != uid)) {
828 continue;
829 }
830
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800831 if (element->getSequence() <= start) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800832 continue;
833 }
834
835 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800836 if (filter) {
837 int ret = (*filter)(element, arg);
838 if (ret == false) {
839 continue;
840 }
841 if (ret != true) {
842 break;
843 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800844 }
845
846 pthread_mutex_unlock(&mLogElementsLock);
847
848 // range locking in LastLogTimes looks after us
Mark Salyzyn3443fda2015-12-03 15:38:35 -0800849 max = element->flushTo(reader, this, privileged);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800850
851 if (max == element->FLUSH_ERROR) {
852 return max;
853 }
854
855 pthread_mutex_lock(&mLogElementsLock);
856 }
857 pthread_mutex_unlock(&mLogElementsLock);
858
859 return max;
860}
Mark Salyzynd774bce2014-02-06 14:48:50 -0800861
Mark Salyzyn1c7b6fb2015-08-20 10:01:44 -0700862std::string LogBuffer::formatStatistics(uid_t uid, unsigned int logMask) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800863 pthread_mutex_lock(&mLogElementsLock);
864
Mark Salyzyn1c7b6fb2015-08-20 10:01:44 -0700865 std::string ret = stats.format(uid, logMask);
Mark Salyzynd774bce2014-02-06 14:48:50 -0800866
867 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn1c7b6fb2015-08-20 10:01:44 -0700868
869 return ret;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800870}