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 thread_.Start(); | |
oystein (OOO til 10th of July)
2015/12/16 02:10:05
I'm not sure if the lifespan of the thread should
Zhen Wang
2015/12/16 23:49:12
If stopping the thread when we end tracing, thread
| |
28 } | 29 } |
29 | 30 |
30 PowerTracingAgent::~PowerTracingAgent() {} | 31 PowerTracingAgent::~PowerTracingAgent() {} |
31 | 32 |
32 std::string PowerTracingAgent::GetTracingAgentName() { | 33 std::string PowerTracingAgent::GetTracingAgentName() { |
33 return kPowerTracingAgentName; | 34 return kPowerTracingAgentName; |
34 } | 35 } |
35 | 36 |
36 std::string PowerTracingAgent::GetTraceEventLabel() { | 37 std::string PowerTracingAgent::GetTraceEventLabel() { |
37 return kPowerTraceLabel; | 38 return kPowerTraceLabel; |
38 } | 39 } |
39 | 40 |
40 bool PowerTracingAgent::StartAgentTracing( | 41 bool PowerTracingAgent::StartAgentTracing( |
41 const base::trace_event::TraceConfig& trace_config) { | 42 const base::trace_event::TraceConfig& trace_config) { |
42 // Tracing session already in progress. | 43 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
43 if (is_tracing_) | 44 DCHECK(thread_.IsRunning()); |
44 return false; | |
45 | 45 |
46 // TODO(prabhur) Start tracing probably needs to be done in a | 46 thread_.task_runner()->PostTask( |
47 // separate thread since it involves talking to the h/w. | 47 FROM_HERE, |
48 // Revisit once battor h/w communication is enabled. | 48 base::Bind(&PowerTracingAgent::TraceOnThread, base::Unretained(this))); |
49 is_tracing_ = battor_trace_provider_->StartTracing(); | 49 return true; |
50 return is_tracing_; | |
51 } | 50 } |
52 | 51 |
53 void PowerTracingAgent::StopAgentTracing( | 52 void PowerTracingAgent::StopAgentTracing( |
54 const StopAgentTracingCallback& callback) { | 53 const StopAgentTracingCallback& callback) { |
55 // No tracing session in progress. | 54 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
56 if (!is_tracing_) | 55 DCHECK(thread_.IsRunning()); |
57 return; | |
58 | 56 |
59 // Stop tracing & collect logs. | 57 thread_.task_runner()->PostTask( |
60 BrowserThread::PostTask( | 58 FROM_HERE, |
61 BrowserThread::UI, FROM_HERE, | 59 base::Bind(&PowerTracingAgent::FlushOnThread, base::Unretained(this), |
62 base::Bind(&PowerTracingAgent::FlushOnThread, | |
63 base::Unretained(this), | |
64 callback)); | 60 callback)); |
65 } | 61 } |
66 | 62 |
67 void PowerTracingAgent::OnStopTracingDone( | 63 void PowerTracingAgent::OnStopTracingDone( |
68 const StopAgentTracingCallback& callback, | 64 const StopAgentTracingCallback& callback, |
69 const scoped_refptr<base::RefCountedString>& result) { | 65 const scoped_refptr<base::RefCountedString>& result) { |
70 is_tracing_ = false; | 66 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
67 | |
71 // Pass the serialized events. | 68 // Pass the serialized events. |
72 callback.Run(GetTracingAgentName(), GetTraceEventLabel(), result); | 69 callback.Run(GetTracingAgentName(), GetTraceEventLabel(), result); |
73 } | 70 } |
74 | 71 |
72 void PowerTracingAgent::TraceOnThread() { | |
73 DCHECK(thread_.task_runner()->BelongsToCurrentThread()); | |
74 battor_trace_provider_->StartTracing(); | |
75 } | |
76 | |
75 void PowerTracingAgent::FlushOnThread( | 77 void PowerTracingAgent::FlushOnThread( |
76 const StopAgentTracingCallback& callback) { | 78 const StopAgentTracingCallback& callback) { |
77 // Pass the result to the UI Thread. | 79 DCHECK(thread_.task_runner()->BelongsToCurrentThread()); |
78 | 80 |
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(); | 81 battor_trace_provider_->StopTracing(); |
82 std::string battor_logs; | 82 std::string battor_logs; |
83 battor_trace_provider_->GetLog(&battor_logs); | 83 battor_trace_provider_->GetLog(&battor_logs); |
84 scoped_refptr<base::RefCountedString> result = | 84 scoped_refptr<base::RefCountedString> result = |
85 base::RefCountedString::TakeString(&battor_logs); | 85 base::RefCountedString::TakeString(&battor_logs); |
86 BrowserThread::PostTask( | 86 BrowserThread::PostTask( |
87 BrowserThread::UI, FROM_HERE, | 87 BrowserThread::UI, FROM_HERE, |
88 base::Bind(&PowerTracingAgent::OnStopTracingDone, | 88 base::Bind(&PowerTracingAgent::OnStopTracingDone, |
89 base::Unretained(this), | 89 base::Unretained(this), |
90 callback, | 90 callback, |
91 result)); | 91 result)); |
92 } | 92 } |
93 | 93 |
94 bool PowerTracingAgent::SupportsExplicitClockSync() { | 94 bool PowerTracingAgent::SupportsExplicitClockSync() { |
95 // TODO(zhenw): return true after implementing explicit clock sync. | 95 return true; |
96 return false; | |
97 } | 96 } |
98 | 97 |
99 void PowerTracingAgent::RecordClockSyncMarker( | 98 void PowerTracingAgent::RecordClockSyncMarker( |
100 int sync_id, | 99 int sync_id, |
101 const RecordClockSyncMarkerCallback& callback) { | 100 const RecordClockSyncMarkerCallback& callback) { |
101 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
102 DCHECK(SupportsExplicitClockSync()); | 102 DCHECK(SupportsExplicitClockSync()); |
103 // TODO(zhenw): implement explicit clock sync. | 103 |
104 thread_.task_runner()->PostTask( | |
105 FROM_HERE, | |
106 base::Bind(&PowerTracingAgent::RecordClockSyncMarkerOnThread, | |
107 base::Unretained(this), | |
108 sync_id, | |
109 callback)); | |
110 } | |
111 | |
112 void PowerTracingAgent::RecordClockSyncMarkerOnThread( | |
113 int sync_id, | |
114 const RecordClockSyncMarkerCallback& callback) { | |
115 DCHECK(thread_.task_runner()->BelongsToCurrentThread()); | |
116 DCHECK(SupportsExplicitClockSync()); | |
117 | |
118 base::TimeTicks issue_ts = base::TimeTicks::Now(); | |
119 battor_trace_provider_->RecordClockSyncMarker(sync_id); | |
120 base::TimeTicks issue_end_ts = base::TimeTicks::Now(); | |
121 | |
122 BrowserThread::PostTask( | |
123 BrowserThread::UI, FROM_HERE, | |
124 base::Bind(callback, sync_id, issue_ts, issue_end_ts)); | |
104 } | 125 } |
105 | 126 |
106 } // namespace content | 127 } // namespace content |
OLD | NEW |