blob: 1c5cef03b1a1408d46753082e1b49fd7c0753379 [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
17#include <stdio.h>
18#include <string.h>
19#include <time.h>
20#include <unistd.h>
21
22#include <log/logger.h>
23
24#include "LogBuffer.h"
Mark Salyzyn34facab2014-02-06 14:48:50 -080025#include "LogStatistics.h"
Mark Salyzyndfa7a072014-02-11 12:29:31 -080026#include "LogWhiteBlackList.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080027#include "LogReader.h"
28
Mark Salyzyndfa7a072014-02-11 12:29:31 -080029// Default
Mark Salyzyn0175b072014-02-26 09:50:16 -080030#define LOG_BUFFER_SIZE (256 * 1024) // Tuned on a per-platform basis here?
Mark Salyzyndfa7a072014-02-11 12:29:31 -080031#ifdef USERDEBUG_BUILD
32#define log_buffer_size(id) mMaxSize[id]
33#else
34#define log_buffer_size(id) LOG_BUFFER_SIZE
35#endif
Mark Salyzyn0175b072014-02-26 09:50:16 -080036
37LogBuffer::LogBuffer(LastLogTimes *times)
38 : mTimes(*times) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080039 pthread_mutex_init(&mLogElementsLock, NULL);
Mark Salyzyndfa7a072014-02-11 12:29:31 -080040#ifdef USERDEBUG_BUILD
41 log_id_for_each(i) {
42 mMaxSize[i] = LOG_BUFFER_SIZE;
43 }
44#endif
Mark Salyzyn0175b072014-02-26 09:50:16 -080045}
46
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -080047void LogBuffer::log(log_id_t log_id, log_time realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -070048 uid_t uid, pid_t pid, pid_t tid,
49 const char *msg, unsigned short len) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080050 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
51 return;
52 }
53 LogBufferElement *elem = new LogBufferElement(log_id, realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -070054 uid, pid, tid, msg, len);
Mark Salyzyn0175b072014-02-26 09:50:16 -080055
56 pthread_mutex_lock(&mLogElementsLock);
57
58 // Insert elements in time sorted order if possible
59 // NB: if end is region locked, place element at end of list
60 LogBufferElementCollection::iterator it = mLogElements.end();
61 LogBufferElementCollection::iterator last = it;
62 while (--it != mLogElements.begin()) {
Mark Salyzync03e72c2014-02-18 11:23:53 -080063 if ((*it)->getRealTime() <= realtime) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080064 break;
65 }
66 last = it;
67 }
Mark Salyzync03e72c2014-02-18 11:23:53 -080068
Mark Salyzyn0175b072014-02-26 09:50:16 -080069 if (last == mLogElements.end()) {
70 mLogElements.push_back(elem);
71 } else {
72 log_time end;
73 bool end_set = false;
74 bool end_always = false;
75
76 LogTimeEntry::lock();
77
78 LastLogTimes::iterator t = mTimes.begin();
79 while(t != mTimes.end()) {
80 LogTimeEntry *entry = (*t);
81 if (entry->owned_Locked()) {
82 if (!entry->mNonBlock) {
83 end_always = true;
84 break;
85 }
86 if (!end_set || (end <= entry->mEnd)) {
87 end = entry->mEnd;
88 end_set = true;
89 }
90 }
91 t++;
92 }
93
94 if (end_always
Mark Salyzync03e72c2014-02-18 11:23:53 -080095 || (end_set && (end >= (*last)->getMonotonicTime()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080096 mLogElements.push_back(elem);
97 } else {
98 mLogElements.insert(last,elem);
99 }
100
101 LogTimeEntry::unlock();
102 }
103
Mark Salyzyn34facab2014-02-06 14:48:50 -0800104 stats.add(len, log_id, uid, pid);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800105 maybePrune(log_id);
106 pthread_mutex_unlock(&mLogElementsLock);
107}
108
109// If we're using more than 256K of memory for log entries, prune
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800110// at least 10% of the log entries.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800111//
112// mLogElementsLock must be held when this function is called.
113void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800114 size_t sizes = stats.sizes(id);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800115 if (sizes > log_buffer_size(id)) {
116 size_t sizeOver90Percent = sizes - ((log_buffer_size(id) * 9) / 10);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800117 size_t elements = stats.elements(id);
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800118 unsigned long pruneRows = elements * sizeOver90Percent / sizes;
119 elements /= 10;
120 if (pruneRows <= elements) {
121 pruneRows = elements;
122 }
123 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800124 }
125}
126
127// prune "pruneRows" of type "id" from the buffer.
128//
129// mLogElementsLock must be held when this function is called.
130void LogBuffer::prune(log_id_t id, unsigned long pruneRows) {
131 LogTimeEntry *oldest = NULL;
132
133 LogTimeEntry::lock();
134
135 // Region locked?
136 LastLogTimes::iterator t = mTimes.begin();
137 while(t != mTimes.end()) {
138 LogTimeEntry *entry = (*t);
139 if (entry->owned_Locked()
140 && (!oldest || (oldest->mStart > entry->mStart))) {
141 oldest = entry;
142 }
143 t++;
144 }
145
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800146 LogBufferElementCollection::iterator it;
147
148 // prune by worst offender by uid
149 while (pruneRows > 0) {
150 // recalculate the worst offender on every batched pass
151 uid_t worst = (uid_t) -1;
152 size_t worst_sizes = 0;
153 size_t second_worst_sizes = 0;
154
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800155#ifdef USERDEBUG_BUILD
156 if (mPrune.worstUidEnabled())
157#endif
158 {
159 LidStatistics &l = stats.id(id);
160 UidStatisticsCollection::iterator iu;
161 for (iu = l.begin(); iu != l.end(); ++iu) {
162 UidStatistics *u = (*iu);
163 size_t sizes = u->sizes();
164 if (worst_sizes < sizes) {
165 second_worst_sizes = worst_sizes;
166 worst_sizes = sizes;
167 worst = u->getUid();
168 }
169 if ((second_worst_sizes < sizes) && (sizes < worst_sizes)) {
170 second_worst_sizes = sizes;
171 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800172 }
173 }
174
175 bool kick = false;
176 for(it = mLogElements.begin(); it != mLogElements.end();) {
177 LogBufferElement *e = *it;
178
179 if (oldest && (oldest->mStart <= e->getMonotonicTime())) {
180 break;
181 }
182
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800183 if (e->getLogId() != id) {
184 ++it;
185 continue;
186 }
187
188 uid_t uid = e->getUid();
189
190 if (uid == worst) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800191 it = mLogElements.erase(it);
192 unsigned short len = e->getMsgLen();
193 stats.subtract(len, id, worst, e->getPid());
194 delete e;
195 kick = true;
196 pruneRows--;
197 if ((pruneRows == 0) || (worst_sizes < second_worst_sizes)) {
198 break;
199 }
200 worst_sizes -= len;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800201 }
202#ifdef USERDEBUG_BUILD
203 else if (mPrune.naughty(e)) { // BlackListed
204 it = mLogElements.erase(it);
205 stats.subtract(e->getMsgLen(), id, uid, e->getPid());
206 delete e;
207 pruneRows--;
208 if (pruneRows == 0) {
209 break;
210 }
211 }
212#endif
213 else {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800214 ++it;
215 }
216 }
217
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800218 if (!kick
219#ifdef USERDEBUG_BUILD
220 || !mPrune.worstUidEnabled()
221#endif
222 ) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800223 break; // the following loop will ask bad clients to skip/drop
224 }
225 }
226
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800227#ifdef USERDEBUG_BUILD
228 bool whitelist = false;
229#endif
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800230 it = mLogElements.begin();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800231 while((pruneRows > 0) && (it != mLogElements.end())) {
232 LogBufferElement *e = *it;
233 if (e->getLogId() == id) {
234 if (oldest && (oldest->mStart <= e->getMonotonicTime())) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800235#ifdef USERDEBUG_BUILD
236 if (!whitelist)
237#endif
238 {
239 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
240 // kick a misbehaving log reader client off the island
241 oldest->release_Locked();
242 } else {
243 oldest->triggerSkip_Locked(pruneRows);
244 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800245 }
246 break;
247 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800248#ifdef USERDEBUG_BUILD
249 if (mPrune.nice(e)) { // WhiteListed
250 whitelist = true;
251 it++;
252 continue;
253 }
254#endif
Mark Salyzyn0175b072014-02-26 09:50:16 -0800255 it = mLogElements.erase(it);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800256 stats.subtract(e->getMsgLen(), id, e->getUid(), e->getPid());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800257 delete e;
258 pruneRows--;
259 } else {
260 it++;
261 }
262 }
263
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800264#ifdef USERDEBUG_BUILD
265 if (whitelist && (pruneRows > 0)) {
266 it = mLogElements.begin();
267 while((it != mLogElements.end()) && (pruneRows > 0)) {
268 LogBufferElement *e = *it;
269 if (e->getLogId() == id) {
270 if (oldest && (oldest->mStart <= e->getMonotonicTime())) {
271 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
272 // kick a misbehaving log reader client off the island
273 oldest->release_Locked();
274 } else {
275 oldest->triggerSkip_Locked(pruneRows);
276 }
277 break;
278 }
279 it = mLogElements.erase(it);
280 stats.subtract(e->getMsgLen(), id, e->getUid(), e->getPid());
281 delete e;
282 pruneRows--;
283 } else {
284 it++;
285 }
286 }
287 }
288#endif
289
Mark Salyzyn0175b072014-02-26 09:50:16 -0800290 LogTimeEntry::unlock();
291}
292
293// clear all rows of type "id" from the buffer.
294void LogBuffer::clear(log_id_t id) {
295 pthread_mutex_lock(&mLogElementsLock);
296 prune(id, ULONG_MAX);
297 pthread_mutex_unlock(&mLogElementsLock);
298}
299
300// get the used space associated with "id".
301unsigned long LogBuffer::getSizeUsed(log_id_t id) {
302 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800303 size_t retval = stats.sizes(id);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800304 pthread_mutex_unlock(&mLogElementsLock);
305 return retval;
306}
307
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800308#ifdef USERDEBUG_BUILD
309
310// set the total space allocated to "id"
311int LogBuffer::setSize(log_id_t id, unsigned long size) {
312 // Reasonable limits ...
313 if ((size < (64 * 1024)) || ((256 * 1024 * 1024) < size)) {
314 return -1;
315 }
316 pthread_mutex_lock(&mLogElementsLock);
317 log_buffer_size(id) = size;
318 pthread_mutex_unlock(&mLogElementsLock);
319 return 0;
320}
321
322// get the total space allocated to "id"
323unsigned long LogBuffer::getSize(log_id_t id) {
324 pthread_mutex_lock(&mLogElementsLock);
325 size_t retval = log_buffer_size(id);
326 pthread_mutex_unlock(&mLogElementsLock);
327 return retval;
328}
329
330#else // ! USERDEBUG_BUILD
331
Mark Salyzyn0175b072014-02-26 09:50:16 -0800332// get the total space allocated to "id"
333unsigned long LogBuffer::getSize(log_id_t /*id*/) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800334 return log_buffer_size(id);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800335}
336
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800337#endif
338
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -0800339log_time LogBuffer::flushTo(
340 SocketClient *reader, const log_time start, bool privileged,
Mark Salyzyn0175b072014-02-26 09:50:16 -0800341 bool (*filter)(const LogBufferElement *element, void *arg), void *arg) {
342 LogBufferElementCollection::iterator it;
343 log_time max = start;
344 uid_t uid = reader->getUid();
345
346 pthread_mutex_lock(&mLogElementsLock);
347 for (it = mLogElements.begin(); it != mLogElements.end(); ++it) {
348 LogBufferElement *element = *it;
349
350 if (!privileged && (element->getUid() != uid)) {
351 continue;
352 }
353
354 if (element->getMonotonicTime() <= start) {
355 continue;
356 }
357
358 // NB: calling out to another object with mLogElementsLock held (safe)
359 if (filter && !(*filter)(element, arg)) {
360 continue;
361 }
362
363 pthread_mutex_unlock(&mLogElementsLock);
364
365 // range locking in LastLogTimes looks after us
366 max = element->flushTo(reader);
367
368 if (max == element->FLUSH_ERROR) {
369 return max;
370 }
371
372 pthread_mutex_lock(&mLogElementsLock);
373 }
374 pthread_mutex_unlock(&mLogElementsLock);
375
376 return max;
377}
Mark Salyzyn34facab2014-02-06 14:48:50 -0800378
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800379void LogBuffer::formatStatistics(char **strp, uid_t uid, unsigned int logMask) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800380 log_time oldest(CLOCK_MONOTONIC);
381
382 pthread_mutex_lock(&mLogElementsLock);
383
384 // Find oldest element in the log(s)
385 LogBufferElementCollection::iterator it;
386 for (it = mLogElements.begin(); it != mLogElements.end(); ++it) {
387 LogBufferElement *element = *it;
388
389 if ((logMask & (1 << element->getLogId()))) {
390 oldest = element->getMonotonicTime();
391 break;
392 }
393 }
394
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800395 stats.format(strp, uid, logMask, oldest);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800396
397 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800398}