Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1 | /* |
| 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 Salyzyn | 94a89c4 | 2015-08-19 13:41:51 -0700 | [diff] [blame] | 22 | #include <list> |
Mark Salyzyn | 73160ac | 2015-08-20 10:01:44 -0700 | [diff] [blame] | 23 | #include <string> |
Mark Salyzyn | 94a89c4 | 2015-08-19 13:41:51 -0700 | [diff] [blame] | 24 | |
Mark Salyzyn | 0dd4431 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 25 | #include <android/log.h> |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 26 | #include <private/android_filesystem_config.h> |
Mark Salyzyn | 0dd4431 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 27 | #include <sysutils/SocketClient.h> |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 28 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 29 | #include "LogBufferElement.h" |
Chenjie Luo | fafea32 | 2017-04-27 16:49:09 -0700 | [diff] [blame] | 30 | #include "LogBufferInterface.h" |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 31 | #include "LogStatistics.h" |
Mark Salyzyn | 61e9ce6 | 2016-09-12 14:51:54 -0700 | [diff] [blame] | 32 | #include "LogTags.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 33 | #include "LogTimes.h" |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 34 | #include "LogWhiteBlackList.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 35 | |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 36 | // |
Mark Salyzyn | 10b82b6 | 2015-12-28 15:33:01 -0800 | [diff] [blame] | 37 | // 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 Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 42 | // |
| 43 | namespace android { |
| 44 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 45 | static bool isMonotonic(const log_time& mono) { |
Mark Salyzyn | 10b82b6 | 2015-12-28 15:33:01 -0800 | [diff] [blame] | 46 | static const uint32_t EPOCH_PLUS_2_YEARS = 2 * 24 * 60 * 60 * 1461 / 4; |
| 47 | static const uint32_t EPOCH_PLUS_MINUTE = 60; |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 48 | |
Mark Salyzyn | 10b82b6 | 2015-12-28 15:33:01 -0800 | [diff] [blame] | 49 | 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 Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 73 | } |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 76 | typedef std::list<LogBufferElement*> LogBufferElementCollection; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 77 | |
Chenjie Luo | fafea32 | 2017-04-27 16:49:09 -0700 | [diff] [blame] | 78 | class LogBuffer : public LogBufferInterface { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 79 | LogBufferElementCollection mLogElements; |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 80 | pthread_rwlock_t mLogElementsLock; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 81 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 82 | LogStatistics stats; |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 83 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 84 | PruneList mPrune; |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 85 | // watermark for last per log id |
| 86 | LogBufferElementCollection::iterator mLast[LOG_ID_MAX]; |
| 87 | bool mLastSet[LOG_ID_MAX]; |
Mark Salyzyn | c892ea3 | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 88 | // watermark of any worst/chatty uid processing |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 89 | typedef std::unordered_map<uid_t, LogBufferElementCollection::iterator> |
| 90 | LogBufferIteratorMap; |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 91 | LogBufferIteratorMap mLastWorst[LOG_ID_MAX]; |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 92 | // watermark of any worst/chatty pid of system processing |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 93 | typedef std::unordered_map<pid_t, LogBufferElementCollection::iterator> |
| 94 | LogBufferPidIteratorMap; |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 95 | LogBufferPidIteratorMap mLastWorstPidOfSystem[LOG_ID_MAX]; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 96 | |
| 97 | unsigned long mMaxSize[LOG_ID_MAX]; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 98 | |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 99 | bool monotonic; |
| 100 | |
Mark Salyzyn | 61e9ce6 | 2016-09-12 14:51:54 -0700 | [diff] [blame] | 101 | LogTags tags; |
| 102 | |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 103 | LogBufferElement* lastLoggedElements[LOG_ID_MAX]; |
| 104 | LogBufferElement* droppedElements[LOG_ID_MAX]; |
| 105 | void log(LogBufferElement* elem); |
| 106 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 107 | public: |
| 108 | LastLogTimes& mTimes; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 109 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 110 | explicit LogBuffer(LastLogTimes* times); |
Chenjie Luo | fafea32 | 2017-04-27 16:49:09 -0700 | [diff] [blame] | 111 | ~LogBuffer() override; |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 112 | void init(); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 113 | bool isMonotonic() { |
| 114 | return monotonic; |
| 115 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 116 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 117 | int log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid, |
Chih-Hung Hsieh | 08d470b | 2018-08-13 14:22:56 -0700 | [diff] [blame^] | 118 | const char* msg, uint16_t len) override; |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 119 | // 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 Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 122 | log_time flushTo(SocketClient* writer, const log_time& start, |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 123 | pid_t* lastTid, // &lastTid[LOG_ID_MAX] or nullptr |
Mark Salyzyn | 8fa8896 | 2016-01-26 14:32:35 -0800 | [diff] [blame] | 124 | bool privileged, bool security, |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 125 | int (*filter)(const LogBufferElement* element, |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 126 | void* arg) = nullptr, |
| 127 | void* arg = nullptr); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 128 | |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 129 | bool clear(log_id_t id, uid_t uid = AID_ROOT); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 130 | unsigned long getSize(log_id_t id); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 131 | int setSize(log_id_t id, unsigned long size); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 132 | unsigned long getSizeUsed(log_id_t id); |
Mark Salyzyn | 004cd3c | 2016-09-28 08:38:21 -0700 | [diff] [blame] | 133 | |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 134 | std::string formatStatistics(uid_t uid, pid_t pid, unsigned int logMask); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 135 | |
Mark Salyzyn | f5fc509 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 136 | void enableStatistics() { |
| 137 | stats.enableStatistics(); |
| 138 | } |
| 139 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 140 | int initPrune(const char* cp) { |
| 141 | return mPrune.init(cp); |
| 142 | } |
| 143 | std::string formatPrune() { |
| 144 | return mPrune.format(); |
| 145 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 146 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 147 | std::string formatGetEventTag(uid_t uid, const char* name, |
| 148 | const char* format) { |
Mark Salyzyn | 61e9ce6 | 2016-09-12 14:51:54 -0700 | [diff] [blame] | 149 | return tags.formatGetEventTag(uid, name, format); |
| 150 | } |
Mark Salyzyn | 407537f | 2017-02-21 16:19:08 -0800 | [diff] [blame] | 151 | std::string formatEntry(uint32_t tag, uid_t uid) { |
| 152 | return tags.formatEntry(tag, uid); |
| 153 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 154 | const char* tagToName(uint32_t tag) { |
| 155 | return tags.tagToName(tag); |
| 156 | } |
Mark Salyzyn | 61e9ce6 | 2016-09-12 14:51:54 -0700 | [diff] [blame] | 157 | |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 158 | // helper must be protected directly or implicitly by wrlock()/unlock() |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 159 | const char* pidToName(pid_t pid) { |
| 160 | return stats.pidToName(pid); |
| 161 | } |
Mark Salyzyn | c4e4823 | 2017-05-04 13:54:46 -0700 | [diff] [blame] | 162 | virtual uid_t pidToUid(pid_t pid) override { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 163 | return stats.pidToUid(pid); |
| 164 | } |
Mark Salyzyn | c4e4823 | 2017-05-04 13:54:46 -0700 | [diff] [blame] | 165 | virtual pid_t tidToPid(pid_t tid) override { |
| 166 | return stats.tidToPid(tid); |
| 167 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 168 | const char* uidToName(uid_t uid) { |
| 169 | return stats.uidToName(uid); |
| 170 | } |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 171 | void wrlock() { |
| 172 | pthread_rwlock_wrlock(&mLogElementsLock); |
| 173 | } |
| 174 | void rdlock() { |
| 175 | pthread_rwlock_rdlock(&mLogElementsLock); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 176 | } |
| 177 | void unlock() { |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 178 | pthread_rwlock_unlock(&mLogElementsLock); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 179 | } |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 180 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 181 | private: |
Mark Salyzyn | 58b8be8 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 182 | static constexpr size_t minPrune = 4; |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 183 | static constexpr size_t maxPrune = 256; |
Mark Salyzyn | 5836379 | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 184 | static const log_time pruneMargin; |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 185 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 186 | void maybePrune(log_id_t id); |
Mark Salyzyn | 0878a7c | 2017-05-11 13:28:33 -0700 | [diff] [blame] | 187 | bool isBusy(log_time watermark); |
| 188 | void kickMe(LogTimeEntry* me, log_id_t id, unsigned long pruneRows); |
| 189 | |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 190 | bool prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT); |
Mark Salyzyn | 831aa29 | 2015-09-03 16:08:50 -0700 | [diff] [blame] | 191 | LogBufferElementCollection::iterator erase( |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 192 | LogBufferElementCollection::iterator it, bool coalesce = false); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 193 | }; |
| 194 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 195 | #endif // _LOGD_LOG_BUFFER_H__ |