1 from __future__
import generators
7 #####################################################################
9 # M5 Python Configuration Utility
11 # The basic idea is to write simple Python programs that build Python
12 # objects corresponding to M5 SimObjects for the deisred simulation
13 # configuration. For now, the Python emits a .ini file that can be
14 # parsed by M5. In the future, some tighter integration between M5
15 # and the Python interpreter may allow bypassing the .ini file.
17 # Each SimObject class in M5 is represented by a Python class with the
18 # same name. The Python inheritance tree mirrors the M5 C++ tree
19 # (e.g., SimpleCPU derives from BaseCPU in both cases, and all
20 # SimObjects inherit from a single SimObject base class). To specify
21 # an instance of an M5 SimObject in a configuration, the user simply
22 # instantiates the corresponding Python object. The parameters for
23 # that SimObject are given by assigning to attributes of the Python
24 # object, either using keyword assignment in the constructor or in
25 # separate assignment statements. For example:
27 # cache = BaseCache('my_cache', root, size=64*K)
28 # cache.hit_latency = 3
31 # (The first two constructor arguments specify the name of the created
32 # cache and its parent node in the hierarchy.)
34 # The magic lies in the mapping of the Python attributes for SimObject
35 # classes to the actual SimObject parameter specifications. This
36 # allows parameter validity checking in the Python code. Continuing
37 # the example above, the statements "cache.blurfl=3" or
38 # "cache.assoc='hello'" would both result in runtime errors in Python,
39 # since the BaseCache object has no 'blurfl' parameter and the 'assoc'
40 # parameter requires an integer, respectively. This magic is done
41 # primarily by overriding the special __setattr__ method that controls
42 # assignment to object attributes.
44 # The Python module provides another class, ConfigNode, which is a
45 # superclass of SimObject. ConfigNode implements the parent/child
46 # relationship for building the configuration hierarchy tree.
47 # Concrete instances of ConfigNode can be used to group objects in the
48 # hierarchy, but do not correspond to SimObjects themselves (like a
49 # .ini section with "children=" but no "type=".
51 # Once a set of Python objects have been instantiated in a hierarchy,
52 # calling 'instantiate(obj)' (where obj is the root of the hierarchy)
53 # will generate a .ini file. See simple-4cpu.py for an example
54 # (corresponding to m5-test/simple-4cpu.ini).
56 #####################################################################
58 #####################################################################
60 # ConfigNode/SimObject classes
62 # The Python class hierarchy rooted by ConfigNode (which is the base
63 # class of SimObject, which in turn is the base class of all other M5
64 # SimObject classes) has special attribute behavior. In general, an
65 # object in this hierarchy has three categories of attribute-like
68 # 1. Regular Python methods and variables. These must start with an
69 # underscore to be treated normally.
71 # 2. SimObject parameters. These values are stored as normal Python
72 # attributes, but all assignments to these attributes are checked
73 # against the pre-defined set of parameters stored in the class's
74 # _param_dict dictionary. Assignments to attributes that do not
75 # correspond to predefined parameters, or that are not of the correct
76 # type, incur runtime errors.
78 # 3. Hierarchy children. The child nodes of a ConfigNode are stored
79 # in the node's _children dictionary, but can be accessed using the
80 # Python attribute dot-notation (just as they are printed out by the
81 # simulator). Children cannot be created using attribute assigment;
82 # they must be added by specifying the parent node in the child's
83 # constructor or using the '+=' operator.
85 # The SimObject parameters are the most complex, for a few reasons.
86 # First, both parameter descriptions and parameter values are
87 # inherited. Thus parameter description lookup must go up the
88 # inheritance chain like normal attribute lookup, but this behavior
89 # must be explicitly coded since the lookup occurs in each class's
90 # _param_dict attribute. Second, because parameter values can be set
91 # on SimObject classes (to implement default values), the parameter
92 # checking behavior must be enforced on class attribute assignments as
93 # well as instance attribute assignments. Finally, because we allow
94 # class specialization via inheritance (e.g., see the L1Cache class in
95 # the simple-4cpu.py example), we must do parameter checking even on
96 # class instantiation. To provide all these features, we use a
97 # metaclass to define most of the SimObject parameter behavior for
98 # this class hierarchy.
100 #####################################################################
102 # The metaclass for ConfigNode (and thus for everything that derives
103 # from ConfigNode, including SimObject). This class controls how new
104 # classes that derive from ConfigNode are instantiated, and provides
105 # inherited class behavior (just like a class controls how instances
106 # of that class are instantiated, and provides inherited instance
108 class MetaConfigNode(type):
110 # __new__ is called before __init__, and is where the statements
111 # in the body of the class definition get loaded into the class's
112 # __dict__. We intercept this to filter out parameter assignments
113 # and only allow "private" attributes to be passed to the base
114 # __new__ (starting with underscore).
115 def __new__(cls
, name
, bases
, dict):
116 priv_keys
= [k
for k
in dict.iterkeys() if k
.startswith('_')]
118 for k
in priv_keys
: priv_dict
[k
] = dict[k
]; del dict[k
]
119 # entries left in dict will get passed to __init__, where we'll
120 # deal with them as params.
121 return super(MetaConfigNode
, cls
).__new
__(cls
, name
, bases
, priv_dict
)
123 # initialization: start out with an empty param dict (makes life
124 # simpler if we can assume _param_dict is always valid). Also
125 # build inheritance list to simplify searching for inherited
126 # params. Finally set parameters specified in class definition
128 def __init__(cls
, name
, bases
, dict):
129 super(MetaConfigNode
, cls
).__init
__(cls
, name
, bases
, {})
130 # initialize _param_dict to empty
132 # __mro__ is the ordered list of classes Python uses for
133 # method resolution. We want to pick out the ones that have a
134 # _param_dict attribute for doing parameter lookups.
136 [c
for c
in cls
.__mro
__ if hasattr(c
, '_param_dict')]
137 # initialize attributes with values from class definition
138 for (pname
, value
) in dict.items():
140 setattr(cls
, pname
, value
)
141 except Exception, exc
:
142 print "Error setting '%s' to '%s' on class '%s'\n" \
143 % (pname
, value
, cls
.__name
__), exc
145 # set the class's parameter dictionary (called when loading
146 # class descriptions)
147 def set_param_dict(cls
, param_dict
):
148 # should only be called once (current one should be empty one
150 assert not cls
._param
_dict
151 cls
._param
_dict
= param_dict
152 # initialize attributes with default values
153 for (pname
, param
) in param_dict
.items():
155 setattr(cls
, pname
, param
.default
)
156 except Exception, exc
:
157 print "Error setting '%s' default on class '%s'\n" \
158 % (pname
, cls
.__name
__), exc
160 # Set the class's parameter dictionary given a code string of
161 # parameter initializers (as from an object description file).
162 # Note that the caller must pass in the namespace in which to
163 # execute the code (usually the caller's globals()), since if we
164 # call globals() from inside this function all we get is this
165 # module's internal scope.
166 def init_params(cls
, init_code
, ctx
):
169 exec fixPythonIndentation(init_code
) in ctx
, dict
170 except Exception, exc
:
171 print "Error in %s.init_params:" % cls
.__name
__, exc
173 cls
.set_param_dict(dict)
175 # Lookup a parameter description by name in the given class. Use
176 # the _param_bases list defined in __init__ to go up the
177 # inheritance hierarchy if necessary.
178 def lookup_param(cls
, param_name
):
179 for c
in cls
._param
_bases
:
180 param
= c
._param
_dict
.get(param_name
)
181 if param
: return param
184 # Set attribute (called on foo.attr_name = value when foo is an
185 # instance of class cls).
186 def __setattr__(cls
, attr_name
, value
):
187 # normal processing for private attributes
188 if attr_name
.startswith('_'):
189 object.__setattr
__(cls
, attr_name
, value
)
191 # no '_': must be SimObject param
192 param
= cls
.lookup_param(attr_name
)
194 raise AttributeError, \
195 "Class %s has no parameter %s" % (cls
.__name
__, attr_name
)
196 # It's ok: set attribute by delegating to 'object' class.
197 # Note the use of param.make_value() to verify/canonicalize
199 object.__setattr
__(cls
, attr_name
, param
.make_value(value
))
201 # generator that iterates across all parameters for this class and
202 # all classes it inherits from
203 def all_param_names(cls
):
204 for c
in cls
._param
_bases
:
205 for p
in c
._param
_dict
.iterkeys():
208 # The ConfigNode class is the root of the special hierarchy. Most of
209 # the code in this class deals with the configuration hierarchy itself
210 # (parent/child node relationships).
211 class ConfigNode(object):
212 # Specify metaclass. Any class inheriting from ConfigNode will
213 # get this metaclass.
214 __metaclass__
= MetaConfigNode
216 # Constructor. Since bare ConfigNodes don't have parameters, just
217 # worry about the name and the parent/child stuff.
218 def __init__(self
, _name
, _parent
=None):
220 if type(_name
) != str:
221 if isinstance(_name
, ConfigNode
):
222 # special case message for common error of trying to
223 # coerce a SimObject to the wrong type
225 "Attempt to coerce %s to %s" \
226 % (_name
.__class
__.__name
__, self
.__class
__.__name
__)
229 "%s name must be string (was %s, %s)" \
230 % (self
.__class
__.__name
__, _name
, type(_name
))
231 # if specified, parent must be a subclass of ConfigNode
232 if _parent
!= None and not isinstance(_parent
, ConfigNode
):
234 "%s parent must be ConfigNode subclass (was %s, %s)" \
235 % (self
.__class
__.__name
__, _name
, type(_name
))
237 self
._parent
= _parent
239 _parent
._add
_child
(self
)
241 # keep a list of children in addition to the dictionary keys
242 # so we can remember the order they were added and print them
244 self
._child
_list
= []
246 # When printing (e.g. to .ini file), just give the name.
250 # Catch attribute accesses that could be requesting children, and
251 # satisfy them. Note that __getattr__ is called only if the
252 # regular attribute lookup fails, so private and parameter lookups
253 # will already be satisfied before we ever get here.
254 def __getattr__(self
, name
):
256 return self
._children
[name
]
258 raise AttributeError, \
259 "Node '%s' has no attribute or child '%s'" \
262 # Set attribute. All attribute assignments go through here. Must
263 # be private attribute (starts with '_') or valid parameter entry.
264 # Basically identical to MetaConfigClass.__setattr__(), except
265 # this sets attributes on specific instances rather than on classes.
266 def __setattr__(self
, attr_name
, value
):
267 if attr_name
.startswith('_'):
268 object.__setattr
__(self
, attr_name
, value
)
270 # not private; look up as param
271 param
= self
.__class
__.lookup_param(attr_name
)
273 raise AttributeError, \
274 "Class %s has no parameter %s" \
275 % (self
.__class
__.__name
__, attr_name
)
276 # It's ok: set attribute by delegating to 'object' class.
277 # Note the use of param.make_value() to verify/canonicalize
278 # the assigned value.
279 v
= param
.make_value(value
)
280 object.__setattr
__(self
, attr_name
, v
)
282 # A little convenient magic: if the parameter is a ConfigNode
283 # (or vector of ConfigNodes, or anything else with a
284 # '_set_parent_if_none' function attribute) that does not have
285 # a parent (and so is not part of the configuration
286 # hierarchy), then make this node its parent.
287 if hasattr(v
, '_set_parent_if_none'):
288 v
._set
_parent
_if
_none
(self
)
291 # Return absolute path from root.
292 if not self
._parent
and self
._name
!= 'Universe':
293 print >> sys
.stderr
, "Warning:", self
._name
, "has no parent"
294 parent_path
= self
._parent
and self
._parent
._path
()
295 if parent_path
and parent_path
!= 'Universe':
296 return parent_path
+ '.' + self
._name
300 # Add a child to this node.
301 def _add_child(self
, new_child
):
302 # set child's parent before calling this function
303 assert new_child
._parent
== self
304 if not isinstance(new_child
, ConfigNode
):
306 "ConfigNode child must also be of class ConfigNode"
307 if new_child
._name
in self
._children
:
308 raise AttributeError, \
309 "Node '%s' already has a child '%s'" \
310 % (self
._name
, new_child
._name
)
311 self
._children
[new_child
._name
] = new_child
312 self
._child
_list
+= [new_child
]
314 # operator overload for '+='. You can say "node += child" to add
315 # a child that was created with parent=None. An early attempt
316 # at playing with syntax; turns out not to be that useful.
317 def __iadd__(self
, new_child
):
318 if new_child
._parent
!= None:
319 raise AttributeError, \
320 "Node '%s' already has a parent" % new_child
._name
321 new_child
._parent
= self
322 self
._add
_child
(new_child
)
325 # Set this instance's parent to 'parent' if it doesn't already
326 # have one. See ConfigNode.__setattr__().
327 def _set_parent_if_none(self
, parent
):
328 if self
._parent
== None:
331 # Print instance info to .ini file.
332 def _instantiate(self
):
333 print '[' + self
._path
() + ']' # .ini section header
335 # instantiate children in same order they were added for
336 # backward compatibility (else we can end up with cpu1
338 print 'children =', ' '.join([c
._name
for c
in self
._child
_list
])
339 self
._instantiateParams
()
341 # recursively dump out children
342 for c
in self
._child
_list
:
345 # ConfigNodes have no parameters. Overridden by SimObject.
346 def _instantiateParams(self
):
349 # SimObject is a minimal extension of ConfigNode, implementing a
350 # hierarchy node that corresponds to an M5 SimObject. It prints out a
351 # "type=" line to indicate its SimObject class, prints out the
352 # assigned parameters corresponding to its class, and allows
353 # parameters to be set by keyword in the constructor. Note that most
354 # of the heavy lifting for the SimObject param handling is done in the
355 # MetaConfigNode metaclass.
357 class SimObject(ConfigNode
):
358 # initialization: like ConfigNode, but handle keyword-based
359 # parameter initializers.
360 def __init__(self
, _name
, _parent
=None, **params
):
361 ConfigNode
.__init
__(self
, _name
, _parent
)
362 for param
, value
in params
.items():
363 setattr(self
, param
, value
)
365 # print type and parameter values to .ini file
366 def _instantiateParams(self
):
367 print "type =", self
.__class
__._name
368 for pname
in self
.__class
__.all_param_names():
369 value
= getattr(self
, pname
)
371 print pname
, '=', value
375 param_names
= cls
._param
_dict
.keys()
377 code
= "BEGIN_DECLARE_SIM_OBJECT_PARAMS(%s)\n" % name
378 decls
= [" " + cls
._param
_dict
[pname
].sim_decl(pname
) \
379 for pname
in param_names
]
380 code
+= "\n".join(decls
) + "\n"
381 code
+= "END_DECLARE_SIM_OBJECT_PARAMS(%s)\n\n" % name
382 code
+= "BEGIN_INIT_SIM_OBJECT_PARAMS(%s)\n" % name
383 inits
= [" " + cls
._param
_dict
[pname
].sim_init(pname
) \
384 for pname
in param_names
]
385 code
+= ",\n".join(inits
) + "\n"
386 code
+= "END_INIT_SIM_OBJECT_PARAMS(%s)\n\n" % name
388 _sim_code
= classmethod(_sim_code
)
390 #####################################################################
392 # Parameter description classes
394 # The _param_dict dictionary in each class maps parameter names to
395 # either a Param or a VectorParam object. These objects contain the
396 # parameter description string, the parameter type, and the default
397 # value (loaded from the PARAM section of the .odesc files). The
398 # make_value() method on these objects is used to force whatever value
399 # is assigned to the parameter to the appropriate type.
401 # Note that the default values are loaded into the class's attribute
402 # space when the parameter dictionary is initialized (in
403 # MetaConfigNode.set_param_dict()); after that point they aren't
406 #####################################################################
408 def isNullPointer(value
):
409 return isinstance(value
, NullSimObject
)
413 # Constructor. E.g., Param(Int, "number of widgets", 5)
414 def __init__(self
, ptype
, desc
, default
=None):
416 self
.ptype_name
= self
.ptype
.__name
__
418 self
.default
= default
420 # Convert assigned value to appropriate type. Force parameter
421 # value (rhs of '=') to ptype (or None, which means not set).
422 def make_value(self
, value
):
423 # nothing to do if None or already correct type. Also allow NULL
424 # pointer to be assigned where a SimObject is expected.
425 if value
== None or isinstance(value
, self
.ptype
) or \
426 isNullPointer(value
) and issubclass(self
.ptype
, ConfigNode
):
428 # this type conversion will raise an exception if it's illegal
429 return self
.ptype(value
)
431 def sim_decl(self
, name
):
432 return 'Param<%s> %s;' % (self
.ptype_name
, name
)
434 def sim_init(self
, name
):
435 if self
.default
== None:
436 return 'INIT_PARAM(%s, "%s")' % (name
, self
.desc
)
438 return 'INIT_PARAM_DFLT(%s, "%s", %s)' % \
439 (name
, self
.desc
, str(self
.default
))
441 # The _VectorParamValue class is a wrapper for vector-valued
442 # parameters. The leading underscore indicates that users shouldn't
443 # see this class; it's magically generated by VectorParam. The
444 # parameter values are stored in the 'value' field as a Python list of
445 # whatever type the parameter is supposed to be. The only purpose of
446 # storing these instead of a raw Python list is that we can override
447 # the __str__() method to not print out '[' and ']' in the .ini file.
448 class _VectorParamValue(object):
449 def __init__(self
, value
):
450 assert isinstance(value
, list) or value
== None
454 return ' '.join(map(str, self
.value
))
456 # Set member instance's parents to 'parent' if they don't already
457 # have one. Extends "magic" parenting of ConfigNodes to vectors
458 # of ConfigNodes as well. See ConfigNode.__setattr__().
459 def _set_parent_if_none(self
, parent
):
460 if self
.value
and hasattr(self
.value
[0], '_set_parent_if_none'):
462 v
._set
_parent
_if
_none
(parent
)
464 # Vector-valued parameter description. Just like Param, except that
465 # the value is a vector (list) of the specified type instead of a
467 class VectorParam(Param
):
469 # Inherit Param constructor. However, the resulting parameter
470 # will be a list of ptype rather than a single element of ptype.
471 def __init__(self
, ptype
, desc
, default
=None):
472 Param
.__init
__(self
, ptype
, desc
, default
)
474 # Convert assigned value to appropriate type. If the RHS is not a
475 # list or tuple, it generates a single-element list.
476 def make_value(self
, value
):
477 if value
== None: return value
478 if isinstance(value
, list) or isinstance(value
, tuple):
479 # list: coerce each element into new list
480 val_list
= [Param
.make_value(self
, v
) for v
in iter(value
)]
482 # singleton: coerce & wrap in a list
483 val_list
= [Param
.make_value(self
, value
)]
484 # wrap list in _VectorParamValue (see above)
485 return _VectorParamValue(val_list
)
487 def sim_decl(self
, name
):
488 return 'VectorParam<%s> %s;' % (self
.ptype_name
, name
)
490 # sim_init inherited from Param
492 #####################################################################
496 # Though native Python types could be used to specify parameter types
497 # (the 'ptype' field of the Param and VectorParam classes), it's more
498 # flexible to define our own set of types. This gives us more control
499 # over how Python expressions are converted to values (via the
500 # __init__() constructor) and how these values are printed out (via
501 # the __str__() conversion method). Eventually we'll need these types
502 # to correspond to distinct C++ types as well.
504 #####################################################################
506 # Integer parameter type.
508 # Constructor. Value must be Python int or long (long integer).
509 def __init__(self
, value
):
511 if t
== int or t
== long:
514 raise TypeError, "Int param got value %s %s" % (repr(value
), t
)
516 # Use Python string conversion. Note that this puts an 'L' on the
517 # end of long integers; we can strip that off here if it gives us
520 return str(self
.value
)
522 # Counter, Addr, and Tick are just aliases for Int for now.
532 # Boolean parameter type.
535 # Constructor. Typically the value will be one of the Python bool
536 # constants True or False (or the aliases true and false below).
537 # Also need to take integer 0 or 1 values since bool was not a
538 # distinct type in Python 2.2. Parse a bunch of boolean-sounding
539 # strings too just for kicks.
540 def __init__(self
, value
):
544 elif t
== int or t
== long:
551 if v
== "true" or v
== "t" or v
== "yes" or v
== "y":
553 elif v
== "false" or v
== "f" or v
== "no" or v
== "n":
555 # if we didn't set it yet, it must not be something we understand
556 if not hasattr(self
, 'value'):
557 raise TypeError, "Bool param got value %s %s" % (repr(value
), t
)
559 # Generate printable string version.
561 if self
.value
: return "true"
564 # String-valued parameter.
565 class String(object):
566 # Constructor. Value must be Python string.
567 def __init__(self
, value
):
572 raise TypeError, "String param got value %s %s" % (repr(value
), t
)
574 # Generate printable string version. Not too tricky.
578 # Special class for NULL pointers. Note the special check in
579 # make_param_value() above that lets these be assigned where a
580 # SimObject is required.
581 class NullSimObject(object):
582 # Constructor. No parameters, nothing to do.
589 # The only instance you'll ever need...
590 NULL
= NullSimObject()
592 # Enumerated types are a little more complex. The user specifies the
593 # type as Enum(foo) where foo is either a list or dictionary of
594 # alternatives (typically strings, but not necessarily so). (In the
595 # long run, the integer value of the parameter will be the list index
596 # or the corresponding dictionary value. For now, since we only check
597 # that the alternative is valid and then spit it into a .ini file,
598 # there's not much point in using the dictionary.)
600 # What Enum() must do is generate a new type encapsulating the
601 # provided list/dictionary so that specific values of the parameter
602 # can be instances of that type. We define two hidden internal
603 # classes (_ListEnum and _DictEnum) to serve as base classes, then
604 # derive the new type from the appropriate base class on the fly.
607 # Base class for list-based Enum types.
608 class _ListEnum(object):
609 # Constructor. Value must be a member of the type's map list.
610 def __init__(self
, value
):
611 if value
in self
.map:
613 self
.index
= self
.map.index(value
)
615 raise TypeError, "Enum param got bad value '%s' (not in %s)" \
618 # Generate printable string version of value.
620 return str(self
.value
)
622 class _DictEnum(object):
623 # Constructor. Value must be a key in the type's map dictionary.
624 def __init__(self
, value
):
625 if value
in self
.map:
627 self
.index
= self
.map[value
]
629 raise TypeError, "Enum param got bad value '%s' (not in %s)" \
630 % (value
, self
.map.keys())
632 # Generate printable string version of value.
634 return str(self
.value
)
636 # Enum metaclass... calling Enum(foo) generates a new type (class)
637 # that derives from _ListEnum or _DictEnum as appropriate.
639 # counter to generate unique names for generated classes
642 def __new__(cls
, map):
643 if isinstance(map, dict):
646 elif isinstance(map, list):
650 raise TypeError, "Enum map must be list or dict (got %s)" % map
651 classname
= "Enum%04d" % Enum
.counter
653 # New class derives from selected base, and gets a 'map'
654 # attribute containing the specified list or dict.
655 return type.__new
__(cls
, classname
, (base
,), { 'map': map })
659 # "Constants"... handy aliases for various values.
662 # For compatibility with C++ bool constants.
666 # Some memory range specifications use this as a default upper bound.
669 # For power-of-two sizing, e.g. 64*K gives an integer value 65536.
674 #####################################################################
676 # Munge an arbitrary Python code string to get it to execute (mostly
677 # dealing with indentation). Stolen from isa_parser.py... see
678 # comments there for a more detailed description.
679 def fixPythonIndentation(s
):
680 # get rid of blank lines first
681 s
= re
.sub(r
'(?m)^\s*\n', '', s
);
682 if (s
!= '' and re
.match(r
'[ \t]', s
[0])):
686 # Hook to generate C++ parameter code.
687 def gen_sim_code(file):
688 for objname
in sim_object_list
:
689 print >> file, eval("%s._sim_code()" % objname
)
691 # The final hook to generate .ini files. Called from configuration
692 # script once config is built.
693 def instantiate(*objs
):