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" |
| 30 | #include "LogTimes.h" |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 31 | #include "LogStatistics.h" |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 32 | #include "LogWhiteBlackList.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 33 | |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 34 | // |
Mark Salyzyn | 10b82b6 | 2015-12-28 15:33:01 -0800 | [diff] [blame] | 35 | // 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 Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 40 | // |
| 41 | namespace android { |
| 42 | |
| 43 | static bool isMonotonic(const log_time &mono) { |
Mark Salyzyn | 10b82b6 | 2015-12-28 15:33:01 -0800 | [diff] [blame] | 44 | static const uint32_t EPOCH_PLUS_2_YEARS = 2 * 24 * 60 * 60 * 1461 / 4; |
| 45 | static const uint32_t EPOCH_PLUS_MINUTE = 60; |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 46 | |
Mark Salyzyn | 10b82b6 | 2015-12-28 15:33:01 -0800 | [diff] [blame] | 47 | 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 Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | } |
| 74 | |
Mark Salyzyn | 94a89c4 | 2015-08-19 13:41:51 -0700 | [diff] [blame] | 75 | typedef std::list<LogBufferElement *> LogBufferElementCollection; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 76 | |
| 77 | class LogBuffer { |
| 78 | LogBufferElementCollection mLogElements; |
| 79 | pthread_mutex_t mLogElementsLock; |
| 80 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 81 | LogStatistics stats; |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 82 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 83 | PruneList mPrune; |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 84 | // watermark for last per log id |
| 85 | LogBufferElementCollection::iterator mLast[LOG_ID_MAX]; |
| 86 | bool mLastSet[LOG_ID_MAX]; |
Mark Salyzyn | c892ea3 | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 87 | // watermark of any worst/chatty uid processing |
| 88 | typedef std::unordered_map<uid_t, |
| 89 | 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 |
| 93 | typedef std::unordered_map<pid_t, |
| 94 | LogBufferElementCollection::iterator> |
| 95 | LogBufferPidIteratorMap; |
| 96 | LogBufferPidIteratorMap mLastWorstPidOfSystem[LOG_ID_MAX]; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 97 | |
| 98 | unsigned long mMaxSize[LOG_ID_MAX]; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 99 | |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 100 | bool monotonic; |
| 101 | |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 102 | LogBufferElement* lastLoggedElements[LOG_ID_MAX]; |
| 103 | LogBufferElement* droppedElements[LOG_ID_MAX]; |
| 104 | void log(LogBufferElement* elem); |
| 105 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 106 | public: |
| 107 | LastLogTimes &mTimes; |
| 108 | |
Chih-Hung Hsieh | 034c475 | 2016-07-12 13:50:44 -0700 | [diff] [blame] | 109 | explicit LogBuffer(LastLogTimes *times); |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 110 | ~LogBuffer(); |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 111 | void init(); |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 112 | bool isMonotonic() { return monotonic; } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 113 | |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 114 | 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 Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 117 | uint64_t flushTo(SocketClient *writer, const uint64_t start, |
Mark Salyzyn | 8fa8896 | 2016-01-26 14:32:35 -0800 | [diff] [blame] | 118 | bool privileged, bool security, |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 119 | int (*filter)(const LogBufferElement *element, void *arg) = NULL, |
Mark Salyzyn | 7e2f83c | 2014-03-05 07:41:49 -0800 | [diff] [blame] | 120 | void *arg = NULL); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 121 | |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 122 | bool clear(log_id_t id, uid_t uid = AID_ROOT); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 123 | unsigned long getSize(log_id_t id); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 124 | int setSize(log_id_t id, unsigned long size); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 125 | unsigned long getSizeUsed(log_id_t id); |
Mark Salyzyn | 004cd3c | 2016-09-28 08:38:21 -0700 | [diff] [blame] | 126 | |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 127 | std::string formatStatistics(uid_t uid, pid_t pid, unsigned int logMask); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 128 | |
Mark Salyzyn | f5fc509 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 129 | void enableStatistics() { |
| 130 | stats.enableStatistics(); |
| 131 | } |
| 132 | |
Mark Salyzyn | 73160ac | 2015-08-20 10:01:44 -0700 | [diff] [blame] | 133 | int initPrune(const char *cp) { return mPrune.init(cp); } |
| 134 | std::string formatPrune() { return mPrune.format(); } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 135 | |
Mark Salyzyn | ed777e9 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 136 | // helper must be protected directly or implicitly by lock()/unlock() |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 137 | const char *pidToName(pid_t pid) { return stats.pidToName(pid); } |
Mark Salyzyn | 4ba0387 | 2014-04-07 07:15:33 -0700 | [diff] [blame] | 138 | uid_t pidToUid(pid_t pid) { return stats.pidToUid(pid); } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 139 | const char *uidToName(uid_t uid) { return stats.uidToName(uid); } |
Mark Salyzyn | ed777e9 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 140 | void lock() { pthread_mutex_lock(&mLogElementsLock); } |
| 141 | void unlock() { pthread_mutex_unlock(&mLogElementsLock); } |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 142 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 143 | private: |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 144 | |
Mark Salyzyn | 58b8be8 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 145 | static constexpr size_t minPrune = 4; |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 146 | static constexpr size_t maxPrune = 256; |
| 147 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 148 | void maybePrune(log_id_t id); |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 149 | 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] | 150 | LogBufferElementCollection::iterator erase( |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 151 | LogBufferElementCollection::iterator it, bool coalesce = false); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 152 | }; |
| 153 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 154 | #endif // _LOGD_LOG_BUFFER_H__ |