auto_tune.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env ruby
  2. # frozen_string_literal: true
  3. # Instantiate about one process per X MiB of available memory, scaling up to as
  4. # close to MAX_THREADS as possible while observing an upper bound based on the
  5. # number of virtual/logical CPUs. If there are fewer processes than
  6. # MAX_THREADS, add threads per process to reach MAX_THREADS.
  7. require 'etc'
  8. KB_PER_WORKER = 128 * 1_024 # average of peak PSS of single-threaded processes (watch smem -k)
  9. MIN_WORKERS = 15
  10. MAX_WORKERS_PER_VCPU = 1.25 # virtual/logical
  11. MIN_THREADS_PER_WORKER = 1
  12. MAX_THREADS = Integer(ENV['MAX_CONCURRENCY'] || 256)
  13. def meminfo(arg)
  14. File.open('/proc/meminfo') do |f|
  15. f.each_line do |line|
  16. key, value = line.split(/:\s+/)
  17. return value.split(/\s+/).first.to_i if key == arg
  18. end
  19. end
  20. raise "Unable to find `#{arg}' in /proc/meminfo!"
  21. end
  22. def auto_tune
  23. avail_mem = meminfo('MemAvailable') * 0.8 - MAX_THREADS * 1_024
  24. workers = [
  25. [(1.0 * avail_mem / KB_PER_WORKER).floor, MIN_WORKERS].max,
  26. [(Etc.nprocessors * MAX_WORKERS_PER_VCPU).ceil, MIN_WORKERS].max
  27. ].min
  28. threads_per_worker = [
  29. workers < MAX_THREADS ? (1.0 * MAX_THREADS / workers).ceil : -Float::INFINITY,
  30. MIN_THREADS_PER_WORKER
  31. ].max
  32. [workers, threads_per_worker]
  33. end
  34. p auto_tune if $PROGRAM_NAME == __FILE__