• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 SRC_TRACING_CORE_TRACE_BUFFER_H_
18 #define SRC_TRACING_CORE_TRACE_BUFFER_H_
19 
20 #include <stdint.h>
21 #include <string.h>
22 
23 #include <array>
24 #include <limits>
25 #include <map>
26 #include <tuple>
27 
28 #include "perfetto/base/logging.h"
29 #include "perfetto/ext/base/paged_memory.h"
30 #include "perfetto/ext/base/thread_annotations.h"
31 #include "perfetto/ext/base/utils.h"
32 #include "perfetto/ext/tracing/core/basic_types.h"
33 #include "perfetto/ext/tracing/core/slice.h"
34 #include "perfetto/ext/tracing/core/trace_stats.h"
35 
36 namespace perfetto {
37 
38 class TracePacket;
39 
40 // The main buffer, owned by the tracing service, where all the trace data is
41 // ultimately stored into. The service will own several instances of this class,
42 // at least one per active consumer (as defined in the |buffers| section of
43 // trace_config.proto) and will copy chunks from the producer's shared memory
44 // buffers into here when a CommitData IPC is received.
45 //
46 // Writing into the buffer
47 // -----------------------
48 // Data is copied from the SMB(s) using CopyChunkUntrusted(). The buffer will
49 // hence contain data coming from different producers and different writer
50 // sequences, more specifically:
51 // - The service receives data by several producer(s), identified by their ID.
52 // - Each producer writes several sequences identified by the same WriterID.
53 //   (they correspond to TraceWriter instances in the producer).
54 // - Each Writer writes, in order, several chunks.
55 // - Each chunk contains zero, one, or more TracePacket(s), or even just
56 //   fragments of packets (when they span across several chunks).
57 //
58 // So at any point in time, the buffer will contain a variable number of logical
59 // sequences identified by the {ProducerID, WriterID} tuple. Any given chunk
60 // will only contain packets (or fragments) belonging to the same sequence.
61 //
62 // The buffer operates by default as a ring buffer.
63 // It has two overwrite policies:
64 //  1. kOverwrite (default): if the write pointer reaches the read pointer, old
65 //     unread chunks will be overwritten by new chunks.
66 //  2. kDiscard: if the write pointer reaches the read pointer, unread chunks
67 //     are preserved and the new chunks are discarded. Any future write becomes
68 //     a no-op, even if the reader manages to fully catch up. This is because
69 //     once a chunk is discarded, the sequence of packets is broken and trying
70 //     to recover would be too hard (also due to the fact that, at the same
71 //     time, we allow out-of-order commits and chunk re-writes).
72 //
73 // Chunks are (over)written in the same order of the CopyChunkUntrusted() calls.
74 // When overwriting old content, entire chunks are overwritten or clobbered.
75 // The buffer never leaves a partial chunk around. Chunks' payload is copied
76 // as-is, but their header is not and is repacked in order to keep the
77 // ProducerID around.
78 //
79 // Chunks are stored in the buffer next to each other. Each chunk is prefixed by
80 // an inline header (ChunkRecord), which contains most of the fields of the
81 // SharedMemoryABI ChunkHeader + the ProducerID + the size of the payload.
82 // It's a conventional binary object stream essentially, where each ChunkRecord
83 // tells where it ends and hence where to find the next one, like this:
84 //
85 //          .-------------------------. 16 byte boundary
86 //          | ChunkRecord:   16 bytes |
87 //          | - chunk id:     4 bytes |
88 //          | - producer id:  2 bytes |
89 //          | - writer id:    2 bytes |
90 //          | - #fragments:   2 bytes |
91 //    +-----+ - record size:  2 bytes |
92 //    |     | - flags+pad:    4 bytes |
93 //    |     +-------------------------+
94 //    |     |                         |
95 //    |     :     Chunk payload       :
96 //    |     |                         |
97 //    |     +-------------------------+
98 //    |     |    Optional padding     |
99 //    +---> +-------------------------+ 16 byte boundary
100 //          |      ChunkRecord        |
101 //          :                         :
102 // Chunks stored in the buffer are always rounded up to 16 bytes (that is
103 // sizeof(ChunkRecord)), in order to avoid further inner fragmentation.
104 // Special "padding" chunks can be put in the buffer, e.g. in the case when we
105 // try to write a chunk of size N while the write pointer is at the end of the
106 // buffer, but the write pointer is < N bytes from the end (and hence needs to
107 // wrap over).
108 // Because of this, the buffer is self-describing: the contents of the buffer
109 // can be reconstructed by just looking at the buffer content (this will be
110 // quite useful in future to recover the buffer from crash reports).
111 //
112 // However, in order to keep some operations (patching and reading) fast, a
113 // lookaside index is maintained (in |index_|), keeping each chunk in the buffer
114 // indexed by their {ProducerID, WriterID, ChunkID} tuple.
115 //
116 // Patching data out-of-band
117 // -------------------------
118 // This buffer also supports patching chunks' payload out-of-band, after they
119 // have been stored. This is to allow producers to backfill the "size" fields
120 // of the protos that spawn across several chunks, when the previous chunks are
121 // returned to the service. The MaybePatchChunkContents() deals with the fact
122 // that a chunk might have been lost (because of wrapping) by the time the OOB
123 // IPC comes.
124 //
125 // Reading from the buffer
126 // -----------------------
127 // This class supports one reader only (the consumer). Reads are NOT idempotent
128 // as they move the read cursors around. Reading back the buffer is the most
129 // conceptually complex part. The ReadNextTracePacket() method operates with
130 // whole packet granularity. Packets are returned only when all their fragments
131 // are available.
132 // This class takes care of:
133 // - Gluing packets within the same sequence, even if they are not stored
134 //   adjacently in the buffer.
135 // - Re-ordering chunks within a sequence (using the ChunkID, which wraps).
136 // - Detecting holes in packet fragments (because of loss of chunks).
137 // Reads guarantee that packets for the same sequence are read in FIFO order
138 // (according to their ChunkID), but don't give any guarantee about the read
139 // order of packets from different sequences, see comments in
140 // ReadNextTracePacket() below.
141 class TraceBuffer {
142  public:
143   static const size_t InlineChunkHeaderSize;  // For test/fake_packet.{cc,h}.
144 
145   // See comment in the header above.
146   enum OverwritePolicy { kOverwrite, kDiscard };
147 
148   // Argument for out-of-band patches applied through TryPatchChunkContents().
149   struct Patch {
150     // From SharedMemoryABI::kPacketHeaderSize.
151     static constexpr size_t kSize = 4;
152 
153     size_t offset_untrusted;
154     std::array<uint8_t, kSize> data;
155   };
156 
157   // Identifiers that are constant for a packet sequence.
158   struct PacketSequenceProperties {
159     ProducerID producer_id_trusted;
160     uid_t producer_uid_trusted;
161     WriterID writer_id;
162   };
163 
164   // Can return nullptr if the memory allocation fails.
165   static std::unique_ptr<TraceBuffer> Create(size_t size_in_bytes,
166                                              OverwritePolicy = kOverwrite);
167 
168   ~TraceBuffer();
169 
170   // Copies a Chunk from a producer Shared Memory Buffer into the trace buffer.
171   // |src| points to the first packet in the SharedMemoryABI's chunk shared with
172   // an untrusted producer. "untrusted" here means: the producer might be
173   // malicious and might change |src| concurrently while we read it (internally
174   // this method memcpy()-s first the chunk before processing it). None of the
175   // arguments should be trusted, unless otherwise stated. We can trust that
176   // |src| points to a valid memory area, but not its contents.
177   //
178   // This method may be called multiple times for the same chunk. In this case,
179   // the original chunk's payload will be overridden and its number of fragments
180   // and flags adjusted to match |num_fragments| and |chunk_flags|. The service
181   // may use this to insert partial chunks (|chunk_complete = false|) before the
182   // producer has committed them.
183   //
184   // If |chunk_complete| is |false|, the TraceBuffer will only consider the
185   // first |num_fragments - 1| packets to be complete, since the producer may
186   // not have finished writing the latest packet. Reading from a sequence will
187   // also not progress past any incomplete chunks until they were rewritten with
188   // |chunk_complete = true|, e.g. after a producer's commit.
189   //
190   // TODO(eseckler): Pass in a PacketStreamProperties instead of individual IDs.
191   void CopyChunkUntrusted(ProducerID producer_id_trusted,
192                           uid_t producer_uid_trusted,
193                           WriterID writer_id,
194                           ChunkID chunk_id,
195                           uint16_t num_fragments,
196                           uint8_t chunk_flags,
197                           bool chunk_complete,
198                           const uint8_t* src,
199                           size_t size);
200   // Applies a batch of |patches| to the given chunk, if the given chunk is
201   // still in the buffer. Does nothing if the given ChunkID is gone.
202   // Returns true if the chunk has been found and patched, false otherwise.
203   // |other_patches_pending| is used to determine whether this is the only
204   // batch of patches for the chunk or there is more.
205   // If |other_patches_pending| == false, the chunk is marked as ready to be
206   // consumed. If true, the state of the chunk is not altered.
207   //
208   // Note: If the producer is batching commits (see shared_memory_arbiter.h), it
209   // will also attempt to do patching locally. Namely, if nested messages are
210   // completed while the chunk on which they started is being batched (i.e.
211   // before it has been committed to the service), the producer will apply the
212   // respective patches to the batched chunk. These patches will not be sent to
213   // the service - i.e. only the patches that the producer did not manage to
214   // apply before committing the chunk will be applied here.
215   bool TryPatchChunkContents(ProducerID,
216                              WriterID,
217                              ChunkID,
218                              const Patch* patches,
219                              size_t patches_size,
220                              bool other_patches_pending);
221 
222   // To read the contents of the buffer the caller needs to:
223   //   BeginRead()
224   //   while (ReadNextTracePacket(packet_fragments)) { ... }
225   // No other calls to any other method should be interleaved between
226   // BeginRead() and ReadNextTracePacket().
227   // Reads in the TraceBuffer are NOT idempotent.
228   void BeginRead();
229 
230   // Returns the next packet in the buffer, if any, and the producer_id,
231   // producer_uid, and writer_id of the producer/writer that wrote it (as passed
232   // in the CopyChunkUntrusted() call). Returns false if no packets can be read
233   // at this point. If a packet was read successfully,
234   // |previous_packet_on_sequence_dropped| is set to |true| if the previous
235   // packet on the sequence was dropped from the buffer before it could be read
236   // (e.g. because its chunk was overridden due to the ring buffer wrapping or
237   // due to an ABI violation), and to |false| otherwise.
238   //
239   // This function returns only complete packets. Specifically:
240   // When there is at least one complete packet in the buffer, this function
241   // returns true and populates the TracePacket argument with the boundaries of
242   // each fragment for one packet.
243   // TracePacket will have at least one slice when this function returns true.
244   // When there are no whole packets eligible to read (e.g. we are still missing
245   // fragments) this function returns false.
246   // This function guarantees also that packets for a given
247   // {ProducerID, WriterID} are read in FIFO order.
248   // This function does not guarantee any ordering w.r.t. packets belonging to
249   // different WriterID(s). For instance, given the following packets copied
250   // into the buffer:
251   //   {ProducerID: 1, WriterID: 1}: P1 P2 P3
252   //   {ProducerID: 1, WriterID: 2}: P4 P5 P6
253   //   {ProducerID: 2, WriterID: 1}: P7 P8 P9
254   // The following read sequence is possible:
255   //   P1, P4, P7, P2, P3, P5, P8, P9, P6
256   // But the following is guaranteed to NOT happen:
257   //   P1, P5, P7, P4 (P4 cannot come after P5)
258   bool ReadNextTracePacket(TracePacket*,
259                            PacketSequenceProperties* sequence_properties,
260                            bool* previous_packet_on_sequence_dropped);
261 
stats()262   const TraceStats::BufferStats& stats() const { return stats_; }
size()263   size_t size() const { return size_; }
264 
265  private:
266   friend class TraceBufferTest;
267 
268   // ChunkRecord is a Chunk header stored inline in the |data_| buffer, before
269   // the chunk payload (the packets' data). The |data_| buffer looks like this:
270   // +---------------+------------------++---------------+-----------------+
271   // | ChunkRecord 1 | Chunk payload 1  || ChunkRecord 2 | Chunk payload 2 | ...
272   // +---------------+------------------++---------------+-----------------+
273   // Most of the ChunkRecord fields are copied from SharedMemoryABI::ChunkHeader
274   // (the chunk header used in the shared memory buffers).
275   // A ChunkRecord can be a special "padding" record. In this case its payload
276   // should be ignored and the record should be just skipped.
277   //
278   // Full page move optimization:
279   // This struct has to be exactly (sizeof(PageHeader) + sizeof(ChunkHeader))
280   // (from shared_memory_abi.h) to allow full page move optimizations
281   // (TODO(primiano): not implemented yet). In the special case of moving a full
282   // 4k page that contains only one chunk, in fact, we can just ask the kernel
283   // to move the full SHM page (see SPLICE_F_{GIFT,MOVE}) and overlay the
284   // ChunkRecord on top of the moved SMB's header (page + chunk header).
285   // This special requirement is covered by static_assert(s) in the .cc file.
286   struct ChunkRecord {
ChunkRecordChunkRecord287     explicit ChunkRecord(size_t sz) : flags{0}, is_padding{0} {
288       PERFETTO_DCHECK(sz >= sizeof(ChunkRecord) &&
289                       sz % sizeof(ChunkRecord) == 0 && sz <= kMaxSize);
290       size = static_cast<decltype(size)>(sz);
291     }
292 
is_validChunkRecord293     bool is_valid() const { return size != 0; }
294 
295     // Keep this structure packed and exactly 16 bytes (128 bits) big.
296 
297     // [32 bits] Monotonic counter within the same writer_id.
298     ChunkID chunk_id = 0;
299 
300     // [16 bits] ID of the Producer from which the Chunk was copied from.
301     ProducerID producer_id = 0;
302 
303     // [16 bits] Unique per Producer (but not within the service).
304     // If writer_id == kWriterIdPadding the record should just be skipped.
305     WriterID writer_id = 0;
306 
307     // Number of fragments contained in the chunk.
308     uint16_t num_fragments = 0;
309 
310     // Size in bytes, including sizeof(ChunkRecord) itself.
311     uint16_t size;
312 
313     uint8_t flags : 6;  // See SharedMemoryABI::ChunkHeader::flags.
314     uint8_t is_padding : 1;
315     uint8_t unused_flag : 1;
316 
317     // Not strictly needed, can be reused for more fields in the future. But
318     // right now helps to spot chunks in hex dumps.
319     char unused[3] = {'C', 'H', 'U'};
320 
321     static constexpr size_t kMaxSize =
322         std::numeric_limits<decltype(size)>::max();
323   };
324 
325   // Lookaside index entry. This serves two purposes:
326   // 1) Allow a fast lookup of ChunkRecord by their ID (the tuple
327   //   {ProducerID, WriterID, ChunkID}). This is used when applying out-of-band
328   //   patches to the contents of the chunks after they have been copied into
329   //   the TraceBuffer.
330   // 2) keep the chunks ordered by their ID. This is used when reading back.
331   // 3) Keep metadata about the status of the chunk, e.g. whether the contents
332   //    have been read already and should be skipped in a future read pass.
333   // This struct should not have any field that is essential for reconstructing
334   // the contents of the buffer from a crash dump.
335   struct ChunkMeta {
336     // Key used for sorting in the map.
337     struct Key {
KeyChunkMeta::Key338       Key(ProducerID p, WriterID w, ChunkID c)
339           : producer_id{p}, writer_id{w}, chunk_id{c} {}
340 
KeyChunkMeta::Key341       explicit Key(const ChunkRecord& cr)
342           : Key(cr.producer_id, cr.writer_id, cr.chunk_id) {}
343 
344       // Note that this sorting doesn't keep into account the fact that ChunkID
345       // will wrap over at some point. The extra logic in SequenceIterator deals
346       // with that.
347       bool operator<(const Key& other) const {
348         return std::tie(producer_id, writer_id, chunk_id) <
349                std::tie(other.producer_id, other.writer_id, other.chunk_id);
350       }
351 
352       bool operator==(const Key& other) const {
353         return std::tie(producer_id, writer_id, chunk_id) ==
354                std::tie(other.producer_id, other.writer_id, other.chunk_id);
355       }
356 
357       bool operator!=(const Key& other) const { return !(*this == other); }
358 
359       // These fields should match at all times the corresponding fields in
360       // the |chunk_record|. They are copied here purely for efficiency to avoid
361       // dereferencing the buffer all the time.
362       ProducerID producer_id;
363       WriterID writer_id;
364       ChunkID chunk_id;
365     };
366 
367     enum IndexFlags : uint8_t {
368       // If set, the chunk state was kChunkComplete at the time it was copied.
369       // If unset, the chunk was still kChunkBeingWritten while copied. When
370       // reading from the chunk's sequence, the sequence will not advance past
371       // this chunk until this flag is set.
372       kComplete = 1 << 0,
373 
374       // If set, we skipped the last packet that we read from this chunk e.g.
375       // because we it was a continuation from a previous chunk that was dropped
376       // or due to an ABI violation.
377       kLastReadPacketSkipped = 1 << 1
378     };
379 
ChunkMetaChunkMeta380     ChunkMeta(ChunkRecord* r, uint16_t p, bool complete, uint8_t f, uid_t u)
381         : chunk_record{r}, trusted_uid{u}, flags{f}, num_fragments{p} {
382       if (complete)
383         index_flags = kComplete;
384     }
385 
is_completeChunkMeta386     bool is_complete() const { return index_flags & kComplete; }
387 
set_completeChunkMeta388     void set_complete(bool complete) {
389       if (complete) {
390         index_flags |= kComplete;
391       } else {
392         index_flags &= ~kComplete;
393       }
394     }
395 
last_read_packet_skippedChunkMeta396     bool last_read_packet_skipped() const {
397       return index_flags & kLastReadPacketSkipped;
398     }
399 
set_last_read_packet_skippedChunkMeta400     void set_last_read_packet_skipped(bool skipped) {
401       if (skipped) {
402         index_flags |= kLastReadPacketSkipped;
403       } else {
404         index_flags &= ~kLastReadPacketSkipped;
405       }
406     }
407 
408     ChunkRecord* const chunk_record;  // Addr of ChunkRecord within |data_|.
409     const uid_t trusted_uid;          // uid of the producer.
410 
411     // Flags set by TraceBuffer to track the state of the chunk in the index.
412     uint8_t index_flags = 0;
413 
414     // Correspond to |chunk_record->flags| and |chunk_record->num_fragments|.
415     // Copied here for performance reasons (avoids having to dereference
416     // |chunk_record| while iterating over ChunkMeta) and to aid debugging in
417     // case the buffer gets corrupted.
418     uint8_t flags = 0;           // See SharedMemoryABI::ChunkHeader::flags.
419     uint16_t num_fragments = 0;  // Total number of packet fragments.
420 
421     uint16_t num_fragments_read = 0;  // Number of fragments already read.
422 
423     // The start offset of the next fragment (the |num_fragments_read|-th) to be
424     // read. This is the offset in bytes from the beginning of the ChunkRecord's
425     // payload (the 1st fragment starts at |chunk_record| +
426     // sizeof(ChunkRecord)).
427     uint16_t cur_fragment_offset = 0;
428   };
429 
430   using ChunkMap = std::map<ChunkMeta::Key, ChunkMeta>;
431 
432   // Allows to iterate over a sub-sequence of |index_| for all keys belonging to
433   // the same {ProducerID,WriterID}. Furthermore takes into account the wrapping
434   // of ChunkID. Instances are valid only as long as the |index_| is not altered
435   // (can be used safely only between adjacent ReadNextTracePacket() calls).
436   // The order of the iteration will proceed in the following order:
437   // |wrapping_id| + 1 -> |seq_end|, |seq_begin| -> |wrapping_id|.
438   // Practical example:
439   // - Assume that kMaxChunkID == 7
440   // - Assume that we have all 8 chunks in the range (0..7).
441   // - Hence, |seq_begin| == c0, |seq_end| == c7
442   // - Assume |wrapping_id| = 4 (c4 is the last chunk copied over
443   //   through a CopyChunkUntrusted()).
444   // The resulting iteration order will be: c5, c6, c7, c0, c1, c2, c3, c4.
445   struct SequenceIterator {
446     // Points to the 1st key (the one with the numerically min ChunkID).
447     ChunkMap::iterator seq_begin;
448 
449     // Points one past the last key (the one with the numerically max ChunkID).
450     ChunkMap::iterator seq_end;
451 
452     // Current iterator, always >= seq_begin && <= seq_end.
453     ChunkMap::iterator cur;
454 
455     // The latest ChunkID written. Determines the start/end of the sequence.
456     ChunkID wrapping_id;
457 
is_validSequenceIterator458     bool is_valid() const { return cur != seq_end; }
459 
producer_idSequenceIterator460     ProducerID producer_id() const {
461       PERFETTO_DCHECK(is_valid());
462       return cur->first.producer_id;
463     }
464 
writer_idSequenceIterator465     WriterID writer_id() const {
466       PERFETTO_DCHECK(is_valid());
467       return cur->first.writer_id;
468     }
469 
chunk_idSequenceIterator470     ChunkID chunk_id() const {
471       PERFETTO_DCHECK(is_valid());
472       return cur->first.chunk_id;
473     }
474 
475     ChunkMeta& operator*() {
476       PERFETTO_DCHECK(is_valid());
477       return cur->second;
478     }
479 
480     // Moves |cur| to the next chunk in the index.
481     // is_valid() will become false after calling this, if this was the last
482     // entry of the sequence.
483     void MoveNext();
484 
MoveToEndSequenceIterator485     void MoveToEnd() { cur = seq_end; }
486   };
487 
488   enum class ReadAheadResult {
489     kSucceededReturnSlices,
490     kFailedMoveToNextSequence,
491     kFailedStayOnSameSequence,
492   };
493 
494   enum class ReadPacketResult {
495     kSucceeded,
496     kFailedInvalidPacket,
497     kFailedEmptyPacket,
498   };
499 
500   explicit TraceBuffer(OverwritePolicy);
501   TraceBuffer(const TraceBuffer&) = delete;
502   TraceBuffer& operator=(const TraceBuffer&) = delete;
503 
504   bool Initialize(size_t size);
505 
506   // Returns an object that allows to iterate over chunks in the |index_| that
507   // have the same {ProducerID, WriterID} of
508   // |seq_begin.first.{producer,writer}_id|. |seq_begin| must be an iterator to
509   // the first entry in the |index_| that has a different {ProducerID, WriterID}
510   // from the previous one. It is valid for |seq_begin| to be == index_.end()
511   // (i.e. if the index is empty). The iteration takes care of ChunkID wrapping,
512   // by using |last_chunk_id_|.
513   SequenceIterator GetReadIterForSequence(ChunkMap::iterator seq_begin);
514 
515   // Used as a last resort when a buffer corruption is detected.
516   void ClearContentsAndResetRWCursors();
517 
518   // Adds a padding record of the given size (must be a multiple of
519   // sizeof(ChunkRecord)).
520   void AddPaddingRecord(size_t);
521 
522   // Look for contiguous fragment of the same packet starting from |read_iter_|.
523   // If a contiguous packet is found, all the fragments are pushed into
524   // TracePacket and the function returns kSucceededReturnSlices. If not, the
525   // function returns either kFailedMoveToNextSequence or
526   // kFailedStayOnSameSequence, telling the caller to continue looking for
527   // packets.
528   ReadAheadResult ReadAhead(TracePacket*);
529 
530   // Deletes (by marking the record invalid and removing form the index) all
531   // chunks from |wptr_| to |wptr_| + |bytes_to_clear|.
532   // Returns:
533   //   * The size of the gap left between the next valid Chunk and the end of
534   //     the deletion range.
535   //   * 0 if no next valid chunk exists (if the buffer is still zeroed).
536   //   * -1 if the buffer |overwrite_policy_| == kDiscard and the deletion would
537   //     cause unread chunks to be overwritten. In this case the buffer is left
538   //     untouched.
539   // Graphically, assume the initial situation is the following (|wptr_| = 10).
540   // |0        |10 (wptr_)       |30       |40                 |60
541   // +---------+-----------------+---------+-------------------+---------+
542   // | Chunk 1 | Chunk 2         | Chunk 3 | Chunk 4           | Chunk 5 |
543   // +---------+-----------------+---------+-------------------+---------+
544   //           |_________Deletion range_______|~~return value~~|
545   //
546   // A call to DeleteNextChunksFor(32) will remove chunks 2,3,4 and return 18
547   // (60 - 42), the distance between chunk 5 and the end of the deletion range.
548   ssize_t DeleteNextChunksFor(size_t bytes_to_clear);
549 
550   // Decodes the boundaries of the next packet (or a fragment) pointed by
551   // ChunkMeta and pushes that into |TracePacket|. It also increments the
552   // |num_fragments_read| counter.
553   // TracePacket can be nullptr, in which case the read state is still advanced.
554   // When TracePacket is not nullptr, ProducerID must also be not null and will
555   // be updated with the ProducerID that originally wrote the chunk.
556   ReadPacketResult ReadNextPacketInChunk(ChunkMeta*, TracePacket*);
557 
DcheckIsAlignedAndWithinBounds(const uint8_t * ptr)558   void DcheckIsAlignedAndWithinBounds(const uint8_t* ptr) const {
559     PERFETTO_DCHECK(ptr >= begin() && ptr <= end() - sizeof(ChunkRecord));
560     PERFETTO_DCHECK(
561         (reinterpret_cast<uintptr_t>(ptr) & (alignof(ChunkRecord) - 1)) == 0);
562   }
563 
GetChunkRecordAt(uint8_t * ptr)564   ChunkRecord* GetChunkRecordAt(uint8_t* ptr) {
565     DcheckIsAlignedAndWithinBounds(ptr);
566     // We may be accessing a new (empty) record.
567     data_.EnsureCommitted(
568         static_cast<size_t>(ptr + sizeof(ChunkRecord) - begin()));
569     return reinterpret_cast<ChunkRecord*>(ptr);
570   }
571 
572   void DiscardWrite();
573 
574   // |src| can be nullptr (in which case |size| must be ==
575   // record.size - sizeof(ChunkRecord)), for the case of writing a padding
576   // record. |wptr_| is NOT advanced by this function, the caller must do that.
WriteChunkRecord(uint8_t * wptr,const ChunkRecord & record,const uint8_t * src,size_t size)577   void WriteChunkRecord(uint8_t* wptr,
578                         const ChunkRecord& record,
579                         const uint8_t* src,
580                         size_t size) {
581     // Note: |record.size| will be slightly bigger than |size| because of the
582     // ChunkRecord header and rounding, to ensure that all ChunkRecord(s) are
583     // multiple of sizeof(ChunkRecord). The invariant is:
584     // record.size >= |size| + sizeof(ChunkRecord) (== if no rounding).
585     PERFETTO_DCHECK(size <= ChunkRecord::kMaxSize);
586     PERFETTO_DCHECK(record.size >= sizeof(record));
587     PERFETTO_DCHECK(record.size % sizeof(record) == 0);
588     PERFETTO_DCHECK(record.size >= size + sizeof(record));
589     PERFETTO_CHECK(record.size <= size_to_end());
590     DcheckIsAlignedAndWithinBounds(wptr);
591 
592     // We may be writing to this area for the first time.
593     data_.EnsureCommitted(static_cast<size_t>(wptr + record.size - begin()));
594 
595     // Deliberately not a *D*CHECK.
596     PERFETTO_CHECK(wptr + sizeof(record) + size <= end());
597     memcpy(wptr, &record, sizeof(record));
598     if (PERFETTO_LIKELY(src)) {
599       // If the producer modifies the data in the shared memory buffer while we
600       // are copying it to the central buffer, TSAN will (rightfully) flag that
601       // as a race. However the entire purpose of copying the data into the
602       // central buffer is that we can validate it without worrying that the
603       // producer changes it from under our feet, so this race is benign. The
604       // alternative would be to try computing which part of the buffer is safe
605       // to read (assuming a well-behaving client), but the risk of introducing
606       // a bug that way outweighs the benefit.
607       PERFETTO_ANNOTATE_BENIGN_RACE_SIZED(
608           src, size, "Benign race when copying chunk from shared memory.")
609       memcpy(wptr + sizeof(record), src, size);
610     } else {
611       PERFETTO_DCHECK(size == record.size - sizeof(record));
612     }
613     const size_t rounding_size = record.size - sizeof(record) - size;
614     memset(wptr + sizeof(record) + size, 0, rounding_size);
615   }
616 
begin()617   uint8_t* begin() const { return reinterpret_cast<uint8_t*>(data_.Get()); }
end()618   uint8_t* end() const { return begin() + size_; }
size_to_end()619   size_t size_to_end() const { return static_cast<size_t>(end() - wptr_); }
620 
621   base::PagedMemory data_;
622   size_t size_ = 0;            // Size in bytes of |data_|.
623   size_t max_chunk_size_ = 0;  // Max size in bytes allowed for a chunk.
624   uint8_t* wptr_ = nullptr;    // Write pointer.
625 
626   // An index that keeps track of the positions and metadata of each
627   // ChunkRecord.
628   ChunkMap index_;
629 
630   // Read iterator used for ReadNext(). It is reset by calling BeginRead().
631   // It becomes invalid after any call to methods that alters the |index_|.
632   SequenceIterator read_iter_;
633 
634   // See comments at the top of the file.
635   OverwritePolicy overwrite_policy_ = kOverwrite;
636 
637   // Only used when |overwrite_policy_ == kDiscard|. This is set the first time
638   // a write fails because it would overwrite unread chunks.
639   bool discard_writes_ = false;
640 
641   // Keeps track of the highest ChunkID written for a given sequence, taking
642   // into account a potential overflow of ChunkIDs. In the case of overflow,
643   // stores the highest ChunkID written since the overflow.
644   //
645   // TODO(primiano): should clean up keys from this map. Right now it grows
646   // without bounds (although realistically is not a problem unless we have too
647   // many producers/writers within the same trace session).
648   std::map<std::pair<ProducerID, WriterID>, ChunkID> last_chunk_id_written_;
649 
650   // Statistics about buffer usage.
651   TraceStats::BufferStats stats_;
652 
653 #if PERFETTO_DCHECK_IS_ON()
654   bool changed_since_last_read_ = false;
655 #endif
656 
657   // When true disable some DCHECKs that have been put in place to detect
658   // bugs in the producers. This is for tests that feed malicious inputs and
659   // hence mimic a buggy producer.
660   bool suppress_client_dchecks_for_testing_ = false;
661 };
662 
663 }  // namespace perfetto
664 
665 #endif  // SRC_TRACING_CORE_TRACE_BUFFER_H_
666