1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include <cmath> 6 7 #include "cc/scheduler/rolling_time_delta_history.h" 8 9 namespace cc { 10 11 RollingTimeDeltaHistory::RollingTimeDeltaHistory(size_t max_size) 12 : max_size_(max_size) { 13 } 14 15 RollingTimeDeltaHistory::~RollingTimeDeltaHistory() { 16 } 17 18 void RollingTimeDeltaHistory::InsertSample(base::TimeDelta time) { 19 if (max_size_ == 0) 20 return; 21 22 if (sample_set_.size() == max_size_) { 23 sample_set_.erase(chronological_sample_deque_.front()); 24 chronological_sample_deque_.pop_front(); 25 } 26 27 TimeDeltaMultiset::iterator it = sample_set_.insert(time); 28 chronological_sample_deque_.push_back(it); 29 } 30 31 void RollingTimeDeltaHistory::Clear() { 32 chronological_sample_deque_.clear(); 33 sample_set_.clear(); 34 } 35 36 base::TimeDelta RollingTimeDeltaHistory::Percentile(double percent) const { 37 if (sample_set_.size() == 0) 38 return base::TimeDelta(); 39 40 double fraction = percent / 100.0; 41 42 if (fraction <= 0.0) 43 return *(sample_set_.begin()); 44 45 if (fraction >= 1.0) 46 return *(sample_set_.rbegin()); 47 48 size_t num_smaller_samples = 49 static_cast<size_t>(std::ceil(fraction * sample_set_.size())) - 1; 50 51 if (num_smaller_samples > sample_set_.size() / 2) { 52 size_t num_larger_samples = sample_set_.size() - num_smaller_samples - 1; 53 TimeDeltaMultiset::const_reverse_iterator it = sample_set_.rbegin(); 54 for (size_t i = 0; i < num_larger_samples; i++) 55 it++; 56 return *it; 57 } 58 59 TimeDeltaMultiset::const_iterator it = sample_set_.begin(); 60 for (size_t i = 0; i < num_smaller_samples; i++) 61 it++; 62 return *it; 63 } 64 65 } // namespace cc 66