blob: cf93c23ae3896546876507bb12b9ef16e585f289 [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
Tom Cherry1a12ae32020-05-01 16:13:18 -070017#pragma once
Mark Salyzyn0175b072014-02-26 09:50:16 -080018
19#include <sys/types.h>
20
Mark Salyzyn94a89c42015-08-19 13:41:51 -070021#include <list>
Tom Cherry385c2c92020-04-29 17:58:18 -070022#include <optional>
Mark Salyzyn73160ac2015-08-20 10:01:44 -070023#include <string>
Mark Salyzyn94a89c42015-08-19 13:41:51 -070024
Mark Salyzyn0dd44312016-09-28 15:54:45 -070025#include <android/log.h>
Mark Salyzyn1a240b42014-06-12 11:16:16 -070026#include <private/android_filesystem_config.h>
Mark Salyzyn0dd44312016-09-28 15:54:45 -070027#include <sysutils/SocketClient.h>
Mark Salyzyn1a240b42014-06-12 11:16:16 -070028
Mark Salyzyn0175b072014-02-26 09:50:16 -080029#include "LogBufferElement.h"
Mark Salyzyn501c3732017-03-10 14:31:54 -080030#include "LogStatistics.h"
Mark Salyzyn61e9ce62016-09-12 14:51:54 -070031#include "LogTags.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080032#include "LogTimes.h"
Mark Salyzyndfa7a072014-02-11 12:29:31 -080033#include "LogWhiteBlackList.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080034
Mark Salyzynb6bee332015-09-08 08:56:32 -070035//
Mark Salyzyn10b82b62015-12-28 15:33:01 -080036// We are either in 1970ish (MONOTONIC) or 2016+ish (REALTIME) so to
37// differentiate without prejudice, we use 1972 to delineate, earlier
38// is likely monotonic, later is real. Otherwise we start using a
39// dividing line between monotonic and realtime if more than a minute
40// difference between them.
Mark Salyzynb6bee332015-09-08 08:56:32 -070041//
42namespace android {
43
Mark Salyzyn501c3732017-03-10 14:31:54 -080044static bool isMonotonic(const log_time& mono) {
Mark Salyzyn10b82b62015-12-28 15:33:01 -080045 static const uint32_t EPOCH_PLUS_2_YEARS = 2 * 24 * 60 * 60 * 1461 / 4;
46 static const uint32_t EPOCH_PLUS_MINUTE = 60;
Mark Salyzynb6bee332015-09-08 08:56:32 -070047
Mark Salyzyn10b82b62015-12-28 15:33:01 -080048 if (mono.tv_sec >= EPOCH_PLUS_2_YEARS) {
49 return false;
50 }
51
52 log_time now(CLOCK_REALTIME);
53
54 /* Timezone and ntp time setup? */
55 if (now.tv_sec >= EPOCH_PLUS_2_YEARS) {
56 return true;
57 }
58
59 /* no way to differentiate realtime from monotonic time */
60 if (now.tv_sec < EPOCH_PLUS_MINUTE) {
61 return false;
62 }
63
64 log_time cpu(CLOCK_MONOTONIC);
65 /* too close to call to differentiate monotonic times from realtime */
66 if ((cpu.tv_sec + EPOCH_PLUS_MINUTE) >= now.tv_sec) {
67 return false;
68 }
69
70 /* dividing line half way between monotonic and realtime */
71 return mono.tv_sec < ((cpu.tv_sec + now.tv_sec) / 2);
Mark Salyzynb6bee332015-09-08 08:56:32 -070072}
Mark Salyzynb6bee332015-09-08 08:56:32 -070073}
74
Mark Salyzyn501c3732017-03-10 14:31:54 -080075typedef std::list<LogBufferElement*> LogBufferElementCollection;
Mark Salyzyn0175b072014-02-26 09:50:16 -080076
Tom Cherry30968182019-06-28 13:37:10 -070077class LogBuffer {
Mark Salyzyn0175b072014-02-26 09:50:16 -080078 LogBufferElementCollection mLogElements;
Mark Salyzyn3c501b52017-04-18 14:09:45 -070079 pthread_rwlock_t mLogElementsLock;
Mark Salyzyn0175b072014-02-26 09:50:16 -080080
Mark Salyzyn34facab2014-02-06 14:48:50 -080081 LogStatistics stats;
Mark Salyzyne457b742014-02-19 17:18:31 -080082
Tom Cherry385c2c92020-04-29 17:58:18 -070083 // Keeps track of the iterator to the oldest log message of a given log type, as an
84 // optimization when pruning logs. Use GetOldest() to retrieve.
85 std::optional<LogBufferElementCollection::iterator> mOldest[LOG_ID_MAX];
Mark Salyzync892ea32015-08-19 17:06:11 -070086 // watermark of any worst/chatty uid processing
Mark Salyzyn501c3732017-03-10 14:31:54 -080087 typedef std::unordered_map<uid_t, LogBufferElementCollection::iterator>
88 LogBufferIteratorMap;
Mark Salyzyn6a066942016-07-14 15:34:30 -070089 LogBufferIteratorMap mLastWorst[LOG_ID_MAX];
Mark Salyzynbec3c3d2015-08-28 08:02:59 -070090 // watermark of any worst/chatty pid of system processing
Mark Salyzyn501c3732017-03-10 14:31:54 -080091 typedef std::unordered_map<pid_t, LogBufferElementCollection::iterator>
92 LogBufferPidIteratorMap;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -070093 LogBufferPidIteratorMap mLastWorstPidOfSystem[LOG_ID_MAX];
Mark Salyzyndfa7a072014-02-11 12:29:31 -080094
95 unsigned long mMaxSize[LOG_ID_MAX];
Mark Salyzyndfa7a072014-02-11 12:29:31 -080096
Mark Salyzynb6bee332015-09-08 08:56:32 -070097 bool monotonic;
98
Mark Salyzyna2c02222016-12-13 10:31:29 -080099 LogBufferElement* lastLoggedElements[LOG_ID_MAX];
100 LogBufferElement* droppedElements[LOG_ID_MAX];
101 void log(LogBufferElement* elem);
102
Mark Salyzyn501c3732017-03-10 14:31:54 -0800103 public:
104 LastLogTimes& mTimes;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800105
Tom Cherry5a3db392020-05-01 17:03:20 -0700106 LogBuffer(LastLogTimes* times, LogTags* tags, PruneList* prune);
Tom Cherry30968182019-06-28 13:37:10 -0700107 ~LogBuffer();
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700108 void init();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800109 bool isMonotonic() {
110 return monotonic;
111 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800112
Tom Cherry30968182019-06-28 13:37:10 -0700113 int log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid, const char* msg,
114 uint16_t len);
Mark Salyzynae2abf12017-03-31 10:48:39 -0700115 // lastTid is an optional context to help detect if the last previous
116 // valid message was from the same source so we can differentiate chatty
117 // filter types (identical or expired)
Tom Cherry10d086e2019-08-21 14:16:34 -0700118 uint64_t flushTo(SocketClient* writer, uint64_t start,
Mark Salyzynae2abf12017-03-31 10:48:39 -0700119 pid_t* lastTid, // &lastTid[LOG_ID_MAX] or nullptr
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800120 bool privileged, bool security,
Tom Cherry10d086e2019-08-21 14:16:34 -0700121 int (*filter)(const LogBufferElement* element, void* arg) = nullptr,
Mark Salyzynae2abf12017-03-31 10:48:39 -0700122 void* arg = nullptr);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800123
Mark Salyzync5dc9702015-09-16 15:34:00 -0700124 bool clear(log_id_t id, uid_t uid = AID_ROOT);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800125 unsigned long getSize(log_id_t id);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800126 int setSize(log_id_t id, unsigned long size);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800127 unsigned long getSizeUsed(log_id_t id);
Mark Salyzyn004cd3c2016-09-28 08:38:21 -0700128
Mark Salyzynee3b8382015-12-17 09:58:43 -0800129 std::string formatStatistics(uid_t uid, pid_t pid, unsigned int logMask);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800130
Mark Salyzynf5fc5092014-09-21 14:22:18 -0700131 void enableStatistics() {
132 stats.enableStatistics();
133 }
134
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700135 // helper must be protected directly or implicitly by wrlock()/unlock()
Mark Salyzyn501c3732017-03-10 14:31:54 -0800136 const char* pidToName(pid_t pid) {
137 return stats.pidToName(pid);
138 }
Tom Cherry40da03b2019-06-28 13:21:27 -0700139 uid_t pidToUid(pid_t pid) { return stats.pidToUid(pid); }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800140 const char* uidToName(uid_t uid) {
141 return stats.uidToName(uid);
142 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700143 void wrlock() {
144 pthread_rwlock_wrlock(&mLogElementsLock);
145 }
146 void rdlock() {
147 pthread_rwlock_rdlock(&mLogElementsLock);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800148 }
149 void unlock() {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700150 pthread_rwlock_unlock(&mLogElementsLock);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800151 }
Mark Salyzyn9a038632014-04-07 07:05:40 -0700152
Mark Salyzyn501c3732017-03-10 14:31:54 -0800153 private:
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700154 static constexpr size_t minPrune = 4;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700155 static constexpr size_t maxPrune = 256;
156
Mark Salyzyn0175b072014-02-26 09:50:16 -0800157 void maybePrune(log_id_t id);
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700158 void kickMe(LogTimeEntry* me, log_id_t id, unsigned long pruneRows);
159
Mark Salyzync5dc9702015-09-16 15:34:00 -0700160 bool prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700161 LogBufferElementCollection::iterator erase(
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700162 LogBufferElementCollection::iterator it, bool coalesce = false);
Tom Cherry385c2c92020-04-29 17:58:18 -0700163
164 // Returns an iterator to the oldest element for a given log type, or mLogElements.end() if
165 // there are no logs for the given log type. Requires mLogElementsLock to be held.
166 LogBufferElementCollection::iterator GetOldest(log_id_t log_id);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800167
Tom Cherry1a12ae32020-05-01 16:13:18 -0700168 LogTags* tags_;
Tom Cherry5a3db392020-05-01 17:03:20 -0700169 PruneList* prune_;
Tom Cherry1a12ae32020-05-01 16:13:18 -0700170};