Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
da624f582d437ee6b9779357ef9771c189a1c2f6
[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 "simgrid/simix.h"
10 #include "mc/mc.h"
11 #include "simgrid/s4u/As.hpp"
12 #include "simgrid/s4u/engine.hpp"
13 #include "simgrid/s4u/Mailbox.hpp"
14 #include "simgrid/s4u/storage.hpp"
15 #include "simgrid/simix.h"
16 #include "src/kernel/EngineImpl.hpp"
17
18 #include "surf/surf.h"               // routing_platf. FIXME:KILLME. SOON
19 #include "src/surf/surf_routing.hpp" // routing_platf. FIXME:KILLME. SOON
20
21 XBT_LOG_NEW_CATEGORY(s4u,"Log channels of the S4U (Simgrid for you) interface");
22
23 namespace simgrid {
24 namespace s4u {
25
26 Engine *Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
27
28
29 Engine::Engine(int *argc, char **argv) {
30   xbt_assert(s4u::Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
31   s4u::Engine::instance_ = this;
32   pimpl                  = new kernel::EngineImpl();
33
34   TRACE_global_init(argc, argv);
35   SIMIX_global_init(argc, argv);
36 }
37
38 Engine::~Engine()
39 {
40   delete pimpl;
41 }
42
43 Engine *Engine::instance() {
44   if (s4u::Engine::instance_ == nullptr)
45     new Engine(0,nullptr);
46   return s4u::Engine::instance_;
47 }
48
49 void Engine::shutdown() {
50   delete s4u::Engine::instance_;
51   s4u::Engine::instance_ = nullptr;
52   delete s4u::Storage::storages_;
53 }
54
55 double Engine::getClock()
56 {
57   return SIMIX_get_clock();
58 }
59
60 void Engine::loadPlatform(const char *platf)
61 {
62   SIMIX_create_environment(platf);
63 }
64
65 void Engine::registerFunction(const char*name, int (*code)(int,char**))
66 {
67   SIMIX_function_register(name,code);
68 }
69 void Engine::registerDefault(int (*code)(int,char**))
70 {
71   SIMIX_function_register_default(code);
72 }
73 void Engine::loadDeployment(const char *deploy)
74 {
75   SIMIX_launch_application(deploy);
76 }
77
78 void Engine::run() {
79   if (MC_is_active()) {
80     MC_run();
81   } else {
82     SIMIX_run();
83   }
84 }
85
86 s4u::As *Engine::rootAs()
87 {
88   return routing_platf->root_; // FIXME: get the root into the Engine directly (and kill the platf)
89   // return pimpl->rootAs_;
90 }
91
92 static s4u::As *asByNameRecursive(s4u::As *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   AS_t elem;
100   xbt_dict_foreach(current->children(), cursor, key, elem) {
101     simgrid::s4u::As *tmp = asByNameRecursive(elem, name);
102     if (tmp != nullptr )
103         return tmp;
104   }
105   return nullptr;
106 }
107
108 /** @brief Retrieve the AS of the given name (or nullptr if not found) */
109 As *Engine::asByNameOrNull(const char *name) {
110   return asByNameRecursive(rootAs(),name);
111 }
112
113 }
114 }