1 # Copyright (c) 2004-2006 The Regents of The University of Michigan
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met: redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer;
8 # redistributions in binary form must reproduce the above copyright
9 # notice, this list of conditions and the following disclaimer in the
10 # documentation and/or other materials provided with the distribution;
11 # neither the name of the copyright holders nor the names of its
12 # contributors may be used to endorse or promote products derived from
13 # this software without specific prior written permission.
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 # Authors: Steve Reinhardt
33 from multidict
import multidict
35 # These utility functions have to come first because they're
36 # referenced in params.py... otherwise they won't be defined when we
37 # import params below, and the recursive import of this file from
38 # params.py will not find these names.
39 def isSimObject(value
):
40 return isinstance(value
, SimObject
)
42 def isSimObjectClass(value
):
43 return issubclass(value
, SimObject
)
45 def isSimObjectSequence(value
):
46 if not isinstance(value
, (list, tuple)) or len(value
) == 0:
50 if not isNullPointer(val
) and not isSimObject(val
):
55 def isSimObjectOrSequence(value
):
56 return isSimObject(value
) or isSimObjectSequence(value
)
58 # Have to import params up top since Param is referenced on initial
59 # load (when SimObject class references Param to create a class
60 # variable, the 'name' param)...
62 # There are a few things we need that aren't in params.__all__ since
63 # normal users don't need them
64 from params
import ParamDesc
, isNullPointer
, SimObjVector
72 #####################################################################
74 # M5 Python Configuration Utility
76 # The basic idea is to write simple Python programs that build Python
77 # objects corresponding to M5 SimObjects for the desired simulation
78 # configuration. For now, the Python emits a .ini file that can be
79 # parsed by M5. In the future, some tighter integration between M5
80 # and the Python interpreter may allow bypassing the .ini file.
82 # Each SimObject class in M5 is represented by a Python class with the
83 # same name. The Python inheritance tree mirrors the M5 C++ tree
84 # (e.g., SimpleCPU derives from BaseCPU in both cases, and all
85 # SimObjects inherit from a single SimObject base class). To specify
86 # an instance of an M5 SimObject in a configuration, the user simply
87 # instantiates the corresponding Python object. The parameters for
88 # that SimObject are given by assigning to attributes of the Python
89 # object, either using keyword assignment in the constructor or in
90 # separate assignment statements. For example:
92 # cache = BaseCache(size='64KB')
93 # cache.hit_latency = 3
96 # The magic lies in the mapping of the Python attributes for SimObject
97 # classes to the actual SimObject parameter specifications. This
98 # allows parameter validity checking in the Python code. Continuing
99 # the example above, the statements "cache.blurfl=3" or
100 # "cache.assoc='hello'" would both result in runtime errors in Python,
101 # since the BaseCache object has no 'blurfl' parameter and the 'assoc'
102 # parameter requires an integer, respectively. This magic is done
103 # primarily by overriding the special __setattr__ method that controls
104 # assignment to object attributes.
106 # Once a set of Python objects have been instantiated in a hierarchy,
107 # calling 'instantiate(obj)' (where obj is the root of the hierarchy)
108 # will generate a .ini file.
110 #####################################################################
112 # dict to look up SimObjects based on path
115 # The metaclass for SimObject. This class controls how new classes
116 # that derive from SimObject are instantiated, and provides inherited
117 # class behavior (just like a class controls how instances of that
118 # class are instantiated, and provides inherited instance behavior).
119 class MetaSimObject(type):
120 # Attributes that can be set only at initialization time
121 init_keywords
= { 'abstract' : types
.BooleanType
,
122 'type' : types
.StringType
}
123 # Attributes that can be set any time
124 keywords
= { 'check' : types
.FunctionType
,
125 'cxx_type' : types
.StringType
,
126 'cxx_predecls' : types
.ListType
,
127 'swig_predecls' : types
.ListType
}
129 # __new__ is called before __init__, and is where the statements
130 # in the body of the class definition get loaded into the class's
131 # __dict__. We intercept this to filter out parameter & port assignments
132 # and only allow "private" attributes to be passed to the base
133 # __new__ (starting with underscore).
134 def __new__(mcls
, name
, bases
, dict):
135 # Copy "private" attributes, functions, and classes to the
136 # official dict. Everything else goes in _init_dict to be
137 # filtered in __init__.
140 for key
,val
in dict.items():
141 if key
.startswith('_') or isinstance(val
, (types
.FunctionType
,
145 # must be a param/port setting
146 value_dict
[key
] = val
147 cls_dict
['_value_dict'] = value_dict
148 return super(MetaSimObject
, mcls
).__new
__(mcls
, name
, bases
, cls_dict
)
150 # subclass initialization
151 def __init__(cls
, name
, bases
, dict):
152 # calls type.__init__()... I think that's a no-op, but leave
153 # it here just in case it's not.
154 super(MetaSimObject
, cls
).__init
__(name
, bases
, dict)
156 # initialize required attributes
158 # class-only attributes
159 cls
._params
= multidict() # param descriptions
160 cls
._ports
= multidict() # port descriptions
162 # class or instance attributes
163 cls
._values
= multidict() # param values
164 cls
._port
_map
= multidict() # port bindings
165 cls
._instantiated
= False # really instantiated, cloned, or subclassed
167 # We don't support multiple inheritance. If you want to, you
168 # must fix multidict to deal with it properly.
170 raise TypeError, "SimObjects do not support multiple inheritance"
174 # Set up general inheritance via multidicts. A subclass will
175 # inherit all its settings from the base class. The only time
176 # the following is not true is when we define the SimObject
177 # class itself (in which case the multidicts have no parent).
178 if isinstance(base
, MetaSimObject
):
179 cls
._params
.parent
= base
._params
180 cls
._ports
.parent
= base
._ports
181 cls
._values
.parent
= base
._values
182 cls
._port
_map
.parent
= base
._port
_map
183 # mark base as having been subclassed
184 base
._instantiated
= True
186 # Now process the _value_dict items. They could be defining
187 # new (or overriding existing) parameters or ports, setting
188 # class keywords (e.g., 'abstract'), or setting parameter
189 # values or port bindings. The first 3 can only be set when
190 # the class is defined, so we handle them here. The others
191 # can be set later too, so just emulate that by calling
193 for key
,val
in cls
._value
_dict
.items():
195 if isinstance(val
, ParamDesc
):
196 cls
._new
_param
(key
, val
)
199 elif isinstance(val
, Port
):
200 cls
._ports
[key
] = val
202 # init-time-only keywords
203 elif cls
.init_keywords
.has_key(key
):
204 cls
._set
_keyword
(key
, val
, cls
.init_keywords
[key
])
206 # default: use normal path (ends up in __setattr__)
208 setattr(cls
, key
, val
)
210 cls
.cxx_type
= cls
.type + '*'
211 # A forward class declaration is sufficient since we are just
212 # declaring a pointer.
213 cls
.cxx_predecls
= ['class %s;' % cls
.type]
214 cls
.swig_predecls
= cls
.cxx_predecls
216 def _set_keyword(cls
, keyword
, val
, kwtype
):
217 if not isinstance(val
, kwtype
):
218 raise TypeError, 'keyword %s has bad type %s (expecting %s)' % \
219 (keyword
, type(val
), kwtype
)
220 if isinstance(val
, types
.FunctionType
):
221 val
= classmethod(val
)
222 type.__setattr
__(cls
, keyword
, val
)
224 def _new_param(cls
, name
, pdesc
):
225 # each param desc should be uniquely assigned to one variable
226 assert(not hasattr(pdesc
, 'name'))
228 cls
._params
[name
] = pdesc
229 if hasattr(pdesc
, 'default'):
230 setattr(cls
, name
, pdesc
.default
)
232 # Set attribute (called on foo.attr = value when foo is an
233 # instance of class cls).
234 def __setattr__(cls
, attr
, value
):
235 # normal processing for private attributes
236 if attr
.startswith('_'):
237 type.__setattr
__(cls
, attr
, value
)
240 if cls
.keywords
.has_key(attr
):
241 cls
._set
_keyword
(attr
, value
, cls
.keywords
[attr
])
244 if cls
._ports
.has_key(attr
):
245 self
._ports
[attr
].connect(self
, attr
, value
)
248 if isSimObjectOrSequence(value
) and cls
._instantiated
:
249 raise RuntimeError, \
250 "cannot set SimObject parameter '%s' after\n" \
251 " class %s has been instantiated or subclassed" \
252 % (attr
, cls
.__name
__)
255 param
= cls
._params
.get(attr
, None)
258 cls
._values
[attr
] = param
.convert(value
)
260 msg
= "%s\nError setting param %s.%s to %s\n" % \
261 (e
, cls
.__name
__, attr
, value
)
264 elif isSimObjectOrSequence(value
):
265 # if RHS is a SimObject, it's an implicit child assignment
266 cls
._values
[attr
] = value
268 raise AttributeError, \
269 "Class %s has no parameter \'%s\'" % (cls
.__name
__, attr
)
271 def __getattr__(cls
, attr
):
272 if cls
._values
.has_key(attr
):
273 return cls
._values
[attr
]
275 raise AttributeError, \
276 "object '%s' has no attribute '%s'" % (cls
.__name
__, attr
)
282 code
= "#ifndef __PARAMS__%s\n#define __PARAMS__%s\n\n" % (cls
, cls
)
284 if str(cls
) != 'SimObject':
285 base
= cls
.__bases
__[0].type
289 # The 'dict' attribute restricts us to the params declared in
290 # the object itself, not including inherited params (which
291 # will also be inherited from the base class's param struct
293 params
= cls
._params
.dict.values()
295 ptypes
= [p
.ptype
for p
in params
]
297 print cls
, p
, p
.ptype_str
301 # get a list of lists of predeclaration lines
302 predecls
= [p
.cxx_predecls() for p
in params
]
304 predecls
= reduce(lambda x
,y
:x
+y
, predecls
, [])
305 # remove redundant lines
308 if pd
not in predecls2
:
311 code
+= "\n".join(predecls2
)
315 code
+= '#include "params/%s.hh"\n\n' % base
317 # Generate declarations for locally defined enumerations.
318 enum_ptypes
= [t
for t
in ptypes
if issubclass(t
, Enum
)]
320 code
+= "\n".join([t
.cxx_decl() for t
in enum_ptypes
])
323 # now generate the actual param struct
324 code
+= "struct %sParams" % cls
326 code
+= " : public %sParams" % base
328 decls
= [p
.cxx_decl() for p
in params
]
330 code
+= "".join([" %s\n" % d
for d
in decls
])
333 # close #ifndef __PARAMS__* guard
339 code
= '%%module %sParams\n' % cls
341 if str(cls
) != 'SimObject':
342 base
= cls
.__bases
__[0].type
346 # The 'dict' attribute restricts us to the params declared in
347 # the object itself, not including inherited params (which
348 # will also be inherited from the base class's param struct
350 params
= cls
._params
.dict.values()
351 ptypes
= [p
.ptype
for p
in params
]
353 # get a list of lists of predeclaration lines
354 predecls
= [p
.swig_predecls() for p
in params
]
356 predecls
= reduce(lambda x
,y
:x
+y
, predecls
, [])
357 # remove redundant lines
360 if pd
not in predecls2
:
363 code
+= "\n".join(predecls2
)
367 code
+= '%%import "python/m5/swig/%sParams.i"\n\n' % base
370 code
+= '#include "params/%s.hh"\n' % cls
372 code
+= '%%include "params/%s.hh"\n\n' % cls
376 # The SimObject class is the root of the special hierarchy. Most of
377 # the code in this class deals with the configuration hierarchy itself
378 # (parent/child node relationships).
379 class SimObject(object):
380 # Specify metaclass. Any class inheriting from SimObject will
381 # get this metaclass.
382 __metaclass__
= MetaSimObject
385 name
= Param
.String("Object name")
387 # Initialize new instance. For objects with SimObject-valued
388 # children, we need to recursively clone the classes represented
389 # by those param values as well in a consistent "deep copy"-style
390 # fashion. That is, we want to make sure that each instance is
391 # cloned only once, and that if there are multiple references to
392 # the same original object, we end up with the corresponding
393 # cloned references all pointing to the same cloned instance.
394 def __init__(self
, **kwargs
):
395 ancestor
= kwargs
.get('_ancestor')
396 memo_dict
= kwargs
.get('_memo')
397 if memo_dict
is None:
398 # prepare to memoize any recursively instantiated objects
401 # memoize me now to avoid problems with recursive calls
402 memo_dict
[ancestor
] = self
405 ancestor
= self
.__class
__
406 ancestor
._instantiated
= True
408 # initialize required attributes
411 self
._ccObject
= None # pointer to C++ object
412 self
._instantiated
= False # really "cloned"
414 # Inherit parameter values from class using multidict so
415 # individual value settings can be overridden.
416 self
._values
= multidict(ancestor
._values
)
417 # clone SimObject-valued parameters
418 for key
,val
in ancestor
._values
.iteritems():
420 setattr(self
, key
, val(_memo
=memo_dict
))
421 elif isSimObjectSequence(val
) and len(val
):
422 setattr(self
, key
, [ v(_memo
=memo_dict
) for v
in val
])
423 # clone port references. no need to use a multidict here
424 # since we will be creating new references for all ports.
426 for key
,val
in ancestor
._port
_map
.iteritems():
427 self
._port
_map
[key
] = applyOrMap(val
, 'clone', memo_dict
)
428 # apply attribute assignments from keyword args, if any
429 for key
,val
in kwargs
.iteritems():
430 setattr(self
, key
, val
)
432 # "Clone" the current instance by creating another instance of
433 # this instance's class, but that inherits its parameter values
434 # and port mappings from the current instance. If we're in a
435 # "deep copy" recursive clone, check the _memo dict to see if
436 # we've already cloned this instance.
437 def __call__(self
, **kwargs
):
438 memo_dict
= kwargs
.get('_memo')
439 if memo_dict
is None:
440 # no memo_dict: must be top-level clone operation.
441 # this is only allowed at the root of a hierarchy
443 raise RuntimeError, "attempt to clone object %s " \
444 "not at the root of a tree (parent = %s)" \
445 % (self
, self
._parent
)
446 # create a new dict and use that.
448 kwargs
['_memo'] = memo_dict
449 elif memo_dict
.has_key(self
):
450 # clone already done & memoized
451 return memo_dict
[self
]
452 return self
.__class
__(_ancestor
= self
, **kwargs
)
454 def __getattr__(self
, attr
):
455 if self
._ports
.has_key(attr
):
456 # return reference that can be assigned to another port
458 return self
._ports
[attr
].makeRef(self
, attr
)
460 if self
._values
.has_key(attr
):
461 return self
._values
[attr
]
463 raise AttributeError, "object '%s' has no attribute '%s'" \
464 % (self
.__class
__.__name
__, attr
)
466 # Set attribute (called on foo.attr = value when foo is an
467 # instance of class cls).
468 def __setattr__(self
, attr
, value
):
469 # normal processing for private attributes
470 if attr
.startswith('_'):
471 object.__setattr
__(self
, attr
, value
)
474 if self
._ports
.has_key(attr
):
475 # set up port connection
476 self
._ports
[attr
].connect(self
, attr
, value
)
479 if isSimObjectOrSequence(value
) and self
._instantiated
:
480 raise RuntimeError, \
481 "cannot set SimObject parameter '%s' after\n" \
482 " instance been cloned %s" % (attr
, `self`
)
484 # must be SimObject param
485 param
= self
._params
.get(attr
, None)
488 value
= param
.convert(value
)
490 msg
= "%s\nError setting param %s.%s to %s\n" % \
491 (e
, self
.__class
__.__name
__, attr
, value
)
494 elif isSimObjectOrSequence(value
):
497 raise AttributeError, "Class %s has no parameter %s" \
498 % (self
.__class
__.__name
__, attr
)
500 # clear out old child with this name, if any
501 self
.clear_child(attr
)
503 if isSimObject(value
):
504 value
.set_path(self
, attr
)
505 elif isSimObjectSequence(value
):
506 value
= SimObjVector(value
)
507 [v
.set_path(self
, "%s%d" % (attr
, i
)) for i
,v
in enumerate(value
)]
509 self
._values
[attr
] = value
511 # this hack allows tacking a '[0]' onto parameters that may or may
512 # not be vectors, and always getting the first element (e.g. cpus)
513 def __getitem__(self
, key
):
516 raise TypeError, "Non-zero index '%s' to SimObject" % key
518 # clear out children with given name, even if it's a vector
519 def clear_child(self
, name
):
520 if not self
._children
.has_key(name
):
522 child
= self
._children
[name
]
523 if isinstance(child
, SimObjVector
):
524 for i
in xrange(len(child
)):
525 del self
._children
["s%d" % (name
, i
)]
526 del self
._children
[name
]
528 def add_child(self
, name
, value
):
529 self
._children
[name
] = value
531 def set_path(self
, parent
, name
):
533 self
._parent
= parent
535 parent
.add_child(name
, self
)
540 ppath
= self
._parent
.path()
543 return ppath
+ "." + self
._name
551 def find_any(self
, ptype
):
552 if isinstance(self
, ptype
):
556 for child
in self
._children
.itervalues():
557 if isinstance(child
, ptype
):
558 if found_obj
!= None and child
!= found_obj
:
559 raise AttributeError, \
560 'parent.any matched more than one: %s %s' % \
561 (found_obj
.path
, child
.path
)
564 for pname
,pdesc
in self
._params
.iteritems():
565 if issubclass(pdesc
.ptype
, ptype
):
566 match_obj
= self
._values
[pname
]
567 if found_obj
!= None and found_obj
!= match_obj
:
568 raise AttributeError, \
569 'parent.any matched more than one: %s' % obj
.path
570 found_obj
= match_obj
571 return found_obj
, found_obj
!= None
573 def unproxy(self
, base
):
577 print '[' + self
.path() + ']' # .ini section header
579 instanceDict
[self
.path()] = self
581 if hasattr(self
, 'type') and not isinstance(self
, ParamContext
):
582 print 'type=%s' % self
.type
584 child_names
= self
._children
.keys()
586 np_child_names
= [c
for c
in child_names \
587 if not isinstance(self
._children
[c
], ParamContext
)]
588 if len(np_child_names
):
589 print 'children=%s' % ' '.join(np_child_names
)
591 param_names
= self
._params
.keys()
593 for param
in param_names
:
594 value
= self
._values
.get(param
, None)
596 if proxy
.isproxy(value
):
598 value
= value
.unproxy(self
)
600 print >> sys
.stderr
, \
601 "Error in unproxying param '%s' of %s" % \
604 setattr(self
, param
, value
)
605 print '%s=%s' % (param
, self
._values
[param
].ini_str())
607 print # blank line between objects
609 for child
in child_names
:
610 self
._children
[child
].print_ini()
612 # Call C++ to create C++ object corresponding to this object and
613 # (recursively) all its children
614 def createCCObject(self
):
615 self
.getCCObject() # force creation
616 for child
in self
._children
.itervalues():
617 child
.createCCObject()
619 # Get C++ object corresponding to this object, calling C++ if
620 # necessary to construct it. Does *not* recursively create
622 def getCCObject(self
):
623 if not self
._ccObject
:
624 self
._ccObject
= -1 # flag to catch cycles in recursion
625 self
._ccObject
= cc_main
.createSimObject(self
.path())
626 elif self
._ccObject
== -1:
627 raise RuntimeError, "%s: recursive call to getCCObject()" \
629 return self
._ccObject
631 # Create C++ port connections corresponding to the connections in
632 # _port_map (& recursively for all children)
633 def connectPorts(self
):
634 for portRef
in self
._port
_map
.itervalues():
635 applyOrMap(portRef
, 'ccConnect')
636 for child
in self
._children
.itervalues():
639 def startDrain(self
, drain_event
, recursive
):
641 # ParamContexts don't serialize
642 if isinstance(self
, SimObject
) and not isinstance(self
, ParamContext
):
643 count
+= self
._ccObject
.drain(drain_event
)
645 for child
in self
._children
.itervalues():
646 count
+= child
.startDrain(drain_event
, True)
650 if isinstance(self
, SimObject
) and not isinstance(self
, ParamContext
):
651 self
._ccObject
.resume()
652 for child
in self
._children
.itervalues():
655 def changeTiming(self
, mode
):
656 if isinstance(self
, System
):
657 self
._ccObject
.setMemoryMode(mode
)
658 for child
in self
._children
.itervalues():
659 child
.changeTiming(mode
)
661 def takeOverFrom(self
, old_cpu
):
662 cpu_ptr
= cc_main
.convertToBaseCPUPtr(old_cpu
._ccObject
)
663 self
._ccObject
.takeOverFrom(cpu_ptr
)
665 # generate output file for 'dot' to display as a pretty graph.
666 # this code is currently broken.
667 def outputDot(self
, dot
):
668 label
= "{%s|" % self
.path
669 if isSimObject(self
.realtype
):
670 label
+= '%s|' % self
.type
673 # instantiate children in same order they were added for
674 # backward compatibility (else we can end up with cpu1
676 for c
in self
.children
:
677 dot
.add_edge(pydot
.Edge(self
.path
,c
.path
, style
="bold"))
680 for param
in self
.params
:
682 if param
.value
is None:
683 raise AttributeError, 'Parameter with no value'
686 string
= param
.string(value
)
688 msg
= 'exception in %s:%s\n%s' % (self
.name
, param
.name
, e
)
692 if isSimObject(param
.ptype
) and string
!= "Null":
693 simobjs
.append(string
)
695 label
+= '%s = %s\\n' % (param
.name
, string
)
698 label
+= "|<%s> %s" % (so
, so
)
699 dot
.add_edge(pydot
.Edge("%s:%s" % (self
.path
, so
), so
,
702 dot
.add_node(pydot
.Node(self
.path
,shape
="Mrecord",label
=label
))
704 # recursively dump out children
705 for c
in self
.children
:
708 class ParamContext(SimObject
):
711 # Function to provide to C++ so it can look up instances based on paths
712 def resolveSimObject(name
):
713 obj
= instanceDict
[name
]
714 return obj
.getCCObject()
716 # __all__ defines the list of symbols that get exported when
717 # 'from config import *' is invoked. Try to keep this reasonably
718 # short to avoid polluting other namespaces.
719 __all__
= ['SimObject', 'ParamContext']
722 # see comment on imports at end of __init__.py.