Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(448)

Side by Side Diff: components/tracing/core/scattered_stream_writer.cc

Issue 2047273002: tracing v2: Add ScatteredStreamWriter (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@tv_ups1
Patch Set: Typo Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 if (write_ptr_ + size > cur_range_.end) {
67 Extend();
68 DCHECK_LE(write_ptr_ + size, cur_range_.end);
69 }
70 uint8_t* begin = write_ptr_;
71 write_ptr_ += size;
72 #ifndef NDEBUG
73 memset(begin, '\xFF', size);
74 #endif
75 return {begin, begin + size};
76 }
77
78 } // namespace v2
79 } // namespace tracing
OLDNEW
« no previous file with comments | « components/tracing/core/scattered_stream_writer.h ('k') | components/tracing/core/scattered_stream_writer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698