Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[simgrid.git] / src / simix / smx_io.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 "simgrid/s4u/Host.hpp"
9 #include "simgrid/s4u/Io.hpp"
10
11 #include "smx_private.hpp"
12 #include "src/kernel/activity/IoImpl.hpp"
13 #include "src/mc/mc_replay.hpp"
14 #include "src/simix/smx_io_private.hpp"
15 #include "src/surf/StorageImpl.hpp"
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
18
19 simgrid::kernel::activity::IoImplPtr SIMIX_io_start(std::string name, sg_size_t size, sg_storage_t storage,
20                                                     simgrid::s4u::Io::OpType type)
21 {
22   /* set surf's action */
23   simgrid::kernel::resource::Action* surf_action = storage->get_impl()->io_start(size, type);
24
25   simgrid::kernel::activity::IoImplPtr io =
26       simgrid::kernel::activity::IoImplPtr(new simgrid::kernel::activity::IoImpl(name, surf_action, storage));
27
28   XBT_DEBUG("Create IO synchro %p %s", io.get(), name.c_str());
29   simgrid::kernel::activity::IoImpl::on_creation(io);
30
31   return io;
32 }
33
34 void simcall_HANDLER_io_wait(smx_simcall_t simcall, smx_activity_t synchro)
35 {
36   XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro.get(), (int)synchro->state_);
37
38   /* Associate this simcall to the synchro */
39   synchro->simcalls_.push_back(simcall);
40   simcall->issuer->waiting_synchro = synchro;
41
42   /* set surf's synchro */
43   if (MC_is_active() || MC_record_replay_is_active()) {
44     synchro->state_ = SIMIX_DONE;
45     SIMIX_io_finish(synchro);
46     return;
47   }
48
49   /* If the synchro is already finished then perform the error handling */
50   if (synchro->state_ != SIMIX_RUNNING)
51     SIMIX_io_finish(synchro);
52 }
53
54 void SIMIX_io_finish(smx_activity_t synchro)
55 {
56   for (smx_simcall_t const& simcall : synchro->simcalls_) {
57     switch (synchro->state_) {
58       case SIMIX_DONE:
59         /* do nothing, synchro done */
60         break;
61       case SIMIX_FAILED:
62         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
63         break;
64       case SIMIX_CANCELED:
65         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
66         break;
67       default:
68         xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d", static_cast<int>(synchro->state_));
69     }
70
71     if (simcall->issuer->host_->is_off()) {
72       simcall->issuer->context_->iwannadie = 1;
73     }
74
75     simcall->issuer->waiting_synchro = nullptr;
76     SIMIX_simcall_answer(simcall);
77   }
78 }