summaryrefslogtreecommitdiffstats
path: root/rteval/modules/loads/hackbench.py
blob: 53c4739de0893838d74c3928ed44be26ede294a3 (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
#  
#   hackbench.py - class to manage an instance of hackbench load
#
#   Copyright 2009 - 2012  Clark Williams <williams@redhat.com>
#   Copyright 2009 - 2012  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307 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 sys, os, time, glob, subprocess, errno
from signal import SIGTERM, SIGKILL
from rteval.modules.loads import CommandLineLoad
from rteval.Log import Log


class Hackbench(CommandLineLoad):
    def __init__(self, config, logger):
        CommandLineLoad.__init__(self, "hackbench", config, logger)


    def _WorkloadSetup(self):
        'calculate arguments based on input parameters'
        (mem, units) = self.memsize
        if units == 'KB':
            mem = mem / (1024.0 * 1024.0)
        elif units == 'MB':
            mem = mem / 1024.0
        elif units == 'TB':
            mem = mem * 1024
        ratio = float(mem) / float(self.num_cpus)
        if ratio >= 0.75:
            mult = float(self._cfg.setdefault('jobspercore', 2))
        else:
            self._log(Log.INFO, "hackbench: low memory system (%f GB/core)! Not running\n" % ratio)
            mult = 0
            self._donotrun = True

        self.jobs = self.num_cpus * mult

        self.args = ['hackbench',  '-P',
                     '-g', str(self.jobs), 
                     '-l', str(self._cfg.setdefault('loops', '100')),
                     '-s', str(self._cfg.setdefault('datasize', '100'))
                     ]
        self.__err_sleep = 5.0


    def _WorkloadBuild(self):
        # Nothing to build, so we're basically ready
        self._setReady()


    def _WorkloadPrepare(self):
        self.__nullfp = os.open("/dev/null", os.O_RDWR)
        if self._logging:
            self.__out = self.open_logfile("hackbench.stdout")
            self.__err = self.open_logfile("hackbench.stderr")
        else:
            self.__out = self.__err = self.__nullfp

        self._log(Log.DEBUG, "starting loop (jobs: %d)" % self.jobs)


    def _WorkloadTask(self):
        if self.shouldStop():
            return

        self._log(Log.DEBUG, "running: %s" % " ".join(self.args))
        try:
            self.__hbproc = subprocess.Popen(self.args,
                                             stdin=self.__nullfp,
                                             stdout=self.__out,
                                             stderr=self.__err)
        except OSError, e:
            if e.errno != errno.ENOMEM:
                raise e
            # Catch out-of-memory errors and wait a bit to (hopefully)
            # ease memory pressure
            self._log(Log.DEBUG, "hackbench: %s, sleeping for %f seconds" % (e.strerror, self.__err_sleep))
            time.sleep(self.__err_sleep)
            if self.__err_sleep < 60.0:
                self.__err_sleep *= 2.0
            if self.__err_sleep > 60.0:
                self.__err_sleep = 60.0


    def WorkloadAlive(self):
        # As hackbench is short-lived, lets pretend it is always alive
        return True


    def _WorkloadCleanup(self):
        if self._donotrun:
            return

        if self.__hbproc.poll() == None:
            os.kill(self.__hbproc.pid, SIGKILL)
        self.__hbproc.wait()

        os.close(self.__nullfp)
        if self._logging:
            os.close(self.__out)
            del self.__out
            os.close(self.__err)
            del self.__err

        del self.__hbproc
        del self.__nullfp



def ModuleParameters():
    return {"jobspercore": {"descr": "Number of working threads per CPU core",
                            "default": 5,
                            "metavar": "NUM"}
            }



def create(config, logger):
    return Hackbench(config, logger)


if __name__ == '__main__':
    h = Hackbench(params={'debugging':True, 'verbose':True})
    h.run()