blob: 369aff3e7be2908477e78ff4ecfead26e45d105b [file] [log] [blame]
Tom Cherry0c6eb462020-05-12 12:46:43 -07001/*
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 Cherryc5c9eba2020-10-06 10:22:35 -070028#include "LogdLock.h"
Tom Cherry0c6eb462020-05-12 12:46:43 -070029
30class SimpleLogBuffer : public LogBuffer {
31 public:
32 SimpleLogBuffer(LogReaderList* reader_list, LogTags* tags, LogStatistics* stats);
33 ~SimpleLogBuffer();
Tom Cherry17b77582020-06-17 11:40:55 -070034 void Init() override final;
Tom Cherry0c6eb462020-05-12 12:46:43 -070035
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 Cherryc5c9eba2020-10-06 10:22:35 -070038 std::unique_ptr<FlushToState> CreateFlushToState(uint64_t start, LogMask log_mask)
39 REQUIRES(logd_lock) override;
Tom Cherryd444ab42020-05-28 12:38:21 -070040 bool FlushTo(LogWriter* writer, FlushToState& state,
41 const std::function<FilterResult(log_id_t log_id, pid_t pid, uint64_t sequence,
Tom Cherryc5c9eba2020-10-06 10:22:35 -070042 log_time realtime)>& filter)
43 REQUIRES(logd_lock) override;
Tom Cherry0c6eb462020-05-12 12:46:43 -070044
45 bool Clear(log_id_t id, uid_t uid) override;
Tom Cherry80228952020-08-05 12:14:45 -070046 size_t GetSize(log_id_t id) override;
47 bool SetSize(log_id_t id, size_t size) override final;
Tom Cherry0c6eb462020-05-12 12:46:43 -070048
49 uint64_t sequence() const override { return sequence_.load(std::memory_order_relaxed); }
50
51 protected:
Tom Cherryc5c9eba2020-10-06 10:22:35 -070052 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 Cherry0c6eb462020-05-12 12:46:43 -070054
55 // Returns an iterator to the oldest element for a given log type, or logs_.end() if
Tom Cherryc5c9eba2020-10-06 10:22:35 -070056 // 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 Cherry0c6eb462020-05-12 12:46:43 -070058 std::list<LogBufferElement>::iterator Erase(std::list<LogBufferElement>::iterator it)
Tom Cherryc5c9eba2020-10-06 10:22:35 -070059 REQUIRES(logd_lock);
Tom Cherry0c6eb462020-05-12 12:46:43 -070060 void KickReader(LogReaderThread* reader, log_id_t id, unsigned long prune_rows)
Tom Cherryc5c9eba2020-10-06 10:22:35 -070061 REQUIRES(logd_lock);
Tom Cherry0c6eb462020-05-12 12:46:43 -070062
63 LogStatistics* stats() { return stats_; }
64 LogReaderList* reader_list() { return reader_list_; }
Tom Cherryc5c9eba2020-10-06 10:22:35 -070065 size_t max_size(log_id_t id) REQUIRES_SHARED(logd_lock) { return max_size_[id]; }
Tom Cherry0c6eb462020-05-12 12:46:43 -070066
Tom Cherry0c6eb462020-05-12 12:46:43 -070067 private:
68 bool ShouldLog(log_id_t log_id, const char* msg, uint16_t len);
Tom Cherryc5c9eba2020-10-06 10:22:35 -070069 void MaybePrune(log_id_t id) REQUIRES(logd_lock);
Tom Cherry0c6eb462020-05-12 12:46:43 -070070
71 LogReaderList* reader_list_;
72 LogTags* tags_;
73 LogStatistics* stats_;
74
75 std::atomic<uint64_t> sequence_ = 1;
76
Tom Cherryc5c9eba2020-10-06 10:22:35 -070077 size_t max_size_[LOG_ID_MAX] GUARDED_BY(logd_lock);
78 std::list<LogBufferElement> logs_ GUARDED_BY(logd_lock);
Tom Cherry0c6eb462020-05-12 12:46:43 -070079 // 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 Cherryc5c9eba2020-10-06 10:22:35 -070081 std::optional<std::list<LogBufferElement>::iterator> oldest_[LOG_ID_MAX] GUARDED_BY(logd_lock);
Tom Cherry0c6eb462020-05-12 12:46:43 -070082};