Tom Cherry | 1a796bc | 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" |
| 22 | |
| 23 | SerializedLogChunk::~SerializedLogChunk() { |
| 24 | CHECK_EQ(reader_ref_count_, 0U); |
| 25 | } |
| 26 | |
| 27 | void SerializedLogChunk::Compress() { |
Tom Cherry | 2adbece | 2020-09-09 20:13:18 +0000 | [diff] [blame] | 28 | CHECK_EQ(compressed_log_.size(), 0U); |
| 29 | CompressionEngine::GetInstance().Compress(contents_, write_offset_, compressed_log_); |
| 30 | LOG(INFO) << "Compressed Log, buffer max size: " << contents_.size() |
| 31 | << " size used: " << write_offset_ << " compressed size: " << compressed_log_.size(); |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | // TODO: Develop a better reference counting strategy to guard against the case where the writer is |
| 35 | // much faster than the reader, and we needlessly compess / decompress the logs. |
| 36 | void SerializedLogChunk::IncReaderRefCount() { |
| 37 | if (++reader_ref_count_ != 1 || writer_active_) { |
| 38 | return; |
| 39 | } |
Tom Cherry | b6cb992 | 2020-06-24 13:51:04 -0700 | [diff] [blame] | 40 | contents_.Resize(write_offset_); |
| 41 | CompressionEngine::GetInstance().Decompress(compressed_log_, contents_); |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Tom Cherry | 59caa7a | 2020-07-16 20:46:14 -0700 | [diff] [blame] | 44 | void SerializedLogChunk::DecReaderRefCount() { |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 45 | CHECK_NE(reader_ref_count_, 0U); |
| 46 | if (--reader_ref_count_ != 0) { |
| 47 | return; |
| 48 | } |
Tom Cherry | 59caa7a | 2020-07-16 20:46:14 -0700 | [diff] [blame] | 49 | if (!writer_active_) { |
| 50 | contents_.Resize(0); |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | bool SerializedLogChunk::ClearUidLogs(uid_t uid, log_id_t log_id, LogStatistics* stats) { |
| 55 | CHECK_EQ(reader_ref_count_, 0U); |
| 56 | if (write_offset_ == 0) { |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | IncReaderRefCount(); |
| 61 | |
| 62 | int read_offset = 0; |
| 63 | int new_write_offset = 0; |
| 64 | while (read_offset < write_offset_) { |
| 65 | const auto* entry = log_entry(read_offset); |
| 66 | if (entry->uid() == uid) { |
| 67 | read_offset += entry->total_len(); |
| 68 | if (stats != nullptr) { |
| 69 | stats->Subtract(entry->ToLogStatisticsElement(log_id)); |
| 70 | } |
| 71 | continue; |
| 72 | } |
| 73 | size_t entry_total_len = entry->total_len(); |
| 74 | if (read_offset != new_write_offset) { |
| 75 | memmove(contents_.data() + new_write_offset, contents_.data() + read_offset, |
| 76 | entry_total_len); |
| 77 | } |
| 78 | read_offset += entry_total_len; |
| 79 | new_write_offset += entry_total_len; |
| 80 | } |
| 81 | |
| 82 | if (new_write_offset == 0) { |
Tom Cherry | 59caa7a | 2020-07-16 20:46:14 -0700 | [diff] [blame] | 83 | DecReaderRefCount(); |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 84 | return true; |
| 85 | } |
| 86 | |
Tom Cherry | 59caa7a | 2020-07-16 20:46:14 -0700 | [diff] [blame] | 87 | // Clear the old compressed logs and set write_offset_ appropriately to compress the new |
| 88 | // partially cleared log. |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 89 | if (new_write_offset != write_offset_) { |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 90 | write_offset_ = new_write_offset; |
Tom Cherry | 2adbece | 2020-09-09 20:13:18 +0000 | [diff] [blame] | 91 | if (!writer_active_) { |
| 92 | compressed_log_.Resize(0); |
| 93 | Compress(); |
| 94 | } |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Tom Cherry | 59caa7a | 2020-07-16 20:46:14 -0700 | [diff] [blame] | 97 | DecReaderRefCount(); |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 98 | |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | bool SerializedLogChunk::CanLog(size_t len) { |
| 103 | return write_offset_ + len <= contents_.size(); |
| 104 | } |
| 105 | |
| 106 | SerializedLogEntry* SerializedLogChunk::Log(uint64_t sequence, log_time realtime, uid_t uid, |
| 107 | pid_t pid, pid_t tid, const char* msg, uint16_t len) { |
| 108 | auto new_log_address = contents_.data() + write_offset_; |
| 109 | auto* entry = new (new_log_address) SerializedLogEntry(uid, pid, tid, sequence, realtime, len); |
| 110 | memcpy(entry->msg(), msg, len); |
| 111 | write_offset_ += entry->total_len(); |
| 112 | highest_sequence_number_ = sequence; |
| 113 | return entry; |
| 114 | } |