Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
53fd30cd5d0a82b8e99ed59a35476f0fc7006467
[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_parallel_start(std::string name, int host_nb,
28                                                                       sg_host_t* host_list, double* flops_amount,
29                                                                       double* bytes_amount, double rate, double timeout)
30 {
31
32   /* Check that we are not mixing VMs and PMs in the parallel task */
33   bool is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[0]));
34   for (int i = 1; i < host_nb; i++) {
35     bool tmp_is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[i]));
36     xbt_assert(is_a_vm == tmp_is_a_vm, "parallel_execute: mixing VMs and PMs is not supported (yet).");
37   }
38
39   /* set surf's synchro */
40   simgrid::kernel::resource::Action* surf_action      = nullptr;
41   simgrid::kernel::resource::Action* timeout_detector = nullptr;
42   if (not MC_is_active() && not MC_record_replay_is_active()) {
43     surf_action = surf_host_model->execute_parallel(host_nb, host_list, flops_amount, bytes_amount, rate);
44     if (timeout > 0) {
45       timeout_detector = host_list[0]->pimpl_cpu->sleep(timeout);
46     }
47   }
48
49   simgrid::kernel::activity::ExecImplPtr exec = simgrid::kernel::activity::ExecImplPtr(
50       new simgrid::kernel::activity::ExecImpl(name, "", timeout_detector, nullptr));
51   if (surf_action != nullptr) {
52     exec->surf_action_ = surf_action;
53     exec->surf_action_->set_data(exec.get());
54   }
55   XBT_DEBUG("Create parallel execute synchro %p", exec.get());
56
57   return exec;
58 }
59
60 void simcall_HANDLER_execution_wait(smx_simcall_t simcall, smx_activity_t synchro)
61 {
62   XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro.get(), (int)synchro->state_);
63
64   /* Associate this simcall to the synchro */
65   synchro->simcalls_.push_back(simcall);
66   simcall->issuer->waiting_synchro = synchro;
67
68   /* set surf's synchro */
69   if (MC_is_active() || MC_record_replay_is_active()) {
70     synchro->state_ = SIMIX_DONE;
71     SIMIX_execution_finish(synchro);
72     return;
73   }
74
75   /* If the synchro is already finished then perform the error handling */
76   if (synchro->state_ != SIMIX_RUNNING)
77     SIMIX_execution_finish(synchro);
78 }
79
80 void simcall_HANDLER_execution_test(smx_simcall_t simcall, smx_activity_t synchro)
81 {
82   int res = (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING);
83   if (res) {
84     synchro->simcalls_.push_back(simcall);
85     SIMIX_execution_finish(synchro);
86   } else {
87     SIMIX_simcall_answer(simcall);
88   }
89   simcall_execution_test__set__result(simcall, res);
90 }
91
92 void SIMIX_execution_finish(smx_activity_t synchro)
93 {
94   simgrid::kernel::activity::ExecImplPtr exec =
95       boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(synchro);
96
97   while (not synchro->simcalls_.empty()) {
98     smx_simcall_t simcall = synchro->simcalls_.front();
99     synchro->simcalls_.pop_front();
100     switch (exec->state_) {
101
102       case SIMIX_DONE:
103         /* do nothing, synchro done */
104         XBT_DEBUG("SIMIX_execution_finished: execution successful");
105         break;
106
107       case SIMIX_FAILED:
108         XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", simcall->issuer->host_->get_cname());
109         simcall->issuer->context_->iwannadie = true;
110         simcall->issuer->exception =
111             std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Host failed"));
112         break;
113
114       case SIMIX_CANCELED:
115         XBT_DEBUG("SIMIX_execution_finished: execution canceled");
116         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
117         break;
118
119       case SIMIX_TIMEOUT:
120         XBT_DEBUG("SIMIX_execution_finished: execution timeouted");
121         simcall->issuer->exception = std::make_exception_ptr(simgrid::TimeoutError(XBT_THROW_POINT, "Timeouted"));
122         break;
123
124       default:
125         xbt_die("Internal error in SIMIX_execution_finish: unexpected synchro state %d", (int)exec->state_);
126     }
127     /* Fail the process if the host is down */
128     if (simcall->issuer->host_->is_off())
129       simcall->issuer->context_->iwannadie = true;
130
131     simcall->issuer->waiting_synchro = nullptr;
132     simcall_execution_wait__set__result(simcall, exec->state_);
133     SIMIX_simcall_answer(simcall);
134   }
135 }