aboutsummaryrefslogtreecommitdiffstats
path: root/plot_pm2.py
blob: 866b4b2ea1f72df5127f7c69ea867e2909238261 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python
from os.path  import splitext, basename
from optparse import make_option as o
from tempfile import NamedTemporaryFile as Tmp

import numpy as np
from util import load_csv_file, select

import stats
import defapp

from plot     import decode
from gnuplot  import gnuplot, FORMATS




MACHINE_TOPOLOGY = {
    'jupiter-cs' : (4, [('preempt', lambda x, y: x == y),
                        ('mem', lambda x, y: x != y)]),

    # Socket0  Socket1  Socket2  Socket3
    # ------   -------  -------  -------
    # | 0, 4|  | 1, 5|  | 2, 6|  | 3, 7|
    # | 8,12|  | 9,13|  |10,14|  |11,15|
    # |16,20|  |17,21|  |18,22|  |19,23|
    # -------  -------  -------  -------
    'ludwig.cs.unc.edu' : (24, [('preempt', lambda x, y: x == y),
                                ('l2',
                                 lambda x, y: abs(y - x) == 4),
                                ('l3',
                                 lambda x, y:
                                     abs(y - x) > 4 and \
                                     abs(y - x) % 4 == 0),
                                ('mem', lambda x, y: abs(y - x) % 4 != 0)])
}

PMO_PARAM = {
    'wss'    : 'WSS',
    'host'   : 'host',
    'wcycle' : 'write-cycle'
}

PMO_MEM = {
    'mem'     : 'a migration through main memory',
    'l3'      : 'a migration through a shared L3 cache',
    'l2'      : 'a migration through a shared L2 cache',
    'preempt' : 'a preemption',
    'all'     : 'either a migration or preemption',
}

PMO_SUBPLOTS = [
    # x, y, y-delta, split according to mem-hierarchy?
    (0,  6, None, False),
    (0,  7, None, False),
    (0,  8, None, False),
    (0,  9, None, False),
    (0, 10, None,  True),
    (3, 10, None,  True),
    (0, 10,    9,  True),
    (3, 10,    9,  True),
]

PMO_COL_LABEL = [('measurement', 'sample', 'index'),
                 ('write cycles', 'wcycle', 'every nth access'),
                 ('WSS', 'wcc', 'kilobytes'),
                 ('suspension length', 'delay', 'microseconds'),
                 ('CPU (preempted on)', 'from', 'processor'),
                 ('CPU (resumed on)', 'to', 'processor'),
                 ('cold access', 'cold', 'cycles'),
                 ('first hot access', 'hot1', 'cycles'),
                 ('second hot access', 'hot2', 'cycles'),
                 ('third hot access', 'hot3', 'cycles'),
                 ('access after resuming', 'after', 'cycles')
                 ]

PMO_FROM_CPU = 4
PMO_TO_CPU   = 5

options = [
    o('-f', '--format', action='store', dest='format', type='choice',
      choices=FORMATS, help='output format'),
    o(None, '--paper', action='store_true', dest='paper'),
    o(None, '--wide', action='store_true', dest='wide'),
    o(None, '--split', action='store_true', dest='split'),
    o(None, '--extend', action='store', type='float', dest='extend'),
    ]

defaults = {
    'format' : 'show',
    'paper'  : False,
    'split'  : False,
    'wide'   : False,
    'extend' : 1.5,
    }

def extract_cols(data, xcol, ycol1, ycol2, cast=int, cpu_filter=lambda x, y: True):
    def matching_cpus(row):
        return cpu_filter(row[PMO_FROM_CPU], row[PMO_TO_CPU])
    rows = select(matching_cpus, data)
    if not (ycol2 is None):
        rows[:,ycol1] -= rows[:,ycol2]
    return rows[:,(xcol, ycol1)]

class CyclePlotter(defapp.App):
    def __init__(self):
        defapp.App.__init__(self, options, defaults, no_std_opts=True)

    def setup_pmo_graphs(self, datafile, conf):
        host = conf['host']
        if host in MACHINE_TOPOLOGY:
            (cpus, hier) = MACHINE_TOPOLOGY[host]
            plots = []
            data = load_csv_file(datafile, dtype=int)
            for (xcol, ycol, yminus, by_mem_hierarchy) in PMO_SUBPLOTS:
                sub = [('all', lambda x, y: True)]
                if by_mem_hierarchy:
                    sub += hier
                for tag, test in sub:
                    tmp    = Tmp()
                    rows = extract_cols(data,
                                        xcol, ycol, yminus,
                                        cpu_filter=test)
                    for row in rows:
                        tmp.write("%s, %s\n" % (row[0], row[1]))
                    tmp.flush()
                    plots.append((tmp, xcol, ycol, yminus, tag, rows))
            return plots
        else:
            self.err('Unkown host: %s' % host)
            return None

    def plot_preempt_migrate(self, datafile, name, conf):
        plots = self.setup_pmo_graphs(datafile, conf)
        if plots is None:
            print "Skipping %s..." % datafile
            return
        for (tmp, xcol, ycol, yminus, tag, rows) in plots:
            xtag = PMO_COL_LABEL[xcol][1]
            ytag = PMO_COL_LABEL[ycol][1]
            dtag = "-delta-%s" % PMO_COL_LABEL[yminus][1] if not yminus is None else ""
            figname = "%s_%s%s-vs-%s_%s" % (name, ytag, dtag, xtag, tag)
            xunit = PMO_COL_LABEL[xcol][2]
            yunit = PMO_COL_LABEL[ycol][2]
            ylabel = PMO_COL_LABEL[ycol][0]
            xlabel = PMO_COL_LABEL[xcol][0]
            title = "%s" % ylabel
            if ycol == 10:
                title += " from %s" % PMO_MEM[tag]
            title += "\\n"
            for key in conf:
                if key in PMO_PARAM:
                    title += " %s=%s" % (PMO_PARAM[key], conf[key])
            graphs = [(tmp.name, 1, 2, ylabel)]
            # plot cutoff
            (s, lo, hi) = stats.iqr(rows[:,1])
            lo -= s * self.options.extend
            hi += s * self.options.extend
            m99 = stats.cutoff_max(rows[:, 1])
            graphs += [(lo, 'IQR cutoff (%d)' % lo, 'line'),
                       (hi, 'IQR cutoff (%d)' % hi, 'line'),
                       (m99,'99%% cutoff (%d)' % m99, 'line lw 2')]
            gnuplot(graphs,
                    xlabel="%s (%s)" % (xlabel, xunit),
                    ylabel="%s (%s)" % ("access cost" if yminus is None
                                        else "delta to %s" % PMO_COL_LABEL[yminus][0],
                                        yunit),
                    title=title,
                    style='points',
                    format=self.options.format,
                    fname=figname)

    def plot_file(self, datafile):
        bname = basename(datafile)
        name, ext = splitext(bname)
        if ext != '.csv':
            self.err("Warning: '%s' doesn't look like a CSV file."
                     % bname)
        conf    = decode(name)
        if 'pmo' in conf:
            self.plot_preempt_migrate(datafile, name, conf)
        else:
            self.err("Skipped '%s'; unkown experiment type."
                     % bname)

    def default(self, _):
        for datafile in self.args:
            self.plot_file(datafile)

if __name__ == "__main__":
    CyclePlotter().launch()