Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [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 | #pragma once |
| 18 | |
| 19 | #include <sys/types.h> |
| 20 | |
| 21 | #include <list> |
| 22 | #include <optional> |
| 23 | #include <string> |
| 24 | |
| 25 | #include <android/log.h> |
| 26 | #include <private/android_filesystem_config.h> |
| 27 | #include <sysutils/SocketClient.h> |
| 28 | |
| 29 | #include "LogBuffer.h" |
| 30 | #include "LogBufferElement.h" |
Tom Cherry | 283c9a1 | 2020-05-14 19:25:05 -0700 | [diff] [blame] | 31 | #include "LogReaderList.h" |
| 32 | #include "LogReaderThread.h" |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 33 | #include "LogStatistics.h" |
| 34 | #include "LogTags.h" |
| 35 | #include "LogWhiteBlackList.h" |
Tom Cherry | 283c9a1 | 2020-05-14 19:25:05 -0700 | [diff] [blame] | 36 | #include "LogWriter.h" |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 37 | |
| 38 | typedef std::list<LogBufferElement*> LogBufferElementCollection; |
| 39 | |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 40 | class ChattyLogBuffer : public LogBuffer { |
| 41 | LogBufferElementCollection mLogElements; |
| 42 | pthread_rwlock_t mLogElementsLock; |
| 43 | |
| 44 | // watermark of any worst/chatty uid processing |
| 45 | typedef std::unordered_map<uid_t, LogBufferElementCollection::iterator> LogBufferIteratorMap; |
| 46 | LogBufferIteratorMap mLastWorst[LOG_ID_MAX]; |
| 47 | // watermark of any worst/chatty pid of system processing |
| 48 | typedef std::unordered_map<pid_t, LogBufferElementCollection::iterator> LogBufferPidIteratorMap; |
| 49 | LogBufferPidIteratorMap mLastWorstPidOfSystem[LOG_ID_MAX]; |
| 50 | |
| 51 | unsigned long mMaxSize[LOG_ID_MAX]; |
| 52 | |
| 53 | LogBufferElement* lastLoggedElements[LOG_ID_MAX]; |
| 54 | LogBufferElement* droppedElements[LOG_ID_MAX]; |
| 55 | void log(LogBufferElement* elem); |
| 56 | |
| 57 | public: |
| 58 | ChattyLogBuffer(LogReaderList* reader_list, LogTags* tags, PruneList* prune, |
| 59 | LogStatistics* stats); |
| 60 | ~ChattyLogBuffer(); |
| 61 | void Init() override; |
| 62 | |
| 63 | int Log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid, const char* msg, |
| 64 | uint16_t len) override; |
| 65 | uint64_t FlushTo( |
Tom Cherry | 283c9a1 | 2020-05-14 19:25:05 -0700 | [diff] [blame] | 66 | LogWriter* writer, uint64_t start, pid_t* lastTid, |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 67 | const std::function<FlushToResult(const LogBufferElement* element)>& filter) override; |
| 68 | |
| 69 | bool Clear(log_id_t id, uid_t uid = AID_ROOT) override; |
| 70 | unsigned long GetSize(log_id_t id) override; |
| 71 | int SetSize(log_id_t id, unsigned long size) override; |
| 72 | |
| 73 | private: |
| 74 | void wrlock() { pthread_rwlock_wrlock(&mLogElementsLock); } |
| 75 | void rdlock() { pthread_rwlock_rdlock(&mLogElementsLock); } |
| 76 | void unlock() { pthread_rwlock_unlock(&mLogElementsLock); } |
| 77 | |
| 78 | void maybePrune(log_id_t id); |
| 79 | void kickMe(LogReaderThread* me, log_id_t id, unsigned long pruneRows); |
| 80 | |
| 81 | bool prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT); |
| 82 | LogBufferElementCollection::iterator erase(LogBufferElementCollection::iterator it, |
| 83 | bool coalesce = false); |
| 84 | |
| 85 | // Returns an iterator to the oldest element for a given log type, or mLogElements.end() if |
| 86 | // there are no logs for the given log type. Requires mLogElementsLock to be held. |
| 87 | LogBufferElementCollection::iterator GetOldest(log_id_t log_id); |
| 88 | |
| 89 | LogReaderList* reader_list_; |
| 90 | LogTags* tags_; |
| 91 | PruneList* prune_; |
| 92 | LogStatistics* stats_; |
| 93 | |
| 94 | // Keeps track of the iterator to the oldest log message of a given log type, as an |
| 95 | // optimization when pruning logs. Use GetOldest() to retrieve. |
| 96 | std::optional<LogBufferElementCollection::iterator> oldest_[LOG_ID_MAX]; |
| 97 | }; |