SimObject.py revision 8900:7d74a97c525f
13804SN/A# Copyright (c) 2012 ARM Limited 212776Snikos.nikoleris@arm.com# All rights reserved. 39235Sandreas.hansson@arm.com# 49235Sandreas.hansson@arm.com# The license below extends only to copyright in the software and shall 59235Sandreas.hansson@arm.com# not be construed as granting a license to any other intellectual 69235Sandreas.hansson@arm.com# property including but not limited to intellectual property relating 79235Sandreas.hansson@arm.com# to a hardware implementation of the functionality of the software 89235Sandreas.hansson@arm.com# licensed hereunder. You may use the software subject to the license 99235Sandreas.hansson@arm.com# terms below provided that you ensure that this notice is replicated 109235Sandreas.hansson@arm.com# unmodified and in its entirety in all distributions of the software, 119235Sandreas.hansson@arm.com# modified or unmodified, in source code or in binary form. 129235Sandreas.hansson@arm.com# 139235Sandreas.hansson@arm.com# Copyright (c) 2004-2006 The Regents of The University of Michigan 143804SN/A# Copyright (c) 2010 Advanced Micro Devices, Inc. 153804SN/A# All rights reserved. 163804SN/A# 173804SN/A# Redistribution and use in source and binary forms, with or without 183804SN/A# modification, are permitted provided that the following conditions are 193804SN/A# met: redistributions of source code must retain the above copyright 203804SN/A# notice, this list of conditions and the following disclaimer; 213804SN/A# redistributions in binary form must reproduce the above copyright 223804SN/A# notice, this list of conditions and the following disclaimer in the 233804SN/A# documentation and/or other materials provided with the distribution; 243804SN/A# neither the name of the copyright holders nor the names of its 253804SN/A# contributors may be used to endorse or promote products derived from 263804SN/A# this software without specific prior written permission. 273804SN/A# 283804SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 293804SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 303804SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 313804SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 323804SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 333804SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 343804SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 353804SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 363804SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 373804SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 383804SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 393804SN/A# 403804SN/A# Authors: Steve Reinhardt 419235Sandreas.hansson@arm.com# Nathan Binkert 423804SN/A# Andreas Hansson 433804SN/A 449235Sandreas.hansson@arm.comimport sys 459235Sandreas.hansson@arm.comfrom types import FunctionType, MethodType, ModuleType 463804SN/A 4712783Snikos.nikoleris@arm.comtry: 4812783Snikos.nikoleris@arm.com import pydot 4912783Snikos.nikoleris@arm.comexcept: 508229SN/A pydot = False 518902SN/A 528229SN/Aimport m5 539235Sandreas.hansson@arm.comfrom m5.util import * 5412783Snikos.nikoleris@arm.com 553804SN/A# Have to import params up top since Param is referenced on initial 568918SN/A# load (when SimObject class references Param to create a class 579235Sandreas.hansson@arm.com# variable, the 'name' param)... 589235Sandreas.hansson@arm.comfrom m5.params import * 599235Sandreas.hansson@arm.com# There are a few things we need that aren't in params.__all__ since 608918SN/A# normal users don't need them 6112777Sgabeblack@google.comfrom m5.params import ParamDesc, VectorParamDesc, \ 629235Sandreas.hansson@arm.com isNullPointer, SimObjectVector, Port 633804SN/A 643804SN/Afrom m5.proxy import * 659235Sandreas.hansson@arm.comfrom m5.proxy import isproxy 663804SN/A 673804SN/A##################################################################### 683804SN/A# 698918SN/A# M5 Python Configuration Utility 703804SN/A# 7112776Snikos.nikoleris@arm.com# The basic idea is to write simple Python programs that build Python 7212776Snikos.nikoleris@arm.com# objects corresponding to M5 SimObjects for the desired simulation 7312776Snikos.nikoleris@arm.com# configuration. For now, the Python emits a .ini file that can be 7412776Snikos.nikoleris@arm.com# parsed by M5. In the future, some tighter integration between M5 7512776Snikos.nikoleris@arm.com# and the Python interpreter may allow bypassing the .ini file. 7612776Snikos.nikoleris@arm.com# 7712776Snikos.nikoleris@arm.com# Each SimObject class in M5 is represented by a Python class with the 7812776Snikos.nikoleris@arm.com# same name. The Python inheritance tree mirrors the M5 C++ tree 7912776Snikos.nikoleris@arm.com# (e.g., SimpleCPU derives from BaseCPU in both cases, and all 8012776Snikos.nikoleris@arm.com# SimObjects inherit from a single SimObject base class). To specify 818918SN/A# an instance of an M5 SimObject in a configuration, the user simply 8212776Snikos.nikoleris@arm.com# instantiates the corresponding Python object. The parameters for 838918SN/A# that SimObject are given by assigning to attributes of the Python 8412776Snikos.nikoleris@arm.com# object, either using keyword assignment in the constructor or in 853804SN/A# separate assignment statements. For example: 863804SN/A# 8712776Snikos.nikoleris@arm.com# cache = BaseCache(size='64KB') 8812776Snikos.nikoleris@arm.com# cache.hit_latency = 3 8912776Snikos.nikoleris@arm.com# cache.assoc = 8 9012776Snikos.nikoleris@arm.com# 9112776Snikos.nikoleris@arm.com# The magic lies in the mapping of the Python attributes for SimObject 9212776Snikos.nikoleris@arm.com# classes to the actual SimObject parameter specifications. This 9312776Snikos.nikoleris@arm.com# allows parameter validity checking in the Python code. Continuing 9412776Snikos.nikoleris@arm.com# the example above, the statements "cache.blurfl=3" or 9512776Snikos.nikoleris@arm.com# "cache.assoc='hello'" would both result in runtime errors in Python, 9612776Snikos.nikoleris@arm.com# since the BaseCache object has no 'blurfl' parameter and the 'assoc' 978918SN/A# parameter requires an integer, respectively. This magic is done 9812776Snikos.nikoleris@arm.com# primarily by overriding the special __setattr__ method that controls 998918SN/A# assignment to object attributes. 10012776Snikos.nikoleris@arm.com# 1018918SN/A# Once a set of Python objects have been instantiated in a hierarchy, 1028918SN/A# calling 'instantiate(obj)' (where obj is the root of the hierarchy) 10312776Snikos.nikoleris@arm.com# will generate a .ini file. 10412776Snikos.nikoleris@arm.com# 10512776Snikos.nikoleris@arm.com##################################################################### 10612776Snikos.nikoleris@arm.com 10712776Snikos.nikoleris@arm.com# list of all SimObject classes 10812776Snikos.nikoleris@arm.comallClasses = {} 10912776Snikos.nikoleris@arm.com 11012776Snikos.nikoleris@arm.com# dict to look up SimObjects based on path 11112776Snikos.nikoleris@arm.cominstanceDict = {} 11212776Snikos.nikoleris@arm.com 11312776Snikos.nikoleris@arm.comdef public_value(key, value): 11412776Snikos.nikoleris@arm.com return key.startswith('_') or \ 1155609SN/A isinstance(value, (FunctionType, MethodType, ModuleType, 11612776Snikos.nikoleris@arm.com classmethod, type)) 1175609SN/A 1185609SN/A# The metaclass for SimObject. This class controls how new classes 1199409Sandreas.hansson@arm.com# that derive from SimObject are instantiated, and provides inherited 1209235Sandreas.hansson@arm.com# class behavior (just like a class controls how instances of that 1213804SN/A# class are instantiated, and provides inherited instance behavior). 12212776Snikos.nikoleris@arm.comclass MetaSimObject(type): 1233804SN/A # Attributes that can be set only at initialization time 1243804SN/A init_keywords = { 'abstract' : bool, 1258902SN/A 'cxx_class' : str, 1263804SN/A 'cxx_type' : str, 1273804SN/A 'type' : str } 1285608SN/A # Attributes that can be set any time 1295608SN/A keywords = { 'check' : FunctionType } 1303804SN/A 13112777Sgabeblack@google.com # __new__ is called before __init__, and is where the statements 1323804SN/A # in the body of the class definition get loaded into the class's 1333804SN/A # __dict__. We intercept this to filter out parameter & port assignments 1343804SN/A # and only allow "private" attributes to be passed to the base 1355608SN/A # __new__ (starting with underscore). 1365608SN/A def __new__(mcls, name, bases, dict): 1373804SN/A assert name not in allClasses, "SimObject %s already present" % name 13812777Sgabeblack@google.com 13912777Sgabeblack@google.com # Copy "private" attributes, functions, and classes to the 14012777Sgabeblack@google.com # official dict. Everything else goes in _init_dict to be 1413804SN/A # filtered in __init__. 1423804SN/A cls_dict = {} 1433804SN/A value_dict = {} 1445608SN/A for key,val in dict.items(): 1455608SN/A if public_value(key, val): 1463804SN/A cls_dict[key] = val 14712777Sgabeblack@google.com else: 1483804SN/A # must be a param/port setting 1493804SN/A value_dict[key] = val 1503804SN/A if 'abstract' not in value_dict: 1518918SN/A value_dict['abstract'] = False 1528918SN/A cls_dict['_value_dict'] = value_dict 1538918SN/A cls = super(MetaSimObject, mcls).__new__(mcls, name, bases, cls_dict) 1548918SN/A if 'type' in value_dict: 1558918SN/A allClasses[name] = cls 1568918SN/A return cls 1575608SN/A 1585608SN/A # subclass initialization 1593804SN/A def __init__(cls, name, bases, dict): 1603804SN/A # calls type.__init__()... I think that's a no-op, but leave 1613804SN/A # it here just in case it's not. 1623804SN/A super(MetaSimObject, cls).__init__(name, bases, dict) 1638918SN/A 1648918SN/A # initialize required attributes 1658918SN/A 1668918SN/A # class-only attributes 1678918SN/A cls._params = multidict() # param descriptions 1688918SN/A cls._ports = multidict() # port descriptions 1695608SN/A 1705608SN/A # class or instance attributes 1713804SN/A cls._values = multidict() # param values 1723804SN/A cls._children = multidict() # SimObject children 1733804SN/A cls._port_refs = multidict() # port ref objects 1743804SN/A cls._instantiated = False # really instantiated, cloned, or subclassed 1759235Sandreas.hansson@arm.com 1768918SN/A # We don't support multiple inheritance. If you want to, you 1773804SN/A # must fix multidict to deal with it properly. 1783804SN/A if len(bases) > 1: 1793804SN/A raise TypeError, "SimObjects do not support multiple inheritance" 1803804SN/A 1815608SN/A base = bases[0] 1828918SN/A 1833804SN/A # Set up general inheritance via multidicts. A subclass will 1843804SN/A # inherit all its settings from the base class. The only time 1853804SN/A # the following is not true is when we define the SimObject 18612776Snikos.nikoleris@arm.com # class itself (in which case the multidicts have no parent). 18712776Snikos.nikoleris@arm.com if isinstance(base, MetaSimObject): 18812776Snikos.nikoleris@arm.com cls._base = base 18912777Sgabeblack@google.com cls._params.parent = base._params 19012777Sgabeblack@google.com cls._ports.parent = base._ports 19112777Sgabeblack@google.com cls._values.parent = base._values 19212777Sgabeblack@google.com cls._children.parent = base._children 19312777Sgabeblack@google.com cls._port_refs.parent = base._port_refs 19412777Sgabeblack@google.com # mark base as having been subclassed 19512777Sgabeblack@google.com base._instantiated = True 19612777Sgabeblack@google.com else: 19712777Sgabeblack@google.com cls._base = None 19812777Sgabeblack@google.com 19912777Sgabeblack@google.com # default keyword values 20012777Sgabeblack@google.com if 'type' in cls._value_dict: 20112777Sgabeblack@google.com if 'cxx_class' not in cls._value_dict: 20212777Sgabeblack@google.com cls._value_dict['cxx_class'] = cls._value_dict['type'] 20312777Sgabeblack@google.com 20412777Sgabeblack@google.com cls._value_dict['cxx_type'] = '%s *' % cls._value_dict['cxx_class'] 20512777Sgabeblack@google.com 20612777Sgabeblack@google.com # Export methods are automatically inherited via C++, so we 20712777Sgabeblack@google.com # don't want the method declarations to get inherited on the 20812777Sgabeblack@google.com # python side (and thus end up getting repeated in the wrapped 20912777Sgabeblack@google.com # versions of derived classes). The code below basicallly 21012777Sgabeblack@google.com # suppresses inheritance by substituting in the base (null) 21112777Sgabeblack@google.com # versions of these methods unless a different version is 21212777Sgabeblack@google.com # explicitly supplied. 21312777Sgabeblack@google.com for method_name in ('export_methods', 'export_method_cxx_predecls', 21412776Snikos.nikoleris@arm.com 'export_method_swig_predecls'): 21512776Snikos.nikoleris@arm.com if method_name not in cls.__dict__: 21612776Snikos.nikoleris@arm.com base_method = getattr(MetaSimObject, method_name) 21712776Snikos.nikoleris@arm.com m = MethodType(base_method, cls, MetaSimObject) 21812776Snikos.nikoleris@arm.com setattr(cls, method_name, m) 21912776Snikos.nikoleris@arm.com 22012776Snikos.nikoleris@arm.com # Now process the _value_dict items. They could be defining 22112776Snikos.nikoleris@arm.com # new (or overriding existing) parameters or ports, setting 22212776Snikos.nikoleris@arm.com # class keywords (e.g., 'abstract'), or setting parameter 22312776Snikos.nikoleris@arm.com # values or port bindings. The first 3 can only be set when 22412776Snikos.nikoleris@arm.com # the class is defined, so we handle them here. The others 22512776Snikos.nikoleris@arm.com # can be set later too, so just emulate that by calling 22612776Snikos.nikoleris@arm.com # setattr(). 22712777Sgabeblack@google.com for key,val in cls._value_dict.items(): 22812777Sgabeblack@google.com # param descriptions 22912777Sgabeblack@google.com if isinstance(val, ParamDesc): 23012777Sgabeblack@google.com cls._new_param(key, val) 23112777Sgabeblack@google.com 23212777Sgabeblack@google.com # port objects 23312777Sgabeblack@google.com elif isinstance(val, Port): 23412777Sgabeblack@google.com cls._new_port(key, val) 23512777Sgabeblack@google.com 23612777Sgabeblack@google.com # init-time-only keywords 23712777Sgabeblack@google.com elif cls.init_keywords.has_key(key): 23812776Snikos.nikoleris@arm.com cls._set_keyword(key, val, cls.init_keywords[key]) 23912776Snikos.nikoleris@arm.com 24012777Sgabeblack@google.com # default: use normal path (ends up in __setattr__) 24112776Snikos.nikoleris@arm.com else: 24212776Snikos.nikoleris@arm.com setattr(cls, key, val) 24312776Snikos.nikoleris@arm.com 24412776Snikos.nikoleris@arm.com def _set_keyword(cls, keyword, val, kwtype): 24512776Snikos.nikoleris@arm.com if not isinstance(val, kwtype): 24612776Snikos.nikoleris@arm.com raise TypeError, 'keyword %s has bad type %s (expecting %s)' % \ 24712776Snikos.nikoleris@arm.com (keyword, type(val), kwtype) 24812776Snikos.nikoleris@arm.com if isinstance(val, FunctionType): 24912776Snikos.nikoleris@arm.com val = classmethod(val) 25012776Snikos.nikoleris@arm.com type.__setattr__(cls, keyword, val) 25112777Sgabeblack@google.com 25212776Snikos.nikoleris@arm.com def _new_param(cls, name, pdesc): 25312776Snikos.nikoleris@arm.com # each param desc should be uniquely assigned to one variable 25412776Snikos.nikoleris@arm.com assert(not hasattr(pdesc, 'name')) 25512776Snikos.nikoleris@arm.com pdesc.name = name 25612776Snikos.nikoleris@arm.com cls._params[name] = pdesc 25712776Snikos.nikoleris@arm.com if hasattr(pdesc, 'default'): 25812776Snikos.nikoleris@arm.com cls._set_param(name, pdesc.default, pdesc) 25912776Snikos.nikoleris@arm.com 26012776Snikos.nikoleris@arm.com def _set_param(cls, name, value, param): 26112776Snikos.nikoleris@arm.com assert(param.name == name) 26212777Sgabeblack@google.com try: 26312777Sgabeblack@google.com value = param.convert(value) 26412777Sgabeblack@google.com except Exception, e: 26512777Sgabeblack@google.com msg = "%s\nError setting param %s.%s to %s\n" % \ 26612777Sgabeblack@google.com (e, cls.__name__, name, value) 26712777Sgabeblack@google.com e.args = (msg, ) 26812777Sgabeblack@google.com raise 26912777Sgabeblack@google.com cls._values[name] = value 2703804SN/A # if param value is a SimObject, make it a child too, so that 2713804SN/A # it gets cloned properly when the class is instantiated 2729235Sandreas.hansson@arm.com if isSimObjectOrVector(value) and not value.has_parent(): 273 cls._add_cls_child(name, value) 274 275 def _add_cls_child(cls, name, child): 276 # It's a little funky to have a class as a parent, but these 277 # objects should never be instantiated (only cloned, which 278 # clears the parent pointer), and this makes it clear that the 279 # object is not an orphan and can provide better error 280 # messages. 281 child.set_parent(cls, name) 282 cls._children[name] = child 283 284 def _new_port(cls, name, port): 285 # each port should be uniquely assigned to one variable 286 assert(not hasattr(port, 'name')) 287 port.name = name 288 cls._ports[name] = port 289 290 # same as _get_port_ref, effectively, but for classes 291 def _cls_get_port_ref(cls, attr): 292 # Return reference that can be assigned to another port 293 # via __setattr__. There is only ever one reference 294 # object per port, but we create them lazily here. 295 ref = cls._port_refs.get(attr) 296 if not ref: 297 ref = cls._ports[attr].makeRef(cls) 298 cls._port_refs[attr] = ref 299 return ref 300 301 # Set attribute (called on foo.attr = value when foo is an 302 # instance of class cls). 303 def __setattr__(cls, attr, value): 304 # normal processing for private attributes 305 if public_value(attr, value): 306 type.__setattr__(cls, attr, value) 307 return 308 309 if cls.keywords.has_key(attr): 310 cls._set_keyword(attr, value, cls.keywords[attr]) 311 return 312 313 if cls._ports.has_key(attr): 314 cls._cls_get_port_ref(attr).connect(value) 315 return 316 317 if isSimObjectOrSequence(value) and cls._instantiated: 318 raise RuntimeError, \ 319 "cannot set SimObject parameter '%s' after\n" \ 320 " class %s has been instantiated or subclassed" \ 321 % (attr, cls.__name__) 322 323 # check for param 324 param = cls._params.get(attr) 325 if param: 326 cls._set_param(attr, value, param) 327 return 328 329 if isSimObjectOrSequence(value): 330 # If RHS is a SimObject, it's an implicit child assignment. 331 cls._add_cls_child(attr, coerceSimObjectOrVector(value)) 332 return 333 334 # no valid assignment... raise exception 335 raise AttributeError, \ 336 "Class %s has no parameter \'%s\'" % (cls.__name__, attr) 337 338 def __getattr__(cls, attr): 339 if attr == 'cxx_class_path': 340 return cls.cxx_class.split('::') 341 342 if attr == 'cxx_class_name': 343 return cls.cxx_class_path[-1] 344 345 if attr == 'cxx_namespaces': 346 return cls.cxx_class_path[:-1] 347 348 if cls._values.has_key(attr): 349 return cls._values[attr] 350 351 if cls._children.has_key(attr): 352 return cls._children[attr] 353 354 raise AttributeError, \ 355 "object '%s' has no attribute '%s'" % (cls.__name__, attr) 356 357 def __str__(cls): 358 return cls.__name__ 359 360 # See ParamValue.cxx_predecls for description. 361 def cxx_predecls(cls, code): 362 code('#include "params/$cls.hh"') 363 364 # See ParamValue.swig_predecls for description. 365 def swig_predecls(cls, code): 366 code('%import "python/m5/internal/param_$cls.i"') 367 368 # Hook for exporting additional C++ methods to Python via SWIG. 369 # Default is none, override using @classmethod in class definition. 370 def export_methods(cls, code): 371 pass 372 373 # Generate the code needed as a prerequisite for the C++ methods 374 # exported via export_methods() to be compiled in the _wrap.cc 375 # file. Typically generates one or more #include statements. If 376 # any methods are exported, typically at least the C++ header 377 # declaring the relevant SimObject class must be included. 378 def export_method_cxx_predecls(cls, code): 379 pass 380 381 # Generate the code needed as a prerequisite for the C++ methods 382 # exported via export_methods() to be processed by SWIG. 383 # Typically generates one or more %include or %import statements. 384 # If any methods are exported, typically at least the C++ header 385 # declaring the relevant SimObject class must be included. 386 def export_method_swig_predecls(cls, code): 387 pass 388 389 # Generate the declaration for this object for wrapping with SWIG. 390 # Generates code that goes into a SWIG .i file. Called from 391 # src/SConscript. 392 def swig_decl(cls, code): 393 class_path = cls.cxx_class.split('::') 394 classname = class_path[-1] 395 namespaces = class_path[:-1] 396 397 # The 'local' attribute restricts us to the params declared in 398 # the object itself, not including inherited params (which 399 # will also be inherited from the base class's param struct 400 # here). 401 params = cls._params.local.values() 402 ports = cls._ports.local 403 404 code('%module(package="m5.internal") param_$cls') 405 code() 406 code('%{') 407 code('#include "params/$cls.hh"') 408 for param in params: 409 param.cxx_predecls(code) 410 cls.export_method_cxx_predecls(code) 411 code('''\ 412/** 413 * This is a workaround for bug in swig. Prior to gcc 4.6.1 the STL 414 * headers like vector, string, etc. used to automatically pull in 415 * the cstddef header but starting with gcc 4.6.1 they no longer do. 416 * This leads to swig generated a file that does not compile so we 417 * explicitly include cstddef. Additionally, including version 2.0.4, 418 * swig uses ptrdiff_t without the std:: namespace prefix which is 419 * required with gcc 4.6.1. We explicitly provide access to it. 420 */ 421#include <cstddef> 422using std::ptrdiff_t; 423''') 424 code('%}') 425 code() 426 427 for param in params: 428 param.swig_predecls(code) 429 cls.export_method_swig_predecls(code) 430 431 code() 432 if cls._base: 433 code('%import "python/m5/internal/param_${{cls._base}}.i"') 434 code() 435 436 for ns in namespaces: 437 code('namespace $ns {') 438 439 if namespaces: 440 code('// avoid name conflicts') 441 sep_string = '_COLONS_' 442 flat_name = sep_string.join(class_path) 443 code('%rename($flat_name) $classname;') 444 445 code() 446 code('// stop swig from creating/wrapping default ctor/dtor') 447 code('%nodefault $classname;') 448 code('class $classname') 449 if cls._base: 450 code(' : public ${{cls._base.cxx_class}}') 451 code('{') 452 code(' public:') 453 cls.export_methods(code) 454 code('};') 455 456 for ns in reversed(namespaces): 457 code('} // namespace $ns') 458 459 code() 460 code('%include "params/$cls.hh"') 461 462 463 # Generate the C++ declaration (.hh file) for this SimObject's 464 # param struct. Called from src/SConscript. 465 def cxx_param_decl(cls, code): 466 # The 'local' attribute restricts us to the params declared in 467 # the object itself, not including inherited params (which 468 # will also be inherited from the base class's param struct 469 # here). 470 params = cls._params.local.values() 471 ports = cls._ports.local 472 try: 473 ptypes = [p.ptype for p in params] 474 except: 475 print cls, p, p.ptype_str 476 print params 477 raise 478 479 class_path = cls._value_dict['cxx_class'].split('::') 480 481 code('''\ 482#ifndef __PARAMS__${cls}__ 483#define __PARAMS__${cls}__ 484 485''') 486 487 # A forward class declaration is sufficient since we are just 488 # declaring a pointer. 489 for ns in class_path[:-1]: 490 code('namespace $ns {') 491 code('class $0;', class_path[-1]) 492 for ns in reversed(class_path[:-1]): 493 code('} // namespace $ns') 494 code() 495 496 # The base SimObject has a couple of params that get 497 # automatically set from Python without being declared through 498 # the normal Param mechanism; we slip them in here (needed 499 # predecls now, actual declarations below) 500 if cls == SimObject: 501 code(''' 502#ifndef PY_VERSION 503struct PyObject; 504#endif 505 506#include <string> 507 508class EventQueue; 509''') 510 for param in params: 511 param.cxx_predecls(code) 512 for port in ports.itervalues(): 513 port.cxx_predecls(code) 514 code() 515 516 if cls._base: 517 code('#include "params/${{cls._base.type}}.hh"') 518 code() 519 520 for ptype in ptypes: 521 if issubclass(ptype, Enum): 522 code('#include "enums/${{ptype.__name__}}.hh"') 523 code() 524 525 # now generate the actual param struct 526 code("struct ${cls}Params") 527 if cls._base: 528 code(" : public ${{cls._base.type}}Params") 529 code("{") 530 if not hasattr(cls, 'abstract') or not cls.abstract: 531 if 'type' in cls.__dict__: 532 code(" ${{cls.cxx_type}} create();") 533 534 code.indent() 535 if cls == SimObject: 536 code(''' 537 SimObjectParams() 538 { 539 extern EventQueue mainEventQueue; 540 eventq = &mainEventQueue; 541 } 542 virtual ~SimObjectParams() {} 543 544 std::string name; 545 PyObject *pyobj; 546 EventQueue *eventq; 547 ''') 548 for param in params: 549 param.cxx_decl(code) 550 for port in ports.itervalues(): 551 port.cxx_decl(code) 552 553 code.dedent() 554 code('};') 555 556 code() 557 code('#endif // __PARAMS__${cls}__') 558 return code 559 560 561 562# The SimObject class is the root of the special hierarchy. Most of 563# the code in this class deals with the configuration hierarchy itself 564# (parent/child node relationships). 565class SimObject(object): 566 # Specify metaclass. Any class inheriting from SimObject will 567 # get this metaclass. 568 __metaclass__ = MetaSimObject 569 type = 'SimObject' 570 abstract = True 571 572 @classmethod 573 def export_method_cxx_predecls(cls, code): 574 code(''' 575#include <Python.h> 576 577#include "sim/serialize.hh" 578#include "sim/sim_object.hh" 579''') 580 581 @classmethod 582 def export_method_swig_predecls(cls, code): 583 code(''' 584%include <std_string.i> 585''') 586 587 @classmethod 588 def export_methods(cls, code): 589 code(''' 590 enum State { 591 Running, 592 Draining, 593 Drained 594 }; 595 596 void init(); 597 void loadState(Checkpoint *cp); 598 void initState(); 599 void regStats(); 600 void regFormulas(); 601 void resetStats(); 602 void startup(); 603 604 unsigned int drain(Event *drain_event); 605 void resume(); 606 void switchOut(); 607 void takeOverFrom(BaseCPU *cpu); 608''') 609 610 # Initialize new instance. For objects with SimObject-valued 611 # children, we need to recursively clone the classes represented 612 # by those param values as well in a consistent "deep copy"-style 613 # fashion. That is, we want to make sure that each instance is 614 # cloned only once, and that if there are multiple references to 615 # the same original object, we end up with the corresponding 616 # cloned references all pointing to the same cloned instance. 617 def __init__(self, **kwargs): 618 ancestor = kwargs.get('_ancestor') 619 memo_dict = kwargs.get('_memo') 620 if memo_dict is None: 621 # prepare to memoize any recursively instantiated objects 622 memo_dict = {} 623 elif ancestor: 624 # memoize me now to avoid problems with recursive calls 625 memo_dict[ancestor] = self 626 627 if not ancestor: 628 ancestor = self.__class__ 629 ancestor._instantiated = True 630 631 # initialize required attributes 632 self._parent = None 633 self._name = None 634 self._ccObject = None # pointer to C++ object 635 self._ccParams = None 636 self._instantiated = False # really "cloned" 637 638 # Clone children specified at class level. No need for a 639 # multidict here since we will be cloning everything. 640 # Do children before parameter values so that children that 641 # are also param values get cloned properly. 642 self._children = {} 643 for key,val in ancestor._children.iteritems(): 644 self.add_child(key, val(_memo=memo_dict)) 645 646 # Inherit parameter values from class using multidict so 647 # individual value settings can be overridden but we still 648 # inherit late changes to non-overridden class values. 649 self._values = multidict(ancestor._values) 650 # clone SimObject-valued parameters 651 for key,val in ancestor._values.iteritems(): 652 val = tryAsSimObjectOrVector(val) 653 if val is not None: 654 self._values[key] = val(_memo=memo_dict) 655 656 # clone port references. no need to use a multidict here 657 # since we will be creating new references for all ports. 658 self._port_refs = {} 659 for key,val in ancestor._port_refs.iteritems(): 660 self._port_refs[key] = val.clone(self, memo_dict) 661 # apply attribute assignments from keyword args, if any 662 for key,val in kwargs.iteritems(): 663 setattr(self, key, val) 664 665 # "Clone" the current instance by creating another instance of 666 # this instance's class, but that inherits its parameter values 667 # and port mappings from the current instance. If we're in a 668 # "deep copy" recursive clone, check the _memo dict to see if 669 # we've already cloned this instance. 670 def __call__(self, **kwargs): 671 memo_dict = kwargs.get('_memo') 672 if memo_dict is None: 673 # no memo_dict: must be top-level clone operation. 674 # this is only allowed at the root of a hierarchy 675 if self._parent: 676 raise RuntimeError, "attempt to clone object %s " \ 677 "not at the root of a tree (parent = %s)" \ 678 % (self, self._parent) 679 # create a new dict and use that. 680 memo_dict = {} 681 kwargs['_memo'] = memo_dict 682 elif memo_dict.has_key(self): 683 # clone already done & memoized 684 return memo_dict[self] 685 return self.__class__(_ancestor = self, **kwargs) 686 687 def _get_port_ref(self, attr): 688 # Return reference that can be assigned to another port 689 # via __setattr__. There is only ever one reference 690 # object per port, but we create them lazily here. 691 ref = self._port_refs.get(attr) 692 if not ref: 693 ref = self._ports[attr].makeRef(self) 694 self._port_refs[attr] = ref 695 return ref 696 697 def __getattr__(self, attr): 698 if self._ports.has_key(attr): 699 return self._get_port_ref(attr) 700 701 if self._values.has_key(attr): 702 return self._values[attr] 703 704 if self._children.has_key(attr): 705 return self._children[attr] 706 707 # If the attribute exists on the C++ object, transparently 708 # forward the reference there. This is typically used for 709 # SWIG-wrapped methods such as init(), regStats(), 710 # regFormulas(), resetStats(), startup(), drain(), and 711 # resume(). 712 if self._ccObject and hasattr(self._ccObject, attr): 713 return getattr(self._ccObject, attr) 714 715 raise AttributeError, "object '%s' has no attribute '%s'" \ 716 % (self.__class__.__name__, attr) 717 718 # Set attribute (called on foo.attr = value when foo is an 719 # instance of class cls). 720 def __setattr__(self, attr, value): 721 # normal processing for private attributes 722 if attr.startswith('_'): 723 object.__setattr__(self, attr, value) 724 return 725 726 if self._ports.has_key(attr): 727 # set up port connection 728 self._get_port_ref(attr).connect(value) 729 return 730 731 if isSimObjectOrSequence(value) and self._instantiated: 732 raise RuntimeError, \ 733 "cannot set SimObject parameter '%s' after\n" \ 734 " instance been cloned %s" % (attr, `self`) 735 736 param = self._params.get(attr) 737 if param: 738 try: 739 value = param.convert(value) 740 except Exception, e: 741 msg = "%s\nError setting param %s.%s to %s\n" % \ 742 (e, self.__class__.__name__, attr, value) 743 e.args = (msg, ) 744 raise 745 self._values[attr] = value 746 # implicitly parent unparented objects assigned as params 747 if isSimObjectOrVector(value) and not value.has_parent(): 748 self.add_child(attr, value) 749 return 750 751 # if RHS is a SimObject, it's an implicit child assignment 752 if isSimObjectOrSequence(value): 753 self.add_child(attr, value) 754 return 755 756 # no valid assignment... raise exception 757 raise AttributeError, "Class %s has no parameter %s" \ 758 % (self.__class__.__name__, attr) 759 760 761 # this hack allows tacking a '[0]' onto parameters that may or may 762 # not be vectors, and always getting the first element (e.g. cpus) 763 def __getitem__(self, key): 764 if key == 0: 765 return self 766 raise TypeError, "Non-zero index '%s' to SimObject" % key 767 768 # Also implemented by SimObjectVector 769 def clear_parent(self, old_parent): 770 assert self._parent is old_parent 771 self._parent = None 772 773 # Also implemented by SimObjectVector 774 def set_parent(self, parent, name): 775 self._parent = parent 776 self._name = name 777 778 # Also implemented by SimObjectVector 779 def get_name(self): 780 return self._name 781 782 # Also implemented by SimObjectVector 783 def has_parent(self): 784 return self._parent is not None 785 786 # clear out child with given name. This code is not likely to be exercised. 787 # See comment in add_child. 788 def clear_child(self, name): 789 child = self._children[name] 790 child.clear_parent(self) 791 del self._children[name] 792 793 # Add a new child to this object. 794 def add_child(self, name, child): 795 child = coerceSimObjectOrVector(child) 796 if child.has_parent(): 797 print "warning: add_child('%s'): child '%s' already has parent" % \ 798 (name, child.get_name()) 799 if self._children.has_key(name): 800 # This code path had an undiscovered bug that would make it fail 801 # at runtime. It had been here for a long time and was only 802 # exposed by a buggy script. Changes here will probably not be 803 # exercised without specialized testing. 804 self.clear_child(name) 805 child.set_parent(self, name) 806 self._children[name] = child 807 808 # Take SimObject-valued parameters that haven't been explicitly 809 # assigned as children and make them children of the object that 810 # they were assigned to as a parameter value. This guarantees 811 # that when we instantiate all the parameter objects we're still 812 # inside the configuration hierarchy. 813 def adoptOrphanParams(self): 814 for key,val in self._values.iteritems(): 815 if not isSimObjectVector(val) and isSimObjectSequence(val): 816 # need to convert raw SimObject sequences to 817 # SimObjectVector class so we can call has_parent() 818 val = SimObjectVector(val) 819 self._values[key] = val 820 if isSimObjectOrVector(val) and not val.has_parent(): 821 print "warning: %s adopting orphan SimObject param '%s'" \ 822 % (self, key) 823 self.add_child(key, val) 824 825 def path(self): 826 if not self._parent: 827 return '<orphan %s>' % self.__class__ 828 ppath = self._parent.path() 829 if ppath == 'root': 830 return self._name 831 return ppath + "." + self._name 832 833 def __str__(self): 834 return self.path() 835 836 def ini_str(self): 837 return self.path() 838 839 def find_any(self, ptype): 840 if isinstance(self, ptype): 841 return self, True 842 843 found_obj = None 844 for child in self._children.itervalues(): 845 if isinstance(child, ptype): 846 if found_obj != None and child != found_obj: 847 raise AttributeError, \ 848 'parent.any matched more than one: %s %s' % \ 849 (found_obj.path, child.path) 850 found_obj = child 851 # search param space 852 for pname,pdesc in self._params.iteritems(): 853 if issubclass(pdesc.ptype, ptype): 854 match_obj = self._values[pname] 855 if found_obj != None and found_obj != match_obj: 856 raise AttributeError, \ 857 'parent.any matched more than one: %s and %s' % (found_obj.path, match_obj.path) 858 found_obj = match_obj 859 return found_obj, found_obj != None 860 861 def find_all(self, ptype): 862 all = {} 863 # search children 864 for child in self._children.itervalues(): 865 if isinstance(child, ptype) and not isproxy(child) and \ 866 not isNullPointer(child): 867 all[child] = True 868 # search param space 869 for pname,pdesc in self._params.iteritems(): 870 if issubclass(pdesc.ptype, ptype): 871 match_obj = self._values[pname] 872 if not isproxy(match_obj) and not isNullPointer(match_obj): 873 all[match_obj] = True 874 return all.keys(), True 875 876 def unproxy(self, base): 877 return self 878 879 def unproxyParams(self): 880 for param in self._params.iterkeys(): 881 value = self._values.get(param) 882 if value != None and isproxy(value): 883 try: 884 value = value.unproxy(self) 885 except: 886 print "Error in unproxying param '%s' of %s" % \ 887 (param, self.path()) 888 raise 889 setattr(self, param, value) 890 891 # Unproxy ports in sorted order so that 'append' operations on 892 # vector ports are done in a deterministic fashion. 893 port_names = self._ports.keys() 894 port_names.sort() 895 for port_name in port_names: 896 port = self._port_refs.get(port_name) 897 if port != None: 898 port.unproxy(self) 899 900 def print_ini(self, ini_file): 901 print >>ini_file, '[' + self.path() + ']' # .ini section header 902 903 instanceDict[self.path()] = self 904 905 if hasattr(self, 'type'): 906 print >>ini_file, 'type=%s' % self.type 907 908 if len(self._children.keys()): 909 print >>ini_file, 'children=%s' % \ 910 ' '.join(self._children[n].get_name() \ 911 for n in sorted(self._children.keys())) 912 913 for param in sorted(self._params.keys()): 914 value = self._values.get(param) 915 if value != None: 916 print >>ini_file, '%s=%s' % (param, 917 self._values[param].ini_str()) 918 919 for port_name in sorted(self._ports.keys()): 920 port = self._port_refs.get(port_name, None) 921 if port != None: 922 print >>ini_file, '%s=%s' % (port_name, port.ini_str()) 923 924 print >>ini_file # blank line between objects 925 926 # generate a tree of dictionaries expressing all the parameters in the 927 # instantiated system for use by scripts that want to do power, thermal 928 # visualization, and other similar tasks 929 def get_config_as_dict(self): 930 d = attrdict() 931 if hasattr(self, 'type'): 932 d.type = self.type 933 if hasattr(self, 'cxx_class'): 934 d.cxx_class = self.cxx_class 935 936 for param in sorted(self._params.keys()): 937 value = self._values.get(param) 938 if value != None: 939 try: 940 # Use native type for those supported by JSON and 941 # strings for everything else. skipkeys=True seems 942 # to not work as well as one would hope 943 if type(self._values[param].value) in \ 944 [str, unicode, int, long, float, bool, None]: 945 d[param] = self._values[param].value 946 else: 947 d[param] = str(self._values[param]) 948 949 except AttributeError: 950 pass 951 952 for n in sorted(self._children.keys()): 953 d[self._children[n].get_name()] = self._children[n].get_config_as_dict() 954 955 for port_name in sorted(self._ports.keys()): 956 port = self._port_refs.get(port_name, None) 957 if port != None: 958 # Might want to actually make this reference the object 959 # in the future, although execing the string problem would 960 # get some of the way there 961 d[port_name] = port.ini_str() 962 963 return d 964 965 def getCCParams(self): 966 if self._ccParams: 967 return self._ccParams 968 969 cc_params_struct = getattr(m5.internal.params, '%sParams' % self.type) 970 cc_params = cc_params_struct() 971 cc_params.pyobj = self 972 cc_params.name = str(self) 973 974 param_names = self._params.keys() 975 param_names.sort() 976 for param in param_names: 977 value = self._values.get(param) 978 if value is None: 979 fatal("%s.%s without default or user set value", 980 self.path(), param) 981 982 value = value.getValue() 983 if isinstance(self._params[param], VectorParamDesc): 984 assert isinstance(value, list) 985 vec = getattr(cc_params, param) 986 assert not len(vec) 987 for v in value: 988 vec.append(v) 989 else: 990 setattr(cc_params, param, value) 991 992 port_names = self._ports.keys() 993 port_names.sort() 994 for port_name in port_names: 995 port = self._port_refs.get(port_name, None) 996 setattr(cc_params, 'port_' + port_name + '_connection_count', 997 len(port) if port != None else 0) 998 self._ccParams = cc_params 999 return self._ccParams 1000 1001 # Get C++ object corresponding to this object, calling C++ if 1002 # necessary to construct it. Does *not* recursively create 1003 # children. 1004 def getCCObject(self): 1005 if not self._ccObject: 1006 # Make sure this object is in the configuration hierarchy 1007 if not self._parent and not isRoot(self): 1008 raise RuntimeError, "Attempt to instantiate orphan node" 1009 # Cycles in the configuration hierarchy are not supported. This 1010 # will catch the resulting recursion and stop. 1011 self._ccObject = -1 1012 params = self.getCCParams() 1013 self._ccObject = params.create() 1014 elif self._ccObject == -1: 1015 raise RuntimeError, "%s: Cycle found in configuration hierarchy." \ 1016 % self.path() 1017 return self._ccObject 1018 1019 def descendants(self): 1020 yield self 1021 for child in self._children.itervalues(): 1022 for obj in child.descendants(): 1023 yield obj 1024 1025 # Call C++ to create C++ object corresponding to this object 1026 def createCCObject(self): 1027 self.getCCParams() 1028 self.getCCObject() # force creation 1029 1030 def getValue(self): 1031 return self.getCCObject() 1032 1033 # Create C++ port connections corresponding to the connections in 1034 # _port_refs 1035 def connectPorts(self): 1036 for portRef in self._port_refs.itervalues(): 1037 portRef.ccConnect() 1038 1039 def getMemoryMode(self): 1040 if not isinstance(self, m5.objects.System): 1041 return None 1042 1043 return self._ccObject.getMemoryMode() 1044 1045 def changeTiming(self, mode): 1046 if isinstance(self, m5.objects.System): 1047 # i don't know if there's a better way to do this - calling 1048 # setMemoryMode directly from self._ccObject results in calling 1049 # SimObject::setMemoryMode, not the System::setMemoryMode 1050 self._ccObject.setMemoryMode(mode) 1051 1052 def takeOverFrom(self, old_cpu): 1053 self._ccObject.takeOverFrom(old_cpu._ccObject) 1054 1055 # generate output file for 'dot' to display as a pretty graph. 1056 # this code is currently broken. 1057 def outputDot(self, dot): 1058 label = "{%s|" % self.path 1059 if isSimObject(self.realtype): 1060 label += '%s|' % self.type 1061 1062 if self.children: 1063 # instantiate children in same order they were added for 1064 # backward compatibility (else we can end up with cpu1 1065 # before cpu0). 1066 for c in self.children: 1067 dot.add_edge(pydot.Edge(self.path,c.path, style="bold")) 1068 1069 simobjs = [] 1070 for param in self.params: 1071 try: 1072 if param.value is None: 1073 raise AttributeError, 'Parameter with no value' 1074 1075 value = param.value 1076 string = param.string(value) 1077 except Exception, e: 1078 msg = 'exception in %s:%s\n%s' % (self.name, param.name, e) 1079 e.args = (msg, ) 1080 raise 1081 1082 if isSimObject(param.ptype) and string != "Null": 1083 simobjs.append(string) 1084 else: 1085 label += '%s = %s\\n' % (param.name, string) 1086 1087 for so in simobjs: 1088 label += "|<%s> %s" % (so, so) 1089 dot.add_edge(pydot.Edge("%s:%s" % (self.path, so), so, 1090 tailport="w")) 1091 label += '}' 1092 dot.add_node(pydot.Node(self.path,shape="Mrecord",label=label)) 1093 1094 # recursively dump out children 1095 for c in self.children: 1096 c.outputDot(dot) 1097 1098# Function to provide to C++ so it can look up instances based on paths 1099def resolveSimObject(name): 1100 obj = instanceDict[name] 1101 return obj.getCCObject() 1102 1103def isSimObject(value): 1104 return isinstance(value, SimObject) 1105 1106def isSimObjectClass(value): 1107 return issubclass(value, SimObject) 1108 1109def isSimObjectVector(value): 1110 return isinstance(value, SimObjectVector) 1111 1112def isSimObjectSequence(value): 1113 if not isinstance(value, (list, tuple)) or len(value) == 0: 1114 return False 1115 1116 for val in value: 1117 if not isNullPointer(val) and not isSimObject(val): 1118 return False 1119 1120 return True 1121 1122def isSimObjectOrSequence(value): 1123 return isSimObject(value) or isSimObjectSequence(value) 1124 1125def isRoot(obj): 1126 from m5.objects import Root 1127 return obj and obj is Root.getInstance() 1128 1129def isSimObjectOrVector(value): 1130 return isSimObject(value) or isSimObjectVector(value) 1131 1132def tryAsSimObjectOrVector(value): 1133 if isSimObjectOrVector(value): 1134 return value 1135 if isSimObjectSequence(value): 1136 return SimObjectVector(value) 1137 return None 1138 1139def coerceSimObjectOrVector(value): 1140 value = tryAsSimObjectOrVector(value) 1141 if value is None: 1142 raise TypeError, "SimObject or SimObjectVector expected" 1143 return value 1144 1145baseClasses = allClasses.copy() 1146baseInstances = instanceDict.copy() 1147 1148def clear(): 1149 global allClasses, instanceDict 1150 1151 allClasses = baseClasses.copy() 1152 instanceDict = baseInstances.copy() 1153 1154# __all__ defines the list of symbols that get exported when 1155# 'from config import *' is invoked. Try to keep this reasonably 1156# short to avoid polluting other namespaces. 1157__all__ = [ 'SimObject' ] 1158