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