SimObject.py revision 8664:42052d5bb793
16657Snate@binkert.org# Copyright (c) 2004-2006 The Regents of The University of Michigan 26657Snate@binkert.org# Copyright (c) 2010 Advanced Micro Devices, Inc. 36657Snate@binkert.org# All rights reserved. 46657Snate@binkert.org# 56657Snate@binkert.org# Redistribution and use in source and binary forms, with or without 66657Snate@binkert.org# modification, are permitted provided that the following conditions are 76657Snate@binkert.org# met: redistributions of source code must retain the above copyright 86657Snate@binkert.org# notice, this list of conditions and the following disclaimer; 96657Snate@binkert.org# redistributions in binary form must reproduce the above copyright 106657Snate@binkert.org# notice, this list of conditions and the following disclaimer in the 116657Snate@binkert.org# documentation and/or other materials provided with the distribution; 126657Snate@binkert.org# neither the name of the copyright holders nor the names of its 136657Snate@binkert.org# contributors may be used to endorse or promote products derived from 146657Snate@binkert.org# this software without specific prior written permission. 156657Snate@binkert.org# 166657Snate@binkert.org# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 176657Snate@binkert.org# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 186657Snate@binkert.org# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 196657Snate@binkert.org# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 206657Snate@binkert.org# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 216657Snate@binkert.org# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 226657Snate@binkert.org# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 236657Snate@binkert.org# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 246657Snate@binkert.org# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 256657Snate@binkert.org# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 266657Snate@binkert.org# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 276657Snate@binkert.org# 286657Snate@binkert.org# Authors: Steve Reinhardt 296657Snate@binkert.org# Nathan Binkert 306657Snate@binkert.org 316657Snate@binkert.orgimport sys 326657Snate@binkert.orgfrom types import FunctionType, MethodType, ModuleType 336657Snate@binkert.org 346657Snate@binkert.orgtry: 356657Snate@binkert.org import pydot 366657Snate@binkert.orgexcept: 376657Snate@binkert.org pydot = False 386657Snate@binkert.org 396657Snate@binkert.orgimport m5 408337Snilay@cs.wisc.edufrom m5.util import * 416657Snate@binkert.org 428337Snilay@cs.wisc.edu# Have to import params up top since Param is referenced on initial 438337Snilay@cs.wisc.edu# load (when SimObject class references Param to create a class 446657Snate@binkert.org# variable, the 'name' param)... 456657Snate@binkert.orgfrom m5.params import * 466657Snate@binkert.org# There are a few things we need that aren't in params.__all__ since 476657Snate@binkert.org# normal users don't need them 486657Snate@binkert.orgfrom m5.params import ParamDesc, VectorParamDesc, \ 496657Snate@binkert.org isNullPointer, SimObjectVector 506657Snate@binkert.org 516657Snate@binkert.orgfrom m5.proxy import * 526657Snate@binkert.orgfrom m5.proxy import isproxy 536657Snate@binkert.org 546657Snate@binkert.org##################################################################### 556657Snate@binkert.org# 566657Snate@binkert.org# M5 Python Configuration Utility 576657Snate@binkert.org# 586657Snate@binkert.org# The basic idea is to write simple Python programs that build Python 596657Snate@binkert.org# objects corresponding to M5 SimObjects for the desired simulation 607839Snilay@cs.wisc.edu# configuration. For now, the Python emits a .ini file that can be 617839Snilay@cs.wisc.edu# parsed by M5. In the future, some tighter integration between M5 626657Snate@binkert.org# and the Python interpreter may allow bypassing the .ini file. 636657Snate@binkert.org# 646657Snate@binkert.org# Each SimObject class in M5 is represented by a Python class with the 656657Snate@binkert.org# same name. The Python inheritance tree mirrors the M5 C++ tree 666657Snate@binkert.org# (e.g., SimpleCPU derives from BaseCPU in both cases, and all 678478Snilay@cs.wisc.edu# SimObjects inherit from a single SimObject base class). To specify 688478Snilay@cs.wisc.edu# an instance of an M5 SimObject in a configuration, the user simply 698478Snilay@cs.wisc.edu# instantiates the corresponding Python object. The parameters for 706657Snate@binkert.org# that SimObject are given by assigning to attributes of the Python 716657Snate@binkert.org# object, either using keyword assignment in the constructor or in 728478Snilay@cs.wisc.edu# separate assignment statements. For example: 736657Snate@binkert.org# 746999Snate@binkert.org# cache = BaseCache(size='64KB') 756657Snate@binkert.org# cache.hit_latency = 3 766657Snate@binkert.org# cache.assoc = 8 776657Snate@binkert.org# 786657Snate@binkert.org# The magic lies in the mapping of the Python attributes for SimObject 796657Snate@binkert.org# classes to the actual SimObject parameter specifications. This 806657Snate@binkert.org# allows parameter validity checking in the Python code. Continuing 817839Snilay@cs.wisc.edu# the example above, the statements "cache.blurfl=3" or 827839Snilay@cs.wisc.edu# "cache.assoc='hello'" would both result in runtime errors in Python, 836657Snate@binkert.org# since the BaseCache object has no 'blurfl' parameter and the 'assoc' 846657Snate@binkert.org# parameter requires an integer, respectively. This magic is done 856657Snate@binkert.org# primarily by overriding the special __setattr__ method that controls 866657Snate@binkert.org# assignment to object attributes. 876657Snate@binkert.org# 886657Snate@binkert.org# Once a set of Python objects have been instantiated in a hierarchy, 896657Snate@binkert.org# calling 'instantiate(obj)' (where obj is the root of the hierarchy) 906657Snate@binkert.org# will generate a .ini file. 916657Snate@binkert.org# 927007Snate@binkert.org##################################################################### 937007Snate@binkert.org 946657Snate@binkert.org# list of all SimObject classes 956657Snate@binkert.orgallClasses = {} 966657Snate@binkert.org 976657Snate@binkert.org# dict to look up SimObjects based on path 988478Snilay@cs.wisc.eduinstanceDict = {} 996657Snate@binkert.org 1006657Snate@binkert.orgdef public_value(key, value): 101 return key.startswith('_') or \ 102 isinstance(value, (FunctionType, MethodType, ModuleType, 103 classmethod, type)) 104 105# The metaclass for SimObject. This class controls how new classes 106# that derive from SimObject are instantiated, and provides inherited 107# class behavior (just like a class controls how instances of that 108# class are instantiated, and provides inherited instance behavior). 109class MetaSimObject(type): 110 # Attributes that can be set only at initialization time 111 init_keywords = { 'abstract' : bool, 112 'cxx_class' : str, 113 'cxx_type' : str, 114 'type' : str } 115 # Attributes that can be set any time 116 keywords = { 'check' : FunctionType } 117 118 # __new__ is called before __init__, and is where the statements 119 # in the body of the class definition get loaded into the class's 120 # __dict__. We intercept this to filter out parameter & port assignments 121 # and only allow "private" attributes to be passed to the base 122 # __new__ (starting with underscore). 123 def __new__(mcls, name, bases, dict): 124 assert name not in allClasses, "SimObject %s already present" % name 125 126 # Copy "private" attributes, functions, and classes to the 127 # official dict. Everything else goes in _init_dict to be 128 # filtered in __init__. 129 cls_dict = {} 130 value_dict = {} 131 for key,val in dict.items(): 132 if public_value(key, val): 133 cls_dict[key] = val 134 else: 135 # must be a param/port setting 136 value_dict[key] = val 137 if 'abstract' not in value_dict: 138 value_dict['abstract'] = False 139 cls_dict['_value_dict'] = value_dict 140 cls = super(MetaSimObject, mcls).__new__(mcls, name, bases, cls_dict) 141 if 'type' in value_dict: 142 allClasses[name] = cls 143 return cls 144 145 # subclass initialization 146 def __init__(cls, name, bases, dict): 147 # calls type.__init__()... I think that's a no-op, but leave 148 # it here just in case it's not. 149 super(MetaSimObject, cls).__init__(name, bases, dict) 150 151 # initialize required attributes 152 153 # class-only attributes 154 cls._params = multidict() # param descriptions 155 cls._ports = multidict() # port descriptions 156 157 # class or instance attributes 158 cls._values = multidict() # param values 159 cls._children = multidict() # SimObject children 160 cls._port_refs = multidict() # port ref objects 161 cls._instantiated = False # really instantiated, cloned, or subclassed 162 163 # We don't support multiple inheritance. If you want to, you 164 # must fix multidict to deal with it properly. 165 if len(bases) > 1: 166 raise TypeError, "SimObjects do not support multiple inheritance" 167 168 base = bases[0] 169 170 # Set up general inheritance via multidicts. A subclass will 171 # inherit all its settings from the base class. The only time 172 # the following is not true is when we define the SimObject 173 # class itself (in which case the multidicts have no parent). 174 if isinstance(base, MetaSimObject): 175 cls._base = base 176 cls._params.parent = base._params 177 cls._ports.parent = base._ports 178 cls._values.parent = base._values 179 cls._children.parent = base._children 180 cls._port_refs.parent = base._port_refs 181 # mark base as having been subclassed 182 base._instantiated = True 183 else: 184 cls._base = None 185 186 # default keyword values 187 if 'type' in cls._value_dict: 188 if 'cxx_class' not in cls._value_dict: 189 cls._value_dict['cxx_class'] = cls._value_dict['type'] 190 191 cls._value_dict['cxx_type'] = '%s *' % cls._value_dict['cxx_class'] 192 193 # Export methods are automatically inherited via C++, so we 194 # don't want the method declarations to get inherited on the 195 # python side (and thus end up getting repeated in the wrapped 196 # versions of derived classes). The code below basicallly 197 # suppresses inheritance by substituting in the base (null) 198 # versions of these methods unless a different version is 199 # explicitly supplied. 200 for method_name in ('export_methods', 'export_method_cxx_predecls', 201 'export_method_swig_predecls'): 202 if method_name not in cls.__dict__: 203 base_method = getattr(MetaSimObject, method_name) 204 m = MethodType(base_method, cls, MetaSimObject) 205 setattr(cls, method_name, m) 206 207 # Now process the _value_dict items. They could be defining 208 # new (or overriding existing) parameters or ports, setting 209 # class keywords (e.g., 'abstract'), or setting parameter 210 # values or port bindings. The first 3 can only be set when 211 # the class is defined, so we handle them here. The others 212 # can be set later too, so just emulate that by calling 213 # setattr(). 214 for key,val in cls._value_dict.items(): 215 # param descriptions 216 if isinstance(val, ParamDesc): 217 cls._new_param(key, val) 218 219 # port objects 220 elif isinstance(val, Port): 221 cls._new_port(key, val) 222 223 # init-time-only keywords 224 elif cls.init_keywords.has_key(key): 225 cls._set_keyword(key, val, cls.init_keywords[key]) 226 227 # default: use normal path (ends up in __setattr__) 228 else: 229 setattr(cls, key, val) 230 231 def _set_keyword(cls, keyword, val, kwtype): 232 if not isinstance(val, kwtype): 233 raise TypeError, 'keyword %s has bad type %s (expecting %s)' % \ 234 (keyword, type(val), kwtype) 235 if isinstance(val, FunctionType): 236 val = classmethod(val) 237 type.__setattr__(cls, keyword, val) 238 239 def _new_param(cls, name, pdesc): 240 # each param desc should be uniquely assigned to one variable 241 assert(not hasattr(pdesc, 'name')) 242 pdesc.name = name 243 cls._params[name] = pdesc 244 if hasattr(pdesc, 'default'): 245 cls._set_param(name, pdesc.default, pdesc) 246 247 def _set_param(cls, name, value, param): 248 assert(param.name == name) 249 try: 250 value = param.convert(value) 251 except Exception, e: 252 msg = "%s\nError setting param %s.%s to %s\n" % \ 253 (e, cls.__name__, name, value) 254 e.args = (msg, ) 255 raise 256 cls._values[name] = value 257 # if param value is a SimObject, make it a child too, so that 258 # it gets cloned properly when the class is instantiated 259 if isSimObjectOrVector(value) and not value.has_parent(): 260 cls._add_cls_child(name, value) 261 262 def _add_cls_child(cls, name, child): 263 # It's a little funky to have a class as a parent, but these 264 # objects should never be instantiated (only cloned, which 265 # clears the parent pointer), and this makes it clear that the 266 # object is not an orphan and can provide better error 267 # messages. 268 child.set_parent(cls, name) 269 cls._children[name] = child 270 271 def _new_port(cls, name, port): 272 # each port should be uniquely assigned to one variable 273 assert(not hasattr(port, 'name')) 274 port.name = name 275 cls._ports[name] = port 276 if hasattr(port, 'default'): 277 cls._cls_get_port_ref(name).connect(port.default) 278 279 # same as _get_port_ref, effectively, but for classes 280 def _cls_get_port_ref(cls, attr): 281 # Return reference that can be assigned to another port 282 # via __setattr__. There is only ever one reference 283 # object per port, but we create them lazily here. 284 ref = cls._port_refs.get(attr) 285 if not ref: 286 ref = cls._ports[attr].makeRef(cls) 287 cls._port_refs[attr] = ref 288 return ref 289 290 # Set attribute (called on foo.attr = value when foo is an 291 # instance of class cls). 292 def __setattr__(cls, attr, value): 293 # normal processing for private attributes 294 if public_value(attr, value): 295 type.__setattr__(cls, attr, value) 296 return 297 298 if cls.keywords.has_key(attr): 299 cls._set_keyword(attr, value, cls.keywords[attr]) 300 return 301 302 if cls._ports.has_key(attr): 303 cls._cls_get_port_ref(attr).connect(value) 304 return 305 306 if isSimObjectOrSequence(value) and cls._instantiated: 307 raise RuntimeError, \ 308 "cannot set SimObject parameter '%s' after\n" \ 309 " class %s has been instantiated or subclassed" \ 310 % (attr, cls.__name__) 311 312 # check for param 313 param = cls._params.get(attr) 314 if param: 315 cls._set_param(attr, value, param) 316 return 317 318 if isSimObjectOrSequence(value): 319 # If RHS is a SimObject, it's an implicit child assignment. 320 cls._add_cls_child(attr, coerceSimObjectOrVector(value)) 321 return 322 323 # no valid assignment... raise exception 324 raise AttributeError, \ 325 "Class %s has no parameter \'%s\'" % (cls.__name__, attr) 326 327 def __getattr__(cls, attr): 328 if attr == 'cxx_class_path': 329 return cls.cxx_class.split('::') 330 331 if attr == 'cxx_class_name': 332 return cls.cxx_class_path[-1] 333 334 if attr == 'cxx_namespaces': 335 return cls.cxx_class_path[:-1] 336 337 if cls._values.has_key(attr): 338 return cls._values[attr] 339 340 if cls._children.has_key(attr): 341 return cls._children[attr] 342 343 raise AttributeError, \ 344 "object '%s' has no attribute '%s'" % (cls.__name__, attr) 345 346 def __str__(cls): 347 return cls.__name__ 348 349 # See ParamValue.cxx_predecls for description. 350 def cxx_predecls(cls, code): 351 code('#include "params/$cls.hh"') 352 353 # See ParamValue.swig_predecls for description. 354 def swig_predecls(cls, code): 355 code('%import "python/m5/internal/param_$cls.i"') 356 357 # Hook for exporting additional C++ methods to Python via SWIG. 358 # Default is none, override using @classmethod in class definition. 359 def export_methods(cls, code): 360 pass 361 362 # Generate the code needed as a prerequisite for the C++ methods 363 # exported via export_methods() to be compiled in the _wrap.cc 364 # file. Typically generates one or more #include statements. If 365 # any methods are exported, typically at least the C++ header 366 # declaring the relevant SimObject class must be included. 367 def export_method_cxx_predecls(cls, code): 368 pass 369 370 # Generate the code needed as a prerequisite for the C++ methods 371 # exported via export_methods() to be processed by SWIG. 372 # Typically generates one or more %include or %import statements. 373 # If any methods are exported, typically at least the C++ header 374 # declaring the relevant SimObject class must be included. 375 def export_method_swig_predecls(cls, code): 376 pass 377 378 # Generate the declaration for this object for wrapping with SWIG. 379 # Generates code that goes into a SWIG .i file. Called from 380 # src/SConscript. 381 def swig_decl(cls, code): 382 class_path = cls.cxx_class.split('::') 383 classname = class_path[-1] 384 namespaces = class_path[:-1] 385 386 # The 'local' attribute restricts us to the params declared in 387 # the object itself, not including inherited params (which 388 # will also be inherited from the base class's param struct 389 # here). 390 params = cls._params.local.values() 391 392 code('%module(package="m5.internal") param_$cls') 393 code() 394 code('%{') 395 code('#include "params/$cls.hh"') 396 for param in params: 397 param.cxx_predecls(code) 398 cls.export_method_cxx_predecls(code) 399 code('%}') 400 code() 401 402 for param in params: 403 param.swig_predecls(code) 404 cls.export_method_swig_predecls(code) 405 406 code() 407 if cls._base: 408 code('%import "python/m5/internal/param_${{cls._base}}.i"') 409 code() 410 411 for ns in namespaces: 412 code('namespace $ns {') 413 414 if namespaces: 415 code('// avoid name conflicts') 416 sep_string = '_COLONS_' 417 flat_name = sep_string.join(class_path) 418 code('%rename($flat_name) $classname;') 419 420 code() 421 code('// stop swig from creating/wrapping default ctor/dtor') 422 code('%nodefault $classname;') 423 code('class $classname') 424 if cls._base: 425 code(' : public ${{cls._base.cxx_class}}') 426 code('{') 427 code(' public:') 428 cls.export_methods(code) 429 code('};') 430 431 for ns in reversed(namespaces): 432 code('} // namespace $ns') 433 434 code() 435 code('%include "params/$cls.hh"') 436 437 438 # Generate the C++ declaration (.hh file) for this SimObject's 439 # param struct. Called from src/SConscript. 440 def cxx_param_decl(cls, code): 441 # The 'local' attribute restricts us to the params declared in 442 # the object itself, not including inherited params (which 443 # will also be inherited from the base class's param struct 444 # here). 445 params = cls._params.local.values() 446 try: 447 ptypes = [p.ptype for p in params] 448 except: 449 print cls, p, p.ptype_str 450 print params 451 raise 452 453 class_path = cls._value_dict['cxx_class'].split('::') 454 455 code('''\ 456#ifndef __PARAMS__${cls}__ 457#define __PARAMS__${cls}__ 458 459''') 460 461 # A forward class declaration is sufficient since we are just 462 # declaring a pointer. 463 for ns in class_path[:-1]: 464 code('namespace $ns {') 465 code('class $0;', class_path[-1]) 466 for ns in reversed(class_path[:-1]): 467 code('} // namespace $ns') 468 code() 469 470 # The base SimObject has a couple of params that get 471 # automatically set from Python without being declared through 472 # the normal Param mechanism; we slip them in here (needed 473 # predecls now, actual declarations below) 474 if cls == SimObject: 475 code(''' 476#ifndef PY_VERSION 477struct PyObject; 478#endif 479 480#include <string> 481 482struct EventQueue; 483''') 484 for param in params: 485 param.cxx_predecls(code) 486 code() 487 488 if cls._base: 489 code('#include "params/${{cls._base.type}}.hh"') 490 code() 491 492 for ptype in ptypes: 493 if issubclass(ptype, Enum): 494 code('#include "enums/${{ptype.__name__}}.hh"') 495 code() 496 497 # now generate the actual param struct 498 code("struct ${cls}Params") 499 if cls._base: 500 code(" : public ${{cls._base.type}}Params") 501 code("{") 502 if not hasattr(cls, 'abstract') or not cls.abstract: 503 if 'type' in cls.__dict__: 504 code(" ${{cls.cxx_type}} create();") 505 506 code.indent() 507 if cls == SimObject: 508 code(''' 509 SimObjectParams() 510 { 511 extern EventQueue mainEventQueue; 512 eventq = &mainEventQueue; 513 } 514 virtual ~SimObjectParams() {} 515 516 std::string name; 517 PyObject *pyobj; 518 EventQueue *eventq; 519 ''') 520 for param in params: 521 param.cxx_decl(code) 522 code.dedent() 523 code('};') 524 525 code() 526 code('#endif // __PARAMS__${cls}__') 527 return code 528 529 530 531# The SimObject class is the root of the special hierarchy. Most of 532# the code in this class deals with the configuration hierarchy itself 533# (parent/child node relationships). 534class SimObject(object): 535 # Specify metaclass. Any class inheriting from SimObject will 536 # get this metaclass. 537 __metaclass__ = MetaSimObject 538 type = 'SimObject' 539 abstract = True 540 541 @classmethod 542 def export_method_cxx_predecls(cls, code): 543 code(''' 544#include <Python.h> 545 546#include "sim/serialize.hh" 547#include "sim/sim_object.hh" 548''') 549 550 @classmethod 551 def export_method_swig_predecls(cls, code): 552 code(''' 553%include <std_string.i> 554''') 555 556 @classmethod 557 def export_methods(cls, code): 558 code(''' 559 enum State { 560 Running, 561 Draining, 562 Drained 563 }; 564 565 void init(); 566 void loadState(Checkpoint *cp); 567 void initState(); 568 void regStats(); 569 void regFormulas(); 570 void resetStats(); 571 void startup(); 572 573 unsigned int drain(Event *drain_event); 574 void resume(); 575 void switchOut(); 576 void takeOverFrom(BaseCPU *cpu); 577''') 578 579 # Initialize new instance. For objects with SimObject-valued 580 # children, we need to recursively clone the classes represented 581 # by those param values as well in a consistent "deep copy"-style 582 # fashion. That is, we want to make sure that each instance is 583 # cloned only once, and that if there are multiple references to 584 # the same original object, we end up with the corresponding 585 # cloned references all pointing to the same cloned instance. 586 def __init__(self, **kwargs): 587 ancestor = kwargs.get('_ancestor') 588 memo_dict = kwargs.get('_memo') 589 if memo_dict is None: 590 # prepare to memoize any recursively instantiated objects 591 memo_dict = {} 592 elif ancestor: 593 # memoize me now to avoid problems with recursive calls 594 memo_dict[ancestor] = self 595 596 if not ancestor: 597 ancestor = self.__class__ 598 ancestor._instantiated = True 599 600 # initialize required attributes 601 self._parent = None 602 self._name = None 603 self._ccObject = None # pointer to C++ object 604 self._ccParams = None 605 self._instantiated = False # really "cloned" 606 607 # Clone children specified at class level. No need for a 608 # multidict here since we will be cloning everything. 609 # Do children before parameter values so that children that 610 # are also param values get cloned properly. 611 self._children = {} 612 for key,val in ancestor._children.iteritems(): 613 self.add_child(key, val(_memo=memo_dict)) 614 615 # Inherit parameter values from class using multidict so 616 # individual value settings can be overridden but we still 617 # inherit late changes to non-overridden class values. 618 self._values = multidict(ancestor._values) 619 # clone SimObject-valued parameters 620 for key,val in ancestor._values.iteritems(): 621 val = tryAsSimObjectOrVector(val) 622 if val is not None: 623 self._values[key] = val(_memo=memo_dict) 624 625 # clone port references. no need to use a multidict here 626 # since we will be creating new references for all ports. 627 self._port_refs = {} 628 for key,val in ancestor._port_refs.iteritems(): 629 self._port_refs[key] = val.clone(self, memo_dict) 630 # apply attribute assignments from keyword args, if any 631 for key,val in kwargs.iteritems(): 632 setattr(self, key, val) 633 634 # "Clone" the current instance by creating another instance of 635 # this instance's class, but that inherits its parameter values 636 # and port mappings from the current instance. If we're in a 637 # "deep copy" recursive clone, check the _memo dict to see if 638 # we've already cloned this instance. 639 def __call__(self, **kwargs): 640 memo_dict = kwargs.get('_memo') 641 if memo_dict is None: 642 # no memo_dict: must be top-level clone operation. 643 # this is only allowed at the root of a hierarchy 644 if self._parent: 645 raise RuntimeError, "attempt to clone object %s " \ 646 "not at the root of a tree (parent = %s)" \ 647 % (self, self._parent) 648 # create a new dict and use that. 649 memo_dict = {} 650 kwargs['_memo'] = memo_dict 651 elif memo_dict.has_key(self): 652 # clone already done & memoized 653 return memo_dict[self] 654 return self.__class__(_ancestor = self, **kwargs) 655 656 def _get_port_ref(self, attr): 657 # Return reference that can be assigned to another port 658 # via __setattr__. There is only ever one reference 659 # object per port, but we create them lazily here. 660 ref = self._port_refs.get(attr) 661 if not ref: 662 ref = self._ports[attr].makeRef(self) 663 self._port_refs[attr] = ref 664 return ref 665 666 def __getattr__(self, attr): 667 if self._ports.has_key(attr): 668 return self._get_port_ref(attr) 669 670 if self._values.has_key(attr): 671 return self._values[attr] 672 673 if self._children.has_key(attr): 674 return self._children[attr] 675 676 # If the attribute exists on the C++ object, transparently 677 # forward the reference there. This is typically used for 678 # SWIG-wrapped methods such as init(), regStats(), 679 # regFormulas(), resetStats(), startup(), drain(), and 680 # resume(). 681 if self._ccObject and hasattr(self._ccObject, attr): 682 return getattr(self._ccObject, attr) 683 684 raise AttributeError, "object '%s' has no attribute '%s'" \ 685 % (self.__class__.__name__, attr) 686 687 # Set attribute (called on foo.attr = value when foo is an 688 # instance of class cls). 689 def __setattr__(self, attr, value): 690 # normal processing for private attributes 691 if attr.startswith('_'): 692 object.__setattr__(self, attr, value) 693 return 694 695 if self._ports.has_key(attr): 696 # set up port connection 697 self._get_port_ref(attr).connect(value) 698 return 699 700 if isSimObjectOrSequence(value) and self._instantiated: 701 raise RuntimeError, \ 702 "cannot set SimObject parameter '%s' after\n" \ 703 " instance been cloned %s" % (attr, `self`) 704 705 param = self._params.get(attr) 706 if param: 707 try: 708 value = param.convert(value) 709 except Exception, e: 710 msg = "%s\nError setting param %s.%s to %s\n" % \ 711 (e, self.__class__.__name__, attr, value) 712 e.args = (msg, ) 713 raise 714 self._values[attr] = value 715 # implicitly parent unparented objects assigned as params 716 if isSimObjectOrVector(value) and not value.has_parent(): 717 self.add_child(attr, value) 718 return 719 720 # if RHS is a SimObject, it's an implicit child assignment 721 if isSimObjectOrSequence(value): 722 self.add_child(attr, value) 723 return 724 725 # no valid assignment... raise exception 726 raise AttributeError, "Class %s has no parameter %s" \ 727 % (self.__class__.__name__, attr) 728 729 730 # this hack allows tacking a '[0]' onto parameters that may or may 731 # not be vectors, and always getting the first element (e.g. cpus) 732 def __getitem__(self, key): 733 if key == 0: 734 return self 735 raise TypeError, "Non-zero index '%s' to SimObject" % key 736 737 # Also implemented by SimObjectVector 738 def clear_parent(self, old_parent): 739 assert self._parent is old_parent 740 self._parent = None 741 742 # Also implemented by SimObjectVector 743 def set_parent(self, parent, name): 744 self._parent = parent 745 self._name = name 746 747 # Also implemented by SimObjectVector 748 def get_name(self): 749 return self._name 750 751 # Also implemented by SimObjectVector 752 def has_parent(self): 753 return self._parent is not None 754 755 # clear out child with given name. This code is not likely to be exercised. 756 # See comment in add_child. 757 def clear_child(self, name): 758 child = self._children[name] 759 child.clear_parent(self) 760 del self._children[name] 761 762 # Add a new child to this object. 763 def add_child(self, name, child): 764 child = coerceSimObjectOrVector(child) 765 if child.has_parent(): 766 print "warning: add_child('%s'): child '%s' already has parent" % \ 767 (name, child.get_name()) 768 if self._children.has_key(name): 769 # This code path had an undiscovered bug that would make it fail 770 # at runtime. It had been here for a long time and was only 771 # exposed by a buggy script. Changes here will probably not be 772 # exercised without specialized testing. 773 self.clear_child(name) 774 child.set_parent(self, name) 775 self._children[name] = child 776 777 # Take SimObject-valued parameters that haven't been explicitly 778 # assigned as children and make them children of the object that 779 # they were assigned to as a parameter value. This guarantees 780 # that when we instantiate all the parameter objects we're still 781 # inside the configuration hierarchy. 782 def adoptOrphanParams(self): 783 for key,val in self._values.iteritems(): 784 if not isSimObjectVector(val) and isSimObjectSequence(val): 785 # need to convert raw SimObject sequences to 786 # SimObjectVector class so we can call has_parent() 787 val = SimObjectVector(val) 788 self._values[key] = val 789 if isSimObjectOrVector(val) and not val.has_parent(): 790 print "warning: %s adopting orphan SimObject param '%s'" \ 791 % (self, key) 792 self.add_child(key, val) 793 794 def path(self): 795 if not self._parent: 796 return '<orphan %s>' % self.__class__ 797 ppath = self._parent.path() 798 if ppath == 'root': 799 return self._name 800 return ppath + "." + self._name 801 802 def __str__(self): 803 return self.path() 804 805 def ini_str(self): 806 return self.path() 807 808 def find_any(self, ptype): 809 if isinstance(self, ptype): 810 return self, True 811 812 found_obj = None 813 for child in self._children.itervalues(): 814 if isinstance(child, ptype): 815 if found_obj != None and child != found_obj: 816 raise AttributeError, \ 817 'parent.any matched more than one: %s %s' % \ 818 (found_obj.path, child.path) 819 found_obj = child 820 # search param space 821 for pname,pdesc in self._params.iteritems(): 822 if issubclass(pdesc.ptype, ptype): 823 match_obj = self._values[pname] 824 if found_obj != None and found_obj != match_obj: 825 raise AttributeError, \ 826 'parent.any matched more than one: %s and %s' % (found_obj.path, match_obj.path) 827 found_obj = match_obj 828 return found_obj, found_obj != None 829 830 def find_all(self, ptype): 831 all = {} 832 # search children 833 for child in self._children.itervalues(): 834 if isinstance(child, ptype) and not isproxy(child) and \ 835 not isNullPointer(child): 836 all[child] = True 837 # search param space 838 for pname,pdesc in self._params.iteritems(): 839 if issubclass(pdesc.ptype, ptype): 840 match_obj = self._values[pname] 841 if not isproxy(match_obj) and not isNullPointer(match_obj): 842 all[match_obj] = True 843 return all.keys(), True 844 845 def unproxy(self, base): 846 return self 847 848 def unproxyParams(self): 849 for param in self._params.iterkeys(): 850 value = self._values.get(param) 851 if value != None and isproxy(value): 852 try: 853 value = value.unproxy(self) 854 except: 855 print "Error in unproxying param '%s' of %s" % \ 856 (param, self.path()) 857 raise 858 setattr(self, param, value) 859 860 # Unproxy ports in sorted order so that 'append' operations on 861 # vector ports are done in a deterministic fashion. 862 port_names = self._ports.keys() 863 port_names.sort() 864 for port_name in port_names: 865 port = self._port_refs.get(port_name) 866 if port != None: 867 port.unproxy(self) 868 869 def print_ini(self, ini_file): 870 print >>ini_file, '[' + self.path() + ']' # .ini section header 871 872 instanceDict[self.path()] = self 873 874 if hasattr(self, 'type'): 875 print >>ini_file, 'type=%s' % self.type 876 877 if len(self._children.keys()): 878 print >>ini_file, 'children=%s' % \ 879 ' '.join(self._children[n].get_name() \ 880 for n in sorted(self._children.keys())) 881 882 for param in sorted(self._params.keys()): 883 value = self._values.get(param) 884 if value != None: 885 print >>ini_file, '%s=%s' % (param, 886 self._values[param].ini_str()) 887 888 for port_name in sorted(self._ports.keys()): 889 port = self._port_refs.get(port_name, None) 890 if port != None: 891 print >>ini_file, '%s=%s' % (port_name, port.ini_str()) 892 893 print >>ini_file # blank line between objects 894 895 # generate a tree of dictionaries expressing all the parameters in the 896 # instantiated system for use by scripts that want to do power, thermal 897 # visualization, and other similar tasks 898 def get_config_as_dict(self): 899 d = attrdict() 900 if hasattr(self, 'type'): 901 d.type = self.type 902 if hasattr(self, 'cxx_class'): 903 d.cxx_class = self.cxx_class 904 905 for param in sorted(self._params.keys()): 906 value = self._values.get(param) 907 try: 908 d[param] = self._values[param].value 909 except AttributeError: 910 pass 911 912 for n in sorted(self._children.keys()): 913 d[self._children[n].get_name()] = self._children[n].get_config_as_dict() 914 915 for port_name in sorted(self._ports.keys()): 916 port = self._port_refs.get(port_name, None) 917 if port != None: 918 # Might want to actually make this reference the object 919 # in the future, although execing the string problem would 920 # get some of the way there 921 d[port_name] = port.ini_str() 922 923 return d 924 925 def getCCParams(self): 926 if self._ccParams: 927 return self._ccParams 928 929 cc_params_struct = getattr(m5.internal.params, '%sParams' % self.type) 930 cc_params = cc_params_struct() 931 cc_params.pyobj = self 932 cc_params.name = str(self) 933 934 param_names = self._params.keys() 935 param_names.sort() 936 for param in param_names: 937 value = self._values.get(param) 938 if value is None: 939 fatal("%s.%s without default or user set value", 940 self.path(), param) 941 942 value = value.getValue() 943 if isinstance(self._params[param], VectorParamDesc): 944 assert isinstance(value, list) 945 vec = getattr(cc_params, param) 946 assert not len(vec) 947 for v in value: 948 vec.append(v) 949 else: 950 setattr(cc_params, param, value) 951 952 port_names = self._ports.keys() 953 port_names.sort() 954 for port_name in port_names: 955 port = self._port_refs.get(port_name, None) 956 if port != None: 957 setattr(cc_params, port_name, port) 958 self._ccParams = cc_params 959 return self._ccParams 960 961 # Get C++ object corresponding to this object, calling C++ if 962 # necessary to construct it. Does *not* recursively create 963 # children. 964 def getCCObject(self): 965 if not self._ccObject: 966 # Make sure this object is in the configuration hierarchy 967 if not self._parent and not isRoot(self): 968 raise RuntimeError, "Attempt to instantiate orphan node" 969 # Cycles in the configuration hierarchy are not supported. This 970 # will catch the resulting recursion and stop. 971 self._ccObject = -1 972 params = self.getCCParams() 973 self._ccObject = params.create() 974 elif self._ccObject == -1: 975 raise RuntimeError, "%s: Cycle found in configuration hierarchy." \ 976 % self.path() 977 return self._ccObject 978 979 def descendants(self): 980 yield self 981 for child in self._children.itervalues(): 982 for obj in child.descendants(): 983 yield obj 984 985 # Call C++ to create C++ object corresponding to this object 986 def createCCObject(self): 987 self.getCCParams() 988 self.getCCObject() # force creation 989 990 def getValue(self): 991 return self.getCCObject() 992 993 # Create C++ port connections corresponding to the connections in 994 # _port_refs 995 def connectPorts(self): 996 for portRef in self._port_refs.itervalues(): 997 portRef.ccConnect() 998 999 def getMemoryMode(self): 1000 if not isinstance(self, m5.objects.System): 1001 return None 1002 1003 return self._ccObject.getMemoryMode() 1004 1005 def changeTiming(self, mode): 1006 if isinstance(self, m5.objects.System): 1007 # i don't know if there's a better way to do this - calling 1008 # setMemoryMode directly from self._ccObject results in calling 1009 # SimObject::setMemoryMode, not the System::setMemoryMode 1010 self._ccObject.setMemoryMode(mode) 1011 1012 def takeOverFrom(self, old_cpu): 1013 self._ccObject.takeOverFrom(old_cpu._ccObject) 1014 1015 # generate output file for 'dot' to display as a pretty graph. 1016 # this code is currently broken. 1017 def outputDot(self, dot): 1018 label = "{%s|" % self.path 1019 if isSimObject(self.realtype): 1020 label += '%s|' % self.type 1021 1022 if self.children: 1023 # instantiate children in same order they were added for 1024 # backward compatibility (else we can end up with cpu1 1025 # before cpu0). 1026 for c in self.children: 1027 dot.add_edge(pydot.Edge(self.path,c.path, style="bold")) 1028 1029 simobjs = [] 1030 for param in self.params: 1031 try: 1032 if param.value is None: 1033 raise AttributeError, 'Parameter with no value' 1034 1035 value = param.value 1036 string = param.string(value) 1037 except Exception, e: 1038 msg = 'exception in %s:%s\n%s' % (self.name, param.name, e) 1039 e.args = (msg, ) 1040 raise 1041 1042 if isSimObject(param.ptype) and string != "Null": 1043 simobjs.append(string) 1044 else: 1045 label += '%s = %s\\n' % (param.name, string) 1046 1047 for so in simobjs: 1048 label += "|<%s> %s" % (so, so) 1049 dot.add_edge(pydot.Edge("%s:%s" % (self.path, so), so, 1050 tailport="w")) 1051 label += '}' 1052 dot.add_node(pydot.Node(self.path,shape="Mrecord",label=label)) 1053 1054 # recursively dump out children 1055 for c in self.children: 1056 c.outputDot(dot) 1057 1058# Function to provide to C++ so it can look up instances based on paths 1059def resolveSimObject(name): 1060 obj = instanceDict[name] 1061 return obj.getCCObject() 1062 1063def isSimObject(value): 1064 return isinstance(value, SimObject) 1065 1066def isSimObjectClass(value): 1067 return issubclass(value, SimObject) 1068 1069def isSimObjectVector(value): 1070 return isinstance(value, SimObjectVector) 1071 1072def isSimObjectSequence(value): 1073 if not isinstance(value, (list, tuple)) or len(value) == 0: 1074 return False 1075 1076 for val in value: 1077 if not isNullPointer(val) and not isSimObject(val): 1078 return False 1079 1080 return True 1081 1082def isSimObjectOrSequence(value): 1083 return isSimObject(value) or isSimObjectSequence(value) 1084 1085def isRoot(obj): 1086 from m5.objects import Root 1087 return obj and obj is Root.getInstance() 1088 1089def isSimObjectOrVector(value): 1090 return isSimObject(value) or isSimObjectVector(value) 1091 1092def tryAsSimObjectOrVector(value): 1093 if isSimObjectOrVector(value): 1094 return value 1095 if isSimObjectSequence(value): 1096 return SimObjectVector(value) 1097 return None 1098 1099def coerceSimObjectOrVector(value): 1100 value = tryAsSimObjectOrVector(value) 1101 if value is None: 1102 raise TypeError, "SimObject or SimObjectVector expected" 1103 return value 1104 1105baseClasses = allClasses.copy() 1106baseInstances = instanceDict.copy() 1107 1108def clear(): 1109 global allClasses, instanceDict 1110 1111 allClasses = baseClasses.copy() 1112 instanceDict = baseInstances.copy() 1113 1114# __all__ defines the list of symbols that get exported when 1115# 'from config import *' is invoked. Try to keep this reasonably 1116# short to avoid polluting other namespaces. 1117__all__ = [ 'SimObject' ] 1118