Logo AND Algorithmique Numérique Distribuée

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