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 | |
Peter Boström | 7623ce4 | 2015-12-09 12:13:30 +0100 | [diff] [blame] | 11 | #include "webrtc/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> |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 19 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 20 | #include "webrtc/api/video/video_frame.h" |
tommi@webrtc.org | 7a57f8f | 2015-02-08 18:27:46 +0000 | [diff] [blame] | 21 | #include "webrtc/base/checks.h" |
Peter Boström | 415d2cd | 2015-10-26 11:35:17 +0100 | [diff] [blame] | 22 | #include "webrtc/base/logging.h" |
terelius | bc5d921 | 2017-01-13 09:14:33 -0800 | [diff] [blame] | 23 | #include "webrtc/base/numerics/exp_filter.h" |
pbos | a96b60b | 2016-04-18 21:12:48 -0700 | [diff] [blame] | 24 | #include "webrtc/common_video/include/frame_callback.h" |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 25 | |
pbos | a102507 | 2016-05-14 03:04:19 -0700 | [diff] [blame] | 26 | #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
torbjorng | 448468d | 2016-02-10 08:11:57 -0800 | [diff] [blame] | 27 | #include <mach/mach.h> |
pbos | a102507 | 2016-05-14 03:04:19 -0700 | [diff] [blame] | 28 | #endif // defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
torbjorng | 448468d | 2016-02-10 08:11:57 -0800 | [diff] [blame] | 29 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 30 | namespace webrtc { |
| 31 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 32 | namespace { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 33 | const int64_t kCheckForOveruseIntervalMs = 5000; |
| 34 | const int64_t kTimeToFirstCheckForOveruseMs = 100; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 35 | |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 36 | // Delay between consecutive rampups. (Used for quick recovery.) |
| 37 | const int kQuickRampUpDelayMs = 10 * 1000; |
| 38 | // Delay between rampup attempts. Initially uses standard, scales up to max. |
asapersson@webrtc.org | 23a4d85 | 2014-08-13 14:33:49 +0000 | [diff] [blame] | 39 | const int kStandardRampUpDelayMs = 40 * 1000; |
asapersson@webrtc.org | 2881ab1 | 2014-06-12 08:46:46 +0000 | [diff] [blame] | 40 | const int kMaxRampUpDelayMs = 240 * 1000; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 41 | // Expontential back-off factor, to prevent annoying up-down behaviour. |
| 42 | const double kRampUpBackoffFactor = 2.0; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 43 | |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 44 | // Max number of overuses detected before always applying the rampup delay. |
asapersson@webrtc.org | 23a4d85 | 2014-08-13 14:33:49 +0000 | [diff] [blame] | 45 | const int kMaxOverusesBeforeApplyRampupDelay = 4; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 46 | |
| 47 | // The maximum exponent to use in VCMExpFilter. |
| 48 | const float kSampleDiffMs = 33.0f; |
| 49 | const float kMaxExp = 7.0f; |
| 50 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 51 | const auto kScaleReasonCpu = ScalingObserverInterface::ScaleReason::kCpu; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 52 | } // namespace |
| 53 | |
torbjorng | 448468d | 2016-02-10 08:11:57 -0800 | [diff] [blame] | 54 | CpuOveruseOptions::CpuOveruseOptions() |
| 55 | : high_encode_usage_threshold_percent(85), |
| 56 | frame_timeout_interval_ms(1500), |
| 57 | min_frame_samples(120), |
| 58 | min_process_count(3), |
| 59 | high_threshold_consecutive_count(2) { |
pbos | a102507 | 2016-05-14 03:04:19 -0700 | [diff] [blame] | 60 | #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
torbjorng | 448468d | 2016-02-10 08:11:57 -0800 | [diff] [blame] | 61 | // This is proof-of-concept code for letting the physical core count affect |
| 62 | // the interval into which we attempt to scale. For now, the code is Mac OS |
| 63 | // specific, since that's the platform were we saw most problems. |
| 64 | // TODO(torbjorng): Enhance SystemInfo to return this metric. |
| 65 | |
| 66 | mach_port_t mach_host = mach_host_self(); |
| 67 | host_basic_info hbi = {}; |
| 68 | mach_msg_type_number_t info_count = HOST_BASIC_INFO_COUNT; |
| 69 | kern_return_t kr = |
| 70 | host_info(mach_host, HOST_BASIC_INFO, reinterpret_cast<host_info_t>(&hbi), |
| 71 | &info_count); |
| 72 | mach_port_deallocate(mach_task_self(), mach_host); |
| 73 | |
| 74 | int n_physical_cores; |
| 75 | if (kr != KERN_SUCCESS) { |
| 76 | // If we couldn't get # of physical CPUs, don't panic. Assume we have 1. |
| 77 | n_physical_cores = 1; |
| 78 | LOG(LS_ERROR) << "Failed to determine number of physical cores, assuming 1"; |
| 79 | } else { |
| 80 | n_physical_cores = hbi.physical_cpu; |
| 81 | LOG(LS_INFO) << "Number of physical cores:" << n_physical_cores; |
| 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) |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 109 | : kWeightFactorFrameDiff(0.998f), |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 110 | kWeightFactorProcessing(0.995f), |
asapersson@webrtc.org | e41dbee | 2014-05-13 13:45:13 +0000 | [diff] [blame] | 111 | kInitialSampleDiffMs(40.0f), |
| 112 | kMaxSampleDiffMs(45.0f), |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 113 | count_(0), |
Peter Boström | 4b91bd0 | 2015-06-26 06:58:16 +0200 | [diff] [blame] | 114 | options_(options), |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 115 | filtered_processing_ms_(new rtc::ExpFilter(kWeightFactorProcessing)), |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 116 | filtered_frame_diff_ms_(new rtc::ExpFilter(kWeightFactorFrameDiff)) { |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 117 | Reset(); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 118 | } |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 119 | ~SendProcessingUsage() {} |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 120 | |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 121 | void Reset() { |
| 122 | count_ = 0; |
| 123 | filtered_frame_diff_ms_->Reset(kWeightFactorFrameDiff); |
| 124 | filtered_frame_diff_ms_->Apply(1.0f, kInitialSampleDiffMs); |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 125 | filtered_processing_ms_->Reset(kWeightFactorProcessing); |
| 126 | filtered_processing_ms_->Apply(1.0f, InitialProcessingMs()); |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 127 | } |
| 128 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 129 | void AddCaptureSample(float sample_ms) { |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 130 | float exp = sample_ms / kSampleDiffMs; |
| 131 | exp = std::min(exp, kMaxExp); |
| 132 | filtered_frame_diff_ms_->Apply(exp, sample_ms); |
| 133 | } |
| 134 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 135 | void AddSample(float processing_ms, int64_t diff_last_sample_ms) { |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 136 | ++count_; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 137 | float exp = diff_last_sample_ms / kSampleDiffMs; |
| 138 | exp = std::min(exp, kMaxExp); |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 139 | filtered_processing_ms_->Apply(exp, processing_ms); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 140 | } |
| 141 | |
asapersson@webrtc.org | 2881ab1 | 2014-06-12 08:46:46 +0000 | [diff] [blame] | 142 | int Value() const { |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 143 | if (count_ < static_cast<uint32_t>(options_.min_frame_samples)) { |
| 144 | return static_cast<int>(InitialUsageInPercent() + 0.5f); |
| 145 | } |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 146 | float frame_diff_ms = std::max(filtered_frame_diff_ms_->filtered(), 1.0f); |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 147 | frame_diff_ms = std::min(frame_diff_ms, kMaxSampleDiffMs); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 148 | float encode_usage_percent = |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 149 | 100.0f * filtered_processing_ms_->filtered() / frame_diff_ms; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 150 | return static_cast<int>(encode_usage_percent + 0.5); |
| 151 | } |
| 152 | |
asapersson@webrtc.org | 2881ab1 | 2014-06-12 08:46:46 +0000 | [diff] [blame] | 153 | private: |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 154 | float InitialUsageInPercent() const { |
| 155 | // Start in between the underuse and overuse threshold. |
| 156 | return (options_.low_encode_usage_threshold_percent + |
| 157 | options_.high_encode_usage_threshold_percent) / 2.0f; |
| 158 | } |
| 159 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 160 | float InitialProcessingMs() const { |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 161 | return InitialUsageInPercent() * kInitialSampleDiffMs / 100; |
| 162 | } |
| 163 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 164 | const float kWeightFactorFrameDiff; |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 165 | const float kWeightFactorProcessing; |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 166 | const float kInitialSampleDiffMs; |
| 167 | const float kMaxSampleDiffMs; |
| 168 | uint64_t count_; |
Peter Boström | 4b91bd0 | 2015-06-26 06:58:16 +0200 | [diff] [blame] | 169 | const CpuOveruseOptions options_; |
kwiberg | 27f982b | 2016-03-01 11:52:33 -0800 | [diff] [blame] | 170 | std::unique_ptr<rtc::ExpFilter> filtered_processing_ms_; |
| 171 | std::unique_ptr<rtc::ExpFilter> filtered_frame_diff_ms_; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 174 | class OveruseFrameDetector::CheckOveruseTask : public rtc::QueuedTask { |
| 175 | public: |
| 176 | explicit CheckOveruseTask(OveruseFrameDetector* overuse_detector) |
| 177 | : overuse_detector_(overuse_detector) { |
| 178 | rtc::TaskQueue::Current()->PostDelayedTask( |
| 179 | std::unique_ptr<rtc::QueuedTask>(this), kTimeToFirstCheckForOveruseMs); |
| 180 | } |
| 181 | |
| 182 | void Stop() { |
| 183 | RTC_CHECK(task_checker_.CalledSequentially()); |
| 184 | overuse_detector_ = nullptr; |
| 185 | } |
| 186 | |
| 187 | private: |
| 188 | bool Run() override { |
| 189 | RTC_CHECK(task_checker_.CalledSequentially()); |
| 190 | if (!overuse_detector_) |
| 191 | return true; // This will make the task queue delete this task. |
| 192 | overuse_detector_->CheckForOveruse(); |
| 193 | |
| 194 | rtc::TaskQueue::Current()->PostDelayedTask( |
| 195 | std::unique_ptr<rtc::QueuedTask>(this), kCheckForOveruseIntervalMs); |
| 196 | // Return false to prevent this task from being deleted. Ownership has been |
| 197 | // transferred to the task queue when PostDelayedTask was called. |
| 198 | return false; |
| 199 | } |
| 200 | rtc::SequencedTaskChecker task_checker_; |
| 201 | OveruseFrameDetector* overuse_detector_; |
| 202 | }; |
| 203 | |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 204 | OveruseFrameDetector::OveruseFrameDetector( |
Peter Boström | 4b91bd0 | 2015-06-26 06:58:16 +0200 | [diff] [blame] | 205 | const CpuOveruseOptions& options, |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 206 | ScalingObserverInterface* observer, |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 207 | EncodedFrameObserver* encoder_timing, |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 208 | CpuOveruseMetricsObserver* metrics_observer) |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 209 | : check_overuse_task_(nullptr), |
| 210 | options_(options), |
Peter Boström | 4b91bd0 | 2015-06-26 06:58:16 +0200 | [diff] [blame] | 211 | observer_(observer), |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 212 | encoder_timing_(encoder_timing), |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 213 | metrics_observer_(metrics_observer), |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 214 | num_process_times_(0), |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 215 | // TODO(nisse): Use rtc::Optional |
| 216 | last_capture_time_us_(-1), |
| 217 | last_processed_capture_time_us_(-1), |
asapersson | 74d85e1 | 2015-09-24 00:53:32 -0700 | [diff] [blame] | 218 | num_pixels_(0), |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 219 | last_overuse_time_ms_(-1), |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 220 | checks_above_threshold_(0), |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 221 | num_overuse_detections_(0), |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 222 | last_rampup_time_ms_(-1), |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 223 | in_quick_rampup_(false), |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 224 | current_rampup_delay_ms_(kStandardRampUpDelayMs), |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 225 | usage_(new SendProcessingUsage(options)) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 226 | task_checker_.Detach(); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 227 | } |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 228 | |
| 229 | OveruseFrameDetector::~OveruseFrameDetector() { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 230 | RTC_DCHECK(!check_overuse_task_) << "StopCheckForOverUse must be called."; |
| 231 | } |
| 232 | |
| 233 | void OveruseFrameDetector::StartCheckForOveruse() { |
| 234 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 235 | RTC_DCHECK(!check_overuse_task_); |
| 236 | check_overuse_task_ = new CheckOveruseTask(this); |
| 237 | } |
| 238 | void OveruseFrameDetector::StopCheckForOveruse() { |
| 239 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 240 | check_overuse_task_->Stop(); |
| 241 | check_overuse_task_ = nullptr; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 244 | void OveruseFrameDetector::EncodedFrameTimeMeasured(int encode_duration_ms) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 245 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 246 | if (!metrics_) |
| 247 | metrics_ = rtc::Optional<CpuOveruseMetrics>(CpuOveruseMetrics()); |
| 248 | metrics_->encode_usage_percent = usage_->Value(); |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 249 | |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 250 | metrics_observer_->OnEncodedFrameTimeMeasured(encode_duration_ms, *metrics_); |
asapersson@webrtc.org | ab6bf4f | 2014-05-27 07:43:15 +0000 | [diff] [blame] | 251 | } |
| 252 | |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 253 | bool OveruseFrameDetector::FrameSizeChanged(int num_pixels) const { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 254 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 255 | if (num_pixels != num_pixels_) { |
| 256 | return true; |
| 257 | } |
| 258 | return false; |
| 259 | } |
| 260 | |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 261 | bool OveruseFrameDetector::FrameTimeoutDetected(int64_t now_us) const { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 262 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 263 | if (last_capture_time_us_ == -1) |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 264 | return false; |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 265 | return (now_us - last_capture_time_us_) > |
| 266 | options_.frame_timeout_interval_ms * rtc::kNumMicrosecsPerMillisec; |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void OveruseFrameDetector::ResetAll(int num_pixels) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 270 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 271 | num_pixels_ = num_pixels; |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 272 | usage_->Reset(); |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 273 | frame_timing_.clear(); |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 274 | last_capture_time_us_ = -1; |
| 275 | last_processed_capture_time_us_ = -1; |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 276 | num_process_times_ = 0; |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 277 | metrics_ = rtc::Optional<CpuOveruseMetrics>(); |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 278 | } |
| 279 | |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 280 | void OveruseFrameDetector::FrameCaptured(const VideoFrame& frame, |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 281 | int64_t time_when_first_seen_us) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 282 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 283 | |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 284 | if (FrameSizeChanged(frame.width() * frame.height()) || |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 285 | FrameTimeoutDetected(time_when_first_seen_us)) { |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 286 | ResetAll(frame.width() * frame.height()); |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 287 | } |
| 288 | |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 289 | if (last_capture_time_us_ != -1) |
| 290 | usage_->AddCaptureSample( |
| 291 | 1e-3 * (time_when_first_seen_us - last_capture_time_us_)); |
Åsa Persson | 746210f | 2015-09-08 10:52:42 +0200 | [diff] [blame] | 292 | |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 293 | last_capture_time_us_ = time_when_first_seen_us; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 294 | |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 295 | frame_timing_.push_back(FrameTiming(frame.timestamp_us(), frame.timestamp(), |
| 296 | time_when_first_seen_us)); |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 297 | } |
| 298 | |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 299 | void OveruseFrameDetector::FrameSent(uint32_t timestamp, |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 300 | int64_t time_sent_in_us) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 301 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 302 | // Delay before reporting actual encoding time, used to have the ability to |
| 303 | // detect total encoding time when encoding more than one layer. Encoding is |
| 304 | // here assumed to finish within a second (or that we get enough long-time |
| 305 | // samples before one second to trigger an overuse even when this is not the |
| 306 | // case). |
| 307 | static const int64_t kEncodingTimeMeasureWindowMs = 1000; |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 308 | for (auto& it : frame_timing_) { |
| 309 | if (it.timestamp == timestamp) { |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 310 | it.last_send_us = time_sent_in_us; |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 311 | break; |
| 312 | } |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 313 | } |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 314 | // TODO(pbos): Handle the case/log errors when not finding the corresponding |
| 315 | // frame (either very slow encoding or incorrect wrong timestamps returned |
| 316 | // from the encoder). |
| 317 | // This is currently the case for all frames on ChromeOS, so logging them |
| 318 | // would be spammy, and triggering overuse would be wrong. |
| 319 | // https://crbug.com/350106 |
| 320 | while (!frame_timing_.empty()) { |
| 321 | FrameTiming timing = frame_timing_.front(); |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 322 | if (time_sent_in_us - timing.capture_us < |
| 323 | kEncodingTimeMeasureWindowMs * rtc::kNumMicrosecsPerMillisec) |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 324 | break; |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 325 | if (timing.last_send_us != -1) { |
| 326 | int encode_duration_us = |
| 327 | static_cast<int>(timing.last_send_us - timing.capture_us); |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 328 | if (encoder_timing_) { |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 329 | // TODO(nisse): Update encoder_timing_ to also use us units. |
| 330 | encoder_timing_->OnEncodeTiming(timing.capture_time_us / |
| 331 | rtc::kNumMicrosecsPerMillisec, |
| 332 | encode_duration_us / |
| 333 | rtc::kNumMicrosecsPerMillisec); |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 334 | } |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 335 | if (last_processed_capture_time_us_ != -1) { |
| 336 | int64_t diff_us = timing.capture_us - last_processed_capture_time_us_; |
| 337 | usage_->AddSample(1e-3 * encode_duration_us, 1e-3 * diff_us); |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 338 | } |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 339 | last_processed_capture_time_us_ = timing.capture_us; |
| 340 | EncodedFrameTimeMeasured(encode_duration_us / |
| 341 | rtc::kNumMicrosecsPerMillisec); |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 342 | } |
| 343 | frame_timing_.pop_front(); |
| 344 | } |
asapersson@webrtc.org | c7ff8f9 | 2013-11-26 11:12:33 +0000 | [diff] [blame] | 345 | } |
| 346 | |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 347 | void OveruseFrameDetector::CheckForOveruse() { |
| 348 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 349 | ++num_process_times_; |
| 350 | if (num_process_times_ <= options_.min_process_count || !metrics_) |
| 351 | return; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 352 | |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 353 | int64_t now_ms = rtc::TimeMillis(); |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 354 | |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 355 | if (IsOverusing(*metrics_)) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 356 | // If the last thing we did was going up, and now have to back down, we need |
| 357 | // to check if this peak was short. If so we should back off to avoid going |
| 358 | // 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] | 359 | 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] | 360 | if (check_for_backoff) { |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 361 | if (now_ms - last_rampup_time_ms_ < kStandardRampUpDelayMs || |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 362 | num_overuse_detections_ > kMaxOverusesBeforeApplyRampupDelay) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 363 | // Going up was not ok for very long, back off. |
| 364 | current_rampup_delay_ms_ *= kRampUpBackoffFactor; |
| 365 | if (current_rampup_delay_ms_ > kMaxRampUpDelayMs) |
| 366 | current_rampup_delay_ms_ = kMaxRampUpDelayMs; |
| 367 | } else { |
| 368 | // Not currently backing off, reset rampup delay. |
| 369 | current_rampup_delay_ms_ = kStandardRampUpDelayMs; |
| 370 | } |
| 371 | } |
| 372 | |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 373 | last_overuse_time_ms_ = now_ms; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 374 | in_quick_rampup_ = false; |
| 375 | checks_above_threshold_ = 0; |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 376 | ++num_overuse_detections_; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 377 | |
Peter Boström | 74f6e9e | 2016-04-04 17:56:10 +0200 | [diff] [blame] | 378 | if (observer_) |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 379 | observer_->ScaleDown(kScaleReasonCpu); |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 380 | } else if (IsUnderusing(*metrics_, now_ms)) { |
| 381 | last_rampup_time_ms_ = now_ms; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 382 | in_quick_rampup_ = true; |
| 383 | |
Peter Boström | 74f6e9e | 2016-04-04 17:56:10 +0200 | [diff] [blame] | 384 | if (observer_) |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 385 | observer_->ScaleUp(kScaleReasonCpu); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 386 | } |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 387 | |
mflodman@webrtc.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 388 | int rampup_delay = |
| 389 | in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; |
asapersson | 74d85e1 | 2015-09-24 00:53:32 -0700 | [diff] [blame] | 390 | |
| 391 | LOG(LS_VERBOSE) << " Frame stats: " |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 392 | << " encode usage " << metrics_->encode_usage_percent |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 393 | << " overuse detections " << num_overuse_detections_ |
| 394 | << " rampup delay " << rampup_delay; |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 395 | } |
| 396 | |
asapersson | 74d85e1 | 2015-09-24 00:53:32 -0700 | [diff] [blame] | 397 | bool OveruseFrameDetector::IsOverusing(const CpuOveruseMetrics& metrics) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 398 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Peter Boström | 01f364e | 2016-01-07 16:38:25 +0100 | [diff] [blame] | 399 | if (metrics.encode_usage_percent >= |
| 400 | options_.high_encode_usage_threshold_percent) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 401 | ++checks_above_threshold_; |
| 402 | } else { |
| 403 | checks_above_threshold_ = 0; |
| 404 | } |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 405 | return checks_above_threshold_ >= options_.high_threshold_consecutive_count; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 406 | } |
| 407 | |
asapersson | 74d85e1 | 2015-09-24 00:53:32 -0700 | [diff] [blame] | 408 | bool OveruseFrameDetector::IsUnderusing(const CpuOveruseMetrics& metrics, |
| 409 | int64_t time_now) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 410 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 411 | int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 412 | if (time_now < last_rampup_time_ms_ + delay) |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 413 | return false; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 414 | |
Peter Boström | 01f364e | 2016-01-07 16:38:25 +0100 | [diff] [blame] | 415 | return metrics.encode_usage_percent < |
| 416 | options_.low_encode_usage_threshold_percent; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 417 | } |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 418 | } // namespace webrtc |