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