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 | |
| 11 | #include "webrtc/video_engine/overuse_frame_detector.h" |
| 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 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 16 | #include "webrtc/system_wrappers/interface/clock.h" |
| 17 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame^] | 18 | #include "webrtc/system_wrappers/interface/trace.h" |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 19 | #include "webrtc/video_engine/include/vie_base.h" |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame^] | 23 | Statistics::Statistics() { Reset(); } |
| 24 | |
| 25 | void Statistics::Reset() { |
| 26 | sum_ = sum_squared_ = 0.0; |
| 27 | count_ = 0; |
| 28 | } |
| 29 | |
| 30 | void Statistics::AddSample(double sample) { |
| 31 | sum_ += sample; |
| 32 | sum_squared_ += sample * sample; |
| 33 | ++count_; |
| 34 | } |
| 35 | |
| 36 | double Statistics::Mean() const { |
| 37 | if (count_ == 0) |
| 38 | return 0.0; |
| 39 | return sum_ / count_; |
| 40 | } |
| 41 | |
| 42 | double Statistics::Variance() const { |
| 43 | if (count_ == 0) |
| 44 | return 0.0; |
| 45 | return sum_squared_ / count_ - Mean() * Mean(); |
| 46 | } |
| 47 | |
| 48 | double Statistics::StdDev() const { return sqrt(Variance()); } |
| 49 | |
| 50 | uint64_t Statistics::Samples() const { return count_; } |
| 51 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 52 | // TODO(mflodman) Test different values for all of these to trigger correctly, |
| 53 | // avoid fluctuations etc. |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 54 | namespace { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame^] | 55 | const int64_t kProcessIntervalMs = 5000; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 56 | |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame^] | 57 | // Limits on standard deviation for under/overuse. |
| 58 | const double kOveruseStdDevMs = 15.0; |
| 59 | const double kNormalUseStdDevMs = 10.0; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 60 | |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame^] | 61 | // Rampdown checks. |
| 62 | const int kConsecutiveChecksAboveThreshold = 2; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 63 | |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame^] | 64 | // Delay between consecutive rampups. (Used for quick recovery.) |
| 65 | const int kQuickRampUpDelayMs = 10 * 1000; |
| 66 | // Delay between rampup attempts. Initially uses standard, scales up to max. |
| 67 | const int kStandardRampUpDelayMs = 30 * 1000; |
| 68 | const int kMaxRampUpDelayMs = 120 * 1000; |
| 69 | // Expontential back-off factor, to prevent annoying up-down behaviour. |
| 70 | const double kRampUpBackoffFactor = 2.0; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 71 | |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame^] | 72 | // Minimum samples required to perform a check. |
| 73 | const size_t kMinFrameSampleCount = 30; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 74 | } // namespace |
| 75 | |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 76 | OveruseFrameDetector::OveruseFrameDetector(Clock* clock) |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 77 | : crit_(CriticalSectionWrapper::CreateCriticalSection()), |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 78 | observer_(NULL), |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 79 | clock_(clock), |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame^] | 80 | next_process_time_(clock_->TimeInMilliseconds()), |
| 81 | last_capture_time_(0), |
| 82 | last_overuse_time_(0), |
| 83 | checks_above_threshold_(0), |
| 84 | last_rampup_time_(0), |
| 85 | in_quick_rampup_(false), |
| 86 | current_rampup_delay_ms_(kStandardRampUpDelayMs) {} |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 87 | |
| 88 | OveruseFrameDetector::~OveruseFrameDetector() { |
| 89 | } |
| 90 | |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 91 | void OveruseFrameDetector::SetObserver(CpuOveruseObserver* observer) { |
| 92 | CriticalSectionScoped cs(crit_.get()); |
| 93 | observer_ = observer; |
| 94 | } |
| 95 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 96 | void OveruseFrameDetector::FrameCaptured() { |
| 97 | CriticalSectionScoped cs(crit_.get()); |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame^] | 98 | int64_t time = clock_->TimeInMilliseconds(); |
| 99 | if (last_capture_time_ != 0) { |
| 100 | capture_deltas_.AddSample(time - last_capture_time_); |
| 101 | } |
| 102 | last_capture_time_ = time; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | int32_t OveruseFrameDetector::TimeUntilNextProcess() { |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 106 | CriticalSectionScoped cs(crit_.get()); |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame^] | 107 | return next_process_time_ - clock_->TimeInMilliseconds(); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | int32_t OveruseFrameDetector::Process() { |
| 111 | CriticalSectionScoped cs(crit_.get()); |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame^] | 112 | |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 113 | int64_t now = clock_->TimeInMilliseconds(); |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame^] | 114 | |
| 115 | // Used to protect against Process() being called too often. |
| 116 | if (now < next_process_time_) |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 117 | return 0; |
| 118 | |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame^] | 119 | next_process_time_ = now + kProcessIntervalMs; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 120 | |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame^] | 121 | // Don't trigger overuse unless we've seen any frames |
| 122 | if (capture_deltas_.Samples() < kMinFrameSampleCount) |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 123 | return 0; |
| 124 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 125 | if (IsOverusing()) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame^] | 126 | // If the last thing we did was going up, and now have to back down, we need |
| 127 | // to check if this peak was short. If so we should back off to avoid going |
| 128 | // back and forth between this load, the system doesn't seem to handle it. |
| 129 | bool check_for_backoff = last_rampup_time_ > last_overuse_time_; |
| 130 | if (check_for_backoff) { |
| 131 | if (now - last_rampup_time_ < kStandardRampUpDelayMs) { |
| 132 | // Going up was not ok for very long, back off. |
| 133 | current_rampup_delay_ms_ *= kRampUpBackoffFactor; |
| 134 | if (current_rampup_delay_ms_ > kMaxRampUpDelayMs) |
| 135 | current_rampup_delay_ms_ = kMaxRampUpDelayMs; |
| 136 | } else { |
| 137 | // Not currently backing off, reset rampup delay. |
| 138 | current_rampup_delay_ms_ = kStandardRampUpDelayMs; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | last_overuse_time_ = now; |
| 143 | in_quick_rampup_ = false; |
| 144 | checks_above_threshold_ = 0; |
| 145 | |
| 146 | if (observer_ != NULL) |
| 147 | observer_->OveruseDetected(); |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 148 | } else if (IsUnderusing(now)) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame^] | 149 | last_rampup_time_ = now; |
| 150 | in_quick_rampup_ = true; |
| 151 | |
| 152 | if (observer_ != NULL) |
| 153 | observer_->NormalUsage(); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 154 | } |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame^] | 155 | |
| 156 | capture_deltas_.Reset(); |
| 157 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 158 | return 0; |
| 159 | } |
| 160 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 161 | bool OveruseFrameDetector::IsOverusing() { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame^] | 162 | WEBRTC_TRACE( |
| 163 | webrtc::kTraceInfo, |
| 164 | webrtc::kTraceVideo, |
| 165 | -1, |
| 166 | "Capture input stats: avg: %.2fms, std_dev: %.2fms (rampup delay: " |
| 167 | "%dms, overuse: >=%.2fms, " |
| 168 | "underuse: <%.2fms)", |
| 169 | capture_deltas_.Mean(), |
| 170 | capture_deltas_.StdDev(), |
| 171 | in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_, |
| 172 | kOveruseStdDevMs, |
| 173 | kNormalUseStdDevMs); |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 174 | |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame^] | 175 | if (capture_deltas_.StdDev() >= kOveruseStdDevMs) { |
| 176 | ++checks_above_threshold_; |
| 177 | } else { |
| 178 | checks_above_threshold_ = 0; |
| 179 | } |
| 180 | |
| 181 | return checks_above_threshold_ >= kConsecutiveChecksAboveThreshold; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | bool OveruseFrameDetector::IsUnderusing(int64_t time_now) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame^] | 185 | int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; |
| 186 | if (time_now < last_rampup_time_ + delay) |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 187 | return false; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 188 | |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame^] | 189 | return capture_deltas_.StdDev() < kNormalUseStdDevMs; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 190 | } |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 191 | } // namespace webrtc |