1 # Copyright (c) 2017 ARM Limited
4 # The license below extends only to copyright in the software and shall
5 # not be construed as granting a license to any other intellectual
6 # property including but not limited to intellectual property relating
7 # to a hardware implementation of the functionality of the software
8 # licensed hereunder. You may use the software subject to the license
9 # terms below provided that you ensure that this notice is replicated
10 # unmodified and in its entirety in all distributions of the software,
11 # modified or unmodified, in source code or in binary form.
13 # Copyright (c) 2004-2006 The Regents of The University of Michigan
14 # Copyright (c) 2010-20013 Advanced Micro Devices, Inc.
15 # Copyright (c) 2013 Mark D. Hill and David A. Wood
16 # All rights reserved.
18 # Redistribution and use in source and binary forms, with or without
19 # modification, are permitted provided that the following conditions are
20 # met: redistributions of source code must retain the above copyright
21 # notice, this list of conditions and the following disclaimer;
22 # redistributions in binary form must reproduce the above copyright
23 # notice, this list of conditions and the following disclaimer in the
24 # documentation and/or other materials provided with the distribution;
25 # neither the name of the copyright holders nor the names of its
26 # contributors may be used to endorse or promote products derived from
27 # this software without specific prior written permission.
29 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 # Authors: Steve Reinhardt
47 from types
import FunctionType
, MethodType
, ModuleType
48 from functools
import wraps
53 from m5
.util
.pybind
import *
55 # Have to import params up top since Param is referenced on initial
56 # load (when SimObject class references Param to create a class
57 # variable, the 'name' param)...
58 from m5
.params
import *
59 # There are a few things we need that aren't in params.__all__ since
60 # normal users don't need them
61 from m5
.params
import ParamDesc
, VectorParamDesc
, \
62 isNullPointer
, SimObjectVector
, Port
64 from m5
.proxy
import *
65 from m5
.proxy
import isproxy
67 #####################################################################
69 # M5 Python Configuration Utility
71 # The basic idea is to write simple Python programs that build Python
72 # objects corresponding to M5 SimObjects for the desired simulation
73 # configuration. For now, the Python emits a .ini file that can be
74 # parsed by M5. In the future, some tighter integration between M5
75 # and the Python interpreter may allow bypassing the .ini file.
77 # Each SimObject class in M5 is represented by a Python class with the
78 # same name. The Python inheritance tree mirrors the M5 C++ tree
79 # (e.g., SimpleCPU derives from BaseCPU in both cases, and all
80 # SimObjects inherit from a single SimObject base class). To specify
81 # an instance of an M5 SimObject in a configuration, the user simply
82 # instantiates the corresponding Python object. The parameters for
83 # that SimObject are given by assigning to attributes of the Python
84 # object, either using keyword assignment in the constructor or in
85 # separate assignment statements. For example:
87 # cache = BaseCache(size='64KB')
88 # cache.hit_latency = 3
91 # The magic lies in the mapping of the Python attributes for SimObject
92 # classes to the actual SimObject parameter specifications. This
93 # allows parameter validity checking in the Python code. Continuing
94 # the example above, the statements "cache.blurfl=3" or
95 # "cache.assoc='hello'" would both result in runtime errors in Python,
96 # since the BaseCache object has no 'blurfl' parameter and the 'assoc'
97 # parameter requires an integer, respectively. This magic is done
98 # primarily by overriding the special __setattr__ method that controls
99 # assignment to object attributes.
101 # Once a set of Python objects have been instantiated in a hierarchy,
102 # calling 'instantiate(obj)' (where obj is the root of the hierarchy)
103 # will generate a .ini file.
105 #####################################################################
107 # list of all SimObject classes
110 # dict to look up SimObjects based on path
113 # Did any of the SimObjects lack a header file?
116 def public_value(key
, value
):
117 return key
.startswith('_') or \
118 isinstance(value
, (FunctionType
, MethodType
, ModuleType
,
121 def createCxxConfigDirectoryEntryFile(code
, name
, simobj
, is_header
):
122 entry_class
= 'CxxConfigDirectoryEntry_%s' % name
123 param_class
= '%sCxxConfigParams' % name
125 code('#include "params/%s.hh"' % name
)
128 for param
in simobj
._params
.values():
129 if isSimObjectClass(param
.ptype
):
130 code('#include "%s"' % param
.ptype
._value
_dict
['cxx_header'])
131 code('#include "params/%s.hh"' % param
.ptype
.__name
__)
133 param
.ptype
.cxx_ini_predecls(code
)
138 code('#include "sim/cxx_config.hh"')
140 code('class ${param_class} : public CxxConfigParams,'
141 ' public ${name}Params')
145 code('class DirectoryEntry : public CxxConfigDirectoryEntry')
149 code('DirectoryEntry();');
151 code('CxxConfigParams *makeParamsObject() const')
152 code('{ return new ${param_class}; }')
160 member_prefix
= '%s::' % param_class
162 code('#include "%s"' % simobj
._value
_dict
['cxx_header'])
163 code('#include "base/str.hh"')
164 code('#include "cxx_config/${name}.hh"')
166 if simobj
._ports
.values() != []:
167 code('#include "mem/mem_object.hh"')
168 code('#include "mem/port.hh"')
171 code('${member_prefix}DirectoryEntry::DirectoryEntry()');
175 return 'true' if b
else 'false'
178 for param
in simobj
._params
.values():
179 is_vector
= isinstance(param
, m5
.params
.VectorParamDesc
)
180 is_simobj
= issubclass(param
.ptype
, m5
.SimObject
.SimObject
)
182 code('parameters["%s"] = new ParamDesc("%s", %s, %s);' %
183 (param
.name
, param
.name
, cxx_bool(is_vector
),
184 cxx_bool(is_simobj
)));
186 for port
in simobj
._ports
.values():
187 is_vector
= isinstance(port
, m5
.params
.VectorPort
)
188 is_master
= port
.role
== 'MASTER'
190 code('ports["%s"] = new PortDesc("%s", %s, %s);' %
191 (port
.name
, port
.name
, cxx_bool(is_vector
),
192 cxx_bool(is_master
)))
198 code('bool ${member_prefix}setSimObject(const std::string &name,')
199 code(' SimObject *simObject)${end_of_decl}')
204 code('bool ret = true;')
207 for param
in simobj
._params
.values():
208 is_vector
= isinstance(param
, m5
.params
.VectorParamDesc
)
209 is_simobj
= issubclass(param
.ptype
, m5
.SimObject
.SimObject
)
211 if is_simobj
and not is_vector
:
212 code('} else if (name == "${{param.name}}") {')
214 code('this->${{param.name}} = '
215 'dynamic_cast<${{param.ptype.cxx_type}}>(simObject);')
216 code('if (simObject && !this->${{param.name}})')
217 code(' ret = false;')
220 code(' ret = false;')
228 code('bool ${member_prefix}setSimObjectVector('
229 'const std::string &name,')
230 code(' const std::vector<SimObject *> &simObjects)${end_of_decl}')
235 code('bool ret = true;')
238 for param
in simobj
._params
.values():
239 is_vector
= isinstance(param
, m5
.params
.VectorParamDesc
)
240 is_simobj
= issubclass(param
.ptype
, m5
.SimObject
.SimObject
)
242 if is_simobj
and is_vector
:
243 code('} else if (name == "${{param.name}}") {')
245 code('this->${{param.name}}.clear();')
246 code('for (auto i = simObjects.begin(); '
247 'ret && i != simObjects.end(); i ++)')
250 code('${{param.ptype.cxx_type}} object = '
251 'dynamic_cast<${{param.ptype.cxx_type}}>(*i);')
252 code('if (*i && !object)')
253 code(' ret = false;')
255 code(' this->${{param.name}}.push_back(object);')
260 code(' ret = false;')
268 code('void ${member_prefix}setName(const std::string &name_)'
274 code('this->name = name_;')
279 code('const std::string &${member_prefix}getName()')
280 code('{ return this->name; }')
283 code('bool ${member_prefix}setParam(const std::string &name,')
284 code(' const std::string &value, const Flags flags)${end_of_decl}')
289 code('bool ret = true;')
292 for param
in simobj
._params
.values():
293 is_vector
= isinstance(param
, m5
.params
.VectorParamDesc
)
294 is_simobj
= issubclass(param
.ptype
, m5
.SimObject
.SimObject
)
296 if not is_simobj
and not is_vector
:
297 code('} else if (name == "${{param.name}}") {')
299 param
.ptype
.cxx_ini_parse(code
,
300 'value', 'this->%s' % param
.name
, 'ret =')
303 code(' ret = false;')
311 code('bool ${member_prefix}setParamVector('
312 'const std::string &name,')
313 code(' const std::vector<std::string> &values,')
314 code(' const Flags flags)${end_of_decl}')
319 code('bool ret = true;')
322 for param
in simobj
._params
.values():
323 is_vector
= isinstance(param
, m5
.params
.VectorParamDesc
)
324 is_simobj
= issubclass(param
.ptype
, m5
.SimObject
.SimObject
)
326 if not is_simobj
and is_vector
:
327 code('} else if (name == "${{param.name}}") {')
329 code('${{param.name}}.clear();')
330 code('for (auto i = values.begin(); '
331 'ret && i != values.end(); i ++)')
334 code('${{param.ptype.cxx_type}} elem;')
335 param
.ptype
.cxx_ini_parse(code
,
336 '*i', 'elem', 'ret =')
338 code(' this->${{param.name}}.push_back(elem);')
343 code(' ret = false;')
351 code('bool ${member_prefix}setPortConnectionCount('
352 'const std::string &name,')
353 code(' unsigned int count)${end_of_decl}')
358 code('bool ret = true;')
362 for port
in simobj
._ports
.values():
363 code('else if (name == "${{port.name}}")')
364 code(' this->port_${{port.name}}_connection_count = count;')
366 code(' ret = false;')
373 code('SimObject *${member_prefix}simObjectCreate()${end_of_decl}')
377 if hasattr(simobj
, 'abstract') and simobj
.abstract
:
378 code(' return NULL;')
380 code(' return this->create();')
385 code('static CxxConfigDirectoryEntry'
386 ' *${member_prefix}makeDirectoryEntry()')
387 code('{ return new DirectoryEntry; }')
393 # The metaclass for SimObject. This class controls how new classes
394 # that derive from SimObject are instantiated, and provides inherited
395 # class behavior (just like a class controls how instances of that
396 # class are instantiated, and provides inherited instance behavior).
397 class MetaSimObject(type):
398 # Attributes that can be set only at initialization time
406 'cxx_exports' : list,
407 'cxx_param_exports' : list,
409 # Attributes that can be set any time
410 keywords
= { 'check' : FunctionType
}
412 # __new__ is called before __init__, and is where the statements
413 # in the body of the class definition get loaded into the class's
414 # __dict__. We intercept this to filter out parameter & port assignments
415 # and only allow "private" attributes to be passed to the base
416 # __new__ (starting with underscore).
417 def __new__(mcls
, name
, bases
, dict):
418 assert name
not in allClasses
, "SimObject %s already present" % name
420 # Copy "private" attributes, functions, and classes to the
421 # official dict. Everything else goes in _init_dict to be
422 # filtered in __init__.
426 for key
,val
in dict.items():
428 cxx_exports
.append(getattr(val
, "__pybind"))
429 except AttributeError:
432 if public_value(key
, val
):
435 # must be a param/port setting
436 value_dict
[key
] = val
437 if 'abstract' not in value_dict
:
438 value_dict
['abstract'] = False
439 if 'cxx_bases' not in value_dict
:
440 value_dict
['cxx_bases'] = []
441 if 'cxx_exports' not in value_dict
:
442 value_dict
['cxx_exports'] = cxx_exports
444 value_dict
['cxx_exports'] += cxx_exports
445 if 'cxx_param_exports' not in value_dict
:
446 value_dict
['cxx_param_exports'] = []
447 cls_dict
['_value_dict'] = value_dict
448 cls
= super(MetaSimObject
, mcls
).__new
__(mcls
, name
, bases
, cls_dict
)
449 if 'type' in value_dict
:
450 allClasses
[name
] = cls
453 # subclass initialization
454 def __init__(cls
, name
, bases
, dict):
455 # calls type.__init__()... I think that's a no-op, but leave
456 # it here just in case it's not.
457 super(MetaSimObject
, cls
).__init
__(name
, bases
, dict)
459 # initialize required attributes
461 # class-only attributes
462 cls
._params
= multidict() # param descriptions
463 cls
._ports
= multidict() # port descriptions
465 # class or instance attributes
466 cls
._values
= multidict() # param values
467 cls
._hr
_values
= multidict() # human readable param values
468 cls
._children
= multidict() # SimObject children
469 cls
._port
_refs
= multidict() # port ref objects
470 cls
._instantiated
= False # really instantiated, cloned, or subclassed
472 # We don't support multiple inheritance of sim objects. If you want
473 # to, you must fix multidict to deal with it properly. Non sim-objects
477 if isinstance(c
, MetaSimObject
):
481 "SimObjects do not support multiple inheritance"
485 # Set up general inheritance via multidicts. A subclass will
486 # inherit all its settings from the base class. The only time
487 # the following is not true is when we define the SimObject
488 # class itself (in which case the multidicts have no parent).
489 if isinstance(base
, MetaSimObject
):
491 cls
._params
.parent
= base
._params
492 cls
._ports
.parent
= base
._ports
493 cls
._values
.parent
= base
._values
494 cls
._hr
_values
.parent
= base
._hr
_values
495 cls
._children
.parent
= base
._children
496 cls
._port
_refs
.parent
= base
._port
_refs
497 # mark base as having been subclassed
498 base
._instantiated
= True
502 # default keyword values
503 if 'type' in cls
._value
_dict
:
504 if 'cxx_class' not in cls
._value
_dict
:
505 cls
._value
_dict
['cxx_class'] = cls
._value
_dict
['type']
507 cls
._value
_dict
['cxx_type'] = '%s *' % cls
._value
_dict
['cxx_class']
509 if 'cxx_header' not in cls
._value
_dict
:
512 warn("No header file specified for SimObject: %s", name
)
514 # Now process the _value_dict items. They could be defining
515 # new (or overriding existing) parameters or ports, setting
516 # class keywords (e.g., 'abstract'), or setting parameter
517 # values or port bindings. The first 3 can only be set when
518 # the class is defined, so we handle them here. The others
519 # can be set later too, so just emulate that by calling
521 for key
,val
in cls
._value
_dict
.items():
523 if isinstance(val
, ParamDesc
):
524 cls
._new
_param
(key
, val
)
527 elif isinstance(val
, Port
):
528 cls
._new
_port
(key
, val
)
530 # init-time-only keywords
531 elif cls
.init_keywords
.has_key(key
):
532 cls
._set
_keyword
(key
, val
, cls
.init_keywords
[key
])
534 # default: use normal path (ends up in __setattr__)
536 setattr(cls
, key
, val
)
538 def _set_keyword(cls
, keyword
, val
, kwtype
):
539 if not isinstance(val
, kwtype
):
540 raise TypeError, 'keyword %s has bad type %s (expecting %s)' % \
541 (keyword
, type(val
), kwtype
)
542 if isinstance(val
, FunctionType
):
543 val
= classmethod(val
)
544 type.__setattr
__(cls
, keyword
, val
)
546 def _new_param(cls
, name
, pdesc
):
547 # each param desc should be uniquely assigned to one variable
548 assert(not hasattr(pdesc
, 'name'))
550 cls
._params
[name
] = pdesc
551 if hasattr(pdesc
, 'default'):
552 cls
._set
_param
(name
, pdesc
.default
, pdesc
)
554 def _set_param(cls
, name
, value
, param
):
555 assert(param
.name
== name
)
558 value
= param
.convert(value
)
560 msg
= "%s\nError setting param %s.%s to %s\n" % \
561 (e
, cls
.__name
__, name
, value
)
564 cls
._values
[name
] = value
565 # if param value is a SimObject, make it a child too, so that
566 # it gets cloned properly when the class is instantiated
567 if isSimObjectOrVector(value
) and not value
.has_parent():
568 cls
._add
_cls
_child
(name
, value
)
569 # update human-readable values of the param if it has a literal
570 # value and is not an object or proxy.
571 if not (isSimObjectOrVector(value
) or\
572 isinstance(value
, m5
.proxy
.BaseProxy
)):
573 cls
._hr
_values
[name
] = hr_value
575 def _add_cls_child(cls
, name
, child
):
576 # It's a little funky to have a class as a parent, but these
577 # objects should never be instantiated (only cloned, which
578 # clears the parent pointer), and this makes it clear that the
579 # object is not an orphan and can provide better error
581 child
.set_parent(cls
, name
)
582 cls
._children
[name
] = child
584 def _new_port(cls
, name
, port
):
585 # each port should be uniquely assigned to one variable
586 assert(not hasattr(port
, 'name'))
588 cls
._ports
[name
] = port
590 # same as _get_port_ref, effectively, but for classes
591 def _cls_get_port_ref(cls
, attr
):
592 # Return reference that can be assigned to another port
593 # via __setattr__. There is only ever one reference
594 # object per port, but we create them lazily here.
595 ref
= cls
._port
_refs
.get(attr
)
597 ref
= cls
._ports
[attr
].makeRef(cls
)
598 cls
._port
_refs
[attr
] = ref
601 # Set attribute (called on foo.attr = value when foo is an
602 # instance of class cls).
603 def __setattr__(cls
, attr
, value
):
604 # normal processing for private attributes
605 if public_value(attr
, value
):
606 type.__setattr
__(cls
, attr
, value
)
609 if cls
.keywords
.has_key(attr
):
610 cls
._set
_keyword
(attr
, value
, cls
.keywords
[attr
])
613 if cls
._ports
.has_key(attr
):
614 cls
._cls
_get
_port
_ref
(attr
).connect(value
)
617 if isSimObjectOrSequence(value
) and cls
._instantiated
:
618 raise RuntimeError, \
619 "cannot set SimObject parameter '%s' after\n" \
620 " class %s has been instantiated or subclassed" \
621 % (attr
, cls
.__name
__)
624 param
= cls
._params
.get(attr
)
626 cls
._set
_param
(attr
, value
, param
)
629 if isSimObjectOrSequence(value
):
630 # If RHS is a SimObject, it's an implicit child assignment.
631 cls
._add
_cls
_child
(attr
, coerceSimObjectOrVector(value
))
634 # no valid assignment... raise exception
635 raise AttributeError, \
636 "Class %s has no parameter \'%s\'" % (cls
.__name
__, attr
)
638 def __getattr__(cls
, attr
):
639 if attr
== 'cxx_class_path':
640 return cls
.cxx_class
.split('::')
642 if attr
== 'cxx_class_name':
643 return cls
.cxx_class_path
[-1]
645 if attr
== 'cxx_namespaces':
646 return cls
.cxx_class_path
[:-1]
648 if cls
._values
.has_key(attr
):
649 return cls
._values
[attr
]
651 if cls
._children
.has_key(attr
):
652 return cls
._children
[attr
]
654 raise AttributeError, \
655 "object '%s' has no attribute '%s'" % (cls
.__name
__, attr
)
660 # See ParamValue.cxx_predecls for description.
661 def cxx_predecls(cls
, code
):
662 code('#include "params/$cls.hh"')
664 def pybind_predecls(cls
, code
):
665 code('#include "${{cls.cxx_header}}"')
667 def pybind_decl(cls
, code
):
668 class_path
= cls
.cxx_class
.split('::')
669 namespaces
, classname
= class_path
[:-1], class_path
[-1]
670 py_class_name
= '_COLONS_'.join(class_path
) if namespaces
else \
673 # The 'local' attribute restricts us to the params declared in
674 # the object itself, not including inherited params (which
675 # will also be inherited from the base class's param struct
676 # here). Sort the params based on their key
677 params
= map(lambda (k
, v
): v
, sorted(cls
._params
.local
.items()))
678 ports
= cls
._ports
.local
680 code('''#include "pybind11/pybind11.h"
681 #include "pybind11/stl.h"
683 #include "params/$cls.hh"
684 #include "python/pybind11/core.hh"
685 #include "sim/init.hh"
686 #include "sim/sim_object.hh"
688 #include "${{cls.cxx_header}}"
693 param
.pybind_predecls(code
)
695 code('''namespace py = pybind11;
698 module_init(py::module &m_internal)
700 py::module m = m_internal.def_submodule("param_${cls}");
704 code('py::class_<${cls}Params, ${{cls._base.type}}Params>(m, ' \
707 code('py::class_<${cls}Params>(m, "${cls}Params")')
710 if not hasattr(cls
, 'abstract') or not cls
.abstract
:
711 code('.def(py::init<>())')
712 code('.def("create", &${cls}Params::create)')
714 param_exports
= cls
.cxx_param_exports
+ [
716 for k
, v
in sorted(cls
._params
.local
.items())
718 PyBindProperty("port_%s_connection_count" % port
.name
)
719 for port
in ports
.itervalues()
721 for exp
in param_exports
:
722 exp
.export(code
, "%sParams" % cls
)
728 bases
= [ cls
._base
.cxx_class
] + cls
.cxx_bases
if cls
._base
else \
731 base_str
= ", ".join(bases
)
732 code('py::class_<${{cls.cxx_class}}, ${base_str}>(m, ' \
733 '"${py_class_name}")')
735 code('py::class_<${{cls.cxx_class}}>(m, "${py_class_name}")')
737 for exp
in cls
.cxx_exports
:
738 exp
.export(code
, cls
.cxx_class
)
745 code('static EmbeddedPyBind embed_obj("${0}", module_init, "${1}");',
746 cls
, cls
._base
.type if cls
._base
else "")
749 # Generate the C++ declaration (.hh file) for this SimObject's
750 # param struct. Called from src/SConscript.
751 def cxx_param_decl(cls
, code
):
752 # The 'local' attribute restricts us to the params declared in
753 # the object itself, not including inherited params (which
754 # will also be inherited from the base class's param struct
755 # here). Sort the params based on their key
756 params
= map(lambda (k
, v
): v
, sorted(cls
._params
.local
.items()))
757 ports
= cls
._ports
.local
759 ptypes
= [p
.ptype
for p
in params
]
761 print cls
, p
, p
.ptype_str
765 class_path
= cls
._value
_dict
['cxx_class'].split('::')
768 #ifndef __PARAMS__${cls}__
769 #define __PARAMS__${cls}__
774 # The base SimObject has a couple of params that get
775 # automatically set from Python without being declared through
776 # the normal Param mechanism; we slip them in here (needed
777 # predecls now, actual declarations below)
779 code('''#include <string>''')
781 # A forward class declaration is sufficient since we are just
782 # declaring a pointer.
783 for ns
in class_path
[:-1]:
784 code('namespace $ns {')
785 code('class $0;', class_path
[-1])
786 for ns
in reversed(class_path
[:-1]):
787 code('} // namespace $ns')
791 param
.cxx_predecls(code
)
792 for port
in ports
.itervalues():
793 port
.cxx_predecls(code
)
797 code('#include "params/${{cls._base.type}}.hh"')
801 if issubclass(ptype
, Enum
):
802 code('#include "enums/${{ptype.__name__}}.hh"')
805 # now generate the actual param struct
806 code("struct ${cls}Params")
808 code(" : public ${{cls._base.type}}Params")
810 if not hasattr(cls
, 'abstract') or not cls
.abstract
:
811 if 'type' in cls
.__dict
__:
812 code(" ${{cls.cxx_type}} create();")
818 virtual ~SimObjectParams() {}
825 for port
in ports
.itervalues():
832 code('#endif // __PARAMS__${cls}__')
835 # Generate the C++ declaration/definition files for this SimObject's
836 # param struct to allow C++ initialisation
837 def cxx_config_param_file(cls
, code
, is_header
):
838 createCxxConfigDirectoryEntryFile(code
, cls
.__name
__, cls
, is_header
)
841 # This *temporary* definition is required to support calls from the
842 # SimObject class definition to the MetaSimObject methods (in
843 # particular _set_param, which gets called for parameters with default
844 # values defined on the SimObject class itself). It will get
845 # overridden by the permanent definition (which requires that
846 # SimObject be defined) lower in this file.
847 def isSimObjectOrVector(value
):
850 def cxxMethod(*args
, **kwargs
):
851 """Decorator to export C++ functions to Python"""
854 name
= func
.func_name
855 override
= kwargs
.get("override", False)
856 cxx_name
= kwargs
.get("cxx_name", name
)
858 args
, varargs
, keywords
, defaults
= inspect
.getargspec(func
)
859 if varargs
or keywords
:
860 raise ValueError("Wrapped methods must not contain variable " \
863 # Create tuples of (argument, default)
865 args
= args
[:-len(defaults
)] + zip(args
[-len(defaults
):], defaults
)
866 # Don't include self in the argument list to PyBind
871 def cxx_call(self
, *args
, **kwargs
):
872 ccobj
= self
.getCCObject()
873 return getattr(ccobj
, name
)(*args
, **kwargs
)
876 def py_call(self
, *args
, **kwargs
):
877 return self
.func(*args
, **kwargs
)
879 f
= py_call
if override
else cxx_call
880 f
.__pybind
= PyBindMethod(name
, cxx_name
=cxx_name
, args
=args
)
886 elif len(args
) == 1 and len(kwargs
) == 0:
887 return decorate(*args
)
889 raise TypeError("One argument and no kwargs, or only kwargs expected")
891 # This class holds information about each simobject parameter
892 # that should be displayed on the command line for use in the
893 # configuration system.
894 class ParamInfo(object):
895 def __init__(self
, type, desc
, type_str
, example
, default_val
, access_str
):
898 self
.type_str
= type_str
899 self
.example_str
= example
900 self
.default_val
= default_val
901 # The string representation used to access this param through python.
902 # The method to access this parameter presented on the command line may
903 # be different, so this needs to be stored for later use.
904 self
.access_str
= access_str
907 # Make it so we can only set attributes at initialization time
908 # and effectively make this a const object.
909 def __setattr__(self
, name
, value
):
910 if not "created" in self
.__dict
__:
911 self
.__dict
__[name
] = value
913 # The SimObject class is the root of the special hierarchy. Most of
914 # the code in this class deals with the configuration hierarchy itself
915 # (parent/child node relationships).
916 class SimObject(object):
917 # Specify metaclass. Any class inheriting from SimObject will
918 # get this metaclass.
919 __metaclass__
= MetaSimObject
923 cxx_header
= "sim/sim_object.hh"
924 cxx_bases
= [ "Drainable", "Serializable" ]
925 eventq_index
= Param
.UInt32(Parent
.eventq_index
, "Event Queue Index")
928 PyBindMethod("init"),
929 PyBindMethod("initState"),
930 PyBindMethod("memInvalidate"),
931 PyBindMethod("memWriteback"),
932 PyBindMethod("regStats"),
933 PyBindMethod("resetStats"),
934 PyBindMethod("regProbePoints"),
935 PyBindMethod("regProbeListeners"),
936 PyBindMethod("startup"),
939 cxx_param_exports
= [
940 PyBindProperty("name"),
944 def loadState(self
, cp
):
945 """Load SimObject state from a checkpoint"""
948 # Returns a dict of all the option strings that can be
949 # generated as command line options for this simobject instance
950 # by tracing all reachable params in the top level instance and
951 # any children it contains.
952 def enumerateParams(self
, flags_dict
= {},
953 cmd_line_str
= "", access_str
= ""):
954 if hasattr(self
, "_paramEnumed"):
955 print "Cycle detected enumerating params"
957 self
._paramEnumed
= True
958 # Scan the children first to pick up all the objects in this SimObj
959 for keys
in self
._children
:
960 child
= self
._children
[keys
]
961 next_cmdline_str
= cmd_line_str
+ keys
962 next_access_str
= access_str
+ keys
963 if not isSimObjectVector(child
):
964 next_cmdline_str
= next_cmdline_str
+ "."
965 next_access_str
= next_access_str
+ "."
966 flags_dict
= child
.enumerateParams(flags_dict
,
970 # Go through the simple params in the simobject in this level
971 # of the simobject hierarchy and save information about the
972 # parameter to be used for generating and processing command line
973 # options to the simulator to set these parameters.
974 for keys
,values
in self
._params
.items():
975 if values
.isCmdLineSettable():
977 ex_str
= values
.example_str()
979 if isinstance(values
, VectorParamDesc
):
980 type_str
= 'Vector_%s' % values
.ptype_str
983 type_str
= '%s' % values
.ptype_str
986 if keys
in self
._hr
_values\
987 and keys
in self
._values\
988 and not isinstance(self
._values
[keys
],
990 cmd_str
= cmd_line_str
+ keys
991 acc_str
= access_str
+ keys
992 flags_dict
[cmd_str
] = ParamInfo(ptype
,
993 self
._params
[keys
].desc
, type_str
, ex_str
,
994 values
.pretty_print(self
._hr
_values
[keys
]),
996 elif not keys
in self
._hr
_values\
997 and not keys
in self
._values
:
999 cmd_str
= cmd_line_str
+ keys
1000 acc_str
= access_str
+ keys
1001 flags_dict
[cmd_str
] = ParamInfo(ptype
,
1002 self
._params
[keys
].desc
,
1003 type_str
, ex_str
, '', acc_str
)
1007 # Initialize new instance. For objects with SimObject-valued
1008 # children, we need to recursively clone the classes represented
1009 # by those param values as well in a consistent "deep copy"-style
1010 # fashion. That is, we want to make sure that each instance is
1011 # cloned only once, and that if there are multiple references to
1012 # the same original object, we end up with the corresponding
1013 # cloned references all pointing to the same cloned instance.
1014 def __init__(self
, **kwargs
):
1015 ancestor
= kwargs
.get('_ancestor')
1016 memo_dict
= kwargs
.get('_memo')
1017 if memo_dict
is None:
1018 # prepare to memoize any recursively instantiated objects
1021 # memoize me now to avoid problems with recursive calls
1022 memo_dict
[ancestor
] = self
1025 ancestor
= self
.__class
__
1026 ancestor
._instantiated
= True
1028 # initialize required attributes
1031 self
._ccObject
= None # pointer to C++ object
1032 self
._ccParams
= None
1033 self
._instantiated
= False # really "cloned"
1035 # Clone children specified at class level. No need for a
1036 # multidict here since we will be cloning everything.
1037 # Do children before parameter values so that children that
1038 # are also param values get cloned properly.
1040 for key
,val
in ancestor
._children
.iteritems():
1041 self
.add_child(key
, val(_memo
=memo_dict
))
1043 # Inherit parameter values from class using multidict so
1044 # individual value settings can be overridden but we still
1045 # inherit late changes to non-overridden class values.
1046 self
._values
= multidict(ancestor
._values
)
1047 self
._hr
_values
= multidict(ancestor
._hr
_values
)
1048 # clone SimObject-valued parameters
1049 for key
,val
in ancestor
._values
.iteritems():
1050 val
= tryAsSimObjectOrVector(val
)
1052 self
._values
[key
] = val(_memo
=memo_dict
)
1054 # clone port references. no need to use a multidict here
1055 # since we will be creating new references for all ports.
1056 self
._port
_refs
= {}
1057 for key
,val
in ancestor
._port
_refs
.iteritems():
1058 self
._port
_refs
[key
] = val
.clone(self
, memo_dict
)
1059 # apply attribute assignments from keyword args, if any
1060 for key
,val
in kwargs
.iteritems():
1061 setattr(self
, key
, val
)
1063 # "Clone" the current instance by creating another instance of
1064 # this instance's class, but that inherits its parameter values
1065 # and port mappings from the current instance. If we're in a
1066 # "deep copy" recursive clone, check the _memo dict to see if
1067 # we've already cloned this instance.
1068 def __call__(self
, **kwargs
):
1069 memo_dict
= kwargs
.get('_memo')
1070 if memo_dict
is None:
1071 # no memo_dict: must be top-level clone operation.
1072 # this is only allowed at the root of a hierarchy
1074 raise RuntimeError, "attempt to clone object %s " \
1075 "not at the root of a tree (parent = %s)" \
1076 % (self
, self
._parent
)
1077 # create a new dict and use that.
1079 kwargs
['_memo'] = memo_dict
1080 elif memo_dict
.has_key(self
):
1081 # clone already done & memoized
1082 return memo_dict
[self
]
1083 return self
.__class
__(_ancestor
= self
, **kwargs
)
1085 def _get_port_ref(self
, attr
):
1086 # Return reference that can be assigned to another port
1087 # via __setattr__. There is only ever one reference
1088 # object per port, but we create them lazily here.
1089 ref
= self
._port
_refs
.get(attr
)
1091 ref
= self
._ports
[attr
].makeRef(self
)
1092 self
._port
_refs
[attr
] = ref
1095 def __getattr__(self
, attr
):
1096 if self
._ports
.has_key(attr
):
1097 return self
._get
_port
_ref
(attr
)
1099 if self
._values
.has_key(attr
):
1100 return self
._values
[attr
]
1102 if self
._children
.has_key(attr
):
1103 return self
._children
[attr
]
1105 # If the attribute exists on the C++ object, transparently
1106 # forward the reference there. This is typically used for
1107 # methods exported to Python (e.g., init(), and startup())
1108 if self
._ccObject
and hasattr(self
._ccObject
, attr
):
1109 return getattr(self
._ccObject
, attr
)
1111 err_string
= "object '%s' has no attribute '%s'" \
1112 % (self
.__class
__.__name
__, attr
)
1114 if not self
._ccObject
:
1115 err_string
+= "\n (C++ object is not yet constructed," \
1116 " so wrapped C++ methods are unavailable.)"
1118 raise AttributeError, err_string
1120 # Set attribute (called on foo.attr = value when foo is an
1121 # instance of class cls).
1122 def __setattr__(self
, attr
, value
):
1123 # normal processing for private attributes
1124 if attr
.startswith('_'):
1125 object.__setattr
__(self
, attr
, value
)
1128 if self
._ports
.has_key(attr
):
1129 # set up port connection
1130 self
._get
_port
_ref
(attr
).connect(value
)
1133 param
= self
._params
.get(attr
)
1137 value
= param
.convert(value
)
1138 except Exception, e
:
1139 msg
= "%s\nError setting param %s.%s to %s\n" % \
1140 (e
, self
.__class
__.__name
__, attr
, value
)
1143 self
._values
[attr
] = value
1144 # implicitly parent unparented objects assigned as params
1145 if isSimObjectOrVector(value
) and not value
.has_parent():
1146 self
.add_child(attr
, value
)
1147 # set the human-readable value dict if this is a param
1148 # with a literal value and is not being set as an object
1150 if not (isSimObjectOrVector(value
) or\
1151 isinstance(value
, m5
.proxy
.BaseProxy
)):
1152 self
._hr
_values
[attr
] = hr_value
1156 # if RHS is a SimObject, it's an implicit child assignment
1157 if isSimObjectOrSequence(value
):
1158 self
.add_child(attr
, value
)
1161 # no valid assignment... raise exception
1162 raise AttributeError, "Class %s has no parameter %s" \
1163 % (self
.__class
__.__name
__, attr
)
1166 # this hack allows tacking a '[0]' onto parameters that may or may
1167 # not be vectors, and always getting the first element (e.g. cpus)
1168 def __getitem__(self
, key
):
1171 raise IndexError, "Non-zero index '%s' to SimObject" % key
1173 # this hack allows us to iterate over a SimObject that may
1174 # not be a vector, so we can call a loop over it and get just one
1179 # Also implemented by SimObjectVector
1180 def clear_parent(self
, old_parent
):
1181 assert self
._parent
is old_parent
1184 # Also implemented by SimObjectVector
1185 def set_parent(self
, parent
, name
):
1186 self
._parent
= parent
1189 # Return parent object of this SimObject, not implemented by
1190 # SimObjectVector because the elements in a SimObjectVector may not share
1192 def get_parent(self
):
1195 # Also implemented by SimObjectVector
1199 # Also implemented by SimObjectVector
1200 def has_parent(self
):
1201 return self
._parent
is not None
1203 # clear out child with given name. This code is not likely to be exercised.
1204 # See comment in add_child.
1205 def clear_child(self
, name
):
1206 child
= self
._children
[name
]
1207 child
.clear_parent(self
)
1208 del self
._children
[name
]
1210 # Add a new child to this object.
1211 def add_child(self
, name
, child
):
1212 child
= coerceSimObjectOrVector(child
)
1213 if child
.has_parent():
1214 warn("add_child('%s'): child '%s' already has parent", name
,
1216 if self
._children
.has_key(name
):
1217 # This code path had an undiscovered bug that would make it fail
1218 # at runtime. It had been here for a long time and was only
1219 # exposed by a buggy script. Changes here will probably not be
1220 # exercised without specialized testing.
1221 self
.clear_child(name
)
1222 child
.set_parent(self
, name
)
1223 self
._children
[name
] = child
1225 # Take SimObject-valued parameters that haven't been explicitly
1226 # assigned as children and make them children of the object that
1227 # they were assigned to as a parameter value. This guarantees
1228 # that when we instantiate all the parameter objects we're still
1229 # inside the configuration hierarchy.
1230 def adoptOrphanParams(self
):
1231 for key
,val
in self
._values
.iteritems():
1232 if not isSimObjectVector(val
) and isSimObjectSequence(val
):
1233 # need to convert raw SimObject sequences to
1234 # SimObjectVector class so we can call has_parent()
1235 val
= SimObjectVector(val
)
1236 self
._values
[key
] = val
1237 if isSimObjectOrVector(val
) and not val
.has_parent():
1238 warn("%s adopting orphan SimObject param '%s'", self
, key
)
1239 self
.add_child(key
, val
)
1242 if not self
._parent
:
1243 return '<orphan %s>' % self
.__class
__
1244 elif isinstance(self
._parent
, MetaSimObject
):
1245 return str(self
.__class
__)
1247 ppath
= self
._parent
.path()
1250 return ppath
+ "." + self
._name
1255 def config_value(self
):
1261 def find_any(self
, ptype
):
1262 if isinstance(self
, ptype
):
1266 for child
in self
._children
.itervalues():
1268 if hasattr(child
, '_visited'):
1269 visited
= getattr(child
, '_visited')
1271 if isinstance(child
, ptype
) and not visited
:
1272 if found_obj
!= None and child
!= found_obj
:
1273 raise AttributeError, \
1274 'parent.any matched more than one: %s %s' % \
1275 (found_obj
.path
, child
.path
)
1277 # search param space
1278 for pname
,pdesc
in self
._params
.iteritems():
1279 if issubclass(pdesc
.ptype
, ptype
):
1280 match_obj
= self
._values
[pname
]
1281 if found_obj
!= None and found_obj
!= match_obj
:
1282 raise AttributeError, \
1283 'parent.any matched more than one: %s and %s' % \
1284 (found_obj
.path
, match_obj
.path
)
1285 found_obj
= match_obj
1286 return found_obj
, found_obj
!= None
1288 def find_all(self
, ptype
):
1291 for child
in self
._children
.itervalues():
1292 # a child could be a list, so ensure we visit each item
1293 if isinstance(child
, list):
1298 for child
in children
:
1299 if isinstance(child
, ptype
) and not isproxy(child
) and \
1300 not isNullPointer(child
):
1302 if isSimObject(child
):
1303 # also add results from the child itself
1304 child_all
, done
= child
.find_all(ptype
)
1305 all
.update(dict(zip(child_all
, [done
] * len(child_all
))))
1306 # search param space
1307 for pname
,pdesc
in self
._params
.iteritems():
1308 if issubclass(pdesc
.ptype
, ptype
):
1309 match_obj
= self
._values
[pname
]
1310 if not isproxy(match_obj
) and not isNullPointer(match_obj
):
1311 all
[match_obj
] = True
1312 # Also make sure to sort the keys based on the objects' path to
1313 # ensure that the order is the same on all hosts
1314 return sorted(all
.keys(), key
= lambda o
: o
.path()), True
1316 def unproxy(self
, base
):
1319 def unproxyParams(self
):
1320 for param
in self
._params
.iterkeys():
1321 value
= self
._values
.get(param
)
1322 if value
!= None and isproxy(value
):
1324 value
= value
.unproxy(self
)
1326 print "Error in unproxying param '%s' of %s" % \
1327 (param
, self
.path())
1329 setattr(self
, param
, value
)
1331 # Unproxy ports in sorted order so that 'append' operations on
1332 # vector ports are done in a deterministic fashion.
1333 port_names
= self
._ports
.keys()
1335 for port_name
in port_names
:
1336 port
= self
._port
_refs
.get(port_name
)
1340 def print_ini(self
, ini_file
):
1341 print >>ini_file
, '[' + self
.path() + ']' # .ini section header
1343 instanceDict
[self
.path()] = self
1345 if hasattr(self
, 'type'):
1346 print >>ini_file
, 'type=%s' % self
.type
1348 if len(self
._children
.keys()):
1349 print >>ini_file
, 'children=%s' % \
1350 ' '.join(self
._children
[n
].get_name() \
1351 for n
in sorted(self
._children
.keys()))
1353 for param
in sorted(self
._params
.keys()):
1354 value
= self
._values
.get(param
)
1356 print >>ini_file
, '%s=%s' % (param
,
1357 self
._values
[param
].ini_str())
1359 for port_name
in sorted(self
._ports
.keys()):
1360 port
= self
._port
_refs
.get(port_name
, None)
1362 print >>ini_file
, '%s=%s' % (port_name
, port
.ini_str())
1364 print >>ini_file
# blank line between objects
1366 # generate a tree of dictionaries expressing all the parameters in the
1367 # instantiated system for use by scripts that want to do power, thermal
1368 # visualization, and other similar tasks
1369 def get_config_as_dict(self
):
1371 if hasattr(self
, 'type'):
1373 if hasattr(self
, 'cxx_class'):
1374 d
.cxx_class
= self
.cxx_class
1375 # Add the name and path of this object to be able to link to
1377 d
.name
= self
.get_name()
1378 d
.path
= self
.path()
1380 for param
in sorted(self
._params
.keys()):
1381 value
= self
._values
.get(param
)
1383 d
[param
] = value
.config_value()
1385 for n
in sorted(self
._children
.keys()):
1386 child
= self
._children
[n
]
1387 # Use the name of the attribute (and not get_name()) as
1388 # the key in the JSON dictionary to capture the hierarchy
1389 # in the Python code that assembled this system
1390 d
[n
] = child
.get_config_as_dict()
1392 for port_name
in sorted(self
._ports
.keys()):
1393 port
= self
._port
_refs
.get(port_name
, None)
1395 # Represent each port with a dictionary containing the
1396 # prominent attributes
1397 d
[port_name
] = port
.get_config_as_dict()
1401 def getCCParams(self
):
1403 return self
._ccParams
1405 cc_params_struct
= getattr(m5
.internal
.params
, '%sParams' % self
.type)
1406 cc_params
= cc_params_struct()
1407 cc_params
.name
= str(self
)
1409 param_names
= self
._params
.keys()
1411 for param
in param_names
:
1412 value
= self
._values
.get(param
)
1414 fatal("%s.%s without default or user set value",
1417 value
= value
.getValue()
1418 if isinstance(self
._params
[param
], VectorParamDesc
):
1419 assert isinstance(value
, list)
1420 vec
= getattr(cc_params
, param
)
1422 # Some types are exposed as opaque types. They support
1423 # the append operation unlike the automatically
1425 if isinstance(vec
, list):
1426 setattr(cc_params
, param
, list(value
))
1429 getattr(cc_params
, param
).append(v
)
1431 setattr(cc_params
, param
, value
)
1433 port_names
= self
._ports
.keys()
1435 for port_name
in port_names
:
1436 port
= self
._port
_refs
.get(port_name
, None)
1438 port_count
= len(port
)
1441 setattr(cc_params
, 'port_' + port_name
+ '_connection_count',
1443 self
._ccParams
= cc_params
1444 return self
._ccParams
1446 # Get C++ object corresponding to this object, calling C++ if
1447 # necessary to construct it. Does *not* recursively create
1449 def getCCObject(self
):
1450 if not self
._ccObject
:
1451 # Make sure this object is in the configuration hierarchy
1452 if not self
._parent
and not isRoot(self
):
1453 raise RuntimeError, "Attempt to instantiate orphan node"
1454 # Cycles in the configuration hierarchy are not supported. This
1455 # will catch the resulting recursion and stop.
1457 if not self
.abstract
:
1458 params
= self
.getCCParams()
1459 self
._ccObject
= params
.create()
1460 elif self
._ccObject
== -1:
1461 raise RuntimeError, "%s: Cycle found in configuration hierarchy." \
1463 return self
._ccObject
1465 def descendants(self
):
1467 # The order of the dict is implementation dependent, so sort
1468 # it based on the key (name) to ensure the order is the same
1470 for (name
, child
) in sorted(self
._children
.iteritems()):
1471 for obj
in child
.descendants():
1474 # Call C++ to create C++ object corresponding to this object
1475 def createCCObject(self
):
1477 self
.getCCObject() # force creation
1480 return self
.getCCObject()
1482 # Create C++ port connections corresponding to the connections in
1484 def connectPorts(self
):
1485 # Sort the ports based on their attribute name to ensure the
1486 # order is the same on all hosts
1487 for (attr
, portRef
) in sorted(self
._port
_refs
.iteritems()):
1490 # Function to provide to C++ so it can look up instances based on paths
1491 def resolveSimObject(name
):
1492 obj
= instanceDict
[name
]
1493 return obj
.getCCObject()
1495 def isSimObject(value
):
1496 return isinstance(value
, SimObject
)
1498 def isSimObjectClass(value
):
1499 return issubclass(value
, SimObject
)
1501 def isSimObjectVector(value
):
1502 return isinstance(value
, SimObjectVector
)
1504 def isSimObjectSequence(value
):
1505 if not isinstance(value
, (list, tuple)) or len(value
) == 0:
1509 if not isNullPointer(val
) and not isSimObject(val
):
1514 def isSimObjectOrSequence(value
):
1515 return isSimObject(value
) or isSimObjectSequence(value
)
1518 from m5
.objects
import Root
1519 return obj
and obj
is Root
.getInstance()
1521 def isSimObjectOrVector(value
):
1522 return isSimObject(value
) or isSimObjectVector(value
)
1524 def tryAsSimObjectOrVector(value
):
1525 if isSimObjectOrVector(value
):
1527 if isSimObjectSequence(value
):
1528 return SimObjectVector(value
)
1531 def coerceSimObjectOrVector(value
):
1532 value
= tryAsSimObjectOrVector(value
)
1534 raise TypeError, "SimObject or SimObjectVector expected"
1537 baseClasses
= allClasses
.copy()
1538 baseInstances
= instanceDict
.copy()
1541 global allClasses
, instanceDict
, noCxxHeader
1543 allClasses
= baseClasses
.copy()
1544 instanceDict
= baseInstances
.copy()
1547 # __all__ defines the list of symbols that get exported when
1548 # 'from config import *' is invoked. Try to keep this reasonably
1549 # short to avoid polluting other namespaces.