#!/usr/bin/env ruby

LKP_SRC = ENV['LKP_SRC'] || File.dirname(File.dirname(File.realpath($PROGRAM_NAME)))

require "#{LKP_SRC}/lib/string_ext"

def show_dmesg_times
  dmesg = "#{ENV['RESULT_ROOT']}/kmsg"
  return unless File.exist? dmesg

  dhcp = false
  smp_start = false

  File.open(dmesg).each do |line|
    line = line.resolve_invalid_bytes
    case line
    when /\[ *(\d+\.\d+)\] Sending DHCP requests/
      unless dhcp
        puts 'dhcp: ' + $1
        dhcp = true
      end
    when /\[ *(\d+\.\d+)\] x86: Booting SMP configuration:/
      smp_start = $1.to_f
    when /\[ *(\d+\.\d+)\] smp: Brought up \d+ nodes, \d+ CPUs$/
      printf "smp_boot: %g\n", $1.to_f - smp_start if smp_start
    when /\[ *(\d+\.\d+)\] Freeing unused kernel memory:/
      puts 'kernel_boot: ' + $1
      break
    end
  end
end
show_dmesg_times

if (line = STDIN.gets)
  boot, idle = line.split
  puts 'boot: ' + boot
  puts 'idle: ' + idle
end
