__init__.py revision 11923
16657Snate@binkert.org# Copyright (c) 2016 ARM Limited 26657Snate@binkert.org# All rights reserved. 310972Sdavid.hashe@amd.com# 46657Snate@binkert.org# The license below extends only to copyright in the software and shall 56657Snate@binkert.org# not be construed as granting a license to any other intellectual 66657Snate@binkert.org# property including but not limited to intellectual property relating 76657Snate@binkert.org# to a hardware implementation of the functionality of the software 86657Snate@binkert.org# licensed hereunder. You may use the software subject to the license 96657Snate@binkert.org# terms below provided that you ensure that this notice is replicated 106657Snate@binkert.org# unmodified and in its entirety in all distributions of the software, 116657Snate@binkert.org# modified or unmodified, in source code or in binary form. 126657Snate@binkert.org# 136657Snate@binkert.org# Copyright (c) 2008-2009 The Hewlett-Packard Development Company 146657Snate@binkert.org# Copyright (c) 2004-2006 The Regents of The University of Michigan 156657Snate@binkert.org# All rights reserved. 166657Snate@binkert.org# 176657Snate@binkert.org# Redistribution and use in source and binary forms, with or without 186657Snate@binkert.org# modification, are permitted provided that the following conditions are 196657Snate@binkert.org# met: redistributions of source code must retain the above copyright 206657Snate@binkert.org# notice, this list of conditions and the following disclaimer; 216657Snate@binkert.org# redistributions in binary form must reproduce the above copyright 226657Snate@binkert.org# notice, this list of conditions and the following disclaimer in the 236657Snate@binkert.org# documentation and/or other materials provided with the distribution; 246657Snate@binkert.org# neither the name of the copyright holders nor the names of its 256657Snate@binkert.org# contributors may be used to endorse or promote products derived from 266657Snate@binkert.org# this software without specific prior written permission. 276657Snate@binkert.org# 286657Snate@binkert.org# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 296999Snate@binkert.org# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 306657Snate@binkert.org# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 316657Snate@binkert.org# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 326657Snate@binkert.org# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 336657Snate@binkert.org# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 348189SLisa.Hsu@amd.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 356657Snate@binkert.org# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 369499Snilay@cs.wisc.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 379499Snilay@cs.wisc.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3811308Santhony.gutierrez@amd.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 399364Snilay@cs.wisc.edu# 407055Snate@binkert.org# Authors: Nathan Binkert 416882SBrad.Beckmann@amd.com 426882SBrad.Beckmann@amd.comimport os 438191SLisa.Hsu@amd.comimport re 446882SBrad.Beckmann@amd.comimport sys 4511308Santhony.gutierrez@amd.com 4611308Santhony.gutierrez@amd.comimport convert 476882SBrad.Beckmann@amd.comimport jobfile 4811308Santhony.gutierrez@amd.com 499102SNuwan.Jayasena@amd.comfrom attrdict import attrdict, multiattrdict, optiondict 5011084Snilay@cs.wisc.edufrom code_formatter import code_formatter 519366Snilay@cs.wisc.edufrom multidict import multidict 529499Snilay@cs.wisc.edufrom orderdict import orderdict 539499Snilay@cs.wisc.edufrom smartdict import SmartDict 549499Snilay@cs.wisc.edufrom sorteddict import SortedDict 556882SBrad.Beckmann@amd.com 566657Snate@binkert.org# panic() should be called when something happens that should never 576657Snate@binkert.org# ever happen regardless of what the user does (i.e., an acutal m5 586657Snate@binkert.org# bug). 596657Snate@binkert.orgdef panic(fmt, *args): 6010311Snilay@cs.wisc.edu print >>sys.stderr, 'panic:', fmt % args 6110311Snilay@cs.wisc.edu sys.exit(1) 6210311Snilay@cs.wisc.edu 6310311Snilay@cs.wisc.edu# fatal() should be called when the simulation cannot continue due to 646657Snate@binkert.org# some condition that is the user's fault (bad configuration, invalid 6510311Snilay@cs.wisc.edu# arguments, etc.) and not a simulator bug. 669366Snilay@cs.wisc.edudef fatal(fmt, *args): 677839Snilay@cs.wisc.edu print >>sys.stderr, 'fatal:', fmt % args 686657Snate@binkert.org sys.exit(1) 696882SBrad.Beckmann@amd.com 7010308Snilay@cs.wisc.edu# warn() should be called when the user should be warned about some condition 7110308Snilay@cs.wisc.edu# that may or may not be the user's fault, but that they should be made aware 726882SBrad.Beckmann@amd.com# of as it may affect the simulation or results. 7310308Snilay@cs.wisc.edudef warn(fmt, *args): 7410308Snilay@cs.wisc.edu print >>sys.stderr, 'warn:', fmt % args 7510308Snilay@cs.wisc.edu 7610308Snilay@cs.wisc.edu# inform() should be called when the user should be informed about some 7710308Snilay@cs.wisc.edu# condition that they may be interested in. 789366Snilay@cs.wisc.edudef inform(fmt, *args): 799366Snilay@cs.wisc.edu print >>sys.stdout, 'info:', fmt % args 806657Snate@binkert.org 816657Snate@binkert.orgclass Singleton(type): 826657Snate@binkert.org def __call__(cls, *args, **kwargs): 836657Snate@binkert.org if hasattr(cls, '_instance'): 849104Shestness@cs.utexas.edu return cls._instance 856657Snate@binkert.org 866657Snate@binkert.org cls._instance = super(Singleton, cls).__call__(*args, **kwargs) 876657Snate@binkert.org return cls._instance 8810311Snilay@cs.wisc.edu 8910311Snilay@cs.wisc.edudef addToPath(path): 9010311Snilay@cs.wisc.edu """Prepend given directory to system module search path. We may not 9110311Snilay@cs.wisc.edu need this anymore if we can structure our config library more like a 926657Snate@binkert.org Python package.""" 937839Snilay@cs.wisc.edu 947839Snilay@cs.wisc.edu # if it's a relative path and we know what directory the current 9510972Sdavid.hashe@amd.com # python script is in, make the path relative to that directory. 9610972Sdavid.hashe@amd.com if not os.path.isabs(path) and sys.path[0]: 9710972Sdavid.hashe@amd.com path = os.path.join(sys.path[0], path) 986657Snate@binkert.org path = os.path.realpath(path) 996657Snate@binkert.org # sys.path[0] should always refer to the current script's directory, 1006657Snate@binkert.org # so place the new dir right after that. 1016657Snate@binkert.org sys.path.insert(1, path) 1026657Snate@binkert.org 1036657Snate@binkert.org# Apply method to object. 1046657Snate@binkert.org# applyMethod(obj, 'meth', <args>) is equivalent to obj.meth(<args>) 1056657Snate@binkert.orgdef applyMethod(obj, meth, *args, **kwargs): 1066657Snate@binkert.org return getattr(obj, meth)(*args, **kwargs) 1076657Snate@binkert.org 1086657Snate@binkert.org# If the first argument is an (non-sequence) object, apply the named 1096657Snate@binkert.org# method with the given arguments. If the first argument is a 1106657Snate@binkert.org# sequence, apply the method to each element of the sequence (a la 1116657Snate@binkert.org# 'map'). 1126657Snate@binkert.orgdef applyOrMap(objOrSeq, meth, *args, **kwargs): 1136657Snate@binkert.org if not isinstance(objOrSeq, (list, tuple)): 1146657Snate@binkert.org return applyMethod(objOrSeq, meth, *args, **kwargs) 1156657Snate@binkert.org else: 1166779SBrad.Beckmann@amd.com return [applyMethod(o, meth, *args, **kwargs) for o in objOrSeq] 1176657Snate@binkert.org 1186657Snate@binkert.orgdef compareVersions(v1, v2): 1196657Snate@binkert.org """helper function: compare arrays or strings of version numbers. 1206657Snate@binkert.org E.g., compare_version((1,3,25), (1,4,1)') 1216657Snate@binkert.org returns -1, 0, 1 if v1 is <, ==, > v2 1226657Snate@binkert.org """ 1236657Snate@binkert.org def make_version_list(v): 1246657Snate@binkert.org if isinstance(v, (list,tuple)): 1256657Snate@binkert.org return v 12610972Sdavid.hashe@amd.com elif isinstance(v, str): 12710972Sdavid.hashe@amd.com return map(lambda x: int(re.match('\d+', x).group()), v.split('.')) 12810972Sdavid.hashe@amd.com else: 1299104Shestness@cs.utexas.edu raise TypeError 1309104Shestness@cs.utexas.edu 1319104Shestness@cs.utexas.edu v1 = make_version_list(v1) 1329104Shestness@cs.utexas.edu v2 = make_version_list(v2) 1336657Snate@binkert.org # Compare corresponding elements of lists 1346657Snate@binkert.org for n1,n2 in zip(v1, v2): 1356657Snate@binkert.org if n1 < n2: return -1 1366657Snate@binkert.org if n1 > n2: return 1 1376657Snate@binkert.org # all corresponding values are equal... see if one has extra values 1386657Snate@binkert.org if len(v1) < len(v2): return -1 1396657Snate@binkert.org if len(v1) > len(v2): return 1 1406657Snate@binkert.org return 0 1416657Snate@binkert.org 1426657Snate@binkert.orgdef crossproduct(items): 1436657Snate@binkert.org if len(items) == 1: 1446657Snate@binkert.org for i in items[0]: 1456657Snate@binkert.org yield (i,) 14610307Snilay@cs.wisc.edu else: 1476657Snate@binkert.org for i in items[0]: 1486657Snate@binkert.org for j in crossproduct(items[1:]): 1497839Snilay@cs.wisc.edu yield (i,) + j 1507839Snilay@cs.wisc.edu 1517839Snilay@cs.wisc.edudef flatten(items): 1527839Snilay@cs.wisc.edu while items: 1537839Snilay@cs.wisc.edu item = items.pop(0) 1547839Snilay@cs.wisc.edu if isinstance(item, (list, tuple)): 1557839Snilay@cs.wisc.edu items[0:0] = item 1567839Snilay@cs.wisc.edu else: 1577839Snilay@cs.wisc.edu yield item 1587839Snilay@cs.wisc.edu 15910968Sdavid.hashe@amd.com# force scalars to one-element lists for uniformity 16010968Sdavid.hashe@amd.comdef makeList(objOrList): 16110968Sdavid.hashe@amd.com if isinstance(objOrList, list): 16210968Sdavid.hashe@amd.com return objOrList 16310968Sdavid.hashe@amd.com return [objOrList] 16410968Sdavid.hashe@amd.com 16510968Sdavid.hashe@amd.comdef printList(items, indent=4): 1667839Snilay@cs.wisc.edu line = ' ' * indent 1676657Snate@binkert.org for i,item in enumerate(items): 1686657Snate@binkert.org if len(line) + len(item) > 76: 1696657Snate@binkert.org print line 1706657Snate@binkert.org line = ' ' * indent 1716657Snate@binkert.org 1726657Snate@binkert.org if i < len(items) - 1: 1736657Snate@binkert.org line += '%s, ' % item 1746657Snate@binkert.org else: 1756657Snate@binkert.org line += item 1766657Snate@binkert.org print line 1776657Snate@binkert.org 1786657Snate@binkert.orgdef readCommand(cmd, **kwargs): 1796657Snate@binkert.org """run the command cmd, read the results and return them 1806657Snate@binkert.org this is sorta like `cmd` in shell""" 1816657Snate@binkert.org from subprocess import Popen, PIPE, STDOUT 1826657Snate@binkert.org 1836657Snate@binkert.org if isinstance(cmd, str): 1846657Snate@binkert.org cmd = cmd.split() 1856657Snate@binkert.org 1866657Snate@binkert.org no_exception = 'exception' in kwargs 1876657Snate@binkert.org exception = kwargs.pop('exception', None) 1886657Snate@binkert.org 1896657Snate@binkert.org kwargs.setdefault('shell', False) 1906657Snate@binkert.org kwargs.setdefault('stdout', PIPE) 1916657Snate@binkert.org kwargs.setdefault('stderr', STDOUT) 1926657Snate@binkert.org kwargs.setdefault('close_fds', True) 1936657Snate@binkert.org try: 1946657Snate@binkert.org subp = Popen(cmd, **kwargs) 1956657Snate@binkert.org except Exception, e: 1966657Snate@binkert.org if no_exception: 19710963Sdavid.hashe@amd.com return exception 19810963Sdavid.hashe@amd.com raise 19910963Sdavid.hashe@amd.com 20010963Sdavid.hashe@amd.com return subp.communicate()[0] 20110963Sdavid.hashe@amd.com 20210963Sdavid.hashe@amd.comdef makeDir(path): 20311095Snilay@cs.wisc.edu """Make a directory if it doesn't exist. If the path does exist, 20410963Sdavid.hashe@amd.com ensure that it is a directory""" 20510963Sdavid.hashe@amd.com if os.path.exists(path): 20610963Sdavid.hashe@amd.com if not os.path.isdir(path): 20710963Sdavid.hashe@amd.com raise AttributeError, "%s exists but is not directory" % path 20810963Sdavid.hashe@amd.com else: 20910963Sdavid.hashe@amd.com os.mkdir(path) 21010963Sdavid.hashe@amd.com 21110963Sdavid.hashe@amd.comdef isInteractive(): 2129219Spower.jg@gmail.com """Check if the simulator is run interactively or in a batch environment""" 2136877Ssteve.reinhardt@amd.com 2146657Snate@binkert.org return sys.__stdin__.isatty() 2159219Spower.jg@gmail.com