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