Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
no need for a lib to store the netcards. A dict is easier
[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 "surf/surf.h"               // routing_platf. FIXME:KILLME. SOON
21
22 XBT_LOG_NEW_CATEGORY(s4u,"Log channels of the S4U (Simgrid for you) interface");
23
24 namespace simgrid {
25 namespace s4u {
26
27 Engine *Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
28
29
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();
34
35   TRACE_global_init(argc, argv);
36   SIMIX_global_init(argc, argv);
37 }
38
39 Engine::~Engine()
40 {
41   delete pimpl;
42 }
43
44 Engine *Engine::instance() {
45   if (s4u::Engine::instance_ == nullptr)
46     new Engine(0,nullptr);
47   return s4u::Engine::instance_;
48 }
49
50 void Engine::shutdown() {
51   delete s4u::Engine::instance_;
52   s4u::Engine::instance_ = nullptr;
53   delete s4u::Storage::storages_;
54 }
55
56 double Engine::getClock()
57 {
58   return SIMIX_get_clock();
59 }
60
61 void Engine::loadPlatform(const char *platf)
62 {
63   SIMIX_create_environment(platf);
64 }
65
66 void Engine::registerFunction(const char*name, int (*code)(int,char**))
67 {
68   SIMIX_function_register(name,code);
69 }
70 void Engine::registerDefault(int (*code)(int,char**))
71 {
72   SIMIX_function_register_default(code);
73 }
74 void Engine::loadDeployment(const char *deploy)
75 {
76   SIMIX_launch_application(deploy);
77 }
78
79 void Engine::run() {
80   if (MC_is_active()) {
81     MC_run();
82   } else {
83     SIMIX_run();
84   }
85 }
86
87 s4u::NetZone* Engine::netRoot()
88 {
89   return pimpl->netRoot_;
90 }
91
92 static s4u::NetZone* netzoneByNameRecursive(s4u::NetZone* current, const char* name)
93 {
94   if(!strcmp(current->name(), name))
95     return current;
96
97   xbt_dict_cursor_t cursor = nullptr;
98   char *key;
99   NetZone_t elem;
100   xbt_dict_foreach(current->children(), cursor, key, elem) {
101     simgrid::s4u::NetZone* tmp = netzoneByNameRecursive(elem, name);
102     if (tmp != nullptr )
103         return tmp;
104   }
105   return nullptr;
106 }
107
108 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
109 NetZone* Engine::netzoneByNameOrNull(const char* name)
110 {
111   return netzoneByNameRecursive(netRoot(), name);
112 }
113
114 }
115 }