blob: 38a237c5d20232160fe2029011ce8ddb2ed56bef [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#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn0175b072014-02-26 09:50:16 -080032
33LogBuffer::LogBuffer(LastLogTimes *times)
34 : mTimes(*times) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080035 pthread_mutex_init(&mLogElementsLock, NULL);
Mark Salyzyne457b742014-02-19 17:18:31 -080036 dgram_qlen_statistics = false;
37
Mark Salyzyndfa7a072014-02-11 12:29:31 -080038 log_id_for_each(i) {
39 mMaxSize[i] = LOG_BUFFER_SIZE;
40 }
Mark Salyzyn0175b072014-02-26 09:50:16 -080041}
42
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -080043void LogBuffer::log(log_id_t log_id, log_time realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -070044 uid_t uid, pid_t pid, pid_t tid,
45 const char *msg, unsigned short len) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080046 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
47 return;
48 }
49 LogBufferElement *elem = new LogBufferElement(log_id, realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -070050 uid, pid, tid, msg, len);
Mark Salyzyn0175b072014-02-26 09:50:16 -080051
52 pthread_mutex_lock(&mLogElementsLock);
53
54 // Insert elements in time sorted order if possible
55 // NB: if end is region locked, place element at end of list
56 LogBufferElementCollection::iterator it = mLogElements.end();
57 LogBufferElementCollection::iterator last = it;
58 while (--it != mLogElements.begin()) {
Mark Salyzync03e72c2014-02-18 11:23:53 -080059 if ((*it)->getRealTime() <= realtime) {
Mark Salyzyne457b742014-02-19 17:18:31 -080060 // halves the peak performance, use with caution
61 if (dgram_qlen_statistics) {
62 LogBufferElementCollection::iterator ib = it;
63 unsigned short buckets, num = 1;
64 for (unsigned short i = 0; (buckets = stats.dgram_qlen(i)); ++i) {
65 buckets -= num;
66 num += buckets;
67 while (buckets && (--ib != mLogElements.begin())) {
68 --buckets;
69 }
70 if (buckets) {
71 break;
72 }
73 stats.recordDiff(
74 elem->getRealTime() - (*ib)->getRealTime(), i);
75 }
76 }
Mark Salyzyn0175b072014-02-26 09:50:16 -080077 break;
78 }
79 last = it;
80 }
Mark Salyzync03e72c2014-02-18 11:23:53 -080081
Mark Salyzyn0175b072014-02-26 09:50:16 -080082 if (last == mLogElements.end()) {
83 mLogElements.push_back(elem);
84 } else {
85 log_time end;
86 bool end_set = false;
87 bool end_always = false;
88
89 LogTimeEntry::lock();
90
91 LastLogTimes::iterator t = mTimes.begin();
92 while(t != mTimes.end()) {
93 LogTimeEntry *entry = (*t);
94 if (entry->owned_Locked()) {
95 if (!entry->mNonBlock) {
96 end_always = true;
97 break;
98 }
99 if (!end_set || (end <= entry->mEnd)) {
100 end = entry->mEnd;
101 end_set = true;
102 }
103 }
104 t++;
105 }
106
107 if (end_always
Mark Salyzync03e72c2014-02-18 11:23:53 -0800108 || (end_set && (end >= (*last)->getMonotonicTime()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800109 mLogElements.push_back(elem);
110 } else {
111 mLogElements.insert(last,elem);
112 }
113
114 LogTimeEntry::unlock();
115 }
116
Mark Salyzyn34facab2014-02-06 14:48:50 -0800117 stats.add(len, log_id, uid, pid);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800118 maybePrune(log_id);
119 pthread_mutex_unlock(&mLogElementsLock);
120}
121
122// If we're using more than 256K of memory for log entries, prune
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800123// at least 10% of the log entries.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800124//
125// mLogElementsLock must be held when this function is called.
126void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800127 size_t sizes = stats.sizes(id);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800128 if (sizes > log_buffer_size(id)) {
129 size_t sizeOver90Percent = sizes - ((log_buffer_size(id) * 9) / 10);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800130 size_t elements = stats.elements(id);
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800131 unsigned long pruneRows = elements * sizeOver90Percent / sizes;
132 elements /= 10;
133 if (pruneRows <= elements) {
134 pruneRows = elements;
135 }
136 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800137 }
138}
139
140// prune "pruneRows" of type "id" from the buffer.
141//
142// mLogElementsLock must be held when this function is called.
143void LogBuffer::prune(log_id_t id, unsigned long pruneRows) {
144 LogTimeEntry *oldest = NULL;
145
146 LogTimeEntry::lock();
147
148 // Region locked?
149 LastLogTimes::iterator t = mTimes.begin();
150 while(t != mTimes.end()) {
151 LogTimeEntry *entry = (*t);
152 if (entry->owned_Locked()
153 && (!oldest || (oldest->mStart > entry->mStart))) {
154 oldest = entry;
155 }
156 t++;
157 }
158
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800159 LogBufferElementCollection::iterator it;
160
161 // prune by worst offender by uid
162 while (pruneRows > 0) {
163 // recalculate the worst offender on every batched pass
164 uid_t worst = (uid_t) -1;
165 size_t worst_sizes = 0;
166 size_t second_worst_sizes = 0;
167
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700168 if ((id != LOG_ID_CRASH) && mPrune.worstUidEnabled()) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800169 LidStatistics &l = stats.id(id);
Mark Salyzync8a576c2014-04-04 16:35:59 -0700170 l.sort();
171 UidStatisticsCollection::iterator iu = l.begin();
172 if (iu != l.end()) {
173 UidStatistics *u = *iu;
174 worst = u->getUid();
175 worst_sizes = u->sizes();
176 if (++iu != l.end()) {
177 second_worst_sizes = (*iu)->sizes();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800178 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800179 }
180 }
181
182 bool kick = false;
183 for(it = mLogElements.begin(); it != mLogElements.end();) {
184 LogBufferElement *e = *it;
185
186 if (oldest && (oldest->mStart <= e->getMonotonicTime())) {
187 break;
188 }
189
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800190 if (e->getLogId() != id) {
191 ++it;
192 continue;
193 }
194
195 uid_t uid = e->getUid();
196
197 if (uid == worst) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800198 it = mLogElements.erase(it);
199 unsigned short len = e->getMsgLen();
200 stats.subtract(len, id, worst, e->getPid());
201 delete e;
202 kick = true;
203 pruneRows--;
204 if ((pruneRows == 0) || (worst_sizes < second_worst_sizes)) {
205 break;
206 }
207 worst_sizes -= len;
Mark Salyzyn1c950472014-04-01 17:19:47 -0700208 } else if (mPrune.naughty(e)) { // BlackListed
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800209 it = mLogElements.erase(it);
210 stats.subtract(e->getMsgLen(), id, uid, e->getPid());
211 delete e;
212 pruneRows--;
213 if (pruneRows == 0) {
214 break;
215 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700216 } else {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800217 ++it;
218 }
219 }
220
Mark Salyzyn1c950472014-04-01 17:19:47 -0700221 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800222 break; // the following loop will ask bad clients to skip/drop
223 }
224 }
225
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800226 bool whitelist = false;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800227 it = mLogElements.begin();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800228 while((pruneRows > 0) && (it != mLogElements.end())) {
229 LogBufferElement *e = *it;
230 if (e->getLogId() == id) {
231 if (oldest && (oldest->mStart <= e->getMonotonicTime())) {
Mark Salyzyn1c950472014-04-01 17:19:47 -0700232 if (!whitelist) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800233 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
234 // kick a misbehaving log reader client off the island
235 oldest->release_Locked();
236 } else {
237 oldest->triggerSkip_Locked(pruneRows);
238 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800239 }
240 break;
241 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700242
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800243 if (mPrune.nice(e)) { // WhiteListed
244 whitelist = true;
245 it++;
246 continue;
247 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700248
Mark Salyzyn0175b072014-02-26 09:50:16 -0800249 it = mLogElements.erase(it);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800250 stats.subtract(e->getMsgLen(), id, e->getUid(), e->getPid());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800251 delete e;
252 pruneRows--;
253 } else {
254 it++;
255 }
256 }
257
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800258 if (whitelist && (pruneRows > 0)) {
259 it = mLogElements.begin();
260 while((it != mLogElements.end()) && (pruneRows > 0)) {
261 LogBufferElement *e = *it;
262 if (e->getLogId() == id) {
263 if (oldest && (oldest->mStart <= e->getMonotonicTime())) {
264 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
265 // kick a misbehaving log reader client off the island
266 oldest->release_Locked();
267 } else {
268 oldest->triggerSkip_Locked(pruneRows);
269 }
270 break;
271 }
272 it = mLogElements.erase(it);
273 stats.subtract(e->getMsgLen(), id, e->getUid(), e->getPid());
274 delete e;
275 pruneRows--;
276 } else {
277 it++;
278 }
279 }
280 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800281
Mark Salyzyn0175b072014-02-26 09:50:16 -0800282 LogTimeEntry::unlock();
283}
284
285// clear all rows of type "id" from the buffer.
286void LogBuffer::clear(log_id_t id) {
287 pthread_mutex_lock(&mLogElementsLock);
288 prune(id, ULONG_MAX);
289 pthread_mutex_unlock(&mLogElementsLock);
290}
291
292// get the used space associated with "id".
293unsigned long LogBuffer::getSizeUsed(log_id_t id) {
294 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800295 size_t retval = stats.sizes(id);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800296 pthread_mutex_unlock(&mLogElementsLock);
297 return retval;
298}
299
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800300// set the total space allocated to "id"
301int LogBuffer::setSize(log_id_t id, unsigned long size) {
302 // Reasonable limits ...
303 if ((size < (64 * 1024)) || ((256 * 1024 * 1024) < size)) {
304 return -1;
305 }
306 pthread_mutex_lock(&mLogElementsLock);
307 log_buffer_size(id) = size;
308 pthread_mutex_unlock(&mLogElementsLock);
309 return 0;
310}
311
312// get the total space allocated to "id"
313unsigned long LogBuffer::getSize(log_id_t id) {
314 pthread_mutex_lock(&mLogElementsLock);
315 size_t retval = log_buffer_size(id);
316 pthread_mutex_unlock(&mLogElementsLock);
317 return retval;
318}
319
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -0800320log_time LogBuffer::flushTo(
321 SocketClient *reader, const log_time start, bool privileged,
Mark Salyzyn0175b072014-02-26 09:50:16 -0800322 bool (*filter)(const LogBufferElement *element, void *arg), void *arg) {
323 LogBufferElementCollection::iterator it;
324 log_time max = start;
325 uid_t uid = reader->getUid();
326
327 pthread_mutex_lock(&mLogElementsLock);
328 for (it = mLogElements.begin(); it != mLogElements.end(); ++it) {
329 LogBufferElement *element = *it;
330
331 if (!privileged && (element->getUid() != uid)) {
332 continue;
333 }
334
335 if (element->getMonotonicTime() <= start) {
336 continue;
337 }
338
339 // NB: calling out to another object with mLogElementsLock held (safe)
340 if (filter && !(*filter)(element, arg)) {
341 continue;
342 }
343
344 pthread_mutex_unlock(&mLogElementsLock);
345
346 // range locking in LastLogTimes looks after us
347 max = element->flushTo(reader);
348
349 if (max == element->FLUSH_ERROR) {
350 return max;
351 }
352
353 pthread_mutex_lock(&mLogElementsLock);
354 }
355 pthread_mutex_unlock(&mLogElementsLock);
356
357 return max;
358}
Mark Salyzyn34facab2014-02-06 14:48:50 -0800359
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800360void LogBuffer::formatStatistics(char **strp, uid_t uid, unsigned int logMask) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800361 log_time oldest(CLOCK_MONOTONIC);
362
363 pthread_mutex_lock(&mLogElementsLock);
364
365 // Find oldest element in the log(s)
366 LogBufferElementCollection::iterator it;
367 for (it = mLogElements.begin(); it != mLogElements.end(); ++it) {
368 LogBufferElement *element = *it;
369
370 if ((logMask & (1 << element->getLogId()))) {
371 oldest = element->getMonotonicTime();
372 break;
373 }
374 }
375
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800376 stats.format(strp, uid, logMask, oldest);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800377
378 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800379}