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