Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
update function documentation and variable names in the As -> NetZone transition
[simgrid.git] / src / s4u / s4u_engine.cpp
1 /* s4u::Engine Simulation Engine and global functions. */
2
3 /* Copyright (c) 2006-2015. The SimGrid Team. All rights reserved.          */
4
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. */
7
8 #include "instr/instr_interface.h"
9 #include "mc/mc.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/NetZoneImpl.hpp"
18
19 #include "src/surf/network_interface.hpp"
20 #include "src/surf/surf_routing.hpp" // routing_platf. FIXME:KILLME. SOON
21 #include "surf/surf.h"               // routing_platf. FIXME:KILLME. SOON
22
23 XBT_LOG_NEW_CATEGORY(s4u,"Log channels of the S4U (Simgrid for you) interface");
24
25 namespace simgrid {
26 namespace s4u {
27
28 Engine *Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
29
30
31 Engine::Engine(int *argc, char **argv) {
32   xbt_assert(s4u::Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
33   s4u::Engine::instance_ = this;
34   pimpl                  = new kernel::EngineImpl();
35
36   TRACE_global_init(argc, argv);
37   SIMIX_global_init(argc, argv);
38 }
39
40 Engine::~Engine()
41 {
42   delete pimpl;
43 }
44
45 Engine *Engine::instance() {
46   if (s4u::Engine::instance_ == nullptr)
47     new Engine(0,nullptr);
48   return s4u::Engine::instance_;
49 }
50
51 void Engine::shutdown() {
52   delete s4u::Engine::instance_;
53   s4u::Engine::instance_ = nullptr;
54   delete s4u::Storage::storages_;
55 }
56
57 double Engine::getClock()
58 {
59   return SIMIX_get_clock();
60 }
61
62 void Engine::loadPlatform(const char *platf)
63 {
64   SIMIX_create_environment(platf);
65 }
66
67 void Engine::registerFunction(const char*name, int (*code)(int,char**))
68 {
69   SIMIX_function_register(name,code);
70 }
71 void Engine::registerDefault(int (*code)(int,char**))
72 {
73   SIMIX_function_register_default(code);
74 }
75 void Engine::loadDeployment(const char *deploy)
76 {
77   SIMIX_launch_application(deploy);
78 }
79
80 void Engine::run() {
81   if (MC_is_active()) {
82     MC_run();
83   } else {
84     SIMIX_run();
85   }
86 }
87
88 s4u::NetZone* Engine::netRoot()
89 {
90   return pimpl->netRoot_;
91 }
92
93 static s4u::NetZone* netzoneByNameRecursive(s4u::NetZone* current, const char* name)
94 {
95   if(!strcmp(current->name(), name))
96     return current;
97
98   xbt_dict_cursor_t cursor = nullptr;
99   char *key;
100   NetZone_t elem;
101   xbt_dict_foreach(current->children(), cursor, key, elem) {
102     simgrid::s4u::NetZone* tmp = netzoneByNameRecursive(elem, name);
103     if (tmp != nullptr )
104         return tmp;
105   }
106   return nullptr;
107 }
108
109 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
110 NetZone* Engine::netzoneByNameOrNull(const char* name)
111 {
112   return netzoneByNameRecursive(netRoot(), name);
113 }
114
115 }
116 }