Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
reorganize *LinkImpl stuff
[simgrid.git] / teshsuite / surf / surf_usage / surf_usage.cpp
1 /* A few basic tests for the surf library                                   */
2
3 /* Copyright (c) 2004-2021. 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/host.h"
9 #include "simgrid/kernel/routing/NetZoneImpl.hpp" // full type for NetZoneImpl object
10 #include "simgrid/s4u/Engine.hpp"
11 #include "simgrid/zone.h"
12 #include "src/kernel/EngineImpl.hpp"
13 #include "src/kernel/resource/CpuImpl.hpp"
14 #include "src/kernel/resource/LinkImpl.hpp"
15 #include "xbt/config.hpp"
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(surf_test, "Messages specific for surf example");
18
19 static const char* string_action(simgrid::kernel::resource::Action::State state)
20 {
21   switch (state) {
22     case simgrid::kernel::resource::Action::State::INITED:
23       return "SURF_ACTION_INITED";
24     case simgrid::kernel::resource::Action::State::STARTED:
25       return "SURF_ACTION_RUNNING";
26     case simgrid::kernel::resource::Action::State::FAILED:
27       return "SURF_ACTION_FAILED";
28     case simgrid::kernel::resource::Action::State::FINISHED:
29       return "SURF_ACTION_DONE";
30     case simgrid::kernel::resource::Action::State::IGNORED:
31       return "SURF_ACTION_IGNORED";
32     default:
33       return "INVALID STATE";
34   }
35 }
36
37 int main(int argc, char** argv)
38 {
39   simgrid::s4u::Engine e(&argc, argv);
40   simgrid::s4u::Engine::set_config("cpu/model:Cas01");
41   simgrid::s4u::Engine::set_config("network/model:CM02");
42
43   xbt_assert(argc > 1, "Usage: %s platform.xml\n", argv[0]);
44   e.load_platform(argv[1]);
45
46   const_sg_netzone_t as_zone = e.netzone_by_name_or_null("AS0");
47   auto net_model             = as_zone->get_impl()->get_network_model();
48   auto cpu_model_pm          = as_zone->get_impl()->get_cpu_pm_model();
49
50   XBT_DEBUG("CPU model: %p", cpu_model_pm.get());
51   XBT_DEBUG("Network model: %p", net_model.get());
52   simgrid::s4u::Host* hostA = sg_host_by_name("Cpu A");
53   simgrid::s4u::Host* hostB = sg_host_by_name("Cpu B");
54
55   /* Let's do something on it */
56   const simgrid::kernel::resource::Action* actionA = hostA->get_cpu()->execution_start(1000.0, -1);
57   const simgrid::kernel::resource::Action* actionB = hostB->get_cpu()->execution_start(1000.0, -1);
58   const simgrid::kernel::resource::Action* actionC = hostB->get_cpu()->sleep(7.32);
59
60   simgrid::kernel::resource::Action::State stateActionA = actionA->get_state();
61   simgrid::kernel::resource::Action::State stateActionB = actionB->get_state();
62   simgrid::kernel::resource::Action::State stateActionC = actionC->get_state();
63
64   /* And just look at the state of these tasks */
65   XBT_INFO("actionA state: %s", string_action(stateActionA));
66   XBT_INFO("actionB state: %s", string_action(stateActionB));
67   XBT_INFO("actionC state: %s", string_action(stateActionC));
68
69   /* Let's do something on it */
70   net_model->communicate(hostA, hostB, 150.0, -1.0);
71
72   e.get_impl()->solve(-1.0);
73   do {
74     XBT_INFO("Next Event : %g", simgrid::s4u::Engine::get_clock());
75     XBT_DEBUG("\t CPU actions");
76
77     simgrid::kernel::resource::Action::StateSet* action_list = cpu_model_pm->get_failed_action_set();
78     while (not action_list->empty()) {
79       simgrid::kernel::resource::Action& action = action_list->front();
80       XBT_INFO("   CPU Failed action");
81       XBT_DEBUG("\t * Failed : %p", &action);
82       action.unref();
83     }
84
85     action_list = cpu_model_pm->get_finished_action_set();
86     while (not action_list->empty()) {
87       simgrid::kernel::resource::Action& action = action_list->front();
88       XBT_INFO("   CPU Done action");
89       XBT_DEBUG("\t * Done : %p", &action);
90       action.unref();
91     }
92
93     action_list = net_model->get_failed_action_set();
94     while (not action_list->empty()) {
95       simgrid::kernel::resource::Action& action = action_list->front();
96       XBT_INFO("   Network Failed action");
97       XBT_DEBUG("\t * Failed : %p", &action);
98       action.unref();
99     }
100
101     action_list = net_model->get_finished_action_set();
102     while (not action_list->empty()) {
103       simgrid::kernel::resource::Action& action = action_list->front();
104       XBT_INFO("   Network Done action");
105       XBT_DEBUG("\t * Done : %p", &action);
106       action.unref();
107     }
108   } while ((net_model->get_started_action_set()->size() || cpu_model_pm->get_started_action_set()->size()) &&
109            e.get_impl()->solve(-1.0) >= 0.0);
110
111   XBT_DEBUG("Simulation Terminated");
112
113   return 0;
114 }