2 * Copyright (C) 2012-2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <cutils/properties.h>
26 #include <log/logger.h>
28 #include "LogBuffer.h"
29 #include "LogReader.h"
30 #include "LogStatistics.h"
31 #include "LogWhiteBlackList.h"
34 #define LOG_BUFFER_SIZE (256 * 1024) // Tuned on a per-platform basis here?
35 #define log_buffer_size(id) mMaxSize[id]
36 #define LOG_BUFFER_MIN_SIZE (64 * 1024UL)
37 #define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL)
39 static bool valid_size(unsigned long value) {
40 if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) {
44 long pages = sysconf(_SC_PHYS_PAGES);
49 long pagesize = sysconf(_SC_PAGESIZE);
54 // maximum memory impact a somewhat arbitrary ~3%
55 pages = (pages + 31) / 32;
56 unsigned long maximum = pages * pagesize;
58 if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) {
62 return value <= maximum;
65 static unsigned long property_get_size(const char *key) {
66 char property[PROPERTY_VALUE_MAX];
67 property_get(key, property, "");
70 unsigned long value = strtoul(property, &cp, 10);
88 if (!valid_size(value)) {
95 LogBuffer::LogBuffer(LastLogTimes *times)
96 : dgramQlenStatistics(false)
98 pthread_mutex_init(&mLogElementsLock, NULL);
100 static const char global_tuneable[] = "persist.logd.size"; // Settings App
101 static const char global_default[] = "ro.logd.size"; // BoardConfig.mk
103 unsigned long default_size = property_get_size(global_tuneable);
105 default_size = property_get_size(global_default);
109 char key[PROP_NAME_MAX];
111 snprintf(key, sizeof(key), "%s.%s",
112 global_tuneable, android_log_id_to_name(i));
113 unsigned long property_size = property_get_size(key);
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);
121 if (!property_size) {
122 property_size = default_size;
125 if (!property_size) {
126 property_size = LOG_BUFFER_SIZE;
129 if (setSize(i, property_size)) {
130 setSize(i, LOG_BUFFER_MIN_SIZE);
135 void 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) {
138 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
141 LogBufferElement *elem = new LogBufferElement(log_id, realtime,
142 uid, pid, tid, msg, len);
144 pthread_mutex_lock(&mLogElementsLock);
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;
150 while (--it != mLogElements.begin()) {
151 if ((*it)->getRealTime() <= realtime) {
152 // halves the peak performance, use with caution
153 if (dgramQlenStatistics) {
154 LogBufferElementCollection::iterator ib = it;
155 unsigned short buckets, num = 1;
156 for (unsigned short i = 0; (buckets = stats.dgramQlen(i)); ++i) {
159 while (buckets && (--ib != mLogElements.begin())) {
166 elem->getRealTime() - (*ib)->getRealTime(), i);
174 if (last == mLogElements.end()) {
175 mLogElements.push_back(elem);
177 log_time end = log_time::EPOCH;
178 bool end_set = false;
179 bool end_always = false;
181 LogTimeEntry::lock();
183 LastLogTimes::iterator t = mTimes.begin();
184 while(t != mTimes.end()) {
185 LogTimeEntry *entry = (*t);
186 if (entry->owned_Locked()) {
187 if (!entry->mNonBlock) {
191 if (!end_set || (end <= entry->mEnd)) {
200 || (end_set && (end >= (*last)->getMonotonicTime()))) {
201 mLogElements.push_back(elem);
203 mLogElements.insert(last,elem);
206 LogTimeEntry::unlock();
209 stats.add(len, log_id, uid, pid);
211 pthread_mutex_unlock(&mLogElementsLock);
214 // If we're using more than 256K of memory for log entries, prune
215 // at least 10% of the log entries.
217 // mLogElementsLock must be held when this function is called.
218 void LogBuffer::maybePrune(log_id_t id) {
219 size_t sizes = stats.sizes(id);
220 if (sizes > log_buffer_size(id)) {
221 size_t sizeOver90Percent = sizes - ((log_buffer_size(id) * 9) / 10);
222 size_t elements = stats.elements(id);
223 unsigned long pruneRows = elements * sizeOver90Percent / sizes;
225 if (pruneRows <= elements) {
226 pruneRows = elements;
228 prune(id, pruneRows);
232 // prune "pruneRows" of type "id" from the buffer.
234 // mLogElementsLock must be held when this function is called.
235 void LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
236 LogTimeEntry *oldest = NULL;
238 LogTimeEntry::lock();
241 LastLogTimes::iterator t = mTimes.begin();
242 while(t != mTimes.end()) {
243 LogTimeEntry *entry = (*t);
244 if (entry->owned_Locked()
245 && (!oldest || (oldest->mStart > entry->mStart))) {
251 LogBufferElementCollection::iterator it;
253 if (caller_uid != AID_ROOT) {
254 for(it = mLogElements.begin(); it != mLogElements.end();) {
255 LogBufferElement *e = *it;
257 if (oldest && (oldest->mStart <= e->getMonotonicTime())) {
261 if (e->getLogId() != id) {
266 uid_t uid = e->getUid();
268 if (uid == caller_uid) {
269 it = mLogElements.erase(it);
270 stats.subtract(e->getMsgLen(), id, uid, e->getPid());
273 if (pruneRows == 0) {
280 LogTimeEntry::unlock();
284 // prune by worst offender by uid
285 while (pruneRows > 0) {
286 // recalculate the worst offender on every batched pass
287 uid_t worst = (uid_t) -1;
288 size_t worst_sizes = 0;
289 size_t second_worst_sizes = 0;
291 if ((id != LOG_ID_CRASH) && mPrune.worstUidEnabled()) {
292 LidStatistics &l = stats.id(id);
294 UidStatisticsCollection::iterator iu = l.begin();
296 UidStatistics *u = *iu;
298 worst_sizes = u->sizes();
299 if (++iu != l.end()) {
300 second_worst_sizes = (*iu)->sizes();
306 for(it = mLogElements.begin(); it != mLogElements.end();) {
307 LogBufferElement *e = *it;
309 if (oldest && (oldest->mStart <= e->getMonotonicTime())) {
313 if (e->getLogId() != id) {
318 uid_t uid = e->getUid();
320 if ((uid == worst) || mPrune.naughty(e)) { // Worst or BlackListed
321 it = mLogElements.erase(it);
322 unsigned short len = e->getMsgLen();
323 stats.subtract(len, id, uid, e->getPid());
328 if ((pruneRows == 0) || (worst_sizes < second_worst_sizes)) {
332 } else if (pruneRows == 0) {
340 if (!kick || !mPrune.worstUidEnabled()) {
341 break; // the following loop will ask bad clients to skip/drop
345 bool whitelist = false;
346 it = mLogElements.begin();
347 while((pruneRows > 0) && (it != mLogElements.end())) {
348 LogBufferElement *e = *it;
349 if (e->getLogId() == id) {
350 if (oldest && (oldest->mStart <= e->getMonotonicTime())) {
352 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
353 // kick a misbehaving log reader client off the island
354 oldest->release_Locked();
356 oldest->triggerSkip_Locked(pruneRows);
362 if (mPrune.nice(e)) { // WhiteListed
368 it = mLogElements.erase(it);
369 stats.subtract(e->getMsgLen(), id, e->getUid(), e->getPid());
377 if (whitelist && (pruneRows > 0)) {
378 it = mLogElements.begin();
379 while((it != mLogElements.end()) && (pruneRows > 0)) {
380 LogBufferElement *e = *it;
381 if (e->getLogId() == id) {
382 if (oldest && (oldest->mStart <= e->getMonotonicTime())) {
383 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
384 // kick a misbehaving log reader client off the island
385 oldest->release_Locked();
387 oldest->triggerSkip_Locked(pruneRows);
391 it = mLogElements.erase(it);
392 stats.subtract(e->getMsgLen(), id, e->getUid(), e->getPid());
401 LogTimeEntry::unlock();
404 // clear all rows of type "id" from the buffer.
405 void LogBuffer::clear(log_id_t id, uid_t uid) {
406 pthread_mutex_lock(&mLogElementsLock);
407 prune(id, ULONG_MAX, uid);
408 pthread_mutex_unlock(&mLogElementsLock);
411 // get the used space associated with "id".
412 unsigned long LogBuffer::getSizeUsed(log_id_t id) {
413 pthread_mutex_lock(&mLogElementsLock);
414 size_t retval = stats.sizes(id);
415 pthread_mutex_unlock(&mLogElementsLock);
419 // set the total space allocated to "id"
420 int LogBuffer::setSize(log_id_t id, unsigned long size) {
421 // Reasonable limits ...
422 if (!valid_size(size)) {
425 pthread_mutex_lock(&mLogElementsLock);
426 log_buffer_size(id) = size;
427 pthread_mutex_unlock(&mLogElementsLock);
431 // get the total space allocated to "id"
432 unsigned long LogBuffer::getSize(log_id_t id) {
433 pthread_mutex_lock(&mLogElementsLock);
434 size_t retval = log_buffer_size(id);
435 pthread_mutex_unlock(&mLogElementsLock);
439 log_time LogBuffer::flushTo(
440 SocketClient *reader, const log_time start, bool privileged,
441 bool (*filter)(const LogBufferElement *element, void *arg), void *arg) {
442 LogBufferElementCollection::iterator it;
443 log_time max = start;
444 uid_t uid = reader->getUid();
446 pthread_mutex_lock(&mLogElementsLock);
447 for (it = mLogElements.begin(); it != mLogElements.end(); ++it) {
448 LogBufferElement *element = *it;
450 if (!privileged && (element->getUid() != uid)) {
454 if (element->getMonotonicTime() <= start) {
458 // NB: calling out to another object with mLogElementsLock held (safe)
459 if (filter && !(*filter)(element, arg)) {
463 pthread_mutex_unlock(&mLogElementsLock);
465 // range locking in LastLogTimes looks after us
466 max = element->flushTo(reader);
468 if (max == element->FLUSH_ERROR) {
472 pthread_mutex_lock(&mLogElementsLock);
474 pthread_mutex_unlock(&mLogElementsLock);
479 void LogBuffer::formatStatistics(char **strp, uid_t uid, unsigned int logMask) {
480 log_time oldest(CLOCK_MONOTONIC);
482 pthread_mutex_lock(&mLogElementsLock);
484 // Find oldest element in the log(s)
485 LogBufferElementCollection::iterator it;
486 for (it = mLogElements.begin(); it != mLogElements.end(); ++it) {
487 LogBufferElement *element = *it;
489 if ((logMask & (1 << element->getLogId()))) {
490 oldest = element->getMonotonicTime();
495 stats.format(strp, uid, logMask, oldest);
497 pthread_mutex_unlock(&mLogElementsLock);