blob: 932d55f9aa421060919c650b0b8ea4dd1a818d63 [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"
30#include "LogTimes.h"
Mark Salyzyn34facab2014-02-06 14:48:50 -080031#include "LogStatistics.h"
Mark Salyzyndfa7a072014-02-11 12:29:31 -080032#include "LogWhiteBlackList.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080033
Mark Salyzynb6bee332015-09-08 08:56:32 -070034//
Mark Salyzyn10b82b62015-12-28 15:33:01 -080035// We are either in 1970ish (MONOTONIC) or 2016+ish (REALTIME) so to
36// differentiate without prejudice, we use 1972 to delineate, earlier
37// is likely monotonic, later is real. Otherwise we start using a
38// dividing line between monotonic and realtime if more than a minute
39// difference between them.
Mark Salyzynb6bee332015-09-08 08:56:32 -070040//
41namespace android {
42
43static bool isMonotonic(const log_time &mono) {
Mark Salyzyn10b82b62015-12-28 15:33:01 -080044 static const uint32_t EPOCH_PLUS_2_YEARS = 2 * 24 * 60 * 60 * 1461 / 4;
45 static const uint32_t EPOCH_PLUS_MINUTE = 60;
Mark Salyzynb6bee332015-09-08 08:56:32 -070046
Mark Salyzyn10b82b62015-12-28 15:33:01 -080047 if (mono.tv_sec >= EPOCH_PLUS_2_YEARS) {
48 return false;
49 }
50
51 log_time now(CLOCK_REALTIME);
52
53 /* Timezone and ntp time setup? */
54 if (now.tv_sec >= EPOCH_PLUS_2_YEARS) {
55 return true;
56 }
57
58 /* no way to differentiate realtime from monotonic time */
59 if (now.tv_sec < EPOCH_PLUS_MINUTE) {
60 return false;
61 }
62
63 log_time cpu(CLOCK_MONOTONIC);
64 /* too close to call to differentiate monotonic times from realtime */
65 if ((cpu.tv_sec + EPOCH_PLUS_MINUTE) >= now.tv_sec) {
66 return false;
67 }
68
69 /* dividing line half way between monotonic and realtime */
70 return mono.tv_sec < ((cpu.tv_sec + now.tv_sec) / 2);
Mark Salyzynb6bee332015-09-08 08:56:32 -070071}
72
73}
74
Mark Salyzyn94a89c42015-08-19 13:41:51 -070075typedef std::list<LogBufferElement *> LogBufferElementCollection;
Mark Salyzyn0175b072014-02-26 09:50:16 -080076
77class LogBuffer {
78 LogBufferElementCollection mLogElements;
79 pthread_mutex_t mLogElementsLock;
80
Mark Salyzyn34facab2014-02-06 14:48:50 -080081 LogStatistics stats;
Mark Salyzyne457b742014-02-19 17:18:31 -080082
Mark Salyzyndfa7a072014-02-11 12:29:31 -080083 PruneList mPrune;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -080084 // watermark for last per log id
85 LogBufferElementCollection::iterator mLast[LOG_ID_MAX];
86 bool mLastSet[LOG_ID_MAX];
Mark Salyzync892ea32015-08-19 17:06:11 -070087 // watermark of any worst/chatty uid processing
88 typedef std::unordered_map<uid_t,
89 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
93 typedef std::unordered_map<pid_t,
94 LogBufferElementCollection::iterator>
95 LogBufferPidIteratorMap;
96 LogBufferPidIteratorMap mLastWorstPidOfSystem[LOG_ID_MAX];
Mark Salyzyndfa7a072014-02-11 12:29:31 -080097
98 unsigned long mMaxSize[LOG_ID_MAX];
Mark Salyzyndfa7a072014-02-11 12:29:31 -080099
Mark Salyzynb6bee332015-09-08 08:56:32 -0700100 bool monotonic;
101
Mark Salyzyna2c02222016-12-13 10:31:29 -0800102 LogBufferElement* lastLoggedElements[LOG_ID_MAX];
103 LogBufferElement* droppedElements[LOG_ID_MAX];
104 void log(LogBufferElement* elem);
105
Mark Salyzyn0175b072014-02-26 09:50:16 -0800106public:
107 LastLogTimes &mTimes;
108
Chih-Hung Hsieh034c4752016-07-12 13:50:44 -0700109 explicit LogBuffer(LastLogTimes *times);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800110 ~LogBuffer();
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700111 void init();
Mark Salyzynb6bee332015-09-08 08:56:32 -0700112 bool isMonotonic() { return monotonic; }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800113
Mark Salyzyn202e1532015-02-09 08:21:05 -0800114 int 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 Salyzynf7c0f752015-03-03 13:39:37 -0800117 uint64_t flushTo(SocketClient *writer, const uint64_t start,
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800118 bool privileged, bool security,
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800119 int (*filter)(const LogBufferElement *element, void *arg) = NULL,
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -0800120 void *arg = NULL);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800121
Mark Salyzync5dc9702015-09-16 15:34:00 -0700122 bool clear(log_id_t id, uid_t uid = AID_ROOT);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800123 unsigned long getSize(log_id_t id);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800124 int setSize(log_id_t id, unsigned long size);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800125 unsigned long getSizeUsed(log_id_t id);
Mark Salyzyn004cd3c2016-09-28 08:38:21 -0700126
Mark Salyzynee3b8382015-12-17 09:58:43 -0800127 std::string formatStatistics(uid_t uid, pid_t pid, unsigned int logMask);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800128
Mark Salyzynf5fc5092014-09-21 14:22:18 -0700129 void enableStatistics() {
130 stats.enableStatistics();
131 }
132
Mark Salyzyn73160ac2015-08-20 10:01:44 -0700133 int initPrune(const char *cp) { return mPrune.init(cp); }
134 std::string formatPrune() { return mPrune.format(); }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800135
Mark Salyzyned777e92015-06-24 16:22:54 -0700136 // helper must be protected directly or implicitly by lock()/unlock()
Mark Salyzyn758058f2015-08-21 16:44:30 -0700137 const char *pidToName(pid_t pid) { return stats.pidToName(pid); }
Mark Salyzyn4ba03872014-04-07 07:15:33 -0700138 uid_t pidToUid(pid_t pid) { return stats.pidToUid(pid); }
Mark Salyzyn758058f2015-08-21 16:44:30 -0700139 const char *uidToName(uid_t uid) { return stats.uidToName(uid); }
Mark Salyzyned777e92015-06-24 16:22:54 -0700140 void lock() { pthread_mutex_lock(&mLogElementsLock); }
141 void unlock() { pthread_mutex_unlock(&mLogElementsLock); }
Mark Salyzyn9a038632014-04-07 07:05:40 -0700142
Mark Salyzyn0175b072014-02-26 09:50:16 -0800143private:
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700144
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700145 static constexpr size_t minPrune = 4;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700146 static constexpr size_t maxPrune = 256;
147
Mark Salyzyn0175b072014-02-26 09:50:16 -0800148 void maybePrune(log_id_t id);
Mark Salyzync5dc9702015-09-16 15:34:00 -0700149 bool prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700150 LogBufferElementCollection::iterator erase(
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700151 LogBufferElementCollection::iterator it, bool coalesce = false);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800152};
153
Mark Salyzyn34facab2014-02-06 14:48:50 -0800154#endif // _LOGD_LOG_BUFFER_H__