blob: 5cab7a8c4383d40813d2fc22e0bb0aa9c9187ef8 [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
Mark Salyzyn671e3432014-05-06 07:34:59 -070017#include <ctype.h>
Mark Salyzyn202e1532015-02-09 08:21:05 -080018#include <errno.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080019#include <stdio.h>
20#include <string.h>
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070021#include <sys/cdefs.h>
Mark Salyzyn57a0af92014-05-09 17:44:18 -070022#include <sys/user.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080023#include <time.h>
24#include <unistd.h>
25
Mark Salyzyn511338d2015-05-19 09:12:30 -070026#include <unordered_map>
27
Mark Salyzyn671e3432014-05-06 07:34:59 -070028#include <cutils/properties.h>
Mark Salyzynf10e2732016-09-27 13:08:23 -070029#include <private/android_logger.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080030
31#include "LogBuffer.h"
Mark Salyzynb6bee332015-09-08 08:56:32 -070032#include "LogKlog.h"
Mark Salyzyn671e3432014-05-06 07:34:59 -070033#include "LogReader.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080034
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070035#ifndef __predict_false
36#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
37#endif
38
Mark Salyzyndfa7a072014-02-11 12:29:31 -080039// Default
Mark Salyzyndfa7a072014-02-11 12:29:31 -080040#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn671e3432014-05-06 07:34:59 -070041
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070042void LogBuffer::init() {
Mark Salyzyndfa7a072014-02-11 12:29:31 -080043 log_id_for_each(i) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -080044 mLastSet[i] = false;
45 mLast[i] = mLogElements.begin();
46
Mark Salyzynf10e2732016-09-27 13:08:23 -070047 if (setSize(i, __android_logger_get_buffer_size(i))) {
Mark Salyzyn57a0af92014-05-09 17:44:18 -070048 setSize(i, LOG_BUFFER_MIN_SIZE);
49 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -080050 }
Mark Salyzynb6bee332015-09-08 08:56:32 -070051 bool lastMonotonic = monotonic;
Mark Salyzynba7a9a02015-12-01 15:57:25 -080052 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzynb75cce02015-11-30 11:35:56 -080053 if (lastMonotonic != monotonic) {
54 //
55 // Fixup all timestamps, may not be 100% accurate, but better than
56 // throwing what we have away when we get 'surprised' by a change.
57 // In-place element fixup so no need to check reader-lock. Entries
58 // should already be in timestamp order, but we could end up with a
59 // few out-of-order entries if new monotonics come in before we
60 // are notified of the reinit change in status. A Typical example would
61 // be:
62 // --------- beginning of system
63 // 10.494082 184 201 D Cryptfs : Just triggered post_fs_data
64 // --------- beginning of kernel
65 // 0.000000 0 0 I : Initializing cgroup subsys
66 // as the act of mounting /data would trigger persist.logd.timestamp to
67 // be corrected. 1/30 corner case YMMV.
68 //
69 pthread_mutex_lock(&mLogElementsLock);
70 LogBufferElementCollection::iterator it = mLogElements.begin();
71 while((it != mLogElements.end())) {
72 LogBufferElement *e = *it;
73 if (monotonic) {
74 if (!android::isMonotonic(e->mRealTime)) {
75 LogKlog::convertRealToMonotonic(e->mRealTime);
76 }
77 } else {
78 if (android::isMonotonic(e->mRealTime)) {
79 LogKlog::convertMonotonicToReal(e->mRealTime);
80 }
81 }
82 ++it;
83 }
84 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzynb6bee332015-09-08 08:56:32 -070085 }
86
Mark Salyzynb75cce02015-11-30 11:35:56 -080087 // We may have been triggered by a SIGHUP. Release any sleeping reader
88 // threads to dump their current content.
Mark Salyzynb6bee332015-09-08 08:56:32 -070089 //
Mark Salyzynb75cce02015-11-30 11:35:56 -080090 // NB: this is _not_ performed in the context of a SIGHUP, it is
91 // performed during startup, and in context of reinit administrative thread
92 LogTimeEntry::lock();
93
94 LastLogTimes::iterator times = mTimes.begin();
95 while(times != mTimes.end()) {
96 LogTimeEntry *entry = (*times);
97 if (entry->owned_Locked()) {
98 entry->triggerReader_Locked();
Mark Salyzynb6bee332015-09-08 08:56:32 -070099 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800100 times++;
Mark Salyzynb6bee332015-09-08 08:56:32 -0700101 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800102
103 LogTimeEntry::unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800104}
105
Mark Salyzynb6bee332015-09-08 08:56:32 -0700106LogBuffer::LogBuffer(LastLogTimes *times):
Mark Salyzynba7a9a02015-12-01 15:57:25 -0800107 monotonic(android_log_clockid() == CLOCK_MONOTONIC),
Mark Salyzynb6bee332015-09-08 08:56:32 -0700108 mTimes(*times) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700109 pthread_mutex_init(&mLogElementsLock, NULL);
110
111 init();
112}
113
Mark Salyzyn202e1532015-02-09 08:21:05 -0800114int LogBuffer::log(log_id_t log_id, log_time realtime,
115 uid_t uid, pid_t pid, pid_t tid,
116 const char *msg, unsigned short len) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800117 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800118 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800119 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700120
Mark Salyzyn0175b072014-02-26 09:50:16 -0800121 LogBufferElement *elem = new LogBufferElement(log_id, realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700122 uid, pid, tid, msg, len);
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000123 if (log_id != LOG_ID_SECURITY) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800124 int prio = ANDROID_LOG_INFO;
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000125 const char *tag = NULL;
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700126 size_t len = 0;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800127 if (log_id == LOG_ID_EVENTS) {
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700128 tag = android::tagToName(&len, elem->getTag());
Mark Salyzyn083b0372015-12-04 10:59:45 -0800129 } else {
130 prio = *msg;
131 tag = msg + 1;
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700132 len = strlen(tag);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800133 }
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700134 if (!__android_log_is_loggable_len(prio, tag, len, ANDROID_LOG_VERBOSE)) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800135 // Log traffic received to total
136 pthread_mutex_lock(&mLogElementsLock);
137 stats.add(elem);
138 stats.subtract(elem);
139 pthread_mutex_unlock(&mLogElementsLock);
140 delete elem;
141 return -EACCES;
142 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700143 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800144
145 pthread_mutex_lock(&mLogElementsLock);
146
147 // Insert elements in time sorted order if possible
148 // NB: if end is region locked, place element at end of list
149 LogBufferElementCollection::iterator it = mLogElements.end();
150 LogBufferElementCollection::iterator last = it;
Mark Salyzyneae155e2014-10-13 16:49:47 -0700151 while (last != mLogElements.begin()) {
152 --it;
Mark Salyzync03e72c2014-02-18 11:23:53 -0800153 if ((*it)->getRealTime() <= realtime) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800154 break;
155 }
156 last = it;
157 }
Mark Salyzync03e72c2014-02-18 11:23:53 -0800158
Mark Salyzyn0175b072014-02-26 09:50:16 -0800159 if (last == mLogElements.end()) {
160 mLogElements.push_back(elem);
161 } else {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800162 uint64_t end = 1;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800163 bool end_set = false;
164 bool end_always = false;
165
166 LogTimeEntry::lock();
167
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700168 LastLogTimes::iterator times = mTimes.begin();
169 while(times != mTimes.end()) {
170 LogTimeEntry *entry = (*times);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800171 if (entry->owned_Locked()) {
172 if (!entry->mNonBlock) {
173 end_always = true;
174 break;
175 }
176 if (!end_set || (end <= entry->mEnd)) {
177 end = entry->mEnd;
178 end_set = true;
179 }
180 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700181 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800182 }
183
184 if (end_always
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800185 || (end_set && (end >= (*last)->getSequence()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800186 mLogElements.push_back(elem);
187 } else {
188 mLogElements.insert(last,elem);
189 }
190
191 LogTimeEntry::unlock();
192 }
193
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700194 stats.add(elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800195 maybePrune(log_id);
196 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn202e1532015-02-09 08:21:05 -0800197
198 return len;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800199}
200
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700201// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800202//
203// mLogElementsLock must be held when this function is called.
204void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800205 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700206 unsigned long maxSize = log_buffer_size(id);
207 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700208 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700209 size_t elements = stats.realElements(id);
210 size_t minElements = elements / 100;
211 if (minElements < minPrune) {
212 minElements = minPrune;
213 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700214 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700215 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700216 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800217 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700218 if (pruneRows > maxPrune) {
219 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700220 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800221 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800222 }
223}
224
Mark Salyzyn831aa292015-09-03 16:08:50 -0700225LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700226 LogBufferElementCollection::iterator it, bool coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700227 LogBufferElement *element = *it;
228 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700229
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700230 // Remove iterator references in the various lists that will become stale
231 // after the element is erased from the main logging list.
232
Mark Salyzyn6a066942016-07-14 15:34:30 -0700233 { // start of scope for found iterator
234 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
235 element->getTag() : element->getUid();
236 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
237 if ((found != mLastWorst[id].end()) && (it == found->second)) {
238 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700239 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700240 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700241
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700242 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700243 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700244 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
245 // long term code stability, find() check should be fast for those ids.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700246 LogBufferPidIteratorMap::iterator found =
247 mLastWorstPidOfSystem[id].find(element->getPid());
248 if ((found != mLastWorstPidOfSystem[id].end())
249 && (it == found->second)) {
250 mLastWorstPidOfSystem[id].erase(found);
251 }
252 }
253
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800254 bool setLast[LOG_ID_MAX];
255 bool doSetLast = false;
256 log_id_for_each(i) {
257 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
258 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700259 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800260 if (doSetLast) {
261 log_id_for_each(i) {
262 if (setLast[i]) {
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700263 if (__predict_false(it == mLogElements.end())) { // impossible
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800264 mLastSet[i] = false;
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700265 mLast[i] = mLogElements.begin();
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800266 } else {
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700267 mLast[i] = it; // push down the road as next-best-watermark
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800268 }
269 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800270 }
271 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700272 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700273 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700274 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700275 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700276 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700277 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700278
279 return it;
280}
281
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700282// Define a temporary mechanism to report the last LogBufferElement pointer
283// for the specified uid, pid and tid. Used below to help merge-sort when
284// pruning for worst UID.
285class LogBufferElementKey {
286 const union {
287 struct {
288 uint16_t uid;
289 uint16_t pid;
290 uint16_t tid;
291 uint16_t padding;
292 } __packed;
293 uint64_t value;
294 } __packed;
295
296public:
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700297 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid):
298 uid(uid),
299 pid(pid),
300 tid(tid),
301 padding(0) {
302 }
Chih-Hung Hsieh1cc82ce2016-04-25 13:49:46 -0700303 explicit LogBufferElementKey(uint64_t key):value(key) { }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700304
305 uint64_t getKey() { return value; }
306};
307
Mark Salyzyn511338d2015-05-19 09:12:30 -0700308class LogBufferElementLast {
309
310 typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap;
311 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700312
313public:
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700314
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700315 bool coalesce(LogBufferElement *element, unsigned short dropped) {
316 LogBufferElementKey key(element->getUid(),
317 element->getPid(),
318 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700319 LogBufferElementMap::iterator it = map.find(key.getKey());
320 if (it != map.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700321 LogBufferElement *found = it->second;
322 unsigned short moreDropped = found->getDropped();
323 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700324 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700325 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700326 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700327 return true;
328 }
329 }
330 return false;
331 }
332
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700333 void add(LogBufferElement *element) {
334 LogBufferElementKey key(element->getUid(),
335 element->getPid(),
336 element->getTid());
337 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700338 }
339
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700340 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700341 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700342 }
343
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700344 void clear(LogBufferElement *element) {
345 uint64_t current = element->getRealTime().nsec()
Mark Salyzyn047cc072015-06-04 13:35:30 -0700346 - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn511338d2015-05-19 09:12:30 -0700347 for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700348 LogBufferElement *mapElement = it->second;
349 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD)
350 && (current > mapElement->getRealTime().nsec())) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700351 it = map.erase(it);
352 } else {
353 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700354 }
355 }
356 }
357
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700358};
359
Mark Salyzyn0175b072014-02-26 09:50:16 -0800360// prune "pruneRows" of type "id" from the buffer.
361//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700362// This garbage collection task is used to expire log entries. It is called to
363// remove all logs (clear), all UID logs (unprivileged clear), or every
364// 256 or 10% of the total logs (whichever is less) to prune the logs.
365//
366// First there is a prep phase where we discover the reader region lock that
367// acts as a backstop to any pruning activity to stop there and go no further.
368//
369// There are three major pruning loops that follow. All expire from the oldest
370// entries. Since there are multiple log buffers, the Android logging facility
371// will appear to drop entries 'in the middle' when looking at multiple log
372// sources and buffers. This effect is slightly more prominent when we prune
373// the worst offender by logging source. Thus the logs slowly loose content
374// and value as you move back in time. This is preferred since chatty sources
375// invariably move the logs value down faster as less chatty sources would be
376// expired in the noise.
377//
378// The first loop performs blacklisting and worst offender pruning. Falling
379// through when there are no notable worst offenders and have not hit the
380// region lock preventing further worst offender pruning. This loop also looks
381// after managing the chatty log entries and merging to help provide
382// statistical basis for blame. The chatty entries are not a notification of
383// how much logs you may have, but instead represent how much logs you would
384// have had in a virtual log buffer that is extended to cover all the in-memory
385// logs without loss. They last much longer than the represented pruned logs
386// since they get multiplied by the gains in the non-chatty log sources.
387//
388// The second loop get complicated because an algorithm of watermarks and
389// history is maintained to reduce the order and keep processing time
390// down to a minimum at scale. These algorithms can be costly in the face
391// of larger log buffers, or severly limited processing time granted to a
392// background task at lowest priority.
393//
394// This second loop does straight-up expiration from the end of the logs
395// (again, remember for the specified log buffer id) but does some whitelist
396// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
397// spam filtration all take priority. This second loop also checks if a region
398// lock is causing us to buffer too much in the logs to help the reader(s),
399// and will tell the slowest reader thread to skip log entries, and if
400// persistent and hits a further threshold, kill the reader thread.
401//
402// The third thread is optional, and only gets hit if there was a whitelist
403// and more needs to be pruned against the backstop of the region lock.
404//
Mark Salyzyn0175b072014-02-26 09:50:16 -0800405// mLogElementsLock must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700406//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700407bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800408 LogTimeEntry *oldest = NULL;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700409 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700410 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800411
412 LogTimeEntry::lock();
413
414 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700415 LastLogTimes::iterator times = mTimes.begin();
416 while(times != mTimes.end()) {
417 LogTimeEntry *entry = (*times);
TraianX Schiauda6495d2014-12-17 10:53:41 +0200418 if (entry->owned_Locked() && entry->isWatching(id)
Mark Salyzynb75cce02015-11-30 11:35:56 -0800419 && (!oldest ||
420 (oldest->mStart > entry->mStart) ||
421 ((oldest->mStart == entry->mStart) &&
422 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800423 oldest = entry;
424 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700425 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800426 }
427
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800428 LogBufferElementCollection::iterator it;
429
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700430 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700431 // Only here if clear all request from non system source, so chatty
432 // filter logistics is not required.
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800433 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
434 while (it != mLogElements.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700435 LogBufferElement *element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700436
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700437 if ((element->getLogId() != id) || (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700438 ++it;
439 continue;
440 }
441
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800442 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
443 mLast[id] = it;
444 mLastSet[id] = true;
445 }
446
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700447 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700448 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800449 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
450 oldest->triggerReader_Locked();
451 } else {
452 oldest->triggerSkip_Locked(id, pruneRows);
453 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700454 break;
455 }
456
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700457 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700458 if (--pruneRows == 0) {
459 break;
460 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700461 }
462 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700463 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700464 }
465
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700466 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn083b0372015-12-04 10:59:45 -0800467 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700468 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800469 // recalculate the worst offender on every batched pass
Mark Salyzyn6a066942016-07-14 15:34:30 -0700470 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800471 size_t worst_sizes = 0;
472 size_t second_worst_sizes = 0;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700473 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800474
Mark Salyzynae769232015-03-17 17:17:25 -0700475 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700476 // Calculate threshold as 12.5% of available storage
477 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700478
Mark Salyzyn6a066942016-07-14 15:34:30 -0700479 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
480 stats.sortTags(AID_ROOT, (pid_t)0, 2, id).findWorst(
481 worst, worst_sizes, second_worst_sizes, threshold);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700482 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700483 } else {
484 stats.sort(AID_ROOT, (pid_t)0, 2, id).findWorst(
485 worst, worst_sizes, second_worst_sizes, threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700486
Mark Salyzyn6a066942016-07-14 15:34:30 -0700487 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
488 stats.sortPids(worst, (pid_t)0, 2, id).findWorst(
489 worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700490 }
491 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800492 }
493
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700494 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700495 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700496 break;
497 }
498
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800499 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700500 bool leading = true;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800501 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700502 // Perform at least one mandatory garbage collection cycle in following
503 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700504 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700505 // - check age-out of preserved logs
506 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700507 if (!gc && (worst != -1)) {
508 { // begin scope for worst found iterator
509 LogBufferIteratorMap::iterator found = mLastWorst[id].find(worst);
510 if ((found != mLastWorst[id].end())
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700511 && (found->second != mLogElements.end())) {
512 leading = false;
513 it = found->second;
514 }
515 }
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700516 if (worstPid) { // begin scope for pid worst found iterator
517 // FYI: worstPid only set if !LOG_ID_EVENTS and
518 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700519 LogBufferPidIteratorMap::iterator found
520 = mLastWorstPidOfSystem[id].find(worstPid);
521 if ((found != mLastWorstPidOfSystem[id].end())
522 && (found->second != mLogElements.end())) {
523 leading = false;
524 it = found->second;
525 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700526 }
527 }
Mark Salyzynccfe8442015-08-24 13:43:27 -0700528 static const timespec too_old = {
529 EXPIRE_HOUR_THRESHOLD * 60 * 60, 0
530 };
531 LogBufferElementCollection::iterator lastt;
532 lastt = mLogElements.end();
533 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700534 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700535 while (it != mLogElements.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700536 LogBufferElement *element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800537
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700538 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700539 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800540 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
541 oldest->triggerReader_Locked();
542 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800543 break;
544 }
545
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700546 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800547 ++it;
548 continue;
549 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700550 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800551
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800552 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
553 mLast[id] = it;
554 mLastSet[id] = true;
555 }
556
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700557 unsigned short dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800558
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700559 // remove any leading drops
560 if (leading && dropped) {
561 it = erase(it);
562 continue;
563 }
564
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700565 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700566 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700567 continue;
568 }
569
Mark Salyzyn6a066942016-07-14 15:34:30 -0700570 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
571 element->getTag() :
572 element->getUid();
573
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700574 if (hasBlacklist && mPrune.naughty(element)) {
575 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700576 it = erase(it);
577 if (dropped) {
578 continue;
579 }
580
581 pruneRows--;
582 if (pruneRows == 0) {
583 break;
584 }
585
Mark Salyzyn6a066942016-07-14 15:34:30 -0700586 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700587 kick = true;
588 if (worst_sizes < second_worst_sizes) {
589 break;
590 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700591 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700592 }
593 continue;
594 }
595
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700596 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old))
597 || (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700598 break;
599 }
600
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700601 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700602 last.add(element);
603 if (worstPid
604 && ((!gc && (element->getPid() == worstPid))
Mark Salyzyn6a066942016-07-14 15:34:30 -0700605 || (mLastWorstPidOfSystem[id].find(element->getPid())
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700606 == mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700607 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700608 // watermark if current one empty. id is not LOG_ID_EVENTS
609 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700610 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700611 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700612 if ((!gc && !worstPid && (key == worst))
613 || (mLastWorst[id].find(key) == mLastWorst[id].end())) {
614 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700615 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800616 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700617 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800618 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700619
Mark Salyzyn6a066942016-07-14 15:34:30 -0700620 if ((key != worst)
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700621 || (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700622 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700623 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700624 ++it;
625 continue;
626 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700627 // key == worst below here
628 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700629
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700630 pruneRows--;
631 if (pruneRows == 0) {
632 break;
633 }
634
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700635 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700636
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700637 unsigned short len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700638
639 // do not create any leading drops
640 if (leading) {
641 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700642 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700643 stats.drop(element);
644 element->setDropped(1);
645 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700646 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700647 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700648 last.add(element);
649 if (worstPid && (!gc
650 || (mLastWorstPidOfSystem[id].find(worstPid)
651 == mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700652 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700653 // watermark if current one empty. id is not
654 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700655 mLastWorstPidOfSystem[id][worstPid] = it;
656 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700657 if ((!gc && !worstPid) ||
658 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
659 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700660 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700661 ++it;
662 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700663 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700664 if (worst_sizes < second_worst_sizes) {
665 break;
666 }
667 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800668 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700669 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800670
Mark Salyzyn1c950472014-04-01 17:19:47 -0700671 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800672 break; // the following loop will ask bad clients to skip/drop
673 }
674 }
675
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800676 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800677 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800678 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800679 while((pruneRows > 0) && (it != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700680 LogBufferElement *element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700681
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700682 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700683 it++;
684 continue;
685 }
686
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800687 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
688 mLast[id] = it;
689 mLastSet[id] = true;
690 }
691
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700692 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700693 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700694 if (whitelist) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800695 break;
696 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700697
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700698 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
699 // kick a misbehaving log reader client off the island
700 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800701 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
702 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700703 } else {
704 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800705 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700706 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800707 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700708
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700709 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
710 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700711 whitelist = true;
712 it++;
713 continue;
714 }
715
716 it = erase(it);
717 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800718 }
719
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700720 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800721 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800722 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800723 while((it != mLogElements.end()) && (pruneRows > 0)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700724 LogBufferElement *element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700725
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700726 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700727 ++it;
728 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800729 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700730
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800731 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
732 mLast[id] = it;
733 mLastSet[id] = true;
734 }
735
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700736 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700737 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700738 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
739 // kick a misbehaving log reader client off the island
740 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800741 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
742 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700743 } else {
744 oldest->triggerSkip_Locked(id, pruneRows);
745 }
746 break;
747 }
748
749 it = erase(it);
750 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800751 }
752 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800753
Mark Salyzyn0175b072014-02-26 09:50:16 -0800754 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700755
756 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800757}
758
759// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -0700760bool LogBuffer::clear(log_id_t id, uid_t uid) {
761 bool busy = true;
762 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
763 for (int retry = 4;;) {
764 if (retry == 1) { // last pass
765 // Check if it is still busy after the sleep, we say prune
766 // one entry, not another clear run, so we are looking for
767 // the quick side effect of the return value to tell us if
768 // we have a _blocked_ reader.
769 pthread_mutex_lock(&mLogElementsLock);
770 busy = prune(id, 1, uid);
771 pthread_mutex_unlock(&mLogElementsLock);
772 // It is still busy, blocked reader(s), lets kill them all!
773 // otherwise, lets be a good citizen and preserve the slow
774 // readers and let the clear run (below) deal with determining
775 // if we are still blocked and return an error code to caller.
776 if (busy) {
777 LogTimeEntry::lock();
778 LastLogTimes::iterator times = mTimes.begin();
779 while (times != mTimes.end()) {
780 LogTimeEntry *entry = (*times);
781 // Killer punch
782 if (entry->owned_Locked() && entry->isWatching(id)) {
783 entry->release_Locked();
784 }
785 times++;
786 }
787 LogTimeEntry::unlock();
788 }
789 }
790 pthread_mutex_lock(&mLogElementsLock);
791 busy = prune(id, ULONG_MAX, uid);
792 pthread_mutex_unlock(&mLogElementsLock);
793 if (!busy || !--retry) {
794 break;
795 }
796 sleep (1); // Let reader(s) catch up after notification
797 }
798 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800799}
800
801// get the used space associated with "id".
802unsigned long LogBuffer::getSizeUsed(log_id_t id) {
803 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800804 size_t retval = stats.sizes(id);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800805 pthread_mutex_unlock(&mLogElementsLock);
806 return retval;
807}
808
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800809// set the total space allocated to "id"
810int LogBuffer::setSize(log_id_t id, unsigned long size) {
811 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -0700812 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800813 return -1;
814 }
815 pthread_mutex_lock(&mLogElementsLock);
816 log_buffer_size(id) = size;
817 pthread_mutex_unlock(&mLogElementsLock);
818 return 0;
819}
820
821// get the total space allocated to "id"
822unsigned long LogBuffer::getSize(log_id_t id) {
823 pthread_mutex_lock(&mLogElementsLock);
824 size_t retval = log_buffer_size(id);
825 pthread_mutex_unlock(&mLogElementsLock);
826 return retval;
827}
828
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800829uint64_t LogBuffer::flushTo(
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800830 SocketClient *reader, const uint64_t start,
831 bool privileged, bool security,
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800832 int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800833 LogBufferElementCollection::iterator it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800834 uint64_t max = start;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800835 uid_t uid = reader->getUid();
836
837 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600838
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800839 if (start <= 1) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600840 // client wants to start from the beginning
841 it = mLogElements.begin();
842 } else {
843 // Client wants to start from some specified time. Chances are
844 // we are better off starting from the end of the time sorted list.
845 for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
846 --it;
847 LogBufferElement *element = *it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800848 if (element->getSequence() <= start) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600849 it++;
850 break;
851 }
852 }
853 }
854
855 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800856 LogBufferElement *element = *it;
857
858 if (!privileged && (element->getUid() != uid)) {
859 continue;
860 }
861
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800862 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
863 continue;
864 }
865
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800866 if (element->getSequence() <= start) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800867 continue;
868 }
869
870 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800871 if (filter) {
872 int ret = (*filter)(element, arg);
873 if (ret == false) {
874 continue;
875 }
876 if (ret != true) {
877 break;
878 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800879 }
880
881 pthread_mutex_unlock(&mLogElementsLock);
882
883 // range locking in LastLogTimes looks after us
Mark Salyzyn7b873652015-12-03 15:38:35 -0800884 max = element->flushTo(reader, this, privileged);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800885
886 if (max == element->FLUSH_ERROR) {
887 return max;
888 }
889
890 pthread_mutex_lock(&mLogElementsLock);
891 }
892 pthread_mutex_unlock(&mLogElementsLock);
893
894 return max;
895}
Mark Salyzyn34facab2014-02-06 14:48:50 -0800896
Mark Salyzynee3b8382015-12-17 09:58:43 -0800897std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
898 unsigned int logMask) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800899 pthread_mutex_lock(&mLogElementsLock);
900
Mark Salyzynee3b8382015-12-17 09:58:43 -0800901 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800902
903 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn73160ac2015-08-20 10:01:44 -0700904
905 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800906}