blob: 6307bed636eaa5b23a9f4f42e67609d23891f42b [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 Salyzyn12bac902014-02-26 09:50:16 -080018#include <stdio.h>
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070019#include <stdlib.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080020#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 Salyzyn4e756fb2014-05-06 07:34:59 -070025#include <cutils/properties.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080026#include <log/logger.h>
27
28#include "LogBuffer.h"
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070029#include "LogReader.h"
Mark Salyzynd774bce2014-02-06 14:48:50 -080030#include "LogStatistics.h"
Mark Salyzync89839a2014-02-11 12:29:31 -080031#include "LogWhiteBlackList.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 Salyzyn12bac902014-02-26 09:50:16 -080095LogBuffer::LogBuffer(LastLogTimes *times)
Mark Salyzyn03a6a1a2014-09-21 14:22:18 -070096 : dgramQlenStatistics(false)
97 , mTimes(*times) {
Mark Salyzyn12bac902014-02-26 09:50:16 -080098 pthread_mutex_init(&mLogElementsLock, NULL);
Mark Salyzynfe639a02014-02-19 17:18:31 -080099
Mark Salyzyn7b85c592014-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 Salyzyn4e756fb2014-05-06 07:34:59 -0700107
Mark Salyzync89839a2014-02-11 12:29:31 -0800108 log_id_for_each(i) {
Mark Salyzyn4e756fb2014-05-06 07:34:59 -0700109 char key[PROP_NAME_MAX];
Mark Salyzyn4e756fb2014-05-06 07:34:59 -0700110
Mark Salyzyn7b85c592014-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 Salyzync89839a2014-02-11 12:29:31 -0800132 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800133}
134
Mark Salyzyn71391172014-03-05 07:41:49 -0800135void LogBuffer::log(log_id_t log_id, log_time realtime,
Mark Salyzyn3661dd02014-03-20 16:09:38 -0700136 uid_t uid, pid_t pid, pid_t tid,
137 const char *msg, unsigned short len) {
Mark Salyzyn12bac902014-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 Salyzyn3661dd02014-03-20 16:09:38 -0700142 uid, pid, tid, msg, len);
Mark Salyzyn12bac902014-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 Salyzyna222a772014-10-13 16:49:47 -0700150 while (last != mLogElements.begin()) {
151 --it;
Mark Salyzyn5b7a8d82014-02-18 11:23:53 -0800152 if ((*it)->getRealTime() <= realtime) {
Mark Salyzynfe639a02014-02-19 17:18:31 -0800153 // halves the peak performance, use with caution
Mark Salyzyn03a6a1a2014-09-21 14:22:18 -0700154 if (dgramQlenStatistics) {
Mark Salyzynfe639a02014-02-19 17:18:31 -0800155 LogBufferElementCollection::iterator ib = it;
156 unsigned short buckets, num = 1;
Mark Salyzyn03a6a1a2014-09-21 14:22:18 -0700157 for (unsigned short i = 0; (buckets = stats.dgramQlen(i)); ++i) {
Mark Salyzynfe639a02014-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 Salyzyn12bac902014-02-26 09:50:16 -0800170 break;
171 }
172 last = it;
173 }
Mark Salyzyn5b7a8d82014-02-18 11:23:53 -0800174
Mark Salyzyn12bac902014-02-26 09:50:16 -0800175 if (last == mLogElements.end()) {
176 mLogElements.push_back(elem);
177 } else {
Mark Salyzyn28ac1012014-05-22 16:07:31 -0700178 log_time end = log_time::EPOCH;
Mark Salyzyn12bac902014-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 Salyzyn5b7a8d82014-02-18 11:23:53 -0800201 || (end_set && (end >= (*last)->getMonotonicTime()))) {
Mark Salyzyn12bac902014-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 Salyzynd774bce2014-02-06 14:48:50 -0800210 stats.add(len, log_id, uid, pid);
Mark Salyzyn12bac902014-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 Salyzyn9d4e34e2014-01-13 16:37:51 -0800216// at least 10% of the log entries.
Mark Salyzyn12bac902014-02-26 09:50:16 -0800217//
218// mLogElementsLock must be held when this function is called.
219void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800220 size_t sizes = stats.sizes(id);
Mark Salyzync89839a2014-02-11 12:29:31 -0800221 if (sizes > log_buffer_size(id)) {
222 size_t sizeOver90Percent = sizes - ((log_buffer_size(id) * 9) / 10);
Mark Salyzynd774bce2014-02-06 14:48:50 -0800223 size_t elements = stats.elements(id);
Mark Salyzyn9d4e34e2014-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 Salyzyn12bac902014-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 Salyzyn276d5352014-06-12 11:16:16 -0700236void LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn12bac902014-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 Schiau6ba427e2014-12-17 10:53:41 +0200245 if (entry->owned_Locked() && entry->isWatching(id)
Mark Salyzyn12bac902014-02-26 09:50:16 -0800246 && (!oldest || (oldest->mStart > entry->mStart))) {
247 oldest = entry;
248 }
249 t++;
250 }
251
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800252 LogBufferElementCollection::iterator it;
253
Mark Salyzyn276d5352014-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 Salyzyn03a6a1a2014-09-21 14:22:18 -0700271 stats.subtract(e->getMsgLen(), id, uid, e->getPid());
Mark Salyzyn276d5352014-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 Salyzyn5c06ca42014-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 Salyzyn855c7302014-04-07 14:58:08 -0700292 if ((id != LOG_ID_CRASH) && mPrune.worstUidEnabled()) {
Mark Salyzync89839a2014-02-11 12:29:31 -0800293 LidStatistics &l = stats.id(id);
Mark Salyzyn1867ca42014-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 Salyzync89839a2014-02-11 12:29:31 -0800302 }
Mark Salyzyn5c06ca42014-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 Salyzync89839a2014-02-11 12:29:31 -0800314 if (e->getLogId() != id) {
315 ++it;
316 continue;
317 }
318
319 uid_t uid = e->getUid();
320
Mark Salyzyn03a6a1a2014-09-21 14:22:18 -0700321 if ((uid == worst) || mPrune.naughty(e)) { // Worst or BlackListed
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800322 it = mLogElements.erase(it);
323 unsigned short len = e->getMsgLen();
Mark Salyzyn03a6a1a2014-09-21 14:22:18 -0700324 stats.subtract(len, id, uid, e->getPid());
Mark Salyzync89839a2014-02-11 12:29:31 -0800325 delete e;
326 pruneRows--;
Mark Salyzyn03a6a1a2014-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 Salyzync89839a2014-02-11 12:29:31 -0800334 break;
335 }
Mark Salyzync402bc72014-04-01 17:19:47 -0700336 } else {
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800337 ++it;
338 }
339 }
340
Mark Salyzync402bc72014-04-01 17:19:47 -0700341 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800342 break; // the following loop will ask bad clients to skip/drop
343 }
344 }
345
Mark Salyzync89839a2014-02-11 12:29:31 -0800346 bool whitelist = false;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800347 it = mLogElements.begin();
Mark Salyzyn12bac902014-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 Salyzync402bc72014-04-01 17:19:47 -0700352 if (!whitelist) {
Mark Salyzync89839a2014-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 Schiau6ba427e2014-12-17 10:53:41 +0200357 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzync89839a2014-02-11 12:29:31 -0800358 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800359 }
360 break;
361 }
Mark Salyzync402bc72014-04-01 17:19:47 -0700362
Mark Salyzync89839a2014-02-11 12:29:31 -0800363 if (mPrune.nice(e)) { // WhiteListed
364 whitelist = true;
365 it++;
366 continue;
367 }
Mark Salyzync402bc72014-04-01 17:19:47 -0700368
Mark Salyzyn12bac902014-02-26 09:50:16 -0800369 it = mLogElements.erase(it);
Mark Salyzynd774bce2014-02-06 14:48:50 -0800370 stats.subtract(e->getMsgLen(), id, e->getUid(), e->getPid());
Mark Salyzyn12bac902014-02-26 09:50:16 -0800371 delete e;
372 pruneRows--;
373 } else {
374 it++;
375 }
376 }
377
Mark Salyzync89839a2014-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 Schiau6ba427e2014-12-17 10:53:41 +0200388 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzync89839a2014-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 Salyzync89839a2014-02-11 12:29:31 -0800401
Mark Salyzyn12bac902014-02-26 09:50:16 -0800402 LogTimeEntry::unlock();
403}
404
405// clear all rows of type "id" from the buffer.
Mark Salyzyn276d5352014-06-12 11:16:16 -0700406void LogBuffer::clear(log_id_t id, uid_t uid) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800407 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn276d5352014-06-12 11:16:16 -0700408 prune(id, ULONG_MAX, uid);
Mark Salyzyn12bac902014-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 Salyzynd774bce2014-02-06 14:48:50 -0800415 size_t retval = stats.sizes(id);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800416 pthread_mutex_unlock(&mLogElementsLock);
417 return retval;
418}
419
Mark Salyzync89839a2014-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 Salyzyn7b85c592014-05-09 17:44:18 -0700423 if (!valid_size(size)) {
Mark Salyzync89839a2014-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 Salyzyn71391172014-03-05 07:41:49 -0800440log_time LogBuffer::flushTo(
441 SocketClient *reader, const log_time start, bool privileged,
Mark Salyzyn12bac902014-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);
448 for (it = mLogElements.begin(); it != mLogElements.end(); ++it) {
449 LogBufferElement *element = *it;
450
451 if (!privileged && (element->getUid() != uid)) {
452 continue;
453 }
454
455 if (element->getMonotonicTime() <= start) {
456 continue;
457 }
458
459 // NB: calling out to another object with mLogElementsLock held (safe)
460 if (filter && !(*filter)(element, arg)) {
461 continue;
462 }
463
464 pthread_mutex_unlock(&mLogElementsLock);
465
466 // range locking in LastLogTimes looks after us
467 max = element->flushTo(reader);
468
469 if (max == element->FLUSH_ERROR) {
470 return max;
471 }
472
473 pthread_mutex_lock(&mLogElementsLock);
474 }
475 pthread_mutex_unlock(&mLogElementsLock);
476
477 return max;
478}
Mark Salyzynd774bce2014-02-06 14:48:50 -0800479
Mark Salyzync89839a2014-02-11 12:29:31 -0800480void LogBuffer::formatStatistics(char **strp, uid_t uid, unsigned int logMask) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800481 log_time oldest(CLOCK_MONOTONIC);
482
483 pthread_mutex_lock(&mLogElementsLock);
484
485 // Find oldest element in the log(s)
486 LogBufferElementCollection::iterator it;
487 for (it = mLogElements.begin(); it != mLogElements.end(); ++it) {
488 LogBufferElement *element = *it;
489
490 if ((logMask & (1 << element->getLogId()))) {
491 oldest = element->getMonotonicTime();
492 break;
493 }
494 }
495
Mark Salyzync89839a2014-02-11 12:29:31 -0800496 stats.format(strp, uid, logMask, oldest);
Mark Salyzynd774bce2014-02-06 14:48:50 -0800497
498 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzynd774bce2014-02-06 14:48:50 -0800499}