blob: eb4167414e687b39af339398f1a573529dccd5fe [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 <sys/types.h>
20
Tom Cherryda4752e2021-06-22 23:20:08 -070021#include <list>
Tom Cherry411e2f82020-11-09 13:35:06 -080022#include <vector>
23
Tom Cherry87a17342020-09-18 15:32:32 -070024#include <android-base/logging.h>
25
Tom Cherryfb150dd2020-05-13 09:28:37 -070026#include "LogWriter.h"
Tom Cherry411e2f82020-11-09 13:35:06 -080027#include "LogdLock.h"
Tom Cherry88ada532020-06-24 13:51:04 -070028#include "SerializedData.h"
Tom Cherryfb150dd2020-05-13 09:28:37 -070029#include "SerializedLogEntry.h"
30
Tom Cherry411e2f82020-11-09 13:35:06 -080031class SerializedFlushToState;
32
Tom Cherryfb150dd2020-05-13 09:28:37 -070033class SerializedLogChunk {
34 public:
Tom Cherryda4752e2021-06-22 23:20:08 -070035 friend void VerifyChunks(const std::list<SerializedLogChunk>& expected,
36 const std::list<SerializedLogChunk>& chunks);
37
38 class LogEntryIterator {
39 public:
40 LogEntryIterator(SerializedLogChunk& chunk, int read_offset_)
41 : chunk_(chunk), read_offset_(read_offset_) {}
42
43 LogEntryIterator& operator++() {
44 read_offset_ += chunk_.log_entry(read_offset_)->total_len();
45 return *this;
46 }
47
48 bool operator!=(const LogEntryIterator& other) const {
49 return read_offset_ != other.read_offset_;
50 }
51 const SerializedLogEntry& operator*() const { return *chunk_.log_entry(read_offset_); }
52
53 private:
54 SerializedLogChunk& chunk_;
55 int read_offset_;
56 };
57
Tom Cherry17b77582020-06-17 11:40:55 -070058 explicit SerializedLogChunk(size_t size) : contents_(size) {}
Tom Cherry88ada532020-06-24 13:51:04 -070059 SerializedLogChunk(SerializedLogChunk&& other) noexcept = default;
Tom Cherryfb150dd2020-05-13 09:28:37 -070060 ~SerializedLogChunk();
61
Tom Cherryda4752e2021-06-22 23:20:08 -070062 void FinishWriting();
Tom Cherryfb150dd2020-05-13 09:28:37 -070063 void IncReaderRefCount();
Tom Cherryb8c967e2020-07-16 20:46:14 -070064 void DecReaderRefCount();
Tom Cherry411e2f82020-11-09 13:35:06 -080065 void AttachReader(SerializedFlushToState* reader);
66 void DetachReader(SerializedFlushToState* reader);
67
68 void NotifyReadersOfPrune(log_id_t log_id) REQUIRES(logd_lock);
Tom Cherryfb150dd2020-05-13 09:28:37 -070069
Tom Cherryfb150dd2020-05-13 09:28:37 -070070 bool CanLog(size_t len);
71 SerializedLogEntry* Log(uint64_t sequence, log_time realtime, uid_t uid, pid_t pid, pid_t tid,
72 const char* msg, uint16_t len);
73
74 // If this buffer has been compressed, we only consider its compressed size when accounting for
75 // memory consumption for pruning. This is since the uncompressed log is only by used by
76 // readers, and thus not a representation of how much these logs cost to keep in memory.
Tom Cherrya3d6aa62020-06-19 12:21:21 -070077 size_t PruneSize() const {
78 return sizeof(*this) + (compressed_log_.size() ?: contents_.size());
79 }
Tom Cherryfb150dd2020-05-13 09:28:37 -070080
Tom Cherryfb150dd2020-05-13 09:28:37 -070081 const SerializedLogEntry* log_entry(int offset) const {
Tom Cherry87a17342020-09-18 15:32:32 -070082 CHECK(writer_active_ || reader_ref_count_ > 0);
Tom Cherryfb150dd2020-05-13 09:28:37 -070083 return reinterpret_cast<const SerializedLogEntry*>(data() + offset);
84 }
85 const uint8_t* data() const { return contents_.data(); }
86 int write_offset() const { return write_offset_; }
87 uint64_t highest_sequence_number() const { return highest_sequence_number_; }
88
Tom Cherryda4752e2021-06-22 23:20:08 -070089 LogEntryIterator begin() { return LogEntryIterator(*this, 0); }
90
91 LogEntryIterator end() { return LogEntryIterator(*this, write_offset_); }
92
Tom Cherry682fb3e2020-06-24 11:47:49 -070093 // Exposed for testing
94 uint32_t reader_ref_count() const { return reader_ref_count_; }
95
Tom Cherryfb150dd2020-05-13 09:28:37 -070096 private:
97 // The decompressed contents of this log buffer. Deallocated when the ref_count reaches 0 and
98 // writer_active_ is false.
Tom Cherry88ada532020-06-24 13:51:04 -070099 SerializedData contents_;
Tom Cherryfb150dd2020-05-13 09:28:37 -0700100 int write_offset_ = 0;
101 uint32_t reader_ref_count_ = 0;
102 bool writer_active_ = true;
103 uint64_t highest_sequence_number_ = 1;
Tom Cherry88ada532020-06-24 13:51:04 -0700104 SerializedData compressed_log_;
Tom Cherry411e2f82020-11-09 13:35:06 -0800105 std::vector<SerializedFlushToState*> readers_;
Tom Cherryfb150dd2020-05-13 09:28:37 -0700106};