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

Unified Diff: base/trace_event/thread_local_event_buffer.cc

Issue 2503473002: tracing: split out ThreadLocalEventBuffer
Patch Set: Created 4 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/trace_event/thread_local_event_buffer.h ('k') | base/trace_event/trace_buffer.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/trace_event/thread_local_event_buffer.cc
diff --git a/base/trace_event/thread_local_event_buffer.cc b/base/trace_event/thread_local_event_buffer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..502b27c1ab65c21891e1bfe662b2d41c5122e69c
--- /dev/null
+++ b/base/trace_event/thread_local_event_buffer.cc
@@ -0,0 +1,60 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/trace_event/thread_local_event_buffer.h"
+
+#include "base/trace_event/trace_buffer.h"
+#include "base/trace_event/trace_event_impl.h"
+#include "base/trace_event/trace_log.h"
+
+namespace base {
+namespace trace_event {
+
+ThreadLocalEventBuffer::ThreadLocalEventBuffer(int generation)
+ : chunk_index_(0), generation_(generation) {}
+
+ThreadLocalEventBuffer::~ThreadLocalEventBuffer() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ // The instance must have been flushed before destruction. Otherwise the
+ // owned chunk will be lost forever.
+ DCHECK(!chunk_);
+}
+
+TraceEvent* ThreadLocalEventBuffer::AddTraceEvent(TraceEventHandle* handle) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ if (chunk_ && chunk_->IsFull())
+ Flush(); // |chunk_| will be empty and evaluate to false after this call.
+
+ if (!chunk_) {
+ chunk_ = TraceLog::GetInstance()->TakeChunk(&chunk_index_);
+ if (!chunk_)
+ return NULL;
+ }
+
+ size_t event_index;
+ TraceEvent* trace_event = chunk_->AddTraceEvent(&event_index);
+ DCHECK(trace_event);
+ if (handle)
+ chunk_->MakeHandle(chunk_index_, event_index, handle);
+ return trace_event;
+}
+
+TraceEvent* ThreadLocalEventBuffer::GetEventByHandle(TraceEventHandle handle) {
+ if (!chunk_ || handle.chunk_seq != chunk_->seq() ||
+ handle.chunk_index != chunk_index_) {
+ return nullptr;
+ }
+ return chunk_->GetEventAt(handle.event_index);
+}
+
+void ThreadLocalEventBuffer::Flush() {
+ if (!chunk_)
+ return;
+ TraceLog::GetInstance()->ReturnChunk(std::move(chunk_), generation_,
+ chunk_index_);
+}
+
+} // namespace trace_event
+} // namespace base
« no previous file with comments | « base/trace_event/thread_local_event_buffer.h ('k') | base/trace_event/trace_buffer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698