Method: OpenC3::Throttle#throttle_sleep

Defined in:
lib/openc3/utilities/throttle.rb

#throttle_sleepObject



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