summaryrefslogtreecommitdiffstats
path: root/rteval/modules/measurement/HWLatDetect.py
blob: bb4447a443c4151064198c7dfacfee69d7ea2312 (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
#
#   HWLatDetect.py - Runs hwlatdetect and prepares the result for rteval
#
#   Copyright 2012 - 2013   David Sommerseth <davids@redhat.com>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License along
#   with this program; if not, write to the Free Software Foundation, Inc.,
#   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#   For the avoidance of doubt the "preferred form" of this code is one which
#   is in an open unpatent encumbered format. Where cryptographic key signing
#   forms part of the process of creating an executable the information
#   including keys needed to generate an equivalently functional executable
#   are deemed to be part of the source code.
#

import os, sys, libxml2
from rteval.modules import rtevalModulePrototype
from rteval.Log import Log


class HWLatDetectRunner(rtevalModulePrototype):
    def __init__(self, config, logger=None):
        rtevalModulePrototype.__init__(self, 'measurement', 'hwlatdetect', logger)

        self.__cfg = config
        self.__hwlat = None
        self.__exceeding = None
        self.__samples = []


    def _WorkloadSetup(self):
        try:
            import hwlatdetect
            self.__hwlat = hwlatdetect.Detector()
        except Exception, e:
            self._log(Log.WARN, "hwlatdetect could not be loaded.  Will not run hwlatdetect")
            self._log(Log.DEBUG, str(e))


    def _WorkloadBuild(self):
        self._setReady()


    def _WorkloadPrepare(self):
        self.__running = False
        if self.__hwlat is None:
            return

        self._log(Log.DEBUG, "Preparing hwlatdetect")
        self.__hwlat.set('threshold', int(self.__cfg.setdefault('threshold', 15)))
        self.__hwlat.set('window', int(self.__cfg.setdefault('window', 1000000)))
        self.__hwlat.set('width', int(self.__cfg.setdefault('width', 800000)))
        self.__hwlat.testduration = int(self.__cfg.setdefault('duration', 10))
        self.__hwlat.setup()

        self._log(Log.DEBUG, "HWLatDetect settings: \n"
                  + "\t threshold: %i\n" % (self.__cfg.threshold)
                  + "\t window:    %i\n" % (self.__cfg.window)
                  + "\t width:     %i\n" % (self.__cfg.width)
                  + "\t duration:  %i" % (self.__cfg.duration))


    def _WorkloadTask(self):
        if self.__hwlat is None:
            return
        if self.__running:
            return

        self.__running = True
        self.__hwlat.detect()


    def WorkloadAlive(self):
        return self.__running


    def _WorkloadCleanup(self):
        if not self.__hwlat or not self.__running:
            self._setFinished()
            return

        self._log(Log.DEBUG, "Parsing results")
        # Grab the measurement results
        self.__exceeding = self.__hwlat.get("count")
        for s in self.__hwlat.samples:
            self.__samples.append(s.split('\t'))

        self.__running = False
        self._setFinished()


    def MakeReport(self):
        rep_n = libxml2.newNode('hwlatdetect')
        rep_n.newProp('format', '1.0')

        if self.__hwlat is None:
            rep_n.newProp('aborted', '1')
            return rep_n

        runp_n = rep_n.newChild(None, 'RunParams', None)
        runp_n.newProp('threshold', str(self.__hwlat.get('threshold')))
        runp_n.newProp('window', str(self.__hwlat.get('window')))
        runp_n.newProp('width', str(self.__hwlat.get('width')))
        runp_n.newProp('duration', str(self.__hwlat.testduration))

        sn = rep_n.newChild(None, 'samples', None)
        sn.newProp('exceeding', str(self.__exceeding))
        sn.newProp('count', str(len(self.__samples)))
        for s in self.__samples:
            n = sn.newChild(None, 'sample', None)
            n.newProp('timestamp', s[0])
            n.newProp('duration', s[1])

        return rep_n



def ModuleInfo():
    return {"parallel": False,
            "loads": False}



def ModuleParameters():
    return {"threshold": {"descr": "Specify the TSC gap in microseconds used to detect an SMI",
                          "default": 15,
                          "metavar": "MICROSEC"},
            "window":    {"descr": "Sample window size (in microseconds)",
                          "default": 1000000,
                          "metavar": "MICROSEC"},
            "width":     {"descr": "Sampling width inside the sampling window (in microseconds)",
                          "default": 800000,
                          "metavar": "MICROSEC"},
            "duration":  {"descr": "How long in seconds to run hwlatdetect",
                          "default": 15,
                          "metavar": "SEC"}
        }



def create(params, logger):
    return HWLatDetectRunner(params, logger)


if __name__ == '__main__':
    from rtevalConfig import rtevalConfig
    l = Log()
    l.SetLogVerbosity(Log.INFO|Log.DEBUG|Log.ERR|Log.WARN)
    cfg = rtevalConfig({}, logger=l)
    c = HWLatDetectRunner(cfg, l)
    c._WorkloadSetup()
    c._WorkloadPrepare()
    c._WorkloadTask()
    c._WorkloadCleanup()
    rep_n = c.MakeReport()

    xml = libxml2.newDoc('1.0')
    xml.setRootElement(rep_n)
    xml.saveFormatFileEnc('-','UTF-8',1)