File: micro_benchmark_controller.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (120 lines) | stat: -rw-r--r-- 3,812 bytes parent folder | download | duplicates (6)
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
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/benchmarks/micro_benchmark_controller.h"

#include <algorithm>
#include <limits>
#include <utility>
#include <vector>

#include "base/functional/callback.h"
#include "base/task/single_thread_task_runner.h"
#include "base/values.h"
#include "cc/benchmarks/invalidation_benchmark.h"
#include "cc/benchmarks/rasterize_and_record_benchmark.h"
#include "cc/benchmarks/unittest_only_benchmark.h"
#include "cc/trees/layer_tree_host.h"
#include "cc/trees/layer_tree_host_impl.h"

namespace cc {

int MicroBenchmarkController::next_id_ = 1;

namespace {

std::unique_ptr<MicroBenchmark> CreateBenchmark(
    const std::string& name,
    base::Value::Dict settings,
    MicroBenchmark::DoneCallback callback) {
  if (name == "invalidation_benchmark") {
    return std::make_unique<InvalidationBenchmark>(std::move(settings),
                                                   std::move(callback));
  } else if (name == "rasterize_and_record_benchmark") {
    return std::make_unique<RasterizeAndRecordBenchmark>(std::move(settings),
                                                         std::move(callback));
  } else if (name == "unittest_only_benchmark") {
    return std::make_unique<UnittestOnlyBenchmark>(std::move(settings),
                                                   std::move(callback));
  }
  return nullptr;
}

}  // namespace

MicroBenchmarkController::MicroBenchmarkController(LayerTreeHost* host)
    : host_(host),
      main_controller_task_runner_(
          base::SingleThreadTaskRunner::HasCurrentDefault()
              ? base::SingleThreadTaskRunner::GetCurrentDefault()
              : nullptr) {
  DCHECK(host_);
}

MicroBenchmarkController::~MicroBenchmarkController() = default;

int MicroBenchmarkController::ScheduleRun(
    const std::string& micro_benchmark_name,
    base::Value::Dict settings,
    MicroBenchmark::DoneCallback callback) {
  std::unique_ptr<MicroBenchmark> benchmark = CreateBenchmark(
      micro_benchmark_name, std::move(settings), std::move(callback));
  if (benchmark.get()) {
    int id = GetNextIdAndIncrement();
    benchmark->set_id(id);
    benchmarks_.push_back(std::move(benchmark));
    host_->SetNeedsCommit();
    return id;
  }
  return 0;
}

int MicroBenchmarkController::GetNextIdAndIncrement() {
  int id = next_id_++;
  // Wrap around to 1 if we overflow (very unlikely).
  if (next_id_ == std::numeric_limits<int>::max())
    next_id_ = 1;
  return id;
}

bool MicroBenchmarkController::SendMessage(int id, base::Value::Dict message) {
  auto it = std::ranges::find(benchmarks_, id, &MicroBenchmark::id);
  if (it == benchmarks_.end())
    return false;
  return (*it)->ProcessMessage(std::move(message));
}

std::vector<std::unique_ptr<MicroBenchmarkImpl>>
MicroBenchmarkController::CreateImplBenchmarks() const {
  std::vector<std::unique_ptr<MicroBenchmarkImpl>> result;
  for (const auto& benchmark : benchmarks_) {
    std::unique_ptr<MicroBenchmarkImpl> benchmark_impl;
    if (!benchmark->ProcessedForBenchmarkImpl()) {
      benchmark_impl =
          benchmark->GetBenchmarkImpl(main_controller_task_runner_);
    }

    if (benchmark_impl.get())
      result.push_back(std::move(benchmark_impl));
  }
  return result;
}

void MicroBenchmarkController::DidUpdateLayers() {
  for (const auto& benchmark : benchmarks_) {
    if (!benchmark->IsDone())
      benchmark->DidUpdateLayers(host_);
  }

  CleanUpFinishedBenchmarks();
}

void MicroBenchmarkController::CleanUpFinishedBenchmarks() {
  std::erase_if(benchmarks_,
                [](const std::unique_ptr<MicroBenchmark>& benchmark) {
                  return benchmark->IsDone();
                });
}

}  // namespace cc