mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "video/overuse_frame_detector.h" |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 12 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 13 | #include <assert.h> |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 14 | #include <math.h> |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 15 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 16 | #include <algorithm> |
| 17 | #include <list> |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 18 | #include <map> |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <utility> |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 21 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "api/video/video_frame.h" |
| 23 | #include "common_video/include/frame_callback.h" |
| 24 | #include "rtc_base/checks.h" |
| 25 | #include "rtc_base/logging.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "rtc_base/timeutils.h" |
| 27 | #include "system_wrappers/include/field_trial.h" |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 28 | |
pbos | a102507 | 2016-05-14 03:04:19 -0700 | [diff] [blame] | 29 | #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
torbjorng | 448468d | 2016-02-10 08:11:57 -0800 | [diff] [blame] | 30 | #include <mach/mach.h> |
pbos | a102507 | 2016-05-14 03:04:19 -0700 | [diff] [blame] | 31 | #endif // defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
torbjorng | 448468d | 2016-02-10 08:11:57 -0800 | [diff] [blame] | 32 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 33 | namespace webrtc { |
| 34 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 35 | namespace { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 36 | const int64_t kCheckForOveruseIntervalMs = 5000; |
| 37 | const int64_t kTimeToFirstCheckForOveruseMs = 100; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 38 | |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 39 | // Delay between consecutive rampups. (Used for quick recovery.) |
| 40 | const int kQuickRampUpDelayMs = 10 * 1000; |
| 41 | // Delay between rampup attempts. Initially uses standard, scales up to max. |
asapersson@webrtc.org | 23a4d85 | 2014-08-13 14:33:49 +0000 | [diff] [blame] | 42 | const int kStandardRampUpDelayMs = 40 * 1000; |
asapersson@webrtc.org | 2881ab1 | 2014-06-12 08:46:46 +0000 | [diff] [blame] | 43 | const int kMaxRampUpDelayMs = 240 * 1000; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 44 | // Expontential back-off factor, to prevent annoying up-down behaviour. |
| 45 | const double kRampUpBackoffFactor = 2.0; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 46 | |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 47 | // Max number of overuses detected before always applying the rampup delay. |
asapersson@webrtc.org | 23a4d85 | 2014-08-13 14:33:49 +0000 | [diff] [blame] | 48 | const int kMaxOverusesBeforeApplyRampupDelay = 4; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 49 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 50 | const auto kScaleReasonCpu = AdaptationObserverInterface::AdaptReason::kCpu; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 51 | } // namespace |
| 52 | |
torbjorng | 448468d | 2016-02-10 08:11:57 -0800 | [diff] [blame] | 53 | CpuOveruseOptions::CpuOveruseOptions() |
| 54 | : high_encode_usage_threshold_percent(85), |
| 55 | frame_timeout_interval_ms(1500), |
torbjorng | 448468d | 2016-02-10 08:11:57 -0800 | [diff] [blame] | 56 | min_process_count(3), |
Niels Möller | eee7ced | 2017-12-01 11:25:01 +0100 | [diff] [blame^] | 57 | high_threshold_consecutive_count(2), |
| 58 | filter_time_ms(5000) { |
pbos | a102507 | 2016-05-14 03:04:19 -0700 | [diff] [blame] | 59 | #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
torbjorng | 448468d | 2016-02-10 08:11:57 -0800 | [diff] [blame] | 60 | // This is proof-of-concept code for letting the physical core count affect |
| 61 | // the interval into which we attempt to scale. For now, the code is Mac OS |
| 62 | // specific, since that's the platform were we saw most problems. |
| 63 | // TODO(torbjorng): Enhance SystemInfo to return this metric. |
| 64 | |
| 65 | mach_port_t mach_host = mach_host_self(); |
| 66 | host_basic_info hbi = {}; |
| 67 | mach_msg_type_number_t info_count = HOST_BASIC_INFO_COUNT; |
| 68 | kern_return_t kr = |
| 69 | host_info(mach_host, HOST_BASIC_INFO, reinterpret_cast<host_info_t>(&hbi), |
| 70 | &info_count); |
| 71 | mach_port_deallocate(mach_task_self(), mach_host); |
| 72 | |
| 73 | int n_physical_cores; |
| 74 | if (kr != KERN_SUCCESS) { |
| 75 | // If we couldn't get # of physical CPUs, don't panic. Assume we have 1. |
| 76 | n_physical_cores = 1; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 77 | RTC_LOG(LS_ERROR) |
| 78 | << "Failed to determine number of physical cores, assuming 1"; |
torbjorng | 448468d | 2016-02-10 08:11:57 -0800 | [diff] [blame] | 79 | } else { |
| 80 | n_physical_cores = hbi.physical_cpu; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 81 | RTC_LOG(LS_INFO) << "Number of physical cores:" << n_physical_cores; |
torbjorng | 448468d | 2016-02-10 08:11:57 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // Change init list default for few core systems. The assumption here is that |
| 85 | // encoding, which we measure here, takes about 1/4 of the processing of a |
| 86 | // two-way call. This is roughly true for x86 using both vp8 and vp9 without |
| 87 | // hardware encoding. Since we don't affect the incoming stream here, we only |
| 88 | // control about 1/2 of the total processing needs, but this is not taken into |
| 89 | // account. |
| 90 | if (n_physical_cores == 1) |
| 91 | high_encode_usage_threshold_percent = 20; // Roughly 1/4 of 100%. |
| 92 | else if (n_physical_cores == 2) |
| 93 | high_encode_usage_threshold_percent = 40; // Roughly 1/4 of 200%. |
pbos | a102507 | 2016-05-14 03:04:19 -0700 | [diff] [blame] | 94 | #endif // defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
torbjorng | 448468d | 2016-02-10 08:11:57 -0800 | [diff] [blame] | 95 | |
torbjorng | 448468d | 2016-02-10 08:11:57 -0800 | [diff] [blame] | 96 | // Note that we make the interval 2x+epsilon wide, since libyuv scaling steps |
| 97 | // are close to that (when squared). This wide interval makes sure that |
| 98 | // scaling up or down does not jump all the way across the interval. |
| 99 | low_encode_usage_threshold_percent = |
| 100 | (high_encode_usage_threshold_percent - 1) / 2; |
| 101 | } |
| 102 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 103 | // Class for calculating the processing usage on the send-side (the average |
| 104 | // processing time of a frame divided by the average time difference between |
| 105 | // captured frames). |
| 106 | class OveruseFrameDetector::SendProcessingUsage { |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 107 | public: |
Peter Boström | 4b91bd0 | 2015-06-26 06:58:16 +0200 | [diff] [blame] | 108 | explicit SendProcessingUsage(const CpuOveruseOptions& options) |
Niels Möller | eee7ced | 2017-12-01 11:25:01 +0100 | [diff] [blame^] | 109 | : options_(options) { |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 110 | Reset(); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 111 | } |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 112 | virtual ~SendProcessingUsage() {} |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 113 | |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 114 | void Reset() { |
Niels Möller | eee7ced | 2017-12-01 11:25:01 +0100 | [diff] [blame^] | 115 | // Start in between the underuse and overuse threshold. |
| 116 | load_estimate_ = (options_.low_encode_usage_threshold_percent + |
| 117 | options_.high_encode_usage_threshold_percent) / |
| 118 | 200.0; |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Niels Möller | eee7ced | 2017-12-01 11:25:01 +0100 | [diff] [blame^] | 121 | void AddSample(double encode_time, double diff_time) { |
| 122 | RTC_CHECK_GE(diff_time, 0.0); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 123 | |
Niels Möller | eee7ced | 2017-12-01 11:25:01 +0100 | [diff] [blame^] | 124 | // Use the filter update |
| 125 | // |
| 126 | // load <-- x/d (1-exp (-d/T)) + exp (-d/T) load |
| 127 | // |
| 128 | // where we must take care for small d, using the proper limit |
| 129 | // (1 - exp(-d/tau)) / d = 1/tau - d/2tau^2 + O(d^2) |
| 130 | double tau = (1e-3 * options_.filter_time_ms); |
| 131 | double e = diff_time / tau; |
| 132 | double c; |
| 133 | if (e < 0.0001) { |
| 134 | c = (1 - e / 2) / tau; |
| 135 | } else { |
| 136 | c = -expm1(-e) / diff_time; |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 137 | } |
Niels Möller | eee7ced | 2017-12-01 11:25:01 +0100 | [diff] [blame^] | 138 | load_estimate_ = c * encode_time + exp(-e) * load_estimate_; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Niels Möller | eee7ced | 2017-12-01 11:25:01 +0100 | [diff] [blame^] | 141 | virtual int Value() { return static_cast<int>(100.0 * load_estimate_ + 0.5); } |
| 142 | |
asapersson@webrtc.org | 2881ab1 | 2014-06-12 08:46:46 +0000 | [diff] [blame] | 143 | private: |
Peter Boström | 4b91bd0 | 2015-06-26 06:58:16 +0200 | [diff] [blame] | 144 | const CpuOveruseOptions options_; |
Niels Möller | eee7ced | 2017-12-01 11:25:01 +0100 | [diff] [blame^] | 145 | double load_estimate_; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 146 | }; |
| 147 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 148 | // Class used for manual testing of overuse, enabled via field trial flag. |
| 149 | class OveruseFrameDetector::OverdoseInjector |
| 150 | : public OveruseFrameDetector::SendProcessingUsage { |
| 151 | public: |
| 152 | OverdoseInjector(const CpuOveruseOptions& options, |
| 153 | int64_t normal_period_ms, |
| 154 | int64_t overuse_period_ms, |
| 155 | int64_t underuse_period_ms) |
| 156 | : OveruseFrameDetector::SendProcessingUsage(options), |
| 157 | normal_period_ms_(normal_period_ms), |
| 158 | overuse_period_ms_(overuse_period_ms), |
| 159 | underuse_period_ms_(underuse_period_ms), |
| 160 | state_(State::kNormal), |
| 161 | last_toggling_ms_(-1) { |
| 162 | RTC_DCHECK_GT(overuse_period_ms, 0); |
| 163 | RTC_DCHECK_GT(normal_period_ms, 0); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 164 | RTC_LOG(LS_INFO) << "Simulating overuse with intervals " << normal_period_ms |
| 165 | << "ms normal mode, " << overuse_period_ms |
| 166 | << "ms overuse mode."; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | ~OverdoseInjector() override {} |
| 170 | |
| 171 | int Value() override { |
| 172 | int64_t now_ms = rtc::TimeMillis(); |
| 173 | if (last_toggling_ms_ == -1) { |
| 174 | last_toggling_ms_ = now_ms; |
Niels Möller | eee7ced | 2017-12-01 11:25:01 +0100 | [diff] [blame^] | 175 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 176 | } else { |
| 177 | switch (state_) { |
| 178 | case State::kNormal: |
| 179 | if (now_ms > last_toggling_ms_ + normal_period_ms_) { |
| 180 | state_ = State::kOveruse; |
| 181 | last_toggling_ms_ = now_ms; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 182 | RTC_LOG(LS_INFO) << "Simulating CPU overuse."; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 183 | } |
| 184 | break; |
| 185 | case State::kOveruse: |
| 186 | if (now_ms > last_toggling_ms_ + overuse_period_ms_) { |
| 187 | state_ = State::kUnderuse; |
| 188 | last_toggling_ms_ = now_ms; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 189 | RTC_LOG(LS_INFO) << "Simulating CPU underuse."; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 190 | } |
| 191 | break; |
| 192 | case State::kUnderuse: |
| 193 | if (now_ms > last_toggling_ms_ + underuse_period_ms_) { |
| 194 | state_ = State::kNormal; |
| 195 | last_toggling_ms_ = now_ms; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 196 | RTC_LOG(LS_INFO) << "Actual CPU overuse measurements in effect."; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 197 | } |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | rtc::Optional<int> overried_usage_value; |
| 203 | switch (state_) { |
| 204 | case State::kNormal: |
| 205 | break; |
| 206 | case State::kOveruse: |
| 207 | overried_usage_value.emplace(250); |
| 208 | break; |
| 209 | case State::kUnderuse: |
| 210 | overried_usage_value.emplace(5); |
| 211 | break; |
| 212 | } |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 213 | return overried_usage_value.value_or(SendProcessingUsage::Value()); |
| 214 | } |
| 215 | |
| 216 | private: |
| 217 | const int64_t normal_period_ms_; |
| 218 | const int64_t overuse_period_ms_; |
| 219 | const int64_t underuse_period_ms_; |
| 220 | enum class State { kNormal, kOveruse, kUnderuse } state_; |
| 221 | int64_t last_toggling_ms_; |
| 222 | }; |
| 223 | |
| 224 | std::unique_ptr<OveruseFrameDetector::SendProcessingUsage> |
| 225 | OveruseFrameDetector::CreateSendProcessingUsage( |
| 226 | const CpuOveruseOptions& options) { |
| 227 | std::unique_ptr<SendProcessingUsage> instance; |
| 228 | std::string toggling_interval = |
| 229 | field_trial::FindFullName("WebRTC-ForceSimulatedOveruseIntervalMs"); |
| 230 | if (!toggling_interval.empty()) { |
| 231 | int normal_period_ms = 0; |
| 232 | int overuse_period_ms = 0; |
| 233 | int underuse_period_ms = 0; |
| 234 | if (sscanf(toggling_interval.c_str(), "%d-%d-%d", &normal_period_ms, |
| 235 | &overuse_period_ms, &underuse_period_ms) == 3) { |
| 236 | if (normal_period_ms > 0 && overuse_period_ms > 0 && |
| 237 | underuse_period_ms > 0) { |
| 238 | instance.reset(new OverdoseInjector( |
| 239 | options, normal_period_ms, overuse_period_ms, underuse_period_ms)); |
| 240 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 241 | RTC_LOG(LS_WARNING) |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 242 | << "Invalid (non-positive) normal/overuse/underuse periods: " |
| 243 | << normal_period_ms << " / " << overuse_period_ms << " / " |
| 244 | << underuse_period_ms; |
| 245 | } |
| 246 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 247 | RTC_LOG(LS_WARNING) << "Malformed toggling interval: " |
| 248 | << toggling_interval; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | if (!instance) { |
| 253 | // No valid overuse simulation parameters set, use normal usage class. |
| 254 | instance.reset(new SendProcessingUsage(options)); |
| 255 | } |
| 256 | |
| 257 | return instance; |
| 258 | } |
| 259 | |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 260 | class OveruseFrameDetector::CheckOveruseTask : public rtc::QueuedTask { |
| 261 | public: |
| 262 | explicit CheckOveruseTask(OveruseFrameDetector* overuse_detector) |
| 263 | : overuse_detector_(overuse_detector) { |
| 264 | rtc::TaskQueue::Current()->PostDelayedTask( |
| 265 | std::unique_ptr<rtc::QueuedTask>(this), kTimeToFirstCheckForOveruseMs); |
| 266 | } |
| 267 | |
| 268 | void Stop() { |
| 269 | RTC_CHECK(task_checker_.CalledSequentially()); |
| 270 | overuse_detector_ = nullptr; |
| 271 | } |
| 272 | |
| 273 | private: |
| 274 | bool Run() override { |
| 275 | RTC_CHECK(task_checker_.CalledSequentially()); |
| 276 | if (!overuse_detector_) |
| 277 | return true; // This will make the task queue delete this task. |
| 278 | overuse_detector_->CheckForOveruse(); |
| 279 | |
| 280 | rtc::TaskQueue::Current()->PostDelayedTask( |
| 281 | std::unique_ptr<rtc::QueuedTask>(this), kCheckForOveruseIntervalMs); |
| 282 | // Return false to prevent this task from being deleted. Ownership has been |
| 283 | // transferred to the task queue when PostDelayedTask was called. |
| 284 | return false; |
| 285 | } |
| 286 | rtc::SequencedTaskChecker task_checker_; |
| 287 | OveruseFrameDetector* overuse_detector_; |
| 288 | }; |
| 289 | |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 290 | OveruseFrameDetector::OveruseFrameDetector( |
Peter Boström | 4b91bd0 | 2015-06-26 06:58:16 +0200 | [diff] [blame] | 291 | const CpuOveruseOptions& options, |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 292 | AdaptationObserverInterface* observer, |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 293 | EncodedFrameObserver* encoder_timing, |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 294 | CpuOveruseMetricsObserver* metrics_observer) |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 295 | : check_overuse_task_(nullptr), |
| 296 | options_(options), |
Peter Boström | 4b91bd0 | 2015-06-26 06:58:16 +0200 | [diff] [blame] | 297 | observer_(observer), |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 298 | encoder_timing_(encoder_timing), |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 299 | metrics_observer_(metrics_observer), |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 300 | num_process_times_(0), |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 301 | // TODO(nisse): Use rtc::Optional |
| 302 | last_capture_time_us_(-1), |
| 303 | last_processed_capture_time_us_(-1), |
asapersson | 74d85e1 | 2015-09-24 00:53:32 -0700 | [diff] [blame] | 304 | num_pixels_(0), |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 305 | last_overuse_time_ms_(-1), |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 306 | checks_above_threshold_(0), |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 307 | num_overuse_detections_(0), |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 308 | last_rampup_time_ms_(-1), |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 309 | in_quick_rampup_(false), |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 310 | current_rampup_delay_ms_(kStandardRampUpDelayMs), |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 311 | usage_(CreateSendProcessingUsage(options)) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 312 | task_checker_.Detach(); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 313 | } |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 314 | |
| 315 | OveruseFrameDetector::~OveruseFrameDetector() { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 316 | RTC_DCHECK(!check_overuse_task_) << "StopCheckForOverUse must be called."; |
| 317 | } |
| 318 | |
| 319 | void OveruseFrameDetector::StartCheckForOveruse() { |
| 320 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 321 | RTC_DCHECK(!check_overuse_task_); |
| 322 | check_overuse_task_ = new CheckOveruseTask(this); |
| 323 | } |
| 324 | void OveruseFrameDetector::StopCheckForOveruse() { |
| 325 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 326 | check_overuse_task_->Stop(); |
| 327 | check_overuse_task_ = nullptr; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 330 | void OveruseFrameDetector::EncodedFrameTimeMeasured(int encode_duration_ms) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 331 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 332 | if (!metrics_) |
| 333 | metrics_ = rtc::Optional<CpuOveruseMetrics>(CpuOveruseMetrics()); |
| 334 | metrics_->encode_usage_percent = usage_->Value(); |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 335 | |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 336 | metrics_observer_->OnEncodedFrameTimeMeasured(encode_duration_ms, *metrics_); |
asapersson@webrtc.org | ab6bf4f | 2014-05-27 07:43:15 +0000 | [diff] [blame] | 337 | } |
| 338 | |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 339 | bool OveruseFrameDetector::FrameSizeChanged(int num_pixels) const { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 340 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 341 | if (num_pixels != num_pixels_) { |
| 342 | return true; |
| 343 | } |
| 344 | return false; |
| 345 | } |
| 346 | |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 347 | bool OveruseFrameDetector::FrameTimeoutDetected(int64_t now_us) const { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 348 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 349 | if (last_capture_time_us_ == -1) |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 350 | return false; |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 351 | return (now_us - last_capture_time_us_) > |
| 352 | options_.frame_timeout_interval_ms * rtc::kNumMicrosecsPerMillisec; |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Niels Möller | eee7ced | 2017-12-01 11:25:01 +0100 | [diff] [blame^] | 355 | void OveruseFrameDetector::ResetAll() { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 356 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 357 | usage_->Reset(); |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 358 | last_capture_time_us_ = -1; |
| 359 | last_processed_capture_time_us_ = -1; |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 360 | num_process_times_ = 0; |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 361 | metrics_ = rtc::Optional<CpuOveruseMetrics>(); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Niels Möller | eee7ced | 2017-12-01 11:25:01 +0100 | [diff] [blame^] | 364 | void OveruseFrameDetector::FrameCaptured(int width, int height) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 365 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 366 | |
Niels Möller | eee7ced | 2017-12-01 11:25:01 +0100 | [diff] [blame^] | 367 | if (FrameSizeChanged(width * height)) { |
| 368 | ResetAll(); |
| 369 | num_pixels_ = width * height; |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 370 | } |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Niels Möller | eee7ced | 2017-12-01 11:25:01 +0100 | [diff] [blame^] | 373 | void OveruseFrameDetector::FrameEncoded(int64_t capture_time_us, |
| 374 | int64_t encode_duration_us) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 375 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Niels Möller | eee7ced | 2017-12-01 11:25:01 +0100 | [diff] [blame^] | 376 | if (FrameTimeoutDetected(capture_time_us)) { |
| 377 | ResetAll(); |
| 378 | } else if (last_capture_time_us_ != -1) { |
| 379 | usage_->AddSample(1e-6 * encode_duration_us, |
| 380 | 1e-6 * (capture_time_us - last_capture_time_us_)); |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 381 | } |
Niels Möller | eee7ced | 2017-12-01 11:25:01 +0100 | [diff] [blame^] | 382 | last_capture_time_us_ = capture_time_us; |
| 383 | EncodedFrameTimeMeasured(encode_duration_us / rtc::kNumMicrosecsPerMillisec); |
| 384 | |
| 385 | if (encoder_timing_) { |
| 386 | // TODO(nisse): Update encoder_timing_ to also use us units. |
| 387 | encoder_timing_->OnEncodeTiming( |
| 388 | capture_time_us / rtc::kNumMicrosecsPerMillisec, |
| 389 | encode_duration_us / rtc::kNumMicrosecsPerMillisec); |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 390 | } |
asapersson@webrtc.org | c7ff8f9 | 2013-11-26 11:12:33 +0000 | [diff] [blame] | 391 | } |
| 392 | |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 393 | void OveruseFrameDetector::CheckForOveruse() { |
| 394 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 395 | ++num_process_times_; |
| 396 | if (num_process_times_ <= options_.min_process_count || !metrics_) |
| 397 | return; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 398 | |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 399 | int64_t now_ms = rtc::TimeMillis(); |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 400 | |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 401 | if (IsOverusing(*metrics_)) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 402 | // If the last thing we did was going up, and now have to back down, we need |
| 403 | // to check if this peak was short. If so we should back off to avoid going |
| 404 | // back and forth between this load, the system doesn't seem to handle it. |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 405 | bool check_for_backoff = last_rampup_time_ms_ > last_overuse_time_ms_; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 406 | if (check_for_backoff) { |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 407 | if (now_ms - last_rampup_time_ms_ < kStandardRampUpDelayMs || |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 408 | num_overuse_detections_ > kMaxOverusesBeforeApplyRampupDelay) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 409 | // Going up was not ok for very long, back off. |
| 410 | current_rampup_delay_ms_ *= kRampUpBackoffFactor; |
| 411 | if (current_rampup_delay_ms_ > kMaxRampUpDelayMs) |
| 412 | current_rampup_delay_ms_ = kMaxRampUpDelayMs; |
| 413 | } else { |
| 414 | // Not currently backing off, reset rampup delay. |
| 415 | current_rampup_delay_ms_ = kStandardRampUpDelayMs; |
| 416 | } |
| 417 | } |
| 418 | |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 419 | last_overuse_time_ms_ = now_ms; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 420 | in_quick_rampup_ = false; |
| 421 | checks_above_threshold_ = 0; |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 422 | ++num_overuse_detections_; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 423 | |
Peter Boström | 74f6e9e | 2016-04-04 17:56:10 +0200 | [diff] [blame] | 424 | if (observer_) |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 425 | observer_->AdaptDown(kScaleReasonCpu); |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 426 | } else if (IsUnderusing(*metrics_, now_ms)) { |
| 427 | last_rampup_time_ms_ = now_ms; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 428 | in_quick_rampup_ = true; |
| 429 | |
Peter Boström | 74f6e9e | 2016-04-04 17:56:10 +0200 | [diff] [blame] | 430 | if (observer_) |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 431 | observer_->AdaptUp(kScaleReasonCpu); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 432 | } |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 433 | |
mflodman@webrtc.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 434 | int rampup_delay = |
| 435 | in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; |
asapersson | 74d85e1 | 2015-09-24 00:53:32 -0700 | [diff] [blame] | 436 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 437 | RTC_LOG(LS_VERBOSE) << " Frame stats: " |
| 438 | << " encode usage " << metrics_->encode_usage_percent |
| 439 | << " overuse detections " << num_overuse_detections_ |
| 440 | << " rampup delay " << rampup_delay; |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 441 | } |
| 442 | |
asapersson | 74d85e1 | 2015-09-24 00:53:32 -0700 | [diff] [blame] | 443 | bool OveruseFrameDetector::IsOverusing(const CpuOveruseMetrics& metrics) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 444 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 445 | |
Peter Boström | 01f364e | 2016-01-07 16:38:25 +0100 | [diff] [blame] | 446 | if (metrics.encode_usage_percent >= |
| 447 | options_.high_encode_usage_threshold_percent) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 448 | ++checks_above_threshold_; |
| 449 | } else { |
| 450 | checks_above_threshold_ = 0; |
| 451 | } |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 452 | return checks_above_threshold_ >= options_.high_threshold_consecutive_count; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 453 | } |
| 454 | |
asapersson | 74d85e1 | 2015-09-24 00:53:32 -0700 | [diff] [blame] | 455 | bool OveruseFrameDetector::IsUnderusing(const CpuOveruseMetrics& metrics, |
| 456 | int64_t time_now) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 457 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 458 | int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 459 | if (time_now < last_rampup_time_ms_ + delay) |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 460 | return false; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 461 | |
Peter Boström | 01f364e | 2016-01-07 16:38:25 +0100 | [diff] [blame] | 462 | return metrics.encode_usage_percent < |
| 463 | options_.low_encode_usage_threshold_percent; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 464 | } |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 465 | } // namespace webrtc |