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] / teshsuite / surf / surf_usage / surf_usage.cpp
1 /* A few basic tests for the surf library                                   */
2
3 /* Copyright (c) 2004-2017. 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 "src/surf/cpu_interface.hpp"
10 #include "src/surf/network_interface.hpp"
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(surf_test, "Messages specific for surf example");
13
14 static const char *string_action(simgrid::surf::Action::State state)
15 {
16   switch (state) {
17     case simgrid::surf::Action::State::ready:
18       return "SURF_ACTION_READY";
19     case simgrid::surf::Action::State::running:
20       return "SURF_ACTION_RUNNING";
21     case simgrid::surf::Action::State::failed:
22       return "SURF_ACTION_FAILED";
23     case simgrid::surf::Action::State::done:
24       return "SURF_ACTION_DONE";
25     case simgrid::surf::Action::State::not_in_the_system:
26       return "SURF_ACTION_NOT_IN_THE_SYSTEM";
27     default:
28       return "INVALID STATE";
29   }
30 }
31
32 int main(int argc, char **argv)
33 {
34   double now = -1.0;
35   surf_init(&argc, argv);       /* Initialize some common structures */
36   xbt_cfg_set_parse("cpu/model:Cas01");
37   xbt_cfg_set_parse("network/model:CM02");
38
39   xbt_assert(argc > 1, "Usage: %s platform.xml\n", argv[0]);
40   parse_platform_file(argv[1]);
41
42   XBT_DEBUG("CPU model: %p", surf_cpu_model_pm);
43   XBT_DEBUG("Network model: %p", surf_network_model);
44   simgrid::s4u::Host* hostA = sg_host_by_name("Cpu A");
45   simgrid::s4u::Host* hostB = sg_host_by_name("Cpu B");
46
47   /* Let's do something on it */
48   simgrid::surf::Action* actionA = hostA->pimpl_cpu->execution_start(1000.0);
49   simgrid::surf::Action* actionB = hostB->pimpl_cpu->execution_start(1000.0);
50   simgrid::surf::Action* actionC = hostB->pimpl_cpu->sleep(7.32);
51
52   simgrid::surf::Action::State stateActionA = actionA->getState();
53   simgrid::surf::Action::State stateActionB = actionB->getState();
54   simgrid::surf::Action::State stateActionC = actionC->getState();
55
56   /* And just look at the state of these tasks */
57   XBT_INFO("actionA state: %s", string_action(stateActionA));
58   XBT_INFO("actionB state: %s", string_action(stateActionB));
59   XBT_INFO("actionC state: %s", string_action(stateActionC));
60
61
62   /* Let's do something on it */
63   surf_network_model->communicate(hostA, hostB, 150.0, -1.0);
64
65   surf_solve(-1.0);
66   do {
67     simgrid::surf::ActionList *action_list = nullptr;
68     now = surf_get_clock();
69     XBT_INFO("Next Event : %g", now);
70     XBT_DEBUG("\t CPU actions");
71
72     action_list = surf_cpu_model_pm->getFailedActionSet();
73     for(simgrid::surf::ActionList::iterator it(action_list->begin()), itNext = it, itend(action_list->end()) ;
74         it != itend ; it=itNext) {
75       ++itNext;
76       simgrid::surf::Action *action = static_cast<simgrid::surf::CpuAction*>(&*it);
77        XBT_INFO("   CPU Failed action");
78        XBT_DEBUG("\t * Failed : %p", action);
79        action->unref();
80     }
81
82     action_list = surf_cpu_model_pm->getDoneActionSet();
83     for(simgrid::surf::ActionList::iterator it(action_list->begin()), itNext = it, itend(action_list->end()) ;
84         it != itend ; it=itNext) {
85       ++itNext;
86       simgrid::surf::Action *action = static_cast<simgrid::surf::CpuAction*>(&*it);
87       XBT_INFO("   CPU Done action");
88       XBT_DEBUG("\t * Done : %p", action);
89       action->unref();
90     }
91
92     action_list = surf_network_model->getFailedActionSet();
93     for(simgrid::surf::ActionList::iterator it(action_list->begin()), itNext = it, itend(action_list->end()) ;
94         it != itend ; it=itNext) {
95       ++itNext;
96       simgrid::surf::Action *action = static_cast<simgrid::surf::NetworkAction*>(&*it);
97        XBT_INFO("   Network Failed action");
98        XBT_DEBUG("\t * Failed : %p", action);
99        action->unref();
100     }
101
102     action_list = surf_network_model->getDoneActionSet();
103     for(simgrid::surf::ActionList::iterator it(action_list->begin()), itNext = it, itend(action_list->end()) ;
104         it != itend ; it=itNext) {
105       ++itNext;
106       simgrid::surf::Action *action = static_cast<simgrid::surf::NetworkAction*>(&*it);
107       XBT_INFO("   Network Done action");
108       XBT_DEBUG("\t * Done : %p", action);
109       action->unref();
110     }
111
112   } while ((surf_network_model->getRunningActionSet()->size() ||
113            surf_cpu_model_pm->getRunningActionSet()->size()) && surf_solve(-1.0) >= 0.0);
114
115   XBT_DEBUG("Simulation Terminated");
116
117   return 0;
118 }