xref: /freebsd/contrib/cortex-strings/scripts/libplot.py (revision 8c4282b370bd66908b45b6a223226a9fc2b69d57)
1*09a53ad8SAndrew Turner"""Shared routines for the plotters."""
2*09a53ad8SAndrew Turner
3*09a53ad8SAndrew Turnerimport fileinput
4*09a53ad8SAndrew Turnerimport collections
5*09a53ad8SAndrew Turner
6*09a53ad8SAndrew TurnerRecord = collections.namedtuple('Record', 'variant function bytes loops src_alignment dst_alignment run_id elapsed rest')
7*09a53ad8SAndrew Turner
8*09a53ad8SAndrew Turner
9*09a53ad8SAndrew Turnerdef make_colours():
10*09a53ad8SAndrew Turner    return iter('m b g r c y k pink orange brown grey'.split())
11*09a53ad8SAndrew Turner
12*09a53ad8SAndrew Turnerdef parse_value(v):
13*09a53ad8SAndrew Turner    """Turn text into a primitive"""
14*09a53ad8SAndrew Turner    try:
15*09a53ad8SAndrew Turner        if '.' in v:
16*09a53ad8SAndrew Turner            return float(v)
17*09a53ad8SAndrew Turner        else:
18*09a53ad8SAndrew Turner            return int(v)
19*09a53ad8SAndrew Turner    except ValueError:
20*09a53ad8SAndrew Turner        return v
21*09a53ad8SAndrew Turner
22*09a53ad8SAndrew Turnerdef create_column_tuple(record, names):
23*09a53ad8SAndrew Turner    cols = [getattr(record, name) for name in names]
24*09a53ad8SAndrew Turner    return tuple(cols)
25*09a53ad8SAndrew Turner
26*09a53ad8SAndrew Turnerdef unique(records, name, prefer=''):
27*09a53ad8SAndrew Turner    """Return the unique values of a column in the records"""
28*09a53ad8SAndrew Turner    if type(name) == tuple:
29*09a53ad8SAndrew Turner        values = list(set(create_column_tuple(x, name) for x in records))
30*09a53ad8SAndrew Turner    else:
31*09a53ad8SAndrew Turner        values = list(set(getattr(x, name) for x in records))
32*09a53ad8SAndrew Turner
33*09a53ad8SAndrew Turner    if not values:
34*09a53ad8SAndrew Turner        return values
35*09a53ad8SAndrew Turner    elif type(values[0]) == str:
36*09a53ad8SAndrew Turner        return sorted(values, key=lambda x: '%-06d|%s' % (-prefer.find(x), x))
37*09a53ad8SAndrew Turner    else:
38*09a53ad8SAndrew Turner        return sorted(values)
39*09a53ad8SAndrew Turner
40*09a53ad8SAndrew Turnerdef alignments_equal(alignments):
41*09a53ad8SAndrew Turner    for alignment in alignments:
42*09a53ad8SAndrew Turner        if alignment[0] != alignment[1]:
43*09a53ad8SAndrew Turner            return False
44*09a53ad8SAndrew Turner    return True
45*09a53ad8SAndrew Turner
46*09a53ad8SAndrew Turnerdef parse_row(line):
47*09a53ad8SAndrew Turner    return Record(*[parse_value(y) for y in line.split(':')])
48*09a53ad8SAndrew Turner
49*09a53ad8SAndrew Turnerdef parse():
50*09a53ad8SAndrew Turner    """Parse a record file into named tuples, correcting for loop
51*09a53ad8SAndrew Turner    overhead along the way.
52*09a53ad8SAndrew Turner    """
53*09a53ad8SAndrew Turner    records = [parse_row(x) for x in fileinput.input()]
54*09a53ad8SAndrew Turner
55*09a53ad8SAndrew Turner    # Pull out any bounce values
56*09a53ad8SAndrew Turner    costs = {}
57*09a53ad8SAndrew Turner
58*09a53ad8SAndrew Turner    for record in [x for x in records if x.function=='bounce']:
59*09a53ad8SAndrew Turner        costs[(record.bytes, record.loops)] = record.elapsed
60*09a53ad8SAndrew Turner
61*09a53ad8SAndrew Turner    # Fix up all of the records for cost
62*09a53ad8SAndrew Turner    out = []
63*09a53ad8SAndrew Turner
64*09a53ad8SAndrew Turner    for record in records:
65*09a53ad8SAndrew Turner        if record.function == 'bounce':
66*09a53ad8SAndrew Turner            continue
67*09a53ad8SAndrew Turner
68*09a53ad8SAndrew Turner        cost = costs.get((record.bytes, record.loops), None)
69*09a53ad8SAndrew Turner
70*09a53ad8SAndrew Turner        if not cost:
71*09a53ad8SAndrew Turner            out.append(record)
72*09a53ad8SAndrew Turner        else:
73*09a53ad8SAndrew Turner            # Unfortunately you can't update a namedtuple...
74*09a53ad8SAndrew Turner            values = list(record)
75*09a53ad8SAndrew Turner            values[-2] -= cost
76*09a53ad8SAndrew Turner            out.append(Record(*values))
77*09a53ad8SAndrew Turner
78*09a53ad8SAndrew Turner    return out
79