stats.py revision 2006:3ca085495c69
1#!/usr/bin/env python
2
3# Copyright (c) 2003-2004 The Regents of The University of Michigan
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are
8# met: redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer;
10# redistributions in binary form must reproduce the above copyright
11# notice, this list of conditions and the following disclaimer in the
12# documentation and/or other materials provided with the distribution;
13# neither the name of the copyright holders nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29from __future__ import division
30import re, sys, math
31
32def usage():
33    print '''\
34Usage: %s [-E] [-F] [ -G <get> ] [-d <db> ] [-g <graphdir> ] [-h <host>] [-p]
35       [-s <system>] [-r <runs> ] [-T <samples>] [-u <username>]
36       <command> [command args]
37
38       commands    extra parameters   description
39       ----------- ------------------ ---------------------------------------
40       bins        [regex]            List bins (only matching regex)
41       formula     <formula>          Evaluated formula specified
42       formulas    [regex]            List formulas (only matching regex)
43       runs        none               List all runs in database
44       samples     none               List samples present in database
45       stability   <pairnum> <stats>  Calculated statistical info about stats
46       stat        <regex>            Show stat data (only matching regex)
47       stats       [regex]            List all stats (only matching regex)
48
49       database    <command>          Where command is drop, init, or clean
50
51''' % sys.argv[0]
52    sys.exit(1)
53
54def getopts(list, flags):
55    import getopt
56    try:
57        opts, args = getopt.getopt(list, flags)
58    except getopt.GetoptError:
59        usage()
60
61    return opts, args
62
63class CommandException(Exception):
64    pass
65
66def commands(options, command, args):
67    if command == 'database':
68        if len(args) == 0: raise CommandException
69
70        import dbinit
71        mydb = dbinit.MyDB(options)
72
73        if args[0] == 'drop':
74            if len(args) > 2: raise CommandException
75            mydb.admin()
76            mydb.drop()
77            if len(args) == 2 and args[1] == 'init':
78                mydb.create()
79                mydb.connect()
80                mydb.populate()
81            mydb.close()
82            return
83
84        if args[0] == 'init':
85            if len(args) > 1: raise CommandException
86            mydb.admin()
87            mydb.create()
88            mydb.connect()
89            mydb.populate()
90            mydb.close()
91            return
92
93        if args[0] == 'clean':
94            if len(args) > 1: raise CommandException
95            mydb.connect()
96            mydb.clean()
97            return
98
99        raise CommandException
100
101    import db
102    source = db.Database()
103    source.host = options.host
104    source.db = options.db
105    source.passwd = options.passwd
106    source.user = options.user
107    source.connect()
108    #source.update_dict(globals())
109
110    if type(options.method) is str:
111        source.method = options.method
112
113    if options.runs is None:
114        runs = source.allRuns
115    else:
116        rx = re.compile(options.runs)
117        runs = []
118        for run in source.allRuns:
119            if rx.match(run.name):
120                runs.append(run)
121
122    if command == 'runs':
123        user = None
124        opts, args = getopts(args, '-u')
125        if len(args):
126            raise CommandException
127        for o,a in opts:
128            if o == '-u':
129                user = a
130        source.listRuns(user)
131        return
132
133    if command == 'stats':
134        if len(args) == 0:
135            source.listStats()
136        elif len(args) == 1:
137            source.listStats(args[0])
138        else:
139            raise CommandException
140
141        return
142
143    if command == 'bins':
144        if len(args) == 0:
145            source.listBins()
146        elif len(args) == 1:
147            source.listBins(args[0])
148        else:
149            raise CommandException
150
151        return
152
153    if command == 'formulas':
154        if len(args) == 0:
155            source.listFormulas()
156        elif len(args) == 1:
157            source.listFormulas(args[0])
158        else:
159            raise CommandException
160
161        return
162
163    if command == 'samples':
164        if len(args):
165            raise CommandException
166
167        source.listTicks(runs)
168        return
169
170    if command == 'stability':
171        if len(args) < 2:
172            raise CommandException
173
174        try:
175            merge = int(args[0])
176        except ValueError:
177            usage()
178        stats = source.getStat(args[1])
179        source.method = 'sum'
180
181        def disp(*args):
182            print "%-35s %12s %12s %4s %5s %5s %5s %10s" % args
183
184        # temporary variable containing a bunch of dashes
185        d = '-' * 100
186
187        #loop through all the stats selected
188        for stat in stats:
189            print "%s:" % stat.name
190            disp("run name", "average", "stdev", ">10%", ">1SDV", ">2SDV",
191                 "SAMP", "CV")
192            disp(d[:35], d[:12], d[:12], d[:4], d[:5], d[:5], d[:5], d[:10])
193
194            #loop through all the selected runs
195            for run in runs:
196                runTicks = source.retTicks([ run ])
197                #throw away the first one, it's 0
198                runTicks.pop(0)
199                source.ticks = runTicks
200                avg = 0
201                stdev = 0
202                numoutsideavg  = 0
203                numoutside1std = 0
204                numoutside2std = 0
205                pairRunTicks = []
206                if value(stat, run.run) == 1e300*1e300:
207                    continue
208                for t in range(0, len(runTicks)-(merge-1), merge):
209                    tempPair = []
210                    for p in range(0,merge):
211                        tempPair.append(runTicks[t+p])
212                    pairRunTicks.append(tempPair)
213                #loop through all the various ticks for each run
214                for tick in pairRunTicks:
215                    source.ticks = tick
216                    avg += value(stat, run.run)
217                avg /= len(pairRunTicks)
218                for tick in pairRunTicks:
219                    source.ticks = tick
220                    val = value(stat, run.run)
221                    stdev += pow((val-avg),2)
222                stdev = math.sqrt(stdev / len(pairRunTicks))
223                for tick in pairRunTicks:
224                    source.ticks = tick
225                    val = value(stat, run.run)
226                    if (val < (avg * .9)) or (val > (avg * 1.1)):
227                        numoutsideavg += 1
228                    if (val < (avg - stdev)) or (val > (avg + stdev)):
229                        numoutside1std += 1
230                    if (val < (avg - (2*stdev))) or (val > (avg + (2*stdev))):
231                        numoutside2std += 1
232                if avg > 1000:
233                    disp(run.name, "%.1f" % avg, "%.1f" % stdev,
234                         "%d" % numoutsideavg, "%d" % numoutside1std,
235                         "%d" % numoutside2std, "%d" % len(pairRunTicks),
236                         "%.3f" % (stdev/avg*100))
237                elif avg > 100:
238                    disp(run.name, "%.1f" % avg, "%.1f" % stdev,
239                         "%d" % numoutsideavg, "%d" % numoutside1std,
240                         "%d" % numoutside2std, "%d" % len(pairRunTicks),
241                         "%.5f" % (stdev/avg*100))
242                else:
243                    disp(run.name, "%.5f" % avg, "%.5f" % stdev,
244                         "%d" % numoutsideavg, "%d" % numoutside1std,
245                         "%d" % numoutside2std, "%d" % len(pairRunTicks),
246                         "%.7f" % (stdev/avg*100))
247        return
248
249    if command == 'all':
250        if len(args):
251            raise CommandException
252
253        all = [ 'bps', 'misses', 'mpkb', 'ipkb', 'pps', 'bpt' ]
254        for command in all:
255            commands(options, command, args)
256
257    if options.ticks:
258        if not options.graph:
259            print 'only displaying sample %s' % options.ticks
260        source.ticks = [ int(x) for x in options.ticks.split() ]
261
262    from output import StatOutput
263    output = StatOutput(options.jobfile, source)
264    output.xlabel = 'System Configuration'
265
266    if command == 'stat' or command == 'formula':
267        if len(args) != 1:
268            raise CommandException
269
270        if command == 'stat':
271            stats = source.getStat(args[0])
272        if command == 'formula':
273            stats = eval(args[0])
274
275        for stat in stats:
276            output.stat = stat
277            output.ylabel = stat.name
278            if options.graph:
279                output.graph(stat.name, options.graphdir)
280            else:
281                output.display(stat.name, options.binned, options.printmode)
282
283        return
284
285    if len(args):
286        raise CommandException
287
288    from info import ProxyGroup
289    sim_seconds = source['sim_seconds']
290    proxy = ProxyGroup(system = source[options.system])
291    system = proxy.system
292
293    etherdev = system.tsunami.etherdev0
294    bytes = etherdev.rxBytes + etherdev.txBytes
295    kbytes = bytes / 1024
296    packets = etherdev.rxPackets + etherdev.txPackets
297    bps = etherdev.rxBandwidth + etherdev.txBandwidth
298
299    def display():
300        if options.graph:
301            output.graph(command, options.graphdir, proxy)
302        else:
303            output.display(command, options.binned, options.printmode)
304
305    if command == 'usertime':
306        import copy
307        user = copy.copy(system.run0.numCycles)
308        user.bins = 'user'
309
310        output.stat = user / system.run0.numCycles
311        output.ylabel = 'User Fraction'
312
313        display()
314        return
315
316    if command == 'ticks':
317        output.stat = system.run0.numCycles
318        output.binstats = [ system.run0.numCycles ]
319
320        display()
321        return
322
323    if command == 'bytes':
324        output.stat = bytes
325        display()
326        return
327
328    if command == 'packets':
329        output.stat = packets
330        display()
331        return
332
333    if command == 'ppt' or command == 'tpp':
334        output.stat = packets / system.run0.numCycles
335        output.invert = command == 'tpp'
336        display()
337        return
338
339    if command == 'pps':
340        output.stat = packets / sim_seconds
341        output.ylabel = 'Packets/s'
342        display()
343        return
344
345    if command == 'bpt' or command == 'tpb':
346        output.stat = bytes / system.run0.numCycles * 8
347        output.ylabel = 'bps / Hz'
348        output.invert = command == 'tpb'
349        display()
350        return
351
352    if command in ('rxbps', 'txbps', 'bps'):
353        if command == 'rxbps':
354            output.stat = etherdev.rxBandwidth / 1e9
355        if command == 'txbps':
356            output.stat = etherdev.txBandwidth / 1e9
357        if command == 'bps':
358            output.stat = bps / 1e9
359
360        output.ylabel = 'Bandwidth (Gbps)'
361        output.ylim = [ 0.0, 10.0 ]
362        display()
363        return
364
365    if command == 'bpp':
366        output.stat = bytes / packets
367        output.ylabel = 'Bytes / Packet'
368        display()
369        return
370
371    if command == 'rxbpp':
372        output.stat = etherdev.rxBytes / etherdev.rxPackets
373        output.ylabel = 'Receive Bytes / Packet'
374        display()
375        return
376
377    if command == 'txbpp':
378        output.stat = etherdev.txBytes / etherdev.txPackets
379        output.ylabel = 'Transmit Bytes / Packet'
380        display()
381        return
382
383    if command == 'rtp':
384        output.stat = etherdev.rxPackets / etherdev.txPackets
385        output.ylabel = 'rxPackets / txPackets'
386        display()
387        return
388
389    if command == 'rtb':
390        output.stat = etherdev.rxBytes / etherdev.txBytes
391        output.ylabel = 'rxBytes / txBytes'
392        display()
393        return
394
395    misses = system.l2.overall_mshr_misses
396
397    if command == 'misses':
398        output.stat = misses
399        output.ylabel = 'Overall MSHR Misses'
400        display()
401        return
402
403    if command == 'mpkb':
404        output.stat = misses / (bytes / 1024)
405        output.binstats = [ misses ]
406        output.ylabel = 'Misses / KB'
407        display()
408        return
409
410    if command == 'ipkb':
411        interrupts = system.run0.kern.faults[4]
412        output.stat = interrupts / kbytes
413        output.binstats = [ interrupts ]
414        output.ylabel = 'Interrupts / KB'
415        display()
416        return
417
418    if command == 'execute':
419        output.stat = system.run0.ISSUE__count
420        display()
421        return
422
423    if command == 'commit':
424        output.stat = system.run0.COM__count
425        display()
426        return
427
428    if command == 'fetch':
429        output.stat = system.run0.FETCH__count
430        display()
431        return
432
433    raise CommandException
434
435
436class Options: pass
437
438if __name__ == '__main__':
439    import getpass
440    from jobfile import JobFile
441
442    options = Options()
443    options.host = None
444    options.db = None
445    options.passwd = ''
446    options.user = getpass.getuser()
447    options.runs = None
448    options.system = 'client'
449    options.method = None
450    options.binned = False
451    options.graph = False
452    options.ticks = False
453    options.printmode = 'G'
454    jobfilename = 'Test.py'
455    options.jobfile = None
456    options.all = False
457
458    opts, args = getopts(sys.argv[1:], '-BEFJad:g:h:j:m:pr:s:u:T:')
459    for o,a in opts:
460        if o == '-B':
461            options.binned = True
462        if o == '-E':
463            options.printmode = 'E'
464        if o == '-F':
465            options.printmode = 'F'
466        if o == '-a':
467            options.all = True
468        if o == '-d':
469            options.db = a
470        if o == '-g':
471            options.graph = True;
472            options.graphdir = a
473        if o == '-h':
474            options.host = a
475        if o == '-J':
476            jobfilename = None
477        if o == '-j':
478            jobfilename = a
479        if o == '-m':
480            options.method = a
481        if o == '-p':
482            options.passwd = getpass.getpass()
483        if o == '-r':
484            options.runs = a
485        if o == '-u':
486            options.user = a
487        if o == '-s':
488            options.system = a
489        if o == '-T':
490            options.ticks = a
491
492    if jobfilename:
493        options.jobfile = JobFile(jobfilename)
494        if not options.host:
495            options.host = options.jobfile.dbhost
496        if not options.db:
497            options.db = options.jobfile.statdb
498
499    if not options.host:
500        sys.exit('Database server must be provided from a jobfile or -h')
501
502    if not options.db:
503        sys.exit('Database name must be provided from a jobfile or -d')
504
505    if len(args) == 0:
506        usage()
507
508    command = args[0]
509    args = args[1:]
510
511    try:
512        commands(options, command, args)
513    except CommandException:
514        usage()
515