SimObject.py revision 10195:7d4d0cd3f7e5
113559Snikos.nikoleris@arm.com# Copyright (c) 2012 ARM Limited 212109SRekai.GonzalezAlberquilla@arm.com# All rights reserved. 312109SRekai.GonzalezAlberquilla@arm.com# 412109SRekai.GonzalezAlberquilla@arm.com# The license below extends only to copyright in the software and shall 512109SRekai.GonzalezAlberquilla@arm.com# not be construed as granting a license to any other intellectual 612109SRekai.GonzalezAlberquilla@arm.com# property including but not limited to intellectual property relating 712109SRekai.GonzalezAlberquilla@arm.com# to a hardware implementation of the functionality of the software 812109SRekai.GonzalezAlberquilla@arm.com# licensed hereunder. You may use the software subject to the license 912109SRekai.GonzalezAlberquilla@arm.com# terms below provided that you ensure that this notice is replicated 1012109SRekai.GonzalezAlberquilla@arm.com# unmodified and in its entirety in all distributions of the software, 1112109SRekai.GonzalezAlberquilla@arm.com# modified or unmodified, in source code or in binary form. 1212109SRekai.GonzalezAlberquilla@arm.com# 134486Sbinkertn@umich.edu# Copyright (c) 2004-2006 The Regents of The University of Michigan 144486Sbinkertn@umich.edu# Copyright (c) 2010-20013 Advanced Micro Devices, Inc. 154486Sbinkertn@umich.edu# Copyright (c) 2013 Mark D. Hill and David A. Wood 164486Sbinkertn@umich.edu# All rights reserved. 174486Sbinkertn@umich.edu# 184486Sbinkertn@umich.edu# Redistribution and use in source and binary forms, with or without 194486Sbinkertn@umich.edu# modification, are permitted provided that the following conditions are 204486Sbinkertn@umich.edu# met: redistributions of source code must retain the above copyright 214486Sbinkertn@umich.edu# notice, this list of conditions and the following disclaimer; 224486Sbinkertn@umich.edu# redistributions in binary form must reproduce the above copyright 234486Sbinkertn@umich.edu# notice, this list of conditions and the following disclaimer in the 244486Sbinkertn@umich.edu# documentation and/or other materials provided with the distribution; 254486Sbinkertn@umich.edu# neither the name of the copyright holders nor the names of its 264486Sbinkertn@umich.edu# contributors may be used to endorse or promote products derived from 274486Sbinkertn@umich.edu# this software without specific prior written permission. 284486Sbinkertn@umich.edu# 294486Sbinkertn@umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 304486Sbinkertn@umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 314486Sbinkertn@umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 324486Sbinkertn@umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 334486Sbinkertn@umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 344486Sbinkertn@umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 354486Sbinkertn@umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 364486Sbinkertn@umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 374486Sbinkertn@umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 384486Sbinkertn@umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 394486Sbinkertn@umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 404486Sbinkertn@umich.edu# 4112563Sgabeblack@google.com# Authors: Steve Reinhardt 4212563Sgabeblack@google.com# Nathan Binkert 436654Snate@binkert.org# Andreas Hansson 443102SN/A 453102SN/Aimport sys 4613665Sandreas.sandberg@arm.comfrom types import FunctionType, MethodType, ModuleType 4713665Sandreas.sandberg@arm.com 4813665Sandreas.sandberg@arm.comimport m5 4913665Sandreas.sandberg@arm.comfrom m5.util import * 5013665Sandreas.sandberg@arm.com 514486Sbinkertn@umich.edu# Have to import params up top since Param is referenced on initial 5213559Snikos.nikoleris@arm.com# load (when SimObject class references Param to create a class 5313559Snikos.nikoleris@arm.com# variable, the 'name' param)... 5413559Snikos.nikoleris@arm.comfrom m5.params import * 5513560Snikos.nikoleris@arm.com# There are a few things we need that aren't in params.__all__ since 5613560Snikos.nikoleris@arm.com# normal users don't need them 5713560Snikos.nikoleris@arm.comfrom m5.params import ParamDesc, VectorParamDesc, \ 5813563Snikos.nikoleris@arm.com isNullPointer, SimObjectVector, Port 5913563Snikos.nikoleris@arm.com 6013563Snikos.nikoleris@arm.comfrom m5.proxy import * 612817SN/Afrom m5.proxy import isproxy 622817SN/A 639341SAndreas.Sandberg@arm.com##################################################################### 649341SAndreas.Sandberg@arm.com# 659518SAndreas.Sandberg@ARM.com# M5 Python Configuration Utility 669518SAndreas.Sandberg@ARM.com# 679518SAndreas.Sandberg@ARM.com# The basic idea is to write simple Python programs that build Python 689518SAndreas.Sandberg@ARM.com# objects corresponding to M5 SimObjects for the desired simulation 699518SAndreas.Sandberg@ARM.com# configuration. For now, the Python emits a .ini file that can be 709518SAndreas.Sandberg@ARM.com# parsed by M5. In the future, some tighter integration between M5 719518SAndreas.Sandberg@ARM.com# and the Python interpreter may allow bypassing the .ini file. 729518SAndreas.Sandberg@ARM.com# 739518SAndreas.Sandberg@ARM.com# Each SimObject class in M5 is represented by a Python class with the 749518SAndreas.Sandberg@ARM.com# same name. The Python inheritance tree mirrors the M5 C++ tree 759518SAndreas.Sandberg@ARM.com# (e.g., SimpleCPU derives from BaseCPU in both cases, and all 769518SAndreas.Sandberg@ARM.com# SimObjects inherit from a single SimObject base class). To specify 772932SN/A# an instance of an M5 SimObject in a configuration, the user simply 781681SN/A# instantiates the corresponding Python object. The parameters for 7911780Sarthur.perais@inria.fr# that SimObject are given by assigning to attributes of the Python 8013710Sgabor.dozsa@arm.com# object, either using keyword assignment in the constructor or in 8113710Sgabor.dozsa@arm.com# separate assignment statements. For example: 8213710Sgabor.dozsa@arm.com# 831681SN/A# cache = BaseCache(size='64KB') 849184Sandreas.hansson@arm.com# cache.hit_latency = 3 859184Sandreas.hansson@arm.com# cache.assoc = 8 869184Sandreas.hansson@arm.com# 879184Sandreas.hansson@arm.com# The magic lies in the mapping of the Python attributes for SimObject 889184Sandreas.hansson@arm.com# classes to the actual SimObject parameter specifications. This 892932SN/A# allows parameter validity checking in the Python code. Continuing 909982Satgutier@umich.edu# the example above, the statements "cache.blurfl=3" or 9110331Smitch.hayenga@arm.com# "cache.assoc='hello'" would both result in runtime errors in Python, 9210331Smitch.hayenga@arm.com# since the BaseCache object has no 'blurfl' parameter and the 'assoc' 932932SN/A# parameter requires an integer, respectively. This magic is done 949184Sandreas.hansson@arm.com# primarily by overriding the special __setattr__ method that controls 959184Sandreas.hansson@arm.com# assignment to object attributes. 969184Sandreas.hansson@arm.com# 979184Sandreas.hansson@arm.com# Once a set of Python objects have been instantiated in a hierarchy, 989184Sandreas.hansson@arm.com# calling 'instantiate(obj)' (where obj is the root of the hierarchy) 992932SN/A# will generate a .ini file. 1001681SN/A# 1019184Sandreas.hansson@arm.com##################################################################### 1029184Sandreas.hansson@arm.com 1039184Sandreas.hansson@arm.com# list of all SimObject classes 1049184Sandreas.hansson@arm.comallClasses = {} 1052932SN/A 1061681SN/A# dict to look up SimObjects based on path 1079184Sandreas.hansson@arm.cominstanceDict = {} 1082932SN/A 1099184Sandreas.hansson@arm.com# Did any of the SimObjects lack a header file? 1102932SN/AnoCxxHeader = False 1119184Sandreas.hansson@arm.com 1122932SN/Adef public_value(key, value): 1132932SN/A return key.startswith('_') or \ 1142932SN/A isinstance(value, (FunctionType, MethodType, ModuleType, 1152932SN/A classmethod, type)) 1163223SN/A 1172932SN/A# The metaclass for SimObject. This class controls how new classes 1189184Sandreas.hansson@arm.com# that derive from SimObject are instantiated, and provides inherited 1191681SN/A# class behavior (just like a class controls how instances of that 1209184Sandreas.hansson@arm.com# class are instantiated, and provides inherited instance behavior). 1212932SN/Aclass MetaSimObject(type): 1222932SN/A # Attributes that can be set only at initialization time 1239184Sandreas.hansson@arm.com init_keywords = { 'abstract' : bool, 1249184Sandreas.hansson@arm.com 'cxx_class' : str, 1251681SN/A 'cxx_type' : str, 1262932SN/A 'cxx_header' : str, 1272932SN/A 'type' : str, 1281681SN/A 'cxx_bases' : list } 1292932SN/A # Attributes that can be set any time 1302932SN/A keywords = { 'check' : FunctionType } 1318199SAli.Saidi@ARM.com 1328199SAli.Saidi@ARM.com # __new__ is called before __init__, and is where the statements 1338199SAli.Saidi@ARM.com # in the body of the class definition get loaded into the class's 1348519SAli.Saidi@ARM.com # __dict__. We intercept this to filter out parameter & port assignments 1358519SAli.Saidi@ARM.com # and only allow "private" attributes to be passed to the base 1362932SN/A # __new__ (starting with underscore). 1372932SN/A def __new__(mcls, name, bases, dict): 1381681SN/A assert name not in allClasses, "SimObject %s already present" % name 1392932SN/A 1401681SN/A # Copy "private" attributes, functions, and classes to the 1412932SN/A # official dict. Everything else goes in _init_dict to be 1422932SN/A # filtered in __init__. 1432932SN/A cls_dict = {} 1449921Syasuko.eckert@amd.com value_dict = {} 1459921Syasuko.eckert@amd.com for key,val in dict.items(): 14610338SCurtis.Dunham@arm.com if public_value(key, val): 1479921Syasuko.eckert@amd.com cls_dict[key] = val 1489921Syasuko.eckert@amd.com else: 1499921Syasuko.eckert@amd.com # must be a param/port setting 1509921Syasuko.eckert@amd.com value_dict[key] = val 1519921Syasuko.eckert@amd.com if 'abstract' not in value_dict: 1529921Syasuko.eckert@amd.com value_dict['abstract'] = False 1539921Syasuko.eckert@amd.com if 'cxx_bases' not in value_dict: 15412109SRekai.GonzalezAlberquilla@arm.com value_dict['cxx_bases'] = [] 15512109SRekai.GonzalezAlberquilla@arm.com cls_dict['_value_dict'] = value_dict 15613610Sgiacomo.gabrielli@arm.com cls = super(MetaSimObject, mcls).__new__(mcls, name, bases, cls_dict) 15713610Sgiacomo.gabrielli@arm.com if 'type' in value_dict: 1589921Syasuko.eckert@amd.com allClasses[name] = cls 1599921Syasuko.eckert@amd.com return cls 1602932SN/A 1612932SN/A # subclass initialization 1621681SN/A def __init__(cls, name, bases, dict): 1634597Sbinkertn@umich.edu # calls type.__init__()... I think that's a no-op, but leave 16413559Snikos.nikoleris@arm.com # it here just in case it's not. 16513560Snikos.nikoleris@arm.com super(MetaSimObject, cls).__init__(name, bases, dict) 16613560Snikos.nikoleris@arm.com 1674597Sbinkertn@umich.edu # initialize required attributes 16813561Snikos.nikoleris@arm.com 16913561Snikos.nikoleris@arm.com # class-only attributes 1704597Sbinkertn@umich.edu cls._params = multidict() # param descriptions 17113562Snikos.nikoleris@arm.com cls._ports = multidict() # port descriptions 17213562Snikos.nikoleris@arm.com 1734597Sbinkertn@umich.edu # class or instance attributes 17413563Snikos.nikoleris@arm.com cls._values = multidict() # param values 1754303SN/A cls._children = multidict() # SimObject children 17610785Sgope@wisc.edu cls._port_refs = multidict() # port ref objects 1779849Sandreas.hansson@arm.com cls._instantiated = False # really instantiated, cloned, or subclassed 1789849Sandreas.hansson@arm.com 1798727Snilay@cs.wisc.edu # We don't support multiple inheritance of sim objects. If you want 1808727Snilay@cs.wisc.edu # to, you must fix multidict to deal with it properly. Non sim-objects 1818887Sgeoffrey.blake@arm.com # are ok, though 1828887Sgeoffrey.blake@arm.com bTotal = 0 1838887Sgeoffrey.blake@arm.com for c in bases: 18413665Sandreas.sandberg@arm.com if isinstance(c, MetaSimObject): 1858887Sgeoffrey.blake@arm.com bTotal += 1 1868887Sgeoffrey.blake@arm.com if bTotal > 1: 1878887Sgeoffrey.blake@arm.com raise TypeError, "SimObjects do not support multiple inheritance" 1888887Sgeoffrey.blake@arm.com 1898887Sgeoffrey.blake@arm.com base = bases[0] 1908887Sgeoffrey.blake@arm.com 1918887Sgeoffrey.blake@arm.com # Set up general inheritance via multidicts. A subclass will 1929132Satgutier@umich.edu # inherit all its settings from the base class. The only time 1938887Sgeoffrey.blake@arm.com # the following is not true is when we define the SimObject 1948887Sgeoffrey.blake@arm.com # class itself (in which case the multidicts have no parent). 19512563Sgabeblack@google.com if isinstance(base, MetaSimObject): 1968887Sgeoffrey.blake@arm.com cls._base = base 197 cls._params.parent = base._params 198 cls._ports.parent = base._ports 199 cls._values.parent = base._values 200 cls._children.parent = base._children 201 cls._port_refs.parent = base._port_refs 202 # mark base as having been subclassed 203 base._instantiated = True 204 else: 205 cls._base = None 206 207 # default keyword values 208 if 'type' in cls._value_dict: 209 if 'cxx_class' not in cls._value_dict: 210 cls._value_dict['cxx_class'] = cls._value_dict['type'] 211 212 cls._value_dict['cxx_type'] = '%s *' % cls._value_dict['cxx_class'] 213 214 if 'cxx_header' not in cls._value_dict: 215 global noCxxHeader 216 noCxxHeader = True 217 warn("No header file specified for SimObject: %s", name) 218 219 # Export methods are automatically inherited via C++, so we 220 # don't want the method declarations to get inherited on the 221 # python side (and thus end up getting repeated in the wrapped 222 # versions of derived classes). The code below basicallly 223 # suppresses inheritance by substituting in the base (null) 224 # versions of these methods unless a different version is 225 # explicitly supplied. 226 for method_name in ('export_methods', 'export_method_cxx_predecls', 227 'export_method_swig_predecls'): 228 if method_name not in cls.__dict__: 229 base_method = getattr(MetaSimObject, method_name) 230 m = MethodType(base_method, cls, MetaSimObject) 231 setattr(cls, method_name, m) 232 233 # Now process the _value_dict items. They could be defining 234 # new (or overriding existing) parameters or ports, setting 235 # class keywords (e.g., 'abstract'), or setting parameter 236 # values or port bindings. The first 3 can only be set when 237 # the class is defined, so we handle them here. The others 238 # can be set later too, so just emulate that by calling 239 # setattr(). 240 for key,val in cls._value_dict.items(): 241 # param descriptions 242 if isinstance(val, ParamDesc): 243 cls._new_param(key, val) 244 245 # port objects 246 elif isinstance(val, Port): 247 cls._new_port(key, val) 248 249 # init-time-only keywords 250 elif cls.init_keywords.has_key(key): 251 cls._set_keyword(key, val, cls.init_keywords[key]) 252 253 # default: use normal path (ends up in __setattr__) 254 else: 255 setattr(cls, key, val) 256 257 def _set_keyword(cls, keyword, val, kwtype): 258 if not isinstance(val, kwtype): 259 raise TypeError, 'keyword %s has bad type %s (expecting %s)' % \ 260 (keyword, type(val), kwtype) 261 if isinstance(val, FunctionType): 262 val = classmethod(val) 263 type.__setattr__(cls, keyword, val) 264 265 def _new_param(cls, name, pdesc): 266 # each param desc should be uniquely assigned to one variable 267 assert(not hasattr(pdesc, 'name')) 268 pdesc.name = name 269 cls._params[name] = pdesc 270 if hasattr(pdesc, 'default'): 271 cls._set_param(name, pdesc.default, pdesc) 272 273 def _set_param(cls, name, value, param): 274 assert(param.name == name) 275 try: 276 value = param.convert(value) 277 except Exception, e: 278 msg = "%s\nError setting param %s.%s to %s\n" % \ 279 (e, cls.__name__, name, value) 280 e.args = (msg, ) 281 raise 282 cls._values[name] = value 283 # if param value is a SimObject, make it a child too, so that 284 # it gets cloned properly when the class is instantiated 285 if isSimObjectOrVector(value) and not value.has_parent(): 286 cls._add_cls_child(name, value) 287 288 def _add_cls_child(cls, name, child): 289 # It's a little funky to have a class as a parent, but these 290 # objects should never be instantiated (only cloned, which 291 # clears the parent pointer), and this makes it clear that the 292 # object is not an orphan and can provide better error 293 # messages. 294 child.set_parent(cls, name) 295 cls._children[name] = child 296 297 def _new_port(cls, name, port): 298 # each port should be uniquely assigned to one variable 299 assert(not hasattr(port, 'name')) 300 port.name = name 301 cls._ports[name] = port 302 303 # same as _get_port_ref, effectively, but for classes 304 def _cls_get_port_ref(cls, attr): 305 # Return reference that can be assigned to another port 306 # via __setattr__. There is only ever one reference 307 # object per port, but we create them lazily here. 308 ref = cls._port_refs.get(attr) 309 if not ref: 310 ref = cls._ports[attr].makeRef(cls) 311 cls._port_refs[attr] = ref 312 return ref 313 314 # Set attribute (called on foo.attr = value when foo is an 315 # instance of class cls). 316 def __setattr__(cls, attr, value): 317 # normal processing for private attributes 318 if public_value(attr, value): 319 type.__setattr__(cls, attr, value) 320 return 321 322 if cls.keywords.has_key(attr): 323 cls._set_keyword(attr, value, cls.keywords[attr]) 324 return 325 326 if cls._ports.has_key(attr): 327 cls._cls_get_port_ref(attr).connect(value) 328 return 329 330 if isSimObjectOrSequence(value) and cls._instantiated: 331 raise RuntimeError, \ 332 "cannot set SimObject parameter '%s' after\n" \ 333 " class %s has been instantiated or subclassed" \ 334 % (attr, cls.__name__) 335 336 # check for param 337 param = cls._params.get(attr) 338 if param: 339 cls._set_param(attr, value, param) 340 return 341 342 if isSimObjectOrSequence(value): 343 # If RHS is a SimObject, it's an implicit child assignment. 344 cls._add_cls_child(attr, coerceSimObjectOrVector(value)) 345 return 346 347 # no valid assignment... raise exception 348 raise AttributeError, \ 349 "Class %s has no parameter \'%s\'" % (cls.__name__, attr) 350 351 def __getattr__(cls, attr): 352 if attr == 'cxx_class_path': 353 return cls.cxx_class.split('::') 354 355 if attr == 'cxx_class_name': 356 return cls.cxx_class_path[-1] 357 358 if attr == 'cxx_namespaces': 359 return cls.cxx_class_path[:-1] 360 361 if cls._values.has_key(attr): 362 return cls._values[attr] 363 364 if cls._children.has_key(attr): 365 return cls._children[attr] 366 367 raise AttributeError, \ 368 "object '%s' has no attribute '%s'" % (cls.__name__, attr) 369 370 def __str__(cls): 371 return cls.__name__ 372 373 # See ParamValue.cxx_predecls for description. 374 def cxx_predecls(cls, code): 375 code('#include "params/$cls.hh"') 376 377 # See ParamValue.swig_predecls for description. 378 def swig_predecls(cls, code): 379 code('%import "python/m5/internal/param_$cls.i"') 380 381 # Hook for exporting additional C++ methods to Python via SWIG. 382 # Default is none, override using @classmethod in class definition. 383 def export_methods(cls, code): 384 pass 385 386 # Generate the code needed as a prerequisite for the C++ methods 387 # exported via export_methods() to be compiled in the _wrap.cc 388 # file. Typically generates one or more #include statements. If 389 # any methods are exported, typically at least the C++ header 390 # declaring the relevant SimObject class must be included. 391 def export_method_cxx_predecls(cls, code): 392 pass 393 394 # Generate the code needed as a prerequisite for the C++ methods 395 # exported via export_methods() to be processed by SWIG. 396 # Typically generates one or more %include or %import statements. 397 # If any methods are exported, typically at least the C++ header 398 # declaring the relevant SimObject class must be included. 399 def export_method_swig_predecls(cls, code): 400 pass 401 402 # Generate the declaration for this object for wrapping with SWIG. 403 # Generates code that goes into a SWIG .i file. Called from 404 # src/SConscript. 405 def swig_decl(cls, code): 406 class_path = cls.cxx_class.split('::') 407 classname = class_path[-1] 408 namespaces = class_path[:-1] 409 410 # The 'local' attribute restricts us to the params declared in 411 # the object itself, not including inherited params (which 412 # will also be inherited from the base class's param struct 413 # here). 414 params = cls._params.local.values() 415 ports = cls._ports.local 416 417 code('%module(package="m5.internal") param_$cls') 418 code() 419 code('%{') 420 code('#include "sim/sim_object.hh"') 421 code('#include "params/$cls.hh"') 422 for param in params: 423 param.cxx_predecls(code) 424 code('#include "${{cls.cxx_header}}"') 425 cls.export_method_cxx_predecls(code) 426 code('''\ 427/** 428 * This is a workaround for bug in swig. Prior to gcc 4.6.1 the STL 429 * headers like vector, string, etc. used to automatically pull in 430 * the cstddef header but starting with gcc 4.6.1 they no longer do. 431 * This leads to swig generated a file that does not compile so we 432 * explicitly include cstddef. Additionally, including version 2.0.4, 433 * swig uses ptrdiff_t without the std:: namespace prefix which is 434 * required with gcc 4.6.1. We explicitly provide access to it. 435 */ 436#include <cstddef> 437using std::ptrdiff_t; 438''') 439 code('%}') 440 code() 441 442 for param in params: 443 param.swig_predecls(code) 444 cls.export_method_swig_predecls(code) 445 446 code() 447 if cls._base: 448 code('%import "python/m5/internal/param_${{cls._base}}.i"') 449 code() 450 451 for ns in namespaces: 452 code('namespace $ns {') 453 454 if namespaces: 455 code('// avoid name conflicts') 456 sep_string = '_COLONS_' 457 flat_name = sep_string.join(class_path) 458 code('%rename($flat_name) $classname;') 459 460 code() 461 code('// stop swig from creating/wrapping default ctor/dtor') 462 code('%nodefault $classname;') 463 code('class $classname') 464 if cls._base: 465 bases = [ cls._base.cxx_class ] + cls.cxx_bases 466 else: 467 bases = cls.cxx_bases 468 base_first = True 469 for base in bases: 470 if base_first: 471 code(' : public ${{base}}') 472 base_first = False 473 else: 474 code(' , public ${{base}}') 475 476 code('{') 477 code(' public:') 478 cls.export_methods(code) 479 code('};') 480 481 for ns in reversed(namespaces): 482 code('} // namespace $ns') 483 484 code() 485 code('%include "params/$cls.hh"') 486 487 488 # Generate the C++ declaration (.hh file) for this SimObject's 489 # param struct. Called from src/SConscript. 490 def cxx_param_decl(cls, code): 491 # The 'local' attribute restricts us to the params declared in 492 # the object itself, not including inherited params (which 493 # will also be inherited from the base class's param struct 494 # here). 495 params = cls._params.local.values() 496 ports = cls._ports.local 497 try: 498 ptypes = [p.ptype for p in params] 499 except: 500 print cls, p, p.ptype_str 501 print params 502 raise 503 504 class_path = cls._value_dict['cxx_class'].split('::') 505 506 code('''\ 507#ifndef __PARAMS__${cls}__ 508#define __PARAMS__${cls}__ 509 510''') 511 512 # A forward class declaration is sufficient since we are just 513 # declaring a pointer. 514 for ns in class_path[:-1]: 515 code('namespace $ns {') 516 code('class $0;', class_path[-1]) 517 for ns in reversed(class_path[:-1]): 518 code('} // namespace $ns') 519 code() 520 521 # The base SimObject has a couple of params that get 522 # automatically set from Python without being declared through 523 # the normal Param mechanism; we slip them in here (needed 524 # predecls now, actual declarations below) 525 if cls == SimObject: 526 code(''' 527#ifndef PY_VERSION 528struct PyObject; 529#endif 530 531#include <string> 532''') 533 for param in params: 534 param.cxx_predecls(code) 535 for port in ports.itervalues(): 536 port.cxx_predecls(code) 537 code() 538 539 if cls._base: 540 code('#include "params/${{cls._base.type}}.hh"') 541 code() 542 543 for ptype in ptypes: 544 if issubclass(ptype, Enum): 545 code('#include "enums/${{ptype.__name__}}.hh"') 546 code() 547 548 # now generate the actual param struct 549 code("struct ${cls}Params") 550 if cls._base: 551 code(" : public ${{cls._base.type}}Params") 552 code("{") 553 if not hasattr(cls, 'abstract') or not cls.abstract: 554 if 'type' in cls.__dict__: 555 code(" ${{cls.cxx_type}} create();") 556 557 code.indent() 558 if cls == SimObject: 559 code(''' 560 SimObjectParams() {} 561 virtual ~SimObjectParams() {} 562 563 std::string name; 564 PyObject *pyobj; 565 ''') 566 for param in params: 567 param.cxx_decl(code) 568 for port in ports.itervalues(): 569 port.cxx_decl(code) 570 571 code.dedent() 572 code('};') 573 574 code() 575 code('#endif // __PARAMS__${cls}__') 576 return code 577 578 579# This *temporary* definition is required to support calls from the 580# SimObject class definition to the MetaSimObject methods (in 581# particular _set_param, which gets called for parameters with default 582# values defined on the SimObject class itself). It will get 583# overridden by the permanent definition (which requires that 584# SimObject be defined) lower in this file. 585def isSimObjectOrVector(value): 586 return False 587 588# The SimObject class is the root of the special hierarchy. Most of 589# the code in this class deals with the configuration hierarchy itself 590# (parent/child node relationships). 591class SimObject(object): 592 # Specify metaclass. Any class inheriting from SimObject will 593 # get this metaclass. 594 __metaclass__ = MetaSimObject 595 type = 'SimObject' 596 abstract = True 597 598 cxx_header = "sim/sim_object.hh" 599 cxx_bases = [ "Drainable", "Serializable" ] 600 eventq_index = Param.UInt32(Parent.eventq_index, "Event Queue Index") 601 602 @classmethod 603 def export_method_swig_predecls(cls, code): 604 code(''' 605%include <std_string.i> 606 607%import "python/swig/drain.i" 608%import "python/swig/serialize.i" 609''') 610 611 @classmethod 612 def export_methods(cls, code): 613 code(''' 614 void init(); 615 void loadState(Checkpoint *cp); 616 void initState(); 617 void regStats(); 618 void resetStats(); 619 void regProbePoints(); 620 void regProbeListeners(); 621 void startup(); 622''') 623 624 # Initialize new instance. For objects with SimObject-valued 625 # children, we need to recursively clone the classes represented 626 # by those param values as well in a consistent "deep copy"-style 627 # fashion. That is, we want to make sure that each instance is 628 # cloned only once, and that if there are multiple references to 629 # the same original object, we end up with the corresponding 630 # cloned references all pointing to the same cloned instance. 631 def __init__(self, **kwargs): 632 ancestor = kwargs.get('_ancestor') 633 memo_dict = kwargs.get('_memo') 634 if memo_dict is None: 635 # prepare to memoize any recursively instantiated objects 636 memo_dict = {} 637 elif ancestor: 638 # memoize me now to avoid problems with recursive calls 639 memo_dict[ancestor] = self 640 641 if not ancestor: 642 ancestor = self.__class__ 643 ancestor._instantiated = True 644 645 # initialize required attributes 646 self._parent = None 647 self._name = None 648 self._ccObject = None # pointer to C++ object 649 self._ccParams = None 650 self._instantiated = False # really "cloned" 651 652 # Clone children specified at class level. No need for a 653 # multidict here since we will be cloning everything. 654 # Do children before parameter values so that children that 655 # are also param values get cloned properly. 656 self._children = {} 657 for key,val in ancestor._children.iteritems(): 658 self.add_child(key, val(_memo=memo_dict)) 659 660 # Inherit parameter values from class using multidict so 661 # individual value settings can be overridden but we still 662 # inherit late changes to non-overridden class values. 663 self._values = multidict(ancestor._values) 664 # clone SimObject-valued parameters 665 for key,val in ancestor._values.iteritems(): 666 val = tryAsSimObjectOrVector(val) 667 if val is not None: 668 self._values[key] = val(_memo=memo_dict) 669 670 # clone port references. no need to use a multidict here 671 # since we will be creating new references for all ports. 672 self._port_refs = {} 673 for key,val in ancestor._port_refs.iteritems(): 674 self._port_refs[key] = val.clone(self, memo_dict) 675 # apply attribute assignments from keyword args, if any 676 for key,val in kwargs.iteritems(): 677 setattr(self, key, val) 678 679 # "Clone" the current instance by creating another instance of 680 # this instance's class, but that inherits its parameter values 681 # and port mappings from the current instance. If we're in a 682 # "deep copy" recursive clone, check the _memo dict to see if 683 # we've already cloned this instance. 684 def __call__(self, **kwargs): 685 memo_dict = kwargs.get('_memo') 686 if memo_dict is None: 687 # no memo_dict: must be top-level clone operation. 688 # this is only allowed at the root of a hierarchy 689 if self._parent: 690 raise RuntimeError, "attempt to clone object %s " \ 691 "not at the root of a tree (parent = %s)" \ 692 % (self, self._parent) 693 # create a new dict and use that. 694 memo_dict = {} 695 kwargs['_memo'] = memo_dict 696 elif memo_dict.has_key(self): 697 # clone already done & memoized 698 return memo_dict[self] 699 return self.__class__(_ancestor = self, **kwargs) 700 701 def _get_port_ref(self, attr): 702 # Return reference that can be assigned to another port 703 # via __setattr__. There is only ever one reference 704 # object per port, but we create them lazily here. 705 ref = self._port_refs.get(attr) 706 if ref == None: 707 ref = self._ports[attr].makeRef(self) 708 self._port_refs[attr] = ref 709 return ref 710 711 def __getattr__(self, attr): 712 if self._ports.has_key(attr): 713 return self._get_port_ref(attr) 714 715 if self._values.has_key(attr): 716 return self._values[attr] 717 718 if self._children.has_key(attr): 719 return self._children[attr] 720 721 # If the attribute exists on the C++ object, transparently 722 # forward the reference there. This is typically used for 723 # SWIG-wrapped methods such as init(), regStats(), 724 # resetStats(), startup(), drain(), and 725 # resume(). 726 if self._ccObject and hasattr(self._ccObject, attr): 727 return getattr(self._ccObject, attr) 728 729 err_string = "object '%s' has no attribute '%s'" \ 730 % (self.__class__.__name__, attr) 731 732 if not self._ccObject: 733 err_string += "\n (C++ object is not yet constructed," \ 734 " so wrapped C++ methods are unavailable.)" 735 736 raise AttributeError, err_string 737 738 # Set attribute (called on foo.attr = value when foo is an 739 # instance of class cls). 740 def __setattr__(self, attr, value): 741 # normal processing for private attributes 742 if attr.startswith('_'): 743 object.__setattr__(self, attr, value) 744 return 745 746 if self._ports.has_key(attr): 747 # set up port connection 748 self._get_port_ref(attr).connect(value) 749 return 750 751 param = self._params.get(attr) 752 if param: 753 try: 754 value = param.convert(value) 755 except Exception, e: 756 msg = "%s\nError setting param %s.%s to %s\n" % \ 757 (e, self.__class__.__name__, attr, value) 758 e.args = (msg, ) 759 raise 760 self._values[attr] = value 761 # implicitly parent unparented objects assigned as params 762 if isSimObjectOrVector(value) and not value.has_parent(): 763 self.add_child(attr, value) 764 return 765 766 # if RHS is a SimObject, it's an implicit child assignment 767 if isSimObjectOrSequence(value): 768 self.add_child(attr, value) 769 return 770 771 # no valid assignment... raise exception 772 raise AttributeError, "Class %s has no parameter %s" \ 773 % (self.__class__.__name__, attr) 774 775 776 # this hack allows tacking a '[0]' onto parameters that may or may 777 # not be vectors, and always getting the first element (e.g. cpus) 778 def __getitem__(self, key): 779 if key == 0: 780 return self 781 raise TypeError, "Non-zero index '%s' to SimObject" % key 782 783 # Also implemented by SimObjectVector 784 def clear_parent(self, old_parent): 785 assert self._parent is old_parent 786 self._parent = None 787 788 # Also implemented by SimObjectVector 789 def set_parent(self, parent, name): 790 self._parent = parent 791 self._name = name 792 793 # Return parent object of this SimObject, not implemented by SimObjectVector 794 # because the elements in a SimObjectVector may not share the same parent 795 def get_parent(self): 796 return self._parent 797 798 # Also implemented by SimObjectVector 799 def get_name(self): 800 return self._name 801 802 # Also implemented by SimObjectVector 803 def has_parent(self): 804 return self._parent is not None 805 806 # clear out child with given name. This code is not likely to be exercised. 807 # See comment in add_child. 808 def clear_child(self, name): 809 child = self._children[name] 810 child.clear_parent(self) 811 del self._children[name] 812 813 # Add a new child to this object. 814 def add_child(self, name, child): 815 child = coerceSimObjectOrVector(child) 816 if child.has_parent(): 817 warn("add_child('%s'): child '%s' already has parent", name, 818 child.get_name()) 819 if self._children.has_key(name): 820 # This code path had an undiscovered bug that would make it fail 821 # at runtime. It had been here for a long time and was only 822 # exposed by a buggy script. Changes here will probably not be 823 # exercised without specialized testing. 824 self.clear_child(name) 825 child.set_parent(self, name) 826 self._children[name] = child 827 828 # Take SimObject-valued parameters that haven't been explicitly 829 # assigned as children and make them children of the object that 830 # they were assigned to as a parameter value. This guarantees 831 # that when we instantiate all the parameter objects we're still 832 # inside the configuration hierarchy. 833 def adoptOrphanParams(self): 834 for key,val in self._values.iteritems(): 835 if not isSimObjectVector(val) and isSimObjectSequence(val): 836 # need to convert raw SimObject sequences to 837 # SimObjectVector class so we can call has_parent() 838 val = SimObjectVector(val) 839 self._values[key] = val 840 if isSimObjectOrVector(val) and not val.has_parent(): 841 warn("%s adopting orphan SimObject param '%s'", self, key) 842 self.add_child(key, val) 843 844 def path(self): 845 if not self._parent: 846 return '<orphan %s>' % self.__class__ 847 ppath = self._parent.path() 848 if ppath == 'root': 849 return self._name 850 return ppath + "." + self._name 851 852 def __str__(self): 853 return self.path() 854 855 def ini_str(self): 856 return self.path() 857 858 def find_any(self, ptype): 859 if isinstance(self, ptype): 860 return self, True 861 862 found_obj = None 863 for child in self._children.itervalues(): 864 visited = False 865 if hasattr(child, '_visited'): 866 visited = getattr(child, '_visited') 867 868 if isinstance(child, ptype) and not visited: 869 if found_obj != None and child != found_obj: 870 raise AttributeError, \ 871 'parent.any matched more than one: %s %s' % \ 872 (found_obj.path, child.path) 873 found_obj = child 874 # search param space 875 for pname,pdesc in self._params.iteritems(): 876 if issubclass(pdesc.ptype, ptype): 877 match_obj = self._values[pname] 878 if found_obj != None and found_obj != match_obj: 879 raise AttributeError, \ 880 'parent.any matched more than one: %s and %s' % (found_obj.path, match_obj.path) 881 found_obj = match_obj 882 return found_obj, found_obj != None 883 884 def find_all(self, ptype): 885 all = {} 886 # search children 887 for child in self._children.itervalues(): 888 # a child could be a list, so ensure we visit each item 889 if isinstance(child, list): 890 children = child 891 else: 892 children = [child] 893 894 for child in children: 895 if isinstance(child, ptype) and not isproxy(child) and \ 896 not isNullPointer(child): 897 all[child] = True 898 if isSimObject(child): 899 # also add results from the child itself 900 child_all, done = child.find_all(ptype) 901 all.update(dict(zip(child_all, [done] * len(child_all)))) 902 # search param space 903 for pname,pdesc in self._params.iteritems(): 904 if issubclass(pdesc.ptype, ptype): 905 match_obj = self._values[pname] 906 if not isproxy(match_obj) and not isNullPointer(match_obj): 907 all[match_obj] = True 908 return all.keys(), True 909 910 def unproxy(self, base): 911 return self 912 913 def unproxyParams(self): 914 for param in self._params.iterkeys(): 915 value = self._values.get(param) 916 if value != None and isproxy(value): 917 try: 918 value = value.unproxy(self) 919 except: 920 print "Error in unproxying param '%s' of %s" % \ 921 (param, self.path()) 922 raise 923 setattr(self, param, value) 924 925 # Unproxy ports in sorted order so that 'append' operations on 926 # vector ports are done in a deterministic fashion. 927 port_names = self._ports.keys() 928 port_names.sort() 929 for port_name in port_names: 930 port = self._port_refs.get(port_name) 931 if port != None: 932 port.unproxy(self) 933 934 def print_ini(self, ini_file): 935 print >>ini_file, '[' + self.path() + ']' # .ini section header 936 937 instanceDict[self.path()] = self 938 939 if hasattr(self, 'type'): 940 print >>ini_file, 'type=%s' % self.type 941 942 if len(self._children.keys()): 943 print >>ini_file, 'children=%s' % \ 944 ' '.join(self._children[n].get_name() \ 945 for n in sorted(self._children.keys())) 946 947 for param in sorted(self._params.keys()): 948 value = self._values.get(param) 949 if value != None: 950 print >>ini_file, '%s=%s' % (param, 951 self._values[param].ini_str()) 952 953 for port_name in sorted(self._ports.keys()): 954 port = self._port_refs.get(port_name, None) 955 if port != None: 956 print >>ini_file, '%s=%s' % (port_name, port.ini_str()) 957 958 print >>ini_file # blank line between objects 959 960 # generate a tree of dictionaries expressing all the parameters in the 961 # instantiated system for use by scripts that want to do power, thermal 962 # visualization, and other similar tasks 963 def get_config_as_dict(self): 964 d = attrdict() 965 if hasattr(self, 'type'): 966 d.type = self.type 967 if hasattr(self, 'cxx_class'): 968 d.cxx_class = self.cxx_class 969 # Add the name and path of this object to be able to link to 970 # the stats 971 d.name = self.get_name() 972 d.path = self.path() 973 974 for param in sorted(self._params.keys()): 975 value = self._values.get(param) 976 if value != None: 977 try: 978 # Use native type for those supported by JSON and 979 # strings for everything else. skipkeys=True seems 980 # to not work as well as one would hope 981 if type(self._values[param].value) in \ 982 [str, unicode, int, long, float, bool, None]: 983 d[param] = self._values[param].value 984 else: 985 d[param] = str(self._values[param]) 986 987 except AttributeError: 988 pass 989 990 for n in sorted(self._children.keys()): 991 child = self._children[n] 992 # Use the name of the attribute (and not get_name()) as 993 # the key in the JSON dictionary to capture the hierarchy 994 # in the Python code that assembled this system 995 d[n] = child.get_config_as_dict() 996 997 for port_name in sorted(self._ports.keys()): 998 port = self._port_refs.get(port_name, None) 999 if port != None: 1000 # Represent each port with a dictionary containing the 1001 # prominent attributes 1002 d[port_name] = port.get_config_as_dict() 1003 1004 return d 1005 1006 def getCCParams(self): 1007 if self._ccParams: 1008 return self._ccParams 1009 1010 cc_params_struct = getattr(m5.internal.params, '%sParams' % self.type) 1011 cc_params = cc_params_struct() 1012 cc_params.pyobj = self 1013 cc_params.name = str(self) 1014 1015 param_names = self._params.keys() 1016 param_names.sort() 1017 for param in param_names: 1018 value = self._values.get(param) 1019 if value is None: 1020 fatal("%s.%s without default or user set value", 1021 self.path(), param) 1022 1023 value = value.getValue() 1024 if isinstance(self._params[param], VectorParamDesc): 1025 assert isinstance(value, list) 1026 vec = getattr(cc_params, param) 1027 assert not len(vec) 1028 for v in value: 1029 vec.append(v) 1030 else: 1031 setattr(cc_params, param, value) 1032 1033 port_names = self._ports.keys() 1034 port_names.sort() 1035 for port_name in port_names: 1036 port = self._port_refs.get(port_name, None) 1037 if port != None: 1038 port_count = len(port) 1039 else: 1040 port_count = 0 1041 setattr(cc_params, 'port_' + port_name + '_connection_count', 1042 port_count) 1043 self._ccParams = cc_params 1044 return self._ccParams 1045 1046 # Get C++ object corresponding to this object, calling C++ if 1047 # necessary to construct it. Does *not* recursively create 1048 # children. 1049 def getCCObject(self): 1050 if not self._ccObject: 1051 # Make sure this object is in the configuration hierarchy 1052 if not self._parent and not isRoot(self): 1053 raise RuntimeError, "Attempt to instantiate orphan node" 1054 # Cycles in the configuration hierarchy are not supported. This 1055 # will catch the resulting recursion and stop. 1056 self._ccObject = -1 1057 params = self.getCCParams() 1058 self._ccObject = params.create() 1059 elif self._ccObject == -1: 1060 raise RuntimeError, "%s: Cycle found in configuration hierarchy." \ 1061 % self.path() 1062 return self._ccObject 1063 1064 def descendants(self): 1065 yield self 1066 for child in self._children.itervalues(): 1067 for obj in child.descendants(): 1068 yield obj 1069 1070 # Call C++ to create C++ object corresponding to this object 1071 def createCCObject(self): 1072 self.getCCParams() 1073 self.getCCObject() # force creation 1074 1075 def getValue(self): 1076 return self.getCCObject() 1077 1078 # Create C++ port connections corresponding to the connections in 1079 # _port_refs 1080 def connectPorts(self): 1081 for portRef in self._port_refs.itervalues(): 1082 portRef.ccConnect() 1083 1084# Function to provide to C++ so it can look up instances based on paths 1085def resolveSimObject(name): 1086 obj = instanceDict[name] 1087 return obj.getCCObject() 1088 1089def isSimObject(value): 1090 return isinstance(value, SimObject) 1091 1092def isSimObjectClass(value): 1093 return issubclass(value, SimObject) 1094 1095def isSimObjectVector(value): 1096 return isinstance(value, SimObjectVector) 1097 1098def isSimObjectSequence(value): 1099 if not isinstance(value, (list, tuple)) or len(value) == 0: 1100 return False 1101 1102 for val in value: 1103 if not isNullPointer(val) and not isSimObject(val): 1104 return False 1105 1106 return True 1107 1108def isSimObjectOrSequence(value): 1109 return isSimObject(value) or isSimObjectSequence(value) 1110 1111def isRoot(obj): 1112 from m5.objects import Root 1113 return obj and obj is Root.getInstance() 1114 1115def isSimObjectOrVector(value): 1116 return isSimObject(value) or isSimObjectVector(value) 1117 1118def tryAsSimObjectOrVector(value): 1119 if isSimObjectOrVector(value): 1120 return value 1121 if isSimObjectSequence(value): 1122 return SimObjectVector(value) 1123 return None 1124 1125def coerceSimObjectOrVector(value): 1126 value = tryAsSimObjectOrVector(value) 1127 if value is None: 1128 raise TypeError, "SimObject or SimObjectVector expected" 1129 return value 1130 1131baseClasses = allClasses.copy() 1132baseInstances = instanceDict.copy() 1133 1134def clear(): 1135 global allClasses, instanceDict, noCxxHeader 1136 1137 allClasses = baseClasses.copy() 1138 instanceDict = baseInstances.copy() 1139 noCxxHeader = False 1140 1141# __all__ defines the list of symbols that get exported when 1142# 'from config import *' is invoked. Try to keep this reasonably 1143# short to avoid polluting other namespaces. 1144__all__ = [ 'SimObject' ] 1145