blob: 09429879988aead2890aadca856e95c3cd4988d8 [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
2 * Copyright (C) 2012-2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _LOGD_LOG_BUFFER_H__
18#define _LOGD_LOG_BUFFER_H__
19
20#include <sys/types.h>
21
Mark Salyzyn94a89c42015-08-19 13:41:51 -070022#include <list>
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"
Chenjie Luofafea322017-04-27 16:49:09 -070030#include "LogBufferInterface.h"
Mark Salyzyn501c3732017-03-10 14:31:54 -080031#include "LogStatistics.h"
Mark Salyzyn61e9ce62016-09-12 14:51:54 -070032#include "LogTags.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080033#include "LogTimes.h"
Mark Salyzyndfa7a072014-02-11 12:29:31 -080034#include "LogWhiteBlackList.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080035
Mark Salyzynb6bee332015-09-08 08:56:32 -070036//
Mark Salyzyn10b82b62015-12-28 15:33:01 -080037// We are either in 1970ish (MONOTONIC) or 2016+ish (REALTIME) so to
38// differentiate without prejudice, we use 1972 to delineate, earlier
39// is likely monotonic, later is real. Otherwise we start using a
40// dividing line between monotonic and realtime if more than a minute
41// difference between them.
Mark Salyzynb6bee332015-09-08 08:56:32 -070042//
43namespace android {
44
Mark Salyzyn501c3732017-03-10 14:31:54 -080045static bool isMonotonic(const log_time& mono) {
Mark Salyzyn10b82b62015-12-28 15:33:01 -080046 static const uint32_t EPOCH_PLUS_2_YEARS = 2 * 24 * 60 * 60 * 1461 / 4;
47 static const uint32_t EPOCH_PLUS_MINUTE = 60;
Mark Salyzynb6bee332015-09-08 08:56:32 -070048
Mark Salyzyn10b82b62015-12-28 15:33:01 -080049 if (mono.tv_sec >= EPOCH_PLUS_2_YEARS) {
50 return false;
51 }
52
53 log_time now(CLOCK_REALTIME);
54
55 /* Timezone and ntp time setup? */
56 if (now.tv_sec >= EPOCH_PLUS_2_YEARS) {
57 return true;
58 }
59
60 /* no way to differentiate realtime from monotonic time */
61 if (now.tv_sec < EPOCH_PLUS_MINUTE) {
62 return false;
63 }
64
65 log_time cpu(CLOCK_MONOTONIC);
66 /* too close to call to differentiate monotonic times from realtime */
67 if ((cpu.tv_sec + EPOCH_PLUS_MINUTE) >= now.tv_sec) {
68 return false;
69 }
70
71 /* dividing line half way between monotonic and realtime */
72 return mono.tv_sec < ((cpu.tv_sec + now.tv_sec) / 2);
Mark Salyzynb6bee332015-09-08 08:56:32 -070073}
Mark Salyzynb6bee332015-09-08 08:56:32 -070074}
75
Mark Salyzyn501c3732017-03-10 14:31:54 -080076typedef std::list<LogBufferElement*> LogBufferElementCollection;
Mark Salyzyn0175b072014-02-26 09:50:16 -080077
Chenjie Luofafea322017-04-27 16:49:09 -070078class LogBuffer : public LogBufferInterface {
Mark Salyzyn0175b072014-02-26 09:50:16 -080079 LogBufferElementCollection mLogElements;
Mark Salyzyn3c501b52017-04-18 14:09:45 -070080 pthread_rwlock_t mLogElementsLock;
Mark Salyzyn0175b072014-02-26 09:50:16 -080081
Mark Salyzyn34facab2014-02-06 14:48:50 -080082 LogStatistics stats;
Mark Salyzyne457b742014-02-19 17:18:31 -080083
Mark Salyzyndfa7a072014-02-11 12:29:31 -080084 PruneList mPrune;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -080085 // watermark for last per log id
86 LogBufferElementCollection::iterator mLast[LOG_ID_MAX];
87 bool mLastSet[LOG_ID_MAX];
Mark Salyzync892ea32015-08-19 17:06:11 -070088 // watermark of any worst/chatty uid processing
Mark Salyzyn501c3732017-03-10 14:31:54 -080089 typedef std::unordered_map<uid_t, LogBufferElementCollection::iterator>
90 LogBufferIteratorMap;
Mark Salyzyn6a066942016-07-14 15:34:30 -070091 LogBufferIteratorMap mLastWorst[LOG_ID_MAX];
Mark Salyzynbec3c3d2015-08-28 08:02:59 -070092 // watermark of any worst/chatty pid of system processing
Mark Salyzyn501c3732017-03-10 14:31:54 -080093 typedef std::unordered_map<pid_t, LogBufferElementCollection::iterator>
94 LogBufferPidIteratorMap;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -070095 LogBufferPidIteratorMap mLastWorstPidOfSystem[LOG_ID_MAX];
Mark Salyzyndfa7a072014-02-11 12:29:31 -080096
97 unsigned long mMaxSize[LOG_ID_MAX];
Mark Salyzyndfa7a072014-02-11 12:29:31 -080098
Mark Salyzynb6bee332015-09-08 08:56:32 -070099 bool monotonic;
100
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700101 LogTags tags;
102
Mark Salyzyna2c02222016-12-13 10:31:29 -0800103 LogBufferElement* lastLoggedElements[LOG_ID_MAX];
104 LogBufferElement* droppedElements[LOG_ID_MAX];
105 void log(LogBufferElement* elem);
106
Mark Salyzyn501c3732017-03-10 14:31:54 -0800107 public:
108 LastLogTimes& mTimes;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800109
Mark Salyzyn501c3732017-03-10 14:31:54 -0800110 explicit LogBuffer(LastLogTimes* times);
Chenjie Luofafea322017-04-27 16:49:09 -0700111 ~LogBuffer() override;
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700112 void init();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800113 bool isMonotonic() {
114 return monotonic;
115 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800116
Mark Salyzyn501c3732017-03-10 14:31:54 -0800117 int log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid,
Chenjie Luofafea322017-04-27 16:49:09 -0700118 const char* msg, unsigned short len) override;
Mark Salyzynae2abf12017-03-31 10:48:39 -0700119 // lastTid is an optional context to help detect if the last previous
120 // valid message was from the same source so we can differentiate chatty
121 // filter types (identical or expired)
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800122 log_time flushTo(SocketClient* writer, const log_time& start,
Mark Salyzynae2abf12017-03-31 10:48:39 -0700123 pid_t* lastTid, // &lastTid[LOG_ID_MAX] or nullptr
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800124 bool privileged, bool security,
Mark Salyzyn501c3732017-03-10 14:31:54 -0800125 int (*filter)(const LogBufferElement* element,
Mark Salyzynae2abf12017-03-31 10:48:39 -0700126 void* arg) = nullptr,
127 void* arg = nullptr);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800128
Mark Salyzync5dc9702015-09-16 15:34:00 -0700129 bool clear(log_id_t id, uid_t uid = AID_ROOT);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800130 unsigned long getSize(log_id_t id);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800131 int setSize(log_id_t id, unsigned long size);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800132 unsigned long getSizeUsed(log_id_t id);
Mark Salyzyn004cd3c2016-09-28 08:38:21 -0700133
Mark Salyzynee3b8382015-12-17 09:58:43 -0800134 std::string formatStatistics(uid_t uid, pid_t pid, unsigned int logMask);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800135
Mark Salyzynf5fc5092014-09-21 14:22:18 -0700136 void enableStatistics() {
137 stats.enableStatistics();
138 }
139
Mark Salyzyn501c3732017-03-10 14:31:54 -0800140 int initPrune(const char* cp) {
141 return mPrune.init(cp);
142 }
143 std::string formatPrune() {
144 return mPrune.format();
145 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800146
Mark Salyzyn501c3732017-03-10 14:31:54 -0800147 std::string formatGetEventTag(uid_t uid, const char* name,
148 const char* format) {
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700149 return tags.formatGetEventTag(uid, name, format);
150 }
Mark Salyzyn407537f2017-02-21 16:19:08 -0800151 std::string formatEntry(uint32_t tag, uid_t uid) {
152 return tags.formatEntry(tag, uid);
153 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800154 const char* tagToName(uint32_t tag) {
155 return tags.tagToName(tag);
156 }
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700157
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700158 // helper must be protected directly or implicitly by wrlock()/unlock()
Mark Salyzyn501c3732017-03-10 14:31:54 -0800159 const char* pidToName(pid_t pid) {
160 return stats.pidToName(pid);
161 }
Mark Salyzync4e48232017-05-04 13:54:46 -0700162 virtual uid_t pidToUid(pid_t pid) override {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800163 return stats.pidToUid(pid);
164 }
Mark Salyzync4e48232017-05-04 13:54:46 -0700165 virtual pid_t tidToPid(pid_t tid) override {
166 return stats.tidToPid(tid);
167 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800168 const char* uidToName(uid_t uid) {
169 return stats.uidToName(uid);
170 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700171 void wrlock() {
172 pthread_rwlock_wrlock(&mLogElementsLock);
173 }
174 void rdlock() {
175 pthread_rwlock_rdlock(&mLogElementsLock);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800176 }
177 void unlock() {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700178 pthread_rwlock_unlock(&mLogElementsLock);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800179 }
Mark Salyzyn9a038632014-04-07 07:05:40 -0700180
Mark Salyzyn501c3732017-03-10 14:31:54 -0800181 private:
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700182 static constexpr size_t minPrune = 4;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700183 static constexpr size_t maxPrune = 256;
Mark Salyzyn58363792017-04-17 12:46:12 -0700184 static const log_time pruneMargin;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700185
Mark Salyzyn0175b072014-02-26 09:50:16 -0800186 void maybePrune(log_id_t id);
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700187 bool isBusy(log_time watermark);
188 void kickMe(LogTimeEntry* me, log_id_t id, unsigned long pruneRows);
189
Mark Salyzync5dc9702015-09-16 15:34:00 -0700190 bool prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700191 LogBufferElementCollection::iterator erase(
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700192 LogBufferElementCollection::iterator it, bool coalesce = false);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800193};
194
Mark Salyzyn501c3732017-03-10 14:31:54 -0800195#endif // _LOGD_LOG_BUFFER_H__