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 #include "base/lazy_instance.h" | 5 #include "base/lazy_instance.h" |
6 #include "base/memory/singleton.h" | 6 #include "base/memory/singleton.h" |
7 #include "base/trace_event/trace_event_impl.h" | 7 #include "base/trace_event/trace_event_impl.h" |
8 #include "content/browser/tracing/battor_power_trace_provider.h" | 8 #include "content/browser/tracing/battor_power_trace_provider.h" |
9 #include "content/browser/tracing/power_tracing_agent.h" | 9 #include "content/browser/tracing/power_tracing_agent.h" |
10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
11 | 11 |
12 namespace content { | 12 namespace content { |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 const char kPowerTracingAgentName[] = "battor"; | 16 const char kPowerTracingAgentName[] = "battor"; |
17 const char kPowerTraceLabel[] = "powerTraceAsString"; | 17 const char kPowerTraceLabel[] = "powerTraceAsString"; |
18 | 18 |
19 } // namespace | 19 } // namespace |
20 | 20 |
21 // static | 21 // static |
22 PowerTracingAgent* PowerTracingAgent::GetInstance() { | 22 PowerTracingAgent* PowerTracingAgent::GetInstance() { |
23 return base::Singleton<PowerTracingAgent>::get(); | 23 return base::Singleton<PowerTracingAgent>::get(); |
24 } | 24 } |
25 | 25 |
26 PowerTracingAgent::PowerTracingAgent() : is_tracing_(false) { | 26 PowerTracingAgent::PowerTracingAgent() : thread_("PowerTracingAgentThread") { |
27 battor_trace_provider_.reset(new BattorPowerTraceProvider()); | 27 battor_trace_provider_.reset(new BattorPowerTraceProvider()); |
28 } | 28 } |
29 | 29 |
30 PowerTracingAgent::~PowerTracingAgent() {} | 30 PowerTracingAgent::~PowerTracingAgent() {} |
31 | 31 |
32 std::string PowerTracingAgent::GetTracingAgentName() { | 32 std::string PowerTracingAgent::GetTracingAgentName() { |
33 return kPowerTracingAgentName; | 33 return kPowerTracingAgentName; |
34 } | 34 } |
35 | 35 |
36 std::string PowerTracingAgent::GetTraceEventLabel() { | 36 std::string PowerTracingAgent::GetTraceEventLabel() { |
37 return kPowerTraceLabel; | 37 return kPowerTraceLabel; |
38 } | 38 } |
39 | 39 |
40 bool PowerTracingAgent::StartAgentTracing( | 40 bool PowerTracingAgent::StartAgentTracing( |
41 const base::trace_event::TraceConfig& trace_config) { | 41 const base::trace_event::TraceConfig& trace_config) { |
42 // Tracing session already in progress. | 42 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
43 if (is_tracing_) | 43 |
| 44 // TODO(charliea): When system tracing is enabled in about://tracing, it will |
| 45 // trigger power tracing. We need a way of checking if BattOr is connected. |
| 46 // Currently, IsConnected() always returns false, so that we do not include |
| 47 // BattOr trace until it is hooked up. |
| 48 if (!battor_trace_provider_->IsConnected()) |
44 return false; | 49 return false; |
45 | 50 |
46 // TODO(prabhur) Start tracing probably needs to be done in a | 51 thread_.Start(); |
47 // separate thread since it involves talking to the h/w. | 52 |
48 // Revisit once battor h/w communication is enabled. | 53 thread_.task_runner()->PostTask( |
49 is_tracing_ = battor_trace_provider_->StartTracing(); | 54 FROM_HERE, |
50 return is_tracing_; | 55 base::Bind(&PowerTracingAgent::TraceOnThread, base::Unretained(this))); |
| 56 return true; |
51 } | 57 } |
52 | 58 |
53 void PowerTracingAgent::StopAgentTracing( | 59 void PowerTracingAgent::StopAgentTracing( |
54 const StopAgentTracingCallback& callback) { | 60 const StopAgentTracingCallback& callback) { |
55 // No tracing session in progress. | 61 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
56 if (!is_tracing_) | 62 DCHECK(thread_.IsRunning()); |
57 return; | |
58 | 63 |
59 // Stop tracing & collect logs. | 64 thread_.task_runner()->PostTask( |
60 BrowserThread::PostTask( | 65 FROM_HERE, |
61 BrowserThread::UI, FROM_HERE, | 66 base::Bind(&PowerTracingAgent::FlushOnThread, base::Unretained(this), |
62 base::Bind(&PowerTracingAgent::FlushOnThread, | |
63 base::Unretained(this), | |
64 callback)); | 67 callback)); |
65 } | 68 } |
66 | 69 |
67 void PowerTracingAgent::OnStopTracingDone( | 70 void PowerTracingAgent::OnStopTracingDone( |
68 const StopAgentTracingCallback& callback, | 71 const StopAgentTracingCallback& callback, |
69 const scoped_refptr<base::RefCountedString>& result) { | 72 const scoped_refptr<base::RefCountedString>& result) { |
70 is_tracing_ = false; | 73 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 74 |
71 // Pass the serialized events. | 75 // Pass the serialized events. |
72 callback.Run(GetTracingAgentName(), GetTraceEventLabel(), result); | 76 callback.Run(GetTracingAgentName(), GetTraceEventLabel(), result); |
| 77 |
| 78 // Stop the power tracing agent thread on file thread. |
| 79 BrowserThread::PostTask( |
| 80 BrowserThread::FILE, FROM_HERE, |
| 81 base::Bind(&base::Thread::Stop, base::Unretained(&thread_))); |
| 82 } |
| 83 |
| 84 void PowerTracingAgent::TraceOnThread() { |
| 85 DCHECK(thread_.task_runner()->BelongsToCurrentThread()); |
| 86 battor_trace_provider_->StartTracing(); |
73 } | 87 } |
74 | 88 |
75 void PowerTracingAgent::FlushOnThread( | 89 void PowerTracingAgent::FlushOnThread( |
76 const StopAgentTracingCallback& callback) { | 90 const StopAgentTracingCallback& callback) { |
77 // Pass the result to the UI Thread. | 91 DCHECK(thread_.task_runner()->BelongsToCurrentThread()); |
78 | 92 |
79 // TODO(prabhur) StopTracing & GetLog need to be called on a | |
80 // separate thread depending on how BattorPowerTraceProvider is implemented. | |
81 battor_trace_provider_->StopTracing(); | 93 battor_trace_provider_->StopTracing(); |
82 std::string battor_logs; | 94 std::string battor_logs; |
83 battor_trace_provider_->GetLog(&battor_logs); | 95 battor_trace_provider_->GetLog(&battor_logs); |
84 scoped_refptr<base::RefCountedString> result = | 96 scoped_refptr<base::RefCountedString> result = |
85 base::RefCountedString::TakeString(&battor_logs); | 97 base::RefCountedString::TakeString(&battor_logs); |
86 BrowserThread::PostTask( | 98 BrowserThread::PostTask( |
87 BrowserThread::UI, FROM_HERE, | 99 BrowserThread::UI, FROM_HERE, |
88 base::Bind(&PowerTracingAgent::OnStopTracingDone, | 100 base::Bind(&PowerTracingAgent::OnStopTracingDone, |
89 base::Unretained(this), | 101 base::Unretained(this), |
90 callback, | 102 callback, |
91 result)); | 103 result)); |
92 } | 104 } |
93 | 105 |
94 bool PowerTracingAgent::SupportsExplicitClockSync() { | 106 bool PowerTracingAgent::SupportsExplicitClockSync() { |
95 // TODO(zhenw): return true after implementing explicit clock sync. | 107 return true; |
96 return false; | |
97 } | 108 } |
98 | 109 |
99 void PowerTracingAgent::RecordClockSyncMarker( | 110 void PowerTracingAgent::RecordClockSyncMarker( |
100 int sync_id, | 111 int sync_id, |
101 const RecordClockSyncMarkerCallback& callback) { | 112 const RecordClockSyncMarkerCallback& callback) { |
| 113 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
102 DCHECK(SupportsExplicitClockSync()); | 114 DCHECK(SupportsExplicitClockSync()); |
103 // TODO(zhenw): implement explicit clock sync. | 115 |
| 116 thread_.task_runner()->PostTask( |
| 117 FROM_HERE, |
| 118 base::Bind(&PowerTracingAgent::RecordClockSyncMarkerOnThread, |
| 119 base::Unretained(this), |
| 120 sync_id, |
| 121 callback)); |
| 122 } |
| 123 |
| 124 void PowerTracingAgent::RecordClockSyncMarkerOnThread( |
| 125 int sync_id, |
| 126 const RecordClockSyncMarkerCallback& callback) { |
| 127 DCHECK(thread_.task_runner()->BelongsToCurrentThread()); |
| 128 DCHECK(SupportsExplicitClockSync()); |
| 129 |
| 130 base::TimeTicks issue_ts = base::TimeTicks::Now(); |
| 131 battor_trace_provider_->RecordClockSyncMarker(sync_id); |
| 132 base::TimeTicks issue_end_ts = base::TimeTicks::Now(); |
| 133 |
| 134 BrowserThread::PostTask( |
| 135 BrowserThread::UI, FROM_HERE, |
| 136 base::Bind(callback, sync_id, issue_ts, issue_end_ts)); |
104 } | 137 } |
105 | 138 |
106 } // namespace content | 139 } // namespace content |
OLD | NEW |