blob: 2516003c142b170cfc20f4dbca5da422da3d61a8 [file] [log] [blame]
Tom Cherry1a796bc2020-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"
22
23SerializedLogChunk::~SerializedLogChunk() {
24 CHECK_EQ(reader_ref_count_, 0U);
25}
26
27void SerializedLogChunk::Compress() {
28 if (compressed_log_.empty()) {
29 CompressionEngine::GetInstance().Compress({contents_.data(), write_offset_},
30 compressed_log_);
31 LOG(INFO) << "Compressed Log, buffer max size: " << contents_.size()
32 << " size used: " << write_offset_
33 << " compressed size: " << compressed_log_.size();
34 }
35 contents_.resize(0);
36}
37
38// TODO: Develop a better reference counting strategy to guard against the case where the writer is
39// much faster than the reader, and we needlessly compess / decompress the logs.
40void SerializedLogChunk::IncReaderRefCount() {
41 if (++reader_ref_count_ != 1 || writer_active_) {
42 return;
43 }
44 CompressionEngine::GetInstance().Decompress(compressed_log_, contents_, write_offset_);
45}
46
47void SerializedLogChunk::DecReaderRefCount(bool compress) {
48 CHECK_NE(reader_ref_count_, 0U);
49 if (--reader_ref_count_ != 0) {
50 return;
51 }
52 if (compress && !writer_active_) {
53 Compress();
54 }
55}
56
57bool SerializedLogChunk::ClearUidLogs(uid_t uid, log_id_t log_id, LogStatistics* stats) {
58 CHECK_EQ(reader_ref_count_, 0U);
59 if (write_offset_ == 0) {
60 return true;
61 }
62
63 IncReaderRefCount();
64
65 int read_offset = 0;
66 int new_write_offset = 0;
67 while (read_offset < write_offset_) {
68 const auto* entry = log_entry(read_offset);
69 if (entry->uid() == uid) {
70 read_offset += entry->total_len();
71 if (stats != nullptr) {
72 stats->Subtract(entry->ToLogStatisticsElement(log_id));
73 }
74 continue;
75 }
76 size_t entry_total_len = entry->total_len();
77 if (read_offset != new_write_offset) {
78 memmove(contents_.data() + new_write_offset, contents_.data() + read_offset,
79 entry_total_len);
80 }
81 read_offset += entry_total_len;
82 new_write_offset += entry_total_len;
83 }
84
85 if (new_write_offset == 0) {
86 DecReaderRefCount(false);
87 return true;
88 }
89
90 // Clear the old compressed logs and set write_offset_ appropriately for DecReaderRefCount()
91 // to compress the new partially cleared log.
92 if (new_write_offset != write_offset_) {
93 compressed_log_.clear();
94 write_offset_ = new_write_offset;
95 }
96
97 DecReaderRefCount(true);
98
99 return false;
100}
101
102bool SerializedLogChunk::CanLog(size_t len) {
103 return write_offset_ + len <= contents_.size();
104}
105
106SerializedLogEntry* 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}