1 /* s4u::Engine Simulation Engine and global functions. */
3 /* Copyright (c) 2006-2015. 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/Mailbox.hpp"
11 #include "simgrid/s4u/NetZone.hpp"
12 #include "simgrid/s4u/engine.hpp"
13 #include "simgrid/s4u/host.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");
27 Engine *Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
30 Engine::Engine(int *argc, char **argv) {
31 xbt_assert(s4u::Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
32 s4u::Engine::instance_ = this;
33 pimpl = new kernel::EngineImpl();
35 TRACE_global_init(argc, argv);
36 SIMIX_global_init(argc, argv);
44 Engine *Engine::instance() {
45 if (s4u::Engine::instance_ == nullptr)
46 new Engine(0,nullptr);
47 return s4u::Engine::instance_;
50 void Engine::shutdown() {
51 delete s4u::Engine::instance_;
52 s4u::Engine::instance_ = nullptr;
53 delete s4u::Storage::storages_;
56 double Engine::getClock()
58 return SIMIX_get_clock();
61 void Engine::loadPlatform(const char *platf)
63 SIMIX_create_environment(platf);
66 void Engine::registerFunction(const char*name, int (*code)(int,char**))
68 SIMIX_function_register(name,code);
70 void Engine::registerDefault(int (*code)(int,char**))
72 SIMIX_function_register_default(code);
74 void Engine::loadDeployment(const char *deploy)
76 SIMIX_launch_application(deploy);
87 s4u::NetZone* Engine::netRoot()
89 return pimpl->netRoot_;
92 static s4u::NetZone* netzoneByNameRecursive(s4u::NetZone* current, const char* name)
94 if(!strcmp(current->name(), name))
97 xbt_dict_cursor_t cursor = nullptr;
100 xbt_dict_foreach(current->children(), cursor, key, elem) {
101 simgrid::s4u::NetZone* tmp = netzoneByNameRecursive(elem, name);
108 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
109 NetZone* Engine::netzoneByNameOrNull(const char* name)
111 return netzoneByNameRecursive(netRoot(), name);
114 /** @brief Retrieve the netcard of the given name (or nullptr if not found) */
115 simgrid::kernel::routing::NetPoint* Engine::netcardByNameOrNull(const char* name)
117 if (pimpl->netpoints_.find(name) == pimpl->netpoints_.end())
119 return pimpl->netpoints_.at(name);
121 /** @brief Fill the provided vector with all existing netcards */
122 void Engine::netcardList(std::vector<simgrid::kernel::routing::NetPoint*>* list)
124 for (auto kv : pimpl->netpoints_)
125 list->push_back(kv.second);
127 /** @brief Register a new netcard to the system */
128 void Engine::netcardRegister(simgrid::kernel::routing::NetPoint* card)
130 // simgrid::simix::kernelImmediate([&]{ FIXME: this segfaults in set_thread
131 pimpl->netpoints_[card->name()] = card;
134 /** @brief Unregister a given netcard */
135 void Engine::netcardUnregister(simgrid::kernel::routing::NetPoint* card)
137 simgrid::simix::kernelImmediate([&] {
138 pimpl->netpoints_.erase(card->name());