| 1 | /* |
| 2 | * Copyright (C) 2017 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 "perfetto/protozero/scattered_stream_writer.h" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | |
| 21 | #include "perfetto/base/logging.h" |
| 22 | |
| 23 | namespace protozero { |
| 24 | |
| 25 | ScatteredStreamWriter::Delegate::~Delegate() {} |
| 26 | |
| 27 | uint8_t* ScatteredStreamWriter::Delegate::AnnotatePatch(uint8_t* patch_addr) { |
| 28 | // In most cases, a patch is transparent. The caller can write directly into |
| 29 | // `to_patch`, because its memory is not going away. TraceWriterImpl, however, |
| 30 | // requires a more complicated logic, because the chunks might be copied |
| 31 | // earlier. |
| 32 | return patch_addr; |
| 33 | } |
| 34 | |
| 35 | ScatteredStreamWriter::ScatteredStreamWriter(Delegate* delegate) |
| 36 | : delegate_(delegate), |
| 37 | cur_range_({.begin: nullptr, .end: nullptr}), |
| 38 | write_ptr_(nullptr) {} |
| 39 | |
| 40 | ScatteredStreamWriter::~ScatteredStreamWriter() {} |
| 41 | |
| 42 | void ScatteredStreamWriter::Reset(ContiguousMemoryRange range) { |
| 43 | written_previously_ += static_cast<uint64_t>(write_ptr_ - cur_range_.begin); |
| 44 | cur_range_ = range; |
| 45 | write_ptr_ = range.begin; |
| 46 | PERFETTO_DCHECK(!write_ptr_ || write_ptr_ < cur_range_.end); |
| 47 | } |
| 48 | |
| 49 | void ScatteredStreamWriter::Extend() { |
| 50 | Reset(range: delegate_->GetNewBuffer()); |
| 51 | } |
| 52 | |
| 53 | void ScatteredStreamWriter::WriteBytesSlowPath(const uint8_t* src, |
| 54 | size_t size) { |
| 55 | size_t bytes_left = size; |
| 56 | while (bytes_left > 0) { |
| 57 | if (write_ptr_ >= cur_range_.end) |
| 58 | Extend(); |
| 59 | const size_t burst_size = std::min(bytes_available(), bytes_left); |
| 60 | WriteBytesUnsafe(src, size: burst_size); |
| 61 | bytes_left -= burst_size; |
| 62 | src += burst_size; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // TODO(primiano): perf optimization: I suspect that at the end this will always |
| 67 | // be called with |size| == 4, in which case we might just hardcode it. |
| 68 | uint8_t* ScatteredStreamWriter::ReserveBytes(size_t size) { |
| 69 | if (write_ptr_ + size > cur_range_.end) { |
| 70 | // Assume the reservations are always < Delegate::GetNewBuffer().size(), |
| 71 | // so that one single call to Extend() will definitely give enough headroom. |
| 72 | Extend(); |
| 73 | PERFETTO_DCHECK(write_ptr_ + size <= cur_range_.end); |
| 74 | } |
| 75 | uint8_t* begin = write_ptr_; |
| 76 | write_ptr_ += size; |
| 77 | #if PERFETTO_DCHECK_IS_ON() |
| 78 | // In the past, the service had a matching DCHECK in |
| 79 | // TraceBuffer::TryPatchChunkContents, which was assuming that service and all |
| 80 | // producers are built with matching DCHECK levels. This turned out to be a |
| 81 | // source of problems and was removed in b/197340286. This memset is useless |
| 82 | // these days and is here only to maintain ABI compatibility between producers |
| 83 | // that use a v20+ SDK and older versions of the service that were built in |
| 84 | // debug mode. At some point around 2023 it should be safe to remove it. |
| 85 | // (running a debug version of traced in production seems a bad idea |
| 86 | // regardless). |
| 87 | memset(begin, 0, size); |
| 88 | #endif |
| 89 | return begin; |
| 90 | } |
| 91 | |
| 92 | } // namespace protozero |
| 93 | |