blob: 421d41997c6ee8358895537eadef0df2af2c24a6 [file] [log] [blame]
Tom Cherry1a796bc2020-05-13 09:28:37 -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 <bitset>
21#include <list>
22#include <mutex>
23#include <queue>
Tom Cherry1fdbdbe2020-06-22 08:28:45 -070024#include <thread>
Tom Cherry1a796bc2020-05-13 09:28:37 -070025#include <vector>
26
27#include <android-base/thread_annotations.h>
28
29#include "LogBuffer.h"
30#include "LogReaderList.h"
31#include "LogStatistics.h"
32#include "LogTags.h"
33#include "SerializedLogChunk.h"
34#include "SerializedLogEntry.h"
35#include "rwlock.h"
36
Tom Cherry9b4246d2020-06-17 11:40:55 -070037class SerializedLogBuffer final : public LogBuffer {
Tom Cherry1a796bc2020-05-13 09:28:37 -070038 public:
39 SerializedLogBuffer(LogReaderList* reader_list, LogTags* tags, LogStatistics* stats);
40 ~SerializedLogBuffer();
41 void Init() override;
42
43 int Log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid, const char* msg,
44 uint16_t len) override;
45 std::unique_ptr<FlushToState> CreateFlushToState(uint64_t start, LogMask log_mask) override;
46 bool FlushTo(LogWriter* writer, FlushToState& state,
47 const std::function<FilterResult(log_id_t log_id, pid_t pid, uint64_t sequence,
48 log_time realtime)>& filter) override;
49
50 bool Clear(log_id_t id, uid_t uid) override;
51 unsigned long GetSize(log_id_t id) override;
52 int SetSize(log_id_t id, unsigned long size) override;
53
54 uint64_t sequence() const override { return sequence_.load(std::memory_order_relaxed); }
55
56 private:
57 bool ShouldLog(log_id_t log_id, const char* msg, uint16_t len);
58 void MaybePrune(log_id_t log_id) REQUIRES(lock_);
59 bool Prune(log_id_t log_id, size_t bytes_to_free, uid_t uid) REQUIRES(lock_);
60 void KickReader(LogReaderThread* reader, log_id_t id, size_t bytes_to_free)
61 REQUIRES_SHARED(lock_);
62 void NotifyReadersOfPrune(log_id_t log_id, const std::list<SerializedLogChunk>::iterator& chunk)
63 REQUIRES(reader_list_->reader_threads_lock());
Tom Cherry1fdbdbe2020-06-22 08:28:45 -070064
65 void StartDeleterThread() REQUIRES(lock_);
66 void DeleterThread();
Tom Cherry1a796bc2020-05-13 09:28:37 -070067
68 LogReaderList* reader_list_;
69 LogTags* tags_;
70 LogStatistics* stats_;
71
72 unsigned long max_size_[LOG_ID_MAX] GUARDED_BY(lock_) = {};
73 std::list<SerializedLogChunk> logs_[LOG_ID_MAX] GUARDED_BY(lock_);
74 RwLock lock_;
75
Tom Cherry1fdbdbe2020-06-22 08:28:45 -070076 std::list<SerializedLogChunk> chunks_to_delete_[LOG_ID_MAX] GUARDED_BY(lock_);
77 std::thread deleter_thread_ GUARDED_BY(lock_);
78 bool deleter_thread_running_ GUARDED_BY(lock_) = false;
79
Tom Cherry1a796bc2020-05-13 09:28:37 -070080 std::atomic<uint64_t> sequence_ = 1;
81};