blob: c2f846ef2b5bd1567acdaa6f3a8e7f835c8d9db9 [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 Salyzyn671e3432014-05-06 07:34:59 -070031#include "LogReader.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080032
Mark Salyzyndfa7a072014-02-11 12:29:31 -080033// Default
Mark Salyzyn0175b072014-02-26 09:50:16 -080034#define LOG_BUFFER_SIZE (256 * 1024) // Tuned on a per-platform basis here?
Mark Salyzyndfa7a072014-02-11 12:29:31 -080035#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn57a0af92014-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 Salyzyn0175b072014-02-26 09:50:16 -080064
Mark Salyzyn671e3432014-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 Salyzyn57a0af92014-05-09 17:44:18 -070088 if (!valid_size(value)) {
89 value = 0;
90 }
91
Mark Salyzyn671e3432014-05-06 07:34:59 -070092 return value;
93}
94
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070095void LogBuffer::init() {
Mark Salyzyn57a0af92014-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 Salyzyn671e3432014-05-06 07:34:59 -0700103
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800104 log_id_for_each(i) {
Mark Salyzyn671e3432014-05-06 07:34:59 -0700105 char key[PROP_NAME_MAX];
Mark Salyzyn671e3432014-05-06 07:34:59 -0700106
Mark Salyzyn57a0af92014-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 Salyzyndfa7a072014-02-11 12:29:31 -0800128 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800129}
130
Mark Salyzyn77187782015-05-12 15:21:31 -0700131LogBuffer::LogBuffer(LastLogTimes *times) : mTimes(*times) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700132 pthread_mutex_init(&mLogElementsLock, NULL);
133
134 init();
135}
136
Mark Salyzyn202e1532015-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 Salyzyn0175b072014-02-26 09:50:16 -0800140 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800141 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800142 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700143
Mark Salyzyn0175b072014-02-26 09:50:16 -0800144 LogBufferElement *elem = new LogBufferElement(log_id, realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700145 uid, pid, tid, msg, len);
Mark Salyzyne59c4692014-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 Salyzyn0175b072014-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 Salyzyneae155e2014-10-13 16:49:47 -0700170 while (last != mLogElements.begin()) {
171 --it;
Mark Salyzync03e72c2014-02-18 11:23:53 -0800172 if ((*it)->getRealTime() <= realtime) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800173 break;
174 }
175 last = it;
176 }
Mark Salyzync03e72c2014-02-18 11:23:53 -0800177
Mark Salyzyn0175b072014-02-26 09:50:16 -0800178 if (last == mLogElements.end()) {
179 mLogElements.push_back(elem);
180 } else {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800181 uint64_t end = 1;
Mark Salyzyn0175b072014-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 Salyzynf7c0f752015-03-03 13:39:37 -0800204 || (end_set && (end >= (*last)->getSequence()))) {
Mark Salyzyn0175b072014-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 Salyzyn97c1c2b2015-03-10 13:51:35 -0700213 stats.add(elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800214 maybePrune(log_id);
215 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn202e1532015-02-09 08:21:05 -0800216
217 return len;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800218}
219
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700220// Prune at most 10% of the log entries or 256, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800221//
222// mLogElementsLock must be held when this function is called.
223void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800224 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700225 unsigned long maxSize = log_buffer_size(id);
226 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700227 size_t sizeOver = sizes - ((maxSize * 9) / 10);
228 size_t elements = stats.elements(id);
229 size_t minElements = elements / 10;
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700230 unsigned long pruneRows = elements * sizeOver / sizes;
231 if (pruneRows <= minElements) {
232 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800233 }
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700234 if (pruneRows > 256) {
235 pruneRows = 256;
236 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800237 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800238 }
239}
240
Mark Salyzyn831aa292015-09-03 16:08:50 -0700241LogBufferElementCollection::iterator LogBuffer::erase(
242 LogBufferElementCollection::iterator it, bool engageStats) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700243 LogBufferElement *e = *it;
Mark Salyzync892ea32015-08-19 17:06:11 -0700244 log_id_t id = e->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700245
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700246 LogBufferIteratorMap::iterator f = mLastWorstUid[id].find(e->getUid());
Mark Salyzync892ea32015-08-19 17:06:11 -0700247 if ((f != mLastWorstUid[id].end()) && (it == f->second)) {
248 mLastWorstUid[id].erase(f);
249 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700250 it = mLogElements.erase(it);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700251 if (engageStats) {
252 stats.subtract(e);
253 } else {
254 stats.erase(e);
255 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700256 delete e;
257
258 return it;
259}
260
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700261// Define a temporary mechanism to report the last LogBufferElement pointer
262// for the specified uid, pid and tid. Used below to help merge-sort when
263// pruning for worst UID.
264class LogBufferElementKey {
265 const union {
266 struct {
267 uint16_t uid;
268 uint16_t pid;
269 uint16_t tid;
270 uint16_t padding;
271 } __packed;
272 uint64_t value;
273 } __packed;
274
275public:
276 LogBufferElementKey(uid_t u, pid_t p, pid_t t):uid(u),pid(p),tid(t),padding(0) { }
277 LogBufferElementKey(uint64_t k):value(k) { }
278
279 uint64_t getKey() { return value; }
280};
281
Mark Salyzyn511338d2015-05-19 09:12:30 -0700282class LogBufferElementLast {
283
284 typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap;
285 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700286
287public:
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700288
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700289 bool merge(LogBufferElement *e, unsigned short dropped) {
290 LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700291 LogBufferElementMap::iterator it = map.find(key.getKey());
292 if (it != map.end()) {
293 LogBufferElement *l = it->second;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700294 unsigned short d = l->getDropped();
295 if ((dropped + d) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700296 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700297 } else {
298 l->setDropped(dropped + d);
299 return true;
300 }
301 }
302 return false;
303 }
304
Mark Salyzyn511338d2015-05-19 09:12:30 -0700305 void add(LogBufferElement *e) {
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700306 LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700307 map[key.getKey()] = e;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700308 }
309
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700310 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700311 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700312 }
313
314 void clear(LogBufferElement *e) {
Mark Salyzyn047cc072015-06-04 13:35:30 -0700315 uint64_t current = e->getRealTime().nsec()
316 - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn511338d2015-05-19 09:12:30 -0700317 for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
318 LogBufferElement *l = it->second;
Mark Salyzyn833a9b12015-05-15 15:58:17 -0700319 if ((l->getDropped() >= EXPIRE_THRESHOLD)
320 && (current > l->getRealTime().nsec())) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700321 it = map.erase(it);
322 } else {
323 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700324 }
325 }
326 }
327
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700328};
329
Mark Salyzyn0175b072014-02-26 09:50:16 -0800330// prune "pruneRows" of type "id" from the buffer.
331//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700332// This garbage collection task is used to expire log entries. It is called to
333// remove all logs (clear), all UID logs (unprivileged clear), or every
334// 256 or 10% of the total logs (whichever is less) to prune the logs.
335//
336// First there is a prep phase where we discover the reader region lock that
337// acts as a backstop to any pruning activity to stop there and go no further.
338//
339// There are three major pruning loops that follow. All expire from the oldest
340// entries. Since there are multiple log buffers, the Android logging facility
341// will appear to drop entries 'in the middle' when looking at multiple log
342// sources and buffers. This effect is slightly more prominent when we prune
343// the worst offender by logging source. Thus the logs slowly loose content
344// and value as you move back in time. This is preferred since chatty sources
345// invariably move the logs value down faster as less chatty sources would be
346// expired in the noise.
347//
348// The first loop performs blacklisting and worst offender pruning. Falling
349// through when there are no notable worst offenders and have not hit the
350// region lock preventing further worst offender pruning. This loop also looks
351// after managing the chatty log entries and merging to help provide
352// statistical basis for blame. The chatty entries are not a notification of
353// how much logs you may have, but instead represent how much logs you would
354// have had in a virtual log buffer that is extended to cover all the in-memory
355// logs without loss. They last much longer than the represented pruned logs
356// since they get multiplied by the gains in the non-chatty log sources.
357//
358// The second loop get complicated because an algorithm of watermarks and
359// history is maintained to reduce the order and keep processing time
360// down to a minimum at scale. These algorithms can be costly in the face
361// of larger log buffers, or severly limited processing time granted to a
362// background task at lowest priority.
363//
364// This second loop does straight-up expiration from the end of the logs
365// (again, remember for the specified log buffer id) but does some whitelist
366// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
367// spam filtration all take priority. This second loop also checks if a region
368// lock is causing us to buffer too much in the logs to help the reader(s),
369// and will tell the slowest reader thread to skip log entries, and if
370// persistent and hits a further threshold, kill the reader thread.
371//
372// The third thread is optional, and only gets hit if there was a whitelist
373// and more needs to be pruned against the backstop of the region lock.
374//
Mark Salyzyn0175b072014-02-26 09:50:16 -0800375// mLogElementsLock must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700376//
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700377void LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800378 LogTimeEntry *oldest = NULL;
379
380 LogTimeEntry::lock();
381
382 // Region locked?
383 LastLogTimes::iterator t = mTimes.begin();
384 while(t != mTimes.end()) {
385 LogTimeEntry *entry = (*t);
TraianX Schiauda6495d2014-12-17 10:53:41 +0200386 if (entry->owned_Locked() && entry->isWatching(id)
Mark Salyzyn0175b072014-02-26 09:50:16 -0800387 && (!oldest || (oldest->mStart > entry->mStart))) {
388 oldest = entry;
389 }
390 t++;
391 }
392
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800393 LogBufferElementCollection::iterator it;
394
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700395 if (caller_uid != AID_ROOT) {
396 for(it = mLogElements.begin(); it != mLogElements.end();) {
397 LogBufferElement *e = *it;
398
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800399 if (oldest && (oldest->mStart <= e->getSequence())) {
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700400 break;
401 }
402
403 if (e->getLogId() != id) {
404 ++it;
405 continue;
406 }
407
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700408 if (e->getUid() == caller_uid) {
409 it = erase(it);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700410 pruneRows--;
411 if (pruneRows == 0) {
412 break;
413 }
414 } else {
415 ++it;
416 }
417 }
418 LogTimeEntry::unlock();
419 return;
420 }
421
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800422 // prune by worst offender by uid
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700423 bool hasBlacklist = mPrune.naughty();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800424 while (pruneRows > 0) {
425 // recalculate the worst offender on every batched pass
426 uid_t worst = (uid_t) -1;
427 size_t worst_sizes = 0;
428 size_t second_worst_sizes = 0;
429
Mark Salyzynae769232015-03-17 17:17:25 -0700430 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn720f6d12015-03-16 08:26:05 -0700431 std::unique_ptr<const UidEntry *[]> sorted = stats.sort(2, id);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700432
Mark Salyzyn720f6d12015-03-16 08:26:05 -0700433 if (sorted.get()) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700434 if (sorted[0] && sorted[1]) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700435 worst_sizes = sorted[0]->getSizes();
Mark Salyzynd717d802015-04-20 15:36:12 -0700436 // Calculate threshold as 12.5% of available storage
437 size_t threshold = log_buffer_size(id) / 8;
438 if (worst_sizes > threshold) {
439 worst = sorted[0]->getKey();
440 second_worst_sizes = sorted[1]->getSizes();
441 if (second_worst_sizes < threshold) {
442 second_worst_sizes = threshold;
443 }
444 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800445 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800446 }
447 }
448
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700449 // skip if we have neither worst nor naughty filters
450 if ((worst == (uid_t) -1) && !hasBlacklist) {
451 break;
452 }
453
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800454 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700455 bool leading = true;
Mark Salyzync892ea32015-08-19 17:06:11 -0700456 it = mLogElements.begin();
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700457 // Perform at least one mandatory garbage collection cycle in following
458 // - clear leading chatty tags
459 // - merge chatty tags
460 // - check age-out of preserved logs
461 bool gc = pruneRows <= 1;
462 if (!gc && (worst != (uid_t) -1)) {
Mark Salyzync892ea32015-08-19 17:06:11 -0700463 LogBufferIteratorMap::iterator f = mLastWorstUid[id].find(worst);
464 if ((f != mLastWorstUid[id].end())
465 && (f->second != mLogElements.end())) {
466 leading = false;
467 it = f->second;
468 }
469 }
Mark Salyzynccfe8442015-08-24 13:43:27 -0700470 static const timespec too_old = {
471 EXPIRE_HOUR_THRESHOLD * 60 * 60, 0
472 };
473 LogBufferElementCollection::iterator lastt;
474 lastt = mLogElements.end();
475 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700476 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700477 while (it != mLogElements.end()) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800478 LogBufferElement *e = *it;
479
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800480 if (oldest && (oldest->mStart <= e->getSequence())) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800481 break;
482 }
483
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800484 if (e->getLogId() != id) {
485 ++it;
486 continue;
487 }
488
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700489 unsigned short dropped = e->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800490
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700491 // remove any leading drops
492 if (leading && dropped) {
493 it = erase(it);
494 continue;
495 }
496
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700497 // merge any drops
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700498 if (dropped && last.merge(e, dropped)) {
Mark Salyzyn831aa292015-09-03 16:08:50 -0700499 it = erase(it, false);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700500 continue;
501 }
502
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700503 if (hasBlacklist && mPrune.naughty(e)) {
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700504 last.clear(e);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700505 it = erase(it);
506 if (dropped) {
507 continue;
508 }
509
510 pruneRows--;
511 if (pruneRows == 0) {
512 break;
513 }
514
515 if (e->getUid() == worst) {
516 kick = true;
517 if (worst_sizes < second_worst_sizes) {
518 break;
519 }
520 worst_sizes -= e->getMsgLen();
521 }
522 continue;
523 }
524
Mark Salyzynccfe8442015-08-24 13:43:27 -0700525 if ((e->getRealTime() < ((*lastt)->getRealTime() - too_old))
526 || (e->getRealTime() > (*lastt)->getRealTime())) {
527 break;
528 }
529
Mark Salyzync892ea32015-08-19 17:06:11 -0700530 // unmerged drop message
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700531 if (dropped) {
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700532 last.add(e);
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700533 if ((!gc && (e->getUid() == worst))
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700534 || (mLastWorstUid[id].find(e->getUid())
535 == mLastWorstUid[id].end())) {
536 mLastWorstUid[id][e->getUid()] = it;
537 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800538 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700539 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800540 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700541
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700542 if (e->getUid() != worst) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700543 leading = false;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700544 last.clear(e);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700545 ++it;
546 continue;
547 }
548
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700549 pruneRows--;
550 if (pruneRows == 0) {
551 break;
552 }
553
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700554 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700555
556 unsigned short len = e->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700557
558 // do not create any leading drops
559 if (leading) {
560 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700561 } else {
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700562 stats.drop(e);
563 e->setDropped(1);
564 if (last.merge(e, 1)) {
Mark Salyzyn831aa292015-09-03 16:08:50 -0700565 it = erase(it, false);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700566 } else {
567 last.add(e);
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700568 if (!gc || (mLastWorstUid[id].find(worst)
569 == mLastWorstUid[id].end())) {
570 mLastWorstUid[id][worst] = it;
571 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700572 ++it;
573 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700574 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700575 if (worst_sizes < second_worst_sizes) {
576 break;
577 }
578 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800579 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700580 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800581
Mark Salyzyn1c950472014-04-01 17:19:47 -0700582 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800583 break; // the following loop will ask bad clients to skip/drop
584 }
585 }
586
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800587 bool whitelist = false;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700588 bool hasWhitelist = mPrune.nice();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800589 it = mLogElements.begin();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800590 while((pruneRows > 0) && (it != mLogElements.end())) {
591 LogBufferElement *e = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700592
593 if (e->getLogId() != id) {
594 it++;
595 continue;
596 }
597
598 if (oldest && (oldest->mStart <= e->getSequence())) {
599 if (whitelist) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800600 break;
601 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700602
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700603 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
604 // kick a misbehaving log reader client off the island
605 oldest->release_Locked();
606 } else {
607 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800608 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700609 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800610 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700611
Mark Salyzync5bf3b82015-05-21 12:50:31 -0700612 if (hasWhitelist && !e->getDropped() && mPrune.nice(e)) { // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700613 whitelist = true;
614 it++;
615 continue;
616 }
617
618 it = erase(it);
619 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800620 }
621
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700622 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800623 if (whitelist && (pruneRows > 0)) {
624 it = mLogElements.begin();
625 while((it != mLogElements.end()) && (pruneRows > 0)) {
626 LogBufferElement *e = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700627
628 if (e->getLogId() != id) {
629 ++it;
630 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800631 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700632
633 if (oldest && (oldest->mStart <= e->getSequence())) {
634 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
635 // kick a misbehaving log reader client off the island
636 oldest->release_Locked();
637 } else {
638 oldest->triggerSkip_Locked(id, pruneRows);
639 }
640 break;
641 }
642
643 it = erase(it);
644 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800645 }
646 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800647
Mark Salyzyn0175b072014-02-26 09:50:16 -0800648 LogTimeEntry::unlock();
649}
650
651// clear all rows of type "id" from the buffer.
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700652void LogBuffer::clear(log_id_t id, uid_t uid) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800653 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700654 prune(id, ULONG_MAX, uid);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800655 pthread_mutex_unlock(&mLogElementsLock);
656}
657
658// get the used space associated with "id".
659unsigned long LogBuffer::getSizeUsed(log_id_t id) {
660 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800661 size_t retval = stats.sizes(id);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800662 pthread_mutex_unlock(&mLogElementsLock);
663 return retval;
664}
665
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800666// set the total space allocated to "id"
667int LogBuffer::setSize(log_id_t id, unsigned long size) {
668 // Reasonable limits ...
Mark Salyzyn57a0af92014-05-09 17:44:18 -0700669 if (!valid_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800670 return -1;
671 }
672 pthread_mutex_lock(&mLogElementsLock);
673 log_buffer_size(id) = size;
674 pthread_mutex_unlock(&mLogElementsLock);
675 return 0;
676}
677
678// get the total space allocated to "id"
679unsigned long LogBuffer::getSize(log_id_t id) {
680 pthread_mutex_lock(&mLogElementsLock);
681 size_t retval = log_buffer_size(id);
682 pthread_mutex_unlock(&mLogElementsLock);
683 return retval;
684}
685
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800686uint64_t LogBuffer::flushTo(
687 SocketClient *reader, const uint64_t start, bool privileged,
688 int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800689 LogBufferElementCollection::iterator it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800690 uint64_t max = start;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800691 uid_t uid = reader->getUid();
692
693 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600694
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800695 if (start <= 1) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600696 // client wants to start from the beginning
697 it = mLogElements.begin();
698 } else {
699 // Client wants to start from some specified time. Chances are
700 // we are better off starting from the end of the time sorted list.
701 for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
702 --it;
703 LogBufferElement *element = *it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800704 if (element->getSequence() <= start) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600705 it++;
706 break;
707 }
708 }
709 }
710
711 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800712 LogBufferElement *element = *it;
713
714 if (!privileged && (element->getUid() != uid)) {
715 continue;
716 }
717
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800718 if (element->getSequence() <= start) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800719 continue;
720 }
721
722 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800723 if (filter) {
724 int ret = (*filter)(element, arg);
725 if (ret == false) {
726 continue;
727 }
728 if (ret != true) {
729 break;
730 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800731 }
732
733 pthread_mutex_unlock(&mLogElementsLock);
734
735 // range locking in LastLogTimes looks after us
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700736 max = element->flushTo(reader, this);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800737
738 if (max == element->FLUSH_ERROR) {
739 return max;
740 }
741
742 pthread_mutex_lock(&mLogElementsLock);
743 }
744 pthread_mutex_unlock(&mLogElementsLock);
745
746 return max;
747}
Mark Salyzyn34facab2014-02-06 14:48:50 -0800748
Mark Salyzyn73160ac2015-08-20 10:01:44 -0700749std::string LogBuffer::formatStatistics(uid_t uid, unsigned int logMask) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800750 pthread_mutex_lock(&mLogElementsLock);
751
Mark Salyzyn73160ac2015-08-20 10:01:44 -0700752 std::string ret = stats.format(uid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800753
754 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn73160ac2015-08-20 10:01:44 -0700755
756 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800757}