Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
properly split init/start for Exec activities
[simgrid.git] / src / simix / smx_host.cpp
1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "mc/mc.h"
7 #include "simgrid/Exception.hpp"
8 #include "smx_private.hpp"
9 #include "src/kernel/activity/CommImpl.hpp"
10 #include "src/kernel/activity/ExecImpl.hpp"
11 #include "src/mc/mc_replay.hpp"
12 #include "src/plugins/vm/VirtualMachineImpl.hpp"
13 #include "src/simix/smx_host_private.hpp"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_host, simix, "SIMIX hosts");
16
17 /* needs to be public and without simcall for exceptions and logging events */
18 const char* sg_host_self_get_name()
19 {
20   sg_host_t host = sg_host_self();
21   if (host == nullptr || SIMIX_process_self() == simix_global->maestro_process)
22     return "";
23
24   return host->get_cname();
25 }
26
27 simgrid::kernel::activity::ExecImplPtr SIMIX_execution_start(std::string name, std::string category,
28                                                              double flops_amount, double priority, double bound,
29                                                              sg_host_t host)
30 {
31   /* set surf's action */
32   simgrid::kernel::resource::Action* surf_action = nullptr;
33   if (not MC_is_active() && not MC_record_replay_is_active()) {
34     surf_action = host->pimpl_cpu->execution_start(flops_amount);
35     surf_action->set_priority(priority);
36     if (bound > 0)
37       surf_action->set_bound(bound);
38   }
39
40   simgrid::kernel::activity::ExecImplPtr exec = simgrid::kernel::activity::ExecImplPtr(
41       new simgrid::kernel::activity::ExecImpl(name, category, /*timeout_detector*/ nullptr, host));
42
43   exec->surf_action_ = surf_action;
44   exec->surf_action_->set_data(exec.get());
45
46   XBT_DEBUG("Create execute synchro %p: %s", exec.get(), exec->name_.c_str());
47   simgrid::kernel::activity::ExecImpl::on_creation(exec);
48
49   return exec;
50 }
51
52 simgrid::kernel::activity::ExecImplPtr SIMIX_execution_parallel_start(std::string name, int host_nb,
53                                                                       sg_host_t* host_list, double* flops_amount,
54                                                                       double* bytes_amount, double rate, double timeout)
55 {
56
57   /* Check that we are not mixing VMs and PMs in the parallel task */
58   bool is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[0]));
59   for (int i = 1; i < host_nb; i++) {
60     bool tmp_is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[i]));
61     xbt_assert(is_a_vm == tmp_is_a_vm, "parallel_execute: mixing VMs and PMs is not supported (yet).");
62   }
63
64   /* set surf's synchro */
65   simgrid::kernel::resource::Action* surf_action      = nullptr;
66   simgrid::kernel::resource::Action* timeout_detector = nullptr;
67   if (not MC_is_active() && not MC_record_replay_is_active()) {
68     surf_action = surf_host_model->execute_parallel(host_nb, host_list, flops_amount, bytes_amount, rate);
69     if (timeout > 0) {
70       timeout_detector = host_list[0]->pimpl_cpu->sleep(timeout);
71     }
72   }
73
74   simgrid::kernel::activity::ExecImplPtr exec = simgrid::kernel::activity::ExecImplPtr(
75       new simgrid::kernel::activity::ExecImpl(name, "", timeout_detector, nullptr));
76   exec->surf_action_ = surf_action;
77   exec->surf_action_->set_data(exec.get());
78
79   XBT_DEBUG("Create parallel execute synchro %p", exec.get());
80
81   return exec;
82 }
83
84 void simcall_HANDLER_execution_wait(smx_simcall_t simcall, smx_activity_t synchro)
85 {
86   XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro.get(), (int)synchro->state_);
87
88   /* Associate this simcall to the synchro */
89   synchro->simcalls_.push_back(simcall);
90   simcall->issuer->waiting_synchro = synchro;
91
92   /* set surf's synchro */
93   if (MC_is_active() || MC_record_replay_is_active()) {
94     synchro->state_ = SIMIX_DONE;
95     SIMIX_execution_finish(synchro);
96     return;
97   }
98
99   /* If the synchro is already finished then perform the error handling */
100   if (synchro->state_ != SIMIX_RUNNING)
101     SIMIX_execution_finish(synchro);
102 }
103
104 void simcall_HANDLER_execution_test(smx_simcall_t simcall, smx_activity_t synchro)
105 {
106   int res = (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING);
107   if (res) {
108     synchro->simcalls_.push_back(simcall);
109     SIMIX_execution_finish(synchro);
110   } else {
111     SIMIX_simcall_answer(simcall);
112   }
113   simcall_execution_test__set__result(simcall, res);
114 }
115
116 void SIMIX_execution_finish(smx_activity_t synchro)
117 {
118   simgrid::kernel::activity::ExecImplPtr exec =
119       boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(synchro);
120
121   while (not synchro->simcalls_.empty()) {
122     smx_simcall_t simcall = synchro->simcalls_.front();
123     synchro->simcalls_.pop_front();
124     switch (exec->state_) {
125
126       case SIMIX_DONE:
127         /* do nothing, synchro done */
128         XBT_DEBUG("SIMIX_execution_finished: execution successful");
129         break;
130
131       case SIMIX_FAILED:
132         XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", simcall->issuer->host_->get_cname());
133         simcall->issuer->context_->iwannadie = true;
134         simcall->issuer->exception =
135             std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Host failed"));
136         break;
137
138       case SIMIX_CANCELED:
139         XBT_DEBUG("SIMIX_execution_finished: execution canceled");
140         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
141         break;
142
143       case SIMIX_TIMEOUT:
144         XBT_DEBUG("SIMIX_execution_finished: execution timeouted");
145         simcall->issuer->exception = std::make_exception_ptr(simgrid::TimeoutError(XBT_THROW_POINT, "Timeouted"));
146         break;
147
148       default:
149         xbt_die("Internal error in SIMIX_execution_finish: unexpected synchro state %d", (int)exec->state_);
150     }
151     /* Fail the process if the host is down */
152     if (simcall->issuer->host_->is_off())
153       simcall->issuer->context_->iwannadie = true;
154
155     simcall->issuer->waiting_synchro = nullptr;
156     simcall_execution_wait__set__result(simcall, exec->state_);
157     SIMIX_simcall_answer(simcall);
158   }
159 }
160
161 void SIMIX_set_category(smx_activity_t synchro, std::string category)
162 {
163   if (synchro->state_ != SIMIX_RUNNING)
164     return;
165
166   simgrid::kernel::activity::ExecImplPtr exec =
167       boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(synchro);
168   if (exec != nullptr) {
169     exec->surf_action_->set_category(category);
170     return;
171   }
172
173   simgrid::kernel::activity::CommImplPtr comm =
174       boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
175   if (comm != nullptr) {
176     comm->surf_action_->set_category(category);
177   }
178 }