1 /*
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>
24 #include <thread>
25 #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 
37 class SerializedLogBuffer final : public LogBuffer {
38   public:
39     SerializedLogBuffer(LogReaderList* reader_list, LogTags* tags, LogStatistics* stats);
40     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;
50     unsigned long GetSize(log_id_t id) override;
51     int SetSize(log_id_t id, unsigned long size) override;
52 
sequence()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());
63     void RemoveChunkFromStats(log_id_t log_id, SerializedLogChunk& chunk);
64     unsigned long GetSizeUsed(log_id_t id) REQUIRES(lock_);
65 
66     LogReaderList* reader_list_;
67     LogTags* tags_;
68     LogStatistics* stats_;
69 
70     unsigned long max_size_[LOG_ID_MAX] GUARDED_BY(lock_) = {};
71     std::list<SerializedLogChunk> logs_[LOG_ID_MAX] GUARDED_BY(lock_);
72     RwLock lock_;
73 
74     std::atomic<uint64_t> sequence_ = 1;
75 };
76