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 | // |
| 36 | // We are either in 1970ish (MONOTONIC) or 2015+ish (REALTIME) so to |
| 37 | // differentiate without prejudice, we use 1980 to delineate, earlier |
| 38 | // is monotonic, later is real. |
| 39 | // |
| 40 | namespace android { |
| 41 | |
| 42 | static bool isMonotonic(const log_time &mono) { |
| 43 | static const uint32_t EPOCH_PLUS_10_YEARS = 10 * 1461 / 4 * 24 * 60 * 60; |
| 44 | |
| 45 | return mono.tv_sec < EPOCH_PLUS_10_YEARS; |
| 46 | } |
| 47 | |
| 48 | } |
| 49 | |
Mark Salyzyn | 94a89c4 | 2015-08-19 13:41:51 -0700 | [diff] [blame] | 50 | typedef std::list<LogBufferElement *> LogBufferElementCollection; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 51 | |
| 52 | class LogBuffer { |
| 53 | LogBufferElementCollection mLogElements; |
| 54 | pthread_mutex_t mLogElementsLock; |
| 55 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 56 | LogStatistics stats; |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 57 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 58 | PruneList mPrune; |
Mark Salyzyn | c892ea3 | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 59 | // watermark of any worst/chatty uid processing |
| 60 | typedef std::unordered_map<uid_t, |
| 61 | LogBufferElementCollection::iterator> |
| 62 | LogBufferIteratorMap; |
| 63 | LogBufferIteratorMap mLastWorstUid[LOG_ID_MAX]; |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame^] | 64 | // watermark of any worst/chatty pid of system processing |
| 65 | typedef std::unordered_map<pid_t, |
| 66 | LogBufferElementCollection::iterator> |
| 67 | LogBufferPidIteratorMap; |
| 68 | LogBufferPidIteratorMap mLastWorstPidOfSystem[LOG_ID_MAX]; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 69 | |
| 70 | unsigned long mMaxSize[LOG_ID_MAX]; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 71 | |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 72 | bool monotonic; |
| 73 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 74 | public: |
| 75 | LastLogTimes &mTimes; |
| 76 | |
| 77 | LogBuffer(LastLogTimes *times); |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 78 | void init(); |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 79 | bool isMonotonic() { return monotonic; } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 80 | |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 81 | int log(log_id_t log_id, log_time realtime, |
| 82 | uid_t uid, pid_t pid, pid_t tid, |
| 83 | const char *msg, unsigned short len); |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 84 | uint64_t flushTo(SocketClient *writer, const uint64_t start, |
Mark Salyzyn | 7e2f83c | 2014-03-05 07:41:49 -0800 | [diff] [blame] | 85 | bool privileged, |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 86 | int (*filter)(const LogBufferElement *element, void *arg) = NULL, |
Mark Salyzyn | 7e2f83c | 2014-03-05 07:41:49 -0800 | [diff] [blame] | 87 | void *arg = NULL); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 88 | |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 89 | bool clear(log_id_t id, uid_t uid = AID_ROOT); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 90 | unsigned long getSize(log_id_t id); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 91 | int setSize(log_id_t id, unsigned long size); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 92 | unsigned long getSizeUsed(log_id_t id); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 93 | // *strp uses malloc, use free to release. |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 94 | std::string formatStatistics(uid_t uid, pid_t pid, unsigned int logMask); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 95 | |
Mark Salyzyn | f5fc509 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 96 | void enableStatistics() { |
| 97 | stats.enableStatistics(); |
| 98 | } |
| 99 | |
Mark Salyzyn | 73160ac | 2015-08-20 10:01:44 -0700 | [diff] [blame] | 100 | int initPrune(const char *cp) { return mPrune.init(cp); } |
| 101 | std::string formatPrune() { return mPrune.format(); } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 102 | |
Mark Salyzyn | ed777e9 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 103 | // helper must be protected directly or implicitly by lock()/unlock() |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 104 | const char *pidToName(pid_t pid) { return stats.pidToName(pid); } |
Mark Salyzyn | 4ba0387 | 2014-04-07 07:15:33 -0700 | [diff] [blame] | 105 | uid_t pidToUid(pid_t pid) { return stats.pidToUid(pid); } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 106 | const char *uidToName(uid_t uid) { return stats.uidToName(uid); } |
Mark Salyzyn | ed777e9 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 107 | void lock() { pthread_mutex_lock(&mLogElementsLock); } |
| 108 | void unlock() { pthread_mutex_unlock(&mLogElementsLock); } |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 109 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 110 | private: |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 111 | |
Mark Salyzyn | 58b8be8 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 112 | static constexpr size_t minPrune = 4; |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 113 | static constexpr size_t maxPrune = 256; |
| 114 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 115 | void maybePrune(log_id_t id); |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 116 | 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] | 117 | LogBufferElementCollection::iterator erase( |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 118 | LogBufferElementCollection::iterator it, bool coalesce = false); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 119 | }; |
| 120 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 121 | #endif // _LOGD_LOG_BUFFER_H__ |