#!/usr/bin/python3
#
# Utility to run outfoxed exercise
#
# Written for CS3214 Spring 2016 by G. Back (godmar@gmail.com)
# update Summer 2020
#

import re, os, sys, subprocess, operator, signal, resource, getopt

exe = "./outfoxed"
debug_exe = "./outfoxed.debug"
helgrind = ["valgrind", "--tool=helgrind", "--fair-sched=yes"]

ex_dir = os.path.dirname(os.path.realpath(__file__))
driver_cfile = "%s/outfoxed-driver.c" % (ex_dir)
sol_cfile = "outfoxed.c"

grade = False
# number of tests
NTEST = 100
# number of CPUs on which to run tests, used round-robin
cpus = [4, 8, 10, 20, 2]

try:
    DEFAULT_TIMEOUT = os.environ.get("OUTFOXED_TIMEOUT")
    DEFAULT_TIMEOUT = int(DEFAULT_TIMEOUT)
except:
    DEFAULT_TIMEOUT = 3

try:
    opts, args = getopt.getopt(sys.argv[1:], "gfn:s:", ["force", "ntests"])
except getopt.GetoptError as err:
    print (str(err))
    sys.exit(2)

force = False
for opt, arg in opts:
    if opt == "-f":
        force = True
    if opt == "-n":
        NTEST = int(arg)
    if opt == "-s":
        sol_cfile = arg
    if opt == '-g':
        grade = True

if len(args) < 1:
    print ("Usage: %s [-f] [-n <ntests>] [-s <solutionfile.c>] <gamecode>" % (sys.argv[0]))
    sys.exit()

if grade and NTEST != 300:
    print("Warning: for grading, we will run with -n 300", file=sys.stderr)

code = int(args[0])

# from http://stackoverflow.com/questions/2281850/timeout-function-if-it-takes-too-long-to-finish
from functools import wraps
import errno, signal

class TimeoutError(Exception):
    pass

def timeout(seconds=DEFAULT_TIMEOUT, error_message=os.strerror(errno.ETIME)):
    def decorator(func):
        def _handle_timeout(signum, frame):
            raise TimeoutError(error_message)

        def wrapper(*args, **kwargs):
            signal.signal(signal.SIGALRM, _handle_timeout)
            signal.alarm(seconds)
            try:
                result = func(*args, **kwargs)
            finally:
                signal.alarm(0)
            return result

        return wraps(func)(wrapper)

    return decorator

if not os.access(sol_cfile, os.R_OK):
    raise Exception(sol_cfile + " not found in the current directory. ")

print ("Compiling...")
compile_cmd = "gcc -I%s -pthread -O2 -Wall -Werror %s %s -o %s" % (ex_dir, sol_cfile, driver_cfile, exe)

if os.system(compile_cmd):
    raise Exception("Compile failed, run\n\n%s\n\nto see why" % compile_cmd)

if not os.access(exe, os.X_OK):
    raise Exception(exe + " did not build")

print ("Ok.")

compile_c_cmd = "gcc -I%s -pthread -O2 -fsanitize=undefined -Wall -Werror -c %s %s" % (ex_dir, sol_cfile, driver_cfile)
if os.system(compile_c_cmd):
    raise Exception("Compile failed, run\n\n%s\n\nto see why" % compile_c_cmd)

sol_ofile = sol_cfile.replace(".c", ".o")
allowed_symbols = ['setup_play', 
    'new_suspect_thread',\
    'chicken_detective_thread',\
    'hat_thread',\
    'umbrella_thread',\
    'glasses_thread',\
    'monocle_thread',\
    'flower_thread',\
    'coat_thread',\
    'cane_thread',\
    'gloves_thread',\
    'watch_thread',\
    'necklace_thread',\
    'briefcase_thread',\
    'scarf_thread']

def check_software_engineering(objfile, allowedsymbols):
    hex = r"[0-9A-Fa-f]{8,16}"
    print ("Performing some checks that %s conforms to accepted software engineering practice..." % objfile)

    symbols = subprocess.Popen(["nm", objfile], stdout=subprocess.PIPE)\
        .communicate()[0].decode('utf-8').split("\n")

    for sym in symbols:
        if sym == "" or re.match(r'\s+U (\S+)', sym):
            continue

        m = re.match(hex + r" (\S) (\S+)", sym)
        if not m:
            raise Exception("unexpected line in nm:\n" + sym)

        if m.group(1).islower():    # local symbols are fine
            continue

        if m.group(1) == "T":
            if m.group(2) in allowedsymbols:
                continue
            
            raise Exception(("%s defines global symbol for function '%s'\n"
                +"allowed functions are: %s") % (objfile, m.group(2), str(allowedsymbols)))

        raise Exception(("%s must not define any global symbols"
                +", but you define: %s") % (objfile, m.group(2)))

    print ("Ok.")

check_software_engineering(sol_ofile, allowed_symbols)

# test scenarios (#threads, #tasks)

def check_one_run(sp, output):

    if sp.returncode < 0:
        signames = dict((k, v) for v, k in signal.__dict__.items() if v.startswith('SIG'))
        signum = -sp.returncode
        print ("Program terminated with signal %d (%s)\n" % (signum, signames[signum]))
        lines = output.split("\n")
        if len(lines) > 1:
            print ("Last line output was:")
            print (lines[-2])

        return False

    expected = r'^A chicken detective has caught the thief!  It was: \S+$'
    lines = output.split("\n")

    if len(lines) != 53:
        print ("Program did not output the expected number of lines (53)")
        print ("Check that you do not have additional output to stdout.")
        print ("Keep output to stderr to a minimum.")
        return False

    if not all(re.match(expected, line) for line in output.split("\n")[2:-1]):
        print ("Output did not match expected format.")
        return False

    return True

@timeout(seconds=DEFAULT_TIMEOUT)
def run_one(n):
    """
    Run once using CPU 0...n-1
    """
    cmd = ["taskset", "-c", ",".join(map(str, range(n))), exe, str(code)]
    #print " ".join(cmd)
    try:
      sp = subprocess.Popen(cmd, stdout=subprocess.PIPE)
      output = sp.communicate()[0].decode('utf-8')

      return check_one_run(sp, output)
    finally:
      if sp:
        try:
          sp.kill()
        except:
          pass

@timeout(seconds=20)
def run_on_one_cpu():
    """
    Run on one CPU to check for busy-waiting.
    """
    print ("I will now run your code on one CPU")
    before = resource.getrusage(resource.RUSAGE_CHILDREN);
    sp = subprocess.Popen(["taskset", "-c", "1", exe, str(code)], stdout=subprocess.PIPE)
    output = sp.communicate()[0].decode('utf-8')
    after = resource.getrusage(resource.RUSAGE_CHILDREN);

    if not check_one_run(sp, output):
        return False

    cputimeused = after.ru_utime - before.ru_utime
    print ("Your program used %f seconds of CPU time when run on one CPU." % (cputimeused))
    if cputimeused > 0.1:
        print ("It should not need more than a small fraction of a second.")
        print ("Check that your program is not busy-waiting.")
        return False
    else:
        print ("Good.")
        return True

successes = 0
cpus = [4, 8, 10, 20, 2]
for i in range(NTEST):
    print (".", end='')
    sys.stdout.flush()
    try:
        if run_one(cpus[i%len(cpus)]):
            successes += 1
    except TimeoutError:
        print ("Time-out", file=sys.stderr)

print ("done")

if successes == NTEST:
    print ("Congrats, your code ran successfully %d out of %d times." % (successes, NTEST))
    sys.exit()

print ("Your code ran successfully only %d out of %d times." % (successes, NTEST))
succprob = successes / NTEST
print ("This will result in substantial deductions.  Consider code that runs correctly only %.2f%% of the time. " % (succprob * 100.0)) 
for nr in [10, 20, 50, 100, 200]:
    print ("If it were run %d times, it would fail at least once with a probability of %.4f" % (nr, 1-succprob**nr))

if grade:
  sys.exit()
print ("Compiling debug version...")
compile_cmd = "gcc -I%s -pthread -g -Wall -Werror %s %s -o %s" % (ex_dir, sol_cfile, driver_cfile, debug_exe)

if os.system(compile_cmd):
    raise Exception("Compile failed, run\n\n%s\n\nto see why" % compile_cmd)

if not os.access(debug_exe, os.X_OK):
    raise Exception(debug_exe + " did not build")

print ("Ok.")
print ("Checking for race conditions using helgrind")

cmd = helgrind + [debug_exe, str(code)]
print ("Running", " ".join(cmd), "...", end='')
sp = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stderr = sp.communicate()[1].decode('utf-8')
if not re.search("ERROR SUMMARY: 0 errors from 0 contexts", stderr):
    raise Exception("Your program contains race conditions:\n" + stderr)

if not run_on_one_cpu():
    print ("This will result in substantial deductions.")
    print ("Excessive CPU usage (even if only occasionally) means that your code is not signaling properly")
    print ("Run with -f to continue (for now) anyway.")
    if not force:
        sys.exit(0)

