#!/bin/env ruby

$: << File.join(File.dirname(__FILE__), '..', 'lib')
require 'coo-coo/cuda'

require 'ostruct'
options = OpenStruct.new
options.follow = ARGV[0] == '-f'
options.timeout = (ARGV[1] || 1).to_f

unless options.follow
  puts("Device\tBytes Free\tBytes Total\tPercent")
end

follow = true
$stdout.sync = true

while follow
  follow = options.follow
  CooCoo::CUDA::Runtime.device_count.times do |device|
    CooCoo::CUDA::Runtime.set_device(device)
    free, total = CooCoo::CUDA.memory_info
    $stdout.puts("%i\t%i\t%i\t%.3f" % [ device, free, total, (100.0 * free / total.to_f) ])
  end

  $stdout.flush
  sleep(options.timeout)
end
