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 | #include "SerializedFlushToState.h" |
| 18 | |
Tom Cherry | 6533fff | 2020-09-18 15:32:32 -0700 | [diff] [blame^] | 19 | #include <limits> |
| 20 | |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 21 | #include <android-base/logging.h> |
| 22 | |
| 23 | SerializedFlushToState::SerializedFlushToState(uint64_t start, LogMask log_mask) |
| 24 | : FlushToState(start, log_mask) { |
| 25 | log_id_for_each(i) { |
| 26 | if (((1 << i) & log_mask) == 0) { |
| 27 | continue; |
| 28 | } |
| 29 | logs_needed_from_next_position_[i] = true; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | SerializedFlushToState::~SerializedFlushToState() { |
| 34 | log_id_for_each(i) { |
| 35 | if (log_positions_[i]) { |
Tom Cherry | 59caa7a | 2020-07-16 20:46:14 -0700 | [diff] [blame] | 36 | log_positions_[i]->buffer_it->DecReaderRefCount(); |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | void SerializedFlushToState::CreateLogPosition(log_id_t log_id) { |
| 42 | CHECK(!logs_[log_id].empty()); |
| 43 | LogPosition log_position; |
| 44 | auto it = logs_[log_id].begin(); |
| 45 | while (it != logs_[log_id].end() && start() > it->highest_sequence_number()) { |
| 46 | ++it; |
| 47 | } |
| 48 | if (it == logs_[log_id].end()) { |
| 49 | --it; |
| 50 | } |
| 51 | it->IncReaderRefCount(); |
| 52 | log_position.buffer_it = it; |
| 53 | |
| 54 | // Find the offset of the first log with sequence number >= start(). |
| 55 | int read_offset = 0; |
| 56 | while (read_offset < it->write_offset()) { |
| 57 | const auto* entry = it->log_entry(read_offset); |
| 58 | if (entry->sequence() >= start()) { |
| 59 | break; |
| 60 | } |
| 61 | read_offset += entry->total_len(); |
| 62 | } |
| 63 | log_position.read_offset = read_offset; |
| 64 | |
Tom Cherry | 9b4246d | 2020-06-17 11:40:55 -0700 | [diff] [blame] | 65 | log_positions_[log_id].emplace(log_position); |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Tom Cherry | 6533fff | 2020-09-18 15:32:32 -0700 | [diff] [blame^] | 68 | void SerializedFlushToState::UpdateLogsNeeded(log_id_t log_id) { |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 69 | auto& buffer_it = log_positions_[log_id]->buffer_it; |
| 70 | auto read_offset = log_positions_[log_id]->read_offset; |
| 71 | |
Tom Cherry | 6533fff | 2020-09-18 15:32:32 -0700 | [diff] [blame^] | 72 | // If there is another log to read in this buffer, let it be read. |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 73 | if (read_offset < buffer_it->write_offset()) { |
Tom Cherry | 6533fff | 2020-09-18 15:32:32 -0700 | [diff] [blame^] | 74 | logs_needed_from_next_position_[log_id] = false; |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 75 | } else if (read_offset == buffer_it->write_offset()) { |
| 76 | // If there are no more logs to read in this buffer and it's the last buffer, then |
| 77 | // set logs_needed_from_next_position_ to wait until more logs get logged. |
| 78 | if (buffer_it == std::prev(logs_[log_id].end())) { |
| 79 | logs_needed_from_next_position_[log_id] = true; |
| 80 | } else { |
| 81 | // Otherwise, if there is another buffer piece, move to that and do the same check. |
Tom Cherry | 59caa7a | 2020-07-16 20:46:14 -0700 | [diff] [blame] | 82 | buffer_it->DecReaderRefCount(); |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 83 | ++buffer_it; |
| 84 | buffer_it->IncReaderRefCount(); |
| 85 | log_positions_[log_id]->read_offset = 0; |
| 86 | if (buffer_it->write_offset() == 0) { |
| 87 | logs_needed_from_next_position_[log_id] = true; |
| 88 | } else { |
Tom Cherry | 6533fff | 2020-09-18 15:32:32 -0700 | [diff] [blame^] | 89 | logs_needed_from_next_position_[log_id] = false; |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | } else { |
| 93 | // read_offset > buffer_it->write_offset() should never happen. |
| 94 | CHECK(false); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void SerializedFlushToState::CheckForNewLogs() { |
| 99 | log_id_for_each(i) { |
| 100 | if (!logs_needed_from_next_position_[i]) { |
| 101 | continue; |
| 102 | } |
| 103 | if (!log_positions_[i]) { |
| 104 | if (logs_[i].empty()) { |
| 105 | continue; |
| 106 | } |
| 107 | CreateLogPosition(i); |
| 108 | } |
Tom Cherry | 6533fff | 2020-09-18 15:32:32 -0700 | [diff] [blame^] | 109 | UpdateLogsNeeded(i); |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
Tom Cherry | 6533fff | 2020-09-18 15:32:32 -0700 | [diff] [blame^] | 113 | bool SerializedFlushToState::HasUnreadLogs() { |
| 114 | CheckForNewLogs(); |
| 115 | log_id_for_each(i) { |
| 116 | if (log_positions_[i] && !logs_needed_from_next_position_[i]) { |
| 117 | return true; |
| 118 | } |
| 119 | } |
| 120 | return false; |
| 121 | } |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 122 | |
Tom Cherry | 6533fff | 2020-09-18 15:32:32 -0700 | [diff] [blame^] | 123 | LogWithId SerializedFlushToState::PopNextUnreadLog() { |
| 124 | uint64_t min_sequence = std::numeric_limits<uint64_t>::max(); |
| 125 | log_id_t log_id; |
| 126 | const SerializedLogEntry* entry = nullptr; |
| 127 | log_id_for_each(i) { |
| 128 | if (!log_positions_[i] || logs_needed_from_next_position_[i]) { |
| 129 | continue; |
| 130 | } |
| 131 | if (log_positions_[i]->log_entry()->sequence() < min_sequence) { |
| 132 | log_id = i; |
| 133 | entry = log_positions_[i]->log_entry(); |
| 134 | min_sequence = entry->sequence(); |
| 135 | } |
| 136 | } |
| 137 | CHECK_NE(nullptr, entry); |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 138 | |
| 139 | log_positions_[log_id]->read_offset += entry->total_len(); |
| 140 | |
Tom Cherry | b07e339 | 2020-06-24 11:47:49 -0700 | [diff] [blame] | 141 | logs_needed_from_next_position_[log_id] = true; |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 142 | |
Tom Cherry | 6533fff | 2020-09-18 15:32:32 -0700 | [diff] [blame^] | 143 | return {log_id, entry}; |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void SerializedFlushToState::Prune(log_id_t log_id, |
| 147 | const std::list<SerializedLogChunk>::iterator& buffer_it) { |
| 148 | // If we don't have a position for this log or if we're not referencing buffer_it, ignore. |
| 149 | if (!log_positions_[log_id].has_value() || log_positions_[log_id]->buffer_it != buffer_it) { |
| 150 | return; |
| 151 | } |
| 152 | |
Tom Cherry | 6533fff | 2020-09-18 15:32:32 -0700 | [diff] [blame^] | 153 | // Decrease the ref count since we're deleting our reference. |
Tom Cherry | 59caa7a | 2020-07-16 20:46:14 -0700 | [diff] [blame] | 154 | buffer_it->DecReaderRefCount(); |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 155 | |
| 156 | // Delete in the reference. |
| 157 | log_positions_[log_id].reset(); |
| 158 | |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 159 | // Finally set logs_needed_from_next_position_, so CheckForNewLogs() will re-create the |
| 160 | // log_position_ object during the next read. |
| 161 | logs_needed_from_next_position_[log_id] = true; |
| 162 | } |