OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "gpu/command_buffer/service/gpu_tracer.h" | 5 #include "gpu/command_buffer/service/gpu_tracer.h" |
6 | 6 |
7 #include <deque> | 7 #include <deque> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/debug/trace_event.h" | 10 #include "base/debug/trace_event.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 start_time); | 52 start_time); |
53 TRACE_EVENT_COPY_END_WITH_ID_TID_AND_TIMESTAMP0( | 53 TRACE_EVENT_COPY_END_WITH_ID_TID_AND_TIMESTAMP0( |
54 TRACE_DISABLED_BY_DEFAULT("gpu.device"), | 54 TRACE_DISABLED_BY_DEFAULT("gpu.device"), |
55 name.c_str(), | 55 name.c_str(), |
56 local_trace_id_, | 56 local_trace_id_, |
57 named_thread_.thread_id(), | 57 named_thread_.thread_id(), |
58 end_time); | 58 end_time); |
59 ++local_trace_id_; | 59 ++local_trace_id_; |
60 } | 60 } |
61 | 61 |
62 GPUTrace::GPUTrace(const std::string& name) | |
63 : name_(name), | |
64 outputter_(NULL), | |
65 offset_(0), | |
66 end_time_(0), | |
67 end_requested_(false), | |
68 enabled_(false) { | |
69 } | |
70 | |
71 GPUTrace::GPUTrace(scoped_refptr<Outputter> outputter, | 62 GPUTrace::GPUTrace(scoped_refptr<Outputter> outputter, |
72 const std::string& name, | 63 const std::string& name, |
73 int64 offset) | 64 int64 offset, |
| 65 GpuTracerType tracer_type) |
74 : name_(name), | 66 : name_(name), |
75 outputter_(outputter), | 67 outputter_(outputter), |
76 offset_(offset), | 68 offset_(offset), |
77 start_time_(0), | 69 start_time_(0), |
78 end_time_(0), | 70 end_time_(0), |
79 end_requested_(false), | 71 tracer_type_(tracer_type), |
80 enabled_(true) { | 72 end_requested_(false) { |
81 glGenQueries(2, queries_); | 73 memset(queries_, 0, sizeof(queries_)); |
| 74 switch (tracer_type_) { |
| 75 case kTracerTypeARBTimer: |
| 76 case kTracerTypeDisjointTimer: |
| 77 glGenQueriesARB(2, queries_); |
| 78 break; |
| 79 |
| 80 default: |
| 81 tracer_type_ = kTracerTypeInvalid; |
| 82 } |
82 } | 83 } |
83 | 84 |
84 GPUTrace::~GPUTrace() { | 85 GPUTrace::~GPUTrace() { |
85 if (enabled_) | 86 switch (tracer_type_) { |
86 glDeleteQueries(2, queries_); | 87 case kTracerTypeInvalid: |
| 88 break; |
| 89 |
| 90 case kTracerTypeARBTimer: |
| 91 case kTracerTypeDisjointTimer: |
| 92 glDeleteQueriesARB(2, queries_); |
| 93 break; |
| 94 } |
87 } | 95 } |
88 | 96 |
89 void GPUTrace::Start() { | 97 void GPUTrace::Start() { |
90 TRACE_EVENT_COPY_ASYNC_BEGIN0( | 98 TRACE_EVENT_COPY_ASYNC_BEGIN0( |
91 TRACE_DISABLED_BY_DEFAULT("gpu.service"), name().c_str(), this); | 99 TRACE_DISABLED_BY_DEFAULT("gpu.service"), name().c_str(), this); |
92 if (enabled_) { | 100 |
93 glQueryCounter(queries_[0], GL_TIMESTAMP); | 101 switch (tracer_type_) { |
| 102 case kTracerTypeInvalid: |
| 103 break; |
| 104 |
| 105 case kTracerTypeARBTimer: |
| 106 case kTracerTypeDisjointTimer: |
| 107 // GL_TIMESTAMP and GL_TIMESTAMP_EXT both have the same value. |
| 108 glQueryCounter(queries_[0], GL_TIMESTAMP); |
| 109 break; |
94 } | 110 } |
95 } | 111 } |
96 | 112 |
97 void GPUTrace::End() { | 113 void GPUTrace::End() { |
98 if (enabled_) { | 114 end_requested_ = true; |
99 glQueryCounter(queries_[1], GL_TIMESTAMP); | 115 switch (tracer_type_) { |
100 end_requested_ = true; | 116 case kTracerTypeInvalid: |
| 117 break; |
| 118 |
| 119 case kTracerTypeARBTimer: |
| 120 case kTracerTypeDisjointTimer: |
| 121 // GL_TIMESTAMP and GL_TIMESTAMP_EXT both have the same value. |
| 122 glQueryCounter(queries_[1], GL_TIMESTAMP); |
| 123 break; |
101 } | 124 } |
102 | 125 |
103 TRACE_EVENT_COPY_ASYNC_END0( | 126 TRACE_EVENT_COPY_ASYNC_END0( |
104 TRACE_DISABLED_BY_DEFAULT("gpu.service"), name().c_str(), this); | 127 TRACE_DISABLED_BY_DEFAULT("gpu.service"), name().c_str(), this); |
105 } | 128 } |
106 | 129 |
107 bool GPUTrace::IsAvailable() { | 130 bool GPUTrace::IsAvailable() { |
108 if (!enabled_) | 131 if (tracer_type_ != kTracerTypeInvalid) { |
109 return true; | 132 if (!end_requested_) |
110 else if (!end_requested_) | 133 return false; |
111 return false; | |
112 | 134 |
113 GLint done = 0; | 135 GLint done = 0; |
114 glGetQueryObjectiv(queries_[1], GL_QUERY_RESULT_AVAILABLE, &done); | 136 glGetQueryObjectiv(queries_[1], GL_QUERY_RESULT_AVAILABLE, &done); |
115 return !!done; | 137 return !!done; |
| 138 } |
| 139 |
| 140 return true; |
116 } | 141 } |
117 | 142 |
118 void GPUTrace::Process() { | 143 void GPUTrace::Process() { |
119 if (!enabled_) | 144 if (tracer_type_ == kTracerTypeInvalid) |
120 return; | 145 return; |
121 | 146 |
122 DCHECK(IsAvailable()); | 147 DCHECK(IsAvailable()); |
123 | 148 |
124 GLuint64 timestamp; | 149 GLuint64 begin_stamp = 0; |
| 150 GLuint64 end_stamp = 0; |
125 | 151 |
126 // TODO(dsinclair): It's possible for the timer to wrap during the start/end. | 152 // TODO(dsinclair): It's possible for the timer to wrap during the start/end. |
127 // We need to detect if the end is less then the start and correct for the | 153 // We need to detect if the end is less then the start and correct for the |
128 // wrapping. | 154 // wrapping. |
129 glGetQueryObjectui64v(queries_[0], GL_QUERY_RESULT, ×tamp); | 155 glGetQueryObjectui64v(queries_[0], GL_QUERY_RESULT, &begin_stamp); |
130 start_time_ = (timestamp / base::Time::kNanosecondsPerMicrosecond) + offset_; | 156 glGetQueryObjectui64v(queries_[1], GL_QUERY_RESULT, &end_stamp); |
131 | 157 |
132 glGetQueryObjectui64v(queries_[1], GL_QUERY_RESULT, ×tamp); | 158 start_time_ = (begin_stamp / base::Time::kNanosecondsPerMicrosecond) + |
133 end_time_ = (timestamp / base::Time::kNanosecondsPerMicrosecond) + offset_; | 159 offset_; |
134 | 160 end_time_ = (end_stamp / base::Time::kNanosecondsPerMicrosecond) + offset_; |
135 glDeleteQueries(2, queries_); | |
136 outputter_->Trace(name(), start_time_, end_time_); | 161 outputter_->Trace(name(), start_time_, end_time_); |
137 } | 162 } |
138 | 163 |
139 GPUTracer::GPUTracer(gles2::GLES2Decoder* decoder) | 164 GPUTracer::GPUTracer(gles2::GLES2Decoder* decoder) |
140 : gpu_trace_srv_category(TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( | 165 : gpu_trace_srv_category(TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( |
141 TRACE_DISABLED_BY_DEFAULT("gpu.service"))), | 166 TRACE_DISABLED_BY_DEFAULT("gpu.service"))), |
142 gpu_trace_dev_category(TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( | 167 gpu_trace_dev_category(TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( |
143 TRACE_DISABLED_BY_DEFAULT("gpu.device"))), | 168 TRACE_DISABLED_BY_DEFAULT("gpu.device"))), |
144 decoder_(decoder), | 169 decoder_(decoder), |
145 timer_offset_(0), | 170 timer_offset_(0), |
146 last_tracer_source_(kTraceGroupInvalid), | 171 last_tracer_source_(kTraceGroupInvalid), |
147 enabled_(false), | 172 tracer_type_(kTracerTypeInvalid), |
148 gpu_timing_synced_(false), | 173 gpu_timing_synced_(false), |
149 gpu_executing_(false), | 174 gpu_executing_(false), |
150 process_posted_(false) { | 175 process_posted_(false) { |
151 if (gfx::g_driver_gl.ext.b_GL_ARB_timer_query) { | 176 if (gfx::g_driver_gl.ext.b_GL_EXT_disjoint_timer_query) { |
| 177 tracer_type_ = kTracerTypeDisjointTimer; |
| 178 outputter_ = TraceOutputter::Create("GL_EXT_disjoint_timer_query"); |
| 179 } else if (gfx::g_driver_gl.ext.b_GL_ARB_timer_query) { |
| 180 tracer_type_ = kTracerTypeARBTimer; |
152 outputter_ = TraceOutputter::Create("GL_ARB_timer_query"); | 181 outputter_ = TraceOutputter::Create("GL_ARB_timer_query"); |
153 enabled_ = true; | |
154 } | 182 } |
155 } | 183 } |
156 | 184 |
157 GPUTracer::~GPUTracer() { | 185 GPUTracer::~GPUTracer() { |
158 } | 186 } |
159 | 187 |
160 bool GPUTracer::BeginDecoding() { | 188 bool GPUTracer::BeginDecoding() { |
161 if (enabled_) { | |
162 if (*gpu_trace_dev_category) { | |
163 // Make sure timing is synced before tracing | |
164 if (!gpu_timing_synced_) { | |
165 CalculateTimerOffset(); | |
166 gpu_timing_synced_ = true; | |
167 } | |
168 } else { | |
169 // If GPU device category is off, invalidate timing sync | |
170 gpu_timing_synced_ = false; | |
171 } | |
172 } | |
173 | |
174 if (gpu_executing_) | 189 if (gpu_executing_) |
175 return false; | 190 return false; |
176 | 191 |
| 192 CalculateTimerOffset(); |
177 gpu_executing_ = true; | 193 gpu_executing_ = true; |
178 | 194 |
179 if (IsTracing()) { | 195 if (IsTracing()) { |
| 196 // Reset disjoint bit for the disjoint timer. |
| 197 if (tracer_type_ == kTracerTypeDisjointTimer) { |
| 198 GLint disjoint_value = 0; |
| 199 glGetIntegerv(GL_GPU_DISJOINT_EXT, &disjoint_value); |
| 200 } |
| 201 |
180 // Begin a Trace for all active markers | 202 // Begin a Trace for all active markers |
181 for (int n = 0; n < NUM_TRACER_SOURCES; n++) { | 203 for (int n = 0; n < NUM_TRACER_SOURCES; n++) { |
182 for (size_t i = 0; i < markers_[n].size(); i++) { | 204 for (size_t i = 0; i < markers_[n].size(); i++) { |
183 markers_[n][i].trace_ = CreateTrace(markers_[n][i].name_); | 205 markers_[n][i].trace_ = CreateTrace(markers_[n][i].name_); |
184 markers_[n][i].trace_->Start(); | 206 markers_[n][i].trace_->Start(); |
185 } | 207 } |
186 } | 208 } |
187 } | 209 } |
188 return true; | 210 return true; |
189 } | 211 } |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 const std::string& GPUTracer::CurrentName() const { | 287 const std::string& GPUTracer::CurrentName() const { |
266 if (last_tracer_source_ >= 0 && | 288 if (last_tracer_source_ >= 0 && |
267 last_tracer_source_ < NUM_TRACER_SOURCES && | 289 last_tracer_source_ < NUM_TRACER_SOURCES && |
268 !markers_[last_tracer_source_].empty()) { | 290 !markers_[last_tracer_source_].empty()) { |
269 return markers_[last_tracer_source_].back().name_; | 291 return markers_[last_tracer_source_].back().name_; |
270 } | 292 } |
271 return base::EmptyString(); | 293 return base::EmptyString(); |
272 } | 294 } |
273 | 295 |
274 scoped_refptr<GPUTrace> GPUTracer::CreateTrace(const std::string& name) { | 296 scoped_refptr<GPUTrace> GPUTracer::CreateTrace(const std::string& name) { |
275 if (enabled_ && *gpu_trace_dev_category) | 297 GpuTracerType tracer_type = *gpu_trace_dev_category ? tracer_type_ : |
276 return new GPUTrace(outputter_, name, timer_offset_); | 298 kTracerTypeInvalid; |
277 else | 299 |
278 return new GPUTrace(name); | 300 return new GPUTrace(outputter_, name, timer_offset_, tracer_type); |
279 } | 301 } |
280 | 302 |
281 void GPUTracer::Process() { | 303 void GPUTracer::Process() { |
282 process_posted_ = false; | 304 process_posted_ = false; |
283 ProcessTraces(); | 305 ProcessTraces(); |
284 IssueProcessTask(); | 306 IssueProcessTask(); |
285 } | 307 } |
286 | 308 |
287 void GPUTracer::ProcessTraces() { | 309 void GPUTracer::ProcessTraces() { |
288 if (!enabled_) { | 310 if (tracer_type_ == kTracerTypeInvalid) { |
289 while (!traces_.empty() && traces_.front()->IsAvailable()) { | 311 traces_.clear(); |
290 traces_.front()->Process(); | |
291 traces_.pop_front(); | |
292 } | |
293 return; | 312 return; |
294 } | 313 } |
295 | 314 |
296 TRACE_EVENT0("gpu", "GPUTracer::ProcessTraces"); | 315 TRACE_EVENT0("gpu", "GPUTracer::ProcessTraces"); |
297 | 316 |
298 // Make owning decoder's GL context current | 317 // Make owning decoder's GL context current |
299 if (!decoder_->MakeCurrent()) { | 318 if (!decoder_->MakeCurrent()) { |
300 // Skip subsequent GL calls if MakeCurrent fails | 319 // Skip subsequent GL calls if MakeCurrent fails |
301 traces_.clear(); | 320 traces_.clear(); |
302 return; | 321 return; |
303 } | 322 } |
304 | 323 |
| 324 // Check if disjoint operation has occurred, discard ongoing traces if so. |
| 325 if (tracer_type_ == kTracerTypeDisjointTimer) { |
| 326 GLint disjoint_value = 0; |
| 327 glGetIntegerv(GL_GPU_DISJOINT_EXT, &disjoint_value); |
| 328 if (disjoint_value) |
| 329 traces_.clear(); |
| 330 } |
| 331 |
305 while (!traces_.empty() && traces_.front()->IsAvailable()) { | 332 while (!traces_.empty() && traces_.front()->IsAvailable()) { |
306 traces_.front()->Process(); | 333 traces_.front()->Process(); |
307 traces_.pop_front(); | 334 traces_.pop_front(); |
308 } | 335 } |
309 | 336 |
310 // Clear pending traces if there were are any errors | 337 // Clear pending traces if there were are any errors |
311 GLenum err = glGetError(); | 338 GLenum err = glGetError(); |
312 if (err != GL_NO_ERROR) | 339 if (err != GL_NO_ERROR) |
313 traces_.clear(); | 340 traces_.clear(); |
314 } | 341 } |
315 | 342 |
316 void GPUTracer::CalculateTimerOffset() { | 343 void GPUTracer::CalculateTimerOffset() { |
317 if (enabled_) { | 344 if (tracer_type_ != kTracerTypeInvalid) { |
| 345 // If GPU device category is off, invalidate timing sync. |
| 346 if (*gpu_trace_dev_category == '\0') { |
| 347 gpu_timing_synced_ = false; |
| 348 return; |
| 349 } |
| 350 |
| 351 if (gpu_timing_synced_) |
| 352 return; |
| 353 |
318 TRACE_EVENT0("gpu", "GPUTracer::CalculateTimerOffset"); | 354 TRACE_EVENT0("gpu", "GPUTracer::CalculateTimerOffset"); |
319 | 355 |
320 // NOTE(vmiura): It would be better to use glGetInteger64v, however | 356 // NOTE(vmiura): It would be better to use glGetInteger64v, however |
321 // it's not available everywhere. | 357 // it's not available everywhere. |
322 GLuint64 gl_now = 0; | 358 GLuint64 gl_now = 0; |
323 GLuint query; | 359 GLuint query; |
| 360 GLint disjoint_value = 0; |
| 361 |
| 362 if (tracer_type_ == kTracerTypeDisjointTimer) { |
| 363 // Clear the disjoint bit before we do any queries. |
| 364 glGetIntegerv(GL_GPU_DISJOINT_EXT, &disjoint_value); |
| 365 } |
| 366 |
324 glFinish(); | 367 glFinish(); |
325 glGenQueries(1, &query); | 368 glGenQueriesARB(1, &query); |
326 glQueryCounter(query, GL_TIMESTAMP); | 369 glQueryCounter(query, GL_TIMESTAMP); |
327 glFinish(); | 370 glFinish(); |
| 371 |
328 glGetQueryObjectui64v(query, GL_QUERY_RESULT, &gl_now); | 372 glGetQueryObjectui64v(query, GL_QUERY_RESULT, &gl_now); |
| 373 glDeleteQueriesARB(1, &query); |
| 374 |
| 375 if (tracer_type_ == kTracerTypeDisjointTimer) { |
| 376 glGetIntegerv(GL_GPU_DISJOINT_EXT, &disjoint_value); |
| 377 if (disjoint_value) |
| 378 return; |
| 379 } |
| 380 |
329 base::TimeTicks system_now = base::TimeTicks::NowFromSystemTraceTime(); | 381 base::TimeTicks system_now = base::TimeTicks::NowFromSystemTraceTime(); |
330 | 382 |
331 gl_now /= base::Time::kNanosecondsPerMicrosecond; | 383 gl_now /= base::Time::kNanosecondsPerMicrosecond; |
332 timer_offset_ = system_now.ToInternalValue() - gl_now; | 384 timer_offset_ = system_now.ToInternalValue() - gl_now; |
333 glDeleteQueries(1, &query); | 385 gpu_timing_synced_ = true; |
334 } | 386 } |
335 } | 387 } |
336 | 388 |
337 void GPUTracer::IssueProcessTask() { | 389 void GPUTracer::IssueProcessTask() { |
338 if (traces_.empty() || process_posted_) | 390 if (traces_.empty() || process_posted_) |
339 return; | 391 return; |
340 | 392 |
341 process_posted_ = true; | 393 process_posted_ = true; |
342 base::MessageLoop::current()->PostDelayedTask( | 394 base::MessageLoop::current()->PostDelayedTask( |
343 FROM_HERE, | 395 FROM_HERE, |
344 base::Bind(&GPUTracer::Process, base::AsWeakPtr(this)), | 396 base::Bind(&GPUTracer::Process, base::AsWeakPtr(this)), |
345 base::TimeDelta::FromMilliseconds(kProcessInterval)); | 397 base::TimeDelta::FromMilliseconds(kProcessInterval)); |
346 } | 398 } |
347 | 399 |
348 } // namespace gles2 | 400 } // namespace gles2 |
349 } // namespace gpu | 401 } // namespace gpu |
OLD | NEW |