blob: 8e5b50eb591bb4d872dabf9ca69a66cd2b789f66 [file] [log] [blame]
Tom Cherry8f613462020-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"
28#include "rwlock.h"
29
30class SimpleLogBuffer : public LogBuffer {
31 public:
32 SimpleLogBuffer(LogReaderList* reader_list, LogTags* tags, LogStatistics* stats);
33 ~SimpleLogBuffer();
Tom Cherry9b4246d2020-06-17 11:40:55 -070034 void Init() override final;
Tom Cherry8f613462020-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 Cherry855c7c82020-05-28 12:38:21 -070038 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,
Tom Cherryb3e16332020-05-28 20:02:42 -070041 log_time realtime)>& filter) override;
Tom Cherry8f613462020-05-12 12:46:43 -070042
43 bool Clear(log_id_t id, uid_t uid) override;
Tom Cherry39dc2212020-08-05 12:14:45 -070044 size_t GetSize(log_id_t id) override;
45 bool SetSize(log_id_t id, size_t size) override final;
Tom Cherry8f613462020-05-12 12:46:43 -070046
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
61 LogStatistics* stats() { return stats_; }
62 LogReaderList* reader_list() { return reader_list_; }
Tom Cherry39dc2212020-08-05 12:14:45 -070063 size_t max_size(log_id_t id) REQUIRES_SHARED(lock_) { return max_size_[id]; }
Tom Cherry8f613462020-05-12 12:46:43 -070064 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
Tom Cherry39dc2212020-08-05 12:14:45 -070078 size_t max_size_[LOG_ID_MAX] GUARDED_BY(lock_);
Tom Cherry8f613462020-05-12 12:46:43 -070079 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};