blob: 1ffe7a8f7386e9c2862532c90249bdf98944d57f [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() {
Tom Cherry2adbece2020-09-09 20:13:18 +000028 CHECK_EQ(compressed_log_.size(), 0U);
29 CompressionEngine::GetInstance().Compress(contents_, write_offset_, compressed_log_);
Tom Cherry4ab009f2020-09-28 13:04:15 -070030 LOG(VERBOSE) << "Compressed Log, buffer max size: " << contents_.size()
31 << " size used: " << write_offset_
32 << " compressed size: " << compressed_log_.size();
Tom Cherry1a796bc2020-05-13 09:28:37 -070033}
34
35// TODO: Develop a better reference counting strategy to guard against the case where the writer is
36// much faster than the reader, and we needlessly compess / decompress the logs.
37void SerializedLogChunk::IncReaderRefCount() {
38 if (++reader_ref_count_ != 1 || writer_active_) {
39 return;
40 }
Tom Cherryb6cb9922020-06-24 13:51:04 -070041 contents_.Resize(write_offset_);
42 CompressionEngine::GetInstance().Decompress(compressed_log_, contents_);
Tom Cherry1a796bc2020-05-13 09:28:37 -070043}
44
Tom Cherry59caa7a2020-07-16 20:46:14 -070045void SerializedLogChunk::DecReaderRefCount() {
Tom Cherry1a796bc2020-05-13 09:28:37 -070046 CHECK_NE(reader_ref_count_, 0U);
47 if (--reader_ref_count_ != 0) {
48 return;
49 }
Tom Cherry59caa7a2020-07-16 20:46:14 -070050 if (!writer_active_) {
51 contents_.Resize(0);
Tom Cherry1a796bc2020-05-13 09:28:37 -070052 }
53}
54
55bool SerializedLogChunk::ClearUidLogs(uid_t uid, log_id_t log_id, LogStatistics* stats) {
56 CHECK_EQ(reader_ref_count_, 0U);
57 if (write_offset_ == 0) {
58 return true;
59 }
60
61 IncReaderRefCount();
62
63 int read_offset = 0;
64 int new_write_offset = 0;
65 while (read_offset < write_offset_) {
66 const auto* entry = log_entry(read_offset);
67 if (entry->uid() == uid) {
68 read_offset += entry->total_len();
69 if (stats != nullptr) {
70 stats->Subtract(entry->ToLogStatisticsElement(log_id));
71 }
72 continue;
73 }
74 size_t entry_total_len = entry->total_len();
75 if (read_offset != new_write_offset) {
76 memmove(contents_.data() + new_write_offset, contents_.data() + read_offset,
77 entry_total_len);
78 }
79 read_offset += entry_total_len;
80 new_write_offset += entry_total_len;
81 }
82
83 if (new_write_offset == 0) {
Tom Cherry59caa7a2020-07-16 20:46:14 -070084 DecReaderRefCount();
Tom Cherry1a796bc2020-05-13 09:28:37 -070085 return true;
86 }
87
Tom Cherry59caa7a2020-07-16 20:46:14 -070088 // Clear the old compressed logs and set write_offset_ appropriately to compress the new
89 // partially cleared log.
Tom Cherry1a796bc2020-05-13 09:28:37 -070090 if (new_write_offset != write_offset_) {
Tom Cherry1a796bc2020-05-13 09:28:37 -070091 write_offset_ = new_write_offset;
Tom Cherry2adbece2020-09-09 20:13:18 +000092 if (!writer_active_) {
93 compressed_log_.Resize(0);
94 Compress();
95 }
Tom Cherry1a796bc2020-05-13 09:28:37 -070096 }
97
Tom Cherry59caa7a2020-07-16 20:46:14 -070098 DecReaderRefCount();
Tom Cherry1a796bc2020-05-13 09:28:37 -070099
100 return false;
101}
102
103bool SerializedLogChunk::CanLog(size_t len) {
104 return write_offset_ + len <= contents_.size();
105}
106
107SerializedLogEntry* SerializedLogChunk::Log(uint64_t sequence, log_time realtime, uid_t uid,
108 pid_t pid, pid_t tid, const char* msg, uint16_t len) {
109 auto new_log_address = contents_.data() + write_offset_;
110 auto* entry = new (new_log_address) SerializedLogEntry(uid, pid, tid, sequence, realtime, len);
111 memcpy(entry->msg(), msg, len);
112 write_offset_ += entry->total_len();
113 highest_sequence_number_ = sequence;
114 return entry;
Tom Cherry4ab009f2020-09-28 13:04:15 -0700115}