xref: /aosp_15_r20/external/webrtc/rtc_base/experiments/cpu_speed_experiment.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "rtc_base/experiments/cpu_speed_experiment.h"
12 
13 #include <stdio.h>
14 
15 #include "rtc_base/experiments/field_trial_list.h"
16 #include "rtc_base/logging.h"
17 #include "system_wrappers/include/field_trial.h"
18 
19 namespace webrtc {
20 namespace {
21 constexpr char kFieldTrial[] = "WebRTC-VP8-CpuSpeed-Arm";
22 constexpr int kMinSetting = -16;
23 constexpr int kMaxSetting = -1;
24 
GetValidOrEmpty(const std::vector<CpuSpeedExperiment::Config> & configs)25 std::vector<CpuSpeedExperiment::Config> GetValidOrEmpty(
26     const std::vector<CpuSpeedExperiment::Config>& configs) {
27   if (configs.empty()) {
28     return {};
29   }
30 
31   for (const auto& config : configs) {
32     if (config.cpu_speed < kMinSetting || config.cpu_speed > kMaxSetting) {
33       RTC_LOG(LS_WARNING) << "Unsupported cpu speed setting, value ignored.";
34       return {};
35     }
36   }
37 
38   for (size_t i = 1; i < configs.size(); ++i) {
39     if (configs[i].pixels < configs[i - 1].pixels ||
40         configs[i].cpu_speed > configs[i - 1].cpu_speed) {
41       RTC_LOG(LS_WARNING) << "Invalid parameter value provided.";
42       return {};
43     }
44   }
45 
46   return configs;
47 }
48 
HasLeCores(const std::vector<CpuSpeedExperiment::Config> & configs)49 bool HasLeCores(const std::vector<CpuSpeedExperiment::Config>& configs) {
50   for (const auto& config : configs) {
51     if (config.cpu_speed_le_cores == 0)
52       return false;
53   }
54   return true;
55 }
56 }  // namespace
57 
CpuSpeedExperiment()58 CpuSpeedExperiment::CpuSpeedExperiment() : cores_("cores") {
59   FieldTrialStructList<Config> configs(
60       {FieldTrialStructMember("pixels", [](Config* c) { return &c->pixels; }),
61        FieldTrialStructMember("cpu_speed",
62                               [](Config* c) { return &c->cpu_speed; }),
63        FieldTrialStructMember(
64            "cpu_speed_le_cores",
65            [](Config* c) { return &c->cpu_speed_le_cores; })},
66       {});
67   ParseFieldTrial({&configs, &cores_}, field_trial::FindFullName(kFieldTrial));
68 
69   configs_ = GetValidOrEmpty(configs.Get());
70 }
71 
~CpuSpeedExperiment()72 CpuSpeedExperiment::~CpuSpeedExperiment() {}
73 
GetValue(int pixels,int num_cores) const74 absl::optional<int> CpuSpeedExperiment::GetValue(int pixels,
75                                                  int num_cores) const {
76   if (configs_.empty())
77     return absl::nullopt;
78 
79   bool use_le = HasLeCores(configs_) && cores_ && num_cores <= cores_.Value();
80 
81   for (const auto& config : configs_) {
82     if (pixels <= config.pixels)
83       return use_le ? absl::optional<int>(config.cpu_speed_le_cores)
84                     : absl::optional<int>(config.cpu_speed);
85   }
86   return absl::optional<int>(kMinSetting);
87 }
88 
89 }  // namespace webrtc
90