OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/tracing/core/scattered_stream_writer.h" | |
6 | |
7 #include <string.h> | |
8 | |
9 #include <algorithm> | |
10 | |
11 #include "base/logging.h" | |
12 | |
13 namespace tracing { | |
14 namespace v2 { | |
15 | |
16 ScatteredStreamWriter::ScatteredStreamWriter(Delegate* delegate) | |
17 : delegate_(delegate), | |
18 cur_range_({nullptr, nullptr}), | |
19 write_ptr_(nullptr) {} | |
20 | |
21 ScatteredStreamWriter::~ScatteredStreamWriter() {} | |
22 | |
23 void ScatteredStreamWriter::Reset(ContiguousMemoryRange range) { | |
24 cur_range_ = range; | |
25 write_ptr_ = range.begin; | |
26 DCHECK_LT(write_ptr_, cur_range_.end); | |
27 } | |
28 | |
29 void ScatteredStreamWriter::Extend() { | |
30 Reset(delegate_->GetNewBuffer()); | |
31 } | |
32 | |
33 void ScatteredStreamWriter::WriteByte(uint8_t value) { | |
34 if (write_ptr_ >= cur_range_.end) | |
35 Extend(); | |
36 *write_ptr_++ = value; | |
37 } | |
38 | |
39 void ScatteredStreamWriter::WriteBytes(const uint8_t* src, size_t size) { | |
40 uint8_t* const end = write_ptr_ + size; | |
41 if (end <= cur_range_.end) { | |
42 // Fast-path, the buffer fits into the current contiguous range. | |
43 // TODO(primiano): perf optimization, this is a tracing hot path. The | |
44 // compiler can make strong optimization on memcpy if the size arg is a | |
45 // constexpr. Make a templated variant of this for fixed-size writes. | |
46 memcpy(write_ptr_, src, size); | |
47 write_ptr_ = end; | |
48 return; | |
49 } | |
50 // Slow path, scatter the writes. | |
51 size_t bytes_left = size; | |
52 while (bytes_left > 0) { | |
53 if (write_ptr_ >= cur_range_.end) | |
54 Extend(); | |
55 const size_t burst_size = std::min(bytes_available(), bytes_left); | |
56 WriteBytes(src, burst_size); | |
57 bytes_left -= burst_size; | |
58 src += burst_size; | |
59 } | |
60 } | |
61 | |
62 // TODO(primiano): perf optimization: I suspect that at the end this will always | |
63 // be called with |size| == 4, in which case we might just hardcode it. | |
64 ContiguousMemoryRange ScatteredStreamWriter::ReserveBytes(size_t size) { | |
65 // Assume the reservations are always < TraceRingBuffer::Chunk::kSize. | |
66 | |
petrcermak
2016/06/30 12:39:04
nit: this blank line seems unnecessary (it actuall
Primiano Tucci (use gerrit)
2016/06/30 14:41:24
Oops agree. in fact that is just a typo accident.
| |
67 if (write_ptr_ + size > cur_range_.end) { | |
68 Extend(); | |
69 DCHECK_LE(write_ptr_ + size, cur_range_.end); | |
70 } | |
71 uint8_t* begin = write_ptr_; | |
72 write_ptr_ += size; | |
73 #ifndef NDEBUG | |
74 memset(begin, '\xFF', size); | |
75 #endif | |
76 return {begin, begin + size}; | |
77 } | |
78 | |
79 } // namespace v2 | |
80 } // namespace tracing | |
OLD | NEW |