Mark Salyzyn | 12bac90 | 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 | 277a6d2 | 2015-08-19 13:41:51 -0700 | [diff] [blame] | 22 | #include <list> |
Mark Salyzyn | 1c7b6fb | 2015-08-20 10:01:44 -0700 | [diff] [blame] | 23 | #include <string> |
Mark Salyzyn | 277a6d2 | 2015-08-19 13:41:51 -0700 | [diff] [blame] | 24 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 25 | #include <log/log.h> |
| 26 | #include <sysutils/SocketClient.h> |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 27 | |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 28 | #include <private/android_filesystem_config.h> |
| 29 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 30 | #include "LogBufferElement.h" |
| 31 | #include "LogTimes.h" |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 32 | #include "LogStatistics.h" |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 33 | #include "LogWhiteBlackList.h" |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 34 | |
Mark Salyzyn | 277a6d2 | 2015-08-19 13:41:51 -0700 | [diff] [blame] | 35 | typedef std::list<LogBufferElement *> LogBufferElementCollection; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 36 | |
| 37 | class LogBuffer { |
| 38 | LogBufferElementCollection mLogElements; |
| 39 | pthread_mutex_t mLogElementsLock; |
| 40 | |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 41 | LogStatistics stats; |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 42 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 43 | PruneList mPrune; |
Mark Salyzyn | cfcffdaf | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 44 | // watermark of any worst/chatty uid processing |
| 45 | typedef std::unordered_map<uid_t, |
| 46 | LogBufferElementCollection::iterator> |
| 47 | LogBufferIteratorMap; |
| 48 | LogBufferIteratorMap mLastWorstUid[LOG_ID_MAX]; |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 49 | |
| 50 | unsigned long mMaxSize[LOG_ID_MAX]; |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 51 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 52 | public: |
| 53 | LastLogTimes &mTimes; |
| 54 | |
| 55 | LogBuffer(LastLogTimes *times); |
Mark Salyzyn | 3fe2593 | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 56 | void init(); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 57 | |
Mark Salyzyn | 88fe7cc | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 58 | int log(log_id_t log_id, log_time realtime, |
| 59 | uid_t uid, pid_t pid, pid_t tid, |
| 60 | const char *msg, unsigned short len); |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 61 | uint64_t flushTo(SocketClient *writer, const uint64_t start, |
Mark Salyzyn | 7139117 | 2014-03-05 07:41:49 -0800 | [diff] [blame] | 62 | bool privileged, |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 63 | int (*filter)(const LogBufferElement *element, void *arg) = NULL, |
Mark Salyzyn | 7139117 | 2014-03-05 07:41:49 -0800 | [diff] [blame] | 64 | void *arg = NULL); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 65 | |
Mark Salyzyn | 7702c3e | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 66 | bool clear(log_id_t id, uid_t uid = AID_ROOT); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 67 | unsigned long getSize(log_id_t id); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 68 | int setSize(log_id_t id, unsigned long size); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 69 | unsigned long getSizeUsed(log_id_t id); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 70 | // *strp uses malloc, use free to release. |
Mark Salyzyn | 1c7b6fb | 2015-08-20 10:01:44 -0700 | [diff] [blame] | 71 | std::string formatStatistics(uid_t uid, unsigned int logMask); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 72 | |
Mark Salyzyn | 6b4a4d0 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 73 | void enableStatistics() { |
| 74 | stats.enableStatistics(); |
| 75 | } |
| 76 | |
Mark Salyzyn | 1c7b6fb | 2015-08-20 10:01:44 -0700 | [diff] [blame] | 77 | int initPrune(const char *cp) { return mPrune.init(cp); } |
| 78 | std::string formatPrune() { return mPrune.format(); } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 79 | |
Mark Salyzyn | 10acf78 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 80 | // helper must be protected directly or implicitly by lock()/unlock() |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 81 | const char *pidToName(pid_t pid) { return stats.pidToName(pid); } |
Mark Salyzyn | f67c379 | 2014-04-07 07:15:33 -0700 | [diff] [blame] | 82 | uid_t pidToUid(pid_t pid) { return stats.pidToUid(pid); } |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 83 | const char *uidToName(uid_t uid) { return stats.uidToName(uid); } |
Mark Salyzyn | 10acf78 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 84 | void lock() { pthread_mutex_lock(&mLogElementsLock); } |
| 85 | void unlock() { pthread_mutex_unlock(&mLogElementsLock); } |
Mark Salyzyn | 5766f80 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 86 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 87 | private: |
Mark Salyzyn | 95ff248 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 88 | |
| 89 | static constexpr size_t maxPrune = 256; |
| 90 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 91 | void maybePrune(log_id_t id); |
Mark Salyzyn | 7702c3e | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 92 | bool prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT); |
Mark Salyzyn | 57b82a6 | 2015-09-03 16:08:50 -0700 | [diff] [blame] | 93 | LogBufferElementCollection::iterator erase( |
Mark Salyzyn | 95ff248 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 94 | LogBufferElementCollection::iterator it, bool coalesce = false); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 95 | }; |
| 96 | |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 97 | #endif // _LOGD_LOG_BUFFER_H__ |