• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #include "pw_multisink/multisink.h"
15 
16 #include <cstring>
17 
18 #include "pw_assert/check.h"
19 #include "pw_bytes/span.h"
20 #include "pw_function/function.h"
21 #include "pw_log/log.h"
22 #include "pw_result/result.h"
23 #include "pw_status/status.h"
24 #include "pw_status/try.h"
25 #include "pw_varint/varint.h"
26 
27 namespace pw {
28 namespace multisink {
29 
HandleEntry(ConstByteSpan entry)30 void MultiSink::HandleEntry(ConstByteSpan entry) {
31   std::lock_guard lock(lock_);
32   const Status push_back_status = ring_buffer_.PushBack(entry, sequence_id_++);
33   PW_DCHECK_OK(push_back_status);
34   NotifyListeners();
35 }
36 
HandleDropped(uint32_t drop_count)37 void MultiSink::HandleDropped(uint32_t drop_count) {
38   std::lock_guard lock(lock_);
39   // Updating the sequence ID helps identify where the ingress drop happend when
40   // a drain peeks or pops.
41   sequence_id_ += drop_count;
42   total_ingress_drops_ += drop_count;
43   NotifyListeners();
44 }
45 
PopEntry(Drain & drain,const Drain::PeekedEntry & entry)46 Status MultiSink::PopEntry(Drain& drain, const Drain::PeekedEntry& entry) {
47   std::lock_guard lock(lock_);
48   PW_DCHECK_PTR_EQ(drain.multisink_, this);
49 
50   // Ignore the call if the entry has been handled already.
51   if (entry.sequence_id() == drain.last_handled_sequence_id_) {
52     return OkStatus();
53   }
54 
55   uint32_t next_entry_sequence_id;
56   Status peek_status = drain.reader_.PeekFrontPreamble(next_entry_sequence_id);
57   if (!peek_status.ok()) {
58     // Ignore errors if the multisink is empty.
59     if (peek_status.IsOutOfRange()) {
60       return OkStatus();
61     }
62     return peek_status;
63   }
64   if (next_entry_sequence_id == entry.sequence_id()) {
65     // A crash should not happen, since the peek was successful and `lock_` is
66     // still held, there shouldn't be any modifications to the multisink in
67     // between peeking and popping.
68     PW_CHECK_OK(drain.reader_.PopFront());
69     drain.last_handled_sequence_id_ = next_entry_sequence_id;
70   }
71   return OkStatus();
72 }
73 
PeekOrPopEntry(Drain & drain,ByteSpan buffer,Request request,uint32_t & drop_count_out,uint32_t & ingress_drop_count_out,uint32_t & entry_sequence_id_out)74 Result<ConstByteSpan> MultiSink::PeekOrPopEntry(
75     Drain& drain,
76     ByteSpan buffer,
77     Request request,
78     uint32_t& drop_count_out,
79     uint32_t& ingress_drop_count_out,
80     uint32_t& entry_sequence_id_out) {
81   size_t bytes_read = 0;
82   entry_sequence_id_out = 0;
83   drop_count_out = 0;
84   ingress_drop_count_out = 0;
85 
86   std::lock_guard lock(lock_);
87   PW_DCHECK_PTR_EQ(drain.multisink_, this);
88 
89   const Status peek_status = drain.reader_.PeekFrontWithPreamble(
90       buffer, entry_sequence_id_out, bytes_read);
91 
92   if (peek_status.IsOutOfRange()) {
93     // If the drain has caught up, report the last handled sequence ID so that
94     // it can still process any dropped entries.
95     entry_sequence_id_out = sequence_id_ - 1;
96   } else if (!peek_status.ok()) {
97     // Discard the entry if the result isn't OK or OUT_OF_RANGE and exit, as the
98     // entry_sequence_id_out cannot be used for computation. Later invocations
99     // will calculate the drop count.
100     PW_CHECK(drain.reader_.PopFront().ok());
101     return peek_status;
102   }
103 
104   // Compute the drop count delta by comparing this entry's sequence ID with the
105   // last sequence ID this drain successfully read.
106   //
107   // The drop count calculation simply computes the difference between the
108   // current and last sequence IDs. Consecutive successful reads will always
109   // differ by one at least, so it is subtracted out. If the read was not
110   // successful, the difference is not adjusted.
111   drop_count_out = entry_sequence_id_out - drain.last_handled_sequence_id_ -
112                    (peek_status.ok() ? 1 : 0);
113 
114   // Only report the ingress drop count when the drain catches up to where the
115   // drop happened, accounting only for the drops found and no more, as
116   // indicated by the gap in sequence IDs.
117   if (drop_count_out > 0) {
118     ingress_drop_count_out =
119         std::min(drop_count_out,
120                  total_ingress_drops_ - drain.last_handled_ingress_drop_count_);
121     // Remove the ingress drop count duplicated in drop_count_out.
122     drop_count_out -= ingress_drop_count_out;
123     // Check if all the ingress drops were reported.
124     drain.last_handled_ingress_drop_count_ =
125         total_ingress_drops_ > ingress_drop_count_out
126             ? total_ingress_drops_ - ingress_drop_count_out
127             : total_ingress_drops_;
128   }
129 
130   // The Peek above may have failed due to OutOfRange, now that we've set the
131   // drop count see if we should return before attempting to pop.
132   if (peek_status.IsOutOfRange()) {
133     // No more entries, update the drain.
134     drain.last_handled_sequence_id_ = entry_sequence_id_out;
135     return peek_status;
136   }
137   if (request == Request::kPop) {
138     PW_CHECK(drain.reader_.PopFront().ok());
139     drain.last_handled_sequence_id_ = entry_sequence_id_out;
140   }
141   return std::as_bytes(buffer.first(bytes_read));
142 }
143 
AttachDrain(Drain & drain)144 void MultiSink::AttachDrain(Drain& drain) {
145   std::lock_guard lock(lock_);
146   PW_DCHECK_PTR_EQ(drain.multisink_, nullptr);
147   drain.multisink_ = this;
148 
149   PW_CHECK_OK(ring_buffer_.AttachReader(drain.reader_));
150   if (&drain == &oldest_entry_drain_) {
151     drain.last_handled_sequence_id_ = sequence_id_ - 1;
152   } else {
153     drain.last_handled_sequence_id_ =
154         oldest_entry_drain_.last_handled_sequence_id_;
155   }
156   drain.last_peek_sequence_id_ = drain.last_handled_sequence_id_;
157   drain.last_handled_ingress_drop_count_ = 0;
158 }
159 
DetachDrain(Drain & drain)160 void MultiSink::DetachDrain(Drain& drain) {
161   std::lock_guard lock(lock_);
162   PW_DCHECK_PTR_EQ(drain.multisink_, this);
163   drain.multisink_ = nullptr;
164   PW_CHECK_OK(ring_buffer_.DetachReader(drain.reader_),
165               "The drain wasn't already attached.");
166 }
167 
AttachListener(Listener & listener)168 void MultiSink::AttachListener(Listener& listener) {
169   std::lock_guard lock(lock_);
170   listeners_.push_back(listener);
171   // Notify the newly added entry, in case there are items in the sink.
172   listener.OnNewEntryAvailable();
173 }
174 
DetachListener(Listener & listener)175 void MultiSink::DetachListener(Listener& listener) {
176   std::lock_guard lock(lock_);
177   [[maybe_unused]] bool was_detached = listeners_.remove(listener);
178   PW_DCHECK(was_detached, "The listener was already attached.");
179 }
180 
Clear()181 void MultiSink::Clear() {
182   std::lock_guard lock(lock_);
183   ring_buffer_.Clear();
184 }
185 
NotifyListeners()186 void MultiSink::NotifyListeners() {
187   for (auto& listener : listeners_) {
188     listener.OnNewEntryAvailable();
189   }
190 }
191 
UnsafeForEachEntry(const Function<void (ConstByteSpan)> & callback,size_t max_num_entries)192 Status MultiSink::UnsafeForEachEntry(
193     const Function<void(ConstByteSpan)>& callback, size_t max_num_entries) {
194   MultiSink::UnsafeIterationWrapper multisink_iteration = UnsafeIteration();
195 
196   // First count the number of entries.
197   size_t num_entries = 0;
198   for ([[maybe_unused]] ConstByteSpan entry : multisink_iteration) {
199     num_entries++;
200   }
201 
202   // Log up to the max number of logs to avoid overflowing the crash log
203   // writer.
204   const size_t first_logged_offset =
205       max_num_entries > num_entries ? 0 : num_entries - max_num_entries;
206   pw::multisink::MultiSink::iterator it = multisink_iteration.begin();
207   for (size_t offset = 0; it != multisink_iteration.end(); ++it, ++offset) {
208     if (offset < first_logged_offset) {
209       continue;  // Skip this log.
210     }
211     callback(*it);
212   }
213   if (!it.status().ok()) {
214     PW_LOG_WARN("Multisink corruption detected, some entries may be missing");
215     return Status::DataLoss();
216   }
217 
218   return OkStatus();
219 }
220 
PopEntry(const PeekedEntry & entry)221 Status MultiSink::Drain::PopEntry(const PeekedEntry& entry) {
222   PW_DCHECK_NOTNULL(multisink_);
223   return multisink_->PopEntry(*this, entry);
224 }
225 
PeekEntry(ByteSpan buffer,uint32_t & drop_count_out,uint32_t & ingress_drop_count_out)226 Result<MultiSink::Drain::PeekedEntry> MultiSink::Drain::PeekEntry(
227     ByteSpan buffer,
228     uint32_t& drop_count_out,
229     uint32_t& ingress_drop_count_out) {
230   PW_DCHECK_NOTNULL(multisink_);
231   uint32_t entry_sequence_id_out;
232   Result<ConstByteSpan> peek_result =
233       multisink_->PeekOrPopEntry(*this,
234                                  buffer,
235                                  Request::kPeek,
236                                  drop_count_out,
237                                  ingress_drop_count_out,
238                                  entry_sequence_id_out);
239   if (!peek_result.ok()) {
240     return peek_result.status();
241   }
242   return PeekedEntry(peek_result.value(), entry_sequence_id_out);
243 }
244 
PopEntry(ByteSpan buffer,uint32_t & drop_count_out,uint32_t & ingress_drop_count_out)245 Result<ConstByteSpan> MultiSink::Drain::PopEntry(
246     ByteSpan buffer,
247     uint32_t& drop_count_out,
248     uint32_t& ingress_drop_count_out) {
249   PW_DCHECK_NOTNULL(multisink_);
250   uint32_t entry_sequence_id_out;
251   return multisink_->PeekOrPopEntry(*this,
252                                     buffer,
253                                     Request::kPop,
254                                     drop_count_out,
255                                     ingress_drop_count_out,
256                                     entry_sequence_id_out);
257 }
258 
259 }  // namespace multisink
260 }  // namespace pw
261