measurements.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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
  3. // found in the LICENSE file.
  4. #include "gpu/perftests/measurements.h"
  5. #include <stdint.h>
  6. #include "base/logging.h"
  7. #include "testing/perf/perf_result_reporter.h"
  8. #include "ui/gl/gpu_timing.h"
  9. namespace gpu {
  10. Measurement::Measurement() = default;
  11. Measurement::Measurement(const Measurement& m) = default;
  12. Measurement::Measurement(const std::string& metric_basename,
  13. const base::TimeDelta wall_time,
  14. const base::TimeDelta cpu_time,
  15. const base::TimeDelta gpu_time)
  16. : metric_basename(metric_basename),
  17. wall_time(wall_time),
  18. cpu_time(cpu_time),
  19. gpu_time(gpu_time) {}
  20. void Measurement::PrintResult(const std::string& story) const {
  21. auto reporter =
  22. std::make_unique<perf_test::PerfResultReporter>(metric_basename, story);
  23. reporter->RegisterImportantMetric("_wall", "ms");
  24. reporter->AddResult("_wall", wall_time.InMillisecondsF());
  25. if (cpu_time.InMicroseconds() >= 0) {
  26. reporter->RegisterImportantMetric("_cpu", "ms");
  27. reporter->AddResult("_cpu", cpu_time.InMillisecondsF());
  28. }
  29. if (gpu_time.InMicroseconds() >= 0) {
  30. reporter->RegisterImportantMetric("_gpu", "ms");
  31. reporter->AddResult("_gpu", gpu_time.InMillisecondsF());
  32. }
  33. }
  34. Measurement& Measurement::Increment(const Measurement& m) {
  35. wall_time += m.wall_time;
  36. cpu_time += m.cpu_time;
  37. gpu_time += m.gpu_time;
  38. return *this;
  39. }
  40. Measurement Measurement::Divide(int a) const {
  41. return Measurement(metric_basename, wall_time / a, cpu_time / a,
  42. gpu_time / a);
  43. }
  44. Measurement::~Measurement() = default;
  45. MeasurementTimers::MeasurementTimers(gl::GPUTimingClient* gpu_timing_client)
  46. : wall_time_start_(), cpu_time_start_(), gpu_timer_() {
  47. DCHECK(gpu_timing_client);
  48. wall_time_start_ = base::TimeTicks::Now();
  49. if (base::ThreadTicks::IsSupported()) {
  50. base::ThreadTicks::WaitUntilInitialized();
  51. cpu_time_start_ = base::ThreadTicks::Now();
  52. } else {
  53. static bool logged_once = false;
  54. LOG_IF(WARNING, !logged_once) << "ThreadTicks not supported.";
  55. logged_once = true;
  56. }
  57. if (gpu_timing_client->IsAvailable()) {
  58. gpu_timer_ = gpu_timing_client->CreateGPUTimer(true);
  59. gpu_timer_->Start();
  60. }
  61. }
  62. void MeasurementTimers::Record() {
  63. wall_time_ = base::TimeTicks::Now() - wall_time_start_;
  64. if (base::ThreadTicks::IsSupported()) {
  65. cpu_time_ = base::ThreadTicks::Now() - cpu_time_start_;
  66. }
  67. if (gpu_timer_.get()) {
  68. gpu_timer_->End();
  69. }
  70. }
  71. Measurement MeasurementTimers::GetAsMeasurement(
  72. const std::string& metric_basename) {
  73. DCHECK_NE(base::TimeDelta(),
  74. wall_time_); // At least wall_time_ has been set.
  75. if (!base::ThreadTicks::IsSupported()) {
  76. cpu_time_ = base::Microseconds(-1);
  77. }
  78. int64_t gpu_time = -1;
  79. if (gpu_timer_.get() != nullptr && gpu_timer_->IsAvailable()) {
  80. gpu_time = gpu_timer_->GetDeltaElapsed();
  81. }
  82. return Measurement(metric_basename, wall_time_, cpu_time_,
  83. base::Microseconds(gpu_time));
  84. }
  85. MeasurementTimers::~MeasurementTimers() {
  86. if (gpu_timer_.get()) {
  87. gpu_timer_->Destroy(true);
  88. }
  89. }
  90. } // namespace gpu