#!/usr/bin/env python3
# Copyright (c) 2007, 2009, 2010, 2011 Arista Networks, Inc.  All rights reserved.
# Arista Networks, Inc. Confidential and Proprietary.

# pylint: disable=bad-indentation

import ManagedSubprocess, time, os, Tac, sys

print( "You are running the new improved cpustress program" )
print( "my pid is ", os.getpid() )

# This runs a bunch of subsystem stress tests in parallel.  They
# are all run with ManagedSubprocess so they exit when the parent
# script does.  The following tests are run:
#  1) Two cpu stress tests (burnK7)
#  2) A memory victim bit write test
#  3) A filesystem test on /mnt/flash and one on /mnt/usb1
#  4) A management ethernet traffic test, sending packets out ma1 and ma2
#  5) A test of reading from pci space (The SCD)
#  6) A test of the leds

os.environ["PATH"] = ".:/mnt/flash:" + os.environ["PATH"]

fp = open( "/dev/null" ) # pylint: disable=consider-using-with
os.dup2( fp.fileno(), 0 )

def fork():
   p = ManagedSubprocess.Popen()
   return p

def execlp( *cmd ):
   print( "Running", cmd )
   os.execlp( cmd[0], *cmd )

def background( *cmd ):
   p = ManagedSubprocess.Popen()
   if p.pid == 0:
      execlp( *cmd )
   return p

def memtest(bit):
   while True:
      print( "Doing a memory victim test on bit", bit )
      args = ["testmem","write",str(bit),"4000000","20000"]
      Tac.run( args )
      print( "ran", args )

def fstest( where ):
   print( "Testing files in ", where )
   while True:
      args = ["testfile", "-d", where, "-i", "1000"]
      Tac.run( args )
      print( "ran", args )

def seepromTest():
   print( "testing the mac seeprom" )
   while True:
      args = ["macseeprom","test","-u","-i","50"]
      Tac.run( args )
      print( "ran", args )

def ethTest( intf ):
   args = ["ethxmit","-s","64",intf,"-D","01:00:00:00:00:99","-b","100",
           "--sleep",".1","-c"]
   print( "running ", args )
   while True:
      Tac.run( args )
      print( "ran", args )

def pciTest( filename ):
    print( "hammering the pci file ", filename )
    while True:
      args = ["testmmapfile", filename, "10000000", "128"]
      Tac.run( args )
      print( "ran testmmapfile", args )

start = Tac.now()

processes = []

if len( sys.argv ) == 1:
   tests = ['cpu','cpu','usb1','flash','ma1','ma2','pci','mem','leds']
elif len( sys.argv ) == 2:
   tests = sys.argv[1].split(",")
else:
   print( "You must specify a test list consisting of a comma-separated list of" )
   print( "some subset of these strings:" )
   print( " cpu, usb1, flash, seeprom, ma1, ma2, pci, mem" )
   print( "test names can be repeated." )
   sys.exit(1)

for c in tests:
   if c == "cpu":
      processes.append( background( "burnK7" ) )
   elif c == "leds":
      processes.append( background( "leds" ) )
   elif c == "usb1":
      p3 = fork()
      if not p3.pid:
        fstest( "/mnt/usb1" )
      processes.append( p3 )
   elif c == "flash":
      p3 = fork()
      if not p3.pid:
        fstest( "/mnt/flash" )
      processes.append( p3 )
   elif c == "seeprom":
      p3 = fork()
      if not p3.pid:
        seepromTest()
      processes.append( p3 )
   elif c == "ma1":
      p3 = fork()
      if not p3.pid:
        ethTest( "ma1" )
      processes.append( p3 )
   elif c == "ma2":
      p3 = fork()
      if not p3.pid:
        ethTest( "ma2" )
      processes.append( p3 )
   elif c == "pci":
      p3 = fork()
      if not p3.pid:
        pciTest( "/sys/devices/pci0000:00/0000:00:06.0/0000:01:06.0/resource0" )
      processes.append( p3 )
   elif c == "mem":
      p3 = fork()
      if not p3.pid:
        memtest( 32 )
      processes.append( p3 )
   else:
      print( "unknown test type:", c )
      sys.exit(1)

try:
   time.sleep( 10000000 )
except KeyboardInterrupt:
   print( "exiting after", Tac.now() - start, "seconds" )
