summaryrefslogtreecommitdiff
path: root/util/stats/stats.py
blob: 2a24bf3fd1438750d91444e7a7b2daebdd79eb21 (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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#!/usr/bin/env python

# Copyright (c) 2003-2004 The Regents of The University of Michigan
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met: redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer;
# redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution;
# neither the name of the copyright holders nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from __future__ import division
import re, sys, math

def usage():
    print '''\
Usage: %s [-E] [-F] [ -G <get> ] [-d <db> ] [-g <graphdir> ] [-h <host>] [-p]
       [-s <system>] [-r <runs> ] [-T <samples>] [-u <username>]
       <command> [command args]

       commands    extra parameters   description
       ----------- ------------------ ---------------------------------------
       bins        [regex]            List bins (only matching regex)
       formula     <formula>          Evaluated formula specified
       formulas    [regex]            List formulas (only matching regex)
       runs        none               List all runs in database
       samples     none               List samples present in database
       stability   <pairnum> <stats>  Calculated statistical info about stats
       stat        <regex>            Show stat data (only matching regex)
       stats       [regex]            List all stats (only matching regex)

       database    <command>          Where command is drop, init, or clean

''' % sys.argv[0]
    sys.exit(1)

def getopts(list, flags):
    import getopt
    try:
        opts, args = getopt.getopt(list, flags)
    except getopt.GetoptError:
        usage()

    return opts, args

class CommandException(Exception):
    pass

def commands(options, command, args):
    if command == 'database':
        if len(args) == 0: raise CommandException

        import dbinit
        mydb = dbinit.MyDB(options)

        if args[0] == 'drop':
            if len(args) > 2: raise CommandException
            mydb.admin()
            mydb.drop()
            if len(args) == 2 and args[1] == 'init':
                mydb.create()
                mydb.connect()
                mydb.populate()
            mydb.close()
            return

        if args[0] == 'init':
            if len(args) > 1: raise CommandException
            mydb.admin()
            mydb.create()
            mydb.connect()
            mydb.populate()
            mydb.close()
            return

        if args[0] == 'clean':
            if len(args) > 1: raise CommandException
            mydb.connect()
            mydb.clean()
            return

        raise CommandException

    import db
    source = db.Database()
    source.host = options.host
    source.db = options.db
    source.passwd = options.passwd
    source.user = options.user
    source.connect()
    #source.update_dict(globals())

    if type(options.method) is str:
        source.method = options.method

    if options.runs is None:
        runs = source.allRuns
    else:
        rx = re.compile(options.runs)
        runs = []
        for run in source.allRuns:
            if rx.match(run.name):
                runs.append(run)

    if command == 'runs':
        user = None
        opts, args = getopts(args, '-u')
        if len(args):
            raise CommandException
        for o,a in opts:
            if o == '-u':
                user = a
        source.listRuns(user)
        return

    if command == 'stats':
        if len(args) == 0:
            source.listStats()
        elif len(args) == 1:
            source.listStats(args[0])
        else:
            raise CommandException

        return

    if command == 'bins':
        if len(args) == 0:
            source.listBins()
        elif len(args) == 1:
            source.listBins(args[0])
        else:
            raise CommandException

        return

    if command == 'formulas':
        if len(args) == 0:
            source.listFormulas()
        elif len(args) == 1:
            source.listFormulas(args[0])
        else:
            raise CommandException

        return

    if command == 'samples':
        if len(args):
            raise CommandException

        source.listTicks(runs)
        return

    if command == 'stability':
        if len(args) < 2:
            raise CommandException

        try:
            merge = int(args[0])
        except ValueError:
            usage()
        stats = source.getStat(args[1])
        source.method = 'sum'

        def disp(*args):
            print "%-35s %12s %12s %4s %5s %5s %5s %10s" % args

        # temporary variable containing a bunch of dashes
        d = '-' * 100

        #loop through all the stats selected
        for stat in stats:
            print "%s:" % stat.name
            disp("run name", "average", "stdev", ">10%", ">1SDV", ">2SDV",
                 "SAMP", "CV")
            disp(d[:35], d[:12], d[:12], d[:4], d[:5], d[:5], d[:5], d[:10])

            #loop through all the selected runs
            for run in runs:
                runTicks = source.retTicks([ run ])
                #throw away the first one, it's 0
                runTicks.pop(0)
                source.ticks = runTicks
                avg = 0
                stdev = 0
                numoutsideavg  = 0
                numoutside1std = 0
                numoutside2std = 0
                pairRunTicks = []
                if value(stat, run.run) == 1e300*1e300:
                    continue
                for t in range(0, len(runTicks)-(merge-1), merge):
                    tempPair = []
                    for p in range(0,merge):
                        tempPair.append(runTicks[t+p])
                    pairRunTicks.append(tempPair)
                #loop through all the various ticks for each run
                for tick in pairRunTicks:
                    source.ticks = tick
                    avg += value(stat, run.run)
                avg /= len(pairRunTicks)
                for tick in pairRunTicks:
                    source.ticks = tick
                    val = value(stat, run.run)
                    stdev += pow((val-avg),2)
                stdev = math.sqrt(stdev / len(pairRunTicks))
                for tick in pairRunTicks:
                    source.ticks = tick
                    val = value(stat, run.run)
                    if (val < (avg * .9)) or (val > (avg * 1.1)):
                        numoutsideavg += 1
                    if (val < (avg - stdev)) or (val > (avg + stdev)):
                        numoutside1std += 1
                    if (val < (avg - (2*stdev))) or (val > (avg + (2*stdev))):
                        numoutside2std += 1
                if avg > 1000:
                    disp(run.name, "%.1f" % avg, "%.1f" % stdev,
                         "%d" % numoutsideavg, "%d" % numoutside1std,
                         "%d" % numoutside2std, "%d" % len(pairRunTicks),
                         "%.3f" % (stdev/avg*100))
                elif avg > 100:
                    disp(run.name, "%.1f" % avg, "%.1f" % stdev,
                         "%d" % numoutsideavg, "%d" % numoutside1std,
                         "%d" % numoutside2std, "%d" % len(pairRunTicks),
                         "%.5f" % (stdev/avg*100))
                else:
                    disp(run.name, "%.5f" % avg, "%.5f" % stdev,
                         "%d" % numoutsideavg, "%d" % numoutside1std,
                         "%d" % numoutside2std, "%d" % len(pairRunTicks),
                         "%.7f" % (stdev/avg*100))
        return

    if command == 'all':
        if len(args):
            raise CommandException

        all = [ 'bps', 'misses', 'mpkb', 'ipkb', 'pps', 'bpt' ]
        for command in all:
            commands(options, command, args)

    if options.ticks:
        if not options.graph:
            print 'only displaying sample %s' % options.ticks
        source.ticks = [ int(x) for x in options.ticks.split() ]

    import output

    def display():
        if options.graph:
            output.graph(options.graphdir)
        else:
            output.display(options.binned, options.printmode)


    if command == 'stat' or command == 'formula':
        if len(args) != 1:
            raise CommandException

        if command == 'stat':
            stats = source.getStat(args[0])
        if command == 'formula':
            stats = eval(args[0])

        for stat in stats:
            output = output.StatOutput(stat.name, options.jobfile, source)
            output.stat = stat
            output.label = stat.name
            display()

        return

    if len(args):
        raise CommandException

    system = source.__dict__[options.system]
    from info import ProxyGroup
    sim_seconds = source['sim_seconds']
    proxy = ProxyGroup(system = source[options.system])
    system = proxy.system

    etherdev = system.tsunami.etherdev0
    bytes = etherdev.rxBytes + etherdev.txBytes
    kbytes = bytes / 1024
    packets = etherdev.rxPackets + etherdev.txPackets
    bps = etherdev.rxBandwidth + etherdev.txBandwidth

    output = output.StatOutput(command, options.jobfile, source)

    if command == 'usertime':
        import copy
        user = copy.copy(system.run0.numCycles)
        user.bins = 'user'

        output.stat = user / system.run0.numCycles
        output.label = 'User Fraction'

        display()
        return

    if command == 'ticks':
        output.stat = system.run0.numCycles
        output.binstats = [ system.run0.numCycles ]

        display()
        return

    if command == 'bytes':
        output.stat = bytes
        display()
        return

    if command == 'packets':
        output.stat = packets
        display()
        return

    if command == 'ppt' or command == 'tpp':
        output.stat = packets / system.run0.numCycles
        output.invert = command == 'tpp'
        display()
        return

    if command == 'pps':
        output.stat = packets / sim_seconds
        output.label = 'Packets/s'
        display()
        return

    if command == 'bpt' or command == 'tpb':
        output.stat = bytes / system.run0.numCycles * 8
        output.label = 'bps / Hz'
        output.invert = command == 'tpb'
        display()
        return

    if command in ('rxbps', 'txbps', 'bps'):
        if command == 'rxbps':
            output.stat = etherdev.rxBandwidth / 1e9
        if command == 'txbps':
            output.stat = etherdev.txBandwidth / 1e9
        if command == 'bps':
            output.stat = bps / 1e9

        output.label = 'Bandwidth (Gbps)'
        display()
        return

    if command == 'bpp':
        output.stat = bytes / packets
        output.label = 'Bytes / Packet'
        display()
        return

    if command == 'rxbpp':
        output.stat = etherdev.rxBytes / etherdev.rxPackets
        output.label = 'Receive Bytes / Packet'
        display()
        return

    if command == 'txbpp':
        output.stat = etherdev.txBytes / etherdev.txPackets
        output.label = 'Transmit Bytes / Packet'
        display()
        return

    if command == 'rtp':
        output.stat = etherdev.rxPackets / etherdev.txPackets
        output.label = 'rxPackets / txPackets'
        display()
        return

    if command == 'rtb':
        output.stat = etherdev.rxBytes / etherdev.txBytes
        output.label = 'rxBytes / txBytes'
        display()
        return

    misses = system.l2.overall_mshr_misses

    if command == 'misses':
        output.stat = misses
        output.label = 'Overall MSHR Misses'
        display()
        return

    if command == 'mpkb':
        output.stat = misses / (bytes / 1024)
        output.binstats = [ misses ]
        output.label = 'Misses / KB'
        display()
        return

    if command == 'ipkb':
        interrupts = system.run0.kern.faults[4]
        output.stat = interrupts / kbytes
        output.binstats = [ interrupts ]
        output.label = 'Interrupts / KB'
        display()
        return

    if command == 'execute':
        output.stat = system.run0.ISSUE__count
        display()
        return

    if command == 'commit':
        output.stat = system.run0.COM__count
        display()
        return

    if command == 'fetch':
        output.stat = system.run0.FETCH__count
        display()
        return

    raise CommandException


class Options: pass

if __name__ == '__main__':
    import getpass
    from jobfile import JobFile

    options = Options()
    options.host = None
    options.db = None
    options.passwd = ''
    options.user = getpass.getuser()
    options.runs = None
    options.system = 'client'
    options.method = None
    options.binned = False
    options.graph = False
    options.ticks = False
    options.printmode = 'G'
    jobfilename = 'Test.py'
    options.jobfile = None
    options.all = False

    opts, args = getopts(sys.argv[1:], '-BEFJad:g:h:j:m:pr:s:u:T:')
    for o,a in opts:
        if o == '-B':
            options.binned = True
        if o == '-E':
            options.printmode = 'E'
        if o == '-F':
            options.printmode = 'F'
        if o == '-a':
            options.all = True
        if o == '-d':
            options.db = a
        if o == '-g':
            options.graph = True;
            options.graphdir = a
        if o == '-h':
            options.host = a
        if o == '-J':
            jobfilename = None
        if o == '-j':
            jobfilename = a
        if o == '-m':
            options.method = a
        if o == '-p':
            options.passwd = getpass.getpass()
        if o == '-r':
            options.runs = a
        if o == '-u':
            options.user = a
        if o == '-s':
            options.system = a
        if o == '-T':
            options.ticks = a

    if jobfilename:
        options.jobfile = JobFile(jobfilename)
        if not options.host:
            options.host = options.jobfile.dbhost
        if not options.db:
            options.db = options.jobfile.statdb

    if not options.host:
        sys.exit('Database server must be provided from a jobfile or -h')

    if not options.db:
        sys.exit('Database name must be provided from a jobfile or -d')

    if len(args) == 0:
        usage()

    command = args[0]
    args = args[1:]

    try:
        commands(options, command, args)
    except CommandException:
        usage()