blob: 80fc9f5935c39bbbe808e37dfa89390be7d4d526 [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 }
143 LogBufferElement *elem = new LogBufferElement(log_id, realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700144 uid, pid, tid, msg, len);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800145
146 pthread_mutex_lock(&mLogElementsLock);
147
148 // Insert elements in time sorted order if possible
149 // NB: if end is region locked, place element at end of list
150 LogBufferElementCollection::iterator it = mLogElements.end();
151 LogBufferElementCollection::iterator last = it;
Mark Salyzyneae155e2014-10-13 16:49:47 -0700152 while (last != mLogElements.begin()) {
153 --it;
Mark Salyzync03e72c2014-02-18 11:23:53 -0800154 if ((*it)->getRealTime() <= realtime) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800155 break;
156 }
157 last = it;
158 }
Mark Salyzync03e72c2014-02-18 11:23:53 -0800159
Mark Salyzyn0175b072014-02-26 09:50:16 -0800160 if (last == mLogElements.end()) {
161 mLogElements.push_back(elem);
162 } else {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800163 uint64_t end = 1;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800164 bool end_set = false;
165 bool end_always = false;
166
167 LogTimeEntry::lock();
168
169 LastLogTimes::iterator t = mTimes.begin();
170 while(t != mTimes.end()) {
171 LogTimeEntry *entry = (*t);
172 if (entry->owned_Locked()) {
173 if (!entry->mNonBlock) {
174 end_always = true;
175 break;
176 }
177 if (!end_set || (end <= entry->mEnd)) {
178 end = entry->mEnd;
179 end_set = true;
180 }
181 }
182 t++;
183 }
184
185 if (end_always
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800186 || (end_set && (end >= (*last)->getSequence()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800187 mLogElements.push_back(elem);
188 } else {
189 mLogElements.insert(last,elem);
190 }
191
192 LogTimeEntry::unlock();
193 }
194
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700195 stats.add(elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800196 maybePrune(log_id);
197 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn202e1532015-02-09 08:21:05 -0800198
199 return len;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800200}
201
202// If we're using more than 256K of memory for log entries, prune
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800203// at least 10% of the log entries.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800204//
205// mLogElementsLock must be held when this function is called.
206void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800207 size_t sizes = stats.sizes(id);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800208 if (sizes > log_buffer_size(id)) {
209 size_t sizeOver90Percent = sizes - ((log_buffer_size(id) * 9) / 10);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800210 size_t elements = stats.elements(id);
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800211 unsigned long pruneRows = elements * sizeOver90Percent / sizes;
212 elements /= 10;
213 if (pruneRows <= elements) {
214 pruneRows = elements;
215 }
216 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800217 }
218}
219
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700220LogBufferElementCollection::iterator LogBuffer::erase(LogBufferElementCollection::iterator it) {
221 LogBufferElement *e = *it;
222
223 it = mLogElements.erase(it);
224 stats.subtract(e);
225 delete e;
226
227 return it;
228}
229
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700230// Define a temporary mechanism to report the last LogBufferElement pointer
231// for the specified uid, pid and tid. Used below to help merge-sort when
232// pruning for worst UID.
233class LogBufferElementKey {
234 const union {
235 struct {
236 uint16_t uid;
237 uint16_t pid;
238 uint16_t tid;
239 uint16_t padding;
240 } __packed;
241 uint64_t value;
242 } __packed;
243
244public:
245 LogBufferElementKey(uid_t u, pid_t p, pid_t t):uid(u),pid(p),tid(t),padding(0) { }
246 LogBufferElementKey(uint64_t k):value(k) { }
247
248 uint64_t getKey() { return value; }
249};
250
Mark Salyzyn511338d2015-05-19 09:12:30 -0700251class LogBufferElementLast {
252
253 typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap;
254 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700255
256public:
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700257
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700258 bool merge(LogBufferElement *e, unsigned short dropped) {
259 LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700260 LogBufferElementMap::iterator it = map.find(key.getKey());
261 if (it != map.end()) {
262 LogBufferElement *l = it->second;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700263 unsigned short d = l->getDropped();
264 if ((dropped + d) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700265 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700266 } else {
267 l->setDropped(dropped + d);
268 return true;
269 }
270 }
271 return false;
272 }
273
Mark Salyzyn511338d2015-05-19 09:12:30 -0700274 void add(LogBufferElement *e) {
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700275 LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700276 map[key.getKey()] = e;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700277 }
278
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700279 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700280 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700281 }
282
283 void clear(LogBufferElement *e) {
284 uint64_t current = e->getRealTime().nsec() - NS_PER_SEC;
Mark Salyzyn511338d2015-05-19 09:12:30 -0700285 for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
286 LogBufferElement *l = it->second;
Mark Salyzyn833a9b12015-05-15 15:58:17 -0700287 if ((l->getDropped() >= EXPIRE_THRESHOLD)
288 && (current > l->getRealTime().nsec())) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700289 it = map.erase(it);
290 } else {
291 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700292 }
293 }
294 }
295
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700296};
297
Mark Salyzyn0175b072014-02-26 09:50:16 -0800298// prune "pruneRows" of type "id" from the buffer.
299//
300// mLogElementsLock must be held when this function is called.
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700301void LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800302 LogTimeEntry *oldest = NULL;
303
304 LogTimeEntry::lock();
305
306 // Region locked?
307 LastLogTimes::iterator t = mTimes.begin();
308 while(t != mTimes.end()) {
309 LogTimeEntry *entry = (*t);
TraianX Schiauda6495d2014-12-17 10:53:41 +0200310 if (entry->owned_Locked() && entry->isWatching(id)
Mark Salyzyn0175b072014-02-26 09:50:16 -0800311 && (!oldest || (oldest->mStart > entry->mStart))) {
312 oldest = entry;
313 }
314 t++;
315 }
316
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800317 LogBufferElementCollection::iterator it;
318
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700319 if (caller_uid != AID_ROOT) {
320 for(it = mLogElements.begin(); it != mLogElements.end();) {
321 LogBufferElement *e = *it;
322
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800323 if (oldest && (oldest->mStart <= e->getSequence())) {
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700324 break;
325 }
326
327 if (e->getLogId() != id) {
328 ++it;
329 continue;
330 }
331
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700332 if (e->getUid() == caller_uid) {
333 it = erase(it);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700334 pruneRows--;
335 if (pruneRows == 0) {
336 break;
337 }
338 } else {
339 ++it;
340 }
341 }
342 LogTimeEntry::unlock();
343 return;
344 }
345
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800346 // prune by worst offender by uid
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700347 bool hasBlacklist = mPrune.naughty();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800348 while (pruneRows > 0) {
349 // recalculate the worst offender on every batched pass
350 uid_t worst = (uid_t) -1;
351 size_t worst_sizes = 0;
352 size_t second_worst_sizes = 0;
353
Mark Salyzynae769232015-03-17 17:17:25 -0700354 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn720f6d12015-03-16 08:26:05 -0700355 std::unique_ptr<const UidEntry *[]> sorted = stats.sort(2, id);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700356
Mark Salyzyn720f6d12015-03-16 08:26:05 -0700357 if (sorted.get()) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700358 if (sorted[0] && sorted[1]) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700359 worst_sizes = sorted[0]->getSizes();
Mark Salyzynd717d802015-04-20 15:36:12 -0700360 // Calculate threshold as 12.5% of available storage
361 size_t threshold = log_buffer_size(id) / 8;
362 if (worst_sizes > threshold) {
363 worst = sorted[0]->getKey();
364 second_worst_sizes = sorted[1]->getSizes();
365 if (second_worst_sizes < threshold) {
366 second_worst_sizes = threshold;
367 }
368 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800369 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800370 }
371 }
372
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700373 // skip if we have neither worst nor naughty filters
374 if ((worst == (uid_t) -1) && !hasBlacklist) {
375 break;
376 }
377
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800378 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700379 bool leading = true;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700380 LogBufferElementLast last;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800381 for(it = mLogElements.begin(); it != mLogElements.end();) {
382 LogBufferElement *e = *it;
383
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800384 if (oldest && (oldest->mStart <= e->getSequence())) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800385 break;
386 }
387
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800388 if (e->getLogId() != id) {
389 ++it;
390 continue;
391 }
392
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700393 unsigned short dropped = e->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800394
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700395 // remove any leading drops
396 if (leading && dropped) {
397 it = erase(it);
398 continue;
399 }
400
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700401 // merge any drops
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700402 if (dropped && last.merge(e, dropped)) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700403 it = mLogElements.erase(it);
404 stats.erase(e);
405 delete e;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700406 continue;
407 }
408
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700409 if (hasBlacklist && mPrune.naughty(e)) {
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700410 last.clear(e);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700411 it = erase(it);
412 if (dropped) {
413 continue;
414 }
415
416 pruneRows--;
417 if (pruneRows == 0) {
418 break;
419 }
420
421 if (e->getUid() == worst) {
422 kick = true;
423 if (worst_sizes < second_worst_sizes) {
424 break;
425 }
426 worst_sizes -= e->getMsgLen();
427 }
428 continue;
429 }
430
431 if (dropped) {
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700432 last.add(e);
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800433 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700434 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800435 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700436
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700437 if (e->getUid() != worst) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700438 if (leading) {
Mark Salyzyn833a9b12015-05-15 15:58:17 -0700439 static const timespec too_old = {
440 EXPIRE_HOUR_THRESHOLD * 60 * 60, 0
441 };
Mark Salyzyn59212762015-06-01 09:41:19 -0700442 LogBufferElementCollection::iterator last;
443 last = mLogElements.end();
444 --last;
445 if ((e->getRealTime() < ((*last)->getRealTime() - too_old))
446 || (e->getRealTime() > (*last)->getRealTime())) {
447 break;
448 }
Mark Salyzyn833a9b12015-05-15 15:58:17 -0700449 }
Mark Salyzyn59212762015-06-01 09:41:19 -0700450 leading = false;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700451 last.clear(e);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700452 ++it;
453 continue;
454 }
455
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700456 pruneRows--;
457 if (pruneRows == 0) {
458 break;
459 }
460
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700461 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700462
463 unsigned short len = e->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700464
465 // do not create any leading drops
466 if (leading) {
467 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700468 } else {
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700469 stats.drop(e);
470 e->setDropped(1);
471 if (last.merge(e, 1)) {
472 it = mLogElements.erase(it);
473 stats.erase(e);
474 delete e;
475 } else {
476 last.add(e);
477 ++it;
478 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700479 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700480 if (worst_sizes < second_worst_sizes) {
481 break;
482 }
483 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800484 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700485 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800486
Mark Salyzyn1c950472014-04-01 17:19:47 -0700487 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800488 break; // the following loop will ask bad clients to skip/drop
489 }
490 }
491
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800492 bool whitelist = false;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700493 bool hasWhitelist = mPrune.nice();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800494 it = mLogElements.begin();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800495 while((pruneRows > 0) && (it != mLogElements.end())) {
496 LogBufferElement *e = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700497
498 if (e->getLogId() != id) {
499 it++;
500 continue;
501 }
502
503 if (oldest && (oldest->mStart <= e->getSequence())) {
504 if (whitelist) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800505 break;
506 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700507
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700508 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
509 // kick a misbehaving log reader client off the island
510 oldest->release_Locked();
511 } else {
512 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800513 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700514 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800515 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700516
Mark Salyzync5bf3b82015-05-21 12:50:31 -0700517 if (hasWhitelist && !e->getDropped() && mPrune.nice(e)) { // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700518 whitelist = true;
519 it++;
520 continue;
521 }
522
523 it = erase(it);
524 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800525 }
526
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700527 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800528 if (whitelist && (pruneRows > 0)) {
529 it = mLogElements.begin();
530 while((it != mLogElements.end()) && (pruneRows > 0)) {
531 LogBufferElement *e = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700532
533 if (e->getLogId() != id) {
534 ++it;
535 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800536 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700537
538 if (oldest && (oldest->mStart <= e->getSequence())) {
539 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
540 // kick a misbehaving log reader client off the island
541 oldest->release_Locked();
542 } else {
543 oldest->triggerSkip_Locked(id, pruneRows);
544 }
545 break;
546 }
547
548 it = erase(it);
549 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800550 }
551 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800552
Mark Salyzyn0175b072014-02-26 09:50:16 -0800553 LogTimeEntry::unlock();
554}
555
556// clear all rows of type "id" from the buffer.
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700557void LogBuffer::clear(log_id_t id, uid_t uid) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800558 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700559 prune(id, ULONG_MAX, uid);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800560 pthread_mutex_unlock(&mLogElementsLock);
561}
562
563// get the used space associated with "id".
564unsigned long LogBuffer::getSizeUsed(log_id_t id) {
565 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800566 size_t retval = stats.sizes(id);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800567 pthread_mutex_unlock(&mLogElementsLock);
568 return retval;
569}
570
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800571// set the total space allocated to "id"
572int LogBuffer::setSize(log_id_t id, unsigned long size) {
573 // Reasonable limits ...
Mark Salyzyn57a0af92014-05-09 17:44:18 -0700574 if (!valid_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800575 return -1;
576 }
577 pthread_mutex_lock(&mLogElementsLock);
578 log_buffer_size(id) = size;
579 pthread_mutex_unlock(&mLogElementsLock);
580 return 0;
581}
582
583// get the total space allocated to "id"
584unsigned long LogBuffer::getSize(log_id_t id) {
585 pthread_mutex_lock(&mLogElementsLock);
586 size_t retval = log_buffer_size(id);
587 pthread_mutex_unlock(&mLogElementsLock);
588 return retval;
589}
590
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800591uint64_t LogBuffer::flushTo(
592 SocketClient *reader, const uint64_t start, bool privileged,
593 int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800594 LogBufferElementCollection::iterator it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800595 uint64_t max = start;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800596 uid_t uid = reader->getUid();
597
598 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600599
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800600 if (start <= 1) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600601 // client wants to start from the beginning
602 it = mLogElements.begin();
603 } else {
604 // Client wants to start from some specified time. Chances are
605 // we are better off starting from the end of the time sorted list.
606 for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
607 --it;
608 LogBufferElement *element = *it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800609 if (element->getSequence() <= start) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600610 it++;
611 break;
612 }
613 }
614 }
615
616 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800617 LogBufferElement *element = *it;
618
619 if (!privileged && (element->getUid() != uid)) {
620 continue;
621 }
622
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800623 if (element->getSequence() <= start) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800624 continue;
625 }
626
627 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800628 if (filter) {
629 int ret = (*filter)(element, arg);
630 if (ret == false) {
631 continue;
632 }
633 if (ret != true) {
634 break;
635 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800636 }
637
638 pthread_mutex_unlock(&mLogElementsLock);
639
640 // range locking in LastLogTimes looks after us
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700641 max = element->flushTo(reader, this);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800642
643 if (max == element->FLUSH_ERROR) {
644 return max;
645 }
646
647 pthread_mutex_lock(&mLogElementsLock);
648 }
649 pthread_mutex_unlock(&mLogElementsLock);
650
651 return max;
652}
Mark Salyzyn34facab2014-02-06 14:48:50 -0800653
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800654void LogBuffer::formatStatistics(char **strp, uid_t uid, unsigned int logMask) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800655 pthread_mutex_lock(&mLogElementsLock);
656
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700657 stats.format(strp, uid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800658
659 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800660}