Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c465d624a6117d97fb33dce793d2d04c77a45f42
[simgrid.git] / src / simix / smx_io.cpp
1 /* Copyright (c) 2007-2010, 2012-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "src/surf/surf_interface.hpp"
8 #include "smx_private.h"
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11 #include "xbt/dict.h"
12 #include "mc/mc.h"
13
14 #include "src/simix/SynchroIo.hpp"
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
17
18
19 /**
20  * \brief Internal function to create a SIMIX storage.
21  * \param name name of the storage to create
22  * \param storage the SURF storage to encapsulate
23  * \param data some user data (may be NULL)
24  */
25 smx_storage_t SIMIX_storage_create(const char *name, void *storage, void *data)
26 {
27   smx_storage_priv_t smx_storage = xbt_new0(s_smx_storage_priv_t, 1);
28
29   smx_storage->data = data;
30
31   /* Update global variables */
32   xbt_lib_set(storage_lib,name,SIMIX_STORAGE_LEVEL,smx_storage);
33   return xbt_lib_get_elm_or_null(storage_lib, name);
34 }
35
36 /**
37  * \brief Internal function to destroy a SIMIX storage.
38  *
39  * \param s the host to destroy (a smx_storage_t)
40  */
41 void SIMIX_storage_destroy(void *s)
42 {
43   smx_storage_priv_t storage = (smx_storage_priv_t) s;
44
45   xbt_assert((storage != NULL), "Invalid parameters");
46   if (storage->data)
47     free(storage->data);
48
49   /* Clean storage structure */
50   free(storage);
51 }
52
53 //SIMIX FILE READ
54 void simcall_HANDLER_file_read(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
55 {
56   smx_synchro_t synchro = SIMIX_file_read(fd, size, host);
57   xbt_fifo_push(synchro->simcalls, simcall);
58   simcall->issuer->waiting_synchro = synchro;
59 }
60
61 smx_synchro_t SIMIX_file_read(smx_file_t fd, sg_size_t size, sg_host_t host)
62 {
63   /* check if the host is active */
64   if (host->isOff())
65     THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
66
67
68   simgrid::simix::Io *synchro = new simgrid::simix::Io();
69   synchro->host = host;
70   synchro->surf_io = surf_host_read(host, fd->surf_file, size);
71
72   synchro->surf_io->setData(synchro);
73   XBT_DEBUG("Create io synchro %p", synchro);
74
75   return synchro;
76 }
77
78 //SIMIX FILE WRITE
79 void simcall_HANDLER_file_write(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
80 {
81   smx_synchro_t synchro = SIMIX_file_write(fd,  size, host);
82   xbt_fifo_push(synchro->simcalls, simcall);
83   simcall->issuer->waiting_synchro = synchro;
84 }
85
86 smx_synchro_t SIMIX_file_write(smx_file_t fd, sg_size_t size, sg_host_t host)
87 {
88   if (host->isOff())
89     THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
90
91   simgrid::simix::Io *synchro = new simgrid::simix::Io();
92   synchro->host = host;
93   synchro->surf_io = surf_host_write(host, fd->surf_file, size);
94   synchro->surf_io->setData(synchro);
95   XBT_DEBUG("Create io synchro %p", synchro);
96
97   return synchro;
98 }
99
100 //SIMIX FILE OPEN
101 void simcall_HANDLER_file_open(smx_simcall_t simcall, const char* fullpath, sg_host_t host)
102 {
103   smx_synchro_t synchro = SIMIX_file_open(fullpath, host);
104   xbt_fifo_push(synchro->simcalls, simcall);
105   simcall->issuer->waiting_synchro = synchro;
106 }
107
108 smx_synchro_t SIMIX_file_open(const char* fullpath, sg_host_t host)
109 {
110   if (host->isOff())
111     THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
112
113   simgrid::simix::Io *synchro = new simgrid::simix::Io();
114   synchro->host = host;
115   synchro->surf_io = surf_host_open(host, fullpath);
116   synchro->surf_io->setData(synchro);
117   XBT_DEBUG("Create io synchro %p", synchro);
118
119   return synchro;
120 }
121
122 //SIMIX FILE CLOSE
123 void simcall_HANDLER_file_close(smx_simcall_t simcall, smx_file_t fd, sg_host_t host)
124 {
125   smx_synchro_t synchro = SIMIX_file_close(fd, host);
126   xbt_fifo_push(synchro->simcalls, simcall);
127   simcall->issuer->waiting_synchro = synchro;
128 }
129
130 smx_synchro_t SIMIX_file_close(smx_file_t fd, sg_host_t host)
131 {
132   if (host->isOff())
133     THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
134
135   simgrid::simix::Io *synchro = new simgrid::simix::Io();
136   synchro->host = host;
137   synchro->surf_io = surf_host_close(host, fd->surf_file);
138   synchro->surf_io->setData(synchro);
139   XBT_DEBUG("Create io synchro %p", synchro);
140
141   return synchro;
142 }
143
144
145 //SIMIX FILE UNLINK
146 int SIMIX_file_unlink(smx_file_t fd, sg_host_t host)
147 {
148   if (host->isOff())
149     THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
150
151   int res = surf_host_unlink(host, fd->surf_file);
152   xbt_free(fd);
153   return !!res;
154 }
155
156 sg_size_t simcall_HANDLER_file_get_size(smx_simcall_t simcall, smx_file_t fd)
157 {
158   return SIMIX_file_get_size(simcall->issuer, fd);
159 }
160
161 sg_size_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd)
162 {
163   sg_host_t host = process->host;
164   return  surf_host_get_size(host, fd->surf_file);
165 }
166
167 sg_size_t simcall_HANDLER_file_tell(smx_simcall_t simcall, smx_file_t fd)
168 {
169   return SIMIX_file_tell(simcall->issuer, fd);
170 }
171
172 sg_size_t SIMIX_file_tell(smx_process_t process, smx_file_t fd)
173 {
174   sg_host_t host = process->host;
175   return  surf_host_file_tell(host, fd->surf_file);
176 }
177
178
179 xbt_dynar_t simcall_HANDLER_file_get_info(smx_simcall_t simcall, smx_file_t fd)
180 {
181   return SIMIX_file_get_info(simcall->issuer, fd);
182 }
183
184 xbt_dynar_t SIMIX_file_get_info(smx_process_t process, smx_file_t fd)
185 {
186   sg_host_t host = process->host;
187   return  surf_host_get_info(host, fd->surf_file);
188 }
189
190 int simcall_HANDLER_file_seek(smx_simcall_t simcall, smx_file_t fd, sg_offset_t offset, int origin)
191 {
192   return SIMIX_file_seek(simcall->issuer, fd, offset, origin);
193 }
194
195 int SIMIX_file_seek(smx_process_t process, smx_file_t fd, sg_offset_t offset, int origin)
196 {
197   sg_host_t host = process->host;
198   return  surf_host_file_seek(host, fd->surf_file, offset, origin);
199 }
200
201 int simcall_HANDLER_file_move(smx_simcall_t simcall, smx_file_t file, const char* fullpath)
202 {
203   return SIMIX_file_move(simcall->issuer, file, fullpath);
204 }
205
206 int SIMIX_file_move(smx_process_t process, smx_file_t file, const char* fullpath)
207 {
208   sg_host_t host = process->host;
209   return  surf_host_file_move(host, file->surf_file, fullpath);
210 }
211
212 sg_size_t SIMIX_storage_get_size(smx_storage_t storage){
213   return surf_storage_get_size(storage);
214 }
215
216 sg_size_t simcall_HANDLER_storage_get_free_size(smx_simcall_t simcall, smx_storage_t storage)
217 {
218   return SIMIX_storage_get_free_size(simcall->issuer, storage);
219 }
220
221 sg_size_t SIMIX_storage_get_free_size(smx_process_t process, smx_storage_t storage)
222 {
223   return  surf_storage_get_free_size(storage);
224 }
225
226 sg_size_t simcall_HANDLER_storage_get_used_size(smx_simcall_t simcall, smx_storage_t storage)
227 {
228   return SIMIX_storage_get_used_size(simcall->issuer, storage);
229 }
230
231 sg_size_t SIMIX_storage_get_used_size(smx_process_t process, smx_storage_t storage)
232 {
233   return  surf_storage_get_used_size(storage);
234 }
235
236 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
237   return surf_storage_get_properties(storage);
238 }
239
240 const char* SIMIX_storage_get_name(smx_storage_t storage){
241   return sg_storage_name(storage);
242 }
243
244 xbt_dict_t SIMIX_storage_get_content(smx_storage_t storage){
245   return surf_storage_get_content(storage);
246 }
247
248 const char* SIMIX_storage_get_host(smx_storage_t storage){
249   return surf_storage_get_host(storage);
250 }
251
252 void SIMIX_post_io(smx_synchro_t synchro)
253 {
254 }
255
256 void SIMIX_io_destroy(smx_synchro_t synchro)
257 {
258   simgrid::simix::Io *io = static_cast<simgrid::simix::Io*>(synchro);
259   XBT_DEBUG("Destroy synchro %p", synchro);
260   if (io->surf_io)
261     io->surf_io->unref();
262   delete io;
263 }
264
265 void SIMIX_io_finish(smx_synchro_t synchro)
266 {
267   xbt_fifo_item_t item;
268   smx_simcall_t simcall;
269
270   xbt_fifo_foreach(synchro->simcalls, item, simcall, smx_simcall_t) {
271
272     switch (synchro->state) {
273
274       case SIMIX_DONE:
275         /* do nothing, synchro done */
276         break;
277
278       case SIMIX_FAILED:
279         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
280         break;
281
282       case SIMIX_CANCELED:
283         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
284         break;
285
286       default:
287         xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d",
288             (int)synchro->state);
289     }
290
291     if (simcall->issuer->host->isOff()) {
292       simcall->issuer->context->iwannadie = 1;
293     }
294
295     simcall->issuer->waiting_synchro = NULL;
296     SIMIX_simcall_answer(simcall);
297   }
298
299   /* We no longer need it */
300   SIMIX_io_destroy(synchro);
301 }