1 /* s4u::Engine Simulation Engine and global functions. */
3 /* Copyright (c) 2006-2017. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "instr/instr_interface.h"
10 #include "simgrid/s4u/Engine.hpp"
11 #include "simgrid/s4u/Host.hpp"
12 #include "simgrid/s4u/Mailbox.hpp"
13 #include "simgrid/s4u/NetZone.hpp"
14 #include "simgrid/s4u/Storage.hpp"
15 #include "simgrid/simix.h"
16 #include "src/kernel/EngineImpl.hpp"
17 #include "src/kernel/routing/NetPoint.hpp"
18 #include "src/kernel/routing/NetZoneImpl.hpp"
19 #include "src/surf/network_interface.hpp"
20 #include "surf/surf.h" // routing_platf. FIXME:KILLME. SOON
22 XBT_LOG_NEW_CATEGORY(s4u,"Log channels of the S4U (Simgrid for you) interface");
26 xbt::signal<void()> onPlatformCreated;
27 xbt::signal<void()> onSimulationEnd;
28 xbt::signal<void(double)> onTimeAdvance;
30 Engine *Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
33 Engine::Engine(int *argc, char **argv) {
34 xbt_assert(s4u::Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
35 s4u::Engine::instance_ = this;
36 pimpl = new kernel::EngineImpl();
38 TRACE_global_init(argc, argv);
39 SIMIX_global_init(argc, argv);
47 Engine *Engine::instance() {
48 if (s4u::Engine::instance_ == nullptr)
49 new Engine(0,nullptr);
50 return s4u::Engine::instance_;
53 void Engine::shutdown() {
54 delete s4u::Engine::instance_;
55 s4u::Engine::instance_ = nullptr;
56 delete s4u::Storage::storages_;
59 double Engine::getClock()
61 return SIMIX_get_clock();
64 void Engine::loadPlatform(const char *platf)
66 SIMIX_create_environment(platf);
69 void Engine::registerFunction(const char*name, int (*code)(int,char**))
71 SIMIX_function_register(name,code);
73 void Engine::registerDefault(int (*code)(int,char**))
75 SIMIX_function_register_default(code);
77 void Engine::loadDeployment(const char *deploy)
79 SIMIX_launch_application(deploy);
81 // FIXME: The following duplicates the content of s4u::Host
82 extern std::map<std::string, simgrid::s4u::Host*> host_list;
83 /** @brief Returns the amount of hosts in the platform */
84 size_t Engine::hostCount()
86 return host_list.size();
88 /** @brief Fills the passed list with all hosts found in the platform */
89 void Engine::hostList(std::vector<Host*>* list)
91 for (auto kv : host_list)
92 list->push_back(kv.second);
103 s4u::NetZone* Engine::netRoot()
105 return pimpl->netRoot_;
108 static s4u::NetZone* netzoneByNameRecursive(s4u::NetZone* current, const char* name)
110 if(!strcmp(current->name(), name))
113 for (auto elem : *(current->children())) {
114 simgrid::s4u::NetZone* tmp = netzoneByNameRecursive(elem, name);
115 if (tmp != nullptr) {
122 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
123 NetZone* Engine::netzoneByNameOrNull(const char* name)
125 return netzoneByNameRecursive(netRoot(), name);
128 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
129 simgrid::kernel::routing::NetPoint* Engine::netpointByNameOrNull(const char* name)
131 if (pimpl->netpoints_.find(name) == pimpl->netpoints_.end())
133 return pimpl->netpoints_.at(name);
135 /** @brief Fill the provided vector with all existing netpoints */
136 void Engine::netpointList(std::vector<simgrid::kernel::routing::NetPoint*>* list)
138 for (auto kv : pimpl->netpoints_)
139 list->push_back(kv.second);
141 /** @brief Register a new netpoint to the system */
142 void Engine::netpointRegister(simgrid::kernel::routing::NetPoint* point)
144 // simgrid::simix::kernelImmediate([&]{ FIXME: this segfaults in set_thread
145 pimpl->netpoints_[point->name()] = point;
148 /** @brief Unregister a given netpoint */
149 void Engine::netpointUnregister(simgrid::kernel::routing::NetPoint* point)
151 simgrid::simix::kernelImmediate([this, point] {
152 pimpl->netpoints_.erase(point->name());
157 bool Engine::isInitialized()
159 return Engine::instance_ != nullptr;