summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/rtc_base/experiments/cpu_speed_experiment.cc
blob: 7e61255260ab9052a653b9ef7cf3b67fe52f9a1f (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
/*
 *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "rtc_base/experiments/cpu_speed_experiment.h"

#include <stdio.h>

#include "rtc_base/experiments/field_trial_list.h"
#include "rtc_base/logging.h"
#include "system_wrappers/include/field_trial.h"

namespace webrtc {
namespace {
constexpr char kFieldTrial[] = "WebRTC-VP8-CpuSpeed-Arm";
constexpr int kMinSetting = -16;
constexpr int kMaxSetting = -1;

std::vector<CpuSpeedExperiment::Config> GetValidOrEmpty(
    const std::vector<CpuSpeedExperiment::Config>& configs) {
  if (configs.empty()) {
    return {};
  }

  for (const auto& config : configs) {
    if (config.cpu_speed < kMinSetting || config.cpu_speed > kMaxSetting) {
      RTC_LOG(LS_WARNING) << "Unsupported cpu speed setting, value ignored.";
      return {};
    }
  }

  for (size_t i = 1; i < configs.size(); ++i) {
    if (configs[i].pixels < configs[i - 1].pixels ||
        configs[i].cpu_speed > configs[i - 1].cpu_speed) {
      RTC_LOG(LS_WARNING) << "Invalid parameter value provided.";
      return {};
    }
  }

  return configs;
}

bool HasLeCores(const std::vector<CpuSpeedExperiment::Config>& configs) {
  for (const auto& config : configs) {
    if (config.cpu_speed_le_cores == 0)
      return false;
  }
  return true;
}
}  // namespace

CpuSpeedExperiment::CpuSpeedExperiment() : cores_("cores") {
  FieldTrialStructList<Config> configs(
      {FieldTrialStructMember("pixels", [](Config* c) { return &c->pixels; }),
       FieldTrialStructMember("cpu_speed",
                              [](Config* c) { return &c->cpu_speed; }),
       FieldTrialStructMember(
           "cpu_speed_le_cores",
           [](Config* c) { return &c->cpu_speed_le_cores; })},
      {});
  ParseFieldTrial({&configs, &cores_}, field_trial::FindFullName(kFieldTrial));

  configs_ = GetValidOrEmpty(configs.Get());
}

CpuSpeedExperiment::~CpuSpeedExperiment() {}

absl::optional<int> CpuSpeedExperiment::GetValue(int pixels,
                                                 int num_cores) const {
  if (configs_.empty())
    return absl::nullopt;

  bool use_le = HasLeCores(configs_) && cores_ && num_cores <= cores_.Value();

  for (const auto& config : configs_) {
    if (pixels <= config.pixels)
      return use_le ? absl::optional<int>(config.cpu_speed_le_cores)
                    : absl::optional<int>(config.cpu_speed);
  }
  return absl::optional<int>(kMinSetting);
}

}  // namespace webrtc