blob: 645433d2b0613cff41e2bc351bd7a7ed93f89453 [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#pragma once
18
19#include <sys/types.h>
20
Tom Cherry6533fff2020-09-18 15:32:32 -070021#include <android-base/logging.h>
22
Tom Cherry1a796bc2020-05-13 09:28:37 -070023#include "LogWriter.h"
Tom Cherryb6cb9922020-06-24 13:51:04 -070024#include "SerializedData.h"
Tom Cherry1a796bc2020-05-13 09:28:37 -070025#include "SerializedLogEntry.h"
26
27class SerializedLogChunk {
28 public:
Tom Cherry9b4246d2020-06-17 11:40:55 -070029 explicit SerializedLogChunk(size_t size) : contents_(size) {}
Tom Cherryb6cb9922020-06-24 13:51:04 -070030 SerializedLogChunk(SerializedLogChunk&& other) noexcept = default;
Tom Cherry1a796bc2020-05-13 09:28:37 -070031 ~SerializedLogChunk();
32
33 void Compress();
34 void IncReaderRefCount();
Tom Cherry59caa7a2020-07-16 20:46:14 -070035 void DecReaderRefCount();
Tom Cherry1a796bc2020-05-13 09:28:37 -070036
37 // Must have no readers referencing this. Return true if there are no logs left in this chunk.
38 bool ClearUidLogs(uid_t uid, log_id_t log_id, LogStatistics* stats);
39
40 bool CanLog(size_t len);
41 SerializedLogEntry* Log(uint64_t sequence, log_time realtime, uid_t uid, pid_t pid, pid_t tid,
42 const char* msg, uint16_t len);
43
44 // If this buffer has been compressed, we only consider its compressed size when accounting for
45 // memory consumption for pruning. This is since the uncompressed log is only by used by
46 // readers, and thus not a representation of how much these logs cost to keep in memory.
Tom Cherryf74503d2020-06-19 12:21:21 -070047 size_t PruneSize() const {
48 return sizeof(*this) + (compressed_log_.size() ?: contents_.size());
49 }
Tom Cherry1a796bc2020-05-13 09:28:37 -070050
51 void FinishWriting() {
52 writer_active_ = false;
Tom Cherry59caa7a2020-07-16 20:46:14 -070053 Compress();
Tom Cherry1a796bc2020-05-13 09:28:37 -070054 if (reader_ref_count_ == 0) {
Tom Cherry59caa7a2020-07-16 20:46:14 -070055 contents_.Resize(0);
Tom Cherry1a796bc2020-05-13 09:28:37 -070056 }
57 }
58
59 const SerializedLogEntry* log_entry(int offset) const {
Tom Cherry6533fff2020-09-18 15:32:32 -070060 CHECK(writer_active_ || reader_ref_count_ > 0);
Tom Cherry1a796bc2020-05-13 09:28:37 -070061 return reinterpret_cast<const SerializedLogEntry*>(data() + offset);
62 }
63 const uint8_t* data() const { return contents_.data(); }
64 int write_offset() const { return write_offset_; }
65 uint64_t highest_sequence_number() const { return highest_sequence_number_; }
66
Tom Cherryb07e3392020-06-24 11:47:49 -070067 // Exposed for testing
68 uint32_t reader_ref_count() const { return reader_ref_count_; }
69
Tom Cherry1a796bc2020-05-13 09:28:37 -070070 private:
71 // The decompressed contents of this log buffer. Deallocated when the ref_count reaches 0 and
72 // writer_active_ is false.
Tom Cherryb6cb9922020-06-24 13:51:04 -070073 SerializedData contents_;
Tom Cherry1a796bc2020-05-13 09:28:37 -070074 int write_offset_ = 0;
75 uint32_t reader_ref_count_ = 0;
76 bool writer_active_ = true;
77 uint64_t highest_sequence_number_ = 1;
Tom Cherryb6cb9922020-06-24 13:51:04 -070078 SerializedData compressed_log_;
Tom Cherry1a796bc2020-05-13 09:28:37 -070079};