params.py

来自「M5,一个功能强大的多处理器系统模拟器.很多针对处理器架构,性能的研究都使用它作」· Python 代码 · 共 1,172 行 · 第 1/3 页

PY
1,172
字号
            return self        if attr == 'frequency':            return Frequency(self)        raise AttributeError, "Latency object has no attribute '%s'" % attr    def getValue(self):        if self.ticks or self.value == 0:            value = self.value        else:            value = ticks.fromSeconds(self.value)        return long(value)    # convert latency to ticks    def ini_str(self):        return '%d' % self.getValue()class Frequency(TickParamValue):    def __init__(self, value):        if isinstance(value, (Latency, Clock)):            if value.value == 0:                self.value = 0            else:                self.value = 1.0 / value.value            self.ticks = value.ticks        elif isinstance(value, Frequency):            self.value = value.value            self.ticks = value.ticks        else:            self.ticks = False            self.value = convert.toFrequency(value)    def __getattr__(self, attr):        if attr == 'frequency':            return self        if attr in ('latency', 'period'):            return Latency(self)        raise AttributeError, "Frequency object has no attribute '%s'" % attr    # convert latency to ticks    def getValue(self):        if self.ticks or self.value == 0:            value = self.value        else:            value = ticks.fromSeconds(1.0 / self.value)        return long(value)    def ini_str(self):        return '%d' % self.getValue()# A generic frequency and/or Latency value.  Value is stored as a latency,# but to avoid ambiguity this object does not support numeric ops (* or /).# An explicit conversion to a Latency or Frequency must be made first.class Clock(ParamValue):    cxx_type = 'Tick'    cxx_predecls = ['#include "sim/host.hh"']    swig_predecls = ['%import "stdint.i"\n' +                     '%import "sim/host.hh"']    def __init__(self, value):        if isinstance(value, (Latency, Clock)):            self.ticks = value.ticks            self.value = value.value        elif isinstance(value, Frequency):            self.ticks = value.ticks            self.value = 1.0 / value.value        elif value.endswith('t'):            self.ticks = True            self.value = int(value[:-1])        else:            self.ticks = False            self.value = convert.anyToLatency(value)    def __getattr__(self, attr):        if attr == 'frequency':            return Frequency(self)        if attr in ('latency', 'period'):            return Latency(self)        raise AttributeError, "Frequency object has no attribute '%s'" % attr    def getValue(self):        return self.period.getValue()    def ini_str(self):        return self.period.ini_str()class NetworkBandwidth(float,ParamValue):    cxx_type = 'float'    def __new__(cls, value):        # convert to bits per second        val = convert.toNetworkBandwidth(value)        return super(cls, NetworkBandwidth).__new__(cls, val)    def __str__(self):        return str(self.val)    def getValue(self):        # convert to seconds per byte        value = 8.0 / float(self)        # convert to ticks per byte        value = ticks.fromSeconds(value)        return float(value)    def ini_str(self):        return '%f' % self.getValue()class MemoryBandwidth(float,ParamValue):    cxx_type = 'float'    def __new__(self, value):        # we want the number of ticks per byte of data        val = convert.toMemoryBandwidth(value)        return super(cls, MemoryBandwidth).__new__(cls, val)    def __str__(self):        return str(self.val)    def getValue(self):        # convert to seconds per byte        value = 1.0 / float(self)        # convert to ticks per byte        value = ticks.fromSeconds(value)        return float(value)    def ini_str(self):        return '%f' % self.getValue()## "Constants"... handy aliases for various values.## Special class for NULL pointers.  Note the special check in# make_param_value() above that lets these be assigned where a# SimObject is required.# only one copy of a particular nodeclass NullSimObject(object):    __metaclass__ = Singleton    def __call__(cls):        return cls    def _instantiate(self, parent = None, path = ''):        pass    def ini_str(self):        return 'Null'    def unproxy(self, base):        return self    def set_path(self, parent, name):        pass    def __str__(self):        return 'Null'    def getValue(self):        return None# The only instance you'll ever need...NULL = NullSimObject()def isNullPointer(value):    return isinstance(value, NullSimObject)# Some memory range specifications use this as a default upper bound.MaxAddr = Addr.maxMaxTick = Tick.maxAllMemory = AddrRange(0, MaxAddr)####################################################################### Port objects## Ports are used to interconnect objects in the memory system.####################################################################### Port reference: encapsulates a reference to a particular port on a# particular SimObject.class PortRef(object):    def __init__(self, simobj, name):        assert(isSimObject(simobj) or isSimObjectClass(simobj))        self.simobj = simobj        self.name = name        self.peer = None   # not associated with another port yet        self.ccConnected = False # C++ port connection done?        self.index = -1  # always -1 for non-vector ports    def __str__(self):        return '%s.%s' % (self.simobj, self.name)    # for config.ini, print peer's name (not ours)    def ini_str(self):        return str(self.peer)    def __getattr__(self, attr):        if attr == 'peerObj':            # shorthand for proxies            return self.peer.simobj        raise AttributeError, "'%s' object has no attribute '%s'" % \              (self.__class__.__name__, attr)    # Full connection is symmetric (both ways).  Called via    # SimObject.__setattr__ as a result of a port assignment, e.g.,    # "obj1.portA = obj2.portB", or via VectorPortElementRef.__setitem__,    # e.g., "obj1.portA[3] = obj2.portB".    def connect(self, other):        if isinstance(other, VectorPortRef):            # reference to plain VectorPort is implicit append            other = other._get_next()        if self.peer and not proxy.isproxy(self.peer):            print "warning: overwriting port", self, \                  "value", self.peer, "with", other        self.peer = other        if proxy.isproxy(other):            other.set_param_desc(PortParamDesc())        elif isinstance(other, PortRef):            if other.peer is not self:                other.connect(self)        else:            raise TypeError, \                  "assigning non-port reference '%s' to port '%s'" \                  % (other, self)    def clone(self, simobj, memo):        if memo.has_key(self):            return memo[self]        newRef = copy.copy(self)        memo[self] = newRef        newRef.simobj = simobj        assert(isSimObject(newRef.simobj))        if self.peer and not proxy.isproxy(self.peer):            peerObj = self.peer.simobj(_memo=memo)            newRef.peer = self.peer.clone(peerObj, memo)            assert(not isinstance(newRef.peer, VectorPortRef))        return newRef    def unproxy(self, simobj):        assert(simobj is self.simobj)        if proxy.isproxy(self.peer):            try:                realPeer = self.peer.unproxy(self.simobj)            except:                print "Error in unproxying port '%s' of %s" % \                      (self.name, self.simobj.path())                raise            self.connect(realPeer)    # Call C++ to create corresponding port connection between C++ objects    def ccConnect(self):        from m5.objects.params import connectPorts        if self.ccConnected: # already done this            return        peer = self.peer        connectPorts(self.simobj.getCCObject(), self.name, self.index,                     peer.simobj.getCCObject(), peer.name, peer.index)        self.ccConnected = True        peer.ccConnected = True# A reference to an individual element of a VectorPort... much like a# PortRef, but has an index.class VectorPortElementRef(PortRef):    def __init__(self, simobj, name, index):        PortRef.__init__(self, simobj, name)        self.index = index    def __str__(self):        return '%s.%s[%d]' % (self.simobj, self.name, self.index)# A reference to a complete vector-valued port (not just a single element).# Can be indexed to retrieve individual VectorPortElementRef instances.class VectorPortRef(object):    def __init__(self, simobj, name):        assert(isSimObject(simobj) or isSimObjectClass(simobj))        self.simobj = simobj        self.name = name        self.elements = []    def __str__(self):        return '%s.%s[:]' % (self.simobj, self.name)    # for config.ini, print peer's name (not ours)    def ini_str(self):        return ' '.join([el.ini_str() for el in self.elements])    def __getitem__(self, key):        if not isinstance(key, int):            raise TypeError, "VectorPort index must be integer"        if key >= len(self.elements):            # need to extend list            ext = [VectorPortElementRef(self.simobj, self.name, i)                   for i in range(len(self.elements), key+1)]            self.elements.extend(ext)        return self.elements[key]    def _get_next(self):        return self[len(self.elements)]    def __setitem__(self, key, value):        if not isinstance(key, int):            raise TypeError, "VectorPort index must be integer"        self[key].connect(value)    def connect(self, other):        if isinstance(other, (list, tuple)):            # Assign list of port refs to vector port.            # For now, append them... not sure if that's the right semantics            # or if it should replace the current vector.            for ref in other:                self._get_next().connect(ref)        else:            # scalar assignment to plain VectorPort is implicit append            self._get_next().connect(other)    def clone(self, simobj, memo):        if memo.has_key(self):            return memo[self]        newRef = copy.copy(self)        memo[self] = newRef        newRef.simobj = simobj        assert(isSimObject(newRef.simobj))        newRef.elements = [el.clone(simobj, memo) for el in self.elements]        return newRef    def unproxy(self, simobj):        [el.unproxy(simobj) for el in self.elements]    def ccConnect(self):        [el.ccConnect() for el in self.elements]# Port description object.  Like a ParamDesc object, this represents a# logical port in the SimObject class, not a particular port on a# SimObject instance.  The latter are represented by PortRef objects.class Port(object):    # Port("description") or Port(default, "description")    def __init__(self, *args):        if len(args) == 1:            self.desc = args[0]        elif len(args) == 2:            self.default = args[0]            self.desc = args[1]        else:            raise TypeError, 'wrong number of arguments'        # self.name is set by SimObject class on assignment        # e.g., pio_port = Port("blah") sets self.name to 'pio_port'    # Generate a PortRef for this port on the given SimObject with the    # given name    def makeRef(self, simobj):        return PortRef(simobj, self.name)    # Connect an instance of this port (on the given SimObject with    # the given name) with the port described by the supplied PortRef    def connect(self, simobj, ref):        self.makeRef(simobj).connect(ref)# VectorPort description object.  Like Port, but represents a vector# of connections (e.g., as on a Bus).class VectorPort(Port):    def __init__(self, *args):        Port.__init__(self, *args)        self.isVec = True    def makeRef(self, simobj):        return VectorPortRef(simobj, self.name)# 'Fake' ParamDesc for Port references to assign to the _pdesc slot of# proxy objects (via set_param_desc()) so that proxy error messages# make sense.class PortParamDesc(object):    __metaclass__ = Singleton    ptype_str = 'Port'    ptype = Port__all__ = ['Param', 'VectorParam',           'Enum', 'Bool', 'String', 'Float',           'Int', 'Unsigned', 'Int8', 'UInt8', 'Int16', 'UInt16',           'Int32', 'UInt32', 'Int64', 'UInt64',           'Counter', 'Addr', 'Tick', 'Percent',           'TcpPort', 'UdpPort', 'EthernetAddr',           'MemorySize', 'MemorySize32',           'Latency', 'Frequency', 'Clock',           'NetworkBandwidth', 'MemoryBandwidth',           'Range', 'AddrRange', 'TickRange',           'MaxAddr', 'MaxTick', 'AllMemory',           'Time',           'NextEthernetAddr', 'NULL',           'Port', 'VectorPort']

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?