Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 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 | #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 | |
| 27 | struct LogPosition { |
| 28 | std::list<SerializedLogChunk>::iterator buffer_it; |
| 29 | int read_offset; |
Tom Cherry | 6533fff | 2020-09-18 15:32:32 -0700 | [diff] [blame] | 30 | |
| 31 | const SerializedLogEntry* log_entry() const { return buffer_it->log_entry(read_offset); } |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
Tom Cherry | 6533fff | 2020-09-18 15:32:32 -0700 | [diff] [blame] | 34 | struct LogWithId { |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 35 | log_id_t log_id; |
| 36 | const SerializedLogEntry* entry; |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | // This class tracks the specific point where a FlushTo client has read through the logs. It |
| 40 | // directly references the std::list<> iterators from the parent SerializedLogBuffer and the offset |
| 41 | // into each log chunk where it has last read. All interactions with this class, except for its |
Tom Cherry | 6533fff | 2020-09-18 15:32:32 -0700 | [diff] [blame] | 42 | // construction, must be done with SerializedLogBuffer::lock_ held. |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 43 | class SerializedFlushToState : public FlushToState { |
| 44 | public: |
| 45 | // Initializes this state object. For each log buffer set in log_mask, this sets |
| 46 | // logs_needed_from_next_position_. |
| 47 | SerializedFlushToState(uint64_t start, LogMask log_mask); |
| 48 | |
| 49 | // Decrease the reference of all referenced logs. This happens when a reader is disconnected. |
| 50 | ~SerializedFlushToState() override; |
| 51 | |
| 52 | // We can't hold SerializedLogBuffer::lock_ in the constructor, so we must initialize logs here. |
| 53 | void InitializeLogs(std::list<SerializedLogChunk>* logs) { |
| 54 | if (logs_ == nullptr) logs_ = logs; |
| 55 | } |
| 56 | |
Tom Cherry | 6533fff | 2020-09-18 15:32:32 -0700 | [diff] [blame] | 57 | // Updates the state of log_positions_ and logs_needed_from_next_position_ then returns true if |
| 58 | // there are any unread logs, false otherwise. |
| 59 | bool HasUnreadLogs(); |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 60 | |
Tom Cherry | 6533fff | 2020-09-18 15:32:32 -0700 | [diff] [blame] | 61 | // Returns the next unread log and sets logs_needed_from_next_position_ to indicate that we're |
| 62 | // waiting for more logs from the associated log buffer. |
| 63 | LogWithId PopNextUnreadLog(); |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 64 | |
| 65 | // If the parent log buffer prunes logs, the reference that this class contains may become |
| 66 | // invalid, so this must be called first to drop the reference to buffer_it, if any. |
| 67 | void Prune(log_id_t log_id, const std::list<SerializedLogChunk>::iterator& buffer_it); |
| 68 | |
| 69 | private: |
Tom Cherry | 6533fff | 2020-09-18 15:32:32 -0700 | [diff] [blame] | 70 | // Set logs_needed_from_next_position_[i] to indicate if log_positions_[i] points to an unread |
| 71 | // log or to the point at which the next log will appear. |
| 72 | void UpdateLogsNeeded(log_id_t log_id); |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 73 | |
| 74 | // Create a LogPosition object for the given log_id by searching through the log chunks for the |
| 75 | // first chunk and then first log entry within that chunk that is greater or equal to start(). |
| 76 | void CreateLogPosition(log_id_t log_id); |
| 77 | |
Tom Cherry | b07e339 | 2020-06-24 11:47:49 -0700 | [diff] [blame] | 78 | // Checks to see if any log buffers set in logs_needed_from_next_position_ have new logs and |
Tom Cherry | 6533fff | 2020-09-18 15:32:32 -0700 | [diff] [blame] | 79 | // calls UpdateLogsNeeded() if so. |
Tom Cherry | b07e339 | 2020-06-24 11:47:49 -0700 | [diff] [blame] | 80 | void CheckForNewLogs(); |
| 81 | |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 82 | std::list<SerializedLogChunk>* logs_ = nullptr; |
| 83 | // An optional structure that contains an iterator to the serialized log buffer and offset into |
| 84 | // it that this logger should handle next. |
| 85 | std::optional<LogPosition> log_positions_[LOG_ID_MAX]; |
| 86 | // A bit for each log that is set if a given log_id has no logs or if this client has read all |
| 87 | // of its logs. In order words: `logs_[i].empty() || (buffer_it == std::prev(logs_.end) && |
| 88 | // next_log_position == logs_write_position_)`. These will be re-checked in each |
| 89 | // loop in case new logs came in. |
| 90 | std::bitset<LOG_ID_MAX> logs_needed_from_next_position_ = {}; |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 91 | }; |