Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(273)

Unified Diff: gpu/command_buffer/service/gpu_tracer.cc

Issue 11416117: Hookup the GPUTrace events. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Skip processing of no-op traces. Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..4e19bf9b58c26df09aeff703c68277d2b1bf303e
--- /dev/null
+++ b/gpu/command_buffer/service/gpu_tracer.cc
@@ -0,0 +1,328 @@
+// 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 "base/bind.h"
+#include "base/debug/trace_event.h"
+#include "base/memory/weak_ptr.h"
+#include "base/threading/thread.h"
+#include "base/time.h"
+#include "ui/gl/gl_bindings.h"
+
+namespace gpu {
+namespace gles2 {
+namespace {
+
+class Outputter;
+
+static const unsigned int kProcessInterval = 16;
+static Outputter* kOutputterThread = NULL;
+
+class Outputter
+ : public base::Thread,
jonathan.backer 2012/12/07 15:38:11 nit: just an idea. maybe you want private inherita
dsinclair 2012/12/10 16:59:03 Done.
+ public base::RefCountedThreadSafe<Outputter> {
jonathan.backer 2012/12/07 15:38:11 nit: these don't need to be threadsafe anymore.
dsinclair 2012/12/10 16:59:03 Done.
+ public:
+ static scoped_refptr<Outputter> Create(const std::string& name) {
+ if (!kOutputterThread) {
+ kOutputterThread = new Outputter(name);
+ kOutputterThread->Start();
+ kOutputterThread->Stop();
+ }
+ return kOutputterThread;
+ }
+
+ private:
+ friend class base::RefCountedThreadSafe<Outputter>;
+
+ explicit Outputter(const std::string& name) : base::Thread(name.c_str()) {}
+
+ virtual ~Outputter() {
+ kOutputterThread = NULL;
+ }
+
+ DISALLOW_COPY_AND_ASSIGN(Outputter);
+};
+
+class Trace : public base::RefCountedThreadSafe<Trace> {
jonathan.backer 2012/12/07 15:38:11 nit: no need to be threadsafe.
dsinclair 2012/12/10 16:59:03 Done.
+ public:
+ explicit Trace(const std::string& name) : name_(name) {}
+
+ virtual void Start() = 0;
+ virtual void End() = 0;
+
+ // True if the the results of this query are available.
+ virtual bool IsAvailable() = 0;
+
+ virtual bool IsProcessable() { return true; }
+ virtual void Process() = 0;
+
+ virtual std::string name() {
jonathan.backer 2012/12/07 15:38:11 nit: return |const std::string&|?
dsinclair 2012/12/10 16:59:03 Done.
+ return name_;
+ }
+
+ protected:
+ virtual ~Trace() {}
+
+ private:
+ friend class base::RefCountedThreadSafe<Trace>;
+
+ std::string name_;
+
+ DISALLOW_COPY_AND_ASSIGN(Trace);
+};
+
+class GLARBTimerTrace : public Trace {
+ public:
+ GLARBTimerTrace(scoped_refptr<Outputter> output, const std::string& name,
+ int64 offset);
+
+ // Implementation of Tracer
+ virtual void Start() OVERRIDE;
+ virtual void End() OVERRIDE;
+ virtual bool IsAvailable() OVERRIDE;
+ virtual void Process() OVERRIDE;
+
+ private:
+ ~GLARBTimerTrace() OVERRIDE;
+
+ void Output();
+
+ scoped_refptr<Outputter> outputter_;
+
+ int64 offset_;
+ int64 start_time_;
+ int64 end_time_;
+
+ GLuint queries_[2];
+
+ DISALLOW_COPY_AND_ASSIGN(GLARBTimerTrace);
+};
+
+class NoopTrace : public Trace {
+ public:
+ explicit NoopTrace(const std::string& name) : Trace(name) {}
+
+ // Implementation of Tracer
+ virtual void Start() OVERRIDE {}
+ virtual void End() OVERRIDE {}
+ virtual bool IsAvailable() OVERRIDE { return true; }
+ virtual bool IsProcessable() OVERRIDE { return false; }
+ virtual void Process() OVERRIDE {}
+
+ private:
+ ~NoopTrace() {}
+
+ DISALLOW_COPY_AND_ASSIGN(NoopTrace);
+};
+
+class GPUTracerImpl
+ : public GPUTracer,
+ public base::SupportsWeakPtr<GPUTracerImpl> {
+ public:
+ GPUTracerImpl()
+ : gpu_category_enabled_(TRACE_EVENT_API_GET_CATEGORY_ENABLED("gpu")) {
+ }
+ virtual ~GPUTracerImpl() {}
+
+ // Implementation of gpu::gles2::GPUTracer
+ virtual bool Begin(const std::string& name) OVERRIDE;
+ virtual bool End() OVERRIDE;
+ virtual void Process() OVERRIDE;
+ virtual std::string CurrentName() const OVERRIDE;
+
+ protected:
+ // Create a new trace.
+ virtual scoped_refptr<Trace> CreateTrace(const std::string& name);
+
+ const unsigned char* gpu_category_enabled_;
+
+ private:
+ scoped_refptr<Trace> current_trace_;
+ std::deque<scoped_refptr<Trace> > traces_;
+
+ DISALLOW_COPY_AND_ASSIGN(GPUTracerImpl);
+};
+
+class GPUTracerARBTimerQuery : public GPUTracerImpl {
+ public:
+ GPUTracerARBTimerQuery();
+ virtual ~GPUTracerARBTimerQuery();
+
+ // Implementation of gpu::gles2::GPUTracer
+ virtual bool End() OVERRIDE;
+ virtual void Process() OVERRIDE;
+
+ private:
+ // Implementation of GPUTracerImpl.
+ virtual scoped_refptr<Trace> CreateTrace(const std::string& name) OVERRIDE;
+
+ void CalculateTimerOffset();
+
+ scoped_refptr<Outputter> outputter_;
+
+ int64 timer_offset_;
+ int64 last_offset_check_;
+ bool process_posted_;
+
+ DISALLOW_COPY_AND_ASSIGN(GPUTracerARBTimerQuery);
+};
+
+GLARBTimerTrace::GLARBTimerTrace(scoped_refptr<Outputter> output,
+ const std::string& name, int64 offset)
+ : Trace(name),
+ outputter_(output),
+ offset_(offset),
+ start_time_(0),
+ end_time_(0) {
+ glGenQueries(2, queries_);
+}
+
+GLARBTimerTrace::~GLARBTimerTrace() {
+}
+
+void GLARBTimerTrace::Start() {
+ glQueryCounter(queries_[0], GL_TIMESTAMP);
+}
+
+void GLARBTimerTrace::End() {
+ glQueryCounter(queries_[1], GL_TIMESTAMP);
+}
+
+bool GLARBTimerTrace::IsAvailable() {
+ GLint done = 0;
+ glGetQueryObjectiv(queries_[1], GL_QUERY_RESULT_AVAILABLE, &done);
jonathan.backer 2012/12/07 15:38:11 Just occurred to me, is there a problem if this is
dsinclair 2012/12/10 16:59:03 Done.
+ return !!done;
+}
+
+void GLARBTimerTrace::Process() {
+ DCHECK(IsAvailable());
+
+ GLint64 timestamp;
+
+ glGetQueryObjecti64v(queries_[0], GL_QUERY_RESULT, &timestamp);
+ start_time_ = (timestamp / base::Time::kNanosecondsPerMicrosecond) + offset_;
+
+ glGetQueryObjecti64v(queries_[1], GL_QUERY_RESULT, &timestamp);
+ end_time_ = (timestamp / base::Time::kNanosecondsPerMicrosecond) + offset_;
+
+ glDeleteQueries(2, queries_);
+
+ TRACE_EVENT_COPY_BEGIN_EXPLICIT0("gpu", name().c_str(),
+ outputter_->thread_id(), this, start_time_);
+ TRACE_EVENT_COPY_END_EXPLICIT0("gpu", name().c_str(), outputter_->thread_id(),
+ this, end_time_);
+}
+
+bool GPUTracerImpl::Begin(const std::string& name) {
+ // Make sure we are not nesting trace commands.
+ if (current_trace_.get())
jonathan.backer 2012/12/07 15:38:11 nit: do you need the get()?
dsinclair 2012/12/10 16:59:03 I believe so, current_trace_ exists, it just may n
dsinclair 2012/12/14 16:06:23 Done.
+ return false;
+
+ current_trace_ = CreateTrace(name);
+ current_trace_->Start();
+ return true;
+}
+
+bool GPUTracerImpl::End() {
+ if (!current_trace_.get())
jonathan.backer 2012/12/07 15:38:11 nit: need the get()?
dsinclair 2012/12/10 16:59:03 As above.
dsinclair 2012/12/14 16:06:23 Done.
+ return false;
+
+ current_trace_->End();
+ if (current_trace_->IsProcessable())
+ traces_.push_back(current_trace_);
+ current_trace_ = NULL;
+
+ return true;
+}
+
+void GPUTracerImpl::Process() {
+ while (!traces_.empty() && traces_.front()->IsAvailable()) {
+ traces_.front()->Process();
+ traces_.pop_front();
+ }
+}
+
+std::string GPUTracerImpl::CurrentName() const {
+ if (!current_trace_)
+ return std::string("");
+ return current_trace_->name();
+}
+
+scoped_refptr<Trace> GPUTracerImpl::CreateTrace(const std::string& name) {
+ return new NoopTrace(name);
+}
+
+GPUTracerARBTimerQuery::GPUTracerARBTimerQuery()
+ : GPUTracerImpl(),
+ timer_offset_(0),
+ last_offset_check_(0),
+ process_posted_(false) {
+ CalculateTimerOffset();
+ outputter_ = Outputter::Create("GL_ARB_timer_query");
+}
+
+GPUTracerARBTimerQuery::~GPUTracerARBTimerQuery() {
+}
+
+scoped_refptr<Trace> GPUTracerARBTimerQuery::CreateTrace(
+ const std::string& name) {
+ if (*gpu_category_enabled_)
+ return new GLARBTimerTrace(outputter_, name, timer_offset_);
+ return GPUTracerImpl::CreateTrace(name);
+}
+
+bool GPUTracerARBTimerQuery::End() {
+ bool ret = GPUTracerImpl::End();
+
+ if (!process_posted_) {
jonathan.backer 2012/12/07 15:38:11 seems like you want to move this to GPUTracerImpl
dsinclair 2012/12/10 16:59:03 Done.
+ process_posted_ = true;
+ MessageLoop::current()->PostDelayedTask(FROM_HERE,
+ base::Bind(&GPUTracerARBTimerQuery::Process, base::AsWeakPtr(this)),
+ base::TimeDelta::FromMilliseconds(kProcessInterval));
+ }
+
+ return ret;
+}
+
+void GPUTracerARBTimerQuery::Process() {
+ process_posted_ = false;
+ GPUTracerImpl::Process();
+
+ if (*gpu_category_enabled_ &&
+ (last_offset_check_ + base::Time::kMicrosecondsPerSecond) <
jonathan.backer 2012/12/07 15:38:11 doing this every microsecond?
dsinclair 2012/12/10 16:59:03 Every second.
+ base::TimeTicks::NowFromSystemTraceTime().ToInternalValue())
+ CalculateTimerOffset();
+}
+
+void GPUTracerARBTimerQuery::CalculateTimerOffset() {
+ TRACE_EVENT0("gpu", "CalculateTimerOffset");
+ // TODO(dsinclair): Change to glGetInteger64v.
+ GLuint64 gl_now = 0;
+ GLuint query;
+ glGenQueries(1, &query);
+
+ glQueryCounter(query, GL_TIMESTAMP);
+ glGetQueryObjectui64v(query, GL_QUERY_RESULT, &gl_now);
+ base::TimeTicks system_now = base::TimeTicks::NowFromSystemTraceTime();
+
+ gl_now /= base::Time::kNanosecondsPerMicrosecond;
+ timer_offset_ = system_now.ToInternalValue() - gl_now;
+ glDeleteQueries(1, &query);
+
+ last_offset_check_ = system_now.ToInternalValue();
+}
+
+} // 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>(new GPUTracerImpl());
+}
+
+} // namespace gles2
+} // namespace gpu

Powered by Google App Engine
This is Rietveld 408576698