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 | |
Tom Cherry | 04ad458 | 2020-05-01 16:13:18 -0700 | [diff] [blame] | 17 | #pragma once |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 18 | |
| 19 | #include <sys/types.h> |
| 20 | |
Mark Salyzyn | 9a400aa | 2015-08-19 13:41:51 -0700 | [diff] [blame] | 21 | #include <list> |
Tom Cherry | 402364e | 2020-04-29 17:58:18 -0700 | [diff] [blame] | 22 | #include <optional> |
Mark Salyzyn | 7c528d9 | 2015-08-20 10:01:44 -0700 | [diff] [blame] | 23 | #include <string> |
Mark Salyzyn | 9a400aa | 2015-08-19 13:41:51 -0700 | [diff] [blame] | 24 | |
Mark Salyzyn | 224b86f | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 25 | #include <android/log.h> |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 26 | #include <private/android_filesystem_config.h> |
Mark Salyzyn | 224b86f | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 27 | #include <sysutils/SocketClient.h> |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 28 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 29 | #include "LogBufferElement.h" |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 30 | #include "LogStatistics.h" |
Mark Salyzyn | 9472b4d | 2016-09-12 14:51:54 -0700 | [diff] [blame] | 31 | #include "LogTags.h" |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 32 | #include "LogWhiteBlackList.h" |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 33 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 34 | typedef std::list<LogBufferElement*> LogBufferElementCollection; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 35 | |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 36 | class LogReaderList; |
| 37 | class LogReaderThread; |
| 38 | |
| 39 | enum class FlushToResult { |
| 40 | kSkip, |
| 41 | kStop, |
| 42 | kWrite, |
| 43 | }; |
| 44 | |
Tom Cherry | c49573f | 2019-06-28 13:37:10 -0700 | [diff] [blame] | 45 | class LogBuffer { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 46 | LogBufferElementCollection mLogElements; |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 47 | pthread_rwlock_t mLogElementsLock; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 48 | |
Mark Salyzyn | 5af07be | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 49 | // watermark of any worst/chatty uid processing |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 50 | typedef std::unordered_map<uid_t, LogBufferElementCollection::iterator> |
| 51 | LogBufferIteratorMap; |
Mark Salyzyn | 6fa0d03 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 52 | LogBufferIteratorMap mLastWorst[LOG_ID_MAX]; |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 53 | // watermark of any worst/chatty pid of system processing |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 54 | typedef std::unordered_map<pid_t, LogBufferElementCollection::iterator> |
| 55 | LogBufferPidIteratorMap; |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 56 | LogBufferPidIteratorMap mLastWorstPidOfSystem[LOG_ID_MAX]; |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 57 | |
| 58 | unsigned long mMaxSize[LOG_ID_MAX]; |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 59 | |
Mark Salyzyn | 24a57dc | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 60 | LogBufferElement* lastLoggedElements[LOG_ID_MAX]; |
| 61 | LogBufferElement* droppedElements[LOG_ID_MAX]; |
| 62 | void log(LogBufferElement* elem); |
| 63 | |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 64 | public: |
| 65 | LogBuffer(LogReaderList* reader_list, LogTags* tags, PruneList* prune, LogStatistics* stats); |
Tom Cherry | c49573f | 2019-06-28 13:37:10 -0700 | [diff] [blame] | 66 | ~LogBuffer(); |
Mark Salyzyn | 3fe2593 | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 67 | void init(); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 68 | |
Tom Cherry | c49573f | 2019-06-28 13:37:10 -0700 | [diff] [blame] | 69 | int log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid, const char* msg, |
| 70 | uint16_t len); |
Mark Salyzyn | bec7b2d | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 71 | // lastTid is an optional context to help detect if the last previous |
| 72 | // valid message was from the same source so we can differentiate chatty |
| 73 | // filter types (identical or expired) |
Tom Cherry | ac228f1 | 2019-08-21 14:16:34 -0700 | [diff] [blame] | 74 | uint64_t flushTo(SocketClient* writer, uint64_t start, |
Mark Salyzyn | bec7b2d | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 75 | pid_t* lastTid, // &lastTid[LOG_ID_MAX] or nullptr |
Mark Salyzyn | b10f737 | 2016-01-26 14:32:35 -0800 | [diff] [blame] | 76 | bool privileged, bool security, |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 77 | const std::function<FlushToResult(const LogBufferElement* element)>& filter); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 78 | |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 79 | bool clear(log_id_t id, uid_t uid = AID_ROOT); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 80 | unsigned long getSize(log_id_t id); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 81 | int setSize(log_id_t id, unsigned long size); |
Mark Salyzyn | 3f53836 | 2016-09-28 08:38:21 -0700 | [diff] [blame] | 82 | |
Tom Cherry | 479b503 | 2020-05-07 14:44:43 -0700 | [diff] [blame] | 83 | private: |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 84 | void wrlock() { |
| 85 | pthread_rwlock_wrlock(&mLogElementsLock); |
| 86 | } |
| 87 | void rdlock() { |
| 88 | pthread_rwlock_rdlock(&mLogElementsLock); |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 89 | } |
| 90 | void unlock() { |
Mark Salyzyn | d2712b1 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 91 | pthread_rwlock_unlock(&mLogElementsLock); |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 92 | } |
Mark Salyzyn | 5766f80 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 93 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 94 | void maybePrune(log_id_t id); |
Tom Cherry | 1647cd4 | 2020-05-04 12:53:36 -0700 | [diff] [blame] | 95 | void kickMe(LogReaderThread* me, log_id_t id, unsigned long pruneRows); |
Mark Salyzyn | 434f374 | 2017-05-11 13:28:33 -0700 | [diff] [blame] | 96 | |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 97 | bool prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT); |
Mark Salyzyn | 1fdf71f | 2015-09-03 16:08:50 -0700 | [diff] [blame] | 98 | LogBufferElementCollection::iterator erase( |
Mark Salyzyn | 5c5189a | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 99 | LogBufferElementCollection::iterator it, bool coalesce = false); |
Tom Cherry | 402364e | 2020-04-29 17:58:18 -0700 | [diff] [blame] | 100 | |
| 101 | // Returns an iterator to the oldest element for a given log type, or mLogElements.end() if |
| 102 | // there are no logs for the given log type. Requires mLogElementsLock to be held. |
| 103 | LogBufferElementCollection::iterator GetOldest(log_id_t log_id); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 104 | |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 105 | LogReaderList* reader_list_; |
Tom Cherry | 04ad458 | 2020-05-01 16:13:18 -0700 | [diff] [blame] | 106 | LogTags* tags_; |
Tom Cherry | 4bd63ef | 2020-05-01 17:03:20 -0700 | [diff] [blame] | 107 | PruneList* prune_; |
Tom Cherry | 479b503 | 2020-05-07 14:44:43 -0700 | [diff] [blame] | 108 | LogStatistics* stats_; |
Tom Cherry | 02017d7 | 2020-05-04 10:17:42 -0700 | [diff] [blame] | 109 | |
| 110 | // Keeps track of the iterator to the oldest log message of a given log type, as an |
| 111 | // optimization when pruning logs. Use GetOldest() to retrieve. |
| 112 | std::optional<LogBufferElementCollection::iterator> oldest_[LOG_ID_MAX]; |
Tom Cherry | 04ad458 | 2020-05-01 16:13:18 -0700 | [diff] [blame] | 113 | }; |