SimObject.py (3624:aaba7e06ece4) SimObject.py (4081:80f1e833d118)
1# Copyright (c) 2004-2006 The Regents of The University of Michigan
2# All rights reserved.
3#
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.
14#
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.
26#
27# Authors: Steve Reinhardt
28# Nathan Binkert
29
30import sys, types
31
32from util import *
33from multidict import multidict
34
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.
39def isSimObject(value):
40 return isinstance(value, SimObject)
41
42def isSimObjectClass(value):
43 return issubclass(value, SimObject)
44
45def isSimObjectSequence(value):
46 if not isinstance(value, (list, tuple)) or len(value) == 0:
47 return False
48
49 for val in value:
50 if not isNullPointer(val) and not isSimObject(val):
51 return False
52
53 return True
54
55def isSimObjectOrSequence(value):
56 return isSimObject(value) or isSimObjectSequence(value)
57
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)...
61from params import *
62# There are a few things we need that aren't in params.__all__ since
63# normal users don't need them
64from params import ParamDesc, isNullPointer, SimObjVector
65
66noDot = False
67try:
68 import pydot
69except:
70 noDot = True
71
72#####################################################################
73#
74# M5 Python Configuration Utility
75#
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.
81#
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:
91#
92# cache = BaseCache(size='64KB')
93# cache.hit_latency = 3
94# cache.assoc = 8
95#
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.
105#
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.
109#
110#####################################################################
111
112# dict to look up SimObjects based on path
113instanceDict = {}
114
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).
119class 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 }
128
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__.
138 cls_dict = {}
139 value_dict = {}
140 for key,val in dict.items():
141 if key.startswith('_') or isinstance(val, (types.FunctionType,
142 types.TypeType)):
143 cls_dict[key] = val
144 else:
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)
149
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)
155
156 # initialize required attributes
157
158 # class-only attributes
159 cls._params = multidict() # param descriptions
160 cls._ports = multidict() # port descriptions
161
162 # class or instance attributes
163 cls._values = multidict() # param values
164 cls._port_refs = multidict() # port ref objects
165 cls._instantiated = False # really instantiated, cloned, or subclassed
166
167 # We don't support multiple inheritance. If you want to, you
168 # must fix multidict to deal with it properly.
169 if len(bases) > 1:
170 raise TypeError, "SimObjects do not support multiple inheritance"
171
172 base = bases[0]
173
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_refs.parent = base._port_refs
183 # mark base as having been subclassed
184 base._instantiated = True
185
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
192 # setattr().
193 for key,val in cls._value_dict.items():
194 # param descriptions
195 if isinstance(val, ParamDesc):
196 cls._new_param(key, val)
197
198 # port objects
199 elif isinstance(val, Port):
200 cls._new_port(key, val)
201
202 # init-time-only keywords
203 elif cls.init_keywords.has_key(key):
204 cls._set_keyword(key, val, cls.init_keywords[key])
205
206 # default: use normal path (ends up in __setattr__)
207 else:
208 setattr(cls, key, val)
209
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
215
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)
223
224 def _new_param(cls, name, pdesc):
225 # each param desc should be uniquely assigned to one variable
226 assert(not hasattr(pdesc, 'name'))
227 pdesc.name = name
228 cls._params[name] = pdesc
229 if hasattr(pdesc, 'default'):
230 cls._set_param(name, pdesc.default, pdesc)
231
232 def _set_param(cls, name, value, param):
233 assert(param.name == name)
234 try:
235 cls._values[name] = param.convert(value)
236 except Exception, e:
237 msg = "%s\nError setting param %s.%s to %s\n" % \
238 (e, cls.__name__, name, value)
239 e.args = (msg, )
240 raise
241
242 def _new_port(cls, name, port):
243 # each port should be uniquely assigned to one variable
244 assert(not hasattr(port, 'name'))
245 port.name = name
246 cls._ports[name] = port
247 if hasattr(port, 'default'):
248 cls._cls_get_port_ref(name).connect(port.default)
249
250 # same as _get_port_ref, effectively, but for classes
251 def _cls_get_port_ref(cls, attr):
252 # Return reference that can be assigned to another port
253 # via __setattr__. There is only ever one reference
254 # object per port, but we create them lazily here.
255 ref = cls._port_refs.get(attr)
256 if not ref:
257 ref = cls._ports[attr].makeRef(cls)
258 cls._port_refs[attr] = ref
259 return ref
260
261 # Set attribute (called on foo.attr = value when foo is an
262 # instance of class cls).
263 def __setattr__(cls, attr, value):
264 # normal processing for private attributes
265 if attr.startswith('_'):
266 type.__setattr__(cls, attr, value)
267 return
268
269 if cls.keywords.has_key(attr):
270 cls._set_keyword(attr, value, cls.keywords[attr])
271 return
272
273 if cls._ports.has_key(attr):
274 cls._cls_get_port_ref(attr).connect(value)
275 return
276
277 if isSimObjectOrSequence(value) and cls._instantiated:
278 raise RuntimeError, \
279 "cannot set SimObject parameter '%s' after\n" \
280 " class %s has been instantiated or subclassed" \
281 % (attr, cls.__name__)
282
283 # check for param
284 param = cls._params.get(attr)
285 if param:
286 cls._set_param(attr, value, param)
287 return
288
289 if isSimObjectOrSequence(value):
290 # If RHS is a SimObject, it's an implicit child assignment.
291 # Classes don't have children, so we just put this object
292 # in _values; later, each instance will do a 'setattr(self,
293 # attr, _values[attr])' in SimObject.__init__ which will
294 # add this object as a child.
295 cls._values[attr] = value
296 return
297
298 # no valid assignment... raise exception
299 raise AttributeError, \
300 "Class %s has no parameter \'%s\'" % (cls.__name__, attr)
301
302 def __getattr__(cls, attr):
303 if cls._values.has_key(attr):
304 return cls._values[attr]
305
306 raise AttributeError, \
307 "object '%s' has no attribute '%s'" % (cls.__name__, attr)
308
309 def __str__(cls):
310 return cls.__name__
311
312 def cxx_decl(cls):
313 code = "#ifndef __PARAMS__%s\n#define __PARAMS__%s\n\n" % (cls, cls)
314
315 if str(cls) != 'SimObject':
316 base = cls.__bases__[0].type
317 else:
318 base = None
319
320 # The 'dict' attribute restricts us to the params declared in
321 # the object itself, not including inherited params (which
322 # will also be inherited from the base class's param struct
323 # here).
324 params = cls._params.dict.values()
325 try:
326 ptypes = [p.ptype for p in params]
327 except:
328 print cls, p, p.ptype_str
329 print params
330 raise
331
332 # get a list of lists of predeclaration lines
333 predecls = [p.cxx_predecls() for p in params]
334 # flatten
335 predecls = reduce(lambda x,y:x+y, predecls, [])
336 # remove redundant lines
337 predecls2 = []
338 for pd in predecls:
339 if pd not in predecls2:
340 predecls2.append(pd)
341 predecls2.sort()
342 code += "\n".join(predecls2)
343 code += "\n\n";
344
345 if base:
346 code += '#include "params/%s.hh"\n\n' % base
347
348 # Generate declarations for locally defined enumerations.
349 enum_ptypes = [t for t in ptypes if issubclass(t, Enum)]
350 if enum_ptypes:
351 code += "\n".join([t.cxx_decl() for t in enum_ptypes])
352 code += "\n\n"
353
354 # now generate the actual param struct
355 code += "struct %sParams" % cls
356 if base:
357 code += " : public %sParams" % base
358 code += " {\n"
359 decls = [p.cxx_decl() for p in params]
360 decls.sort()
361 code += "".join([" %s\n" % d for d in decls])
362 code += "};\n"
363
364 # close #ifndef __PARAMS__* guard
365 code += "\n#endif\n"
366 return code
367
368 def swig_decl(cls):
369
370 code = '%%module %sParams\n' % cls
371
372 if str(cls) != 'SimObject':
373 base = cls.__bases__[0].type
374 else:
375 base = None
376
377 # The 'dict' attribute restricts us to the params declared in
378 # the object itself, not including inherited params (which
379 # will also be inherited from the base class's param struct
380 # here).
381 params = cls._params.dict.values()
382 ptypes = [p.ptype for p in params]
383
384 # get a list of lists of predeclaration lines
385 predecls = [p.swig_predecls() for p in params]
386 # flatten
387 predecls = reduce(lambda x,y:x+y, predecls, [])
388 # remove redundant lines
389 predecls2 = []
390 for pd in predecls:
391 if pd not in predecls2:
392 predecls2.append(pd)
393 predecls2.sort()
394 code += "\n".join(predecls2)
395 code += "\n\n";
396
397 if base:
398 code += '%%import "python/m5/swig/%sParams.i"\n\n' % base
399
400 code += '%{\n'
401 code += '#include "params/%s.hh"\n' % cls
402 code += '%}\n\n'
403 code += '%%include "params/%s.hh"\n\n' % cls
404
405 return code
406
407# The SimObject class is the root of the special hierarchy. Most of
408# the code in this class deals with the configuration hierarchy itself
409# (parent/child node relationships).
410class SimObject(object):
411 # Specify metaclass. Any class inheriting from SimObject will
412 # get this metaclass.
413 __metaclass__ = MetaSimObject
414 type = 'SimObject'
415
416 name = Param.String("Object name")
417
418 # Initialize new instance. For objects with SimObject-valued
419 # children, we need to recursively clone the classes represented
420 # by those param values as well in a consistent "deep copy"-style
421 # fashion. That is, we want to make sure that each instance is
422 # cloned only once, and that if there are multiple references to
423 # the same original object, we end up with the corresponding
424 # cloned references all pointing to the same cloned instance.
425 def __init__(self, **kwargs):
426 ancestor = kwargs.get('_ancestor')
427 memo_dict = kwargs.get('_memo')
428 if memo_dict is None:
429 # prepare to memoize any recursively instantiated objects
430 memo_dict = {}
431 elif ancestor:
432 # memoize me now to avoid problems with recursive calls
433 memo_dict[ancestor] = self
434
435 if not ancestor:
436 ancestor = self.__class__
437 ancestor._instantiated = True
438
439 # initialize required attributes
440 self._parent = None
441 self._children = {}
442 self._ccObject = None # pointer to C++ object
443 self._instantiated = False # really "cloned"
444
445 # Inherit parameter values from class using multidict so
446 # individual value settings can be overridden.
447 self._values = multidict(ancestor._values)
448 # clone SimObject-valued parameters
449 for key,val in ancestor._values.iteritems():
450 if isSimObject(val):
451 setattr(self, key, val(_memo=memo_dict))
452 elif isSimObjectSequence(val) and len(val):
453 setattr(self, key, [ v(_memo=memo_dict) for v in val ])
454 # clone port references. no need to use a multidict here
455 # since we will be creating new references for all ports.
456 self._port_refs = {}
457 for key,val in ancestor._port_refs.iteritems():
458 self._port_refs[key] = val.clone(self, memo_dict)
459 # apply attribute assignments from keyword args, if any
460 for key,val in kwargs.iteritems():
461 setattr(self, key, val)
462
463 # "Clone" the current instance by creating another instance of
464 # this instance's class, but that inherits its parameter values
465 # and port mappings from the current instance. If we're in a
466 # "deep copy" recursive clone, check the _memo dict to see if
467 # we've already cloned this instance.
468 def __call__(self, **kwargs):
469 memo_dict = kwargs.get('_memo')
470 if memo_dict is None:
471 # no memo_dict: must be top-level clone operation.
472 # this is only allowed at the root of a hierarchy
473 if self._parent:
474 raise RuntimeError, "attempt to clone object %s " \
475 "not at the root of a tree (parent = %s)" \
476 % (self, self._parent)
477 # create a new dict and use that.
478 memo_dict = {}
479 kwargs['_memo'] = memo_dict
480 elif memo_dict.has_key(self):
481 # clone already done & memoized
482 return memo_dict[self]
483 return self.__class__(_ancestor = self, **kwargs)
484
485 def _get_port_ref(self, attr):
486 # Return reference that can be assigned to another port
487 # via __setattr__. There is only ever one reference
488 # object per port, but we create them lazily here.
489 ref = self._port_refs.get(attr)
490 if not ref:
491 ref = self._ports[attr].makeRef(self)
492 self._port_refs[attr] = ref
493 return ref
494
495 def __getattr__(self, attr):
496 if self._ports.has_key(attr):
497 return self._get_port_ref(attr)
498
499 if self._values.has_key(attr):
500 return self._values[attr]
501
502 raise AttributeError, "object '%s' has no attribute '%s'" \
503 % (self.__class__.__name__, attr)
504
505 # Set attribute (called on foo.attr = value when foo is an
506 # instance of class cls).
507 def __setattr__(self, attr, value):
508 # normal processing for private attributes
509 if attr.startswith('_'):
510 object.__setattr__(self, attr, value)
511 return
512
513 if self._ports.has_key(attr):
514 # set up port connection
515 self._get_port_ref(attr).connect(value)
516 return
517
518 if isSimObjectOrSequence(value) and self._instantiated:
519 raise RuntimeError, \
520 "cannot set SimObject parameter '%s' after\n" \
521 " instance been cloned %s" % (attr, `self`)
522
523 # must be SimObject param
524 param = self._params.get(attr)
525 if param:
526 try:
527 value = param.convert(value)
528 except Exception, e:
529 msg = "%s\nError setting param %s.%s to %s\n" % \
530 (e, self.__class__.__name__, attr, value)
531 e.args = (msg, )
532 raise
533 self._set_child(attr, value)
534 return
535
536 if isSimObjectOrSequence(value):
537 self._set_child(attr, value)
538 return
539
540 # no valid assignment... raise exception
541 raise AttributeError, "Class %s has no parameter %s" \
542 % (self.__class__.__name__, attr)
543
544
545 # this hack allows tacking a '[0]' onto parameters that may or may
546 # not be vectors, and always getting the first element (e.g. cpus)
547 def __getitem__(self, key):
548 if key == 0:
549 return self
550 raise TypeError, "Non-zero index '%s' to SimObject" % key
551
552 # clear out children with given name, even if it's a vector
553 def clear_child(self, name):
554 if not self._children.has_key(name):
555 return
556 child = self._children[name]
557 if isinstance(child, SimObjVector):
558 for i in xrange(len(child)):
559 del self._children["s%d" % (name, i)]
560 del self._children[name]
561
562 def add_child(self, name, value):
563 self._children[name] = value
564
565 def _maybe_set_parent(self, parent, name):
566 if not self._parent:
567 self._parent = parent
568 self._name = name
569 parent.add_child(name, self)
570
571 def _set_child(self, attr, value):
572 # if RHS is a SimObject, it's an implicit child assignment
573 # clear out old child with this name, if any
574 self.clear_child(attr)
575
576 if isSimObject(value):
577 value._maybe_set_parent(self, attr)
578 elif isSimObjectSequence(value):
579 value = SimObjVector(value)
580 [v._maybe_set_parent(self, "%s%d" % (attr, i))
581 for i,v in enumerate(value)]
582
583 self._values[attr] = value
584
585 def path(self):
586 if not self._parent:
587 return 'root'
588 ppath = self._parent.path()
589 if ppath == 'root':
590 return self._name
591 return ppath + "." + self._name
592
593 def __str__(self):
594 return self.path()
595
596 def ini_str(self):
597 return self.path()
598
599 def find_any(self, ptype):
600 if isinstance(self, ptype):
601 return self, True
602
603 found_obj = None
604 for child in self._children.itervalues():
605 if isinstance(child, ptype):
606 if found_obj != None and child != found_obj:
607 raise AttributeError, \
608 'parent.any matched more than one: %s %s' % \
609 (found_obj.path, child.path)
610 found_obj = child
611 # search param space
612 for pname,pdesc in self._params.iteritems():
613 if issubclass(pdesc.ptype, ptype):
614 match_obj = self._values[pname]
615 if found_obj != None and found_obj != match_obj:
616 raise AttributeError, \
617 'parent.any matched more than one: %s' % obj.path
618 found_obj = match_obj
619 return found_obj, found_obj != None
620
621 def unproxy(self, base):
622 return self
623
624 def unproxy_all(self):
625 for param in self._params.iterkeys():
626 value = self._values.get(param)
627 if value != None and proxy.isproxy(value):
628 try:
629 value = value.unproxy(self)
630 except:
631 print "Error in unproxying param '%s' of %s" % \
632 (param, self.path())
633 raise
634 setattr(self, param, value)
635
636 # Unproxy ports in sorted order so that 'append' operations on
637 # vector ports are done in a deterministic fashion.
638 port_names = self._ports.keys()
639 port_names.sort()
640 for port_name in port_names:
641 port = self._port_refs.get(port_name)
642 if port != None:
643 port.unproxy(self)
644
645 # Unproxy children in sorted order for determinism also.
646 child_names = self._children.keys()
647 child_names.sort()
648 for child in child_names:
649 self._children[child].unproxy_all()
650
651 def print_ini(self):
652 print '[' + self.path() + ']' # .ini section header
653
654 instanceDict[self.path()] = self
655
1# Copyright (c) 2004-2006 The Regents of The University of Michigan
2# All rights reserved.
3#
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.
14#
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.
26#
27# Authors: Steve Reinhardt
28# Nathan Binkert
29
30import sys, types
31
32from util import *
33from multidict import multidict
34
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.
39def isSimObject(value):
40 return isinstance(value, SimObject)
41
42def isSimObjectClass(value):
43 return issubclass(value, SimObject)
44
45def isSimObjectSequence(value):
46 if not isinstance(value, (list, tuple)) or len(value) == 0:
47 return False
48
49 for val in value:
50 if not isNullPointer(val) and not isSimObject(val):
51 return False
52
53 return True
54
55def isSimObjectOrSequence(value):
56 return isSimObject(value) or isSimObjectSequence(value)
57
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)...
61from params import *
62# There are a few things we need that aren't in params.__all__ since
63# normal users don't need them
64from params import ParamDesc, isNullPointer, SimObjVector
65
66noDot = False
67try:
68 import pydot
69except:
70 noDot = True
71
72#####################################################################
73#
74# M5 Python Configuration Utility
75#
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.
81#
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:
91#
92# cache = BaseCache(size='64KB')
93# cache.hit_latency = 3
94# cache.assoc = 8
95#
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.
105#
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.
109#
110#####################################################################
111
112# dict to look up SimObjects based on path
113instanceDict = {}
114
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).
119class 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 }
128
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__.
138 cls_dict = {}
139 value_dict = {}
140 for key,val in dict.items():
141 if key.startswith('_') or isinstance(val, (types.FunctionType,
142 types.TypeType)):
143 cls_dict[key] = val
144 else:
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)
149
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)
155
156 # initialize required attributes
157
158 # class-only attributes
159 cls._params = multidict() # param descriptions
160 cls._ports = multidict() # port descriptions
161
162 # class or instance attributes
163 cls._values = multidict() # param values
164 cls._port_refs = multidict() # port ref objects
165 cls._instantiated = False # really instantiated, cloned, or subclassed
166
167 # We don't support multiple inheritance. If you want to, you
168 # must fix multidict to deal with it properly.
169 if len(bases) > 1:
170 raise TypeError, "SimObjects do not support multiple inheritance"
171
172 base = bases[0]
173
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_refs.parent = base._port_refs
183 # mark base as having been subclassed
184 base._instantiated = True
185
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
192 # setattr().
193 for key,val in cls._value_dict.items():
194 # param descriptions
195 if isinstance(val, ParamDesc):
196 cls._new_param(key, val)
197
198 # port objects
199 elif isinstance(val, Port):
200 cls._new_port(key, val)
201
202 # init-time-only keywords
203 elif cls.init_keywords.has_key(key):
204 cls._set_keyword(key, val, cls.init_keywords[key])
205
206 # default: use normal path (ends up in __setattr__)
207 else:
208 setattr(cls, key, val)
209
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
215
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)
223
224 def _new_param(cls, name, pdesc):
225 # each param desc should be uniquely assigned to one variable
226 assert(not hasattr(pdesc, 'name'))
227 pdesc.name = name
228 cls._params[name] = pdesc
229 if hasattr(pdesc, 'default'):
230 cls._set_param(name, pdesc.default, pdesc)
231
232 def _set_param(cls, name, value, param):
233 assert(param.name == name)
234 try:
235 cls._values[name] = param.convert(value)
236 except Exception, e:
237 msg = "%s\nError setting param %s.%s to %s\n" % \
238 (e, cls.__name__, name, value)
239 e.args = (msg, )
240 raise
241
242 def _new_port(cls, name, port):
243 # each port should be uniquely assigned to one variable
244 assert(not hasattr(port, 'name'))
245 port.name = name
246 cls._ports[name] = port
247 if hasattr(port, 'default'):
248 cls._cls_get_port_ref(name).connect(port.default)
249
250 # same as _get_port_ref, effectively, but for classes
251 def _cls_get_port_ref(cls, attr):
252 # Return reference that can be assigned to another port
253 # via __setattr__. There is only ever one reference
254 # object per port, but we create them lazily here.
255 ref = cls._port_refs.get(attr)
256 if not ref:
257 ref = cls._ports[attr].makeRef(cls)
258 cls._port_refs[attr] = ref
259 return ref
260
261 # Set attribute (called on foo.attr = value when foo is an
262 # instance of class cls).
263 def __setattr__(cls, attr, value):
264 # normal processing for private attributes
265 if attr.startswith('_'):
266 type.__setattr__(cls, attr, value)
267 return
268
269 if cls.keywords.has_key(attr):
270 cls._set_keyword(attr, value, cls.keywords[attr])
271 return
272
273 if cls._ports.has_key(attr):
274 cls._cls_get_port_ref(attr).connect(value)
275 return
276
277 if isSimObjectOrSequence(value) and cls._instantiated:
278 raise RuntimeError, \
279 "cannot set SimObject parameter '%s' after\n" \
280 " class %s has been instantiated or subclassed" \
281 % (attr, cls.__name__)
282
283 # check for param
284 param = cls._params.get(attr)
285 if param:
286 cls._set_param(attr, value, param)
287 return
288
289 if isSimObjectOrSequence(value):
290 # If RHS is a SimObject, it's an implicit child assignment.
291 # Classes don't have children, so we just put this object
292 # in _values; later, each instance will do a 'setattr(self,
293 # attr, _values[attr])' in SimObject.__init__ which will
294 # add this object as a child.
295 cls._values[attr] = value
296 return
297
298 # no valid assignment... raise exception
299 raise AttributeError, \
300 "Class %s has no parameter \'%s\'" % (cls.__name__, attr)
301
302 def __getattr__(cls, attr):
303 if cls._values.has_key(attr):
304 return cls._values[attr]
305
306 raise AttributeError, \
307 "object '%s' has no attribute '%s'" % (cls.__name__, attr)
308
309 def __str__(cls):
310 return cls.__name__
311
312 def cxx_decl(cls):
313 code = "#ifndef __PARAMS__%s\n#define __PARAMS__%s\n\n" % (cls, cls)
314
315 if str(cls) != 'SimObject':
316 base = cls.__bases__[0].type
317 else:
318 base = None
319
320 # The 'dict' attribute restricts us to the params declared in
321 # the object itself, not including inherited params (which
322 # will also be inherited from the base class's param struct
323 # here).
324 params = cls._params.dict.values()
325 try:
326 ptypes = [p.ptype for p in params]
327 except:
328 print cls, p, p.ptype_str
329 print params
330 raise
331
332 # get a list of lists of predeclaration lines
333 predecls = [p.cxx_predecls() for p in params]
334 # flatten
335 predecls = reduce(lambda x,y:x+y, predecls, [])
336 # remove redundant lines
337 predecls2 = []
338 for pd in predecls:
339 if pd not in predecls2:
340 predecls2.append(pd)
341 predecls2.sort()
342 code += "\n".join(predecls2)
343 code += "\n\n";
344
345 if base:
346 code += '#include "params/%s.hh"\n\n' % base
347
348 # Generate declarations for locally defined enumerations.
349 enum_ptypes = [t for t in ptypes if issubclass(t, Enum)]
350 if enum_ptypes:
351 code += "\n".join([t.cxx_decl() for t in enum_ptypes])
352 code += "\n\n"
353
354 # now generate the actual param struct
355 code += "struct %sParams" % cls
356 if base:
357 code += " : public %sParams" % base
358 code += " {\n"
359 decls = [p.cxx_decl() for p in params]
360 decls.sort()
361 code += "".join([" %s\n" % d for d in decls])
362 code += "};\n"
363
364 # close #ifndef __PARAMS__* guard
365 code += "\n#endif\n"
366 return code
367
368 def swig_decl(cls):
369
370 code = '%%module %sParams\n' % cls
371
372 if str(cls) != 'SimObject':
373 base = cls.__bases__[0].type
374 else:
375 base = None
376
377 # The 'dict' attribute restricts us to the params declared in
378 # the object itself, not including inherited params (which
379 # will also be inherited from the base class's param struct
380 # here).
381 params = cls._params.dict.values()
382 ptypes = [p.ptype for p in params]
383
384 # get a list of lists of predeclaration lines
385 predecls = [p.swig_predecls() for p in params]
386 # flatten
387 predecls = reduce(lambda x,y:x+y, predecls, [])
388 # remove redundant lines
389 predecls2 = []
390 for pd in predecls:
391 if pd not in predecls2:
392 predecls2.append(pd)
393 predecls2.sort()
394 code += "\n".join(predecls2)
395 code += "\n\n";
396
397 if base:
398 code += '%%import "python/m5/swig/%sParams.i"\n\n' % base
399
400 code += '%{\n'
401 code += '#include "params/%s.hh"\n' % cls
402 code += '%}\n\n'
403 code += '%%include "params/%s.hh"\n\n' % cls
404
405 return code
406
407# The SimObject class is the root of the special hierarchy. Most of
408# the code in this class deals with the configuration hierarchy itself
409# (parent/child node relationships).
410class SimObject(object):
411 # Specify metaclass. Any class inheriting from SimObject will
412 # get this metaclass.
413 __metaclass__ = MetaSimObject
414 type = 'SimObject'
415
416 name = Param.String("Object name")
417
418 # Initialize new instance. For objects with SimObject-valued
419 # children, we need to recursively clone the classes represented
420 # by those param values as well in a consistent "deep copy"-style
421 # fashion. That is, we want to make sure that each instance is
422 # cloned only once, and that if there are multiple references to
423 # the same original object, we end up with the corresponding
424 # cloned references all pointing to the same cloned instance.
425 def __init__(self, **kwargs):
426 ancestor = kwargs.get('_ancestor')
427 memo_dict = kwargs.get('_memo')
428 if memo_dict is None:
429 # prepare to memoize any recursively instantiated objects
430 memo_dict = {}
431 elif ancestor:
432 # memoize me now to avoid problems with recursive calls
433 memo_dict[ancestor] = self
434
435 if not ancestor:
436 ancestor = self.__class__
437 ancestor._instantiated = True
438
439 # initialize required attributes
440 self._parent = None
441 self._children = {}
442 self._ccObject = None # pointer to C++ object
443 self._instantiated = False # really "cloned"
444
445 # Inherit parameter values from class using multidict so
446 # individual value settings can be overridden.
447 self._values = multidict(ancestor._values)
448 # clone SimObject-valued parameters
449 for key,val in ancestor._values.iteritems():
450 if isSimObject(val):
451 setattr(self, key, val(_memo=memo_dict))
452 elif isSimObjectSequence(val) and len(val):
453 setattr(self, key, [ v(_memo=memo_dict) for v in val ])
454 # clone port references. no need to use a multidict here
455 # since we will be creating new references for all ports.
456 self._port_refs = {}
457 for key,val in ancestor._port_refs.iteritems():
458 self._port_refs[key] = val.clone(self, memo_dict)
459 # apply attribute assignments from keyword args, if any
460 for key,val in kwargs.iteritems():
461 setattr(self, key, val)
462
463 # "Clone" the current instance by creating another instance of
464 # this instance's class, but that inherits its parameter values
465 # and port mappings from the current instance. If we're in a
466 # "deep copy" recursive clone, check the _memo dict to see if
467 # we've already cloned this instance.
468 def __call__(self, **kwargs):
469 memo_dict = kwargs.get('_memo')
470 if memo_dict is None:
471 # no memo_dict: must be top-level clone operation.
472 # this is only allowed at the root of a hierarchy
473 if self._parent:
474 raise RuntimeError, "attempt to clone object %s " \
475 "not at the root of a tree (parent = %s)" \
476 % (self, self._parent)
477 # create a new dict and use that.
478 memo_dict = {}
479 kwargs['_memo'] = memo_dict
480 elif memo_dict.has_key(self):
481 # clone already done & memoized
482 return memo_dict[self]
483 return self.__class__(_ancestor = self, **kwargs)
484
485 def _get_port_ref(self, attr):
486 # Return reference that can be assigned to another port
487 # via __setattr__. There is only ever one reference
488 # object per port, but we create them lazily here.
489 ref = self._port_refs.get(attr)
490 if not ref:
491 ref = self._ports[attr].makeRef(self)
492 self._port_refs[attr] = ref
493 return ref
494
495 def __getattr__(self, attr):
496 if self._ports.has_key(attr):
497 return self._get_port_ref(attr)
498
499 if self._values.has_key(attr):
500 return self._values[attr]
501
502 raise AttributeError, "object '%s' has no attribute '%s'" \
503 % (self.__class__.__name__, attr)
504
505 # Set attribute (called on foo.attr = value when foo is an
506 # instance of class cls).
507 def __setattr__(self, attr, value):
508 # normal processing for private attributes
509 if attr.startswith('_'):
510 object.__setattr__(self, attr, value)
511 return
512
513 if self._ports.has_key(attr):
514 # set up port connection
515 self._get_port_ref(attr).connect(value)
516 return
517
518 if isSimObjectOrSequence(value) and self._instantiated:
519 raise RuntimeError, \
520 "cannot set SimObject parameter '%s' after\n" \
521 " instance been cloned %s" % (attr, `self`)
522
523 # must be SimObject param
524 param = self._params.get(attr)
525 if param:
526 try:
527 value = param.convert(value)
528 except Exception, e:
529 msg = "%s\nError setting param %s.%s to %s\n" % \
530 (e, self.__class__.__name__, attr, value)
531 e.args = (msg, )
532 raise
533 self._set_child(attr, value)
534 return
535
536 if isSimObjectOrSequence(value):
537 self._set_child(attr, value)
538 return
539
540 # no valid assignment... raise exception
541 raise AttributeError, "Class %s has no parameter %s" \
542 % (self.__class__.__name__, attr)
543
544
545 # this hack allows tacking a '[0]' onto parameters that may or may
546 # not be vectors, and always getting the first element (e.g. cpus)
547 def __getitem__(self, key):
548 if key == 0:
549 return self
550 raise TypeError, "Non-zero index '%s' to SimObject" % key
551
552 # clear out children with given name, even if it's a vector
553 def clear_child(self, name):
554 if not self._children.has_key(name):
555 return
556 child = self._children[name]
557 if isinstance(child, SimObjVector):
558 for i in xrange(len(child)):
559 del self._children["s%d" % (name, i)]
560 del self._children[name]
561
562 def add_child(self, name, value):
563 self._children[name] = value
564
565 def _maybe_set_parent(self, parent, name):
566 if not self._parent:
567 self._parent = parent
568 self._name = name
569 parent.add_child(name, self)
570
571 def _set_child(self, attr, value):
572 # if RHS is a SimObject, it's an implicit child assignment
573 # clear out old child with this name, if any
574 self.clear_child(attr)
575
576 if isSimObject(value):
577 value._maybe_set_parent(self, attr)
578 elif isSimObjectSequence(value):
579 value = SimObjVector(value)
580 [v._maybe_set_parent(self, "%s%d" % (attr, i))
581 for i,v in enumerate(value)]
582
583 self._values[attr] = value
584
585 def path(self):
586 if not self._parent:
587 return 'root'
588 ppath = self._parent.path()
589 if ppath == 'root':
590 return self._name
591 return ppath + "." + self._name
592
593 def __str__(self):
594 return self.path()
595
596 def ini_str(self):
597 return self.path()
598
599 def find_any(self, ptype):
600 if isinstance(self, ptype):
601 return self, True
602
603 found_obj = None
604 for child in self._children.itervalues():
605 if isinstance(child, ptype):
606 if found_obj != None and child != found_obj:
607 raise AttributeError, \
608 'parent.any matched more than one: %s %s' % \
609 (found_obj.path, child.path)
610 found_obj = child
611 # search param space
612 for pname,pdesc in self._params.iteritems():
613 if issubclass(pdesc.ptype, ptype):
614 match_obj = self._values[pname]
615 if found_obj != None and found_obj != match_obj:
616 raise AttributeError, \
617 'parent.any matched more than one: %s' % obj.path
618 found_obj = match_obj
619 return found_obj, found_obj != None
620
621 def unproxy(self, base):
622 return self
623
624 def unproxy_all(self):
625 for param in self._params.iterkeys():
626 value = self._values.get(param)
627 if value != None and proxy.isproxy(value):
628 try:
629 value = value.unproxy(self)
630 except:
631 print "Error in unproxying param '%s' of %s" % \
632 (param, self.path())
633 raise
634 setattr(self, param, value)
635
636 # Unproxy ports in sorted order so that 'append' operations on
637 # vector ports are done in a deterministic fashion.
638 port_names = self._ports.keys()
639 port_names.sort()
640 for port_name in port_names:
641 port = self._port_refs.get(port_name)
642 if port != None:
643 port.unproxy(self)
644
645 # Unproxy children in sorted order for determinism also.
646 child_names = self._children.keys()
647 child_names.sort()
648 for child in child_names:
649 self._children[child].unproxy_all()
650
651 def print_ini(self):
652 print '[' + self.path() + ']' # .ini section header
653
654 instanceDict[self.path()] = self
655
656 if hasattr(self, 'type') and not isinstance(self, ParamContext):
656 if hasattr(self, 'type'):
657 print 'type=%s' % self.type
658
659 child_names = self._children.keys()
660 child_names.sort()
657 print 'type=%s' % self.type
658
659 child_names = self._children.keys()
660 child_names.sort()
661 np_child_names = [c for c in child_names \
662 if not isinstance(self._children[c], ParamContext)]
663 if len(np_child_names):
664 print 'children=%s' % ' '.join(np_child_names)
661 if len(child_names):
662 print 'children=%s' % ' '.join(child_names)
665
666 param_names = self._params.keys()
667 param_names.sort()
668 for param in param_names:
669 value = self._values.get(param)
670 if value != None:
671 print '%s=%s' % (param, self._values[param].ini_str())
672
673 port_names = self._ports.keys()
674 port_names.sort()
675 for port_name in port_names:
676 port = self._port_refs.get(port_name, None)
677 if port != None:
678 print '%s=%s' % (port_name, port.ini_str())
679
680 print # blank line between objects
681
682 for child in child_names:
683 self._children[child].print_ini()
684
685 # Call C++ to create C++ object corresponding to this object and
686 # (recursively) all its children
687 def createCCObject(self):
688 self.getCCObject() # force creation
689 for child in self._children.itervalues():
690 child.createCCObject()
691
692 # Get C++ object corresponding to this object, calling C++ if
693 # necessary to construct it. Does *not* recursively create
694 # children.
695 def getCCObject(self):
696 if not self._ccObject:
697 self._ccObject = -1 # flag to catch cycles in recursion
698 self._ccObject = internal.main.createSimObject(self.path())
699 elif self._ccObject == -1:
700 raise RuntimeError, "%s: recursive call to getCCObject()" \
701 % self.path()
702 return self._ccObject
703
704 # Create C++ port connections corresponding to the connections in
705 # _port_refs (& recursively for all children)
706 def connectPorts(self):
707 for portRef in self._port_refs.itervalues():
708 portRef.ccConnect()
709 for child in self._children.itervalues():
710 child.connectPorts()
711
712 def startDrain(self, drain_event, recursive):
713 count = 0
663
664 param_names = self._params.keys()
665 param_names.sort()
666 for param in param_names:
667 value = self._values.get(param)
668 if value != None:
669 print '%s=%s' % (param, self._values[param].ini_str())
670
671 port_names = self._ports.keys()
672 port_names.sort()
673 for port_name in port_names:
674 port = self._port_refs.get(port_name, None)
675 if port != None:
676 print '%s=%s' % (port_name, port.ini_str())
677
678 print # blank line between objects
679
680 for child in child_names:
681 self._children[child].print_ini()
682
683 # Call C++ to create C++ object corresponding to this object and
684 # (recursively) all its children
685 def createCCObject(self):
686 self.getCCObject() # force creation
687 for child in self._children.itervalues():
688 child.createCCObject()
689
690 # Get C++ object corresponding to this object, calling C++ if
691 # necessary to construct it. Does *not* recursively create
692 # children.
693 def getCCObject(self):
694 if not self._ccObject:
695 self._ccObject = -1 # flag to catch cycles in recursion
696 self._ccObject = internal.main.createSimObject(self.path())
697 elif self._ccObject == -1:
698 raise RuntimeError, "%s: recursive call to getCCObject()" \
699 % self.path()
700 return self._ccObject
701
702 # Create C++ port connections corresponding to the connections in
703 # _port_refs (& recursively for all children)
704 def connectPorts(self):
705 for portRef in self._port_refs.itervalues():
706 portRef.ccConnect()
707 for child in self._children.itervalues():
708 child.connectPorts()
709
710 def startDrain(self, drain_event, recursive):
711 count = 0
714 # ParamContexts don't serialize
715 if isinstance(self, SimObject) and not isinstance(self, ParamContext):
712 if isinstance(self, SimObject):
716 count += self._ccObject.drain(drain_event)
717 if recursive:
718 for child in self._children.itervalues():
719 count += child.startDrain(drain_event, True)
720 return count
721
722 def resume(self):
713 count += self._ccObject.drain(drain_event)
714 if recursive:
715 for child in self._children.itervalues():
716 count += child.startDrain(drain_event, True)
717 return count
718
719 def resume(self):
723 if isinstance(self, SimObject) and not isinstance(self, ParamContext):
720 if isinstance(self, SimObject):
724 self._ccObject.resume()
725 for child in self._children.itervalues():
726 child.resume()
727
728 def changeTiming(self, mode):
729 if isinstance(self, m5.objects.System):
730 # i don't know if there's a better way to do this - calling
731 # setMemoryMode directly from self._ccObject results in calling
732 # SimObject::setMemoryMode, not the System::setMemoryMode
733 system_ptr = internal.main.convertToSystemPtr(self._ccObject)
734 system_ptr.setMemoryMode(mode)
735 for child in self._children.itervalues():
736 child.changeTiming(mode)
737
738 def takeOverFrom(self, old_cpu):
739 cpu_ptr = internal.main.convertToBaseCPUPtr(old_cpu._ccObject)
740 self._ccObject.takeOverFrom(cpu_ptr)
741
742 # generate output file for 'dot' to display as a pretty graph.
743 # this code is currently broken.
744 def outputDot(self, dot):
745 label = "{%s|" % self.path
746 if isSimObject(self.realtype):
747 label += '%s|' % self.type
748
749 if self.children:
750 # instantiate children in same order they were added for
751 # backward compatibility (else we can end up with cpu1
752 # before cpu0).
753 for c in self.children:
754 dot.add_edge(pydot.Edge(self.path,c.path, style="bold"))
755
756 simobjs = []
757 for param in self.params:
758 try:
759 if param.value is None:
760 raise AttributeError, 'Parameter with no value'
761
762 value = param.value
763 string = param.string(value)
764 except Exception, e:
765 msg = 'exception in %s:%s\n%s' % (self.name, param.name, e)
766 e.args = (msg, )
767 raise
768
769 if isSimObject(param.ptype) and string != "Null":
770 simobjs.append(string)
771 else:
772 label += '%s = %s\\n' % (param.name, string)
773
774 for so in simobjs:
775 label += "|<%s> %s" % (so, so)
776 dot.add_edge(pydot.Edge("%s:%s" % (self.path, so), so,
777 tailport="w"))
778 label += '}'
779 dot.add_node(pydot.Node(self.path,shape="Mrecord",label=label))
780
781 # recursively dump out children
782 for c in self.children:
783 c.outputDot(dot)
784
721 self._ccObject.resume()
722 for child in self._children.itervalues():
723 child.resume()
724
725 def changeTiming(self, mode):
726 if isinstance(self, m5.objects.System):
727 # i don't know if there's a better way to do this - calling
728 # setMemoryMode directly from self._ccObject results in calling
729 # SimObject::setMemoryMode, not the System::setMemoryMode
730 system_ptr = internal.main.convertToSystemPtr(self._ccObject)
731 system_ptr.setMemoryMode(mode)
732 for child in self._children.itervalues():
733 child.changeTiming(mode)
734
735 def takeOverFrom(self, old_cpu):
736 cpu_ptr = internal.main.convertToBaseCPUPtr(old_cpu._ccObject)
737 self._ccObject.takeOverFrom(cpu_ptr)
738
739 # generate output file for 'dot' to display as a pretty graph.
740 # this code is currently broken.
741 def outputDot(self, dot):
742 label = "{%s|" % self.path
743 if isSimObject(self.realtype):
744 label += '%s|' % self.type
745
746 if self.children:
747 # instantiate children in same order they were added for
748 # backward compatibility (else we can end up with cpu1
749 # before cpu0).
750 for c in self.children:
751 dot.add_edge(pydot.Edge(self.path,c.path, style="bold"))
752
753 simobjs = []
754 for param in self.params:
755 try:
756 if param.value is None:
757 raise AttributeError, 'Parameter with no value'
758
759 value = param.value
760 string = param.string(value)
761 except Exception, e:
762 msg = 'exception in %s:%s\n%s' % (self.name, param.name, e)
763 e.args = (msg, )
764 raise
765
766 if isSimObject(param.ptype) and string != "Null":
767 simobjs.append(string)
768 else:
769 label += '%s = %s\\n' % (param.name, string)
770
771 for so in simobjs:
772 label += "|<%s> %s" % (so, so)
773 dot.add_edge(pydot.Edge("%s:%s" % (self.path, so), so,
774 tailport="w"))
775 label += '}'
776 dot.add_node(pydot.Node(self.path,shape="Mrecord",label=label))
777
778 # recursively dump out children
779 for c in self.children:
780 c.outputDot(dot)
781
785class ParamContext(SimObject):
786 pass
787
788# Function to provide to C++ so it can look up instances based on paths
789def resolveSimObject(name):
790 obj = instanceDict[name]
791 return obj.getCCObject()
792
793# __all__ defines the list of symbols that get exported when
794# 'from config import *' is invoked. Try to keep this reasonably
795# short to avoid polluting other namespaces.
782# Function to provide to C++ so it can look up instances based on paths
783def resolveSimObject(name):
784 obj = instanceDict[name]
785 return obj.getCCObject()
786
787# __all__ defines the list of symbols that get exported when
788# 'from config import *' is invoked. Try to keep this reasonably
789# short to avoid polluting other namespaces.
796__all__ = ['SimObject', 'ParamContext']
790__all__ = ['SimObject']
797
798# see comment on imports at end of __init__.py.
799import proxy
800import internal
801import m5
791
792# see comment on imports at end of __init__.py.
793import proxy
794import internal
795import m5