Index: base/trace_event/trace_event_impl.cc |
diff --git a/base/trace_event/trace_event_impl.cc b/base/trace_event/trace_event_impl.cc |
index 95cf06d5a576c061eda861c74f923de6c50b9eef..763ff464b41ac410ef27e2997add4b850c370aa5 100644 |
--- a/base/trace_event/trace_event_impl.cc |
+++ b/base/trace_event/trace_event_impl.cc |
@@ -30,6 +30,7 @@ |
#include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
#include "base/threading/platform_thread.h" |
#include "base/threading/thread_id_name_manager.h" |
+#include "base/threading/worker_pool.h" |
#include "base/time/time.h" |
#include "base/trace_event/trace_event.h" |
#include "base/trace_event/trace_event_synthetic_delay.h" |
@@ -73,7 +74,7 @@ const size_t kTraceEventVectorBigBufferChunks = |
512000000 / kTraceBufferChunkSize; |
const size_t kTraceEventVectorBufferChunks = 256000 / kTraceBufferChunkSize; |
const size_t kTraceEventRingBufferChunks = kTraceEventVectorBufferChunks / 4; |
-const size_t kTraceEventBatchChunks = 1000 / kTraceBufferChunkSize; |
+const size_t kTraceEventBufferSizeInBytes = 10 * 1024 * 1024; |
// Can store results for 30 seconds with 1 ms sampling interval. |
const size_t kMonitorTraceEventBufferChunks = 30000 / kTraceBufferChunkSize; |
// ECHO_TO_CONSOLE needs a small buffer to hold the unfinished COMPLETE events. |
@@ -1211,7 +1212,8 @@ TraceLog::TraceLog() |
event_callback_category_filter_( |
CategoryFilter::kDefaultCategoryFilterString), |
thread_shared_chunk_index_(0), |
- generation_(0) { |
+ generation_(0), |
+ use_worker_thread_(false) { |
// Trace is enabled or disabled on one thread while other threads are |
// accessing the enabled flag. We don't care whether edge-case events are |
// traced or not, so we allow races on the enabled flag to keep the trace |
@@ -1684,7 +1686,9 @@ void TraceLog::SetEventCallbackDisabled() { |
// - The message loop will be removed from thread_message_loops_; |
// If this is the last message loop, finish the flush; |
// 4. If any thread hasn't finish its flush in time, finish the flush. |
-void TraceLog::Flush(const TraceLog::OutputCallback& cb) { |
+void TraceLog::Flush(const TraceLog::OutputCallback& cb, |
+ bool use_worker_thread) { |
+ use_worker_thread_ = use_worker_thread; |
if (IsEnabled()) { |
// Can't flush when tracing is enabled because otherwise PostTask would |
// - generate more trace events; |
@@ -1738,6 +1742,7 @@ void TraceLog::Flush(const TraceLog::OutputCallback& cb) { |
FinishFlush(generation); |
} |
+// Usually it runs on a different thread. |
void TraceLog::ConvertTraceEventsToTraceFormat( |
scoped_ptr<TraceBuffer> logged_events, |
const TraceLog::OutputCallback& flush_output_callback) { |
@@ -1752,19 +1757,17 @@ void TraceLog::ConvertTraceEventsToTraceFormat( |
scoped_refptr<RefCountedString> json_events_str_ptr = |
new RefCountedString(); |
- for (size_t i = 0; i < kTraceEventBatchChunks; ++i) { |
+ while (json_events_str_ptr->size() < kTraceEventBufferSizeInBytes) { |
const TraceBufferChunk* chunk = logged_events->NextChunk(); |
- if (!chunk) { |
- has_more_events = false; |
+ has_more_events = chunk != NULL; |
+ if (!chunk) |
break; |
- } |
for (size_t j = 0; j < chunk->size(); ++j) { |
- if (i > 0 || j > 0) |
+ if (json_events_str_ptr->size()) |
json_events_str_ptr->data().append(",\n"); |
chunk->GetEventAt(j)->AppendAsJSON(&(json_events_str_ptr->data())); |
} |
} |
- |
flush_output_callback.Run(json_events_str_ptr, has_more_events); |
} while (has_more_events); |
} |
@@ -1788,6 +1791,16 @@ void TraceLog::FinishFlush(int generation) { |
flush_output_callback_.Reset(); |
} |
+ if (use_worker_thread_ && |
+ WorkerPool::PostTask( |
+ FROM_HERE, |
+ Bind(&TraceLog::ConvertTraceEventsToTraceFormat, |
+ Passed(&previous_logged_events), |
+ flush_output_callback), |
+ true)) { |
+ return; |
+ } |
+ |
ConvertTraceEventsToTraceFormat(previous_logged_events.Pass(), |
flush_output_callback); |
} |