Tom Cherry | 0c6eb46 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 <atomic> |
| 20 | #include <list> |
| 21 | #include <mutex> |
| 22 | |
| 23 | #include "LogBuffer.h" |
| 24 | #include "LogBufferElement.h" |
| 25 | #include "LogReaderList.h" |
| 26 | #include "LogStatistics.h" |
| 27 | #include "LogTags.h" |
Tom Cherry | c5c9eba | 2020-10-06 10:22:35 -0700 | [diff] [blame] | 28 | #include "LogdLock.h" |
Tom Cherry | 0c6eb46 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 29 | |
| 30 | class SimpleLogBuffer : public LogBuffer { |
| 31 | public: |
| 32 | SimpleLogBuffer(LogReaderList* reader_list, LogTags* tags, LogStatistics* stats); |
| 33 | ~SimpleLogBuffer(); |
Tom Cherry | 17b7758 | 2020-06-17 11:40:55 -0700 | [diff] [blame] | 34 | void Init() override final; |
Tom Cherry | 0c6eb46 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 35 | |
| 36 | int Log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid, const char* msg, |
| 37 | uint16_t len) override; |
Tom Cherry | c5c9eba | 2020-10-06 10:22:35 -0700 | [diff] [blame] | 38 | std::unique_ptr<FlushToState> CreateFlushToState(uint64_t start, LogMask log_mask) |
| 39 | REQUIRES(logd_lock) override; |
Tom Cherry | d444ab4 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 40 | bool FlushTo(LogWriter* writer, FlushToState& state, |
| 41 | const std::function<FilterResult(log_id_t log_id, pid_t pid, uint64_t sequence, |
Tom Cherry | c5c9eba | 2020-10-06 10:22:35 -0700 | [diff] [blame] | 42 | log_time realtime)>& filter) |
| 43 | REQUIRES(logd_lock) override; |
Tom Cherry | 0c6eb46 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 44 | |
| 45 | bool Clear(log_id_t id, uid_t uid) override; |
Tom Cherry | 8022895 | 2020-08-05 12:14:45 -0700 | [diff] [blame] | 46 | size_t GetSize(log_id_t id) override; |
| 47 | bool SetSize(log_id_t id, size_t size) override final; |
Tom Cherry | 0c6eb46 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 48 | |
| 49 | uint64_t sequence() const override { return sequence_.load(std::memory_order_relaxed); } |
| 50 | |
| 51 | protected: |
Tom Cherry | c5c9eba | 2020-10-06 10:22:35 -0700 | [diff] [blame] | 52 | virtual bool Prune(log_id_t id, unsigned long prune_rows, uid_t uid) REQUIRES(logd_lock); |
| 53 | virtual void LogInternal(LogBufferElement&& elem) REQUIRES(logd_lock); |
Tom Cherry | 0c6eb46 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 54 | |
| 55 | // Returns an iterator to the oldest element for a given log type, or logs_.end() if |
Tom Cherry | c5c9eba | 2020-10-06 10:22:35 -0700 | [diff] [blame] | 56 | // there are no logs for the given log type. Requires logs_logd_lock to be held. |
| 57 | std::list<LogBufferElement>::iterator GetOldest(log_id_t log_id) REQUIRES(logd_lock); |
Tom Cherry | 0c6eb46 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 58 | std::list<LogBufferElement>::iterator Erase(std::list<LogBufferElement>::iterator it) |
Tom Cherry | c5c9eba | 2020-10-06 10:22:35 -0700 | [diff] [blame] | 59 | REQUIRES(logd_lock); |
Tom Cherry | 0c6eb46 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 60 | void KickReader(LogReaderThread* reader, log_id_t id, unsigned long prune_rows) |
Tom Cherry | c5c9eba | 2020-10-06 10:22:35 -0700 | [diff] [blame] | 61 | REQUIRES(logd_lock); |
Tom Cherry | 0c6eb46 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 62 | |
| 63 | LogStatistics* stats() { return stats_; } |
| 64 | LogReaderList* reader_list() { return reader_list_; } |
Tom Cherry | c5c9eba | 2020-10-06 10:22:35 -0700 | [diff] [blame] | 65 | size_t max_size(log_id_t id) REQUIRES_SHARED(logd_lock) { return max_size_[id]; } |
Tom Cherry | 0c6eb46 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 66 | |
Tom Cherry | 0c6eb46 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 67 | private: |
| 68 | bool ShouldLog(log_id_t log_id, const char* msg, uint16_t len); |
Tom Cherry | c5c9eba | 2020-10-06 10:22:35 -0700 | [diff] [blame] | 69 | void MaybePrune(log_id_t id) REQUIRES(logd_lock); |
Tom Cherry | 0c6eb46 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 70 | |
| 71 | LogReaderList* reader_list_; |
| 72 | LogTags* tags_; |
| 73 | LogStatistics* stats_; |
| 74 | |
| 75 | std::atomic<uint64_t> sequence_ = 1; |
| 76 | |
Tom Cherry | c5c9eba | 2020-10-06 10:22:35 -0700 | [diff] [blame] | 77 | size_t max_size_[LOG_ID_MAX] GUARDED_BY(logd_lock); |
| 78 | std::list<LogBufferElement> logs_ GUARDED_BY(logd_lock); |
Tom Cherry | 0c6eb46 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 79 | // Keeps track of the iterator to the oldest log message of a given log type, as an |
| 80 | // optimization when pruning logs. Use GetOldest() to retrieve. |
Tom Cherry | c5c9eba | 2020-10-06 10:22:35 -0700 | [diff] [blame] | 81 | std::optional<std::list<LogBufferElement>::iterator> oldest_[LOG_ID_MAX] GUARDED_BY(logd_lock); |
Tom Cherry | 0c6eb46 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 82 | }; |