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 "sky/viewer/services/tracing_impl.h" | 5 #include "mojo/common/tracing_impl.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 | 10 |
11 namespace sky { | 11 namespace mojo { |
12 namespace { | 12 namespace { |
13 | 13 |
14 static bool g_tracing = false; | 14 static bool g_tracing = false; |
15 static int g_blocks = 0; | 15 static int g_blocks = 0; |
16 static FILE* g_trace_file = NULL; | 16 static FILE* g_trace_file = NULL; |
17 | 17 |
18 void WriteTraceDataCollected( | 18 void WriteTraceDataCollected( |
19 const scoped_refptr<base::RefCountedString>& events_str, | 19 const scoped_refptr<base::RefCountedString>& events_str, |
20 bool has_more_events) { | 20 bool has_more_events) { |
21 if (g_blocks) { | 21 if (g_blocks) { |
22 fwrite(",", 1, 1, g_trace_file); | 22 fwrite(",", 1, 1, g_trace_file); |
23 } | 23 } |
24 ++g_blocks; | 24 ++g_blocks; |
25 fwrite( | 25 fwrite(events_str->data().c_str(), 1, events_str->data().length(), |
26 events_str->data().c_str(), 1, events_str->data().length(), g_trace_file); | 26 g_trace_file); |
27 if (!has_more_events) { | 27 if (!has_more_events) { |
28 fwrite("]}", 1, 2, g_trace_file); | 28 fwrite("]}", 1, 2, g_trace_file); |
29 base::CloseFile(g_trace_file); | 29 base::CloseFile(g_trace_file); |
30 g_trace_file = NULL; | 30 g_trace_file = NULL; |
31 g_blocks = 0; | 31 g_blocks = 0; |
32 } | 32 } |
33 } | 33 } |
34 | 34 |
35 void StopTracingAndFlushToDisk() { | 35 void StopTracingAndFlushToDisk(base::FilePath::StringType base_name) { |
36 base::debug::TraceLog::GetInstance()->SetDisabled(); | 36 base::debug::TraceLog::GetInstance()->SetDisabled(); |
37 | 37 |
38 g_trace_file = base::OpenFile( | 38 base_name.append(FILE_PATH_LITERAL(".trace")); |
39 base::FilePath(FILE_PATH_LITERAL("sky_viewer.trace")), "w+"); | 39 g_trace_file = base::OpenFile(base::FilePath(base_name), "w+"); |
40 static const char start[] = "{\"traceEvents\":["; | 40 static const char start[] = "{\"traceEvents\":["; |
41 fwrite(start, 1, strlen(start), g_trace_file); | 41 fwrite(start, 1, strlen(start), g_trace_file); |
42 base::debug::TraceLog::GetInstance()->Flush( | 42 base::debug::TraceLog::GetInstance()->Flush( |
43 base::Bind(&WriteTraceDataCollected)); | 43 base::Bind(&WriteTraceDataCollected)); |
44 } | 44 } |
45 | |
46 } | 45 } |
47 | 46 |
48 TracingImpl::TracingImpl() { | 47 TracingImpl::TracingImpl(InterfaceRequest<Tracing> request, |
| 48 base::FilePath::StringType base_name) |
| 49 : base_name_(base_name), binding_(this, request.Pass()) { |
49 } | 50 } |
50 | 51 |
51 TracingImpl::~TracingImpl() { | 52 TracingImpl::~TracingImpl() { |
52 } | 53 } |
53 | 54 |
54 void TracingImpl::Start() { | 55 void TracingImpl::Start() { |
55 if (g_tracing) | 56 if (g_tracing) |
56 return; | 57 return; |
57 g_tracing = true; | 58 g_tracing = true; |
58 base::debug::TraceLog::GetInstance()->SetEnabled( | 59 base::debug::TraceLog::GetInstance()->SetEnabled( |
59 base::debug::CategoryFilter("*"), | 60 base::debug::CategoryFilter("*"), base::debug::TraceLog::RECORDING_MODE, |
60 base::debug::TraceLog::RECORDING_MODE, | |
61 base::debug::TraceOptions(base::debug::RECORD_UNTIL_FULL)); | 61 base::debug::TraceOptions(base::debug::RECORD_UNTIL_FULL)); |
62 } | 62 } |
63 | 63 |
64 void TracingImpl::Stop() { | 64 void TracingImpl::Stop() { |
65 if (!g_tracing) | 65 if (!g_tracing) |
66 return; | 66 return; |
67 g_tracing = false; | 67 g_tracing = false; |
68 StopTracingAndFlushToDisk(); | 68 StopTracingAndFlushToDisk(base_name_); |
69 } | 69 } |
70 | 70 |
71 } // namespace sky | 71 } // namespace mojo |
OLD | NEW |