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