Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5bc58f7017cb0df1104608a1685305162e16138d
[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.h"
16
17 #include "src/surf/surf_interface.hpp"
18 #include "smx_private.h"
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 //SIMIX FILE READ
25 void simcall_HANDLER_file_read(smx_simcall_t simcall, surf_file_t fd, sg_size_t size, sg_host_t host)
26 {
27   smx_activity_t synchro = SIMIX_file_read(fd, size, host);
28   synchro->simcalls.push_back(simcall);
29   simcall->issuer->waiting_synchro = synchro;
30 }
31
32 smx_activity_t SIMIX_file_read(surf_file_t file, sg_size_t size, sg_host_t host)
33 {
34   /* check if the host is active */
35   if (host->isOff())
36     THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->getCname());
37
38   simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
39   synchro->host = host;
40   synchro->surf_io                           = host->pimpl_->read(file, size);
41
42   synchro->surf_io->setData(synchro);
43   XBT_DEBUG("Create io synchro %p", synchro);
44
45   return synchro;
46 }
47
48 //SIMIX FILE WRITE
49 void simcall_HANDLER_file_write(smx_simcall_t simcall, surf_file_t fd, sg_size_t size, sg_host_t host)
50 {
51   smx_activity_t synchro = SIMIX_file_write(fd,  size, host);
52   synchro->simcalls.push_back(simcall);
53   simcall->issuer->waiting_synchro = synchro;
54 }
55
56 smx_activity_t SIMIX_file_write(surf_file_t file, sg_size_t size, sg_host_t host)
57 {
58   if (host->isOff())
59     THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->getCname());
60
61   simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
62   synchro->host = host;
63   synchro->surf_io                           = host->pimpl_->write(file, size);
64   synchro->surf_io->setData(synchro);
65   XBT_DEBUG("Create io synchro %p", synchro);
66
67   return synchro;
68 }
69
70 //SIMIX FILE OPEN
71 void simcall_HANDLER_file_open(smx_simcall_t simcall, const char* mount, const char* path, sg_storage_t st)
72 {
73   smx_activity_t synchro = SIMIX_file_open(mount, path, st);
74   synchro->simcalls.push_back(simcall);
75   simcall->issuer->waiting_synchro = synchro;
76 }
77
78 smx_activity_t SIMIX_file_open(const char* mount, const char* path, sg_storage_t st)
79 {
80   if (st->getHost()->isOff())
81     THROWF(host_error, 0, "Host %s failed, you cannot call this function", st->getHost()->getCname());
82
83   simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
84   synchro->host                              = st->getHost();
85   synchro->surf_io                           = st->pimpl_->open(mount, path);
86   synchro->surf_io->setData(synchro);
87   XBT_DEBUG("Create io synchro %p", synchro);
88
89   return synchro;
90 }
91
92 //SIMIX FILE CLOSE
93 void simcall_HANDLER_file_close(smx_simcall_t simcall, surf_file_t fd, sg_host_t host)
94 {
95   smx_activity_t synchro = SIMIX_file_close(fd, host);
96   synchro->simcalls.push_back(simcall);
97   simcall->issuer->waiting_synchro = synchro;
98 }
99
100 smx_activity_t SIMIX_file_close(surf_file_t file, sg_host_t host)
101 {
102   if (host->isOff())
103     THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->getCname());
104
105   simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
106   synchro->host = host;
107   synchro->surf_io                           = host->pimpl_->close(file);
108   synchro->surf_io->setData(synchro);
109   XBT_DEBUG("Create io synchro %p", synchro);
110
111   return synchro;
112 }
113
114 void SIMIX_io_destroy(smx_activity_t synchro)
115 {
116   simgrid::kernel::activity::IoImplPtr io = boost::static_pointer_cast<simgrid::kernel::activity::IoImpl>(synchro);
117   XBT_DEBUG("Destroy synchro %p", synchro.get());
118   if (io->surf_io)
119     io->surf_io->unref();
120 }
121
122 void SIMIX_io_finish(smx_activity_t synchro)
123 {
124   for (smx_simcall_t simcall : synchro->simcalls) {
125     switch (synchro->state) {
126       case SIMIX_DONE:
127         /* do nothing, synchro done */
128         break;
129       case SIMIX_FAILED:
130         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
131         break;
132       case SIMIX_CANCELED:
133         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
134         break;
135       default:
136         xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d", static_cast<int>(synchro->state));
137     }
138
139     if (simcall->issuer->host->isOff()) {
140       simcall->issuer->context->iwannadie = 1;
141     }
142
143     simcall->issuer->waiting_synchro = nullptr;
144     SIMIX_simcall_answer(simcall);
145   }
146
147   /* We no longer need it */
148   SIMIX_io_destroy(synchro);
149 }