blob: 32fbe5e985845163fa2259262aa7d5e8485f595d [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);
Tom Cherry1a796bc2020-05-13 09:28:37 -070040 void Init() override;
41
42 int Log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid, const char* msg,
43 uint16_t len) override;
44 std::unique_ptr<FlushToState> CreateFlushToState(uint64_t start, LogMask log_mask) override;
45 bool FlushTo(LogWriter* writer, FlushToState& state,
46 const std::function<FilterResult(log_id_t log_id, pid_t pid, uint64_t sequence,
47 log_time realtime)>& filter) override;
48
49 bool Clear(log_id_t id, uid_t uid) override;
Tom Cherry39dc2212020-08-05 12:14:45 -070050 size_t GetSize(log_id_t id) override;
51 bool SetSize(log_id_t id, size_t size) override;
Tom Cherry1a796bc2020-05-13 09:28:37 -070052
53 uint64_t sequence() const override { return sequence_.load(std::memory_order_relaxed); }
54
55 private:
56 bool ShouldLog(log_id_t log_id, const char* msg, uint16_t len);
57 void MaybePrune(log_id_t log_id) REQUIRES(lock_);
58 bool Prune(log_id_t log_id, size_t bytes_to_free, uid_t uid) REQUIRES(lock_);
59 void KickReader(LogReaderThread* reader, log_id_t id, size_t bytes_to_free)
60 REQUIRES_SHARED(lock_);
61 void NotifyReadersOfPrune(log_id_t log_id, const std::list<SerializedLogChunk>::iterator& chunk)
62 REQUIRES(reader_list_->reader_threads_lock());
Tom Cherry4f8125a2020-07-09 12:12:48 -070063 void RemoveChunkFromStats(log_id_t log_id, SerializedLogChunk& chunk);
Tom Cherry39dc2212020-08-05 12:14:45 -070064 size_t GetSizeUsed(log_id_t id) REQUIRES(lock_);
Tom Cherry1fdbdbe2020-06-22 08:28:45 -070065
Tom Cherry1a796bc2020-05-13 09:28:37 -070066 LogReaderList* reader_list_;
67 LogTags* tags_;
68 LogStatistics* stats_;
69
Tom Cherry39dc2212020-08-05 12:14:45 -070070 size_t max_size_[LOG_ID_MAX] GUARDED_BY(lock_) = {};
Tom Cherry1a796bc2020-05-13 09:28:37 -070071 std::list<SerializedLogChunk> logs_[LOG_ID_MAX] GUARDED_BY(lock_);
72 RwLock lock_;
73
Tom Cherry1a796bc2020-05-13 09:28:37 -070074 std::atomic<uint64_t> sequence_ = 1;
75};