blob: 1b829c602241f1918e611c648538819b65e0265c [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 Salyzynb6bee332015-09-08 08:56:32 -070031#include "LogKlog.h"
Mark Salyzyn671e3432014-05-06 07:34:59 -070032#include "LogReader.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080033
Mark Salyzyndfa7a072014-02-11 12:29:31 -080034// Default
Mark Salyzyn0175b072014-02-26 09:50:16 -080035#define LOG_BUFFER_SIZE (256 * 1024) // Tuned on a per-platform basis here?
Mark Salyzyndfa7a072014-02-11 12:29:31 -080036#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn57a0af92014-05-09 17:44:18 -070037#define LOG_BUFFER_MIN_SIZE (64 * 1024UL)
38#define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL)
39
40static bool valid_size(unsigned long value) {
41 if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) {
42 return false;
43 }
44
45 long pages = sysconf(_SC_PHYS_PAGES);
46 if (pages < 1) {
47 return true;
48 }
49
50 long pagesize = sysconf(_SC_PAGESIZE);
51 if (pagesize <= 1) {
52 pagesize = PAGE_SIZE;
53 }
54
55 // maximum memory impact a somewhat arbitrary ~3%
56 pages = (pages + 31) / 32;
57 unsigned long maximum = pages * pagesize;
58
59 if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) {
60 return true;
61 }
62
63 return value <= maximum;
64}
Mark Salyzyn0175b072014-02-26 09:50:16 -080065
Mark Salyzyn671e3432014-05-06 07:34:59 -070066static unsigned long property_get_size(const char *key) {
67 char property[PROPERTY_VALUE_MAX];
68 property_get(key, property, "");
69
70 char *cp;
71 unsigned long value = strtoul(property, &cp, 10);
72
73 switch(*cp) {
74 case 'm':
75 case 'M':
76 value *= 1024;
77 /* FALLTHRU */
78 case 'k':
79 case 'K':
80 value *= 1024;
81 /* FALLTHRU */
82 case '\0':
83 break;
84
85 default:
86 value = 0;
87 }
88
Mark Salyzyn57a0af92014-05-09 17:44:18 -070089 if (!valid_size(value)) {
90 value = 0;
91 }
92
Mark Salyzyn671e3432014-05-06 07:34:59 -070093 return value;
94}
95
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070096void LogBuffer::init() {
Mark Salyzyn57a0af92014-05-09 17:44:18 -070097 static const char global_tuneable[] = "persist.logd.size"; // Settings App
98 static const char global_default[] = "ro.logd.size"; // BoardConfig.mk
99
100 unsigned long default_size = property_get_size(global_tuneable);
101 if (!default_size) {
102 default_size = property_get_size(global_default);
Mark Salyzyncdda62b2015-12-14 13:07:12 -0800103 if (!default_size) {
Mark Salyzyn9c66a582015-12-14 16:40:12 -0800104 default_size = property_get_bool("ro.config.low_ram",
105 BOOL_DEFAULT_FALSE)
106 ? LOG_BUFFER_MIN_SIZE // 64K
107 : LOG_BUFFER_SIZE; // 256K
Mark Salyzyncdda62b2015-12-14 13:07:12 -0800108 }
Mark Salyzyn57a0af92014-05-09 17:44:18 -0700109 }
Mark Salyzyn671e3432014-05-06 07:34:59 -0700110
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800111 log_id_for_each(i) {
Mark Salyzyn671e3432014-05-06 07:34:59 -0700112 char key[PROP_NAME_MAX];
Mark Salyzyn671e3432014-05-06 07:34:59 -0700113
Mark Salyzyn57a0af92014-05-09 17:44:18 -0700114 snprintf(key, sizeof(key), "%s.%s",
115 global_tuneable, android_log_id_to_name(i));
116 unsigned long property_size = property_get_size(key);
117
118 if (!property_size) {
119 snprintf(key, sizeof(key), "%s.%s",
120 global_default, android_log_id_to_name(i));
121 property_size = property_get_size(key);
122 }
123
124 if (!property_size) {
125 property_size = default_size;
126 }
127
128 if (!property_size) {
129 property_size = LOG_BUFFER_SIZE;
130 }
131
132 if (setSize(i, property_size)) {
133 setSize(i, LOG_BUFFER_MIN_SIZE);
134 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800135 }
Mark Salyzynb6bee332015-09-08 08:56:32 -0700136 bool lastMonotonic = monotonic;
Mark Salyzynba7a9a02015-12-01 15:57:25 -0800137 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800138 if (lastMonotonic != monotonic) {
139 //
140 // Fixup all timestamps, may not be 100% accurate, but better than
141 // throwing what we have away when we get 'surprised' by a change.
142 // In-place element fixup so no need to check reader-lock. Entries
143 // should already be in timestamp order, but we could end up with a
144 // few out-of-order entries if new monotonics come in before we
145 // are notified of the reinit change in status. A Typical example would
146 // be:
147 // --------- beginning of system
148 // 10.494082 184 201 D Cryptfs : Just triggered post_fs_data
149 // --------- beginning of kernel
150 // 0.000000 0 0 I : Initializing cgroup subsys
151 // as the act of mounting /data would trigger persist.logd.timestamp to
152 // be corrected. 1/30 corner case YMMV.
153 //
154 pthread_mutex_lock(&mLogElementsLock);
155 LogBufferElementCollection::iterator it = mLogElements.begin();
156 while((it != mLogElements.end())) {
157 LogBufferElement *e = *it;
158 if (monotonic) {
159 if (!android::isMonotonic(e->mRealTime)) {
160 LogKlog::convertRealToMonotonic(e->mRealTime);
161 }
162 } else {
163 if (android::isMonotonic(e->mRealTime)) {
164 LogKlog::convertMonotonicToReal(e->mRealTime);
165 }
166 }
167 ++it;
168 }
169 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzynb6bee332015-09-08 08:56:32 -0700170 }
171
Mark Salyzynb75cce02015-11-30 11:35:56 -0800172 // We may have been triggered by a SIGHUP. Release any sleeping reader
173 // threads to dump their current content.
Mark Salyzynb6bee332015-09-08 08:56:32 -0700174 //
Mark Salyzynb75cce02015-11-30 11:35:56 -0800175 // NB: this is _not_ performed in the context of a SIGHUP, it is
176 // performed during startup, and in context of reinit administrative thread
177 LogTimeEntry::lock();
178
179 LastLogTimes::iterator times = mTimes.begin();
180 while(times != mTimes.end()) {
181 LogTimeEntry *entry = (*times);
182 if (entry->owned_Locked()) {
183 entry->triggerReader_Locked();
Mark Salyzynb6bee332015-09-08 08:56:32 -0700184 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800185 times++;
Mark Salyzynb6bee332015-09-08 08:56:32 -0700186 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800187
188 LogTimeEntry::unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800189}
190
Mark Salyzynb6bee332015-09-08 08:56:32 -0700191LogBuffer::LogBuffer(LastLogTimes *times):
Mark Salyzynba7a9a02015-12-01 15:57:25 -0800192 monotonic(android_log_clockid() == CLOCK_MONOTONIC),
Mark Salyzynb6bee332015-09-08 08:56:32 -0700193 mTimes(*times) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700194 pthread_mutex_init(&mLogElementsLock, NULL);
195
196 init();
197}
198
Mark Salyzyn202e1532015-02-09 08:21:05 -0800199int LogBuffer::log(log_id_t log_id, log_time realtime,
200 uid_t uid, pid_t pid, pid_t tid,
201 const char *msg, unsigned short len) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800202 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800203 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800204 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700205
Mark Salyzyn0175b072014-02-26 09:50:16 -0800206 LogBufferElement *elem = new LogBufferElement(log_id, realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700207 uid, pid, tid, msg, len);
Mark Salyzyn6aa21b22015-12-22 15:50:14 -0800208 if (log_id != LOG_ID_SECURITY) { // whitelist LOG_ID_SECURITY
Mark Salyzyn083b0372015-12-04 10:59:45 -0800209 int prio = ANDROID_LOG_INFO;
Mark Salyzyn6aa21b22015-12-22 15:50:14 -0800210 const char *tag = (const char *)-1;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800211 if (log_id == LOG_ID_EVENTS) {
Mark Salyzyn6aa21b22015-12-22 15:50:14 -0800212 // whitelist "snet_event_log"
213 if (elem->getTag() != SNET_EVENT_LOG_TAG) {
214 tag = android::tagToName(elem->getTag());
215 }
Mark Salyzyn083b0372015-12-04 10:59:45 -0800216 } else {
217 prio = *msg;
218 tag = msg + 1;
219 }
Mark Salyzyn6aa21b22015-12-22 15:50:14 -0800220 if ((tag != (const char *)-1) &&
221 !__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800222 // Log traffic received to total
223 pthread_mutex_lock(&mLogElementsLock);
224 stats.add(elem);
225 stats.subtract(elem);
226 pthread_mutex_unlock(&mLogElementsLock);
227 delete elem;
228 return -EACCES;
229 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700230 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800231
232 pthread_mutex_lock(&mLogElementsLock);
233
234 // Insert elements in time sorted order if possible
235 // NB: if end is region locked, place element at end of list
236 LogBufferElementCollection::iterator it = mLogElements.end();
237 LogBufferElementCollection::iterator last = it;
Mark Salyzyneae155e2014-10-13 16:49:47 -0700238 while (last != mLogElements.begin()) {
239 --it;
Mark Salyzync03e72c2014-02-18 11:23:53 -0800240 if ((*it)->getRealTime() <= realtime) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800241 break;
242 }
243 last = it;
244 }
Mark Salyzync03e72c2014-02-18 11:23:53 -0800245
Mark Salyzyn0175b072014-02-26 09:50:16 -0800246 if (last == mLogElements.end()) {
247 mLogElements.push_back(elem);
248 } else {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800249 uint64_t end = 1;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800250 bool end_set = false;
251 bool end_always = false;
252
253 LogTimeEntry::lock();
254
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700255 LastLogTimes::iterator times = mTimes.begin();
256 while(times != mTimes.end()) {
257 LogTimeEntry *entry = (*times);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800258 if (entry->owned_Locked()) {
259 if (!entry->mNonBlock) {
260 end_always = true;
261 break;
262 }
263 if (!end_set || (end <= entry->mEnd)) {
264 end = entry->mEnd;
265 end_set = true;
266 }
267 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700268 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800269 }
270
271 if (end_always
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800272 || (end_set && (end >= (*last)->getSequence()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800273 mLogElements.push_back(elem);
274 } else {
275 mLogElements.insert(last,elem);
276 }
277
278 LogTimeEntry::unlock();
279 }
280
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700281 stats.add(elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800282 maybePrune(log_id);
283 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn202e1532015-02-09 08:21:05 -0800284
285 return len;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800286}
287
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700288// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800289//
290// mLogElementsLock must be held when this function is called.
291void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800292 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700293 unsigned long maxSize = log_buffer_size(id);
294 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700295 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700296 size_t elements = stats.realElements(id);
297 size_t minElements = elements / 100;
298 if (minElements < minPrune) {
299 minElements = minPrune;
300 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700301 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700302 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700303 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800304 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700305 if (pruneRows > maxPrune) {
306 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700307 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800308 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800309 }
310}
311
Mark Salyzyn831aa292015-09-03 16:08:50 -0700312LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700313 LogBufferElementCollection::iterator it, bool coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700314 LogBufferElement *element = *it;
315 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700316
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700317 { // start of scope for uid found iterator
318 LogBufferIteratorMap::iterator found =
319 mLastWorstUid[id].find(element->getUid());
320 if ((found != mLastWorstUid[id].end())
321 && (it == found->second)) {
322 mLastWorstUid[id].erase(found);
323 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700324 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700325
326 if (element->getUid() == AID_SYSTEM) {
327 // start of scope for pid found iterator
328 LogBufferPidIteratorMap::iterator found =
329 mLastWorstPidOfSystem[id].find(element->getPid());
330 if ((found != mLastWorstPidOfSystem[id].end())
331 && (it == found->second)) {
332 mLastWorstPidOfSystem[id].erase(found);
333 }
334 }
335
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700336 it = mLogElements.erase(it);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700337 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700338 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700339 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700340 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700341 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700342 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700343
344 return it;
345}
346
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700347// Define a temporary mechanism to report the last LogBufferElement pointer
348// for the specified uid, pid and tid. Used below to help merge-sort when
349// pruning for worst UID.
350class LogBufferElementKey {
351 const union {
352 struct {
353 uint16_t uid;
354 uint16_t pid;
355 uint16_t tid;
356 uint16_t padding;
357 } __packed;
358 uint64_t value;
359 } __packed;
360
361public:
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700362 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid):
363 uid(uid),
364 pid(pid),
365 tid(tid),
366 padding(0) {
367 }
368 LogBufferElementKey(uint64_t key):value(key) { }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700369
370 uint64_t getKey() { return value; }
371};
372
Mark Salyzyn511338d2015-05-19 09:12:30 -0700373class LogBufferElementLast {
374
375 typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap;
376 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700377
378public:
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700379
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700380 bool coalesce(LogBufferElement *element, unsigned short dropped) {
381 LogBufferElementKey key(element->getUid(),
382 element->getPid(),
383 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700384 LogBufferElementMap::iterator it = map.find(key.getKey());
385 if (it != map.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700386 LogBufferElement *found = it->second;
387 unsigned short moreDropped = found->getDropped();
388 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700389 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700390 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700391 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700392 return true;
393 }
394 }
395 return false;
396 }
397
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700398 void add(LogBufferElement *element) {
399 LogBufferElementKey key(element->getUid(),
400 element->getPid(),
401 element->getTid());
402 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700403 }
404
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700405 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700406 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700407 }
408
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700409 void clear(LogBufferElement *element) {
410 uint64_t current = element->getRealTime().nsec()
Mark Salyzyn047cc072015-06-04 13:35:30 -0700411 - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn511338d2015-05-19 09:12:30 -0700412 for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700413 LogBufferElement *mapElement = it->second;
414 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD)
415 && (current > mapElement->getRealTime().nsec())) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700416 it = map.erase(it);
417 } else {
418 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700419 }
420 }
421 }
422
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700423};
424
Mark Salyzyn0175b072014-02-26 09:50:16 -0800425// prune "pruneRows" of type "id" from the buffer.
426//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700427// This garbage collection task is used to expire log entries. It is called to
428// remove all logs (clear), all UID logs (unprivileged clear), or every
429// 256 or 10% of the total logs (whichever is less) to prune the logs.
430//
431// First there is a prep phase where we discover the reader region lock that
432// acts as a backstop to any pruning activity to stop there and go no further.
433//
434// There are three major pruning loops that follow. All expire from the oldest
435// entries. Since there are multiple log buffers, the Android logging facility
436// will appear to drop entries 'in the middle' when looking at multiple log
437// sources and buffers. This effect is slightly more prominent when we prune
438// the worst offender by logging source. Thus the logs slowly loose content
439// and value as you move back in time. This is preferred since chatty sources
440// invariably move the logs value down faster as less chatty sources would be
441// expired in the noise.
442//
443// The first loop performs blacklisting and worst offender pruning. Falling
444// through when there are no notable worst offenders and have not hit the
445// region lock preventing further worst offender pruning. This loop also looks
446// after managing the chatty log entries and merging to help provide
447// statistical basis for blame. The chatty entries are not a notification of
448// how much logs you may have, but instead represent how much logs you would
449// have had in a virtual log buffer that is extended to cover all the in-memory
450// logs without loss. They last much longer than the represented pruned logs
451// since they get multiplied by the gains in the non-chatty log sources.
452//
453// The second loop get complicated because an algorithm of watermarks and
454// history is maintained to reduce the order and keep processing time
455// down to a minimum at scale. These algorithms can be costly in the face
456// of larger log buffers, or severly limited processing time granted to a
457// background task at lowest priority.
458//
459// This second loop does straight-up expiration from the end of the logs
460// (again, remember for the specified log buffer id) but does some whitelist
461// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
462// spam filtration all take priority. This second loop also checks if a region
463// lock is causing us to buffer too much in the logs to help the reader(s),
464// and will tell the slowest reader thread to skip log entries, and if
465// persistent and hits a further threshold, kill the reader thread.
466//
467// The third thread is optional, and only gets hit if there was a whitelist
468// and more needs to be pruned against the backstop of the region lock.
469//
Mark Salyzyn0175b072014-02-26 09:50:16 -0800470// mLogElementsLock must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700471//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700472bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800473 LogTimeEntry *oldest = NULL;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700474 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700475 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800476
477 LogTimeEntry::lock();
478
479 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700480 LastLogTimes::iterator times = mTimes.begin();
481 while(times != mTimes.end()) {
482 LogTimeEntry *entry = (*times);
TraianX Schiauda6495d2014-12-17 10:53:41 +0200483 if (entry->owned_Locked() && entry->isWatching(id)
Mark Salyzynb75cce02015-11-30 11:35:56 -0800484 && (!oldest ||
485 (oldest->mStart > entry->mStart) ||
486 ((oldest->mStart == entry->mStart) &&
487 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800488 oldest = entry;
489 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700490 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800491 }
492
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800493 LogBufferElementCollection::iterator it;
494
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700495 if (caller_uid != AID_ROOT) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700496 // Only here if clearAll condition (pruneRows == ULONG_MAX)
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700497 for(it = mLogElements.begin(); it != mLogElements.end();) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700498 LogBufferElement *element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700499
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700500 if ((element->getLogId() != id) || (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700501 ++it;
502 continue;
503 }
504
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700505 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700506 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800507 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
508 oldest->triggerReader_Locked();
509 } else {
510 oldest->triggerSkip_Locked(id, pruneRows);
511 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700512 break;
513 }
514
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700515 it = erase(it);
516 pruneRows--;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700517 }
518 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700519 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700520 }
521
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700522 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn083b0372015-12-04 10:59:45 -0800523 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700524 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800525 // recalculate the worst offender on every batched pass
526 uid_t worst = (uid_t) -1;
527 size_t worst_sizes = 0;
528 size_t second_worst_sizes = 0;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700529 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800530
Mark Salyzynae769232015-03-17 17:17:25 -0700531 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700532 { // begin scope for UID sorted list
533 std::unique_ptr<const UidEntry *[]> sorted = stats.sort(
534 AID_ROOT, (pid_t)0, 2, id);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700535
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700536 if (sorted.get() && sorted[0] && sorted[1]) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700537 worst_sizes = sorted[0]->getSizes();
Mark Salyzynd717d802015-04-20 15:36:12 -0700538 // Calculate threshold as 12.5% of available storage
539 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn50122692015-10-12 13:45:51 -0700540 if ((worst_sizes > threshold)
541 // Allow time horizon to extend roughly tenfold, assume
542 // average entry length is 100 characters.
543 && (worst_sizes > (10 * sorted[0]->getDropped()))) {
Mark Salyzynd717d802015-04-20 15:36:12 -0700544 worst = sorted[0]->getKey();
545 second_worst_sizes = sorted[1]->getSizes();
546 if (second_worst_sizes < threshold) {
547 second_worst_sizes = threshold;
548 }
549 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800550 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800551 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700552
553 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
554 // begin scope of PID sorted list
555 std::unique_ptr<const PidEntry *[]> sorted = stats.sort(
556 worst, (pid_t)0, 2, id, worst);
557 if (sorted.get() && sorted[0] && sorted[1]) {
558 worstPid = sorted[0]->getKey();
559 second_worst_sizes = worst_sizes
560 - sorted[0]->getSizes()
561 + sorted[1]->getSizes();
562 }
563 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800564 }
565
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700566 // skip if we have neither worst nor naughty filters
567 if ((worst == (uid_t) -1) && !hasBlacklist) {
568 break;
569 }
570
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800571 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700572 bool leading = true;
Mark Salyzync892ea32015-08-19 17:06:11 -0700573 it = mLogElements.begin();
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700574 // Perform at least one mandatory garbage collection cycle in following
575 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700576 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700577 // - check age-out of preserved logs
578 bool gc = pruneRows <= 1;
579 if (!gc && (worst != (uid_t) -1)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700580 { // begin scope for uid worst found iterator
581 LogBufferIteratorMap::iterator found = mLastWorstUid[id].find(worst);
582 if ((found != mLastWorstUid[id].end())
583 && (found->second != mLogElements.end())) {
584 leading = false;
585 it = found->second;
586 }
587 }
588 if (worstPid) {
589 // begin scope for pid worst found iterator
590 LogBufferPidIteratorMap::iterator found
591 = mLastWorstPidOfSystem[id].find(worstPid);
592 if ((found != mLastWorstPidOfSystem[id].end())
593 && (found->second != mLogElements.end())) {
594 leading = false;
595 it = found->second;
596 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700597 }
598 }
Mark Salyzynccfe8442015-08-24 13:43:27 -0700599 static const timespec too_old = {
600 EXPIRE_HOUR_THRESHOLD * 60 * 60, 0
601 };
602 LogBufferElementCollection::iterator lastt;
603 lastt = mLogElements.end();
604 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700605 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700606 while (it != mLogElements.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700607 LogBufferElement *element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800608
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700609 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700610 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800611 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
612 oldest->triggerReader_Locked();
613 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800614 break;
615 }
616
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700617 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800618 ++it;
619 continue;
620 }
621
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700622 unsigned short dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800623
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700624 // remove any leading drops
625 if (leading && dropped) {
626 it = erase(it);
627 continue;
628 }
629
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700630 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700631 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700632 continue;
633 }
634
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700635 if (hasBlacklist && mPrune.naughty(element)) {
636 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700637 it = erase(it);
638 if (dropped) {
639 continue;
640 }
641
642 pruneRows--;
643 if (pruneRows == 0) {
644 break;
645 }
646
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700647 if (element->getUid() == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700648 kick = true;
649 if (worst_sizes < second_worst_sizes) {
650 break;
651 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700652 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700653 }
654 continue;
655 }
656
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700657 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old))
658 || (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700659 break;
660 }
661
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700662 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700663 last.add(element);
664 if (worstPid
665 && ((!gc && (element->getPid() == worstPid))
666 || (mLastWorstPidOfSystem[id].find(element->getPid())
667 == mLastWorstPidOfSystem[id].end()))) {
668 mLastWorstPidOfSystem[id][element->getUid()] = it;
669 }
670 if ((!gc && !worstPid && (element->getUid() == worst))
671 || (mLastWorstUid[id].find(element->getUid())
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700672 == mLastWorstUid[id].end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700673 mLastWorstUid[id][element->getUid()] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700674 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800675 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700676 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800677 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700678
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700679 if ((element->getUid() != worst)
680 || (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700681 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700682 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700683 ++it;
684 continue;
685 }
686
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700687 pruneRows--;
688 if (pruneRows == 0) {
689 break;
690 }
691
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700692 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700693
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700694 unsigned short len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700695
696 // do not create any leading drops
697 if (leading) {
698 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700699 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700700 stats.drop(element);
701 element->setDropped(1);
702 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700703 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700704 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700705 last.add(element);
706 if (worstPid && (!gc
707 || (mLastWorstPidOfSystem[id].find(worstPid)
708 == mLastWorstPidOfSystem[id].end()))) {
709 mLastWorstPidOfSystem[id][worstPid] = it;
710 }
711 if ((!gc && !worstPid) || (mLastWorstUid[id].find(worst)
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700712 == mLastWorstUid[id].end())) {
713 mLastWorstUid[id][worst] = it;
714 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700715 ++it;
716 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700717 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700718 if (worst_sizes < second_worst_sizes) {
719 break;
720 }
721 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800722 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700723 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800724
Mark Salyzyn1c950472014-04-01 17:19:47 -0700725 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800726 break; // the following loop will ask bad clients to skip/drop
727 }
728 }
729
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800730 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800731 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800732 it = mLogElements.begin();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800733 while((pruneRows > 0) && (it != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700734 LogBufferElement *element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700735
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700736 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700737 it++;
738 continue;
739 }
740
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700741 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700742 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700743 if (whitelist) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800744 break;
745 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700746
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700747 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
748 // kick a misbehaving log reader client off the island
749 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800750 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
751 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700752 } else {
753 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800754 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700755 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800756 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700757
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700758 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
759 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700760 whitelist = true;
761 it++;
762 continue;
763 }
764
765 it = erase(it);
766 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800767 }
768
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700769 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800770 if (whitelist && (pruneRows > 0)) {
771 it = mLogElements.begin();
772 while((it != mLogElements.end()) && (pruneRows > 0)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700773 LogBufferElement *element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700774
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700775 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700776 ++it;
777 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800778 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700779
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700780 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700781 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700782 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
783 // kick a misbehaving log reader client off the island
784 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800785 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
786 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700787 } else {
788 oldest->triggerSkip_Locked(id, pruneRows);
789 }
790 break;
791 }
792
793 it = erase(it);
794 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800795 }
796 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800797
Mark Salyzyn0175b072014-02-26 09:50:16 -0800798 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700799
800 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800801}
802
803// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -0700804bool LogBuffer::clear(log_id_t id, uid_t uid) {
805 bool busy = true;
806 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
807 for (int retry = 4;;) {
808 if (retry == 1) { // last pass
809 // Check if it is still busy after the sleep, we say prune
810 // one entry, not another clear run, so we are looking for
811 // the quick side effect of the return value to tell us if
812 // we have a _blocked_ reader.
813 pthread_mutex_lock(&mLogElementsLock);
814 busy = prune(id, 1, uid);
815 pthread_mutex_unlock(&mLogElementsLock);
816 // It is still busy, blocked reader(s), lets kill them all!
817 // otherwise, lets be a good citizen and preserve the slow
818 // readers and let the clear run (below) deal with determining
819 // if we are still blocked and return an error code to caller.
820 if (busy) {
821 LogTimeEntry::lock();
822 LastLogTimes::iterator times = mTimes.begin();
823 while (times != mTimes.end()) {
824 LogTimeEntry *entry = (*times);
825 // Killer punch
826 if (entry->owned_Locked() && entry->isWatching(id)) {
827 entry->release_Locked();
828 }
829 times++;
830 }
831 LogTimeEntry::unlock();
832 }
833 }
834 pthread_mutex_lock(&mLogElementsLock);
835 busy = prune(id, ULONG_MAX, uid);
836 pthread_mutex_unlock(&mLogElementsLock);
837 if (!busy || !--retry) {
838 break;
839 }
840 sleep (1); // Let reader(s) catch up after notification
841 }
842 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800843}
844
845// get the used space associated with "id".
846unsigned long LogBuffer::getSizeUsed(log_id_t id) {
847 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800848 size_t retval = stats.sizes(id);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800849 pthread_mutex_unlock(&mLogElementsLock);
850 return retval;
851}
852
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800853// set the total space allocated to "id"
854int LogBuffer::setSize(log_id_t id, unsigned long size) {
855 // Reasonable limits ...
Mark Salyzyn57a0af92014-05-09 17:44:18 -0700856 if (!valid_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800857 return -1;
858 }
859 pthread_mutex_lock(&mLogElementsLock);
860 log_buffer_size(id) = size;
861 pthread_mutex_unlock(&mLogElementsLock);
862 return 0;
863}
864
865// get the total space allocated to "id"
866unsigned long LogBuffer::getSize(log_id_t id) {
867 pthread_mutex_lock(&mLogElementsLock);
868 size_t retval = log_buffer_size(id);
869 pthread_mutex_unlock(&mLogElementsLock);
870 return retval;
871}
872
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800873uint64_t LogBuffer::flushTo(
874 SocketClient *reader, const uint64_t start, bool privileged,
875 int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800876 LogBufferElementCollection::iterator it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800877 uint64_t max = start;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800878 uid_t uid = reader->getUid();
879
880 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600881
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800882 if (start <= 1) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600883 // client wants to start from the beginning
884 it = mLogElements.begin();
885 } else {
886 // Client wants to start from some specified time. Chances are
887 // we are better off starting from the end of the time sorted list.
888 for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
889 --it;
890 LogBufferElement *element = *it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800891 if (element->getSequence() <= start) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600892 it++;
893 break;
894 }
895 }
896 }
897
898 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800899 LogBufferElement *element = *it;
900
901 if (!privileged && (element->getUid() != uid)) {
902 continue;
903 }
904
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800905 if (element->getSequence() <= start) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800906 continue;
907 }
908
909 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800910 if (filter) {
911 int ret = (*filter)(element, arg);
912 if (ret == false) {
913 continue;
914 }
915 if (ret != true) {
916 break;
917 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800918 }
919
920 pthread_mutex_unlock(&mLogElementsLock);
921
922 // range locking in LastLogTimes looks after us
Mark Salyzyn7b873652015-12-03 15:38:35 -0800923 max = element->flushTo(reader, this, privileged);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800924
925 if (max == element->FLUSH_ERROR) {
926 return max;
927 }
928
929 pthread_mutex_lock(&mLogElementsLock);
930 }
931 pthread_mutex_unlock(&mLogElementsLock);
932
933 return max;
934}
Mark Salyzyn34facab2014-02-06 14:48:50 -0800935
Mark Salyzynee3b8382015-12-17 09:58:43 -0800936std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
937 unsigned int logMask) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800938 pthread_mutex_lock(&mLogElementsLock);
939
Mark Salyzynee3b8382015-12-17 09:58:43 -0800940 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800941
942 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn73160ac2015-08-20 10:01:44 -0700943
944 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800945}