OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #ifndef SHELL_TRACER_H_ | 5 #ifndef SHELL_TRACER_H_ |
6 #define SHELL_TRACER_H_ | 6 #define SHELL_TRACER_H_ |
7 | 7 |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 | 9 |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ref_counted_memory.h" | 14 #include "base/memory/ref_counted_memory.h" |
15 #include "mojo/common/data_pipe_drainer.h" | 15 #include "mojo/common/data_pipe_drainer.h" |
16 #include "mojo/common/trace_controller_impl.h" | 16 #include "mojo/common/trace_provider_impl.h" |
17 #include "mojo/services/tracing/public/interfaces/tracing.mojom.h" | 17 #include "mojo/services/tracing/public/interfaces/tracing.mojom.h" |
18 | 18 |
19 namespace shell { | 19 namespace shell { |
20 | 20 |
21 // Tracer collects tracing data from base/trace_event and from externally | 21 // Tracer collects tracing data from base/trace_event and from externally |
22 // configured sources, aggregates it into a single stream, and writes it out to | 22 // configured sources, aggregates it into a single stream, and writes it out to |
23 // a file. It should be constructed very early in a process' lifetime before any | 23 // a file. It should be constructed very early in a process' lifetime before any |
24 // initialization that may be interesting to trace has occured and be shut down | 24 // initialization that may be interesting to trace has occured and be shut down |
25 // as late as possible to capture as much initialization/shutdown code as | 25 // as late as possible to capture as much initialization/shutdown code as |
26 // possible. | 26 // possible. |
27 class Tracer : public mojo::common::DataPipeDrainer::Client { | 27 class Tracer : public mojo::common::DataPipeDrainer::Client { |
28 public: | 28 public: |
29 Tracer(); | 29 Tracer(); |
30 ~Tracer() override; | 30 ~Tracer() override; |
31 | 31 |
32 // Starts tracing the current process with the given set of categories. The | 32 // Starts tracing the current process with the given set of categories. The |
33 // tracing results will be saved into the specified filename when | 33 // tracing results will be saved into the specified filename when |
34 // StopAndFlushToFile() is called. | 34 // StopAndFlushToFile() is called. |
35 void Start(const std::string& categories, | 35 void Start(const std::string& categories, |
36 const std::string& duration_seconds_str, | 36 const std::string& duration_seconds_str, |
37 const std::string& filename); | 37 const std::string& filename); |
38 | 38 |
39 // Notifies the tracer that a message loop has been created. If startup | 39 // Notifies the tracer that a message loop has been created. If startup |
40 // tracing is active the tracer can use this to schedule when to stop tracing. | 40 // tracing is active the tracer can use this to schedule when to stop tracing. |
41 void DidCreateMessageLoop(); | 41 void DidCreateMessageLoop(); |
42 | 42 |
43 // Starts collecting data from the tracing service with the given set of | 43 // Starts collecting data from the tracing service with the given set of |
44 // categories. | 44 // categories. |
45 void StartCollectingFromTracingService( | 45 void StartCollectingFromTracingService( |
46 tracing::TraceCoordinatorPtr coordinator); | 46 tracing::TraceCollectorPtr coordinator); |
47 | 47 |
48 // Stops tracing and flushes all collected trace data to the file specified in | 48 // Stops tracing and flushes all collected trace data to the file specified in |
49 // Start(). Blocks until the file write is complete. May be called after the | 49 // Start(). Blocks until the file write is complete. May be called after the |
50 // message loop is shut down. | 50 // message loop is shut down. |
51 void StopAndFlushToFile(); | 51 void StopAndFlushToFile(); |
52 | 52 |
53 void ConnectToController( | 53 void ConnectToProvider( |
54 mojo::InterfaceRequest<tracing::TraceController> request); | 54 mojo::InterfaceRequest<tracing::TraceProvider> request); |
55 | 55 |
56 private: | 56 private: |
57 void StopTracingAndFlushToDisk(); | 57 void StopTracingAndFlushToDisk(); |
58 | 58 |
59 // Called from the flush thread. When all data is collected this runs | 59 // Called from the flush thread. When all data is collected this runs |
60 // |done_callback| on the flush thread. | 60 // |done_callback| on the flush thread. |
61 void EndTraceAndFlush(const std::string& filename, | 61 void EndTraceAndFlush(const std::string& filename, |
62 const base::Closure& done_callback); | 62 const base::Closure& done_callback); |
63 | 63 |
64 // Called from the flush thread. | 64 // Called from the flush thread. |
65 void WriteTraceDataCollected( | 65 void WriteTraceDataCollected( |
66 const base::Closure& done_callback, | 66 const base::Closure& done_callback, |
67 const scoped_refptr<base::RefCountedString>& events_str, | 67 const scoped_refptr<base::RefCountedString>& events_str, |
68 bool has_more_events); | 68 bool has_more_events); |
69 | 69 |
70 // mojo::common::DataPipeDrainer::Client implementation. | 70 // mojo::common::DataPipeDrainer::Client implementation. |
71 void OnDataAvailable(const void* data, size_t num_bytes) override; | 71 void OnDataAvailable(const void* data, size_t num_bytes) override; |
72 void OnDataComplete() override; | 72 void OnDataComplete() override; |
73 | 73 |
74 // Emits a comma if needed. | 74 // Emits a comma if needed. |
75 void WriteCommaIfNeeded(); | 75 void WriteCommaIfNeeded(); |
76 | 76 |
77 // Writes trace file footer and closes out the file. | 77 // Writes trace file footer and closes out the file. |
78 void WriteFooterAndClose(); | 78 void WriteFooterAndClose(); |
79 | 79 |
80 // Set when connected to the tracing service. | 80 // Set when connected to the tracing service. |
81 tracing::TraceCoordinatorPtr coordinator_; | 81 tracing::TraceCollectorPtr coordinator_; |
82 scoped_ptr<mojo::common::DataPipeDrainer> drainer_; | 82 scoped_ptr<mojo::common::DataPipeDrainer> drainer_; |
83 | 83 |
84 // Whether we're currently tracing. | 84 // Whether we're currently tracing. |
85 bool tracing_; | 85 bool tracing_; |
86 // How long to trace after message loop creation. | 86 // How long to trace after message loop creation. |
87 int trace_duration_secs_; | 87 int trace_duration_secs_; |
88 // Categories to trace. | 88 // Categories to trace. |
89 std::string categories_; | 89 std::string categories_; |
90 | 90 |
91 // Whether we've written the first chunk. | 91 // Whether we've written the first chunk. |
92 bool first_chunk_written_; | 92 bool first_chunk_written_; |
93 std::string trace_service_data_; | 93 std::string trace_service_data_; |
94 | 94 |
95 // Trace file, if open. | 95 // Trace file, if open. |
96 FILE* trace_file_; | 96 FILE* trace_file_; |
97 std::string trace_filename_; | 97 std::string trace_filename_; |
98 | 98 |
99 DISALLOW_COPY_AND_ASSIGN(Tracer); | 99 DISALLOW_COPY_AND_ASSIGN(Tracer); |
100 }; | 100 }; |
101 | 101 |
102 } // namespace shell | 102 } // namespace shell |
103 | 103 |
104 #endif // SHELL_TRACER_H_ | 104 #endif // SHELL_TRACER_H_ |
OLD | NEW |