summaryrefslogtreecommitdiffstats
path: root/gpu/command_buffer/service/gpu_tracer.cc
blob: 85814997769274d67201f530fcc6ac6dca046fa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// 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/string_util.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* g_outputter_thread = NULL;

class Outputter
    : private 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();
      g_outputter_thread->Stop();
    }
    return g_outputter_thread;
  }

  uint64 Id() { return thread_id(); }

 private:
  friend class base::RefCounted<Outputter>;

  explicit Outputter(const std::string& name) : base::Thread(name.c_str()) {}

  virtual ~Outputter() {
    g_outputter_thread = NULL;
  }

  DISALLOW_COPY_AND_ASSIGN(Outputter);
};

class Trace : public base::RefCounted<Trace> {
 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 const std::string& name() {
    return name_;
  }

 protected:
  virtual ~Trace() {}

 private:
  friend class base::RefCounted<Trace>;

  std::string name_;

  DISALLOW_COPY_AND_ASSIGN(Trace);
};

class GLARBTimerTrace : public Trace {
 public:
  GLARBTimerTrace(scoped_refptr<Outputter> outputter, 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:
  virtual ~GLARBTimerTrace();

  void Output();

  scoped_refptr<Outputter> outputter_;

  int64 offset_;
  int64 start_time_;
  int64 end_time_;
  bool end_requested_;

  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:
  virtual ~NoopTrace() {}

  DISALLOW_COPY_AND_ASSIGN(NoopTrace);
};

class GPUTracerImpl
    : public GPUTracer,
      public base::SupportsWeakPtr<GPUTracerImpl> {
 public:
  GPUTracerImpl()
      : gpu_category_enabled_(
        TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED("gpu")),
        process_posted_(false) {
  }
  virtual ~GPUTracerImpl() {}

  // Implementation of gpu::gles2::GPUTracer
  virtual bool Begin(const std::string& name) OVERRIDE;
  virtual bool End() OVERRIDE;
  virtual const std::string& CurrentName() const OVERRIDE;

  // Process any completed traces.
  virtual void Process();

 protected:
  // Create a new trace.
  virtual scoped_refptr<Trace> CreateTrace(const std::string& name);

  const unsigned char* gpu_category_enabled_;

 private:
  void IssueProcessTask();

  scoped_refptr<Trace> current_trace_;
  std::deque<scoped_refptr<Trace> > traces_;

  bool process_posted_;

  DISALLOW_COPY_AND_ASSIGN(GPUTracerImpl);
};

class GPUTracerARBTimerQuery : public GPUTracerImpl {
 public:
  GPUTracerARBTimerQuery();
  virtual ~GPUTracerARBTimerQuery();

  // Implementation of GPUTracerImpl
  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_;

  DISALLOW_COPY_AND_ASSIGN(GPUTracerARBTimerQuery);
};

GLARBTimerTrace::GLARBTimerTrace(scoped_refptr<Outputter> outputter,
                                 const std::string& name, int64 offset)
    : Trace(name),
      outputter_(outputter),
      offset_(offset),
      start_time_(0),
      end_time_(0),
      end_requested_(false) {
  glGenQueries(2, queries_);
}

GLARBTimerTrace::~GLARBTimerTrace() {
}

void GLARBTimerTrace::Start() {
  glQueryCounter(queries_[0], GL_TIMESTAMP);
}

void GLARBTimerTrace::End() {
  glQueryCounter(queries_[1], GL_TIMESTAMP);
  end_requested_ = true;
}

bool GLARBTimerTrace::IsAvailable() {
  if (!end_requested_)
    return false;

  GLint done = 0;
  glGetQueryObjectiv(queries_[1], GL_QUERY_RESULT_AVAILABLE, &done);
  return !!done;
}

void GLARBTimerTrace::Process() {
  DCHECK(IsAvailable());

  GLint64 timestamp;

  // TODO(dsinclair): It's possible for the timer to wrap during the start/end.
  // We need to detect if the end is less then the start and correct for the
  // wrapping.
  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_WITH_ID_TID_AND_TIMESTAMP0("gpu", name().c_str(),
                                   this, outputter_->Id(), start_time_);
  TRACE_EVENT_COPY_END_WITH_ID_TID_AND_TIMESTAMP0("gpu", name().c_str(),
                                   this, outputter_->Id(), end_time_);
}

bool GPUTracerImpl::Begin(const std::string& name) {
  // Make sure we are not nesting trace commands.
  if (current_trace_)
    return false;

  current_trace_ = CreateTrace(name);
  current_trace_->Start();
  return true;
}

bool GPUTracerImpl::End() {
  if (!current_trace_)
    return false;

  current_trace_->End();
  if (current_trace_->IsProcessable())
    traces_.push_back(current_trace_);
  current_trace_ = NULL;

  IssueProcessTask();
  return true;
}

void GPUTracerImpl::Process() {
  process_posted_ = false;

  while (!traces_.empty() && traces_.front()->IsAvailable()) {
    traces_.front()->Process();
    traces_.pop_front();
  }

  IssueProcessTask();
}

const std::string& GPUTracerImpl::CurrentName() const {
  if (!current_trace_)
    return EmptyString();
  return current_trace_->name();
}

scoped_refptr<Trace> GPUTracerImpl::CreateTrace(const std::string& name) {
  return new NoopTrace(name);
}

void GPUTracerImpl::IssueProcessTask() {
  if (traces_.empty() || process_posted_)
    return;

  process_posted_ = true;
  base::MessageLoop::current()->PostDelayedTask(
      FROM_HERE,
      base::Bind(&GPUTracerImpl::Process, base::AsWeakPtr(this)),
      base::TimeDelta::FromMilliseconds(kProcessInterval));
}

GPUTracerARBTimerQuery::GPUTracerARBTimerQuery()
    : GPUTracerImpl(),
      timer_offset_(0),
      last_offset_check_(0) {
  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);
}

void GPUTracerARBTimerQuery::Process() {
  GPUTracerImpl::Process();

  if (*gpu_category_enabled_ &&
      (last_offset_check_ + base::Time::kMicrosecondsPerSecond) <
          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