summaryrefslogtreecommitdiff
path: root/tools/xenosci/local_runner.py
blob: 9f146a21d8b34467fe10362dc2baceb4fb85a280 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
# Copyright 2017 Justin Moore. All Rights Reserved.
import argparse
import glob
import itertools
from multiprocessing.dummy import Pool
import os
import time
import subprocess
import sys


def parallel_arg_type(value):
    ivalue = int(value)
    if ivalue < 1:
        raise argparse.ArgumentError(
            "%s is an invalid number of processes (>= 1)" % ivalue)

    return ivalue


def file_type(value):
    if not os.path.isfile(value):
        raise argparse.ArgumentError("%s is an invalid filename" % value)

    return value


def run_dumper(exe_path, input_name):
    start_time = time.perf_counter()

    p = subprocess.Popen([
        exe_path,
        input_name,
        '--log_file=stdout',
    ], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    p.wait()
    elapsed_time = time.perf_counter() - start_time
    print("%.3f seconds, code %2d (%s)" %
            (elapsed_time, p.returncode, input_name))


def main(argv):
    parser = argparse.ArgumentParser()
    parser.add_argument(
        'exepath', help='path to the trace dump executable', type=file_type)
    parser.add_argument('files', nargs='*')
    parser.add_argument('-j', type=parallel_arg_type,
                        help='number of parallel processes', default=1)
    args = parser.parse_args(argv[1:])
    exepath = args.exepath

    files = []
    for wildcard in args.files:
        files.extend(glob.glob(wildcard))

    if len(files) == 0:
        print("No input files found!")
        return 1

    # Test the executable first.
    valid = False
    try:
        p = subprocess.Popen([
            exepath,
            '--log_file=stdout',
        ], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        p.wait()
        if p.returncode == 5:
            # code 5 = invalid trace file / trace file unspecified
            valid = True
    except OSError:
        pass

    if not valid:
        print("Trace executable invalid!")
        return 1

    print("Processing...")
    pool = Pool(args.j)

    start_time = time.perf_counter()
    results = pool.starmap(run_dumper, zip(itertools.repeat(exepath), files))
    pool.close()
    pool.join()

    elapsed_time = time.perf_counter() - start_time
    print("entire runtime took %.3f seconds" % elapsed_time)
    return 0


if __name__ == '__main__':
    sys.exit(main(sys.argv))