read_config.py revision 12410:184beca7f554
1# Copyright (c) 2014 ARM Limited 2# All rights reserved. 3# 4# The license below extends only to copyright in the software and shall 5# not be construed as granting a license to any other intellectual 6# property including but not limited to intellectual property relating 7# to a hardware implementation of the functionality of the software 8# licensed hereunder. You may use the software subject to the license 9# terms below provided that you ensure that this notice is replicated 10# unmodified and in its entirety in all distributions of the software, 11# modified or unmodified, in source code or in binary form. 12# 13# Redistribution and use in source and binary forms, with or without 14# modification, are permitted provided that the following conditions are 15# met: redistributions of source code must retain the above copyright 16# notice, this list of conditions and the following disclaimer; 17# redistributions in binary form must reproduce the above copyright 18# notice, this list of conditions and the following disclaimer in the 19# documentation and/or other materials provided with the distribution; 20# neither the name of the copyright holders nor the names of its 21# contributors may be used to endorse or promote products derived from 22# this software without specific prior written permission. 23# 24# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 25# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 26# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 27# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 28# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 29# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 30# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 31# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 32# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 34# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35# 36# Author: Andrew Bardsley 37 38# This script allows .ini and .json system config file generated from a 39# previous gem5 run to be read in and instantiated. 40# 41# This may be useful as a way of allowing variant run scripts (say, 42# with more complicated than usual checkpointing/stats dumping/ 43# simulation control) to read pre-described systems from config scripts 44# with better system-description capabilities. Splitting scripts 45# between system construction and run control may allow better 46# debugging. 47 48import argparse 49import ConfigParser 50import inspect 51import json 52import re 53import sys 54 55import m5 56import m5.ticks as ticks 57 58sim_object_classes_by_name = { 59 cls.__name__: cls for cls in m5.objects.__dict__.itervalues() 60 if inspect.isclass(cls) and issubclass(cls, m5.objects.SimObject) } 61 62# Add some parsing functions to Param classes to handle reading in .ini 63# file elements. This could be moved into src/python/m5/params.py if 64# reading .ini files from Python proves to be useful 65 66def no_parser(cls, flags, param): 67 raise Exception('Can\'t parse string: %s for parameter' 68 ' class: %s' % (str(param), cls.__name__)) 69 70def simple_parser(suffix='', cast=lambda i: i): 71 def body(cls, flags, param): 72 return cls(cast(param + suffix)) 73 return body 74 75# def tick_parser(cast=m5.objects.Latency): # lambda i: i): 76def tick_parser(cast=lambda i: i): 77 def body(cls, flags, param): 78 old_param = param 79 ret = cls(cast(str(param) + 't')) 80 return ret 81 return body 82 83def addr_range_parser(cls, flags, param): 84 sys.stdout.flush() 85 (low, high, intlv_high_bit, xor_high_bit, 86 intlv_bits, intlv_match) = param.split(':') 87 return m5.objects.AddrRange( 88 start=long(low), end=long(high), 89 intlvHighBit=long(intlv_high_bit), xorHighBit=long(xor_high_bit), 90 intlvBits=long(intlv_bits), intlvMatch=long(intlv_match)) 91 92def memory_bandwidth_parser(cls, flags, param): 93 # The string will be in tick/byte 94 # Convert to byte/tick 95 value = 1.0 / float(param) 96 # Convert to byte/s 97 value = ticks.fromSeconds(value) 98 return cls('%fB/s' % value) 99 100# These parameters have trickier parsing from .ini files than might be 101# expected 102param_parsers = { 103 'Bool': simple_parser(), 104 'ParamValue': no_parser, 105 'NumericParamValue': simple_parser(cast=long), 106 'TickParamValue': tick_parser(), 107 'Frequency': tick_parser(cast=m5.objects.Latency), 108 'Current': simple_parser(suffix='A'), 109 'Voltage': simple_parser(suffix='V'), 110 'Enum': simple_parser(), 111 'MemorySize': simple_parser(suffix='B'), 112 'MemorySize32': simple_parser(suffix='B'), 113 'AddrRange': addr_range_parser, 114 'String': simple_parser(), 115 'MemoryBandwidth': memory_bandwidth_parser, 116 'Time': simple_parser(), 117 'EthernetAddr': simple_parser() 118 } 119 120for name, parser in param_parsers.iteritems(): 121 setattr(m5.params.__dict__[name], 'parse_ini', classmethod(parser)) 122 123class PortConnection(object): 124 """This class is similar to m5.params.PortRef but with just enough 125 information for ConfigManager""" 126 127 def __init__(self, object_name, port_name, index): 128 self.object_name = object_name 129 self.port_name = port_name 130 self.index = index 131 132 @classmethod 133 def from_string(cls, str): 134 m = re.match('(.*)\.([^.\[]+)(\[(\d+)\])?', str) 135 object_name, port_name, whole_index, index = m.groups() 136 if index is not None: 137 index = int(index) 138 else: 139 index = 0 140 141 return PortConnection(object_name, port_name, index) 142 143 def __str__(self): 144 return '%s.%s[%d]' % (self.object_name, self.port_name, self.index) 145 146 def __cmp__(self, right): 147 return cmp((self.object_name, self.port_name, self.index), 148 (right.object_name, right.port_name, right.index)) 149 150def to_list(v): 151 """Convert any non list to a singleton list""" 152 if isinstance(v, list): 153 return v 154 else: 155 return [v] 156 157class ConfigManager(object): 158 """Manager for parsing a Root configuration from a config file""" 159 def __init__(self, config): 160 self.config = config 161 self.objects_by_name = {} 162 self.flags = config.get_flags() 163 164 def find_object(self, object_name): 165 """Find and configure (with just non-SimObject parameters) 166 a single object""" 167 168 if object_name == 'Null': 169 return NULL 170 171 if object_name in self.objects_by_name: 172 return self.objects_by_name[object_name] 173 174 object_type = self.config.get_param(object_name, 'type') 175 176 if object_type not in sim_object_classes_by_name: 177 raise Exception('No SimObject type %s is available to' 178 ' build: %s' % (object_type, object_name)) 179 180 object_class = sim_object_classes_by_name[object_type] 181 182 parsed_params = {} 183 184 for param_name, param in object_class._params.iteritems(): 185 if issubclass(param.ptype, m5.params.ParamValue): 186 if isinstance(param, m5.params.VectorParamDesc): 187 param_values = self.config.get_param_vector(object_name, 188 param_name) 189 190 param_value = [ param.ptype.parse_ini(self.flags, value) 191 for value in param_values ] 192 else: 193 param_value = param.ptype.parse_ini( 194 self.flags, self.config.get_param(object_name, 195 param_name)) 196 197 parsed_params[param_name] = param_value 198 199 obj = object_class(**parsed_params) 200 self.objects_by_name[object_name] = obj 201 202 return obj 203 204 def fill_in_simobj_parameters(self, object_name, obj): 205 """Fill in all references to other SimObjects in an objects 206 parameters. This relies on all referenced objects having been 207 created""" 208 209 if object_name == 'Null': 210 return NULL 211 212 for param_name, param in obj.__class__._params.iteritems(): 213 if issubclass(param.ptype, m5.objects.SimObject): 214 if isinstance(param, m5.params.VectorParamDesc): 215 param_values = self.config.get_param_vector(object_name, 216 param_name) 217 218 setattr(obj, param_name, [ self.objects_by_name[name] 219 for name in param_values ]) 220 else: 221 param_value = self.config.get_param(object_name, 222 param_name) 223 224 if param_value != 'Null': 225 setattr(obj, param_name, self.objects_by_name[ 226 param_value]) 227 228 return obj 229 230 def fill_in_children(self, object_name, obj): 231 """Fill in the children of this object. This relies on all the 232 referenced objects having been created""" 233 234 children = self.config.get_object_children(object_name) 235 236 for child_name, child_paths in children: 237 param = obj.__class__._params.get(child_name, None) 238 239 if isinstance(child_paths, list): 240 child_list = [ self.objects_by_name[path] 241 for path in child_paths ] 242 else: 243 child_list = self.objects_by_name[child_paths] 244 245 obj.add_child(child_name, child_list) 246 247 for path in to_list(child_paths): 248 self.fill_in_children(path, self.objects_by_name[path]) 249 250 return obj 251 252 def parse_port_name(self, port): 253 """Parse the name of a port""" 254 255 m = re.match('(.*)\.([^.\[]+)(\[(\d+)\])?', port) 256 peer, peer_port, whole_index, index = m.groups() 257 if index is not None: 258 index = int(index) 259 else: 260 index = 0 261 262 return (peer, self.objects_by_name[peer], peer_port, index) 263 264 def gather_port_connections(self, object_name, obj): 265 """Gather all the port-to-port connections from the named object. 266 Returns a list of (PortConnection, PortConnection) with unordered 267 (wrt. master/slave) connection information""" 268 269 if object_name == 'Null': 270 return NULL 271 272 parsed_ports = [] 273 for port_name, port in obj.__class__._ports.iteritems(): 274 # Assume that unnamed ports are unconnected 275 peers = self.config.get_port_peers(object_name, port_name) 276 277 for index, peer in zip(xrange(0, len(peers)), peers): 278 parsed_ports.append(( 279 PortConnection(object_name, port.name, index), 280 PortConnection.from_string(peer))) 281 282 return parsed_ports 283 284 def bind_ports(self, connections): 285 """Bind all ports from the given connection list. Note that the 286 connection list *must* list all connections with both (slave,master) 287 and (master,slave) orderings""" 288 289 # Markup a dict of how many connections are made to each port. 290 # This will be used to check that the next-to-be-made connection 291 # has a suitable port index 292 port_bind_indices = {} 293 for from_port, to_port in connections: 294 port_bind_indices[ 295 (from_port.object_name, from_port.port_name)] = 0 296 297 def port_has_correct_index(port): 298 return port_bind_indices[ 299 (port.object_name, port.port_name)] == port.index 300 301 def increment_port_index(port): 302 port_bind_indices[ 303 (port.object_name, port.port_name)] += 1 304 305 # Step through the sorted connections. Exactly one of 306 # each (slave,master) and (master,slave) pairs will be 307 # bindable because the connections are sorted. 308 # For example: port_bind_indices 309 # left right left right 310 # a.b[0] -> d.f[1] 0 0 X 311 # a.b[1] -> e.g 0 0 BIND! 312 # e.g -> a.b[1] 1 X 0 313 # d.f[0] -> f.h 0 0 BIND! 314 # d.f[1] -> a.b[0] 1 0 BIND! 315 connections_to_make = [] 316 for connection in sorted(connections): 317 from_port, to_port = connection 318 319 if (port_has_correct_index(from_port) and 320 port_has_correct_index(to_port)): 321 322 connections_to_make.append((from_port, to_port)) 323 324 increment_port_index(from_port) 325 increment_port_index(to_port) 326 327 # Exactly half of the connections (ie. all of them, one per 328 # direction) must now have been made 329 if (len(connections_to_make) * 2) != len(connections): 330 raise Exception('Port bindings can\'t be ordered') 331 332 # Actually do the binding 333 for from_port, to_port in connections_to_make: 334 from_object = self.objects_by_name[from_port.object_name] 335 to_object = self.objects_by_name[to_port.object_name] 336 337 setattr(from_object, from_port.port_name, 338 getattr(to_object, to_port.port_name)) 339 340 def find_all_objects(self): 341 """Find and build all SimObjects from the config file and connect 342 their ports together as described. Does not instantiate system""" 343 344 # Build SimObjects for all sections of the config file 345 # populating not-SimObject-valued parameters 346 for object_name in self.config.get_all_object_names(): 347 self.find_object(object_name) 348 349 # Add children to objects in the hierarchy from root 350 self.fill_in_children('root', self.find_object('root')) 351 352 # Now fill in SimObject-valued parameters in the knowledge that 353 # this won't be interpreted as becoming the parent of objects 354 # which are already in the root hierarchy 355 for name, obj in self.objects_by_name.iteritems(): 356 self.fill_in_simobj_parameters(name, obj) 357 358 # Gather a list of all port-to-port connections 359 connections = [] 360 for name, obj in self.objects_by_name.iteritems(): 361 connections += self.gather_port_connections(name, obj) 362 363 # Find an acceptable order to bind those port connections and 364 # bind them 365 self.bind_ports(connections) 366 367class ConfigFile(object): 368 def get_flags(self): 369 return set() 370 371 def load(self, config_file): 372 """Load the named config file""" 373 pass 374 375 def get_all_object_names(self): 376 """Get a list of all the SimObject paths in the configuration""" 377 pass 378 379 def get_param(self, object_name, param_name): 380 """Get a single param or SimObject reference from the configuration 381 as a string""" 382 pass 383 384 def get_param_vector(self, object_name, param_name): 385 """Get a vector param or vector of SimObject references from the 386 configuration as a list of strings""" 387 pass 388 389 def get_object_children(self, object_name): 390 """Get a list of (name, paths) for each child of this object. 391 paths is either a single string object path or a list of object 392 paths""" 393 pass 394 395 def get_port_peers(self, object_name, port_name): 396 """Get the list of connected port names (in the string form 397 object.port(\[index\])?) of the port object_name.port_name""" 398 pass 399 400class ConfigIniFile(ConfigFile): 401 def __init__(self): 402 self.parser = ConfigParser.ConfigParser() 403 404 def load(self, config_file): 405 self.parser.read(config_file) 406 407 def get_all_object_names(self): 408 return self.parser.sections() 409 410 def get_param(self, object_name, param_name): 411 return self.parser.get(object_name, param_name) 412 413 def get_param_vector(self, object_name, param_name): 414 return self.parser.get(object_name, param_name).split() 415 416 def get_object_children(self, object_name): 417 if self.parser.has_option(object_name, 'children'): 418 children = self.parser.get(object_name, 'children') 419 child_names = children.split() 420 else: 421 child_names = [] 422 423 def make_path(child_name): 424 if object_name == 'root': 425 return child_name 426 else: 427 return '%s.%s' % (object_name, child_name) 428 429 return [ (name, make_path(name)) for name in child_names ] 430 431 def get_port_peers(self, object_name, port_name): 432 if self.parser.has_option(object_name, port_name): 433 peer_string = self.parser.get(object_name, port_name) 434 return peer_string.split() 435 else: 436 return [] 437 438class ConfigJsonFile(ConfigFile): 439 def __init__(self): 440 pass 441 442 def is_sim_object(self, node): 443 return isinstance(node, dict) and 'path' in node 444 445 def find_all_objects(self, node): 446 if self.is_sim_object(node): 447 self.object_dicts[node['path']] = node 448 449 if isinstance(node, list): 450 for elem in node: 451 self.find_all_objects(elem) 452 elif isinstance(node, dict): 453 for elem in node.itervalues(): 454 self.find_all_objects(elem) 455 456 def load(self, config_file): 457 root = json.load(open(config_file, 'r')) 458 self.object_dicts = {} 459 self.find_all_objects(root) 460 461 def get_all_object_names(self): 462 return sorted(self.object_dicts.keys()) 463 464 def parse_param_string(self, node): 465 if node is None: 466 return "Null" 467 elif self.is_sim_object(node): 468 return node['path'] 469 else: 470 return str(node) 471 472 def get_param(self, object_name, param_name): 473 obj = self.object_dicts[object_name] 474 475 return self.parse_param_string(obj[param_name]) 476 477 def get_param_vector(self, object_name, param_name): 478 obj = self.object_dicts[object_name] 479 480 return [ self.parse_param_string(p) for p in obj[param_name] ] 481 482 def get_object_children(self, object_name): 483 """It is difficult to tell which elements are children in the 484 JSON file as there is no explicit 'children' node. Take any 485 element which is a full SimObject description or a list of 486 SimObject descriptions. This will not work with a mixed list of 487 references and descriptions but that's a scenario that isn't 488 possible (very likely?) with gem5's binding/naming rules""" 489 obj = self.object_dicts[object_name] 490 491 children = [] 492 for name, node in obj.iteritems(): 493 if self.is_sim_object(node): 494 children.append((name, node['path'])) 495 elif isinstance(node, list) and node != [] and all([ 496 self.is_sim_object(e) for e in node ]): 497 children.append((name, [ e['path'] for e in node ])) 498 499 return children 500 501 def get_port_peers(self, object_name, port_name): 502 """Get the 'peer' element of any node with 'peer' and 'role' 503 elements""" 504 obj = self.object_dicts[object_name] 505 506 peers = [] 507 if port_name in obj and 'peer' in obj[port_name] and \ 508 'role' in obj[port_name]: 509 peers = to_list(obj[port_name]['peer']) 510 511 return peers 512 513parser = argparse.ArgumentParser() 514 515parser.add_argument('config_file', metavar='config-file.ini', 516 help='.ini configuration file to load and run') 517parser.add_argument('--checkpoint-dir', type=str, default=None, 518 help='A checkpoint to directory to restore when starting ' 519 'the simulation') 520 521args = parser.parse_args(sys.argv[1:]) 522 523if args.config_file.endswith('.ini'): 524 config = ConfigIniFile() 525 config.load(args.config_file) 526else: 527 config = ConfigJsonFile() 528 config.load(args.config_file) 529 530ticks.fixGlobalFrequency() 531 532mgr = ConfigManager(config) 533 534mgr.find_all_objects() 535 536m5.instantiate(args.checkpoint_dir) 537 538exit_event = m5.simulate() 539print 'Exiting @ tick %i because %s' % ( 540 m5.curTick(), exit_event.getCause()) 541