Logo AND Algorithmique Numérique Distribuée

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