blob: ca78c42c2fbb16ede3251a71c2d5ad0ba95c1f08 [file] [log] [blame]
Tom Cherryfb150dd2020-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 Cherry0a0115f2020-06-22 08:28:45 -070024#include <thread>
Tom Cherryfb150dd2020-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 Cherryc5c9eba2020-10-06 10:22:35 -070033#include "LogdLock.h"
Tom Cherryfb150dd2020-05-13 09:28:37 -070034#include "SerializedLogChunk.h"
35#include "SerializedLogEntry.h"
Tom Cherryfb150dd2020-05-13 09:28:37 -070036
Tom Cherry17b77582020-06-17 11:40:55 -070037class SerializedLogBuffer final : public LogBuffer {
Tom Cherryfb150dd2020-05-13 09:28:37 -070038 public:
Tom Cherryda4752e2021-06-22 23:20:08 -070039 // Create SerializedLogChunk's with size = max_size_[log_id] / kChunkSizeDivisor.
40 static constexpr size_t kChunkSizeDivisor = 4;
41
Tom Cherryfb150dd2020-05-13 09:28:37 -070042 SerializedLogBuffer(LogReaderList* reader_list, LogTags* tags, LogStatistics* stats);
Tom Cherryfb150dd2020-05-13 09:28:37 -070043 void Init() override;
44
45 int Log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid, const char* msg,
46 uint16_t len) override;
Tom Cherryc5c9eba2020-10-06 10:22:35 -070047 std::unique_ptr<FlushToState> CreateFlushToState(uint64_t start, LogMask log_mask)
48 REQUIRES(logd_lock) override;
Tom Cherryfb150dd2020-05-13 09:28:37 -070049 bool FlushTo(LogWriter* writer, FlushToState& state,
50 const std::function<FilterResult(log_id_t log_id, pid_t pid, uint64_t sequence,
Tom Cherryc5c9eba2020-10-06 10:22:35 -070051 log_time realtime)>& filter)
52 REQUIRES(logd_lock) override;
Tom Cherryfb150dd2020-05-13 09:28:37 -070053
54 bool Clear(log_id_t id, uid_t uid) override;
Tom Cherry80228952020-08-05 12:14:45 -070055 size_t GetSize(log_id_t id) override;
56 bool SetSize(log_id_t id, size_t size) override;
Tom Cherryfb150dd2020-05-13 09:28:37 -070057
58 uint64_t sequence() const override { return sequence_.load(std::memory_order_relaxed); }
59
60 private:
61 bool ShouldLog(log_id_t log_id, const char* msg, uint16_t len);
Tom Cherryc5c9eba2020-10-06 10:22:35 -070062 void MaybePrune(log_id_t log_id) REQUIRES(logd_lock);
Tom Cherryda4752e2021-06-22 23:20:08 -070063 void Prune(log_id_t log_id, size_t bytes_to_free) REQUIRES(logd_lock);
64 void UidClear(log_id_t log_id, uid_t uid) REQUIRES(logd_lock);
Tom Cherry4a89fb72020-07-09 12:12:48 -070065 void RemoveChunkFromStats(log_id_t log_id, SerializedLogChunk& chunk);
Tom Cherryc5c9eba2020-10-06 10:22:35 -070066 size_t GetSizeUsed(log_id_t id) REQUIRES(logd_lock);
Tom Cherry0a0115f2020-06-22 08:28:45 -070067
Tom Cherryfb150dd2020-05-13 09:28:37 -070068 LogReaderList* reader_list_;
69 LogTags* tags_;
70 LogStatistics* stats_;
71
Tom Cherryc5c9eba2020-10-06 10:22:35 -070072 size_t max_size_[LOG_ID_MAX] GUARDED_BY(logd_lock) = {};
73 std::list<SerializedLogChunk> logs_[LOG_ID_MAX] GUARDED_BY(logd_lock);
Tom Cherryfb150dd2020-05-13 09:28:37 -070074
Tom Cherryfb150dd2020-05-13 09:28:37 -070075 std::atomic<uint64_t> sequence_ = 1;
Tom Cherryfb150dd2020-05-13 09:28:37 -070076};
Tom Cherryda4752e2021-06-22 23:20:08 -070077
78// Exposed for testing.
79void ClearLogsByUid(std::list<SerializedLogChunk>& log_buffer, uid_t uid, size_t max_size,
80 log_id_t log_id, LogStatistics* stats);