aboutsummaryrefslogtreecommitdiffstats
path: root/parse/ft.py
blob: 1f0532356ea7b76bc0f40d76facb2d59c21b1e37 (plain) (blame)
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
import config.config as conf
import numpy as np
import os
import re
import shutil as sh
import sys
import subprocess

from point import Measurement,Type

FT_SPLIT_NAME  = "overhead={}.bin"
FT_SORTED_NAME = "sorted-ft.bin"
FT_ERR_NAME    = "err-ft"

def parse_overhead(result, overhead_bin, overhead, cycles, out_dir, err_file):
    '''Store statistics for @overhead in @overhead_bin into @result.'''
    ovh_fname = "{}/{}".format(out_dir, FT_SPLIT_NAME).format(overhead)

    if os.path.exists(ovh_fname):
        os.remove(ovh_fname)
    ovh_file = open(ovh_fname, 'w')

    # Extract matching overhead events into a seperate file
    cmd  = [conf.BINS["ftsplit"], "-r", "-b", overhead, overhead_bin]
    ret  = subprocess.call(cmd, cwd=out_dir, stderr=err_file, stdout=ovh_file)
    size = os.stat(ovh_fname).st_size

    if ret:
        raise Exception("Failed (%d) with command: %s" % (ret, " ".join(cmd)))
    if not size:
        os.remove(ovh_fname)
    if size and not ret:
        # Map and sort file for stats
        data = np.memmap(ovh_fname, dtype="float32", mode='c')
        data /= float(cycles) # Scale for processor speed
        data.sort()

        m = Measurement("%s-%s" % (overhead_bin, overhead))
        m[Type.Max] = data[-1]
        m[Type.Avg] = np.mean(data)
        m[Type.Min] = data[0]
        m[Type.Var] = np.var(data)

        result[overhead] = m

        os.remove(ovh_fname)

def sort_ft(ft_file, err_file, out_dir):
    '''Create and return file with sorted overheads from @ft_file.'''
    out_fname = "{}/{}".format(out_dir, FT_SORTED_NAME)

    # Sort happens in-place
    sh.copyfile(ft_file, out_fname)
    cmd = [conf.BINS['ftsort'], out_fname]

    ret = subprocess.call(cmd, cwd=out_dir, stderr=err_file, stdout=err_file)
    if ret:
        raise Exception("Sort failed (%d) with command: %s" % (ret, " ".join(cmd)))

    return out_fname

def extract_ft_data(result, data_dir, work_dir, cycles):
    data_dir = os.path.abspath(data_dir)
    work_dir = os.path.abspath(work_dir)

    freg = conf.FILES['ft_matches'] + "$"
    bins = [f for f in os.listdir(data_dir) if re.match(freg, f)]

    if not len(bins):
        return False

    bin_file = "{}/{}".format(data_dir, bins[0])
    if not os.path.getsize(bin_file):
        return False

    with open("%s/%s" % (work_dir, FT_ERR_NAME), 'w') as err_file:
        sorted_bin = sort_ft(bin_file, err_file, work_dir)

        for event in conf.OVH_BASE_EVENTS:
            parse_overhead(result, sorted_bin, event, cycles,
                           work_dir, err_file)

        os.remove(sorted_bin)

    return True