blob: c33dca68d403def8028cda706045953ff3869c4d [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 Salyzyn671e3432014-05-06 07:34:59 -070025#include <cutils/properties.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080026#include <log/logger.h>
27
28#include "LogBuffer.h"
Mark Salyzyn671e3432014-05-06 07:34:59 -070029#include "LogReader.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080030
Mark Salyzyndfa7a072014-02-11 12:29:31 -080031// Default
Mark Salyzyn0175b072014-02-26 09:50:16 -080032#define LOG_BUFFER_SIZE (256 * 1024) // Tuned on a per-platform basis here?
Mark Salyzyndfa7a072014-02-11 12:29:31 -080033#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn57a0af92014-05-09 17:44:18 -070034#define LOG_BUFFER_MIN_SIZE (64 * 1024UL)
35#define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL)
36
37static bool valid_size(unsigned long value) {
38 if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) {
39 return false;
40 }
41
42 long pages = sysconf(_SC_PHYS_PAGES);
43 if (pages < 1) {
44 return true;
45 }
46
47 long pagesize = sysconf(_SC_PAGESIZE);
48 if (pagesize <= 1) {
49 pagesize = PAGE_SIZE;
50 }
51
52 // maximum memory impact a somewhat arbitrary ~3%
53 pages = (pages + 31) / 32;
54 unsigned long maximum = pages * pagesize;
55
56 if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) {
57 return true;
58 }
59
60 return value <= maximum;
61}
Mark Salyzyn0175b072014-02-26 09:50:16 -080062
Mark Salyzyn671e3432014-05-06 07:34:59 -070063static unsigned long property_get_size(const char *key) {
64 char property[PROPERTY_VALUE_MAX];
65 property_get(key, property, "");
66
67 char *cp;
68 unsigned long value = strtoul(property, &cp, 10);
69
70 switch(*cp) {
71 case 'm':
72 case 'M':
73 value *= 1024;
74 /* FALLTHRU */
75 case 'k':
76 case 'K':
77 value *= 1024;
78 /* FALLTHRU */
79 case '\0':
80 break;
81
82 default:
83 value = 0;
84 }
85
Mark Salyzyn57a0af92014-05-09 17:44:18 -070086 if (!valid_size(value)) {
87 value = 0;
88 }
89
Mark Salyzyn671e3432014-05-06 07:34:59 -070090 return value;
91}
92
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070093void LogBuffer::init() {
Mark Salyzyn57a0af92014-05-09 17:44:18 -070094 static const char global_tuneable[] = "persist.logd.size"; // Settings App
95 static const char global_default[] = "ro.logd.size"; // BoardConfig.mk
96
97 unsigned long default_size = property_get_size(global_tuneable);
98 if (!default_size) {
99 default_size = property_get_size(global_default);
100 }
Mark Salyzyn671e3432014-05-06 07:34:59 -0700101
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800102 log_id_for_each(i) {
Mark Salyzyn671e3432014-05-06 07:34:59 -0700103 char key[PROP_NAME_MAX];
Mark Salyzyn671e3432014-05-06 07:34:59 -0700104
Mark Salyzyn57a0af92014-05-09 17:44:18 -0700105 snprintf(key, sizeof(key), "%s.%s",
106 global_tuneable, android_log_id_to_name(i));
107 unsigned long property_size = property_get_size(key);
108
109 if (!property_size) {
110 snprintf(key, sizeof(key), "%s.%s",
111 global_default, android_log_id_to_name(i));
112 property_size = property_get_size(key);
113 }
114
115 if (!property_size) {
116 property_size = default_size;
117 }
118
119 if (!property_size) {
120 property_size = LOG_BUFFER_SIZE;
121 }
122
123 if (setSize(i, property_size)) {
124 setSize(i, LOG_BUFFER_MIN_SIZE);
125 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800126 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800127}
128
Mark Salyzyn77187782015-05-12 15:21:31 -0700129LogBuffer::LogBuffer(LastLogTimes *times) : mTimes(*times) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700130 pthread_mutex_init(&mLogElementsLock, NULL);
131
132 init();
133}
134
Mark Salyzyn202e1532015-02-09 08:21:05 -0800135int LogBuffer::log(log_id_t log_id, log_time realtime,
136 uid_t uid, pid_t pid, pid_t tid,
137 const char *msg, unsigned short len) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800138 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800139 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800140 }
141 LogBufferElement *elem = new LogBufferElement(log_id, realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700142 uid, pid, tid, msg, len);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800143
144 pthread_mutex_lock(&mLogElementsLock);
145
146 // Insert elements in time sorted order if possible
147 // NB: if end is region locked, place element at end of list
148 LogBufferElementCollection::iterator it = mLogElements.end();
149 LogBufferElementCollection::iterator last = it;
Mark Salyzyneae155e2014-10-13 16:49:47 -0700150 while (last != mLogElements.begin()) {
151 --it;
Mark Salyzync03e72c2014-02-18 11:23:53 -0800152 if ((*it)->getRealTime() <= realtime) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800153 break;
154 }
155 last = it;
156 }
Mark Salyzync03e72c2014-02-18 11:23:53 -0800157
Mark Salyzyn0175b072014-02-26 09:50:16 -0800158 if (last == mLogElements.end()) {
159 mLogElements.push_back(elem);
160 } else {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800161 uint64_t end = 1;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800162 bool end_set = false;
163 bool end_always = false;
164
165 LogTimeEntry::lock();
166
167 LastLogTimes::iterator t = mTimes.begin();
168 while(t != mTimes.end()) {
169 LogTimeEntry *entry = (*t);
170 if (entry->owned_Locked()) {
171 if (!entry->mNonBlock) {
172 end_always = true;
173 break;
174 }
175 if (!end_set || (end <= entry->mEnd)) {
176 end = entry->mEnd;
177 end_set = true;
178 }
179 }
180 t++;
181 }
182
183 if (end_always
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800184 || (end_set && (end >= (*last)->getSequence()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800185 mLogElements.push_back(elem);
186 } else {
187 mLogElements.insert(last,elem);
188 }
189
190 LogTimeEntry::unlock();
191 }
192
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700193 stats.add(elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800194 maybePrune(log_id);
195 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn202e1532015-02-09 08:21:05 -0800196
197 return len;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800198}
199
200// If we're using more than 256K of memory for log entries, prune
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800201// at least 10% of the log entries.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800202//
203// mLogElementsLock must be held when this function is called.
204void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800205 size_t sizes = stats.sizes(id);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800206 if (sizes > log_buffer_size(id)) {
207 size_t sizeOver90Percent = sizes - ((log_buffer_size(id) * 9) / 10);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800208 size_t elements = stats.elements(id);
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800209 unsigned long pruneRows = elements * sizeOver90Percent / sizes;
210 elements /= 10;
211 if (pruneRows <= elements) {
212 pruneRows = elements;
213 }
214 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800215 }
216}
217
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700218LogBufferElementCollection::iterator LogBuffer::erase(LogBufferElementCollection::iterator it) {
219 LogBufferElement *e = *it;
220
221 it = mLogElements.erase(it);
222 stats.subtract(e);
223 delete e;
224
225 return it;
226}
227
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700228// Define a temporary mechanism to report the last LogBufferElement pointer
229// for the specified uid, pid and tid. Used below to help merge-sort when
230// pruning for worst UID.
231class LogBufferElementKey {
232 const union {
233 struct {
234 uint16_t uid;
235 uint16_t pid;
236 uint16_t tid;
237 uint16_t padding;
238 } __packed;
239 uint64_t value;
240 } __packed;
241
242public:
243 LogBufferElementKey(uid_t u, pid_t p, pid_t t):uid(u),pid(p),tid(t),padding(0) { }
244 LogBufferElementKey(uint64_t k):value(k) { }
245
246 uint64_t getKey() { return value; }
247};
248
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700249class LogBufferElementEntry {
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700250 const uint64_t key;
251 LogBufferElement *last;
252
253public:
254 LogBufferElementEntry(const uint64_t &k, LogBufferElement *e):key(k),last(e) { }
255
256 const uint64_t&getKey() const { return key; }
257
258 LogBufferElement *getLast() { return last; }
259};
260
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700261class LogBufferElementLast : public android::BasicHashtable<uint64_t, LogBufferElementEntry> {
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700262
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700263public:
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700264 bool merge(LogBufferElement *e, unsigned short dropped) {
265 LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid());
266 android::hash_t hash = android::hash_type(key.getKey());
267 ssize_t index = find(-1, hash, key.getKey());
268 if (index != -1) {
269 LogBufferElementEntry &entry = editEntryAt(index);
270 LogBufferElement *l = entry.getLast();
271 unsigned short d = l->getDropped();
272 if ((dropped + d) > USHRT_MAX) {
273 removeAt(index);
274 } else {
275 l->setDropped(dropped + d);
276 return true;
277 }
278 }
279 return false;
280 }
281
282 size_t add(LogBufferElement *e) {
283 LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid());
284 android::hash_t hash = android::hash_type(key.getKey());
285 return android::BasicHashtable<uint64_t, LogBufferElementEntry>::
286 add(hash, LogBufferElementEntry(key.getKey(), e));
287 }
288
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700289 inline void clear() {
290 android::BasicHashtable<uint64_t, LogBufferElementEntry>::clear();
291 }
292
293 void clear(LogBufferElement *e) {
294 uint64_t current = e->getRealTime().nsec() - NS_PER_SEC;
295 ssize_t index = -1;
296 while((index = next(index)) >= 0) {
Mark Salyzyn35173a92015-04-23 07:47:54 -0700297 LogBufferElement *l = editEntryAt(index).getLast();
298 if ((l->getDropped() >= 4) && (current > l->getRealTime().nsec())) {
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700299 removeAt(index);
300 index = -1;
301 }
302 }
303 }
304
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700305};
306
Mark Salyzyn0175b072014-02-26 09:50:16 -0800307// prune "pruneRows" of type "id" from the buffer.
308//
309// mLogElementsLock must be held when this function is called.
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700310void LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800311 LogTimeEntry *oldest = NULL;
312
313 LogTimeEntry::lock();
314
315 // Region locked?
316 LastLogTimes::iterator t = mTimes.begin();
317 while(t != mTimes.end()) {
318 LogTimeEntry *entry = (*t);
TraianX Schiauda6495d2014-12-17 10:53:41 +0200319 if (entry->owned_Locked() && entry->isWatching(id)
Mark Salyzyn0175b072014-02-26 09:50:16 -0800320 && (!oldest || (oldest->mStart > entry->mStart))) {
321 oldest = entry;
322 }
323 t++;
324 }
325
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800326 LogBufferElementCollection::iterator it;
327
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700328 if (caller_uid != AID_ROOT) {
329 for(it = mLogElements.begin(); it != mLogElements.end();) {
330 LogBufferElement *e = *it;
331
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800332 if (oldest && (oldest->mStart <= e->getSequence())) {
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700333 break;
334 }
335
336 if (e->getLogId() != id) {
337 ++it;
338 continue;
339 }
340
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700341 if (e->getUid() == caller_uid) {
342 it = erase(it);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700343 pruneRows--;
344 if (pruneRows == 0) {
345 break;
346 }
347 } else {
348 ++it;
349 }
350 }
351 LogTimeEntry::unlock();
352 return;
353 }
354
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800355 // prune by worst offender by uid
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700356 bool hasBlacklist = mPrune.naughty();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800357 while (pruneRows > 0) {
358 // recalculate the worst offender on every batched pass
359 uid_t worst = (uid_t) -1;
360 size_t worst_sizes = 0;
361 size_t second_worst_sizes = 0;
362
Mark Salyzynae769232015-03-17 17:17:25 -0700363 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn720f6d12015-03-16 08:26:05 -0700364 std::unique_ptr<const UidEntry *[]> sorted = stats.sort(2, id);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700365
Mark Salyzyn720f6d12015-03-16 08:26:05 -0700366 if (sorted.get()) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700367 if (sorted[0] && sorted[1]) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700368 worst_sizes = sorted[0]->getSizes();
Mark Salyzynd717d802015-04-20 15:36:12 -0700369 // Calculate threshold as 12.5% of available storage
370 size_t threshold = log_buffer_size(id) / 8;
371 if (worst_sizes > threshold) {
372 worst = sorted[0]->getKey();
373 second_worst_sizes = sorted[1]->getSizes();
374 if (second_worst_sizes < threshold) {
375 second_worst_sizes = threshold;
376 }
377 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800378 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800379 }
380 }
381
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700382 // skip if we have neither worst nor naughty filters
383 if ((worst == (uid_t) -1) && !hasBlacklist) {
384 break;
385 }
386
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800387 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700388 bool leading = true;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700389 LogBufferElementLast last;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800390 for(it = mLogElements.begin(); it != mLogElements.end();) {
391 LogBufferElement *e = *it;
392
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800393 if (oldest && (oldest->mStart <= e->getSequence())) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800394 break;
395 }
396
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800397 if (e->getLogId() != id) {
398 ++it;
399 continue;
400 }
401
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700402 unsigned short dropped = e->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800403
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700404 // remove any leading drops
405 if (leading && dropped) {
406 it = erase(it);
407 continue;
408 }
409
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700410 // merge any drops
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700411 if (dropped && last.merge(e, dropped)) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700412 it = mLogElements.erase(it);
413 stats.erase(e);
414 delete e;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700415 continue;
416 }
417
418 leading = false;
419
420 if (hasBlacklist && mPrune.naughty(e)) {
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700421 last.clear(e);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700422 it = erase(it);
423 if (dropped) {
424 continue;
425 }
426
427 pruneRows--;
428 if (pruneRows == 0) {
429 break;
430 }
431
432 if (e->getUid() == worst) {
433 kick = true;
434 if (worst_sizes < second_worst_sizes) {
435 break;
436 }
437 worst_sizes -= e->getMsgLen();
438 }
439 continue;
440 }
441
442 if (dropped) {
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700443 last.add(e);
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800444 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700445 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800446 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700447
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700448 if (e->getUid() != worst) {
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700449 last.clear(e);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700450 ++it;
451 continue;
452 }
453
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700454 pruneRows--;
455 if (pruneRows == 0) {
456 break;
457 }
458
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700459 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700460
461 unsigned short len = e->getMsgLen();
462 stats.drop(e);
463 e->setDropped(1);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700464 if (last.merge(e, 1)) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700465 it = mLogElements.erase(it);
466 stats.erase(e);
467 delete e;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700468 } else {
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700469 last.add(e);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700470 ++it;
471 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700472 if (worst_sizes < second_worst_sizes) {
473 break;
474 }
475 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800476 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700477 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800478
Mark Salyzyn1c950472014-04-01 17:19:47 -0700479 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800480 break; // the following loop will ask bad clients to skip/drop
481 }
482 }
483
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800484 bool whitelist = false;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700485 bool hasWhitelist = mPrune.nice();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800486 it = mLogElements.begin();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800487 while((pruneRows > 0) && (it != mLogElements.end())) {
488 LogBufferElement *e = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700489
490 if (e->getLogId() != id) {
491 it++;
492 continue;
493 }
494
495 if (oldest && (oldest->mStart <= e->getSequence())) {
496 if (whitelist) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800497 break;
498 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700499
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700500 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
501 // kick a misbehaving log reader client off the island
502 oldest->release_Locked();
503 } else {
504 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800505 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700506 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800507 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700508
509 if (hasWhitelist && mPrune.nice(e)) { // WhiteListed
510 whitelist = true;
511 it++;
512 continue;
513 }
514
515 it = erase(it);
516 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800517 }
518
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700519 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800520 if (whitelist && (pruneRows > 0)) {
521 it = mLogElements.begin();
522 while((it != mLogElements.end()) && (pruneRows > 0)) {
523 LogBufferElement *e = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700524
525 if (e->getLogId() != id) {
526 ++it;
527 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800528 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700529
530 if (oldest && (oldest->mStart <= e->getSequence())) {
531 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
532 // kick a misbehaving log reader client off the island
533 oldest->release_Locked();
534 } else {
535 oldest->triggerSkip_Locked(id, pruneRows);
536 }
537 break;
538 }
539
540 it = erase(it);
541 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800542 }
543 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800544
Mark Salyzyn0175b072014-02-26 09:50:16 -0800545 LogTimeEntry::unlock();
546}
547
548// clear all rows of type "id" from the buffer.
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700549void LogBuffer::clear(log_id_t id, uid_t uid) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800550 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700551 prune(id, ULONG_MAX, uid);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800552 pthread_mutex_unlock(&mLogElementsLock);
553}
554
555// get the used space associated with "id".
556unsigned long LogBuffer::getSizeUsed(log_id_t id) {
557 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800558 size_t retval = stats.sizes(id);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800559 pthread_mutex_unlock(&mLogElementsLock);
560 return retval;
561}
562
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800563// set the total space allocated to "id"
564int LogBuffer::setSize(log_id_t id, unsigned long size) {
565 // Reasonable limits ...
Mark Salyzyn57a0af92014-05-09 17:44:18 -0700566 if (!valid_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800567 return -1;
568 }
569 pthread_mutex_lock(&mLogElementsLock);
570 log_buffer_size(id) = size;
571 pthread_mutex_unlock(&mLogElementsLock);
572 return 0;
573}
574
575// get the total space allocated to "id"
576unsigned long LogBuffer::getSize(log_id_t id) {
577 pthread_mutex_lock(&mLogElementsLock);
578 size_t retval = log_buffer_size(id);
579 pthread_mutex_unlock(&mLogElementsLock);
580 return retval;
581}
582
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800583uint64_t LogBuffer::flushTo(
584 SocketClient *reader, const uint64_t start, bool privileged,
585 int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800586 LogBufferElementCollection::iterator it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800587 uint64_t max = start;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800588 uid_t uid = reader->getUid();
589
590 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600591
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800592 if (start <= 1) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600593 // client wants to start from the beginning
594 it = mLogElements.begin();
595 } else {
596 // Client wants to start from some specified time. Chances are
597 // we are better off starting from the end of the time sorted list.
598 for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
599 --it;
600 LogBufferElement *element = *it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800601 if (element->getSequence() <= start) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600602 it++;
603 break;
604 }
605 }
606 }
607
608 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800609 LogBufferElement *element = *it;
610
611 if (!privileged && (element->getUid() != uid)) {
612 continue;
613 }
614
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800615 if (element->getSequence() <= start) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800616 continue;
617 }
618
619 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800620 if (filter) {
621 int ret = (*filter)(element, arg);
622 if (ret == false) {
623 continue;
624 }
625 if (ret != true) {
626 break;
627 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800628 }
629
630 pthread_mutex_unlock(&mLogElementsLock);
631
632 // range locking in LastLogTimes looks after us
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700633 max = element->flushTo(reader, this);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800634
635 if (max == element->FLUSH_ERROR) {
636 return max;
637 }
638
639 pthread_mutex_lock(&mLogElementsLock);
640 }
641 pthread_mutex_unlock(&mLogElementsLock);
642
643 return max;
644}
Mark Salyzyn34facab2014-02-06 14:48:50 -0800645
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800646void LogBuffer::formatStatistics(char **strp, uid_t uid, unsigned int logMask) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800647 pthread_mutex_lock(&mLogElementsLock);
648
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700649 stats.format(strp, uid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800650
651 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800652}