blob: 74b3de55c718518bd14a8de4a6c61c230a45e031 [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 <bitset>
20#include <list>
21#include <queue>
22
23#include "LogBuffer.h"
24#include "SerializedLogChunk.h"
25#include "SerializedLogEntry.h"
26
27struct LogPosition {
28 std::list<SerializedLogChunk>::iterator buffer_it;
29 int read_offset;
30};
31
32struct MinHeapElement {
33 MinHeapElement(log_id_t log_id, const SerializedLogEntry* entry)
34 : log_id(log_id), entry(entry) {}
35 log_id_t log_id;
36 const SerializedLogEntry* entry;
37 // The change of comparison operators is intentional, std::priority_queue uses operator<() to
38 // compare but creates a max heap. Since we want a min heap, we return the opposite result.
39 bool operator<(const MinHeapElement& rhs) const {
40 return entry->sequence() > rhs.entry->sequence();
41 }
42};
43
44// This class tracks the specific point where a FlushTo client has read through the logs. It
45// directly references the std::list<> iterators from the parent SerializedLogBuffer and the offset
46// into each log chunk where it has last read. All interactions with this class, except for its
47// construction, must be done with SerializedLogBuffer::lock_ held. No log chunks that it
48// references may be pruned, which is handled by ensuring prune does not touch any log chunk with
49// highest sequence number greater or equal to start().
50class SerializedFlushToState : public FlushToState {
51 public:
52 // Initializes this state object. For each log buffer set in log_mask, this sets
53 // logs_needed_from_next_position_.
54 SerializedFlushToState(uint64_t start, LogMask log_mask);
55
56 // Decrease the reference of all referenced logs. This happens when a reader is disconnected.
57 ~SerializedFlushToState() override;
58
59 // We can't hold SerializedLogBuffer::lock_ in the constructor, so we must initialize logs here.
60 void InitializeLogs(std::list<SerializedLogChunk>* logs) {
61 if (logs_ == nullptr) logs_ = logs;
62 }
63
64 // Checks to see if any log buffers set in logs_needed_from_next_position_ have new logs and
65 // calls AddMinHeapEntry() if so.
66 void CheckForNewLogs();
67
68 bool HasUnreadLogs() { return !min_heap_.empty(); }
69
70 // Pops the next unread log from the min heap. Add the next log for that log_id to the min heap
71 // if one is available otherwise set logs_needed_from_next_position_ to indicate that we're
72 // waiting for more logs.
73 MinHeapElement PopNextUnreadLog();
74
75 // If the parent log buffer prunes logs, the reference that this class contains may become
76 // invalid, so this must be called first to drop the reference to buffer_it, if any.
77 void Prune(log_id_t log_id, const std::list<SerializedLogChunk>::iterator& buffer_it);
78
79 private:
80 // If there is a log in the serialized log buffer for `log_id` at the read_offset, add it to the
81 // min heap for reading, otherwise set logs_needed_from_next_position_ to indicate that we're
82 // waiting for the next log.
83 void AddMinHeapEntry(log_id_t log_id);
84
85 // Create a LogPosition object for the given log_id by searching through the log chunks for the
86 // first chunk and then first log entry within that chunk that is greater or equal to start().
87 void CreateLogPosition(log_id_t log_id);
88
89 std::list<SerializedLogChunk>* logs_ = nullptr;
90 // An optional structure that contains an iterator to the serialized log buffer and offset into
91 // it that this logger should handle next.
92 std::optional<LogPosition> log_positions_[LOG_ID_MAX];
93 // A bit for each log that is set if a given log_id has no logs or if this client has read all
94 // of its logs. In order words: `logs_[i].empty() || (buffer_it == std::prev(logs_.end) &&
95 // next_log_position == logs_write_position_)`. These will be re-checked in each
96 // loop in case new logs came in.
97 std::bitset<LOG_ID_MAX> logs_needed_from_next_position_ = {};
98 // A min heap that has up to one entry per log buffer, sorted by sequence number, of the next
99 // element that this reader should read.
100 std::priority_queue<MinHeapElement> min_heap_;
101};