OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/common/tracing_impl.h" | 5 #include "mojo/common/tracing_impl.h" |
6 | 6 |
7 #include "base/callback.h" | |
8 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
9 #include "base/files/file_util.h" | 8 #include "mojo/public/cpp/application/application_connection.h" |
| 9 #include "mojo/public/cpp/application/application_impl.h" |
10 | 10 |
11 namespace mojo { | 11 namespace mojo { |
12 namespace { | |
13 | 12 |
14 static bool g_tracing = false; | 13 // static |
15 static int g_blocks = 0; | 14 void TracingImpl::Create(ApplicationImpl* app) { |
16 static FILE* g_trace_file = NULL; | 15 new TracingImpl(app); |
17 | |
18 void WriteTraceDataCollected( | |
19 const scoped_refptr<base::RefCountedString>& events_str, | |
20 bool has_more_events) { | |
21 if (g_blocks) { | |
22 fwrite(",", 1, 1, g_trace_file); | |
23 } | |
24 ++g_blocks; | |
25 fwrite(events_str->data().c_str(), 1, events_str->data().length(), | |
26 g_trace_file); | |
27 if (!has_more_events) { | |
28 fwrite("]}", 1, 2, g_trace_file); | |
29 base::CloseFile(g_trace_file); | |
30 g_trace_file = NULL; | |
31 g_blocks = 0; | |
32 } | |
33 } | 16 } |
34 | 17 |
35 void StopTracingAndFlushToDisk(base::FilePath::StringType base_name) { | 18 // static |
36 base::debug::TraceLog::GetInstance()->SetDisabled(); | 19 void TracingImpl::Create(tracing::TraceDataCollectorPtr ptr) { |
37 | 20 new TracingImpl(ptr.Pass()); |
38 base_name.append(FILE_PATH_LITERAL(".trace")); | |
39 g_trace_file = base::OpenFile(base::FilePath(base_name), "w+"); | |
40 static const char start[] = "{\"traceEvents\":["; | |
41 fwrite(start, 1, strlen(start), g_trace_file); | |
42 base::debug::TraceLog::GetInstance()->Flush( | |
43 base::Bind(&WriteTraceDataCollected)); | |
44 } | |
45 } | 21 } |
46 | 22 |
47 TracingImpl::TracingImpl(InterfaceRequest<Tracing> request, | 23 TracingImpl::TracingImpl(ApplicationImpl* app) : binding_(this) { |
48 base::FilePath::StringType base_name) | 24 ApplicationConnection* connection = app->ConnectToApplication("mojo:tracing"); |
49 : base_name_(base_name), binding_(this, request.Pass()) { | 25 tracing::TraceDataCollectorPtr trace_data_collector_ptr; |
| 26 connection->ConnectToService(&trace_data_collector_ptr); |
| 27 binding_.Bind(trace_data_collector_ptr.PassMessagePipe()); |
| 28 } |
| 29 |
| 30 TracingImpl::TracingImpl(tracing::TraceDataCollectorPtr ptr) |
| 31 : binding_(this, ptr.PassMessagePipe()) { |
50 } | 32 } |
51 | 33 |
52 TracingImpl::~TracingImpl() { | 34 TracingImpl::~TracingImpl() { |
53 } | 35 } |
54 | 36 |
55 void TracingImpl::Start() { | 37 void TracingImpl::StartTracing(const String& categories) { |
56 if (g_tracing) | 38 std::string categories_str = categories.To<std::string>(); |
57 return; | |
58 g_tracing = true; | |
59 base::debug::TraceLog::GetInstance()->SetEnabled( | 39 base::debug::TraceLog::GetInstance()->SetEnabled( |
60 base::debug::CategoryFilter("*"), base::debug::TraceLog::RECORDING_MODE, | 40 base::debug::CategoryFilter(categories_str), |
| 41 base::debug::TraceLog::RECORDING_MODE, |
61 base::debug::TraceOptions(base::debug::RECORD_UNTIL_FULL)); | 42 base::debug::TraceOptions(base::debug::RECORD_UNTIL_FULL)); |
62 } | 43 } |
63 | 44 |
64 void TracingImpl::Stop() { | 45 void TracingImpl::StopTracing() { |
65 if (!g_tracing) | 46 base::debug::TraceLog::GetInstance()->SetDisabled(); |
66 return; | 47 |
67 g_tracing = false; | 48 base::debug::TraceLog::GetInstance()->Flush( |
68 StopTracingAndFlushToDisk(base_name_); | 49 base::Bind(&TracingImpl::SendChunk, base::Unretained(this))); |
| 50 } |
| 51 |
| 52 void TracingImpl::SendChunk( |
| 53 const scoped_refptr<base::RefCountedString>& events_str, |
| 54 bool has_more_events) { |
| 55 binding_.client()->DataCollected(mojo::String(events_str->data())); |
| 56 if (!has_more_events) { |
| 57 binding_.client()->EndTracing(); |
| 58 } |
69 } | 59 } |
70 | 60 |
71 } // namespace mojo | 61 } // namespace mojo |
OLD | NEW |