blob: e6b4f50d6e84077e985027931a63dca953db84f6 [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#include "SerializedLogChunk.h"
18
19#include <android-base/logging.h>
20
21#include "CompressionEngine.h"
Tom Cherry411e2f82020-11-09 13:35:06 -080022#include "SerializedFlushToState.h"
Tom Cherryfb150dd2020-05-13 09:28:37 -070023
24SerializedLogChunk::~SerializedLogChunk() {
25 CHECK_EQ(reader_ref_count_, 0U);
26}
27
Tom Cherryda4752e2021-06-22 23:20:08 -070028void SerializedLogChunk::FinishWriting() {
29 writer_active_ = false;
Tom Cherry4e66bec2020-09-09 20:13:18 +000030 CHECK_EQ(compressed_log_.size(), 0U);
31 CompressionEngine::GetInstance().Compress(contents_, write_offset_, compressed_log_);
Tom Cherryb5687442020-09-28 13:04:15 -070032 LOG(VERBOSE) << "Compressed Log, buffer max size: " << contents_.size()
33 << " size used: " << write_offset_
34 << " compressed size: " << compressed_log_.size();
Tom Cherryda4752e2021-06-22 23:20:08 -070035 if (reader_ref_count_ == 0) {
36 contents_.Resize(0);
37 }
Tom Cherryfb150dd2020-05-13 09:28:37 -070038}
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.
42void SerializedLogChunk::IncReaderRefCount() {
43 if (++reader_ref_count_ != 1 || writer_active_) {
44 return;
45 }
Tom Cherry88ada532020-06-24 13:51:04 -070046 contents_.Resize(write_offset_);
47 CompressionEngine::GetInstance().Decompress(compressed_log_, contents_);
Tom Cherryfb150dd2020-05-13 09:28:37 -070048}
49
Tom Cherryb8c967e2020-07-16 20:46:14 -070050void SerializedLogChunk::DecReaderRefCount() {
Tom Cherryfb150dd2020-05-13 09:28:37 -070051 CHECK_NE(reader_ref_count_, 0U);
52 if (--reader_ref_count_ != 0) {
53 return;
54 }
Tom Cherryb8c967e2020-07-16 20:46:14 -070055 if (!writer_active_) {
56 contents_.Resize(0);
Tom Cherryfb150dd2020-05-13 09:28:37 -070057 }
58}
59
Tom Cherry411e2f82020-11-09 13:35:06 -080060void SerializedLogChunk::AttachReader(SerializedFlushToState* reader) {
61 readers_.emplace_back(reader);
62 IncReaderRefCount();
63}
64
65void 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
72void 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 Cherryfb150dd2020-05-13 09:28:37 -070080bool SerializedLogChunk::CanLog(size_t len) {
81 return write_offset_ + len <= contents_.size();
82}
83
84SerializedLogEntry* 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 Cherryb5687442020-09-28 13:04:15 -070092}