blob: 1a69f7be156441dc90b4b07551569532c6e7dbef [file] [log] [blame]
Tom Cherryfb150dd2020-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"
Tom Cherryc5c9eba2020-10-06 10:22:35 -070024#include "LogdLock.h"
Tom Cherryfb150dd2020-05-13 09:28:37 -070025#include "SerializedLogChunk.h"
26#include "SerializedLogEntry.h"
27
28struct LogPosition {
29 std::list<SerializedLogChunk>::iterator buffer_it;
30 int read_offset;
Tom Cherry87a17342020-09-18 15:32:32 -070031
32 const SerializedLogEntry* log_entry() const { return buffer_it->log_entry(read_offset); }
Tom Cherryfb150dd2020-05-13 09:28:37 -070033};
34
Tom Cherry87a17342020-09-18 15:32:32 -070035struct LogWithId {
Tom Cherryfb150dd2020-05-13 09:28:37 -070036 log_id_t log_id;
37 const SerializedLogEntry* entry;
Tom Cherryfb150dd2020-05-13 09:28:37 -070038};
39
40// This class tracks the specific point where a FlushTo client has read through the logs. It
41// directly references the std::list<> iterators from the parent SerializedLogBuffer and the offset
42// into each log chunk where it has last read. All interactions with this class, except for its
Tom Cherry87a17342020-09-18 15:32:32 -070043// construction, must be done with SerializedLogBuffer::lock_ held.
Tom Cherryfb150dd2020-05-13 09:28:37 -070044class SerializedFlushToState : public FlushToState {
45 public:
46 // Initializes this state object. For each log buffer set in log_mask, this sets
47 // logs_needed_from_next_position_.
Tom Cherryc5c9eba2020-10-06 10:22:35 -070048 SerializedFlushToState(uint64_t start, LogMask log_mask, std::list<SerializedLogChunk>* logs)
49 REQUIRES(logd_lock);
Tom Cherryfb150dd2020-05-13 09:28:37 -070050
51 // Decrease the reference of all referenced logs. This happens when a reader is disconnected.
52 ~SerializedFlushToState() override;
53
Tom Cherry87a17342020-09-18 15:32:32 -070054 // Updates the state of log_positions_ and logs_needed_from_next_position_ then returns true if
55 // there are any unread logs, false otherwise.
Tom Cherryc5c9eba2020-10-06 10:22:35 -070056 bool HasUnreadLogs() REQUIRES(logd_lock);
Tom Cherryfb150dd2020-05-13 09:28:37 -070057
Tom Cherry87a17342020-09-18 15:32:32 -070058 // Returns the next unread log and sets logs_needed_from_next_position_ to indicate that we're
59 // waiting for more logs from the associated log buffer.
Tom Cherryc5c9eba2020-10-06 10:22:35 -070060 LogWithId PopNextUnreadLog() REQUIRES(logd_lock);
Tom Cherryfb150dd2020-05-13 09:28:37 -070061
62 // If the parent log buffer prunes logs, the reference that this class contains may become
63 // invalid, so this must be called first to drop the reference to buffer_it, if any.
Tom Cherryc5c9eba2020-10-06 10:22:35 -070064 void Prune(log_id_t log_id, const std::list<SerializedLogChunk>::iterator& buffer_it)
65 REQUIRES(logd_lock);
Tom Cherryfb150dd2020-05-13 09:28:37 -070066
67 private:
Tom Cherry87a17342020-09-18 15:32:32 -070068 // Set logs_needed_from_next_position_[i] to indicate if log_positions_[i] points to an unread
69 // log or to the point at which the next log will appear.
Tom Cherryc5c9eba2020-10-06 10:22:35 -070070 void UpdateLogsNeeded(log_id_t log_id) REQUIRES(logd_lock);
Tom Cherryfb150dd2020-05-13 09:28:37 -070071
72 // Create a LogPosition object for the given log_id by searching through the log chunks for the
73 // first chunk and then first log entry within that chunk that is greater or equal to start().
Tom Cherryc5c9eba2020-10-06 10:22:35 -070074 void CreateLogPosition(log_id_t log_id) REQUIRES(logd_lock);
Tom Cherryfb150dd2020-05-13 09:28:37 -070075
Tom Cherry682fb3e2020-06-24 11:47:49 -070076 // Checks to see if any log buffers set in logs_needed_from_next_position_ have new logs and
Tom Cherry87a17342020-09-18 15:32:32 -070077 // calls UpdateLogsNeeded() if so.
Tom Cherryc5c9eba2020-10-06 10:22:35 -070078 void CheckForNewLogs() REQUIRES(logd_lock);
Tom Cherry682fb3e2020-06-24 11:47:49 -070079
Tom Cherryc5c9eba2020-10-06 10:22:35 -070080 std::list<SerializedLogChunk>* logs_ GUARDED_BY(logd_lock) = nullptr;
Tom Cherryfb150dd2020-05-13 09:28:37 -070081 // An optional structure that contains an iterator to the serialized log buffer and offset into
82 // it that this logger should handle next.
Tom Cherryc5c9eba2020-10-06 10:22:35 -070083 std::optional<LogPosition> log_positions_[LOG_ID_MAX] GUARDED_BY(logd_lock);
Tom Cherryfb150dd2020-05-13 09:28:37 -070084 // A bit for each log that is set if a given log_id has no logs or if this client has read all
85 // of its logs. In order words: `logs_[i].empty() || (buffer_it == std::prev(logs_.end) &&
86 // next_log_position == logs_write_position_)`. These will be re-checked in each
87 // loop in case new logs came in.
Tom Cherryc5c9eba2020-10-06 10:22:35 -070088 std::bitset<LOG_ID_MAX> logs_needed_from_next_position_ GUARDED_BY(logd_lock) = {};
Tom Cherryfb150dd2020-05-13 09:28:37 -070089};