# File lib/openshift-origin-node/utils/cgroups/monitored_gear.rb, line 164 def delay @@delay ||= (@@_delay || intervals.min.to_f / 2) end
# File lib/openshift-origin-node/utils/cgroups/monitored_gear.rb, line 153 def delay=(delay) @@delay = delay # Store this explicit delay so that it doesn't get overwritten @@_delay = delay @@max = nil end
# File lib/openshift-origin-node/utils/cgroups/monitored_gear.rb, line 168 def intervals @@intervals end
# File lib/openshift-origin-node/utils/cgroups/monitored_gear.rb, line 147 def intervals=(intervals) @@intervals = intervals @@delay = nil @@max = nil end
# File lib/openshift-origin-node/utils/cgroups/monitored_gear.rb, line 160 def max @@max ||= intervals.max + (delay * 2) end
# File lib/openshift-origin-node/utils/cgroups/monitored_gear.rb, line 54 def initialize(uuid) @gear = OpenShift::Runtime::Utils::Cgroups.new(uuid) @times = {} end
# File lib/openshift-origin-node/utils/cgroups/monitored_gear.rb, line 77 def age (newest - oldest) rescue 0.0 end
For each value in the hash, calculate the difference between elements
# File lib/openshift-origin-node/utils/cgroups/monitored_gear.rb, line 179 def calculate_differences(values) values.inject({}){|h,(k,v)| h[k] = v.each_cons(2).map { |a,b| b-a }; h} end
Collapse multiple hashes into a single hash with the values from corresponding keys combined
# File lib/openshift-origin-node/utils/cgroups/monitored_gear.rb, line 174 def collapse_hashes(hashes) hashes.inject(Hash.new{|h,k| h[k] = []}){|h,vals| vals.each{|k,v| h[k] << v}; h} end
Calculate the elapsed usage as a percentage of the max for that time period Doing it this way allows us to calculate the percentage based on the quota and period at the time of each measurement in case it changes
# File lib/openshift-origin-node/utils/cgroups/monitored_gear.rb, line 113 def elapsed_usage(hashes) # These are keys we don't want to include in our calculations util_keys = [:cfs_quota_us, :nr_periods, :cfs_period_us] # Collect all of the values into a single hash values = collapse_hashes(hashes) # Calculate the differences across values differences = calculate_differences(values) # Disregard the first quota, so we can align with the differences (quotas = values[:cfs_quota_us]).shift periods = differences[:nr_periods] # Find the max utils by multiplying the quotas and number of elapsed periods quota_periods = quotas.mult(periods) differences.inject({}) do |h,(k,vals)| unless util_keys.include?(k) || vals.empty? # Calculate the values as a percentage of the max utilization for a period percentage = vals.divide(quota_periods)#.mult(100) per_period = vals.divide(periods) { nil => vals.average, "per_period" => per_period.average.round(3), "percent" => percentage.average.round(3), }.each do |k2,v| key = [k,k2].compact.join('_').to_sym h[key] = v end end h end end
# File lib/openshift-origin-node/utils/cgroups/monitored_gear.rb, line 73 def newest times.max.first end
# File lib/openshift-origin-node/utils/cgroups/monitored_gear.rb, line 69 def oldest times.min.first end
Get the current values and remove any expired values Then make sure our intervals are updated
# File lib/openshift-origin-node/utils/cgroups/monitored_gear.rb, line 61 def update(vals) time = Time.now @times[time] = vals @utilization = nil cutoff = time - MonitoredGear.max @times.delete_if{|k,v| k < cutoff } end
Update the elapsed intervals
# File lib/openshift-origin-node/utils/cgroups/monitored_gear.rb, line 86 def update_utilization utilization = {} unless times.empty? cur = newest # Go through each interval we want threads = MonitoredGear.intervals.map do |i| Thread.new do if age.to_i >= i # Find any values at or after our cutoff vals = times.select{|k,v| k >= (cur - i)} # Make sure we have enough sample points for this dataset if vals.length > 0 # Calculate the elapsed usage for our values utilization[i] = elapsed_usage(vals.values) end end end end threads.map(&:join) end Hash[utilization.sort] end
# File lib/openshift-origin-node/utils/cgroups/monitored_gear.rb, line 81 def utilization @utilization ||= update_utilization end