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 | #include "SerializedLogChunk.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | |
| 21 | #include "CompressionEngine.h" |
Tom Cherry | 411e2f8 | 2020-11-09 13:35:06 -0800 | [diff] [blame] | 22 | #include "SerializedFlushToState.h" |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 23 | |
| 24 | SerializedLogChunk::~SerializedLogChunk() { |
| 25 | CHECK_EQ(reader_ref_count_, 0U); |
| 26 | } |
| 27 | |
Tom Cherry | da4752e | 2021-06-22 23:20:08 -0700 | [diff] [blame] | 28 | void SerializedLogChunk::FinishWriting() { |
| 29 | writer_active_ = false; |
Tom Cherry | 4e66bec | 2020-09-09 20:13:18 +0000 | [diff] [blame] | 30 | CHECK_EQ(compressed_log_.size(), 0U); |
| 31 | CompressionEngine::GetInstance().Compress(contents_, write_offset_, compressed_log_); |
Tom Cherry | b568744 | 2020-09-28 13:04:15 -0700 | [diff] [blame] | 32 | LOG(VERBOSE) << "Compressed Log, buffer max size: " << contents_.size() |
| 33 | << " size used: " << write_offset_ |
| 34 | << " compressed size: " << compressed_log_.size(); |
Tom Cherry | da4752e | 2021-06-22 23:20:08 -0700 | [diff] [blame] | 35 | if (reader_ref_count_ == 0) { |
| 36 | contents_.Resize(0); |
| 37 | } |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | // TODO: Develop a better reference counting strategy to guard against the case where the writer is |
| 41 | // much faster than the reader, and we needlessly compess / decompress the logs. |
| 42 | void SerializedLogChunk::IncReaderRefCount() { |
| 43 | if (++reader_ref_count_ != 1 || writer_active_) { |
| 44 | return; |
| 45 | } |
Tom Cherry | 88ada53 | 2020-06-24 13:51:04 -0700 | [diff] [blame] | 46 | contents_.Resize(write_offset_); |
| 47 | CompressionEngine::GetInstance().Decompress(compressed_log_, contents_); |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Tom Cherry | b8c967e | 2020-07-16 20:46:14 -0700 | [diff] [blame] | 50 | void SerializedLogChunk::DecReaderRefCount() { |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 51 | CHECK_NE(reader_ref_count_, 0U); |
| 52 | if (--reader_ref_count_ != 0) { |
| 53 | return; |
| 54 | } |
Tom Cherry | b8c967e | 2020-07-16 20:46:14 -0700 | [diff] [blame] | 55 | if (!writer_active_) { |
| 56 | contents_.Resize(0); |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
Tom Cherry | 411e2f8 | 2020-11-09 13:35:06 -0800 | [diff] [blame] | 60 | void SerializedLogChunk::AttachReader(SerializedFlushToState* reader) { |
| 61 | readers_.emplace_back(reader); |
| 62 | IncReaderRefCount(); |
| 63 | } |
| 64 | |
| 65 | void SerializedLogChunk::DetachReader(SerializedFlushToState* reader) { |
| 66 | auto it = std::find(readers_.begin(), readers_.end(), reader); |
| 67 | CHECK(readers_.end() != it); |
| 68 | readers_.erase(it); |
| 69 | DecReaderRefCount(); |
| 70 | } |
| 71 | |
| 72 | void SerializedLogChunk::NotifyReadersOfPrune(log_id_t log_id) { |
| 73 | // Readers will call DetachReader() in their Prune() call, so we make a copy of the list first. |
| 74 | auto readers = readers_; |
| 75 | for (auto& reader : readers) { |
| 76 | reader->Prune(log_id); |
| 77 | } |
| 78 | } |
| 79 | |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 80 | bool SerializedLogChunk::CanLog(size_t len) { |
| 81 | return write_offset_ + len <= contents_.size(); |
| 82 | } |
| 83 | |
| 84 | SerializedLogEntry* SerializedLogChunk::Log(uint64_t sequence, log_time realtime, uid_t uid, |
| 85 | pid_t pid, pid_t tid, const char* msg, uint16_t len) { |
| 86 | auto new_log_address = contents_.data() + write_offset_; |
| 87 | auto* entry = new (new_log_address) SerializedLogEntry(uid, pid, tid, sequence, realtime, len); |
| 88 | memcpy(entry->msg(), msg, len); |
| 89 | write_offset_ += entry->total_len(); |
| 90 | highest_sequence_number_ = sequence; |
| 91 | return entry; |
Tom Cherry | b568744 | 2020-09-28 13:04:15 -0700 | [diff] [blame] | 92 | } |