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

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: Created 4 years, 6 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 "base/logging.h"
10
11 namespace tracing {
12 namespace v2 {
13
14 ScatteredStreamWriter::ScatteredStreamWriter(Delegate* delegate)
15 : delegate_(delegate),
16 cur_range_({nullptr, nullptr}),
17 write_ptr_(nullptr) {}
18
19 ScatteredStreamWriter::~ScatteredStreamWriter() {}
20
21 void ScatteredStreamWriter::Reset(
22 ScatteredStreamWriter::ContiguousMemoryRange range) {
23 cur_range_ = range;
24 write_ptr_ = range.begin;
25 DCHECK_LT(write_ptr_, cur_range_.end);
26 }
27
28 void ScatteredStreamWriter::Expand() {
oystein (OOO til 10th of July) 2016/06/15 08:35:09 Maybe Extend() instead? Or Advance() to signal tha
Primiano Tucci (use gerrit) 2016/06/29 09:32:02 Right. Expand is the worst word given that nothing
29 Reset(delegate_->GetNewContiguousMemoryBuffer());
30 }
31
32 void ScatteredStreamWriter::WriteByte(uint8_t value) {
33 if (write_ptr_ >= cur_range_.end)
34 Expand();
35 *write_ptr_++ = value;
36 }
37
38 void ScatteredStreamWriter::WriteBytes(const uint8_t* src, size_t size) {
39 uint8_t* const end = const_cast<uint8_t*>(write_ptr_) + size;
40 if (end <= cur_range_.end) {
41 // Fast-path, the buffer fits into the current contiguous range.
42 // TODO(primiano): there is some potential perf optimization here, this is
43 // a tracing hot path. Looks like this is faster than memcpy for small
oystein (OOO til 10th of July) 2016/06/15 08:35:09 Huh interesting (and surprising). Any ideas what
alph 2016/06/15 10:08:22 Compiler should be able to handle this optimizatio
Primiano Tucci (use gerrit) 2016/06/29 09:32:02 Ok let's do something, I want to look with a fresh
44 // sizes. Should probably split this into WriteFewBytes() (for varints)
45 // and WriteManyBytes() (for strings).
46 for (size_t i = 0; i < size; ++i)
47 write_ptr_[i] = src[i];
48 write_ptr_ = end;
49 return;
50 } else {
51 // Slow path, scatter the writes.
52 size_t bytes_left = size;
53 while (bytes_left > 0) {
54 if (write_ptr_ >= cur_range_.end)
55 Expand();
56 const size_t burst_size = std::min(bytes_available(), bytes_left);
57 WriteBytes(src, burst_size);
58 bytes_left -= burst_size;
59 src += burst_size;
60 }
61 }
62 }
63
64 // TODO(primiano): perf optimization: I suspect that at the end this will always
65 // be called with size == 4, in which case we might jsut hardcode it. Ditto for
66 // WriteReservedBytes() below.
67 ScatteredStreamWriter::ContiguousMemoryRange
68 ScatteredStreamWriter::ReserveBytes(size_t size) {
69 if (write_ptr_ + size > cur_range_.end)
70 Expand();
71 uint8_t* begin = write_ptr_;
72 write_ptr_ += size;
alph 2016/06/15 10:08:22 Some bytes may be left at the end of the previous
Primiano Tucci (use gerrit) 2016/06/29 09:32:02 Yes, so, are you talking about the fact that the l
73 #ifndef NDEBUG
74 memset(begin, '\x42', size);
75 #endif
76 return {begin, begin + size};
77 }
78
79 void ScatteredStreamWriter::WriteReservedBytes(
80 const ScatteredStreamWriter::ContiguousMemoryRange& reserved_range,
81 const uint8_t* src) {
82 DCHECK_EQ(4, reserved_range.end - reserved_range.begin);
83 for(uint8_t* dst = reserved_range.begin; dst < reserved_range.end;)
84 *dst++ = *src++;
85 }
86
87 } // namespace v2
88 } // namespace tracing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698