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 "LogdLock.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) 39 REQUIRES(logd_lock) override; 40 bool FlushTo(LogWriter* writer, FlushToState& state, 41 const std::function<FilterResult(log_id_t log_id, pid_t pid, uint64_t sequence, 42 log_time realtime)>& filter) 43 REQUIRES(logd_lock) override; 44 45 bool Clear(log_id_t id, uid_t uid) override; 46 size_t GetSize(log_id_t id) override; 47 bool SetSize(log_id_t id, size_t size) override final; 48 sequence()49 uint64_t sequence() const override { return sequence_.load(std::memory_order_relaxed); } 50 51 protected: 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); 54 55 // Returns an iterator to the oldest element for a given log type, or logs_.end() if 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); 58 std::list<LogBufferElement>::iterator Erase(std::list<LogBufferElement>::iterator it) 59 REQUIRES(logd_lock); 60 void KickReader(LogReaderThread* reader, log_id_t id, unsigned long prune_rows) 61 REQUIRES(logd_lock); 62 stats()63 LogStatistics* stats() { return stats_; } reader_list()64 LogReaderList* reader_list() { return reader_list_; } max_size(log_id_t id)65 size_t max_size(log_id_t id) REQUIRES_SHARED(logd_lock) { return max_size_[id]; } logs()66 std::list<LogBufferElement>& logs() { return logs_; } 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(logd_lock); 71 72 LogReaderList* reader_list_; 73 LogTags* tags_; 74 LogStatistics* stats_; 75 76 std::atomic<uint64_t> sequence_ = 1; 77 78 size_t max_size_[LOG_ID_MAX] GUARDED_BY(logd_lock); 79 std::list<LogBufferElement> logs_ GUARDED_BY(logd_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(logd_lock); 83 }; 84