Class: OpenC3::Throttle
Constant Summary collapse
- MIN_SLEEP_SECONDS =
0.001
- MAX_SLEEP_SECONDS =
0.100
Instance Attribute Summary collapse
-
#reset_time ⇒ Object
readonly
Returns the value of attribute reset_time.
-
#total_sleep_time ⇒ Object
readonly
Returns the value of attribute total_sleep_time.
Instance Method Summary collapse
-
#initialize(max_cpu_utilization) ⇒ Throttle
constructor
A new instance of Throttle.
- #reset ⇒ Object
- #throttle_sleep ⇒ Object
Constructor Details
#initialize(max_cpu_utilization) ⇒ Throttle
Returns a new instance of Throttle.
39 40 41 42 43 44 |
# File 'lib/openc3/utilities/throttle.rb', line 39 def initialize(max_cpu_utilization) @max_cpu_utilization = Float(max_cpu_utilization) raise ArgumentError "max_cpu_utilization must be between 0.0 and 100.0" if @max_cpu_utilization > 100.0 or @max_cpu_utilization < 0.0 @max_cpu_utilization /= 100.0 # Normalize reset() end |
Instance Attribute Details
#reset_time ⇒ Object (readonly)
Returns the value of attribute reset_time.
35 36 37 |
# File 'lib/openc3/utilities/throttle.rb', line 35 def reset_time @reset_time end |
#total_sleep_time ⇒ Object (readonly)
Returns the value of attribute total_sleep_time.
36 37 38 |
# File 'lib/openc3/utilities/throttle.rb', line 36 def total_sleep_time @total_sleep_time end |
Instance Method Details
#reset ⇒ Object
46 47 48 49 |
# File 'lib/openc3/utilities/throttle.rb', line 46 def reset @reset_time = Time.now @total_sleep_time = 0 end |
#throttle_sleep ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/openc3/utilities/throttle.rb', line 51 def throttle_sleep return if @max_cpu_utilization >= 1.0 total_time = Time.now - @reset_time if total_time > 0 cpu_utilization = 1.0 - (@total_sleep_time / total_time) if cpu_utilization > @max_cpu_utilization # Need to throttle # max_cpu_utilization = sleep_time + total_sleep_time # ---------------------------- # total_time # # (max_cpu_utilization * total_time) = sleep_time + total_sleep_time # # sleep_time = (max_cpu_utilization * total_time) - total_sleep_time # sleep_time = (@max_cpu_utilization * total_time) - @total_sleep_time if sleep_time > MIN_SLEEP_SECONDS sleep_time = MAX_SLEEP_SECONDS if sleep_time > MAX_SLEEP_SECONDS sleep(sleep_time) @total_sleep_time += sleep_time end end end end |