45#include "debug/CxxConfig.hh"
53 configFile(configFile_),
flags(configFile_.getFlags()),
54 simObjectResolver(*this)
60 std::string &object_type)
63 throw Exception(object_name,
"Can't find sim object");
66 throw Exception(object_name,
"Sim object has no 'type' field");
72 "No sim object type %s is available", object_type));
86 if (from_name.find(renaming.
fromPrefix) == 0) {
88 from_name.substr(renaming.
fromPrefix.length());
101 if (to_name.find(renaming.
toPrefix) == 0) {
103 to_name.substr(renaming.
toPrefix.length());
113 std::ostringstream params;
115 auto i = param_values.begin();
116 auto end_i = param_values.end();
135 std::string instance_name =
rename(object_name);
137 if (object_name ==
"Null")
145 throw Exception(instance_name,
"Cycle in configuration");
147 std::string object_type;
156 DPRINTF(CxxConfig,
"Configuring sim object references for: %s"
157 " (%s from object %s)\n", instance_name, object_type,
178 "Element not found: %s", param->
name));
183 for (
auto n = sub_object_names.begin();
184 n != sub_object_names.end(); ++
n)
190 sub_objects.push_back(sub_object);
197 "Can't assign sim object element %s from \"%s\"",
201 DPRINTF(CxxConfig,
"Setting sim object(s): %s.%s=%s\n",
202 object_name, param->
name,
205 std::string sub_object_name;
211 "Element not found: %s", param->
name));
222 "Can't assign sim object element %s from"
223 " \"%s\"", param->
name, sub_object_name));
227 DPRINTF(CxxConfig,
"Setting sim object(s):"
228 " %s.%s=%s\n", object_name, param->
name,
234 DPRINTF(CxxConfig,
"Creating SimObject: %s\n", instance_name);
239 " type: %s", object_type));
245 if (visit_children) {
250 for (
auto i = children.begin();
i != children.end(); ++
i)
254 delete object_params;
268 std::string instance_name =
rename(object_name);
274 std::string object_type;
278 DPRINTF(CxxConfig,
"Configuring parameters of object: %s (%s)\n",
279 instance_name, object_type);
286 object_params->
setName(instance_name);
304 "Element not found for parameter: %s",
309 param_values,
flags))
312 "Bad parameter value: .%s=X=\"%s\"",
316 DPRINTF(CxxConfig,
"Setting parameter"
317 " %s.%s=%s\n", instance_name, param->
name,
320 std::string param_value;
326 "Element not found for parameter: %s",
334 "Bad parameter value: .%s=X=\"%s\"",
335 param->
name, param_value));
338 DPRINTF(CxxConfig,
"Setting parameter %s.%s=%s\n",
339 instance_name, param->
name, param_value);
346 for (
auto i = entry.
ports.begin();
i != entry.
ports.end(); ++
i) {
351 DPRINTF(CxxConfig,
"Port not found: %s.%s,"
352 " assuming there are no connections\n",
353 instance_name, port->
name);
356 unsigned int peer_count = peers.size();
367 "Unconnected port: %s", port->
name));
370 DPRINTF(CxxConfig,
"Setting port connection count"
371 " for: %s.%s to %d\n",
372 instance_name, port->
name, peer_count);
384 DPRINTF(CxxConfig,
"Nulling sim object reference: %s.%s\n",
385 instance_name, param->
name);
398 "Error nulling sim object reference(s): %s",
404 delete object_params;
410 return object_params;
436 for (
auto i = children.begin();
i != children.end(); ++
i)
450 SimObject *requestor_object,
const std::string &request_port_name,
451 PortID request_port_index,
452 SimObject *responder_object,
const std::string &response_port_name,
453 PortID response_port_index)
461 request_port_name, request_port_index);
463 response_port_name, response_port_index);
467 "Request port: %s[%d] is already connected\n", request_port_name,
468 request_port_index));
473 "Response port: %s[%d] is already connected\n", response_port_name,
474 response_port_index));
477 DPRINTF(CxxConfig,
"Binding port %s.%s[%d]"
479 requestor_object->
name(), request_port_name, request_port_index,
480 responder_object->
name(), response_port_name, response_port_index);
482 request_port.
bind(response_port);
490 unsigned int request_port_index = 0;
492 for (
auto peer_i = peers.begin(); peer_i != peers.end();
495 const std::string &peer = *peer_i;
496 std::string response_object_name;
497 std::string response_port_name;
498 unsigned int response_port_index;
500 parsePort(peer, response_object_name, response_port_name,
501 response_port_index);
503 std::string response_instance_name =
rename(response_object_name);
508 "Can't find response port object: %s",
509 response_instance_name));
515 responder_object, response_port_name, response_port_index);
517 request_port_index++;
526 const std::string &instance_name =
object->name();
528 std::string object_name =
unRename(instance_name);
530 std::string object_type;
534 DPRINTF(CxxConfig,
"Binding ports of object: %s (%s)\n",
535 instance_name, object_type);
537 for (
auto i = entry.
ports.begin();
i != entry.
ports.end(); ++
i) {
540 DPRINTF(CxxConfig,
"Binding port: %s.%s\n", instance_name,
549 if (!port->
isVector && peers.size() > 1) {
551 "Too many connections to non-vector port %s (%d)\n",
552 port->
name, peers.size()));
562 std::string &path, std::string &port,
unsigned int &
index)
564 std::size_t dot_i = inp.rfind(
'.');
565 std::size_t open_square_i = inp.rfind(
'[');
567 if (dot_i == std::string::npos) {
568 DPRINTF(CxxConfig,
"Bad port string: %s\n", inp);
573 path = std::string(inp, 0, dot_i);
575 if (open_square_i == std::string::npos) {
577 port = std::string(inp, dot_i + 1, inp.length() - dot_i);
581 port = std::string(inp, dot_i + 1, (open_square_i - 1) - dot_i);
582 index = std::atoi(inp.c_str() + open_square_i + 1);
602 DPRINTF(CxxConfig,
"Initialising all objects\n");
605 DPRINTF(CxxConfig,
"Registering stats\n");
608 DPRINTF(CxxConfig,
"Registering probe points\n");
611 DPRINTF(CxxConfig,
"Connecting probe listeners\n");
618 DPRINTF(CxxConfig,
"Calling initState on all objects\n");
625 DPRINTF(CxxConfig,
"Starting up all objects\n");
646 os <<
'[' << (*i)->name() <<
"]\n";
655 (*i)->loadState(checkpoint);
662 DPRINTF(CxxConfig,
"Freeing sim object: %s\n", (*i)->name());
671 DPRINTF(CxxConfig,
"Freeing sim object params: %s\n",
682 const std::string ¶m_name,
const std::string ¶m_value)
688 " .%s=X=\"%s\"", param_name, param_value));
690 std::string instance_name =
rename(object_name);
692 DPRINTF(CxxConfig,
"Setting parameter %s.%s=%s\n",
693 instance_name, param_name, param_value);
699 const std::string ¶m_name,
708 std::string instance_name =
rename(object_name);
710 DPRINTF(CxxConfig,
"Setting parameter %s.%s=\"%s\"\n",
const bool isSimObject
Is this a SimObject, and so is to be set with setSimObject... or another from-string parameter set wi...
Similar to ParamDesc to describe ports.
const bool isRequestor
Is this a request or response port.
Config details entry for a SimObject.
std::map< std::string, ParamDesc * > parameters
All parameters (including SimObjects) in order.
virtual CxxConfigParams * makeParamsObject() const
Make a ...Param structure for the SimObject class of this entry.
std::map< std::string, PortDesc * > ports
Ports.
Config file wrapper providing a common interface to CxxConfigManager.
virtual void getAllObjectNames(std::vector< std::string > &list) const =0
Get all SimObjects in the config.
virtual bool getParamVector(const std::string &object_name, const std::string ¶m_name, std::vector< std::string > &values) const =0
Get a list/vector parameter.
virtual void getObjectChildren(const std::string &object_name, std::vector< std::string > &children, bool return_paths=false) const =0
Get the names or paths of all the children SimObjects of this SimObject.
virtual bool getPortPeers(const std::string &object_name, const std::string &port_name, std::vector< std::string > &peers) const =0
Get the peer (connected) ports of the named ports.
virtual bool getParam(const std::string &object_name, const std::string ¶m_name, std::string &value) const =0
Get a single parameter value as a string returned in value.
virtual bool objectExists(const std::string &object_name) const =0
Does an object with this path exist?
Exception for instantiate/post-instantiate errors.
std::set< std::string > inVisit
While configuring, inVisit contains names of SimObjects visited in this recursive configuration walk.
std::list< SimObject * > objectsInOrder
SimObjects in order.
void drainResume()
Resume from drain.
static void parsePort(const std::string &inp, std::string &path, std::string &port, unsigned int &index)
Parse a port string of the form 'path(.path)*.port[index]' into path, port and index.
void startup()
Call startup on all objects.
void deleteObjects()
Delete all objects and clear objectsByName and objectsByOrder.
CxxConfigManager(CxxConfigFileBase &configFile_)
void serialize(std::ostream &os)
Serialize (checkpoint) all objects to the given stream.
std::string rename(const std::string &from_name)
Apply the first matching renaming in renamings to the given name.
std::string unRename(const std::string &to_name)
Apply the first matching renaming in reverse (toPrefix -> fromPrefix for the given name.
CxxConfigParams::Flags flags
Flags to pass to affect param setting.
void setParamVector(const std::string &object_name, const std::string ¶m_name, const std::vector< std::string > ¶m_values)
CxxConfigFileBase & configFile
Configuration file being read.
void bindPort(SimObject *requestorObject, const std::string &requestPort, PortID requestPortIndex, SimObject *responderObject, const std::string &responsePort, PortID responsePortIndex)
Bind a single connection between two objects' ports.
void addRenaming(const Renaming &renaming)
Add a name prefix renaming to those currently applied.
unsigned int drain()
Drain all objects.
void initState()
Call initState on all objects.
std::list< Renaming > renamings
All the renamings applicable when instantiating objects.
void findTraversalOrder(const std::string &object_name)
Populate objectsInOrder with a preorder, depth first traversal from the given object name down throug...
std::map< std::string, SimObject * > objectsByName
SimObject indexed by name.
void setParam(const std::string &object_name, const std::string ¶m_name, const std::string ¶m_value)
Convenience functions for calling set... member functions on a CxxConfigParams for an object.
void forEachObject(void(SimObject::*mem_func)())
Perform mem_func on each SimObject.
void bindRequestPort(SimObject *object, const CxxConfigDirectoryEntry::PortDesc &port, const std::vector< std::string > &peers)
Bind a single (possibly vectored) request port to peers from the unparsed list peers with elements in...
CxxConfigParams * findObjectParams(const std::string &object_name)
Find the parameters for the named object.
void bindObjectPorts(SimObject *object)
Bind the ports of a single SimObject.
std::map< std::string, CxxConfigParams * > objectParamsByName
...Params objects created by this manager
void loadState(CheckpointIn &checkpoint)
Load all objects' state from the given Checkpoint.
void findAllObjects()
Find all objects by iterating over the object names in the config file with findObject.
SimObject * findObject(const std::string &object_name, bool visit_children=false)
Walk the configuration starting with object object_name and fill in all the elements of this object o...
void bindAllPorts()
Bind the ports of all the objects in objectInOrder order.
void instantiate(bool build_all=true)
Build all objects (if build_all is true, otherwise objects must have been individually findObject-ed ...
const CxxConfigDirectoryEntry & findObjectType(const std::string &object_name, std::string &object_type)
Find the type field for a named object and return both the name of the type to object_type and the ob...
Base for peer classes of SimObjectParams derived classes with parameter modifying member functions.
virtual bool setParam(const std::string &name, const std::string &value, const Flags flags)
Set a parameter with a value parsed from the given string.
virtual bool setPortConnectionCount(const std::string &name, unsigned int count)
Set the number of connections expected for the named port.
virtual const std::string & getName()
Get full path name string.
virtual bool setSimObjectVector(const std::string &name, const std::vector< SimObject * > &simObjects)
As setSimObjectVector but set a whole vector of references.
virtual SimObject * simObjectCreate()
Create the associated SimObject.
virtual void setName(const std::string &name_)
Example flag.
virtual bool setSimObject(const std::string &name, SimObject *simObject)
Set a SimObject valued parameter with a reference to the given SimObject.
virtual bool setParamVector(const std::string &name, const std::vector< std::string > &values, const Flags flags)
As setParamVector but for parameters given as vectors pre-separated into elements.
static DrainManager & instance()
Get the singleton DrainManager instance.
virtual std::string name() const
Ports are used to interface objects to each other.
bool isConnected() const
Is this port currently connected to a peer?
virtual void bind(Port &peer)
Attach to a peer port.
Abstract superclass for simulation objects.
C++-only configuration and instantiation support.
void resume()
Resume normal simulation in a Drained system.
bool tryDrain()
Try to drain the system.
virtual void initState()
initState() is called on each SimObject when not restoring from a checkpoint.
virtual void regProbeListeners()
Register probe listeners for this object.
virtual void startup()
startup() is the final initialization call before simulation.
virtual void regProbePoints()
Register probe points for this object.
virtual Port & getPort(const std::string &if_name, PortID idx=InvalidPortID)
Get a port with a given name and index.
virtual void init()
init() is called after all C++ SimObjects have been created and all ports are connected.
virtual void regStats()
Callback to set stat parameters.
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
std::map< std::string, CxxConfigDirectoryEntry * > & cxxConfigDirectory()
Directory of all SimObject classes config details.
int16_t PortID
Port index/ID type, and a symbolic name for an invalid port id.
static std::string formatParamList(const std::vector< std::string > ¶m_values)
std::string csprintf(const char *format, const Args &...args)
Name substitution when instantiating any object whose name starts with fromPrefix.