Logo AND Algorithmique Numérique Distribuée

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