Chromium Code Reviews| Index: gpu/command_buffer/service/gpu_tracer.cc |
| diff --git a/gpu/command_buffer/service/gpu_tracer.cc b/gpu/command_buffer/service/gpu_tracer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0d6a9bc0d46295666283a2e3cf424040d6b6442f |
| --- /dev/null |
| +++ b/gpu/command_buffer/service/gpu_tracer.cc |
| @@ -0,0 +1,313 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "gpu/command_buffer/service/gpu_tracer.h" |
| + |
| +#include <deque> |
| +#include <stack> |
| + |
| +#include "base/bind.h" |
| +#include "base/debug/trace_event.h" |
| +#include "base/memory/linked_ptr.h" |
| +#include "base/threading/non_thread_safe.h" |
| +#include "base/threading/thread.h" |
| +#include "base/time.h" |
| +#include "ui/gl/gl_bindings.h" |
| + |
| +namespace gpu { |
| +namespace gles2 { |
| +namespace { |
| + |
| +static const unsigned int kGPUTraceMaxQueueSize = 1024; |
| + |
| +class Trace { |
| + public: |
| + virtual ~Trace() {}; |
| + |
| + virtual void Start() = 0; |
| + virtual int64 start_time() = 0; |
| + virtual void End() = 0; |
| + virtual int64 end_time() = 0; |
| + |
| + // True if the the results of this query are available. |
| + virtual bool IsAvailable() = 0; |
| + virtual bool Discard() { return false; } |
| + |
| + const char* name() { |
| + return name_.c_str(); |
| + } |
| + |
| + protected: |
| + explicit Trace(const std::string& name) : name_(name) {} |
| + |
| + private: |
| + std::string name_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(Trace); |
| +}; |
| + |
| +class GLARBTimerTrace : public Trace { |
| + public: |
| + explicit GLARBTimerTrace(const std::string& name); |
| + ~GLARBTimerTrace() OVERRIDE; |
| + |
| + void Start() OVERRIDE; |
| + int64 start_time() OVERRIDE; |
| + void End() OVERRIDE; |
| + int64 end_time() OVERRIDE; |
| + bool IsAvailable() OVERRIDE; |
| + |
| + private: |
| + unsigned int start_timer_; |
| + unsigned int end_timer_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(GLARBTimerTrace); |
| +}; |
| + |
| +class NoopTrace : public Trace { |
| + public: |
| + NoopTrace(const std::string& name) : Trace(name) {} |
| + ~NoopTrace() {} |
| + |
| + void Start() {} |
| + int64 start_time() { return 0; } |
| + void End() {} |
| + int64 end_time() { return 0; } |
| + |
| + bool IsAvailable() { return true; } |
| + bool Discard() { return true; } |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(NoopTrace); |
| +}; |
| + |
| +class Outputter |
| + : public base::Thread, |
| + public base::RefCounted<Outputter> { |
| + public: |
| + static scoped_refptr<Outputter> Create(const std::string& name) { |
| + if (!g_outputter_thread) { |
| + g_outputter_thread = new Outputter(name); |
| + g_outputter_thread->Start(); |
| + } |
| + return g_outputter_thread; |
| + } |
| + |
| + void GLARBTimerQueryOutput(const char* name, int64 start, int64 end) { |
| + // TODO(dsinclair): Change to TRACE_EVENT |
| + LOG(ERROR) << "Thread " << thread_id() << ": " |
| + << thread_name() << ": " |
| + << name << ": " |
| + << start << " " |
| + << end; |
| + } |
| + |
| + protected: |
| + explicit Outputter(const std::string& name) |
| + : base::Thread(name.c_str()), |
| + current_id_(0) { |
| + } |
| + |
| + virtual ~Outputter() { |
| + g_outputter_thread->Stop(); |
| + g_outputter_thread = NULL; |
| + } |
| + |
| + static Outputter* g_outputter_thread; |
| + |
| + private: |
| + friend class base::RefCounted<Outputter>; |
| + |
| + int64 current_id_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(Outputter); |
| +}; |
| +Outputter* Outputter::g_outputter_thread = NULL; |
| + |
| +class GPUTracerImpl : public GPUTracer { |
| + public: |
| + GPUTracerImpl() |
| + : gpu_category_enabled_(TRACE_EVENT_API_GET_CATEGORY_ENABLED("gpu")) { |
| + } |
| + virtual ~GPUTracerImpl() {} |
| + |
| + // Begin a trace. |
| + virtual bool Begin(const std::string& name) OVERRIDE; |
| + |
| + // End the last started trace. |
| + virtual bool End() OVERRIDE; |
| + |
| + // Process any completed traces. |
| + virtual void Process() OVERRIDE; |
| + |
| + protected: |
| + // Create a new trace. |
| + virtual linked_ptr<Trace> CreateTrace(const std::string& name); |
| + |
| + virtual void ProcessTrace(linked_ptr<Trace> trace) = 0; |
| + |
| + scoped_refptr<Outputter> output_; |
| + |
| + const unsigned char* gpu_category_enabled_; |
| + |
| + private: |
| + linked_ptr<Trace> current_trace_; |
| + std::deque<linked_ptr<Trace> > traces_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(GPUTracerImpl); |
| +}; |
| + |
| +class GPUTracerARBTimerQuery : public GPUTracerImpl { |
| + public: |
| + GPUTracerARBTimerQuery(); |
| + ~GPUTracerARBTimerQuery(); |
| + |
| + linked_ptr<Trace> CreateTrace(const std::string& name); |
| + |
| + protected: |
| + virtual void ProcessTrace(linked_ptr<Trace> trace); |
| + |
| + private: |
| + int64 timer_offset_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(GPUTracerARBTimerQuery); |
| +}; |
| + |
| +GPUTracerARBTimerQuery::GPUTracerARBTimerQuery() : GPUTracerImpl() { |
| + // TODO(dsinclair): Change to glGetInteger64v. |
| + GLuint64 gl_now = 0; |
| + GLuint query; |
| + glGenQueries(1, &query); |
| + |
| + // Flush the pipeline so our query is more accurate. |
| + glFinish(); |
| + |
| + glQueryCounter(query, GL_TIMESTAMP); |
| + glGetQueryObjectui64v(query, GL_QUERY_RESULT, &gl_now); |
| + glDeleteQueries(1, &query); |
| + |
| + gl_now /= base::Time::kNanosecondsPerMicrosecond; |
| + base::TimeTicks system_now = base::TimeTicks::NowFromSystemTraceTime(); |
| + timer_offset_ = system_now.ToInternalValue() - gl_now; |
| + |
| + output_ = Outputter::Create("GL_ARB_timer_query"); |
| +} |
| + |
| +GPUTracerARBTimerQuery::~GPUTracerARBTimerQuery() { |
| +} |
| + |
| +linked_ptr<Trace> GPUTracerARBTimerQuery::CreateTrace( |
| + const std::string& name) { |
| + if (*gpu_category_enabled_) |
| + return linked_ptr<Trace>(new GLARBTimerTrace(name)); |
| + return GPUTracerImpl::CreateTrace(name); |
| +} |
| + |
| +void GPUTracerARBTimerQuery::ProcessTrace(linked_ptr<Trace> trace) { |
| + output_->message_loop()->PostTask(FROM_HERE, |
| + base::Bind(&Outputter::GLARBTimerQueryOutput, output_, |
| + base::Owned(strdup(trace->name())), |
| + trace->start_time() + timer_offset_, |
| + trace->end_time() + timer_offset_)); |
|
jonathan.backer
2012/11/30 17:59:04
Can't we have the |trace| do this? Then we can dit
dsinclair
2012/11/30 19:45:15
Done.
|
| +} |
| + |
| +GLARBTimerTrace::GLARBTimerTrace(const std::string& name) |
| + : Trace(name), |
| + start_timer_(0), |
| + end_timer_(0) { |
| + GLuint queries[2]; |
| + glGenQueries(2, queries); |
| + |
| + start_timer_ = queries[0]; |
| + end_timer_ = queries[1]; |
| +} |
| + |
| +GLARBTimerTrace::~GLARBTimerTrace() { |
| + GLuint queries[] = {start_timer_, end_timer_}; |
| + glDeleteQueries(2, queries); |
| +} |
| + |
| +void GLARBTimerTrace::Start() { |
| + glQueryCounter(start_timer_, GL_TIMESTAMP); |
| +} |
| + |
| +int64 GLARBTimerTrace::start_time() { |
| + GLint64 timestamp; |
| + glGetQueryObjecti64v(start_timer_, GL_QUERY_RESULT, ×tamp); |
| + return timestamp / base::Time::kNanosecondsPerMicrosecond; |
| +} |
| + |
| +void GLARBTimerTrace::End() { |
| + glQueryCounter(end_timer_, GL_TIMESTAMP); |
| +} |
| + |
| +int64 GLARBTimerTrace::end_time() { |
| + GLint64 timestamp; |
| + glGetQueryObjecti64v(end_timer_, GL_QUERY_RESULT, ×tamp); |
| + return timestamp / base::Time::kNanosecondsPerMicrosecond; |
| +} |
| + |
| +bool GLARBTimerTrace::IsAvailable() { |
| + GLint done = 0; |
| + glGetQueryObjectiv(end_timer_, GL_QUERY_RESULT_AVAILABLE, &done); |
| + return !!done; |
| +} |
| + |
| +bool GPUTracerImpl::Begin(const std::string& name) { |
| + if (!*gpu_category_enabled_) |
| + return true; |
| + |
| + // Make sure we are not nesting trace commands. |
| + if (current_trace_.get() != NULL) |
| + return false; |
| + |
| + current_trace_ = CreateTrace(name); |
| + current_trace_->Start(); |
| + return true; |
| +} |
| + |
| +bool GPUTracerImpl::End() { |
| + if (!*gpu_category_enabled_) { |
| + current_trace_.reset(); |
| + traces_.clear(); |
| + return true; |
| + } |
| + |
| + if (current_trace_.get() == NULL) { |
| + return false; |
| + } |
| + |
| + current_trace_->End(); |
| + if (!current_trace_->Discard()) |
| + traces_.push_back(current_trace_); |
| + current_trace_.reset(); |
| + |
| + if (traces_.size() > kGPUTraceMaxQueueSize) |
| + Process(); |
| + |
| + return true; |
| +} |
| + |
| +void GPUTracerImpl::Process() { |
| + while (!traces_.empty() && traces_.front()->IsAvailable()) { |
| + ProcessTrace(traces_.front()); |
| + traces_.pop_front(); |
| + } |
| +} |
| + |
| +linked_ptr<Trace> GPUTracerImpl::CreateTrace(const std::string& name) { |
| + return linked_ptr<Trace>(new NoopTrace(name)); |
| +} |
| + |
| +} // namespace |
| + |
| +scoped_ptr<GPUTracer> GPUTracer::Create() { |
| + if (gfx::g_driver_gl.ext.b_GL_ARB_timer_query) |
| + return scoped_ptr<GPUTracer>(new GPUTracerARBTimerQuery()); |
| + |
| + return scoped_ptr<GPUTracer>(NULL); |
| +} |
| + |
| +} // namespace gles2 |
| +} // namespace gpu |