Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[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 #include <xbt/dict.h>
10
11 #include "simgrid/s4u/Host.hpp"
12
13 #include <mc/mc.h>
14
15 #include "src/surf/surf_interface.hpp"
16 #include "smx_private.h"
17
18 #include "src/kernel/activity/SynchroIo.hpp"
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
21
22 /**
23  * \brief Internal function to create a SIMIX storage.
24  * \param name name of the storage to create
25  * \param storage the SURF storage to encapsulate
26  * \param data some user data (may be nullptr)
27  */
28 smx_storage_t SIMIX_storage_create(const char *name, void *storage, void *data)
29 {
30   smx_storage_priv_t smx_storage = xbt_new0(s_smx_storage_priv_t, 1);
31
32   smx_storage->data = data;
33
34   /* Update global variables */
35   xbt_lib_set(storage_lib,name,SIMIX_STORAGE_LEVEL,smx_storage);
36   return xbt_lib_get_elm_or_null(storage_lib, name);
37 }
38
39 /**
40  * \brief Internal function to destroy a SIMIX storage.
41  *
42  * \param s the host to destroy (a smx_storage_t)
43  */
44 void SIMIX_storage_destroy(void *s)
45 {
46   smx_storage_priv_t storage = static_cast<smx_storage_priv_t>(s);
47
48   xbt_assert((storage != nullptr), "Invalid parameters");
49   if (storage->data)
50     free(storage->data);
51
52   /* Clean storage structure */
53   free(storage);
54 }
55
56 //SIMIX FILE READ
57 void simcall_HANDLER_file_read(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
58 {
59   smx_activity_t synchro = SIMIX_file_read(fd, size, host);
60   synchro->simcalls.push_back(simcall);
61   simcall->issuer->waiting_synchro = synchro;
62 }
63
64 smx_activity_t SIMIX_file_read(smx_file_t fd, sg_size_t size, sg_host_t host)
65 {
66   /* check if the host is active */
67   if (host->isOff())
68     THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
69
70   simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
71   synchro->host = host;
72   synchro->surf_io = surf_host_read(host, fd->surf_file, size);
73
74   synchro->surf_io->setData(synchro);
75   XBT_DEBUG("Create io synchro %p", synchro);
76
77   return synchro;
78 }
79
80 //SIMIX FILE WRITE
81 void simcall_HANDLER_file_write(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
82 {
83   smx_activity_t synchro = SIMIX_file_write(fd,  size, host);
84   synchro->simcalls.push_back(simcall);
85   simcall->issuer->waiting_synchro = synchro;
86 }
87
88 smx_activity_t SIMIX_file_write(smx_file_t fd, sg_size_t size, sg_host_t host)
89 {
90   if (host->isOff())
91     THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
92
93   simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
94   synchro->host = host;
95   synchro->surf_io = surf_host_write(host, fd->surf_file, size);
96   synchro->surf_io->setData(synchro);
97   XBT_DEBUG("Create io synchro %p", synchro);
98
99   return synchro;
100 }
101
102 //SIMIX FILE OPEN
103 void simcall_HANDLER_file_open(smx_simcall_t simcall, const char* fullpath, sg_host_t host)
104 {
105   smx_activity_t synchro = SIMIX_file_open(fullpath, host);
106   synchro->simcalls.push_back(simcall);
107   simcall->issuer->waiting_synchro = synchro;
108 }
109
110 smx_activity_t SIMIX_file_open(const char* fullpath, sg_host_t host)
111 {
112   if (host->isOff())
113     THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
114
115   simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
116   synchro->host = host;
117   synchro->surf_io = surf_host_open(host, fullpath);
118   synchro->surf_io->setData(synchro);
119   XBT_DEBUG("Create io synchro %p", synchro);
120
121   return synchro;
122 }
123
124 //SIMIX FILE CLOSE
125 void simcall_HANDLER_file_close(smx_simcall_t simcall, smx_file_t fd, sg_host_t host)
126 {
127   smx_activity_t synchro = SIMIX_file_close(fd, host);
128   synchro->simcalls.push_back(simcall);
129   simcall->issuer->waiting_synchro = synchro;
130 }
131
132 smx_activity_t SIMIX_file_close(smx_file_t fd, sg_host_t host)
133 {
134   if (host->isOff())
135     THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
136
137   simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
138   synchro->host = host;
139   synchro->surf_io = surf_host_close(host, fd->surf_file);
140   synchro->surf_io->setData(synchro);
141   XBT_DEBUG("Create io synchro %p", synchro);
142
143   return synchro;
144 }
145
146 //SIMIX FILE UNLINK
147 int SIMIX_file_unlink(smx_file_t fd, sg_host_t host)
148 {
149   if (host->isOff())
150     THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
151
152   int res = surf_host_unlink(host, fd->surf_file);
153   xbt_free(fd);
154   return res;
155 }
156
157 sg_size_t simcall_HANDLER_file_get_size(smx_simcall_t simcall, smx_file_t fd)
158 {
159   return SIMIX_file_get_size(simcall->issuer, fd);
160 }
161
162 sg_size_t SIMIX_file_get_size(smx_actor_t process, smx_file_t fd)
163 {
164   sg_host_t host = process->host;
165   return  surf_host_get_size(host, fd->surf_file);
166 }
167
168 sg_size_t simcall_HANDLER_file_tell(smx_simcall_t simcall, smx_file_t fd)
169 {
170   return SIMIX_file_tell(simcall->issuer, fd);
171 }
172
173 sg_size_t SIMIX_file_tell(smx_actor_t process, smx_file_t fd)
174 {
175   sg_host_t host = process->host;
176   return  surf_host_file_tell(host, fd->surf_file);
177 }
178
179
180 xbt_dynar_t simcall_HANDLER_file_get_info(smx_simcall_t simcall, smx_file_t fd)
181 {
182   return SIMIX_file_get_info(simcall->issuer, fd);
183 }
184
185 xbt_dynar_t SIMIX_file_get_info(smx_actor_t process, smx_file_t fd)
186 {
187   sg_host_t host = process->host;
188   return  surf_host_get_info(host, fd->surf_file);
189 }
190
191 int simcall_HANDLER_file_seek(smx_simcall_t simcall, smx_file_t fd, sg_offset_t offset, int origin)
192 {
193   return SIMIX_file_seek(simcall->issuer, fd, offset, origin);
194 }
195
196 int SIMIX_file_seek(smx_actor_t process, smx_file_t fd, sg_offset_t offset, int origin)
197 {
198   sg_host_t host = process->host;
199   return  surf_host_file_seek(host, fd->surf_file, offset, origin);
200 }
201
202 int simcall_HANDLER_file_move(smx_simcall_t simcall, smx_file_t file, const char* fullpath)
203 {
204   return SIMIX_file_move(simcall->issuer, file, fullpath);
205 }
206
207 int SIMIX_file_move(smx_actor_t process, smx_file_t file, const char* fullpath)
208 {
209   sg_host_t host = process->host;
210   return  surf_host_file_move(host, file->surf_file, fullpath);
211 }
212
213 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
214   return surf_storage_get_properties(storage);
215 }
216
217 const char* SIMIX_storage_get_name(smx_storage_t storage){
218   return sg_storage_name(storage);
219 }
220
221 void SIMIX_io_destroy(smx_activity_t synchro)
222 {
223   simgrid::kernel::activity::Io *io = static_cast<simgrid::kernel::activity::Io*>(synchro);
224   XBT_DEBUG("Destroy synchro %p", synchro);
225   if (io->surf_io)
226     io->surf_io->unref();
227   delete io;
228 }
229
230 void SIMIX_io_finish(smx_activity_t synchro)
231 {
232   for (smx_simcall_t simcall : synchro->simcalls) {
233     switch (synchro->state) {
234       case SIMIX_DONE:
235         /* do nothing, synchro done */
236         break;
237       case SIMIX_FAILED:
238         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
239         break;
240       case SIMIX_CANCELED:
241         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
242         break;
243       default:
244         xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d", static_cast<int>(synchro->state));
245     }
246
247     if (simcall->issuer->host->isOff()) {
248       simcall->issuer->context->iwannadie = 1;
249     }
250
251     simcall->issuer->waiting_synchro = nullptr;
252     SIMIX_simcall_answer(simcall);
253   }
254
255   /* We no longer need it */
256   SIMIX_io_destroy(synchro);
257 }