Commit 5db7e9ce authored by Andreas Zwinkau's avatar Andreas Zwinkau
Browse files

initial version

parents
#!/usr/bin/env python
import os
import platform
import optparse # TODO argparse from python 2.7 on
from zsnippets.shell import execute
SCRIPT_VERSION="0.1"
(KERNEL,HOSTNAME,KERNEL_VERSION,VERSION,ARCH,PROCESSOR) = platform.uname()
assert not PROCESSOR, PROCESSOR
LINUX_NAME, LINUX_VERSION, LINUX_CODENAME = platform.dist()
BITSIZE = {
"i686": 32,
"x86_64": 64,
}.get(ARCH, "unspecified for "+ARCH)
# assert spec dir
for subdir in "benchspec config bin result".split():
assert os.path.isdir(subdir), "subdir missing: "+subdir
for filename in "shrc version.txt README".split():
assert os.path.isfile(filename), "file missing: "+filename
parser = optparse.OptionParser(version=SCRIPT_VERSION)
parser.add_option("--build-only", action="store_true", default=False,
help="Just build SPEC programs, but do not run")
parser.add_option("--c-compiler", metavar="<CC>", default="cparser",
help="Use <CC> to build benchmark programs")
parser.add_option("--fortran-compiler", metavar="<FC>", default="gfortran",
help="Use <FC> to build benchmark programs")
parser.add_option("--cpp-compiler", metavar="<CXX>", default="g++",
help="Use <CXX> to build benchmark programs")
parser.add_option("--compiler-args", metavar="<FLAGS>", default="-O3",
help="Append <FLAGS> to build benchmark programs for all compilers")
parser.add_option("--c-compiler-args", metavar="<CFLAGS>", default="",
help="Append <CFLAGS> to build benchmark programs for C compiler")
parser.add_option("--fortran-compiler-args", metavar="<FFLAGS>", default="",
help="Append <FFLAGS> to build benchmark programs for Fortran compiler")
parser.add_option("--cpp-compiler-args", metavar="<CFLAGS>", default="",
help="Append <CFLAGS> to build benchmark programs for C++ compiler")
# http://www.spec.org/cpu2006/Docs/config.html
_CONFIG_HEAD = """\
#### Automatically generated by spec.py script ####
output_format = csv,html,raw,screen,config
ignore_errors = yes
makeflags = -j4 # allows multiple simultaneous compiles
# just assuming:
hw_fpu = Integrated
sw_state = Multi-user
"""
def path_to_filesystem(path):
for line in execute("stat -f ."):
if "Type:" in line:
i = line.index("Type:")
return line[i+6:].strip()
def _get_proc_info(path):
"""Extract data from proc files"""
data = dict()
for line in file(path):
try:
key, value = line.split(":")
except ValueError:
continue
data[key.strip()] = value.strip()
return data
def get_mem_info():
"""Return data about Memory as dict"""
return _get_proc_info("/proc/meminfo")
def get_cpu_info(): # TODO ignores multiple cpus
"""Return data about CPU as dict"""
return _get_proc_info("/proc/cpuinfo")
def get_compiler_version(compiler):
"""Get the version from a compiler executable"""
d = execute(compiler+" --version")
for line in d:
return line
def get_sw_compiler(options):
"""Generate a string with all compiler versions"""
return "; ".join([
get_compiler_version(options.c_compiler),
get_compiler_version(options.cpp_compiler),
get_compiler_version(options.fortran_compiler)
])
def get_portability():
"""Get necessary portability flags"""
contents = ["\n\n#### Portability Flags ####"]
if LINUX_NAME == "Ubuntu":
if BITSIZE == 32:
contents.append("""\
400.perlbench=default=default=default:
CPORTABILITY= -DSPEC_CPU_LINUX_IA32
462.libquantum=default=default=default:
CPORTABILITY= -DSPEC_CPU_LINUX
483.xalancbmk=default=default=default:
CXXPORTABILITY= -DSPEC_CPU_LINUX
481.wrf=default=default=default:
CPORTABILITY = -DSPEC_CPU_CASE_FLAG -DSPEC_CPU_LINUX
""")
elif BITSIZE == 64:
contents.append("""\
default=base=default=default:
PORTABILITY = -DSPEC_CPU_LP64
400.perlbench=default=default=default:
CPORTABILITY = -DSPEC_CPU_LINUX_X64
462.libquantum=default=default=default:
CPORTABILITY= -DSPEC_CPU_LINUX
483.xalancbmk=default=default=default:
CXXPORTABILITY= -DSPEC_CPU_LINUX
481.wrf=default=default=default:
CPORTABILITY = -DSPEC_CPU_CASE_FLAG -DSPEC_CPU_LINUX
""")
return "\n".join(contents)
def generate_spec_config(options):
"""Generate a config file for SPEC"""
contents = [_CONFIG_HEAD]
cpuinfo = get_cpu_info()
meminfo = get_mem_info()
contents.append("sw_os = %s %s %s" % (LINUX_NAME, LINUX_VERSION, LINUX_CODENAME))
contents.append("sw_compiler = %s" % get_sw_compiler(options))
contents.append("sw_file = %s" % path_to_filesystem("."))
contents.append("sw_base_ptrsize = %d-bit" % BITSIZE)
contents.append("sw_peak_ptrsize = %d-bit" % BITSIZE)
contents.append("")
contents.append("hw_cpu_name = %s" % cpuinfo["model name"])
contents.append("hw_cpu_mhz = %s" % cpuinfo["cpu MHz"])
contents.append("hw_nchips = %d" % (1+int(cpuinfo["processor"])))
contents.append("hw_ncores = %d" % int(cpuinfo["cpu cores"]))
contents.append("hw_vendor = %s" % cpuinfo["vendor_id"])
contents.append("hw_memory = %s" % meminfo["MemTotal"])
contents.append("")
contents.append("CC = %s" % options.c_compiler)
contents.append("CXX = %s" % options.cpp_compiler)
contents.append("FC = %s" % options.fortran_compiler)
contents.append("")
contents.append("OPTIMIZE = %s" % options.compiler_args)
contents.append("notes_comp_001 OPTIMIZE = %s" % options.compiler_args)
contents.append("COPTIMIZE = %s" % options.c_compiler_args)
contents.append("notes_comp_002 COPTIMIZE = %s" % options.c_compiler_args)
contents.append("CXXOPTIMIZE = %s" % options.cpp_compiler_args)
contents.append("notes_comp_003 CXXOPTIMIZE = %s" % options.cpp_compiler_args)
contents.append("FOPTIMIZE = %s" % options.fortran_compiler_args)
contents.append("notes_comp_004 FOPTIMIZE = %s" % options.fortran_compiler_args)
contents.append(get_portability())
return "\n".join(contents)
if __name__ == "__main__":
(options, args) = parser.parse_args()
conf = generate_spec_config(options)
with open("config/generated.cfg", 'w') as fh:
fh.write(conf)
cmd = "bash -c 'source shrc && runspec all -c generated'"
os.system(cmd)
"""
Convenience function
Alternative to subprocess and os.system
"""
import subprocess
import resource
import sys
class SigKill(Exception):
def __init__(self, retcode, name):
self.retcode = retcode
self.name = name
_EXIT_CODES = {
-2: "keyboard interrupt",
-3: "keyboard quit",
-4: "illegal instruction",
-6: "aborted",
-8: "floating point exception",
-9: "kill",
-11: "segfault",
-13: "broken pipe",
-14: "timeout",
-15: "termination",
}
def execute(cmd, env=None, timeout=0):
"""Execute a command and return stderr and/or stdout data"""
preexec_fn = None
if timeout > 0.0:
def set_timeout():
resource.setrlimit(resource.RLIMIT_CPU, (timeout, timeout))
preexec_fn = set_timeout
cmd = filter(lambda x: x, cmd.split(' '))
proc = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
preexec_fn = preexec_fn,
env=env)
out, err = proc.communicate()
try:
for line in out.splitlines():
yield line
except Exception, e:
print "'%s' -> %s" % (c, e)
if proc.returncode in _EXIT_CODES:
raise SigKill(proc.returncode, _EXIT_CODES[proc.returncode])
elif proc.returncode < 0: # program aborted
raise Exception(proc.returncode, cmd)
def silent_shell(cmd, env=None, debug=False):
"""Execute a shell command"""
if debug:
print "silent_shell", cmd
stdout = None
stderr = None
else:
stdout = open("/dev/null", 'a')
stderr = subprocess.STDOUT
try:
return subprocess.call(cmd, shell=True, stdout=stdout, stderr=stderr, env=env)
except OSError, e:
print >>sys.stderr, "Execution failed:", e
def write_file(filename, content):
fh = open(filename, 'w')
fh.write(content)
fh.close()
if __name__ == "__main__":
for line in execute("hostname"):
print line
for line in execute("hostname", True, True):
print line
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment