Logo AND Algorithmique Numérique Distribuée

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