#!/usr/bin/env ruby

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

require "#{LKP_SRC}/lib/statistics"
require "#{LKP_SRC}/lib/string_ext"
require "#{LKP_SRC}/lib/array_ext"
require "#{LKP_SRC}/lib/tests/stats"

# The result line in input is in format:
# [time] $type-torture:--- End of test: $result: arg=value arg=value ...
#
# $type and $result are essential for a test.
# And for now, we care about the value of arg onoff_interval
# which represents doing cpuhotplugs or not.
#
# Now, there are three results here, 'RCU_HOTPLUG' and
# 'FAILURE' both represent the test failed.

# Input example:
# [  317.442785] rcu-torture: Free-Block Circulation:  17263 17263 17262 17261 17259 17258 17256 17253 17252 17251 0
# [  317.445385] rcu-torture:--- End of test: RCU_HOTPLUG: nreaders=1 nfakewriters=4 stat_interval=60 verbose=1 test_no_idle_hz=1 shuffle_interval=3 stutter=5 irqreader=1
# fqs_dura tion=0 fqs_holdoff=0 fqs_stutter=3 test_boost=1/0 test_boost_interval=7 test_boost_duration=4 shutdown_secs=0 stall_cpu=0 stall_cpu_holdoff=10 stall_cpu_irqsoff=0
# n_barrier_cbs=0 onoff_interval=0 onoff_holdoff=0
#
# [  324.961545] tasks-torture:--- End of test: SUCCESS: nreaders=1 nfakewriters=4 stat_interval=60 verbose=1 test_no_idle_hz=1 shuffle_interval=3 stutter=5 irqreader=1
# fqs_duration=0 fqs_holdoff=0 fqs_stutter=3 test_boost=1/0 test_boost_interval=7 test_boost_duration=4 shutdown_secs=0 stall_cpu=0 stall_cpu_holdoff=10 stall_cpu_irqsoff=0
# stall_cpu_block=0 n_barrier_cbs=0 onoff_interval=0 onoff_holdoff=0 read_exit_delay=13 read_exit_burst=16 nocbs_nthreads=0 nocbs_toggle=1000

result = 'unknown'
type = 'unknown'

stats = LKP::Stats.new

while (line = STDIN.gets)
  line = line.resolve_invalid_bytes

  case line
  when /^\[.*\] ([A-Za-z_]+)-torture.*Start of test:/
    type = $1
  when /^\[.*\] #{type}-torture.*End of test: (.*):.*onoff_interval=([0-9]+).*/
    result = $1
    if $2.eql? '0'
      stats.add "#{type}", "#{result}"
    else
      stats.add "cpuhotplug-#{type}", "#{result}"
    end
  end
end

stats.add "#{type}", "#{result}" if stats.empty?
stats.dump('success' => 'pass', 'failure' => 'fail')
