Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement I/O as asynchronous activities
[simgrid.git] / src / simix / smx_io.cpp
1 /* Copyright (c) 2007-2018. 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 "simgrid/s4u/Host.hpp"
7 #include "xbt/ex.hpp"
8
9 #include "smx_private.hpp"
10 #include "src/kernel/activity/IoImpl.hpp"
11 #include "src/simix/smx_io_private.hpp"
12 #include "src/surf/StorageImpl.hpp"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
15
16 simgrid::kernel::activity::IoImplPtr SIMIX_io_start(std::string name, sg_size_t size, sg_storage_t storage)
17 {
18   /* set surf's action */
19   simgrid::kernel::resource::Action* surf_action = storage->pimpl_->io_start(size);
20
21   simgrid::kernel::activity::IoImplPtr io =
22       simgrid::kernel::activity::IoImplPtr(new simgrid::kernel::activity::IoImpl(name, surf_action, storage));
23
24   XBT_DEBUG("Create IO synchro %p %s", io.get(), name.c_str());
25   simgrid::kernel::activity::IoImpl::on_creation(io);
26
27   return io;
28 }
29
30 void simcall_HANDLER_storage_read(smx_simcall_t simcall, surf_storage_t st, sg_size_t size)
31 {
32   smx_activity_t synchro = SIMIX_storage_read(st, size);
33   synchro->simcalls_.push_back(simcall);
34   simcall->issuer->waiting_synchro = synchro;
35 }
36
37 smx_activity_t SIMIX_storage_read(surf_storage_t st, sg_size_t size)
38 {
39   simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
40   synchro->surf_action_                      = st->read(size);
41
42   synchro->surf_action_->set_data(synchro);
43   XBT_DEBUG("Create io synchro %p", synchro);
44
45   return synchro;
46 }
47
48 void simcall_HANDLER_storage_write(smx_simcall_t simcall, surf_storage_t st, sg_size_t size)
49 {
50   smx_activity_t synchro = SIMIX_storage_write(st, size);
51   synchro->simcalls_.push_back(simcall);
52   simcall->issuer->waiting_synchro = synchro;
53 }
54
55 smx_activity_t SIMIX_storage_write(surf_storage_t st, sg_size_t size)
56 {
57   simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
58   synchro->surf_action_                      = st->write(size);
59   synchro->surf_action_->set_data(synchro);
60   XBT_DEBUG("Create io synchro %p", synchro);
61
62   return synchro;
63 }
64
65 void SIMIX_io_destroy(smx_activity_t synchro)
66 {
67   simgrid::kernel::activity::IoImplPtr io = boost::static_pointer_cast<simgrid::kernel::activity::IoImpl>(synchro);
68   XBT_DEBUG("Destroy synchro %p", synchro.get());
69   if (io->surf_action_)
70     io->surf_action_->unref();
71 }
72
73 void SIMIX_io_finish(smx_activity_t synchro)
74 {
75   for (smx_simcall_t const& simcall : synchro->simcalls_) {
76     switch (synchro->state_) {
77       case SIMIX_DONE:
78         /* do nothing, synchro done */
79         break;
80       case SIMIX_FAILED:
81         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
82         break;
83       case SIMIX_CANCELED:
84         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
85         break;
86       default:
87         xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d", static_cast<int>(synchro->state_));
88     }
89
90     if (simcall->issuer->host_->is_off()) {
91       simcall->issuer->context_->iwannadie = 1;
92     }
93
94     simcall->issuer->waiting_synchro = nullptr;
95     SIMIX_simcall_answer(simcall);
96   }
97
98   /* We no longer need it */
99   SIMIX_io_destroy(synchro);
100 }