blob: 6ea41097c967ca99484bc7a498f456f834c1ea26 [file] [log] [blame]
Mark Salyzyn12bac902014-02-26 09:50:16 -08001/*
2 * Copyright (C) 2012-2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070017#include <ctype.h>
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -080018#include <errno.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080019#include <stdio.h>
20#include <string.h>
Mark Salyzyn7b85c592014-05-09 17:44:18 -070021#include <sys/user.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080022#include <time.h>
23#include <unistd.h>
24
Mark Salyzyn6a404192015-05-19 09:12:30 -070025#include <unordered_map>
26
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070027#include <cutils/properties.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080028#include <log/logger.h>
29
30#include "LogBuffer.h"
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070031#include "LogReader.h"
Mark Salyzyn12bac902014-02-26 09:50:16 -080032
Mark Salyzync89839a2014-02-11 12:29:31 -080033// Default
Mark Salyzyn12bac902014-02-26 09:50:16 -080034#define LOG_BUFFER_SIZE (256 * 1024) // Tuned on a per-platform basis here?
Mark Salyzync89839a2014-02-11 12:29:31 -080035#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn7b85c592014-05-09 17:44:18 -070036#define LOG_BUFFER_MIN_SIZE (64 * 1024UL)
37#define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL)
38
39static bool valid_size(unsigned long value) {
40 if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) {
41 return false;
42 }
43
44 long pages = sysconf(_SC_PHYS_PAGES);
45 if (pages < 1) {
46 return true;
47 }
48
49 long pagesize = sysconf(_SC_PAGESIZE);
50 if (pagesize <= 1) {
51 pagesize = PAGE_SIZE;
52 }
53
54 // maximum memory impact a somewhat arbitrary ~3%
55 pages = (pages + 31) / 32;
56 unsigned long maximum = pages * pagesize;
57
58 if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) {
59 return true;
60 }
61
62 return value <= maximum;
63}
Mark Salyzyn12bac902014-02-26 09:50:16 -080064
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070065static unsigned long property_get_size(const char *key) {
66 char property[PROPERTY_VALUE_MAX];
67 property_get(key, property, "");
68
69 char *cp;
70 unsigned long value = strtoul(property, &cp, 10);
71
72 switch(*cp) {
73 case 'm':
74 case 'M':
75 value *= 1024;
76 /* FALLTHRU */
77 case 'k':
78 case 'K':
79 value *= 1024;
80 /* FALLTHRU */
81 case '\0':
82 break;
83
84 default:
85 value = 0;
86 }
87
Mark Salyzyn7b85c592014-05-09 17:44:18 -070088 if (!valid_size(value)) {
89 value = 0;
90 }
91
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070092 return value;
93}
94
Mark Salyzyn3fe25932015-03-10 16:45:17 -070095void LogBuffer::init() {
Mark Salyzyn7b85c592014-05-09 17:44:18 -070096 static const char global_tuneable[] = "persist.logd.size"; // Settings App
97 static const char global_default[] = "ro.logd.size"; // BoardConfig.mk
98
99 unsigned long default_size = property_get_size(global_tuneable);
100 if (!default_size) {
101 default_size = property_get_size(global_default);
102 }
Mark Salyzyn4e756fb2014-05-06 07:34:59 -0700103
Mark Salyzync89839a2014-02-11 12:29:31 -0800104 log_id_for_each(i) {
Mark Salyzyn4e756fb2014-05-06 07:34:59 -0700105 char key[PROP_NAME_MAX];
Mark Salyzyn4e756fb2014-05-06 07:34:59 -0700106
Mark Salyzyn7b85c592014-05-09 17:44:18 -0700107 snprintf(key, sizeof(key), "%s.%s",
108 global_tuneable, android_log_id_to_name(i));
109 unsigned long property_size = property_get_size(key);
110
111 if (!property_size) {
112 snprintf(key, sizeof(key), "%s.%s",
113 global_default, android_log_id_to_name(i));
114 property_size = property_get_size(key);
115 }
116
117 if (!property_size) {
118 property_size = default_size;
119 }
120
121 if (!property_size) {
122 property_size = LOG_BUFFER_SIZE;
123 }
124
125 if (setSize(i, property_size)) {
126 setSize(i, LOG_BUFFER_MIN_SIZE);
127 }
Mark Salyzync89839a2014-02-11 12:29:31 -0800128 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800129}
130
Mark Salyzyncd766f92015-05-12 15:21:31 -0700131LogBuffer::LogBuffer(LastLogTimes *times) : mTimes(*times) {
Mark Salyzyn3fe25932015-03-10 16:45:17 -0700132 pthread_mutex_init(&mLogElementsLock, NULL);
133
134 init();
135}
136
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800137int LogBuffer::log(log_id_t log_id, log_time realtime,
138 uid_t uid, pid_t pid, pid_t tid,
139 const char *msg, unsigned short len) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800140 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800141 return -EINVAL;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800142 }
Mark Salyzyn19c91112014-10-02 13:07:05 -0700143
Mark Salyzyn12bac902014-02-26 09:50:16 -0800144 LogBufferElement *elem = new LogBufferElement(log_id, realtime,
Mark Salyzyn3661dd02014-03-20 16:09:38 -0700145 uid, pid, tid, msg, len);
Mark Salyzyn19c91112014-10-02 13:07:05 -0700146 int prio = ANDROID_LOG_INFO;
147 const char *tag = NULL;
148 if (log_id == LOG_ID_EVENTS) {
149 tag = android::tagToName(elem->getTag());
150 } else {
151 prio = *msg;
152 tag = msg + 1;
153 }
154 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
155 // Log traffic received to total
156 pthread_mutex_lock(&mLogElementsLock);
157 stats.add(elem);
158 stats.subtract(elem);
159 pthread_mutex_unlock(&mLogElementsLock);
160 delete elem;
161 return -EACCES;
162 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800163
164 pthread_mutex_lock(&mLogElementsLock);
165
166 // Insert elements in time sorted order if possible
167 // NB: if end is region locked, place element at end of list
168 LogBufferElementCollection::iterator it = mLogElements.end();
169 LogBufferElementCollection::iterator last = it;
Mark Salyzyna222a772014-10-13 16:49:47 -0700170 while (last != mLogElements.begin()) {
171 --it;
Mark Salyzyn5b7a8d82014-02-18 11:23:53 -0800172 if ((*it)->getRealTime() <= realtime) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800173 break;
174 }
175 last = it;
176 }
Mark Salyzyn5b7a8d82014-02-18 11:23:53 -0800177
Mark Salyzyn12bac902014-02-26 09:50:16 -0800178 if (last == mLogElements.end()) {
179 mLogElements.push_back(elem);
180 } else {
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800181 uint64_t end = 1;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800182 bool end_set = false;
183 bool end_always = false;
184
185 LogTimeEntry::lock();
186
187 LastLogTimes::iterator t = mTimes.begin();
188 while(t != mTimes.end()) {
189 LogTimeEntry *entry = (*t);
190 if (entry->owned_Locked()) {
191 if (!entry->mNonBlock) {
192 end_always = true;
193 break;
194 }
195 if (!end_set || (end <= entry->mEnd)) {
196 end = entry->mEnd;
197 end_set = true;
198 }
199 }
200 t++;
201 }
202
203 if (end_always
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800204 || (end_set && (end >= (*last)->getSequence()))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800205 mLogElements.push_back(elem);
206 } else {
207 mLogElements.insert(last,elem);
208 }
209
210 LogTimeEntry::unlock();
211 }
212
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700213 stats.add(elem);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800214 maybePrune(log_id);
215 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800216
217 return len;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800218}
219
Mark Salyzyn63745722015-08-19 12:20:36 -0700220// Prune at most 10% of the log entries or 256, whichever is less.
Mark Salyzyn12bac902014-02-26 09:50:16 -0800221//
222// mLogElementsLock must be held when this function is called.
223void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800224 size_t sizes = stats.sizes(id);
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700225 unsigned long maxSize = log_buffer_size(id);
226 if (sizes > maxSize) {
Mark Salyzyn63745722015-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 Salyzyn4436d4c2015-08-10 10:23:56 -0700230 unsigned long pruneRows = elements * sizeOver / sizes;
231 if (pruneRows <= minElements) {
232 pruneRows = minElements;
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800233 }
Mark Salyzyn63745722015-08-19 12:20:36 -0700234 if (pruneRows > 256) {
235 pruneRows = 256;
236 }
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800237 prune(id, pruneRows);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800238 }
239}
240
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700241LogBufferElementCollection::iterator LogBuffer::erase(LogBufferElementCollection::iterator it) {
242 LogBufferElement *e = *it;
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700243 log_id_t id = e->getLogId();
244 LogBufferIteratorMap::iterator f = mLastWorstUid[id].find(e->getUid());
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700245
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700246 if ((f != mLastWorstUid[id].end()) && (it == f->second)) {
247 mLastWorstUid[id].erase(f);
248 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700249 it = mLogElements.erase(it);
250 stats.subtract(e);
251 delete e;
252
253 return it;
254}
255
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700256// Define a temporary mechanism to report the last LogBufferElement pointer
257// for the specified uid, pid and tid. Used below to help merge-sort when
258// pruning for worst UID.
259class LogBufferElementKey {
260 const union {
261 struct {
262 uint16_t uid;
263 uint16_t pid;
264 uint16_t tid;
265 uint16_t padding;
266 } __packed;
267 uint64_t value;
268 } __packed;
269
270public:
271 LogBufferElementKey(uid_t u, pid_t p, pid_t t):uid(u),pid(p),tid(t),padding(0) { }
272 LogBufferElementKey(uint64_t k):value(k) { }
273
274 uint64_t getKey() { return value; }
275};
276
Mark Salyzyn6a404192015-05-19 09:12:30 -0700277class LogBufferElementLast {
278
279 typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap;
280 LogBufferElementMap map;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700281
282public:
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700283
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700284 bool merge(LogBufferElement *e, unsigned short dropped) {
285 LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid());
Mark Salyzyn6a404192015-05-19 09:12:30 -0700286 LogBufferElementMap::iterator it = map.find(key.getKey());
287 if (it != map.end()) {
288 LogBufferElement *l = it->second;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700289 unsigned short d = l->getDropped();
290 if ((dropped + d) > USHRT_MAX) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700291 map.erase(it);
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700292 } else {
293 l->setDropped(dropped + d);
294 return true;
295 }
296 }
297 return false;
298 }
299
Mark Salyzyn6a404192015-05-19 09:12:30 -0700300 void add(LogBufferElement *e) {
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700301 LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid());
Mark Salyzyn6a404192015-05-19 09:12:30 -0700302 map[key.getKey()] = e;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700303 }
304
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700305 inline void clear() {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700306 map.clear();
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700307 }
308
309 void clear(LogBufferElement *e) {
Mark Salyzyn80c1fc62015-06-04 13:35:30 -0700310 uint64_t current = e->getRealTime().nsec()
311 - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn6a404192015-05-19 09:12:30 -0700312 for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
313 LogBufferElement *l = it->second;
Mark Salyzyn4f199f32015-05-15 15:58:17 -0700314 if ((l->getDropped() >= EXPIRE_THRESHOLD)
315 && (current > l->getRealTime().nsec())) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700316 it = map.erase(it);
317 } else {
318 ++it;
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700319 }
320 }
321 }
322
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700323};
324
Mark Salyzyn12bac902014-02-26 09:50:16 -0800325// prune "pruneRows" of type "id" from the buffer.
326//
327// mLogElementsLock must be held when this function is called.
Mark Salyzyn276d5352014-06-12 11:16:16 -0700328void LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800329 LogTimeEntry *oldest = NULL;
330
331 LogTimeEntry::lock();
332
333 // Region locked?
334 LastLogTimes::iterator t = mTimes.begin();
335 while(t != mTimes.end()) {
336 LogTimeEntry *entry = (*t);
TraianX Schiau6ba427e2014-12-17 10:53:41 +0200337 if (entry->owned_Locked() && entry->isWatching(id)
Mark Salyzyn12bac902014-02-26 09:50:16 -0800338 && (!oldest || (oldest->mStart > entry->mStart))) {
339 oldest = entry;
340 }
341 t++;
342 }
343
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800344 LogBufferElementCollection::iterator it;
345
Mark Salyzyn276d5352014-06-12 11:16:16 -0700346 if (caller_uid != AID_ROOT) {
347 for(it = mLogElements.begin(); it != mLogElements.end();) {
348 LogBufferElement *e = *it;
349
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800350 if (oldest && (oldest->mStart <= e->getSequence())) {
Mark Salyzyn276d5352014-06-12 11:16:16 -0700351 break;
352 }
353
354 if (e->getLogId() != id) {
355 ++it;
356 continue;
357 }
358
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700359 if (e->getUid() == caller_uid) {
360 it = erase(it);
Mark Salyzyn276d5352014-06-12 11:16:16 -0700361 pruneRows--;
362 if (pruneRows == 0) {
363 break;
364 }
365 } else {
366 ++it;
367 }
368 }
369 LogTimeEntry::unlock();
370 return;
371 }
372
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800373 // prune by worst offender by uid
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700374 bool hasBlacklist = mPrune.naughty();
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800375 while (pruneRows > 0) {
376 // recalculate the worst offender on every batched pass
377 uid_t worst = (uid_t) -1;
378 size_t worst_sizes = 0;
379 size_t second_worst_sizes = 0;
380
Mark Salyzyn0e765c62015-03-17 17:17:25 -0700381 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn1435b472015-03-16 08:26:05 -0700382 std::unique_ptr<const UidEntry *[]> sorted = stats.sort(2, id);
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700383
Mark Salyzyn1435b472015-03-16 08:26:05 -0700384 if (sorted.get()) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700385 if (sorted[0] && sorted[1]) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700386 worst_sizes = sorted[0]->getSizes();
Mark Salyzyn84a83982015-04-20 15:36:12 -0700387 // Calculate threshold as 12.5% of available storage
388 size_t threshold = log_buffer_size(id) / 8;
389 if (worst_sizes > threshold) {
390 worst = sorted[0]->getKey();
391 second_worst_sizes = sorted[1]->getSizes();
392 if (second_worst_sizes < threshold) {
393 second_worst_sizes = threshold;
394 }
395 }
Mark Salyzync89839a2014-02-11 12:29:31 -0800396 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800397 }
398 }
399
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700400 // skip if we have neither worst nor naughty filters
401 if ((worst == (uid_t) -1) && !hasBlacklist) {
402 break;
403 }
404
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800405 bool kick = false;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700406 bool leading = true;
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700407 it = mLogElements.begin();
408 if (worst != (uid_t) -1) {
409 LogBufferIteratorMap::iterator f = mLastWorstUid[id].find(worst);
410 if ((f != mLastWorstUid[id].end())
411 && (f->second != mLogElements.end())) {
412 leading = false;
413 it = f->second;
414 }
415 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700416 LogBufferElementLast last;
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700417 while (it != mLogElements.end()) {
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800418 LogBufferElement *e = *it;
419
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800420 if (oldest && (oldest->mStart <= e->getSequence())) {
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800421 break;
422 }
423
Mark Salyzync89839a2014-02-11 12:29:31 -0800424 if (e->getLogId() != id) {
425 ++it;
426 continue;
427 }
428
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700429 unsigned short dropped = e->getDropped();
Mark Salyzync89839a2014-02-11 12:29:31 -0800430
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700431 // remove any leading drops
432 if (leading && dropped) {
433 it = erase(it);
434 continue;
435 }
436
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700437 // merge any drops
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700438 if (dropped && last.merge(e, dropped)) {
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700439 it = mLogElements.erase(it);
440 stats.erase(e);
441 delete e;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700442 continue;
443 }
444
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700445 if (hasBlacklist && mPrune.naughty(e)) {
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700446 last.clear(e);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700447 it = erase(it);
448 if (dropped) {
449 continue;
450 }
451
452 pruneRows--;
453 if (pruneRows == 0) {
454 break;
455 }
456
457 if (e->getUid() == worst) {
458 kick = true;
459 if (worst_sizes < second_worst_sizes) {
460 break;
461 }
462 worst_sizes -= e->getMsgLen();
463 }
464 continue;
465 }
466
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700467 // unmerged drop message
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700468 if (dropped) {
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700469 last.add(e);
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700470 mLastWorstUid[id][e->getUid()] = it;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800471 ++it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700472 continue;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800473 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700474
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700475 if (e->getUid() != worst) {
Mark Salyzynfd318a82015-06-01 09:41:19 -0700476 if (leading) {
Mark Salyzyn4f199f32015-05-15 15:58:17 -0700477 static const timespec too_old = {
478 EXPIRE_HOUR_THRESHOLD * 60 * 60, 0
479 };
Mark Salyzynfd318a82015-06-01 09:41:19 -0700480 LogBufferElementCollection::iterator last;
481 last = mLogElements.end();
482 --last;
483 if ((e->getRealTime() < ((*last)->getRealTime() - too_old))
484 || (e->getRealTime() > (*last)->getRealTime())) {
485 break;
486 }
Mark Salyzyn4f199f32015-05-15 15:58:17 -0700487 }
Mark Salyzynfd318a82015-06-01 09:41:19 -0700488 leading = false;
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700489 last.clear(e);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700490 ++it;
491 continue;
492 }
493
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700494 pruneRows--;
495 if (pruneRows == 0) {
496 break;
497 }
498
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700499 kick = true;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700500
501 unsigned short len = e->getMsgLen();
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700502
503 // do not create any leading drops
504 if (leading) {
505 it = erase(it);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700506 } else {
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700507 stats.drop(e);
508 e->setDropped(1);
509 if (last.merge(e, 1)) {
510 it = mLogElements.erase(it);
511 stats.erase(e);
512 delete e;
513 } else {
514 last.add(e);
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700515 mLastWorstUid[id][e->getUid()] = it;
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700516 ++it;
517 }
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700518 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700519 if (worst_sizes < second_worst_sizes) {
520 break;
521 }
522 worst_sizes -= len;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800523 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700524 last.clear();
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800525
Mark Salyzync402bc72014-04-01 17:19:47 -0700526 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800527 break; // the following loop will ask bad clients to skip/drop
528 }
529 }
530
Mark Salyzync89839a2014-02-11 12:29:31 -0800531 bool whitelist = false;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700532 bool hasWhitelist = mPrune.nice();
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800533 it = mLogElements.begin();
Mark Salyzyn12bac902014-02-26 09:50:16 -0800534 while((pruneRows > 0) && (it != mLogElements.end())) {
535 LogBufferElement *e = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700536
537 if (e->getLogId() != id) {
538 it++;
539 continue;
540 }
541
542 if (oldest && (oldest->mStart <= e->getSequence())) {
543 if (whitelist) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800544 break;
545 }
Mark Salyzync402bc72014-04-01 17:19:47 -0700546
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700547 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
548 // kick a misbehaving log reader client off the island
549 oldest->release_Locked();
550 } else {
551 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzync89839a2014-02-11 12:29:31 -0800552 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700553 break;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800554 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700555
Mark Salyzynd910ca92015-05-21 12:50:31 -0700556 if (hasWhitelist && !e->getDropped() && mPrune.nice(e)) { // WhiteListed
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700557 whitelist = true;
558 it++;
559 continue;
560 }
561
562 it = erase(it);
563 pruneRows--;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800564 }
565
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700566 // Do not save the whitelist if we are reader range limited
Mark Salyzync89839a2014-02-11 12:29:31 -0800567 if (whitelist && (pruneRows > 0)) {
568 it = mLogElements.begin();
569 while((it != mLogElements.end()) && (pruneRows > 0)) {
570 LogBufferElement *e = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700571
572 if (e->getLogId() != id) {
573 ++it;
574 continue;
Mark Salyzync89839a2014-02-11 12:29:31 -0800575 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700576
577 if (oldest && (oldest->mStart <= e->getSequence())) {
578 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
579 // kick a misbehaving log reader client off the island
580 oldest->release_Locked();
581 } else {
582 oldest->triggerSkip_Locked(id, pruneRows);
583 }
584 break;
585 }
586
587 it = erase(it);
588 pruneRows--;
Mark Salyzync89839a2014-02-11 12:29:31 -0800589 }
590 }
Mark Salyzync89839a2014-02-11 12:29:31 -0800591
Mark Salyzyn12bac902014-02-26 09:50:16 -0800592 LogTimeEntry::unlock();
593}
594
595// clear all rows of type "id" from the buffer.
Mark Salyzyn276d5352014-06-12 11:16:16 -0700596void LogBuffer::clear(log_id_t id, uid_t uid) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800597 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn276d5352014-06-12 11:16:16 -0700598 prune(id, ULONG_MAX, uid);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800599 pthread_mutex_unlock(&mLogElementsLock);
600}
601
602// get the used space associated with "id".
603unsigned long LogBuffer::getSizeUsed(log_id_t id) {
604 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzynd774bce2014-02-06 14:48:50 -0800605 size_t retval = stats.sizes(id);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800606 pthread_mutex_unlock(&mLogElementsLock);
607 return retval;
608}
609
Mark Salyzync89839a2014-02-11 12:29:31 -0800610// set the total space allocated to "id"
611int LogBuffer::setSize(log_id_t id, unsigned long size) {
612 // Reasonable limits ...
Mark Salyzyn7b85c592014-05-09 17:44:18 -0700613 if (!valid_size(size)) {
Mark Salyzync89839a2014-02-11 12:29:31 -0800614 return -1;
615 }
616 pthread_mutex_lock(&mLogElementsLock);
617 log_buffer_size(id) = size;
618 pthread_mutex_unlock(&mLogElementsLock);
619 return 0;
620}
621
622// get the total space allocated to "id"
623unsigned long LogBuffer::getSize(log_id_t id) {
624 pthread_mutex_lock(&mLogElementsLock);
625 size_t retval = log_buffer_size(id);
626 pthread_mutex_unlock(&mLogElementsLock);
627 return retval;
628}
629
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800630uint64_t LogBuffer::flushTo(
631 SocketClient *reader, const uint64_t start, bool privileged,
632 int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800633 LogBufferElementCollection::iterator it;
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800634 uint64_t max = start;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800635 uid_t uid = reader->getUid();
636
637 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -0600638
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800639 if (start <= 1) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -0600640 // client wants to start from the beginning
641 it = mLogElements.begin();
642 } else {
643 // Client wants to start from some specified time. Chances are
644 // we are better off starting from the end of the time sorted list.
645 for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
646 --it;
647 LogBufferElement *element = *it;
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800648 if (element->getSequence() <= start) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -0600649 it++;
650 break;
651 }
652 }
653 }
654
655 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800656 LogBufferElement *element = *it;
657
658 if (!privileged && (element->getUid() != uid)) {
659 continue;
660 }
661
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800662 if (element->getSequence() <= start) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800663 continue;
664 }
665
666 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800667 if (filter) {
668 int ret = (*filter)(element, arg);
669 if (ret == false) {
670 continue;
671 }
672 if (ret != true) {
673 break;
674 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800675 }
676
677 pthread_mutex_unlock(&mLogElementsLock);
678
679 // range locking in LastLogTimes looks after us
Mark Salyzynbf918322015-04-20 07:26:27 -0700680 max = element->flushTo(reader, this);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800681
682 if (max == element->FLUSH_ERROR) {
683 return max;
684 }
685
686 pthread_mutex_lock(&mLogElementsLock);
687 }
688 pthread_mutex_unlock(&mLogElementsLock);
689
690 return max;
691}
Mark Salyzynd774bce2014-02-06 14:48:50 -0800692
Mark Salyzyn1c7b6fb2015-08-20 10:01:44 -0700693std::string LogBuffer::formatStatistics(uid_t uid, unsigned int logMask) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800694 pthread_mutex_lock(&mLogElementsLock);
695
Mark Salyzyn1c7b6fb2015-08-20 10:01:44 -0700696 std::string ret = stats.format(uid, logMask);
Mark Salyzynd774bce2014-02-06 14:48:50 -0800697
698 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn1c7b6fb2015-08-20 10:01:44 -0700699
700 return ret;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800701}