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 #ifndef INCLUDE_PERFETTO_PROTOZERO_SCATTERED_STREAM_WRITER_H_ 18 #define INCLUDE_PERFETTO_PROTOZERO_SCATTERED_STREAM_WRITER_H_ 19 20 #include <assert.h> 21 #include <stddef.h> 22 #include <stdint.h> 23 #include <string.h> 24 25 #include "perfetto/base/compiler.h" 26 #include "perfetto/base/export.h" 27 #include "perfetto/protozero/contiguous_memory_range.h" 28 29 namespace protozero { 30 31 // This class deals with the following problem: append-only proto messages want 32 // to write a stream of bytes, without caring about the implementation of the 33 // underlying buffer (which concretely will be either the trace ring buffer 34 // or a heap-allocated buffer). The main deal is: proto messages don't know in 35 // advance what their size will be. 36 // Due to the tracing buffer being split into fixed-size chunks, on some 37 // occasions, these writes need to be spread over two (or more) non-contiguous 38 // chunks of memory. Similarly, when the buffer is backed by the heap, we want 39 // to avoid realloc() calls, as they might cause a full copy of the contents 40 // of the buffer. 41 // The purpose of this class is to abstract away the non-contiguous write logic. 42 // This class knows how to deal with writes as long as they fall in the same 43 // ContiguousMemoryRange and defers the chunk-chaining logic to the Delegate. 44 class PERFETTO_EXPORT_COMPONENT ScatteredStreamWriter { 45 public: 46 class PERFETTO_EXPORT_COMPONENT Delegate { 47 public: 48 static constexpr size_t kPatchSize = 4; 49 virtual ~Delegate(); 50 51 // Returns a new chunk for writing. 52 virtual ContiguousMemoryRange GetNewBuffer() = 0; 53 54 // Signals the delegate that the location pointed by `to_patch` (which must 55 // be in the last chunk returned by GetNewBuffer()), kPatchSize long, needs 56 // to be updated later (after potentially multiple GetNewBuffer calls). 57 // 58 // The caller must write to the returned location later. If the returned 59 // pointer is nullptr, the caller should not write anything. 60 // 61 // The implementation considers the patch ready to apply when the caller 62 // writes the first byte a value that's different than 0 (the 63 // implementation periodically checks for this). 64 virtual uint8_t* AnnotatePatch(uint8_t* patch_addr); 65 }; 66 67 explicit ScatteredStreamWriter(Delegate* delegate); 68 ~ScatteredStreamWriter(); 69 WriteByte(uint8_t value)70 inline void WriteByte(uint8_t value) { 71 if (write_ptr_ >= cur_range_.end) 72 Extend(); 73 *write_ptr_++ = value; 74 } 75 76 // Assumes that the caller checked that there is enough headroom. 77 // TODO(primiano): perf optimization, this is a tracing hot path. The 78 // compiler can make strong optimization on memcpy if the size arg is a 79 // constexpr. Make a templated variant of this for fixed-size writes. 80 // TODO(primiano): restrict / noalias might also help. WriteBytesUnsafe(const uint8_t * src,size_t size)81 inline void WriteBytesUnsafe(const uint8_t* src, size_t size) { 82 uint8_t* const end = write_ptr_ + size; 83 assert(end <= cur_range_.end); 84 memcpy(write_ptr_, src, size); 85 write_ptr_ = end; 86 } 87 WriteBytes(const uint8_t * src,size_t size)88 inline void WriteBytes(const uint8_t* src, 89 size_t size) PERFETTO_NO_SANITIZE_UNDEFINED { 90 // If the stream writer hasn't been initialized, constructing the end 91 // pointer below invokes undefined behavior because `write_ptr_` is null. 92 // Since this function is on the hot path, we suppress the warning instead 93 // of adding a conditional branch. 94 uint8_t* const end = write_ptr_ + size; 95 if (PERFETTO_LIKELY(end <= cur_range_.end)) 96 return WriteBytesUnsafe(src, size); 97 WriteBytesSlowPath(src, size); 98 } 99 100 void WriteBytesSlowPath(const uint8_t* src, size_t size); 101 102 // Reserves a fixed amount of bytes to be backfilled later. The reserved range 103 // is guaranteed to be contiguous and not span across chunks. |size| has to be 104 // <= than the size of a new buffer returned by the Delegate::GetNewBuffer(). 105 uint8_t* ReserveBytes(size_t size); 106 107 // Fast (but unsafe) version of the above. The caller must have previously 108 // checked that there are at least |size| contiguous bytes available. 109 // Returns only the start pointer of the reservation. ReserveBytesUnsafe(size_t size)110 uint8_t* ReserveBytesUnsafe(size_t size) { 111 uint8_t* begin = write_ptr_; 112 write_ptr_ += size; 113 assert(write_ptr_ <= cur_range_.end); 114 return begin; 115 } 116 117 // Resets the buffer boundaries and the write pointer to the given |range|. 118 // Subsequent WriteByte(s) will write into |range|. 119 void Reset(ContiguousMemoryRange range); 120 121 // Commits the current chunk and gets a new chunk from the delegate. 122 void Extend(); 123 124 // Number of contiguous free bytes in |cur_range_| that can be written without 125 // requesting a new buffer. bytes_available()126 size_t bytes_available() const { 127 return static_cast<size_t>(cur_range_.end - write_ptr_); 128 } 129 cur_range()130 ContiguousMemoryRange cur_range() const { return cur_range_; } 131 write_ptr()132 uint8_t* write_ptr() const { return write_ptr_; } 133 set_write_ptr(uint8_t * write_ptr)134 void set_write_ptr(uint8_t* write_ptr) { 135 assert(cur_range_.begin <= write_ptr && write_ptr <= cur_range_.end); 136 write_ptr_ = write_ptr; 137 } 138 written()139 uint64_t written() const { 140 return written_previously_ + 141 static_cast<uint64_t>(write_ptr_ - cur_range_.begin); 142 } 143 written_previously()144 uint64_t written_previously() const { return written_previously_; } 145 AnnotatePatch(uint8_t * patch_addr)146 uint8_t* AnnotatePatch(uint8_t* patch_addr) { 147 return delegate_->AnnotatePatch(patch_addr); 148 } 149 150 private: 151 ScatteredStreamWriter(const ScatteredStreamWriter&) = delete; 152 ScatteredStreamWriter& operator=(const ScatteredStreamWriter&) = delete; 153 154 Delegate* const delegate_; 155 ContiguousMemoryRange cur_range_; 156 uint8_t* write_ptr_; 157 uint64_t written_previously_ = 0; 158 }; 159 160 } // namespace protozero 161 162 #endif // INCLUDE_PERFETTO_PROTOZERO_SCATTERED_STREAM_WRITER_H_ 163