blob: 749832530394e5c7cb9d9551483f7ec853abc583 [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 */
Mark Salyzyn60636fa2016-10-24 16:22:17 -070016// for manual checking of stale entries during LogBuffer::erase()
17//#define DEBUG_CHECK_FOR_STALE_ENTRIES
Mark Salyzyn0175b072014-02-26 09:50:16 -080018
Mark Salyzyn671e3432014-05-06 07:34:59 -070019#include <ctype.h>
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -080020#include <endian.h>
Mark Salyzyn202e1532015-02-09 08:21:05 -080021#include <errno.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080022#include <stdio.h>
23#include <string.h>
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070024#include <sys/cdefs.h>
Mark Salyzyn57a0af92014-05-09 17:44:18 -070025#include <sys/user.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080026#include <time.h>
27#include <unistd.h>
28
Mark Salyzyn511338d2015-05-19 09:12:30 -070029#include <unordered_map>
30
Mark Salyzyn671e3432014-05-06 07:34:59 -070031#include <cutils/properties.h>
Mark Salyzynf10e2732016-09-27 13:08:23 -070032#include <private/android_logger.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080033
34#include "LogBuffer.h"
Mark Salyzynb6bee332015-09-08 08:56:32 -070035#include "LogKlog.h"
Mark Salyzyn671e3432014-05-06 07:34:59 -070036#include "LogReader.h"
Mark Salyzyna2c02222016-12-13 10:31:29 -080037#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080038
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070039#ifndef __predict_false
40#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
41#endif
42
Mark Salyzyndfa7a072014-02-11 12:29:31 -080043// Default
Mark Salyzyndfa7a072014-02-11 12:29:31 -080044#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn671e3432014-05-06 07:34:59 -070045
Mark Salyzyn58363792017-04-17 12:46:12 -070046const log_time LogBuffer::pruneMargin(3, 0);
47
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070048void LogBuffer::init() {
Mark Salyzyndfa7a072014-02-11 12:29:31 -080049 log_id_for_each(i) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -080050 mLastSet[i] = false;
51 mLast[i] = mLogElements.begin();
52
Mark Salyzynf10e2732016-09-27 13:08:23 -070053 if (setSize(i, __android_logger_get_buffer_size(i))) {
Mark Salyzyn57a0af92014-05-09 17:44:18 -070054 setSize(i, LOG_BUFFER_MIN_SIZE);
55 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -080056 }
Mark Salyzynb6bee332015-09-08 08:56:32 -070057 bool lastMonotonic = monotonic;
Mark Salyzynba7a9a02015-12-01 15:57:25 -080058 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzynb75cce02015-11-30 11:35:56 -080059 if (lastMonotonic != monotonic) {
60 //
61 // Fixup all timestamps, may not be 100% accurate, but better than
62 // throwing what we have away when we get 'surprised' by a change.
63 // In-place element fixup so no need to check reader-lock. Entries
64 // should already be in timestamp order, but we could end up with a
65 // few out-of-order entries if new monotonics come in before we
66 // are notified of the reinit change in status. A Typical example would
67 // be:
68 // --------- beginning of system
69 // 10.494082 184 201 D Cryptfs : Just triggered post_fs_data
70 // --------- beginning of kernel
71 // 0.000000 0 0 I : Initializing cgroup subsys
72 // as the act of mounting /data would trigger persist.logd.timestamp to
73 // be corrected. 1/30 corner case YMMV.
74 //
Mark Salyzyn3c501b52017-04-18 14:09:45 -070075 rdlock();
Mark Salyzynb75cce02015-11-30 11:35:56 -080076 LogBufferElementCollection::iterator it = mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -080077 while ((it != mLogElements.end())) {
78 LogBufferElement* e = *it;
Mark Salyzynb75cce02015-11-30 11:35:56 -080079 if (monotonic) {
80 if (!android::isMonotonic(e->mRealTime)) {
81 LogKlog::convertRealToMonotonic(e->mRealTime);
Mark Salyzyn206ed8e2017-05-18 10:06:00 -070082 if ((e->mRealTime.tv_nsec % 1000) == 0) {
83 e->mRealTime.tv_nsec++;
84 }
Mark Salyzynb75cce02015-11-30 11:35:56 -080085 }
86 } else {
87 if (android::isMonotonic(e->mRealTime)) {
88 LogKlog::convertMonotonicToReal(e->mRealTime);
Mark Salyzyn206ed8e2017-05-18 10:06:00 -070089 if ((e->mRealTime.tv_nsec % 1000) == 0) {
90 e->mRealTime.tv_nsec++;
91 }
Mark Salyzynb75cce02015-11-30 11:35:56 -080092 }
93 }
94 ++it;
95 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -070096 unlock();
Mark Salyzynb6bee332015-09-08 08:56:32 -070097 }
98
Mark Salyzynb75cce02015-11-30 11:35:56 -080099 // We may have been triggered by a SIGHUP. Release any sleeping reader
100 // threads to dump their current content.
Mark Salyzynb6bee332015-09-08 08:56:32 -0700101 //
Mark Salyzynb75cce02015-11-30 11:35:56 -0800102 // NB: this is _not_ performed in the context of a SIGHUP, it is
103 // performed during startup, and in context of reinit administrative thread
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700104 LogTimeEntry::wrlock();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800105
106 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800107 while (times != mTimes.end()) {
108 LogTimeEntry* entry = (*times);
Mark Salyzynb75cce02015-11-30 11:35:56 -0800109 if (entry->owned_Locked()) {
110 entry->triggerReader_Locked();
Mark Salyzynb6bee332015-09-08 08:56:32 -0700111 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800112 times++;
Mark Salyzynb6bee332015-09-08 08:56:32 -0700113 }
Mark Salyzynb75cce02015-11-30 11:35:56 -0800114
115 LogTimeEntry::unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800116}
117
Mark Salyzyn501c3732017-03-10 14:31:54 -0800118LogBuffer::LogBuffer(LastLogTimes* times)
119 : monotonic(android_log_clockid() == CLOCK_MONOTONIC), mTimes(*times) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700120 pthread_rwlock_init(&mLogElementsLock, nullptr);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700121
Mark Salyzyna2c02222016-12-13 10:31:29 -0800122 log_id_for_each(i) {
Mark Salyzynae2abf12017-03-31 10:48:39 -0700123 lastLoggedElements[i] = nullptr;
124 droppedElements[i] = nullptr;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800125 }
126
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700127 init();
128}
129
Mark Salyzyna2c02222016-12-13 10:31:29 -0800130LogBuffer::~LogBuffer() {
131 log_id_for_each(i) {
132 delete lastLoggedElements[i];
133 delete droppedElements[i];
134 }
135}
136
Mark Salyzyn501c3732017-03-10 14:31:54 -0800137enum match_type { DIFFERENT, SAME, SAME_LIBLOG };
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800138
Mark Salyzyn501c3732017-03-10 14:31:54 -0800139static enum match_type identical(LogBufferElement* elem,
140 LogBufferElement* last) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800141 // is it mostly identical?
Mark Salyzyn501c3732017-03-10 14:31:54 -0800142 // if (!elem) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700143 ssize_t lenl = elem->getMsgLen();
144 if (lenl <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800145 // if (!last) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700146 ssize_t lenr = last->getMsgLen();
147 if (lenr <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800148 // if (elem->getLogId() != last->getLogId()) return DIFFERENT;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800149 if (elem->getUid() != last->getUid()) return DIFFERENT;
150 if (elem->getPid() != last->getPid()) return DIFFERENT;
151 if (elem->getTid() != last->getTid()) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800152
153 // last is more than a minute old, stop squashing identical messages
154 if (elem->getRealTime().nsec() >
Mark Salyzyn501c3732017-03-10 14:31:54 -0800155 (last->getRealTime().nsec() + 60 * NS_PER_SEC))
156 return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800157
158 // Identical message
159 const char* msgl = elem->getMsg();
160 const char* msgr = last->getMsg();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800161 if (lenl == lenr) {
162 if (!fastcmp<memcmp>(msgl, msgr, lenl)) return SAME;
163 // liblog tagged messages (content gets summed)
164 if ((elem->getLogId() == LOG_ID_EVENTS) &&
165 (lenl == sizeof(android_log_event_int_t)) &&
Mark Salyzyn501c3732017-03-10 14:31:54 -0800166 !fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_int_t) -
167 sizeof(int32_t)) &&
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700168 (elem->getTag() == LIBLOG_LOG_TAG)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800169 return SAME_LIBLOG;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700170 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800171 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800172
173 // audit message (except sequence number) identical?
Mark Salyzyna2c02222016-12-13 10:31:29 -0800174 if (last->isBinary()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800175 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) -
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700176 sizeof(int32_t))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800177 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700178 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800179 msgl += sizeof(android_log_event_string_t);
180 lenl -= sizeof(android_log_event_string_t);
181 msgr += sizeof(android_log_event_string_t);
182 lenr -= sizeof(android_log_event_string_t);
183 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700184 static const char avc[] = "): avc: ";
Mark Salyzyn501c3732017-03-10 14:31:54 -0800185 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800186 if (!avcl) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800187 lenl -= avcl - msgl;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800188 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800189 if (!avcr) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800190 lenr -= avcr - msgr;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800191 if (lenl != lenr) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700192 if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc),
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700193 lenl - strlen(avc))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800194 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700195 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800196 return SAME;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800197}
198
Mark Salyzyn501c3732017-03-10 14:31:54 -0800199int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
200 pid_t tid, const char* msg, unsigned short len) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800201 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800202 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800203 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700204
Mark Salyzyn206ed8e2017-05-18 10:06:00 -0700205 // Slip the time by 1 nsec if the incoming lands on xxxxxx000 ns.
206 // This prevents any chance that an outside source can request an
207 // exact entry with time specified in ms or us precision.
208 if ((realtime.tv_nsec % 1000) == 0) ++realtime.tv_nsec;
209
Mark Salyzyn501c3732017-03-10 14:31:54 -0800210 LogBufferElement* elem =
211 new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000212 if (log_id != LOG_ID_SECURITY) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800213 int prio = ANDROID_LOG_INFO;
Mark Salyzynae2abf12017-03-31 10:48:39 -0700214 const char* tag = nullptr;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800215 if (log_id == LOG_ID_EVENTS) {
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700216 tag = tagToName(elem->getTag());
Mark Salyzyn083b0372015-12-04 10:59:45 -0800217 } else {
218 prio = *msg;
219 tag = msg + 1;
220 }
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700221 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800222 // Log traffic received to total
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700223 wrlock();
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700224 stats.addTotal(elem);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700225 unlock();
Mark Salyzyn083b0372015-12-04 10:59:45 -0800226 delete elem;
227 return -EACCES;
228 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700229 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800230
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700231 wrlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800232 LogBufferElement* currentLast = lastLoggedElements[log_id];
233 if (currentLast) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800234 LogBufferElement* dropped = droppedElements[log_id];
Mark Salyzyna2c02222016-12-13 10:31:29 -0800235 unsigned short count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800236 //
237 // State Init
238 // incoming:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700239 // dropped = nullptr
240 // currentLast = nullptr;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800241 // elem = incoming message
242 // outgoing:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700243 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800244 // currentLast = copy of elem
245 // log elem
246 // State 0
247 // incoming:
248 // count = 0
Mark Salyzynae2abf12017-03-31 10:48:39 -0700249 // dropped = nullptr
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800250 // currentLast = copy of last message
251 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800252 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800253 // dropped = copy of first identical message -> State 1
254 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800255 // break: if match == DIFFERENT
Mark Salyzynae2abf12017-03-31 10:48:39 -0700256 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800257 // delete copy of last message (incoming currentLast)
258 // currentLast = copy of elem
259 // log elem
260 // State 1
261 // incoming:
262 // count = 0
263 // dropped = copy of first identical message
264 // currentLast = reference to last held-back incoming
265 // message
266 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800267 // outgoing: if match == SAME
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800268 // delete copy of first identical message (dropped)
269 // dropped = reference to last held-back incoming
270 // message set to chatty count of 1 -> State 2
271 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800272 // outgoing: if match == SAME_LIBLOG
273 // dropped = copy of first identical message -> State 1
274 // take sum of currentLast and elem
275 // if sum overflows:
276 // log currentLast
277 // currentLast = reference to elem
278 // else
279 // delete currentLast
280 // currentLast = reference to elem, sum liblog.
281 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800282 // delete dropped
Mark Salyzynae2abf12017-03-31 10:48:39 -0700283 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800284 // log reference to last held-back (currentLast)
285 // currentLast = copy of elem
286 // log elem
287 // State 2
288 // incoming:
289 // count = chatty count
290 // dropped = chatty message holding count
291 // currentLast = reference to last held-back incoming
292 // message.
293 // dropped = chatty message holding count
294 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800295 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800296 // delete chatty message holding count
297 // dropped = reference to last held-back incoming
298 // message, set to chatty count + 1
299 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800300 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800301 // log dropped (chatty message)
Mark Salyzynae2abf12017-03-31 10:48:39 -0700302 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800303 // log reference to last held-back (currentLast)
304 // currentLast = copy of elem
305 // log elem
306 //
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800307 enum match_type match = identical(elem, currentLast);
308 if (match != DIFFERENT) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800309 if (dropped) {
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800310 // Sum up liblog tag messages?
311 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
312 android_log_event_int_t* event =
313 reinterpret_cast<android_log_event_int_t*>(
314 const_cast<char*>(currentLast->getMsg()));
315 //
316 // To unit test, differentiate with something like:
317 // event->header.tag = htole32(CHATTY_LOG_TAG);
318 // here, then instead of delete currentLast below,
319 // log(currentLast) to see the incremental sums form.
320 //
321 uint32_t swab = event->payload.data;
322 unsigned long long total = htole32(swab);
323 event = reinterpret_cast<android_log_event_int_t*>(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800324 const_cast<char*>(elem->getMsg()));
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800325 swab = event->payload.data;
326
327 lastLoggedElements[LOG_ID_EVENTS] = elem;
328 total += htole32(swab);
329 // check for overflow
330 if (total >= UINT32_MAX) {
331 log(currentLast);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700332 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800333 return len;
334 }
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700335 stats.addTotal(currentLast);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800336 delete currentLast;
337 swab = total;
338 event->payload.data = htole32(swab);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700339 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800340 return len;
341 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800342 if (count == USHRT_MAX) {
343 log(dropped);
344 count = 1;
345 } else {
346 delete dropped;
347 ++count;
348 }
349 }
350 if (count) {
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700351 stats.addTotal(currentLast);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800352 currentLast->setDropped(count);
353 }
354 droppedElements[log_id] = currentLast;
355 lastLoggedElements[log_id] = elem;
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700356 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800357 return len;
358 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800359 if (dropped) { // State 1 or 2
360 if (count) { // State 2
361 log(dropped); // report chatty
362 } else { // State 1
363 delete dropped;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800364 }
Mark Salyzynae2abf12017-03-31 10:48:39 -0700365 droppedElements[log_id] = nullptr;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800366 log(currentLast); // report last message in the series
367 } else { // State 0
Mark Salyzyna2c02222016-12-13 10:31:29 -0800368 delete currentLast;
369 }
370 }
371 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800372
Mark Salyzyna2c02222016-12-13 10:31:29 -0800373 log(elem);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700374 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800375
376 return len;
377}
378
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700379// assumes LogBuffer::wrlock() held, owns elem, look after garbage collection
Mark Salyzyna2c02222016-12-13 10:31:29 -0800380void LogBuffer::log(LogBufferElement* elem) {
Mark Salyzyn09d66322017-03-14 13:11:12 -0700381 // cap on how far back we will sort in-place, otherwise append
382 static uint32_t too_far_back = 5; // five seconds
Mark Salyzyn0175b072014-02-26 09:50:16 -0800383 // Insert elements in time sorted order if possible
384 // NB: if end is region locked, place element at end of list
385 LogBufferElementCollection::iterator it = mLogElements.end();
386 LogBufferElementCollection::iterator last = it;
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800387 if (__predict_true(it != mLogElements.begin())) --it;
388 if (__predict_false(it == mLogElements.begin()) ||
Mark Salyzyn09d66322017-03-14 13:11:12 -0700389 __predict_true((*it)->getRealTime() <= elem->getRealTime()) ||
390 __predict_false((((*it)->getRealTime().tv_sec - too_far_back) >
391 elem->getRealTime().tv_sec) &&
392 (elem->getLogId() != LOG_ID_KERNEL) &&
393 ((*it)->getLogId() != LOG_ID_KERNEL))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800394 mLogElements.push_back(elem);
395 } else {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800396 log_time end = log_time::EPOCH;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800397 bool end_set = false;
398 bool end_always = false;
399
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700400 LogTimeEntry::rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800401
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700402 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800403 while (times != mTimes.end()) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800404 LogTimeEntry* entry = (*times);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800405 if (entry->owned_Locked()) {
406 if (!entry->mNonBlock) {
407 end_always = true;
408 break;
409 }
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800410 // it passing mEnd is blocked by the following checks.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800411 if (!end_set || (end <= entry->mEnd)) {
412 end = entry->mEnd;
413 end_set = true;
414 }
415 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700416 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800417 }
418
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800419 if (end_always || (end_set && (end > (*it)->getRealTime()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800420 mLogElements.push_back(elem);
421 } else {
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800422 // should be short as timestamps are localized near end()
423 do {
424 last = it;
425 if (__predict_false(it == mLogElements.begin())) {
426 break;
427 }
Mark Salyzyn1d84f0b2017-03-03 10:21:23 -0800428 --it;
429 } while (((*it)->getRealTime() > elem->getRealTime()) &&
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800430 (!end_set || (end <= (*it)->getRealTime())));
Mark Salyzyna2c02222016-12-13 10:31:29 -0800431 mLogElements.insert(last, elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800432 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800433 LogTimeEntry::unlock();
434 }
435
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700436 stats.add(elem);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800437 maybePrune(elem->getLogId());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800438}
439
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700440// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800441//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700442// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800443void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800444 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700445 unsigned long maxSize = log_buffer_size(id);
446 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700447 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700448 size_t elements = stats.realElements(id);
449 size_t minElements = elements / 100;
450 if (minElements < minPrune) {
451 minElements = minPrune;
452 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700453 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700454 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700455 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800456 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700457 if (pruneRows > maxPrune) {
458 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700459 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800460 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800461 }
462}
463
Mark Salyzyn831aa292015-09-03 16:08:50 -0700464LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800465 LogBufferElementCollection::iterator it, bool coalesce) {
466 LogBufferElement* element = *it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700467 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700468
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700469 // Remove iterator references in the various lists that will become stale
470 // after the element is erased from the main logging list.
471
Mark Salyzyn501c3732017-03-10 14:31:54 -0800472 { // start of scope for found iterator
473 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
474 ? element->getTag()
475 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700476 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
477 if ((found != mLastWorst[id].end()) && (it == found->second)) {
478 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700479 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700480 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700481
Mark Salyzyn501c3732017-03-10 14:31:54 -0800482 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700483 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700484 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
485 // long term code stability, find() check should be fast for those ids.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700486 LogBufferPidIteratorMap::iterator found =
487 mLastWorstPidOfSystem[id].find(element->getPid());
Mark Salyzyn501c3732017-03-10 14:31:54 -0800488 if ((found != mLastWorstPidOfSystem[id].end()) &&
489 (it == found->second)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700490 mLastWorstPidOfSystem[id].erase(found);
491 }
492 }
493
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800494 bool setLast[LOG_ID_MAX];
495 bool doSetLast = false;
496 log_id_for_each(i) {
497 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
498 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700499#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
500 LogBufferElementCollection::iterator bad = it;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800501 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
502 ? element->getTag()
503 : element->getUid();
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700504#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700505 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800506 if (doSetLast) {
507 log_id_for_each(i) {
508 if (setLast[i]) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800509 if (__predict_false(it == mLogElements.end())) { // impossible
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800510 mLastSet[i] = false;
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700511 mLast[i] = mLogElements.begin();
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800512 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800513 mLast[i] = it; // push down the road as next-best-watermark
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800514 }
515 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800516 }
517 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700518#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
519 log_id_for_each(i) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800520 for (auto b : mLastWorst[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700521 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800522 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i,
523 b.first, key);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700524 }
525 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800526 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700527 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800528 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i,
529 b.first);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700530 }
531 }
532 if (mLastSet[i] && (bad == mLast[i])) {
533 android::prdebug("stale mLast[%d]\n", i);
534 mLastSet[i] = false;
535 mLast[i] = mLogElements.begin();
536 }
537 }
538#endif
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700539 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700540 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700541 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700542 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700543 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700544 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700545
546 return it;
547}
548
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700549// Define a temporary mechanism to report the last LogBufferElement pointer
550// for the specified uid, pid and tid. Used below to help merge-sort when
551// pruning for worst UID.
552class LogBufferElementKey {
553 const union {
554 struct {
Mark Salyzyn684bdb52016-12-13 12:44:20 -0800555 uint32_t uid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700556 uint16_t pid;
557 uint16_t tid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700558 } __packed;
559 uint64_t value;
560 } __packed;
561
Mark Salyzyn501c3732017-03-10 14:31:54 -0800562 public:
563 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid)
564 : uid(uid), pid(pid), tid(tid) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700565 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800566 explicit LogBufferElementKey(uint64_t key) : value(key) {
567 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700568
Mark Salyzyn501c3732017-03-10 14:31:54 -0800569 uint64_t getKey() {
570 return value;
571 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700572};
573
Mark Salyzyn511338d2015-05-19 09:12:30 -0700574class LogBufferElementLast {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800575 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn511338d2015-05-19 09:12:30 -0700576 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700577
Mark Salyzyn501c3732017-03-10 14:31:54 -0800578 public:
579 bool coalesce(LogBufferElement* element, unsigned short dropped) {
580 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700581 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700582 LogBufferElementMap::iterator it = map.find(key.getKey());
583 if (it != map.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800584 LogBufferElement* found = it->second;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700585 unsigned short moreDropped = found->getDropped();
586 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700587 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700588 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700589 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700590 return true;
591 }
592 }
593 return false;
594 }
595
Mark Salyzyn501c3732017-03-10 14:31:54 -0800596 void add(LogBufferElement* element) {
597 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700598 element->getTid());
599 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700600 }
601
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700602 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700603 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700604 }
605
Mark Salyzyn501c3732017-03-10 14:31:54 -0800606 void clear(LogBufferElement* element) {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800607 log_time current =
608 element->getRealTime() - log_time(EXPIRE_RATELIMIT, 0);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800609 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
610 LogBufferElement* mapElement = it->second;
611 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD) &&
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800612 (current > mapElement->getRealTime())) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700613 it = map.erase(it);
614 } else {
615 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700616 }
617 }
618 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700619};
620
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700621// Determine if watermark is within pruneMargin + 1s from the end of the list,
622// the caller will use this result to set an internal busy flag indicating
623// the prune operation could not be completed because a reader is blocking
624// the request.
625bool LogBuffer::isBusy(log_time watermark) {
626 LogBufferElementCollection::iterator ei = mLogElements.end();
627 --ei;
628 return watermark < ((*ei)->getRealTime() - pruneMargin - log_time(1, 0));
629}
630
631// If the selected reader is blocking our pruning progress, decide on
632// what kind of mitigation is necessary to unblock the situation.
633void LogBuffer::kickMe(LogTimeEntry* me, log_id_t id, unsigned long pruneRows) {
634 if (stats.sizes(id) > (2 * log_buffer_size(id))) { // +100%
635 // A misbehaving or slow reader has its connection
636 // dropped if we hit too much memory pressure.
637 me->release_Locked();
638 } else if (me->mTimeout.tv_sec || me->mTimeout.tv_nsec) {
639 // Allow a blocked WRAP timeout reader to
640 // trigger and start reporting the log data.
641 me->triggerReader_Locked();
642 } else {
643 // tell slow reader to skip entries to catch up
644 me->triggerSkip_Locked(id, pruneRows);
645 }
646}
647
Mark Salyzyn0175b072014-02-26 09:50:16 -0800648// prune "pruneRows" of type "id" from the buffer.
649//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700650// This garbage collection task is used to expire log entries. It is called to
651// remove all logs (clear), all UID logs (unprivileged clear), or every
652// 256 or 10% of the total logs (whichever is less) to prune the logs.
653//
654// First there is a prep phase where we discover the reader region lock that
655// acts as a backstop to any pruning activity to stop there and go no further.
656//
657// There are three major pruning loops that follow. All expire from the oldest
658// entries. Since there are multiple log buffers, the Android logging facility
659// will appear to drop entries 'in the middle' when looking at multiple log
660// sources and buffers. This effect is slightly more prominent when we prune
661// the worst offender by logging source. Thus the logs slowly loose content
662// and value as you move back in time. This is preferred since chatty sources
663// invariably move the logs value down faster as less chatty sources would be
664// expired in the noise.
665//
666// The first loop performs blacklisting and worst offender pruning. Falling
667// through when there are no notable worst offenders and have not hit the
668// region lock preventing further worst offender pruning. This loop also looks
669// after managing the chatty log entries and merging to help provide
670// statistical basis for blame. The chatty entries are not a notification of
671// how much logs you may have, but instead represent how much logs you would
672// have had in a virtual log buffer that is extended to cover all the in-memory
673// logs without loss. They last much longer than the represented pruned logs
674// since they get multiplied by the gains in the non-chatty log sources.
675//
676// The second loop get complicated because an algorithm of watermarks and
677// history is maintained to reduce the order and keep processing time
678// down to a minimum at scale. These algorithms can be costly in the face
679// of larger log buffers, or severly limited processing time granted to a
680// background task at lowest priority.
681//
682// This second loop does straight-up expiration from the end of the logs
683// (again, remember for the specified log buffer id) but does some whitelist
684// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
685// spam filtration all take priority. This second loop also checks if a region
686// lock is causing us to buffer too much in the logs to help the reader(s),
687// and will tell the slowest reader thread to skip log entries, and if
688// persistent and hits a further threshold, kill the reader thread.
689//
690// The third thread is optional, and only gets hit if there was a whitelist
691// and more needs to be pruned against the backstop of the region lock.
692//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700693// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700694//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700695bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzynae2abf12017-03-31 10:48:39 -0700696 LogTimeEntry* oldest = nullptr;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700697 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700698 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800699
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700700 LogTimeEntry::rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800701
702 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700703 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800704 while (times != mTimes.end()) {
705 LogTimeEntry* entry = (*times);
706 if (entry->owned_Locked() && entry->isWatching(id) &&
707 (!oldest || (oldest->mStart > entry->mStart) ||
708 ((oldest->mStart == entry->mStart) &&
709 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800710 oldest = entry;
711 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700712 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800713 }
Mark Salyzyn58363792017-04-17 12:46:12 -0700714 log_time watermark(log_time::tv_sec_max, log_time::tv_nsec_max);
715 if (oldest) watermark = oldest->mStart - pruneMargin;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800716
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800717 LogBufferElementCollection::iterator it;
718
Mark Salyzyn501c3732017-03-10 14:31:54 -0800719 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700720 // Only here if clear all request from non system source, so chatty
721 // filter logistics is not required.
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800722 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
723 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800724 LogBufferElement* element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700725
Mark Salyzyn501c3732017-03-10 14:31:54 -0800726 if ((element->getLogId() != id) ||
727 (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700728 ++it;
729 continue;
730 }
731
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800732 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
733 mLast[id] = it;
734 mLastSet[id] = true;
735 }
736
Mark Salyzyn58363792017-04-17 12:46:12 -0700737 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700738 busy = isBusy(watermark);
739 if (busy) kickMe(oldest, id, pruneRows);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700740 break;
741 }
742
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700743 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700744 if (--pruneRows == 0) {
745 break;
746 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700747 }
748 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700749 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700750 }
751
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700752 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn083b0372015-12-04 10:59:45 -0800753 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700754 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800755 // recalculate the worst offender on every batched pass
Mark Salyzyn501c3732017-03-10 14:31:54 -0800756 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800757 size_t worst_sizes = 0;
758 size_t second_worst_sizes = 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800759 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800760
Mark Salyzynae769232015-03-17 17:17:25 -0700761 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700762 // Calculate threshold as 12.5% of available storage
763 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700764
Mark Salyzyn6a066942016-07-14 15:34:30 -0700765 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800766 stats.sortTags(AID_ROOT, (pid_t)0, 2, id)
767 .findWorst(worst, worst_sizes, second_worst_sizes,
768 threshold);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700769 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700770 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800771 stats.sort(AID_ROOT, (pid_t)0, 2, id)
772 .findWorst(worst, worst_sizes, second_worst_sizes,
773 threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700774
Mark Salyzyn6a066942016-07-14 15:34:30 -0700775 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800776 stats.sortPids(worst, (pid_t)0, 2, id)
777 .findWorst(worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700778 }
779 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800780 }
781
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700782 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700783 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700784 break;
785 }
786
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800787 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700788 bool leading = true;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800789 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700790 // Perform at least one mandatory garbage collection cycle in following
791 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700792 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700793 // - check age-out of preserved logs
794 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700795 if (!gc && (worst != -1)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800796 { // begin scope for worst found iterator
797 LogBufferIteratorMap::iterator found =
798 mLastWorst[id].find(worst);
799 if ((found != mLastWorst[id].end()) &&
800 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700801 leading = false;
802 it = found->second;
803 }
804 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800805 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700806 // FYI: worstPid only set if !LOG_ID_EVENTS and
807 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzyn501c3732017-03-10 14:31:54 -0800808 LogBufferPidIteratorMap::iterator found =
809 mLastWorstPidOfSystem[id].find(worstPid);
810 if ((found != mLastWorstPidOfSystem[id].end()) &&
811 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700812 leading = false;
813 it = found->second;
814 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700815 }
816 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800817 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzynccfe8442015-08-24 13:43:27 -0700818 LogBufferElementCollection::iterator lastt;
819 lastt = mLogElements.end();
820 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700821 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700822 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800823 LogBufferElement* element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800824
Mark Salyzyn58363792017-04-17 12:46:12 -0700825 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700826 busy = isBusy(watermark);
827 // Do not let chatty eliding trigger any reader mitigation
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800828 break;
829 }
830
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700831 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800832 ++it;
833 continue;
834 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700835 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800836
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800837 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
838 mLast[id] = it;
839 mLastSet[id] = true;
840 }
841
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700842 unsigned short dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800843
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700844 // remove any leading drops
845 if (leading && dropped) {
846 it = erase(it);
847 continue;
848 }
849
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700850 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700851 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700852 continue;
853 }
854
Mark Salyzyn501c3732017-03-10 14:31:54 -0800855 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
856 ? element->getTag()
857 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700858
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700859 if (hasBlacklist && mPrune.naughty(element)) {
860 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700861 it = erase(it);
862 if (dropped) {
863 continue;
864 }
865
866 pruneRows--;
867 if (pruneRows == 0) {
868 break;
869 }
870
Mark Salyzyn6a066942016-07-14 15:34:30 -0700871 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700872 kick = true;
873 if (worst_sizes < second_worst_sizes) {
874 break;
875 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700876 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700877 }
878 continue;
879 }
880
Mark Salyzyn501c3732017-03-10 14:31:54 -0800881 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
882 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700883 break;
884 }
885
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700886 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700887 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800888 if (worstPid &&
889 ((!gc && (element->getPid() == worstPid)) ||
890 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
891 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700892 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700893 // watermark if current one empty. id is not LOG_ID_EVENTS
894 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700895 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700896 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800897 if ((!gc && !worstPid && (key == worst)) ||
898 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700899 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700900 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800901 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700902 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800903 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700904
Mark Salyzyn501c3732017-03-10 14:31:54 -0800905 if ((key != worst) ||
906 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700907 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700908 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700909 ++it;
910 continue;
911 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700912 // key == worst below here
913 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700914
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700915 pruneRows--;
916 if (pruneRows == 0) {
917 break;
918 }
919
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700920 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700921
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700922 unsigned short len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700923
924 // do not create any leading drops
925 if (leading) {
926 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700927 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700928 stats.drop(element);
929 element->setDropped(1);
930 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700931 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700932 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700933 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800934 if (worstPid &&
935 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
936 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700937 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700938 // watermark if current one empty. id is not
939 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700940 mLastWorstPidOfSystem[id][worstPid] = it;
941 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700942 if ((!gc && !worstPid) ||
Mark Salyzyn501c3732017-03-10 14:31:54 -0800943 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700944 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700945 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700946 ++it;
947 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700948 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700949 if (worst_sizes < second_worst_sizes) {
950 break;
951 }
952 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800953 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700954 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800955
Mark Salyzyn1c950472014-04-01 17:19:47 -0700956 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800957 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800958 }
959 }
960
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800961 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800962 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800963 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800964 while ((pruneRows > 0) && (it != mLogElements.end())) {
965 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700966
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700967 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700968 it++;
969 continue;
970 }
971
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800972 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
973 mLast[id] = it;
974 mLastSet[id] = true;
975 }
976
Mark Salyzyn58363792017-04-17 12:46:12 -0700977 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700978 busy = isBusy(watermark);
979 if (!whitelist && busy) kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700980 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800981 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700982
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700983 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
984 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700985 whitelist = true;
986 it++;
987 continue;
988 }
989
990 it = erase(it);
991 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800992 }
993
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700994 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800995 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800996 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800997 while ((it != mLogElements.end()) && (pruneRows > 0)) {
998 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700999
Mark Salyzynbec3c3d2015-08-28 08:02:59 -07001000 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -07001001 ++it;
1002 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001003 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -07001004
Mark Salyzyn507eb9f2016-01-11 10:58:09 -08001005 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
1006 mLast[id] = it;
1007 mLastSet[id] = true;
1008 }
1009
Mark Salyzyn58363792017-04-17 12:46:12 -07001010 if (oldest && (watermark <= element->getRealTime())) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -07001011 busy = isBusy(watermark);
1012 if (busy) kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -07001013 break;
1014 }
1015
1016 it = erase(it);
1017 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001018 }
1019 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001020
Mark Salyzyn0175b072014-02-26 09:50:16 -08001021 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001022
1023 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001024}
1025
1026// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -07001027bool LogBuffer::clear(log_id_t id, uid_t uid) {
1028 bool busy = true;
1029 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
1030 for (int retry = 4;;) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001031 if (retry == 1) { // last pass
Mark Salyzync5dc9702015-09-16 15:34:00 -07001032 // Check if it is still busy after the sleep, we say prune
1033 // one entry, not another clear run, so we are looking for
1034 // the quick side effect of the return value to tell us if
1035 // we have a _blocked_ reader.
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001036 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001037 busy = prune(id, 1, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001038 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001039 // It is still busy, blocked reader(s), lets kill them all!
1040 // otherwise, lets be a good citizen and preserve the slow
1041 // readers and let the clear run (below) deal with determining
1042 // if we are still blocked and return an error code to caller.
1043 if (busy) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001044 LogTimeEntry::wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001045 LastLogTimes::iterator times = mTimes.begin();
1046 while (times != mTimes.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001047 LogTimeEntry* entry = (*times);
Mark Salyzync5dc9702015-09-16 15:34:00 -07001048 // Killer punch
1049 if (entry->owned_Locked() && entry->isWatching(id)) {
1050 entry->release_Locked();
1051 }
1052 times++;
1053 }
1054 LogTimeEntry::unlock();
1055 }
1056 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001057 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001058 busy = prune(id, ULONG_MAX, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001059 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -07001060 if (!busy || !--retry) {
1061 break;
1062 }
Mark Salyzyn501c3732017-03-10 14:31:54 -08001063 sleep(1); // Let reader(s) catch up after notification
Mark Salyzync5dc9702015-09-16 15:34:00 -07001064 }
1065 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001066}
1067
1068// get the used space associated with "id".
1069unsigned long LogBuffer::getSizeUsed(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001070 rdlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001071 size_t retval = stats.sizes(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001072 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001073 return retval;
1074}
1075
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001076// set the total space allocated to "id"
1077int LogBuffer::setSize(log_id_t id, unsigned long size) {
1078 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -07001079 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001080 return -1;
1081 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001082 wrlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001083 log_buffer_size(id) = size;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001084 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001085 return 0;
1086}
1087
1088// get the total space allocated to "id"
1089unsigned long LogBuffer::getSize(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001090 rdlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001091 size_t retval = log_buffer_size(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001092 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001093 return retval;
1094}
1095
Mark Salyzynae2abf12017-03-31 10:48:39 -07001096log_time LogBuffer::flushTo(SocketClient* reader, const log_time& start,
1097 pid_t* lastTid, bool privileged, bool security,
1098 int (*filter)(const LogBufferElement* element,
1099 void* arg),
1100 void* arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -08001101 LogBufferElementCollection::iterator it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001102 uid_t uid = reader->getUid();
1103
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001104 rdlock();
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001105
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -08001106 if (start == log_time::EPOCH) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001107 // client wants to start from the beginning
1108 it = mLogElements.begin();
1109 } else {
Mark Salyzyn0f92fdc2017-04-04 10:57:41 -07001110 // 3 second limit to continue search for out-of-order entries.
Mark Salyzyn58363792017-04-17 12:46:12 -07001111 log_time min = start - pruneMargin;
1112
Mark Salyzyn0f92fdc2017-04-04 10:57:41 -07001113 // Cap to 300 iterations we look back for out-of-order entries.
1114 size_t count = 300;
Mark Salyzyn58363792017-04-17 12:46:12 -07001115
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001116 // Client wants to start from some specified time. Chances are
1117 // we are better off starting from the end of the time sorted list.
Mark Salyzyn58363792017-04-17 12:46:12 -07001118 LogBufferElementCollection::iterator last;
Mark Salyzyn1f467162017-03-27 13:12:41 -07001119 for (last = it = mLogElements.end(); it != mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -08001120 /* do nothing */) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001121 --it;
Mark Salyzyn501c3732017-03-10 14:31:54 -08001122 LogBufferElement* element = *it;
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001123 if (element->getRealTime() > start) {
1124 last = it;
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001125 } else if (element->getRealTime() == start) {
1126 last = ++it;
1127 break;
Mark Salyzyn0f92fdc2017-04-04 10:57:41 -07001128 } else if (!--count || (element->getRealTime() < min)) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001129 break;
1130 }
1131 }
Mark Salyzyn3b941d42017-03-10 10:31:48 -08001132 it = last;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001133 }
1134
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001135 log_time curr = start;
Mark Salyzynb5b87962017-01-23 14:20:31 -08001136
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001137 LogBufferElement* lastElement = nullptr; // iterator corruption paranoia
1138 static const size_t maxSkip = 4194304; // maximum entries to skip
1139 size_t skip = maxSkip;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001140 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001141 LogBufferElement* element = *it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001142
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001143 if (!--skip) {
1144 android::prdebug("reader.per: too many elements skipped");
1145 break;
1146 }
1147 if (element == lastElement) {
1148 android::prdebug("reader.per: identical elements");
1149 break;
1150 }
1151 lastElement = element;
1152
Mark Salyzyn0175b072014-02-26 09:50:16 -08001153 if (!privileged && (element->getUid() != uid)) {
1154 continue;
1155 }
1156
Mark Salyzyn8fa88962016-01-26 14:32:35 -08001157 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1158 continue;
1159 }
1160
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001161 // NB: calling out to another object with wrlock() held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001162 if (filter) {
1163 int ret = (*filter)(element, arg);
1164 if (ret == false) {
1165 continue;
1166 }
1167 if (ret != true) {
1168 break;
1169 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001170 }
1171
Mark Salyzynae2abf12017-03-31 10:48:39 -07001172 bool sameTid = false;
1173 if (lastTid) {
1174 sameTid = lastTid[element->getLogId()] == element->getTid();
1175 // Dropped (chatty) immediately following a valid log from the
1176 // same source in the same log buffer indicates we have a
1177 // multiple identical squash. chatty that differs source
1178 // is due to spam filter. chatty to chatty of different
1179 // source is also due to spam filter.
1180 lastTid[element->getLogId()] =
1181 (element->getDropped() && !sameTid) ? 0 : element->getTid();
1182 }
Mark Salyzynb5b87962017-01-23 14:20:31 -08001183
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001184 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001185
1186 // range locking in LastLogTimes looks after us
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001187 curr = element->flushTo(reader, this, privileged, sameTid);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001188
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001189 if (curr == element->FLUSH_ERROR) {
1190 return curr;
Mark Salyzyneb45db22017-05-17 19:55:12 +00001191 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001192
Mark Salyzyn3614a0c2017-04-17 12:46:12 -07001193 skip = maxSkip;
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001194 rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001195 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001196 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001197
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001198 return curr;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001199}
Mark Salyzyn34facab2014-02-06 14:48:50 -08001200
Mark Salyzynee3b8382015-12-17 09:58:43 -08001201std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1202 unsigned int logMask) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001203 wrlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001204
Mark Salyzynee3b8382015-12-17 09:58:43 -08001205 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001206
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001207 unlock();
Mark Salyzyn73160ac2015-08-20 10:01:44 -07001208
1209 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001210}