blob: e16aa69d93f4690a7eb947e0d1c2ae2b2822c81d [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
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 Salyzynd774bce2014-02-06 14:48:50 -080025#include "LogStatistics.h"
Mark Salyzyn12bac902014-02-26 09:50:16 -080026#include "LogReader.h"
27
28#define LOG_BUFFER_SIZE (256 * 1024) // Tuned on a per-platform basis here?
29
30LogBuffer::LogBuffer(LastLogTimes *times)
31 : mTimes(*times) {
Mark Salyzyn12bac902014-02-26 09:50:16 -080032 pthread_mutex_init(&mLogElementsLock, NULL);
33}
34
Mark Salyzyn71391172014-03-05 07:41:49 -080035void LogBuffer::log(log_id_t log_id, log_time realtime,
Mark Salyzyn12bac902014-02-26 09:50:16 -080036 uid_t uid, pid_t pid, const char *msg,
37 unsigned short len) {
38 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
39 return;
40 }
41 LogBufferElement *elem = new LogBufferElement(log_id, realtime,
42 uid, pid, msg, len);
43
44 pthread_mutex_lock(&mLogElementsLock);
45
46 // Insert elements in time sorted order if possible
47 // NB: if end is region locked, place element at end of list
48 LogBufferElementCollection::iterator it = mLogElements.end();
49 LogBufferElementCollection::iterator last = it;
50 while (--it != mLogElements.begin()) {
Mark Salyzyn5b7a8d82014-02-18 11:23:53 -080051 if ((*it)->getRealTime() <= realtime) {
Mark Salyzyn12bac902014-02-26 09:50:16 -080052 break;
53 }
54 last = it;
55 }
Mark Salyzyn5b7a8d82014-02-18 11:23:53 -080056
Mark Salyzyn12bac902014-02-26 09:50:16 -080057 if (last == mLogElements.end()) {
58 mLogElements.push_back(elem);
59 } else {
60 log_time end;
61 bool end_set = false;
62 bool end_always = false;
63
64 LogTimeEntry::lock();
65
66 LastLogTimes::iterator t = mTimes.begin();
67 while(t != mTimes.end()) {
68 LogTimeEntry *entry = (*t);
69 if (entry->owned_Locked()) {
70 if (!entry->mNonBlock) {
71 end_always = true;
72 break;
73 }
74 if (!end_set || (end <= entry->mEnd)) {
75 end = entry->mEnd;
76 end_set = true;
77 }
78 }
79 t++;
80 }
81
82 if (end_always
Mark Salyzyn5b7a8d82014-02-18 11:23:53 -080083 || (end_set && (end >= (*last)->getMonotonicTime()))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -080084 mLogElements.push_back(elem);
85 } else {
86 mLogElements.insert(last,elem);
87 }
88
89 LogTimeEntry::unlock();
90 }
91
Mark Salyzynd774bce2014-02-06 14:48:50 -080092 stats.add(len, log_id, uid, pid);
Mark Salyzyn12bac902014-02-26 09:50:16 -080093 maybePrune(log_id);
94 pthread_mutex_unlock(&mLogElementsLock);
95}
96
97// If we're using more than 256K of memory for log entries, prune
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -080098// at least 10% of the log entries.
Mark Salyzyn12bac902014-02-26 09:50:16 -080099//
100// mLogElementsLock must be held when this function is called.
101void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800102 size_t sizes = stats.sizes(id);
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800103 if (sizes > LOG_BUFFER_SIZE) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800104 size_t sizeOver90Percent = sizes - ((LOG_BUFFER_SIZE * 9) / 10);
105 size_t elements = stats.elements(id);
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800106 unsigned long pruneRows = elements * sizeOver90Percent / sizes;
107 elements /= 10;
108 if (pruneRows <= elements) {
109 pruneRows = elements;
110 }
111 prune(id, pruneRows);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800112 }
113}
114
115// prune "pruneRows" of type "id" from the buffer.
116//
117// mLogElementsLock must be held when this function is called.
118void LogBuffer::prune(log_id_t id, unsigned long pruneRows) {
119 LogTimeEntry *oldest = NULL;
120
121 LogTimeEntry::lock();
122
123 // Region locked?
124 LastLogTimes::iterator t = mTimes.begin();
125 while(t != mTimes.end()) {
126 LogTimeEntry *entry = (*t);
127 if (entry->owned_Locked()
128 && (!oldest || (oldest->mStart > entry->mStart))) {
129 oldest = entry;
130 }
131 t++;
132 }
133
134 LogBufferElementCollection::iterator it = mLogElements.begin();
135 while((pruneRows > 0) && (it != mLogElements.end())) {
136 LogBufferElement *e = *it;
137 if (e->getLogId() == id) {
138 if (oldest && (oldest->mStart <= e->getMonotonicTime())) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800139 if (stats.sizes(id) > (2 * LOG_BUFFER_SIZE)) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800140 // kick a misbehaving log reader client off the island
141 oldest->release_Locked();
142 } else {
143 oldest->triggerSkip_Locked(pruneRows);
144 }
145 break;
146 }
147 it = mLogElements.erase(it);
Mark Salyzynd774bce2014-02-06 14:48:50 -0800148 stats.subtract(e->getMsgLen(), id, e->getUid(), e->getPid());
Mark Salyzyn12bac902014-02-26 09:50:16 -0800149 delete e;
150 pruneRows--;
151 } else {
152 it++;
153 }
154 }
155
156 LogTimeEntry::unlock();
157}
158
159// clear all rows of type "id" from the buffer.
160void LogBuffer::clear(log_id_t id) {
161 pthread_mutex_lock(&mLogElementsLock);
162 prune(id, ULONG_MAX);
163 pthread_mutex_unlock(&mLogElementsLock);
164}
165
166// get the used space associated with "id".
167unsigned long LogBuffer::getSizeUsed(log_id_t id) {
168 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzynd774bce2014-02-06 14:48:50 -0800169 size_t retval = stats.sizes(id);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800170 pthread_mutex_unlock(&mLogElementsLock);
171 return retval;
172}
173
174// get the total space allocated to "id"
175unsigned long LogBuffer::getSize(log_id_t /*id*/) {
176 return LOG_BUFFER_SIZE;
177}
178
Mark Salyzyn71391172014-03-05 07:41:49 -0800179log_time LogBuffer::flushTo(
180 SocketClient *reader, const log_time start, bool privileged,
Mark Salyzyn12bac902014-02-26 09:50:16 -0800181 bool (*filter)(const LogBufferElement *element, void *arg), void *arg) {
182 LogBufferElementCollection::iterator it;
183 log_time max = start;
184 uid_t uid = reader->getUid();
185
186 pthread_mutex_lock(&mLogElementsLock);
187 for (it = mLogElements.begin(); it != mLogElements.end(); ++it) {
188 LogBufferElement *element = *it;
189
190 if (!privileged && (element->getUid() != uid)) {
191 continue;
192 }
193
194 if (element->getMonotonicTime() <= start) {
195 continue;
196 }
197
198 // NB: calling out to another object with mLogElementsLock held (safe)
199 if (filter && !(*filter)(element, arg)) {
200 continue;
201 }
202
203 pthread_mutex_unlock(&mLogElementsLock);
204
205 // range locking in LastLogTimes looks after us
206 max = element->flushTo(reader);
207
208 if (max == element->FLUSH_ERROR) {
209 return max;
210 }
211
212 pthread_mutex_lock(&mLogElementsLock);
213 }
214 pthread_mutex_unlock(&mLogElementsLock);
215
216 return max;
217}
Mark Salyzynd774bce2014-02-06 14:48:50 -0800218
219size_t LogBuffer::formatStatistics(char **strp, uid_t uid, unsigned int logMask) {
220 log_time oldest(CLOCK_MONOTONIC);
221
222 pthread_mutex_lock(&mLogElementsLock);
223
224 // Find oldest element in the log(s)
225 LogBufferElementCollection::iterator it;
226 for (it = mLogElements.begin(); it != mLogElements.end(); ++it) {
227 LogBufferElement *element = *it;
228
229 if ((logMask & (1 << element->getLogId()))) {
230 oldest = element->getMonotonicTime();
231 break;
232 }
233 }
234
235 size_t ret = stats.format(strp, uid, logMask, oldest);
236
237 pthread_mutex_unlock(&mLogElementsLock);
238
239 return ret;
240}