blob: 239a81ef25293136af3bc399b0dc45c54109006f [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"
Tom Cherryc5818862020-10-06 10:22:35 -070033#include "LogdLock.h"
Tom Cherry1a796bc2020-05-13 09:28:37 -070034#include "SerializedLogChunk.h"
35#include "SerializedLogEntry.h"
Tom Cherry1a796bc2020-05-13 09:28:37 -070036
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;
Tom Cherryc5818862020-10-06 10:22:35 -070044 std::unique_ptr<FlushToState> CreateFlushToState(uint64_t start, LogMask log_mask)
45 REQUIRES(logd_lock) override;
Tom Cherry1a796bc2020-05-13 09:28:37 -070046 bool FlushTo(LogWriter* writer, FlushToState& state,
47 const std::function<FilterResult(log_id_t log_id, pid_t pid, uint64_t sequence,
Tom Cherryc5818862020-10-06 10:22:35 -070048 log_time realtime)>& filter)
49 REQUIRES(logd_lock) override;
Tom Cherry1a796bc2020-05-13 09:28:37 -070050
51 bool Clear(log_id_t id, uid_t uid) override;
Tom Cherry39dc2212020-08-05 12:14:45 -070052 size_t GetSize(log_id_t id) override;
53 bool SetSize(log_id_t id, size_t size) override;
Tom Cherry1a796bc2020-05-13 09:28:37 -070054
55 uint64_t sequence() const override { return sequence_.load(std::memory_order_relaxed); }
56
57 private:
58 bool ShouldLog(log_id_t log_id, const char* msg, uint16_t len);
Tom Cherryc5818862020-10-06 10:22:35 -070059 void MaybePrune(log_id_t log_id) REQUIRES(logd_lock);
60 void Prune(log_id_t log_id, size_t bytes_to_free, uid_t uid) REQUIRES(logd_lock);
Tom Cherry1a796bc2020-05-13 09:28:37 -070061 void NotifyReadersOfPrune(log_id_t log_id, const std::list<SerializedLogChunk>::iterator& chunk)
Tom Cherryc5818862020-10-06 10:22:35 -070062 REQUIRES(logd_lock);
Tom Cherry4f8125a2020-07-09 12:12:48 -070063 void RemoveChunkFromStats(log_id_t log_id, SerializedLogChunk& chunk);
Tom Cherryc5818862020-10-06 10:22:35 -070064 size_t GetSizeUsed(log_id_t id) REQUIRES(logd_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 Cherryc5818862020-10-06 10:22:35 -070070 size_t max_size_[LOG_ID_MAX] GUARDED_BY(logd_lock) = {};
71 std::list<SerializedLogChunk> logs_[LOG_ID_MAX] GUARDED_BY(logd_lock);
Tom Cherry1a796bc2020-05-13 09:28:37 -070072
Tom Cherry1a796bc2020-05-13 09:28:37 -070073 std::atomic<uint64_t> sequence_ = 1;
74};