blob: 4ada7880228d12dd8ff8a64e633636a14922f5f5 [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 Salyzyn4e756fb2014-05-06 07:34:59 -070031#include "LogReader.h"
Mark Salyzyn12bac902014-02-26 09:50:16 -080032
Mark Salyzync89839a2014-02-11 12:29:31 -080033// Default
Mark Salyzyn12bac902014-02-26 09:50:16 -080034#define LOG_BUFFER_SIZE (256 * 1024) // Tuned on a per-platform basis here?
Mark Salyzync89839a2014-02-11 12:29:31 -080035#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn7b85c592014-05-09 17:44:18 -070036#define LOG_BUFFER_MIN_SIZE (64 * 1024UL)
37#define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL)
38
39static bool valid_size(unsigned long value) {
40 if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) {
41 return false;
42 }
43
44 long pages = sysconf(_SC_PHYS_PAGES);
45 if (pages < 1) {
46 return true;
47 }
48
49 long pagesize = sysconf(_SC_PAGESIZE);
50 if (pagesize <= 1) {
51 pagesize = PAGE_SIZE;
52 }
53
54 // maximum memory impact a somewhat arbitrary ~3%
55 pages = (pages + 31) / 32;
56 unsigned long maximum = pages * pagesize;
57
58 if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) {
59 return true;
60 }
61
62 return value <= maximum;
63}
Mark Salyzyn12bac902014-02-26 09:50:16 -080064
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070065static unsigned long property_get_size(const char *key) {
66 char property[PROPERTY_VALUE_MAX];
67 property_get(key, property, "");
68
69 char *cp;
70 unsigned long value = strtoul(property, &cp, 10);
71
72 switch(*cp) {
73 case 'm':
74 case 'M':
75 value *= 1024;
76 /* FALLTHRU */
77 case 'k':
78 case 'K':
79 value *= 1024;
80 /* FALLTHRU */
81 case '\0':
82 break;
83
84 default:
85 value = 0;
86 }
87
Mark Salyzyn7b85c592014-05-09 17:44:18 -070088 if (!valid_size(value)) {
89 value = 0;
90 }
91
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070092 return value;
93}
94
Mark Salyzyn3fe25932015-03-10 16:45:17 -070095void LogBuffer::init() {
Mark Salyzyn7b85c592014-05-09 17:44:18 -070096 static const char global_tuneable[] = "persist.logd.size"; // Settings App
97 static const char global_default[] = "ro.logd.size"; // BoardConfig.mk
98
99 unsigned long default_size = property_get_size(global_tuneable);
100 if (!default_size) {
101 default_size = property_get_size(global_default);
102 }
Mark Salyzyn4e756fb2014-05-06 07:34:59 -0700103
Mark Salyzync89839a2014-02-11 12:29:31 -0800104 log_id_for_each(i) {
Mark Salyzyn4e756fb2014-05-06 07:34:59 -0700105 char key[PROP_NAME_MAX];
Mark Salyzyn4e756fb2014-05-06 07:34:59 -0700106
Mark Salyzyn7b85c592014-05-09 17:44:18 -0700107 snprintf(key, sizeof(key), "%s.%s",
108 global_tuneable, android_log_id_to_name(i));
109 unsigned long property_size = property_get_size(key);
110
111 if (!property_size) {
112 snprintf(key, sizeof(key), "%s.%s",
113 global_default, android_log_id_to_name(i));
114 property_size = property_get_size(key);
115 }
116
117 if (!property_size) {
118 property_size = default_size;
119 }
120
121 if (!property_size) {
122 property_size = LOG_BUFFER_SIZE;
123 }
124
125 if (setSize(i, property_size)) {
126 setSize(i, LOG_BUFFER_MIN_SIZE);
127 }
Mark Salyzync89839a2014-02-11 12:29:31 -0800128 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800129}
130
Mark Salyzyncd766f92015-05-12 15:21:31 -0700131LogBuffer::LogBuffer(LastLogTimes *times) : mTimes(*times) {
Mark Salyzyn3fe25932015-03-10 16:45:17 -0700132 pthread_mutex_init(&mLogElementsLock, NULL);
133
134 init();
135}
136
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800137int LogBuffer::log(log_id_t log_id, log_time realtime,
138 uid_t uid, pid_t pid, pid_t tid,
139 const char *msg, unsigned short len) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800140 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800141 return -EINVAL;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800142 }
Mark Salyzyn0dcfae62014-10-02 13:07:05 -0700143
Mark Salyzyn12bac902014-02-26 09:50:16 -0800144 LogBufferElement *elem = new LogBufferElement(log_id, realtime,
Mark Salyzyn3661dd02014-03-20 16:09:38 -0700145 uid, pid, tid, msg, len);
Mark Salyzyn0dcfae62014-10-02 13:07:05 -0700146 int prio = ANDROID_LOG_INFO;
147 const char *tag = NULL;
148 if (log_id == LOG_ID_EVENTS) {
149 tag = android::tagToName(elem->getTag());
150 } else {
151 prio = *msg;
152 tag = msg + 1;
153 }
154 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
155 // Log traffic received to total
156 pthread_mutex_lock(&mLogElementsLock);
157 stats.add(elem);
158 stats.subtract(elem);
159 pthread_mutex_unlock(&mLogElementsLock);
160 delete elem;
161 return -EACCES;
162 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800163
164 pthread_mutex_lock(&mLogElementsLock);
165
166 // Insert elements in time sorted order if possible
167 // NB: if end is region locked, place element at end of list
168 LogBufferElementCollection::iterator it = mLogElements.end();
169 LogBufferElementCollection::iterator last = it;
Mark Salyzyna222a772014-10-13 16:49:47 -0700170 while (last != mLogElements.begin()) {
171 --it;
Mark Salyzyn5b7a8d82014-02-18 11:23:53 -0800172 if ((*it)->getRealTime() <= realtime) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800173 break;
174 }
175 last = it;
176 }
Mark Salyzyn5b7a8d82014-02-18 11:23:53 -0800177
Mark Salyzyn12bac902014-02-26 09:50:16 -0800178 if (last == mLogElements.end()) {
179 mLogElements.push_back(elem);
180 } else {
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800181 uint64_t end = 1;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800182 bool end_set = false;
183 bool end_always = false;
184
185 LogTimeEntry::lock();
186
187 LastLogTimes::iterator t = mTimes.begin();
188 while(t != mTimes.end()) {
189 LogTimeEntry *entry = (*t);
190 if (entry->owned_Locked()) {
191 if (!entry->mNonBlock) {
192 end_always = true;
193 break;
194 }
195 if (!end_set || (end <= entry->mEnd)) {
196 end = entry->mEnd;
197 end_set = true;
198 }
199 }
200 t++;
201 }
202
203 if (end_always
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800204 || (end_set && (end >= (*last)->getSequence()))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800205 mLogElements.push_back(elem);
206 } else {
207 mLogElements.insert(last,elem);
208 }
209
210 LogTimeEntry::unlock();
211 }
212
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700213 stats.add(elem);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800214 maybePrune(log_id);
215 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800216
217 return len;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800218}
219
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700220// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn12bac902014-02-26 09:50:16 -0800221//
222// mLogElementsLock must be held when this function is called.
223void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800224 size_t sizes = stats.sizes(id);
Mark Salyzyn5f190312015-08-10 10:23:56 -0700225 unsigned long maxSize = log_buffer_size(id);
226 if (sizes > maxSize) {
Mark Salyzyn185d8da2015-08-19 12:20:36 -0700227 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn49858cc2015-09-30 07:40:09 -0700228 size_t elements = stats.realElements(id);
229 size_t minElements = elements / 100;
230 if (minElements < minPrune) {
231 minElements = minPrune;
232 }
Mark Salyzyn5f190312015-08-10 10:23:56 -0700233 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700234 if (pruneRows < minElements) {
Mark Salyzyn5f190312015-08-10 10:23:56 -0700235 pruneRows = minElements;
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800236 }
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700237 if (pruneRows > maxPrune) {
238 pruneRows = maxPrune;
Mark Salyzyn185d8da2015-08-19 12:20:36 -0700239 }
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800240 prune(id, pruneRows);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800241 }
242}
243
Mark Salyzyn1fdf71f2015-09-03 16:08:50 -0700244LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700245 LogBufferElementCollection::iterator it, bool coalesce) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700246 LogBufferElement *e = *it;
Mark Salyzyn5af07be2015-08-19 17:06:11 -0700247 log_id_t id = e->getLogId();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700248
Mark Salyzyne2827d52015-09-08 09:12:51 -0700249 LogBufferIteratorMap::iterator f = mLastWorstUid[id].find(e->getUid());
Mark Salyzyn5af07be2015-08-19 17:06:11 -0700250 if ((f != mLastWorstUid[id].end()) && (it == f->second)) {
251 mLastWorstUid[id].erase(f);
252 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700253 it = mLogElements.erase(it);
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700254 if (coalesce) {
Mark Salyzyn1fdf71f2015-09-03 16:08:50 -0700255 stats.erase(e);
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700256 } else {
257 stats.subtract(e);
Mark Salyzyn1fdf71f2015-09-03 16:08:50 -0700258 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700259 delete e;
260
261 return it;
262}
263
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700264// Define a temporary mechanism to report the last LogBufferElement pointer
265// for the specified uid, pid and tid. Used below to help merge-sort when
266// pruning for worst UID.
267class LogBufferElementKey {
268 const union {
269 struct {
270 uint16_t uid;
271 uint16_t pid;
272 uint16_t tid;
273 uint16_t padding;
274 } __packed;
275 uint64_t value;
276 } __packed;
277
278public:
279 LogBufferElementKey(uid_t u, pid_t p, pid_t t):uid(u),pid(p),tid(t),padding(0) { }
280 LogBufferElementKey(uint64_t k):value(k) { }
281
282 uint64_t getKey() { return value; }
283};
284
Mark Salyzyn6a404192015-05-19 09:12:30 -0700285class LogBufferElementLast {
286
287 typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap;
288 LogBufferElementMap map;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700289
290public:
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700291
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700292 bool coalesce(LogBufferElement *e, unsigned short dropped) {
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700293 LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid());
Mark Salyzyn6a404192015-05-19 09:12:30 -0700294 LogBufferElementMap::iterator it = map.find(key.getKey());
295 if (it != map.end()) {
296 LogBufferElement *l = it->second;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700297 unsigned short d = l->getDropped();
298 if ((dropped + d) > USHRT_MAX) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700299 map.erase(it);
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700300 } else {
301 l->setDropped(dropped + d);
302 return true;
303 }
304 }
305 return false;
306 }
307
Mark Salyzyn6a404192015-05-19 09:12:30 -0700308 void add(LogBufferElement *e) {
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700309 LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid());
Mark Salyzyn6a404192015-05-19 09:12:30 -0700310 map[key.getKey()] = e;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700311 }
312
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700313 inline void clear() {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700314 map.clear();
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700315 }
316
317 void clear(LogBufferElement *e) {
Mark Salyzyn80c1fc62015-06-04 13:35:30 -0700318 uint64_t current = e->getRealTime().nsec()
319 - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn6a404192015-05-19 09:12:30 -0700320 for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
321 LogBufferElement *l = it->second;
Mark Salyzyn4f199f32015-05-15 15:58:17 -0700322 if ((l->getDropped() >= EXPIRE_THRESHOLD)
323 && (current > l->getRealTime().nsec())) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700324 it = map.erase(it);
325 } else {
326 ++it;
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700327 }
328 }
329 }
330
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700331};
332
Mark Salyzyn12bac902014-02-26 09:50:16 -0800333// prune "pruneRows" of type "id" from the buffer.
334//
Mark Salyzyne2827d52015-09-08 09:12:51 -0700335// This garbage collection task is used to expire log entries. It is called to
336// remove all logs (clear), all UID logs (unprivileged clear), or every
337// 256 or 10% of the total logs (whichever is less) to prune the logs.
338//
339// First there is a prep phase where we discover the reader region lock that
340// acts as a backstop to any pruning activity to stop there and go no further.
341//
342// There are three major pruning loops that follow. All expire from the oldest
343// entries. Since there are multiple log buffers, the Android logging facility
344// will appear to drop entries 'in the middle' when looking at multiple log
345// sources and buffers. This effect is slightly more prominent when we prune
346// the worst offender by logging source. Thus the logs slowly loose content
347// and value as you move back in time. This is preferred since chatty sources
348// invariably move the logs value down faster as less chatty sources would be
349// expired in the noise.
350//
351// The first loop performs blacklisting and worst offender pruning. Falling
352// through when there are no notable worst offenders and have not hit the
353// region lock preventing further worst offender pruning. This loop also looks
354// after managing the chatty log entries and merging to help provide
355// statistical basis for blame. The chatty entries are not a notification of
356// how much logs you may have, but instead represent how much logs you would
357// have had in a virtual log buffer that is extended to cover all the in-memory
358// logs without loss. They last much longer than the represented pruned logs
359// since they get multiplied by the gains in the non-chatty log sources.
360//
361// The second loop get complicated because an algorithm of watermarks and
362// history is maintained to reduce the order and keep processing time
363// down to a minimum at scale. These algorithms can be costly in the face
364// of larger log buffers, or severly limited processing time granted to a
365// background task at lowest priority.
366//
367// This second loop does straight-up expiration from the end of the logs
368// (again, remember for the specified log buffer id) but does some whitelist
369// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
370// spam filtration all take priority. This second loop also checks if a region
371// lock is causing us to buffer too much in the logs to help the reader(s),
372// and will tell the slowest reader thread to skip log entries, and if
373// persistent and hits a further threshold, kill the reader thread.
374//
375// The third thread is optional, and only gets hit if there was a whitelist
376// and more needs to be pruned against the backstop of the region lock.
377//
Mark Salyzyn12bac902014-02-26 09:50:16 -0800378// mLogElementsLock must be held when this function is called.
Mark Salyzyne2827d52015-09-08 09:12:51 -0700379//
Mark Salyzyn8cf0d032015-09-16 15:34:00 -0700380bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800381 LogTimeEntry *oldest = NULL;
Mark Salyzyn8cf0d032015-09-16 15:34:00 -0700382 bool busy = false;
Mark Salyzyn71541f32015-09-16 15:34:00 -0700383 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800384
385 LogTimeEntry::lock();
386
387 // Region locked?
388 LastLogTimes::iterator t = mTimes.begin();
389 while(t != mTimes.end()) {
390 LogTimeEntry *entry = (*t);
TraianX Schiau6ba427e2014-12-17 10:53:41 +0200391 if (entry->owned_Locked() && entry->isWatching(id)
Mark Salyzyn12bac902014-02-26 09:50:16 -0800392 && (!oldest || (oldest->mStart > entry->mStart))) {
393 oldest = entry;
394 }
395 t++;
396 }
397
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800398 LogBufferElementCollection::iterator it;
399
Mark Salyzyn276d5352014-06-12 11:16:16 -0700400 if (caller_uid != AID_ROOT) {
Mark Salyzyn71541f32015-09-16 15:34:00 -0700401 // Only here if clearAll condition (pruneRows == ULONG_MAX)
Mark Salyzyn276d5352014-06-12 11:16:16 -0700402 for(it = mLogElements.begin(); it != mLogElements.end();) {
403 LogBufferElement *e = *it;
404
Mark Salyzyn71541f32015-09-16 15:34:00 -0700405 if ((e->getLogId() != id) || (e->getUid() != caller_uid)) {
406 ++it;
407 continue;
408 }
409
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800410 if (oldest && (oldest->mStart <= e->getSequence())) {
Mark Salyzyn8cf0d032015-09-16 15:34:00 -0700411 oldest->triggerSkip_Locked(id, pruneRows);
412 busy = true;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700413 break;
414 }
415
Mark Salyzyn71541f32015-09-16 15:34:00 -0700416 it = erase(it);
417 pruneRows--;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700418 }
419 LogTimeEntry::unlock();
Mark Salyzyn8cf0d032015-09-16 15:34:00 -0700420 return busy;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700421 }
422
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800423 // prune by worst offender by uid
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700424 bool hasBlacklist = mPrune.naughty();
Mark Salyzyn71541f32015-09-16 15:34:00 -0700425 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800426 // recalculate the worst offender on every batched pass
427 uid_t worst = (uid_t) -1;
428 size_t worst_sizes = 0;
429 size_t second_worst_sizes = 0;
430
Mark Salyzyn0e765c62015-03-17 17:17:25 -0700431 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn1435b472015-03-16 08:26:05 -0700432 std::unique_ptr<const UidEntry *[]> sorted = stats.sort(2, id);
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700433
Mark Salyzyn1435b472015-03-16 08:26:05 -0700434 if (sorted.get()) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700435 if (sorted[0] && sorted[1]) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700436 worst_sizes = sorted[0]->getSizes();
Mark Salyzyn84a83982015-04-20 15:36:12 -0700437 // Calculate threshold as 12.5% of available storage
438 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn8b3b07f2015-10-12 13:45:51 -0700439 if ((worst_sizes > threshold)
440 // Allow time horizon to extend roughly tenfold, assume
441 // average entry length is 100 characters.
442 && (worst_sizes > (10 * sorted[0]->getDropped()))) {
Mark Salyzyn84a83982015-04-20 15:36:12 -0700443 worst = sorted[0]->getKey();
444 second_worst_sizes = sorted[1]->getSizes();
445 if (second_worst_sizes < threshold) {
446 second_worst_sizes = threshold;
447 }
448 }
Mark Salyzync89839a2014-02-11 12:29:31 -0800449 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800450 }
451 }
452
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700453 // skip if we have neither worst nor naughty filters
454 if ((worst == (uid_t) -1) && !hasBlacklist) {
455 break;
456 }
457
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800458 bool kick = false;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700459 bool leading = true;
Mark Salyzyn5af07be2015-08-19 17:06:11 -0700460 it = mLogElements.begin();
Mark Salyzyne2827d52015-09-08 09:12:51 -0700461 // Perform at least one mandatory garbage collection cycle in following
462 // - clear leading chatty tags
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700463 // - coalesce chatty tags
Mark Salyzyne2827d52015-09-08 09:12:51 -0700464 // - check age-out of preserved logs
465 bool gc = pruneRows <= 1;
466 if (!gc && (worst != (uid_t) -1)) {
Mark Salyzyn5af07be2015-08-19 17:06:11 -0700467 LogBufferIteratorMap::iterator f = mLastWorstUid[id].find(worst);
468 if ((f != mLastWorstUid[id].end())
469 && (f->second != mLogElements.end())) {
470 leading = false;
471 it = f->second;
472 }
473 }
Mark Salyzyn7f62d6b2015-08-24 13:43:27 -0700474 static const timespec too_old = {
475 EXPIRE_HOUR_THRESHOLD * 60 * 60, 0
476 };
477 LogBufferElementCollection::iterator lastt;
478 lastt = mLogElements.end();
479 --lastt;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700480 LogBufferElementLast last;
Mark Salyzyn5af07be2015-08-19 17:06:11 -0700481 while (it != mLogElements.end()) {
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800482 LogBufferElement *e = *it;
483
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800484 if (oldest && (oldest->mStart <= e->getSequence())) {
Mark Salyzyn8cf0d032015-09-16 15:34:00 -0700485 busy = true;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800486 break;
487 }
488
Mark Salyzync89839a2014-02-11 12:29:31 -0800489 if (e->getLogId() != id) {
490 ++it;
491 continue;
492 }
493
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700494 unsigned short dropped = e->getDropped();
Mark Salyzync89839a2014-02-11 12:29:31 -0800495
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700496 // remove any leading drops
497 if (leading && dropped) {
498 it = erase(it);
499 continue;
500 }
501
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700502 if (dropped && last.coalesce(e, dropped)) {
503 it = erase(it, true);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700504 continue;
505 }
506
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700507 if (hasBlacklist && mPrune.naughty(e)) {
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700508 last.clear(e);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700509 it = erase(it);
510 if (dropped) {
511 continue;
512 }
513
514 pruneRows--;
515 if (pruneRows == 0) {
516 break;
517 }
518
519 if (e->getUid() == worst) {
520 kick = true;
521 if (worst_sizes < second_worst_sizes) {
522 break;
523 }
524 worst_sizes -= e->getMsgLen();
525 }
526 continue;
527 }
528
Mark Salyzyn7f62d6b2015-08-24 13:43:27 -0700529 if ((e->getRealTime() < ((*lastt)->getRealTime() - too_old))
530 || (e->getRealTime() > (*lastt)->getRealTime())) {
531 break;
532 }
533
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700534 if (dropped) {
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700535 last.add(e);
Mark Salyzyne2827d52015-09-08 09:12:51 -0700536 if ((!gc && (e->getUid() == worst))
Mark Salyzyn76aab492015-08-24 13:43:27 -0700537 || (mLastWorstUid[id].find(e->getUid())
538 == mLastWorstUid[id].end())) {
539 mLastWorstUid[id][e->getUid()] = it;
540 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800541 ++it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700542 continue;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800543 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700544
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700545 if (e->getUid() != worst) {
Mark Salyzynfd318a82015-06-01 09:41:19 -0700546 leading = false;
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700547 last.clear(e);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700548 ++it;
549 continue;
550 }
551
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700552 pruneRows--;
553 if (pruneRows == 0) {
554 break;
555 }
556
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700557 kick = true;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700558
559 unsigned short len = e->getMsgLen();
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700560
561 // do not create any leading drops
562 if (leading) {
563 it = erase(it);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700564 } else {
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700565 stats.drop(e);
566 e->setDropped(1);
Mark Salyzyn5c5189a2015-09-30 07:40:09 -0700567 if (last.coalesce(e, 1)) {
568 it = erase(it, true);
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700569 } else {
570 last.add(e);
Mark Salyzyne2827d52015-09-08 09:12:51 -0700571 if (!gc || (mLastWorstUid[id].find(worst)
572 == mLastWorstUid[id].end())) {
573 mLastWorstUid[id][worst] = it;
574 }
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700575 ++it;
576 }
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700577 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700578 if (worst_sizes < second_worst_sizes) {
579 break;
580 }
581 worst_sizes -= len;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800582 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700583 last.clear();
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800584
Mark Salyzync402bc72014-04-01 17:19:47 -0700585 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800586 break; // the following loop will ask bad clients to skip/drop
587 }
588 }
589
Mark Salyzync89839a2014-02-11 12:29:31 -0800590 bool whitelist = false;
Mark Salyzyn71541f32015-09-16 15:34:00 -0700591 bool hasWhitelist = mPrune.nice() && !clearAll;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800592 it = mLogElements.begin();
Mark Salyzyn12bac902014-02-26 09:50:16 -0800593 while((pruneRows > 0) && (it != mLogElements.end())) {
594 LogBufferElement *e = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700595
596 if (e->getLogId() != id) {
597 it++;
598 continue;
599 }
600
601 if (oldest && (oldest->mStart <= e->getSequence())) {
Mark Salyzyn8cf0d032015-09-16 15:34:00 -0700602 busy = true;
603
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700604 if (whitelist) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800605 break;
606 }
Mark Salyzync402bc72014-04-01 17:19:47 -0700607
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700608 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
609 // kick a misbehaving log reader client off the island
610 oldest->release_Locked();
611 } else {
612 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzync89839a2014-02-11 12:29:31 -0800613 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700614 break;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800615 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700616
Mark Salyzynd910ca92015-05-21 12:50:31 -0700617 if (hasWhitelist && !e->getDropped() && mPrune.nice(e)) { // WhiteListed
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700618 whitelist = true;
619 it++;
620 continue;
621 }
622
623 it = erase(it);
624 pruneRows--;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800625 }
626
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700627 // Do not save the whitelist if we are reader range limited
Mark Salyzync89839a2014-02-11 12:29:31 -0800628 if (whitelist && (pruneRows > 0)) {
629 it = mLogElements.begin();
630 while((it != mLogElements.end()) && (pruneRows > 0)) {
631 LogBufferElement *e = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700632
633 if (e->getLogId() != id) {
634 ++it;
635 continue;
Mark Salyzync89839a2014-02-11 12:29:31 -0800636 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700637
638 if (oldest && (oldest->mStart <= e->getSequence())) {
Mark Salyzyn8cf0d032015-09-16 15:34:00 -0700639 busy = true;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700640 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
641 // kick a misbehaving log reader client off the island
642 oldest->release_Locked();
643 } else {
644 oldest->triggerSkip_Locked(id, pruneRows);
645 }
646 break;
647 }
648
649 it = erase(it);
650 pruneRows--;
Mark Salyzync89839a2014-02-11 12:29:31 -0800651 }
652 }
Mark Salyzync89839a2014-02-11 12:29:31 -0800653
Mark Salyzyn12bac902014-02-26 09:50:16 -0800654 LogTimeEntry::unlock();
Mark Salyzyn8cf0d032015-09-16 15:34:00 -0700655
656 return (pruneRows > 0) && busy;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800657}
658
659// clear all rows of type "id" from the buffer.
Mark Salyzyn8cf0d032015-09-16 15:34:00 -0700660bool LogBuffer::clear(log_id_t id, uid_t uid) {
661 bool busy = true;
662 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
663 for (int retry = 4;;) {
664 if (retry == 1) { // last pass
665 // Check if it is still busy after the sleep, we say prune
666 // one entry, not another clear run, so we are looking for
667 // the quick side effect of the return value to tell us if
668 // we have a _blocked_ reader.
669 pthread_mutex_lock(&mLogElementsLock);
670 busy = prune(id, 1, uid);
671 pthread_mutex_unlock(&mLogElementsLock);
672 // It is still busy, blocked reader(s), lets kill them all!
673 // otherwise, lets be a good citizen and preserve the slow
674 // readers and let the clear run (below) deal with determining
675 // if we are still blocked and return an error code to caller.
676 if (busy) {
677 LogTimeEntry::lock();
678 LastLogTimes::iterator times = mTimes.begin();
679 while (times != mTimes.end()) {
680 LogTimeEntry *entry = (*times);
681 // Killer punch
682 if (entry->owned_Locked() && entry->isWatching(id)) {
683 entry->release_Locked();
684 }
685 times++;
686 }
687 LogTimeEntry::unlock();
688 }
689 }
690 pthread_mutex_lock(&mLogElementsLock);
691 busy = prune(id, ULONG_MAX, uid);
692 pthread_mutex_unlock(&mLogElementsLock);
693 if (!busy || !--retry) {
694 break;
695 }
696 sleep (1); // Let reader(s) catch up after notification
697 }
698 return busy;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800699}
700
701// get the used space associated with "id".
702unsigned long LogBuffer::getSizeUsed(log_id_t id) {
703 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzynd774bce2014-02-06 14:48:50 -0800704 size_t retval = stats.sizes(id);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800705 pthread_mutex_unlock(&mLogElementsLock);
706 return retval;
707}
708
Mark Salyzync89839a2014-02-11 12:29:31 -0800709// set the total space allocated to "id"
710int LogBuffer::setSize(log_id_t id, unsigned long size) {
711 // Reasonable limits ...
Mark Salyzyn7b85c592014-05-09 17:44:18 -0700712 if (!valid_size(size)) {
Mark Salyzync89839a2014-02-11 12:29:31 -0800713 return -1;
714 }
715 pthread_mutex_lock(&mLogElementsLock);
716 log_buffer_size(id) = size;
717 pthread_mutex_unlock(&mLogElementsLock);
718 return 0;
719}
720
721// get the total space allocated to "id"
722unsigned long LogBuffer::getSize(log_id_t id) {
723 pthread_mutex_lock(&mLogElementsLock);
724 size_t retval = log_buffer_size(id);
725 pthread_mutex_unlock(&mLogElementsLock);
726 return retval;
727}
728
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800729uint64_t LogBuffer::flushTo(
730 SocketClient *reader, const uint64_t start, bool privileged,
731 int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800732 LogBufferElementCollection::iterator it;
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800733 uint64_t max = start;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800734 uid_t uid = reader->getUid();
735
736 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -0600737
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800738 if (start <= 1) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -0600739 // client wants to start from the beginning
740 it = mLogElements.begin();
741 } else {
742 // Client wants to start from some specified time. Chances are
743 // we are better off starting from the end of the time sorted list.
744 for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
745 --it;
746 LogBufferElement *element = *it;
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800747 if (element->getSequence() <= start) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -0600748 it++;
749 break;
750 }
751 }
752 }
753
754 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800755 LogBufferElement *element = *it;
756
757 if (!privileged && (element->getUid() != uid)) {
758 continue;
759 }
760
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800761 if (element->getSequence() <= start) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800762 continue;
763 }
764
765 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800766 if (filter) {
767 int ret = (*filter)(element, arg);
768 if (ret == false) {
769 continue;
770 }
771 if (ret != true) {
772 break;
773 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800774 }
775
776 pthread_mutex_unlock(&mLogElementsLock);
777
778 // range locking in LastLogTimes looks after us
Mark Salyzynbf918322015-04-20 07:26:27 -0700779 max = element->flushTo(reader, this);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800780
781 if (max == element->FLUSH_ERROR) {
782 return max;
783 }
784
785 pthread_mutex_lock(&mLogElementsLock);
786 }
787 pthread_mutex_unlock(&mLogElementsLock);
788
789 return max;
790}
Mark Salyzynd774bce2014-02-06 14:48:50 -0800791
Mark Salyzyn7c528d92015-08-20 10:01:44 -0700792std::string LogBuffer::formatStatistics(uid_t uid, unsigned int logMask) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800793 pthread_mutex_lock(&mLogElementsLock);
794
Mark Salyzyn7c528d92015-08-20 10:01:44 -0700795 std::string ret = stats.format(uid, logMask);
Mark Salyzynd774bce2014-02-06 14:48:50 -0800796
797 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn7c528d92015-08-20 10:01:44 -0700798
799 return ret;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800800}