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