blob: 3d0b38f8fe57d65ba3c34355f8e3c2cb7ac10e36 [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 Salyzyn0175b072014-02-26 09:50:16 -080018#include <stdio.h>
Mark Salyzyn671e3432014-05-06 07:34:59 -070019#include <stdlib.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080020#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 Salyzyn34facab2014-02-06 14:48:50 -080030#include "LogStatistics.h"
Mark Salyzyndfa7a072014-02-11 12:29:31 -080031#include "LogWhiteBlackList.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080032
Mark Salyzyndfa7a072014-02-11 12:29:31 -080033// Default
Mark Salyzyn0175b072014-02-26 09:50:16 -080034#define LOG_BUFFER_SIZE (256 * 1024) // Tuned on a per-platform basis here?
Mark Salyzyndfa7a072014-02-11 12:29:31 -080035#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn57a0af92014-05-09 17:44:18 -070036#define LOG_BUFFER_MIN_SIZE (64 * 1024UL)
37#define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL)
38
39static bool valid_size(unsigned long value) {
40 if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) {
41 return false;
42 }
43
44 long pages = sysconf(_SC_PHYS_PAGES);
45 if (pages < 1) {
46 return true;
47 }
48
49 long pagesize = sysconf(_SC_PAGESIZE);
50 if (pagesize <= 1) {
51 pagesize = PAGE_SIZE;
52 }
53
54 // maximum memory impact a somewhat arbitrary ~3%
55 pages = (pages + 31) / 32;
56 unsigned long maximum = pages * pagesize;
57
58 if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) {
59 return true;
60 }
61
62 return value <= maximum;
63}
Mark Salyzyn0175b072014-02-26 09:50:16 -080064
Mark Salyzyn671e3432014-05-06 07:34:59 -070065static unsigned long property_get_size(const char *key) {
66 char property[PROPERTY_VALUE_MAX];
67 property_get(key, property, "");
68
69 char *cp;
70 unsigned long value = strtoul(property, &cp, 10);
71
72 switch(*cp) {
73 case 'm':
74 case 'M':
75 value *= 1024;
76 /* FALLTHRU */
77 case 'k':
78 case 'K':
79 value *= 1024;
80 /* FALLTHRU */
81 case '\0':
82 break;
83
84 default:
85 value = 0;
86 }
87
Mark Salyzyn57a0af92014-05-09 17:44:18 -070088 if (!valid_size(value)) {
89 value = 0;
90 }
91
Mark Salyzyn671e3432014-05-06 07:34:59 -070092 return value;
93}
94
Mark Salyzyn0175b072014-02-26 09:50:16 -080095LogBuffer::LogBuffer(LastLogTimes *times)
Mark Salyzyne72c6e42014-09-21 14:22:18 -070096 : dgramQlenStatistics(false)
97 , mTimes(*times) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080098 pthread_mutex_init(&mLogElementsLock, NULL);
Mark Salyzyne457b742014-02-19 17:18:31 -080099
Mark Salyzyn57a0af92014-05-09 17:44:18 -0700100 static const char global_tuneable[] = "persist.logd.size"; // Settings App
101 static const char global_default[] = "ro.logd.size"; // BoardConfig.mk
102
103 unsigned long default_size = property_get_size(global_tuneable);
104 if (!default_size) {
105 default_size = property_get_size(global_default);
106 }
Mark Salyzyn671e3432014-05-06 07:34:59 -0700107
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800108 log_id_for_each(i) {
Mark Salyzyn671e3432014-05-06 07:34:59 -0700109 char key[PROP_NAME_MAX];
Mark Salyzyn671e3432014-05-06 07:34:59 -0700110
Mark Salyzyn57a0af92014-05-09 17:44:18 -0700111 snprintf(key, sizeof(key), "%s.%s",
112 global_tuneable, android_log_id_to_name(i));
113 unsigned long property_size = property_get_size(key);
114
115 if (!property_size) {
116 snprintf(key, sizeof(key), "%s.%s",
117 global_default, android_log_id_to_name(i));
118 property_size = property_get_size(key);
119 }
120
121 if (!property_size) {
122 property_size = default_size;
123 }
124
125 if (!property_size) {
126 property_size = LOG_BUFFER_SIZE;
127 }
128
129 if (setSize(i, property_size)) {
130 setSize(i, LOG_BUFFER_MIN_SIZE);
131 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800132 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800133}
134
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -0800135void LogBuffer::log(log_id_t log_id, log_time realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700136 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)) {
139 return;
140 }
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 Salyzyne457b742014-02-19 17:18:31 -0800153 // halves the peak performance, use with caution
Mark Salyzyne72c6e42014-09-21 14:22:18 -0700154 if (dgramQlenStatistics) {
Mark Salyzyne457b742014-02-19 17:18:31 -0800155 LogBufferElementCollection::iterator ib = it;
156 unsigned short buckets, num = 1;
Mark Salyzyne72c6e42014-09-21 14:22:18 -0700157 for (unsigned short i = 0; (buckets = stats.dgramQlen(i)); ++i) {
Mark Salyzyne457b742014-02-19 17:18:31 -0800158 buckets -= num;
159 num += buckets;
160 while (buckets && (--ib != mLogElements.begin())) {
161 --buckets;
162 }
163 if (buckets) {
164 break;
165 }
166 stats.recordDiff(
167 elem->getRealTime() - (*ib)->getRealTime(), i);
168 }
169 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800170 break;
171 }
172 last = it;
173 }
Mark Salyzync03e72c2014-02-18 11:23:53 -0800174
Mark Salyzyn0175b072014-02-26 09:50:16 -0800175 if (last == mLogElements.end()) {
176 mLogElements.push_back(elem);
177 } else {
Mark Salyzynab4b7302014-05-22 16:07:31 -0700178 log_time end = log_time::EPOCH;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800179 bool end_set = false;
180 bool end_always = false;
181
182 LogTimeEntry::lock();
183
184 LastLogTimes::iterator t = mTimes.begin();
185 while(t != mTimes.end()) {
186 LogTimeEntry *entry = (*t);
187 if (entry->owned_Locked()) {
188 if (!entry->mNonBlock) {
189 end_always = true;
190 break;
191 }
192 if (!end_set || (end <= entry->mEnd)) {
193 end = entry->mEnd;
194 end_set = true;
195 }
196 }
197 t++;
198 }
199
200 if (end_always
Mark Salyzync03e72c2014-02-18 11:23:53 -0800201 || (end_set && (end >= (*last)->getMonotonicTime()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800202 mLogElements.push_back(elem);
203 } else {
204 mLogElements.insert(last,elem);
205 }
206
207 LogTimeEntry::unlock();
208 }
209
Mark Salyzyn34facab2014-02-06 14:48:50 -0800210 stats.add(len, log_id, uid, pid);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800211 maybePrune(log_id);
212 pthread_mutex_unlock(&mLogElementsLock);
213}
214
215// If we're using more than 256K of memory for log entries, prune
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800216// at least 10% of the log entries.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800217//
218// mLogElementsLock must be held when this function is called.
219void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800220 size_t sizes = stats.sizes(id);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800221 if (sizes > log_buffer_size(id)) {
222 size_t sizeOver90Percent = sizes - ((log_buffer_size(id) * 9) / 10);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800223 size_t elements = stats.elements(id);
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800224 unsigned long pruneRows = elements * sizeOver90Percent / sizes;
225 elements /= 10;
226 if (pruneRows <= elements) {
227 pruneRows = elements;
228 }
229 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800230 }
231}
232
233// prune "pruneRows" of type "id" from the buffer.
234//
235// mLogElementsLock must be held when this function is called.
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700236void LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800237 LogTimeEntry *oldest = NULL;
238
239 LogTimeEntry::lock();
240
241 // Region locked?
242 LastLogTimes::iterator t = mTimes.begin();
243 while(t != mTimes.end()) {
244 LogTimeEntry *entry = (*t);
TraianX Schiauda6495d2014-12-17 10:53:41 +0200245 if (entry->owned_Locked() && entry->isWatching(id)
Mark Salyzyn0175b072014-02-26 09:50:16 -0800246 && (!oldest || (oldest->mStart > entry->mStart))) {
247 oldest = entry;
248 }
249 t++;
250 }
251
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800252 LogBufferElementCollection::iterator it;
253
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700254 if (caller_uid != AID_ROOT) {
255 for(it = mLogElements.begin(); it != mLogElements.end();) {
256 LogBufferElement *e = *it;
257
258 if (oldest && (oldest->mStart <= e->getMonotonicTime())) {
259 break;
260 }
261
262 if (e->getLogId() != id) {
263 ++it;
264 continue;
265 }
266
267 uid_t uid = e->getUid();
268
269 if (uid == caller_uid) {
270 it = mLogElements.erase(it);
Mark Salyzyne72c6e42014-09-21 14:22:18 -0700271 stats.subtract(e->getMsgLen(), id, uid, e->getPid());
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700272 delete e;
273 pruneRows--;
274 if (pruneRows == 0) {
275 break;
276 }
277 } else {
278 ++it;
279 }
280 }
281 LogTimeEntry::unlock();
282 return;
283 }
284
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800285 // prune by worst offender by uid
286 while (pruneRows > 0) {
287 // recalculate the worst offender on every batched pass
288 uid_t worst = (uid_t) -1;
289 size_t worst_sizes = 0;
290 size_t second_worst_sizes = 0;
291
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700292 if ((id != LOG_ID_CRASH) && mPrune.worstUidEnabled()) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800293 LidStatistics &l = stats.id(id);
Mark Salyzync8a576c2014-04-04 16:35:59 -0700294 l.sort();
295 UidStatisticsCollection::iterator iu = l.begin();
296 if (iu != l.end()) {
297 UidStatistics *u = *iu;
298 worst = u->getUid();
299 worst_sizes = u->sizes();
300 if (++iu != l.end()) {
301 second_worst_sizes = (*iu)->sizes();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800302 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800303 }
304 }
305
306 bool kick = false;
307 for(it = mLogElements.begin(); it != mLogElements.end();) {
308 LogBufferElement *e = *it;
309
310 if (oldest && (oldest->mStart <= e->getMonotonicTime())) {
311 break;
312 }
313
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800314 if (e->getLogId() != id) {
315 ++it;
316 continue;
317 }
318
319 uid_t uid = e->getUid();
320
Mark Salyzyne72c6e42014-09-21 14:22:18 -0700321 if ((uid == worst) || mPrune.naughty(e)) { // Worst or BlackListed
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800322 it = mLogElements.erase(it);
323 unsigned short len = e->getMsgLen();
Mark Salyzyne72c6e42014-09-21 14:22:18 -0700324 stats.subtract(len, id, uid, e->getPid());
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800325 delete e;
326 pruneRows--;
Mark Salyzyne72c6e42014-09-21 14:22:18 -0700327 if (uid == worst) {
328 kick = true;
329 if ((pruneRows == 0) || (worst_sizes < second_worst_sizes)) {
330 break;
331 }
332 worst_sizes -= len;
333 } else if (pruneRows == 0) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800334 break;
335 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700336 } else {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800337 ++it;
338 }
339 }
340
Mark Salyzyn1c950472014-04-01 17:19:47 -0700341 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800342 break; // the following loop will ask bad clients to skip/drop
343 }
344 }
345
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800346 bool whitelist = false;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800347 it = mLogElements.begin();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800348 while((pruneRows > 0) && (it != mLogElements.end())) {
349 LogBufferElement *e = *it;
350 if (e->getLogId() == id) {
351 if (oldest && (oldest->mStart <= e->getMonotonicTime())) {
Mark Salyzyn1c950472014-04-01 17:19:47 -0700352 if (!whitelist) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800353 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
354 // kick a misbehaving log reader client off the island
355 oldest->release_Locked();
356 } else {
TraianX Schiauda6495d2014-12-17 10:53:41 +0200357 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800358 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800359 }
360 break;
361 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700362
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800363 if (mPrune.nice(e)) { // WhiteListed
364 whitelist = true;
365 it++;
366 continue;
367 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700368
Mark Salyzyn0175b072014-02-26 09:50:16 -0800369 it = mLogElements.erase(it);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800370 stats.subtract(e->getMsgLen(), id, e->getUid(), e->getPid());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800371 delete e;
372 pruneRows--;
373 } else {
374 it++;
375 }
376 }
377
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800378 if (whitelist && (pruneRows > 0)) {
379 it = mLogElements.begin();
380 while((it != mLogElements.end()) && (pruneRows > 0)) {
381 LogBufferElement *e = *it;
382 if (e->getLogId() == id) {
383 if (oldest && (oldest->mStart <= e->getMonotonicTime())) {
384 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
385 // kick a misbehaving log reader client off the island
386 oldest->release_Locked();
387 } else {
TraianX Schiauda6495d2014-12-17 10:53:41 +0200388 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800389 }
390 break;
391 }
392 it = mLogElements.erase(it);
393 stats.subtract(e->getMsgLen(), id, e->getUid(), e->getPid());
394 delete e;
395 pruneRows--;
396 } else {
397 it++;
398 }
399 }
400 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800401
Mark Salyzyn0175b072014-02-26 09:50:16 -0800402 LogTimeEntry::unlock();
403}
404
405// clear all rows of type "id" from the buffer.
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700406void LogBuffer::clear(log_id_t id, uid_t uid) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800407 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700408 prune(id, ULONG_MAX, uid);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800409 pthread_mutex_unlock(&mLogElementsLock);
410}
411
412// get the used space associated with "id".
413unsigned long LogBuffer::getSizeUsed(log_id_t id) {
414 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800415 size_t retval = stats.sizes(id);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800416 pthread_mutex_unlock(&mLogElementsLock);
417 return retval;
418}
419
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800420// set the total space allocated to "id"
421int LogBuffer::setSize(log_id_t id, unsigned long size) {
422 // Reasonable limits ...
Mark Salyzyn57a0af92014-05-09 17:44:18 -0700423 if (!valid_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800424 return -1;
425 }
426 pthread_mutex_lock(&mLogElementsLock);
427 log_buffer_size(id) = size;
428 pthread_mutex_unlock(&mLogElementsLock);
429 return 0;
430}
431
432// get the total space allocated to "id"
433unsigned long LogBuffer::getSize(log_id_t id) {
434 pthread_mutex_lock(&mLogElementsLock);
435 size_t retval = log_buffer_size(id);
436 pthread_mutex_unlock(&mLogElementsLock);
437 return retval;
438}
439
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -0800440log_time LogBuffer::flushTo(
441 SocketClient *reader, const log_time start, bool privileged,
Mark Salyzyn0175b072014-02-26 09:50:16 -0800442 bool (*filter)(const LogBufferElement *element, void *arg), void *arg) {
443 LogBufferElementCollection::iterator it;
444 log_time max = start;
445 uid_t uid = reader->getUid();
446
447 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600448
449 if (start == LogTimeEntry::EPOCH) {
450 // client wants to start from the beginning
451 it = mLogElements.begin();
452 } else {
453 // Client wants to start from some specified time. Chances are
454 // we are better off starting from the end of the time sorted list.
455 for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
456 --it;
457 LogBufferElement *element = *it;
458 if (element->getMonotonicTime() <= start) {
459 it++;
460 break;
461 }
462 }
463 }
464
465 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800466 LogBufferElement *element = *it;
467
468 if (!privileged && (element->getUid() != uid)) {
469 continue;
470 }
471
472 if (element->getMonotonicTime() <= start) {
473 continue;
474 }
475
476 // NB: calling out to another object with mLogElementsLock held (safe)
477 if (filter && !(*filter)(element, arg)) {
478 continue;
479 }
480
481 pthread_mutex_unlock(&mLogElementsLock);
482
483 // range locking in LastLogTimes looks after us
484 max = element->flushTo(reader);
485
486 if (max == element->FLUSH_ERROR) {
487 return max;
488 }
489
490 pthread_mutex_lock(&mLogElementsLock);
491 }
492 pthread_mutex_unlock(&mLogElementsLock);
493
494 return max;
495}
Mark Salyzyn34facab2014-02-06 14:48:50 -0800496
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800497void LogBuffer::formatStatistics(char **strp, uid_t uid, unsigned int logMask) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800498 log_time oldest(CLOCK_MONOTONIC);
499
500 pthread_mutex_lock(&mLogElementsLock);
501
502 // Find oldest element in the log(s)
503 LogBufferElementCollection::iterator it;
504 for (it = mLogElements.begin(); it != mLogElements.end(); ++it) {
505 LogBufferElement *element = *it;
506
507 if ((logMask & (1 << element->getLogId()))) {
508 oldest = element->getMonotonicTime();
509 break;
510 }
511 }
512
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800513 stats.format(strp, uid, logMask, oldest);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800514
515 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800516}