summaryrefslogtreecommitdiffstats
path: root/rteval/rteval.py
blob: ee52e19da630f87e57056af31b5ff3ef0149c891 (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
#!/usr/bin/python -tt
#
# rteval  - script for evaluating platform suitability for RT Linux
#
#           This program is used to determine the suitability of
#           a system for use in a Real Time Linux environment.
#           It starts up various system loads and measures event
#           latency while the loads are running. A report is generated
#           to show the latencies encountered during the run.

import sys
import os
import os.path
import time
import threading
import subprocess
import optparse
import tempfile
import statvfs
import shutil
import rtevalclient
from datetime import datetime

sys.pathconf = "."
import load
import hackbench
import kcompile
import cyclictest
import xmlout
import dmi

class RtEval(object):
    def __init__(self):
        self.version = "0.8"
        self.load_modules = (hackbench, kcompile)
        self.keepdata = True
        self.verbose = False
        self.debugging = False
        self.duration = 60.0
        self.interrupted = False
        self.sysreport = False
        self.reportdir = None
        self.reportfile = None
        self.runlatency = True
        self.runsmi = False
        self.runoprofile = False
        self.loads = []
        self.topdir = os.getcwd()
        self.start = datetime.now()
        self.mydir = '/usr/share/rteval-%s' % self.version
        if not os.path.exists(self.mydir):
            self.mydir = os.path.join(self.topdir, "rteval")
        if not os.path.exists(self.mydir):
            raise RuntimeError, "Can't find rteval directory (%s)!" % self.mydir
        self.xslt = os.path.join(self.mydir, "rteval_text.xsl")
        if not os.path.exists(self.xslt):
            raise RuntimeError, "can't find XSL template (%s)!" % self.xslt
        self.loaddir = os.path.join(self.mydir, 'loadsource')
        self.tmpdir = self.find_biggest_tmp()
        self.numcores = self.get_num_cores()
        self.memsize = self.get_memory_size()
        self.get_clocksources()
        self.xml = ''
        self.xmlreport = xmlout.XMLOut('rteval', self.version)
        self.make_report_dir()

    def find_biggest_tmp(self):
        dir = ''
        avail = 0;
        for d in ('/tmp', '/var/tmp', '/usr/tmp'):
            a = os.statvfs(d)[statvfs.F_BAVAIL]
            if a > avail:
                dir = d
                avail = a
                self.debug("using tmp dir: %s\n" % dir)
        return dir


    def get_num_cores(self):
        f = open('/proc/cpuinfo')
        numcores = 0
        for line in f:
            if line.startswith('processor'):
                numcores += 1
        f.close()
        self.debug("counted %d cores\n" % numcores)
        return numcores

    def get_memory_size(self):
        f = open('/proc/meminfo')
        for l in f:
            if l.startswith('MemTotal:'):
                size = int(l.split()[1])
                f.close()
                self.debug("memory size %d\n" % size)
                return size
        raise RuntimeError, "can't find memtotal in /proc/meminfo!"

    def get_clocksources(self):
        path = '/sys/devices/system/clocksource/clocksource0'
        if not os.path.exists(path):
            raise RuntimeError, "Can't find clocksource path in /sys"
        f = open (os.path.join (path, "current_clocksource"))
        self.current_clocksource = f.readline().strip()
        f = open (os.path.join (path, "available_clocksource"))
        self.available_clocksource = f.readline().strip()
        f.close()

    def parse_options(self):
        parser = optparse.OptionParser()
        parser.add_option("-d", "--duration", dest="duration",
                          type="string", default=str(self.duration),
                          help="specify length of test run")
        parser.add_option("-v", "--verbose", dest="verbose",
                          action="store_true", default=False,
                          help="turn on verbose prints")
        parser.add_option("-w", "--workdir", dest="workdir",
                          type="string", default=self.topdir,
                          help="top directory for rteval data")
        parser.add_option("-l", "--loaddir", dest="loaddir",
                          type="string", default=self.loaddir,
                          help="top directory for rteval data")
        parser.add_option("-s", "--sysreport", dest="sysreport",
                          action="store_true", default=False,
                          help='run sysreport to collect system data')
        parser.add_option("-L", '--latency', dest='latency',
                          action="store_true", default=False,
                          help='run latency detector (default)')
        parser.add_option("-S", '--smi', dest='smi',
                          action="store_true", default=False,
                          help='run smi detector (not implemented)')
        parser.add_option("-D", '--debug', dest='debugging',
                          action='store_true', default=False,
                          help='turn on debug prints')
        parser.add_option("-O", '--oprofile', dest='oprofile',
                          action='store_true', default=False,
                          help='run oprofile while running evaluation (not implemented)')
        parser.add_option("-X", '--xmlrpc-submit', dest='xmlrpchost',
                          action='store', default=None,
                          help='Hostname to XML-RPC server to submit reports', metavar='HOST')
        parser.add_option("-Z", '--summarize', dest='summarize',
                          action='store_true', default=False,
                          help='summarize an already existing XML report')

        (options, args) = parser.parse_args()
        if options.duration:
            mult = 1.0
            v = options.duration.lower()
            if v.endswith('s'):
                v = v[:-1]
            elif v.endswith('m'):
                v = v[:-1]
                mult = 60.0
            elif v.endswith('h'):
                v = v[:-1]
                mult = 3600.0
            elif v.endswith('d'):
                v = v[:-1]
                mult = 3600.0 * 24.0
            options.duration = float(v) * mult
        if options.latency == False and options.smi == False:
            options.latency = True
        return (options, args)

    def debug(self, str):
        if self.debugging: print 

    def info(self, str):
        if self.verbose: print str

    def run_sysreport(self):
        import glob
        if os.path.exists('/usr/sbin/sosreport'):
            exe = '/usr/sbin/sosreport'
        elif os.path.exists('/usr/sbin/sysreport'):
            exe = '/usr/sbin/sysreport'
        else:
            raise RuntimeError, "Can't find sosreport/sysreport"

        self.debug("report tool: %s\n" % exe)
        options =  ['-k', 'rpm.rpmvma=off',
                    '--name=rteval', 
                    '--ticket=1234',
                    '--no-progressbar']

        print "Generating SOS report"
        subprocess.call([exe] + options)
        for s in glob.glob('/tmp/s?sreport-rteval-*'):
            shutil.move(s, self.reportdir)
    

    def genxml(self, duration, accum, samples, xslt = None):
        seconds = duration.seconds
        hours = seconds / 3600
        if hours: seconds -= (hours * 3600)
        minutes = seconds / 60
        if minutes: seconds -= (minutes * 60)
        (sys, node, release, ver, machine) = os.uname()

        # Start new XML report
        self.xmlreport.NewReport()

        self.xmlreport.openblock('run_info', {'days': duration.days,
                                 'hours': hours,
                                 'minutes': minutes,
                                 'seconds': seconds})
        self.xmlreport.taggedvalue('date', self.start.strftime('%Y-%m-%d'))
        self.xmlreport.taggedvalue('time', self.start.strftime('%H:%M:%S'))
        self.xmlreport.closeblock()
        self.xmlreport.openblock('uname')
        self.xmlreport.taggedvalue('node', node)
        isrt = 1
        if ver.find(' RT ') == -1:
            isrt = 0
        self.xmlreport.taggedvalue('kernel', release, {'is_RT':isrt})
        self.xmlreport.taggedvalue('arch', machine)
        self.xmlreport.closeblock()

        self.xmlreport.openblock("clocksource")
        self.xmlreport.taggedvalue('current', self.current_clocksource)
        self.xmlreport.taggedvalue('available', self.available_clocksource)
        self.xmlreport.closeblock()

        self.xmlreport.openblock('hardware')
        self.xmlreport.taggedvalue('cpu_cores', self.numcores)
        self.xmlreport.taggedvalue('memory_size', self.memsize)
        self.xmlreport.closeblock()


        self.xmlreport.openblock('loads', {'load_average':str(accum / samples)})
        for load in self.loads:
            load.genxml(self.xmlreport)
        self.xmlreport.closeblock()
        self.cyclictest.genxml(self.xmlreport)

        # now generate the dmidecode data for this host
        d = dmi.DMIinfo(self.mydir)
        d.genxml(self.xmlreport)
        
        # Close the report - prepare for return the result
        self.xmlreport.close()

        # Write XML (or write XSLT parsed XML if xslt != None)
        if self.xml != None:
            self.xmlreport.Write(self.xml, xslt)
        else:
            # If no file is set, use stdout
            self.xmlreport.Write("-", xslt) # libxml2 defines a filename as "-" to be stdout


    def report(self):
        "Create a screen report, based on a predefined XSLT template"
        self.xmlreport.Write("-", self.xslt)

    def summarize(self, xmlfile):
        print "loading %s for summarizing" % xmlfile
        self.xmlreport.LoadReport(xmlfile)
        self.xmlreport.Write('-', self.xslt)

    def start_loads(self):
        if len(self.loads) == 0:
            raise RuntimeError, "start_loads: No loads defined!"
        self.info ("starting loads:")
        for l in self.loads:
            l.start()
        # now wait until they're all ready
        self.info("waiting for ready from all loads")
        ready=False
        while not ready:
            busy = 0
            for l in self.loads:
                self.debug("checking load: %s" % l.name)
                if not l.isAlive():
                    raise RuntimeError, "%s died" % l.name
                if not l.isReady():
                    busy += 1
                    self.debug("%s is busy" % l.name)
            if busy:
                time.sleep(1.0)
            else:
                ready = True

    def stop_loads(self):
        if len(self.loads) == 0:
            raise RuntimeError, "stop_loads: No loads defined!"
        self.info("stopping loads: ")
        for l in self.loads:
            self.info("\t%s" % l.name)
            l.stopevent.set()
            l.join(2.0)

    def make_report_dir(self):
        t = self.start
        i = 1
        self.reportdir = os.path.join(self.topdir,
                                      t.strftime("rteval-%Y%m%d-"+str(i)))
        while os.path.exists(self.reportdir):
            i += 1
            self.reportdir = os.path.join(self.topdir,
                                          t.strftime('rteval-%Y%m%d-'+str(i)))
        if not os.path.isdir(self.reportdir): 
            os.mkdir(self.reportdir)
        return self.reportdir

    def get_dmesg(self):
        dpath = "/var/log/dmesg"
        if not os.path.exists(dpath):
            print "dmesg file not found at %s" % dpath
            return
        shutil.copyfile(dpath, os.path.join(self.reportdir, "dmesg"))

    def measure_latency(self):
        builddir = os.path.join(self.tmpdir, 'rteval-build')
        if not os.path.isdir(builddir): os.mkdir(builddir)
        self.reportfile = os.path.join(self.reportdir, "summary.rpt")
        self.xml = os.path.join(self.reportdir, "summary.xml")

        nthreads = 0

        print "setting up loads"
        self.loads = []
        for m in self.load_modules:
            self.loads.append(m.create(builddir, self.loaddir, self.verbose, self.numcores))

        self.info("setting up cyclictest")
        self.cyclictest = cyclictest.Cyclictest(duration=self.duration, debugging=self.debugging)

        try:
            # start the loads
            self.start_loads()
            
            print "started %d loads on %d cores" % (len(self.loads), self.numcores)
            print "Run duration: %d seconds" % self.duration
            
            start = datetime.now()
            
            # start the cyclictest thread
            self.info("starting cyclictest")
            self.cyclictest.start()
            
            # turn loose the loads
            self.info("sending start event to all loads")
            for l in self.loads:
                l.startevent.set()
                nthreads += 1
                
            # open the loadavg /proc entry
            p = open("/proc/loadavg")
            accum = 0.0
            samples = 0

            # wait for time to expire or thread to die
            self.info("waiting for duration (%f)" % self.duration)
            stoptime = (time.time() + self.duration)
            while time.time() <= stoptime:
                time.sleep(1.0)
                if not self.cyclictest.isAlive():
                    raise RuntimeError, "cyclictest thread died!"
                if len(threading.enumerate()) < nthreads:
                    raise RuntimeError, "load thread died!"
                p.seek(0)
                accum += float(p.readline().split()[0])
                samples += 1
                
        finally:
            # stop cyclictest
            self.cyclictest.stopevent.set()
            
            # stop the loads
            self.stop_loads()

        end = datetime.now()
        duration = end - start
        self.genxml(duration, accum, samples)
        self.report()
        if self.sysreport:
            self.run_sysreport()

        # if --xmlrpc-submit | -X was given, send our report to this host
        if self.xmlrpchost:
            url = "http://%s/rteval/API1/" % self.xmlrpchost

            client = rtevalclient.rtevalclient(url)
            print "Submitting report to %s" % url
            rterid = client.SendReport(self.xmlreport.GetXMLdocument())
            print "Report registered with rterid %i" % rterid


    def tar_results(self):
        if not os.path.isdir(self.reportdir):
            raise RuntimeError, "no such directory: %s" % self.reportdir
        import tarfile
        dirname = os.path.dirname(self.reportdir)
        rptdir = os.path.basename(self.reportdir)
        cwd = os.getcwd()
        os.chdir(dirname)
        try:
            t = tarfile.open(rptdir + ".tar.bz2", "w:bz2")
            t.add(rptdir)
            t.close()
        except:
            os.chdir(cwd)

    def oprofile_setup(self):
        if self.runoprofile == False:
            return
        rel = os.uname()[2]
        vmlinux = os.path.join('/usr/lib/debug/lib/modules', rel)
        if not os.path.exists(vmlinux):
            print "Can't run oprofile. Load kernel-rt-debuginfo packages."
            return
        ret = subprocess.call(['opcontrol', '--init'])
        if ret:
            print "failed to run opcontrol --init! is oprofile installed?"
            return
        ret = subprocess.call(['opcontrol', '--vmlinux=%s' % vmlinux])
        if ret:
            print "opcontrol failed to set vmlinux image: %s" % vmlinux
            return

    def rteval(self):
        (opts, args) = self.parse_options()
        workdir  = opts.workdir
        self.loaddir  = opts.loaddir
        self.verbose  = opts.verbose
        self.debugging = opts.debugging
        self.duration = opts.duration
        self.sysreport = opts.sysreport
        self.xmlrpchost = opts.xmlrpchost

        # if --summarize was specified then just parse the XML, print it and exit
        if opts.summarize:
            if len(args) < 1:
                raise RuntimeError, "Must specify at least one XML file with --summarize!"
            for x in args:
                self.summarize(x)
            sys.exit(0)

        if os.getuid() != 0:
            raise RuntimeError, "Must be root to run evaluator!"

        self.debug('''rteval options: 
        workdir: %s
        loaddir: %s
        verbose: %s
        debugging: %s
        duration: %f
        sysreport: %s''' % (workdir, self.loaddir, self.verbose, 
                            self.debugging, self.duration, self.sysreport))

        if not os.path.isdir(workdir):
            raise RuntimeError, "work directory %d does not exist" % workdir

        if workdir != self.topdir:
            self.topdir = workdir

        if self.runlatency: 
            self.measure_latency()
            self.get_dmesg()
            self.tar_results()
        

if __name__ == '__main__':
    import pwd, grp

    try:
        RtEval().rteval()
    except KeyboardInterrupt:
        sys.exit(0)