Lego2012
1/17/2017 - 5:50 PM

Limit VTDecoderXPCService using cputhrottle

Limit VTDecoderXPCService using cputhrottle

#!/usr/bin/env ruby

# Configure the below
process_name = 'VTDecoderXPCService'
cpu_percentage = 10

raise 'Must run as root' unless Process.uid == 0

restricted_pids = []
throttle_pids = []

begin
  loop do
    pids = `pgrep "#{process_name}"`

    if pids
      pids = pids.split("\n")
      pids.each do |pid|
        next if restricted_pids.include? pid
        puts "Restricting process ID #{pid}"
        restricted_pids << pid
        throttle_pids << Process.spawn("/usr/local/bin/cputhrottle #{pid} #{cpu_percentage}")
      end
    end

    sleep 1
  end
rescue SystemExit, Interrupt
  throttle_pids.each do |pid|
    begin
      puts "Killing cputhrottle process #{pid}"
      Process.kill('QUIT', pid)
    rescue Errno::ESRCH
      # process exited normally
    end
  end
  raise
end