Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 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 <sys/types.h> |
| 20 | |
Tom Cherry | da4752e | 2021-06-22 23:20:08 -0700 | [diff] [blame] | 21 | #include <list> |
Tom Cherry | 411e2f8 | 2020-11-09 13:35:06 -0800 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
Tom Cherry | 87a1734 | 2020-09-18 15:32:32 -0700 | [diff] [blame] | 24 | #include <android-base/logging.h> |
| 25 | |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 26 | #include "LogWriter.h" |
Tom Cherry | 411e2f8 | 2020-11-09 13:35:06 -0800 | [diff] [blame] | 27 | #include "LogdLock.h" |
Tom Cherry | 88ada53 | 2020-06-24 13:51:04 -0700 | [diff] [blame] | 28 | #include "SerializedData.h" |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 29 | #include "SerializedLogEntry.h" |
| 30 | |
Tom Cherry | 411e2f8 | 2020-11-09 13:35:06 -0800 | [diff] [blame] | 31 | class SerializedFlushToState; |
| 32 | |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 33 | class SerializedLogChunk { |
| 34 | public: |
Tom Cherry | da4752e | 2021-06-22 23:20:08 -0700 | [diff] [blame] | 35 | 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 Cherry | 17b7758 | 2020-06-17 11:40:55 -0700 | [diff] [blame] | 58 | explicit SerializedLogChunk(size_t size) : contents_(size) {} |
Tom Cherry | 88ada53 | 2020-06-24 13:51:04 -0700 | [diff] [blame] | 59 | SerializedLogChunk(SerializedLogChunk&& other) noexcept = default; |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 60 | ~SerializedLogChunk(); |
| 61 | |
Tom Cherry | da4752e | 2021-06-22 23:20:08 -0700 | [diff] [blame] | 62 | void FinishWriting(); |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 63 | void IncReaderRefCount(); |
Tom Cherry | b8c967e | 2020-07-16 20:46:14 -0700 | [diff] [blame] | 64 | void DecReaderRefCount(); |
Tom Cherry | 411e2f8 | 2020-11-09 13:35:06 -0800 | [diff] [blame] | 65 | void AttachReader(SerializedFlushToState* reader); |
| 66 | void DetachReader(SerializedFlushToState* reader); |
| 67 | |
| 68 | void NotifyReadersOfPrune(log_id_t log_id) REQUIRES(logd_lock); |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 69 | |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 70 | 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 Cherry | a3d6aa6 | 2020-06-19 12:21:21 -0700 | [diff] [blame] | 77 | size_t PruneSize() const { |
| 78 | return sizeof(*this) + (compressed_log_.size() ?: contents_.size()); |
| 79 | } |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 80 | |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 81 | const SerializedLogEntry* log_entry(int offset) const { |
Tom Cherry | 87a1734 | 2020-09-18 15:32:32 -0700 | [diff] [blame] | 82 | CHECK(writer_active_ || reader_ref_count_ > 0); |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 83 | 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 Cherry | da4752e | 2021-06-22 23:20:08 -0700 | [diff] [blame] | 89 | LogEntryIterator begin() { return LogEntryIterator(*this, 0); } |
| 90 | |
| 91 | LogEntryIterator end() { return LogEntryIterator(*this, write_offset_); } |
| 92 | |
Tom Cherry | 682fb3e | 2020-06-24 11:47:49 -0700 | [diff] [blame] | 93 | // Exposed for testing |
| 94 | uint32_t reader_ref_count() const { return reader_ref_count_; } |
| 95 | |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 96 | private: |
| 97 | // The decompressed contents of this log buffer. Deallocated when the ref_count reaches 0 and |
| 98 | // writer_active_ is false. |
Tom Cherry | 88ada53 | 2020-06-24 13:51:04 -0700 | [diff] [blame] | 99 | SerializedData contents_; |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 100 | int write_offset_ = 0; |
| 101 | uint32_t reader_ref_count_ = 0; |
| 102 | bool writer_active_ = true; |
| 103 | uint64_t highest_sequence_number_ = 1; |
Tom Cherry | 88ada53 | 2020-06-24 13:51:04 -0700 | [diff] [blame] | 104 | SerializedData compressed_log_; |
Tom Cherry | 411e2f8 | 2020-11-09 13:35:06 -0800 | [diff] [blame] | 105 | std::vector<SerializedFlushToState*> readers_; |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 106 | }; |