Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright headers.
[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 <xbt/ex.hpp>
7 #include <xbt/sysdep.h>
8 #include <xbt/log.h>
9
10 #include "simgrid/s4u/Host.hpp"
11 #include "simgrid/s4u/Storage.hpp"
12 #include "smx_private.hpp"
13 #include "src/simix/smx_io_private.hpp"
14 #include "src/surf/HostImpl.hpp"
15 #include "src/surf/StorageImpl.hpp"
16 #include "src/surf/surf_interface.hpp"
17 #include "surf/surf.hpp"
18
19 #include "src/kernel/activity/SynchroIo.hpp"
20
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
22
23 void simcall_HANDLER_storage_read(smx_simcall_t simcall, surf_storage_t st, sg_size_t size)
24 {
25   smx_activity_t synchro = SIMIX_storage_read(st, size);
26   synchro->simcalls.push_back(simcall);
27   simcall->issuer->waiting_synchro = synchro;
28 }
29
30 smx_activity_t SIMIX_storage_read(surf_storage_t st, sg_size_t size)
31 {
32   simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
33   synchro->surf_io                           = st->read(size);
34
35   synchro->surf_io->set_data(synchro);
36   XBT_DEBUG("Create io synchro %p", synchro);
37
38   return synchro;
39 }
40
41 void simcall_HANDLER_storage_write(smx_simcall_t simcall, surf_storage_t st, sg_size_t size)
42 {
43   smx_activity_t synchro = SIMIX_storage_write(st, size);
44   synchro->simcalls.push_back(simcall);
45   simcall->issuer->waiting_synchro = synchro;
46 }
47
48 smx_activity_t SIMIX_storage_write(surf_storage_t st, sg_size_t size)
49 {
50   simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
51   synchro->surf_io                           = st->write(size);
52   synchro->surf_io->set_data(synchro);
53   XBT_DEBUG("Create io synchro %p", synchro);
54
55   return synchro;
56 }
57
58 void SIMIX_io_destroy(smx_activity_t synchro)
59 {
60   simgrid::kernel::activity::IoImplPtr io = boost::static_pointer_cast<simgrid::kernel::activity::IoImpl>(synchro);
61   XBT_DEBUG("Destroy synchro %p", synchro.get());
62   if (io->surf_io)
63     io->surf_io->unref();
64 }
65
66 void SIMIX_io_finish(smx_activity_t synchro)
67 {
68   for (smx_simcall_t const& simcall : synchro->simcalls) {
69     switch (synchro->state) {
70       case SIMIX_DONE:
71         /* do nothing, synchro done */
72         break;
73       case SIMIX_FAILED:
74         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
75         break;
76       case SIMIX_CANCELED:
77         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
78         break;
79       default:
80         xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d", static_cast<int>(synchro->state));
81     }
82
83     if (simcall->issuer->host->isOff()) {
84       simcall->issuer->context->iwannadie = 1;
85     }
86
87     simcall->issuer->waiting_synchro = nullptr;
88     SIMIX_simcall_answer(simcall);
89   }
90
91   /* We no longer need it */
92   SIMIX_io_destroy(synchro);
93 }