Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
turn SIMIX_comm_new() into a proper constructor
[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->name = NULL;
70   synchro->host = host;
71   synchro->surf_io = surf_host_read(host, fd->surf_file, size);
72
73   synchro->surf_io->setData(synchro);
74   XBT_DEBUG("Create io synchro %p", synchro);
75
76   return synchro;
77 }
78
79 //SIMIX FILE WRITE
80 void simcall_HANDLER_file_write(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
81 {
82   smx_synchro_t synchro = SIMIX_file_write(fd,  size, host);
83   xbt_fifo_push(synchro->simcalls, simcall);
84   simcall->issuer->waiting_synchro = synchro;
85 }
86
87 smx_synchro_t SIMIX_file_write(smx_file_t fd, sg_size_t size, sg_host_t host)
88 {
89   if (host->isOff())
90     THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
91
92   simgrid::simix::Io *synchro = new simgrid::simix::Io();
93   synchro->name = NULL;
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_synchro_t synchro = SIMIX_file_open(fullpath, host);
106   xbt_fifo_push(synchro->simcalls, simcall);
107   simcall->issuer->waiting_synchro = synchro;
108 }
109
110 smx_synchro_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", sg_host_get_name(host));
114
115   simgrid::simix::Io *synchro = new simgrid::simix::Io();
116   synchro->name = NULL;
117   synchro->host = host;
118   synchro->surf_io = surf_host_open(host, fullpath);
119   synchro->surf_io->setData(synchro);
120   XBT_DEBUG("Create io synchro %p", synchro);
121
122   return synchro;
123 }
124
125 //SIMIX FILE CLOSE
126 void simcall_HANDLER_file_close(smx_simcall_t simcall, smx_file_t fd, sg_host_t host)
127 {
128   smx_synchro_t synchro = SIMIX_file_close(fd, host);
129   xbt_fifo_push(synchro->simcalls, simcall);
130   simcall->issuer->waiting_synchro = synchro;
131 }
132
133 smx_synchro_t SIMIX_file_close(smx_file_t fd, sg_host_t host)
134 {
135   if (host->isOff())
136     THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
137
138   simgrid::simix::Io *synchro = new simgrid::simix::Io();
139   synchro->name = NULL;
140   synchro->host = host;
141   synchro->surf_io = surf_host_close(host, fd->surf_file);
142   synchro->surf_io->setData(synchro);
143   XBT_DEBUG("Create io synchro %p", synchro);
144
145   return synchro;
146 }
147
148
149 //SIMIX FILE UNLINK
150 int SIMIX_file_unlink(smx_file_t fd, sg_host_t host)
151 {
152   if (host->isOff())
153     THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
154
155   int res = surf_host_unlink(host, fd->surf_file);
156   xbt_free(fd);
157   return !!res;
158 }
159
160 sg_size_t simcall_HANDLER_file_get_size(smx_simcall_t simcall, smx_file_t fd)
161 {
162   return SIMIX_file_get_size(simcall->issuer, fd);
163 }
164
165 sg_size_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd)
166 {
167   sg_host_t host = process->host;
168   return  surf_host_get_size(host, fd->surf_file);
169 }
170
171 sg_size_t simcall_HANDLER_file_tell(smx_simcall_t simcall, smx_file_t fd)
172 {
173   return SIMIX_file_tell(simcall->issuer, fd);
174 }
175
176 sg_size_t SIMIX_file_tell(smx_process_t process, smx_file_t fd)
177 {
178   sg_host_t host = process->host;
179   return  surf_host_file_tell(host, fd->surf_file);
180 }
181
182
183 xbt_dynar_t simcall_HANDLER_file_get_info(smx_simcall_t simcall, smx_file_t fd)
184 {
185   return SIMIX_file_get_info(simcall->issuer, fd);
186 }
187
188 xbt_dynar_t SIMIX_file_get_info(smx_process_t process, smx_file_t fd)
189 {
190   sg_host_t host = process->host;
191   return  surf_host_get_info(host, fd->surf_file);
192 }
193
194 int simcall_HANDLER_file_seek(smx_simcall_t simcall, smx_file_t fd, sg_offset_t offset, int origin)
195 {
196   return SIMIX_file_seek(simcall->issuer, fd, offset, origin);
197 }
198
199 int SIMIX_file_seek(smx_process_t process, smx_file_t fd, sg_offset_t offset, int origin)
200 {
201   sg_host_t host = process->host;
202   return  surf_host_file_seek(host, fd->surf_file, offset, origin);
203 }
204
205 int simcall_HANDLER_file_move(smx_simcall_t simcall, smx_file_t file, const char* fullpath)
206 {
207   return SIMIX_file_move(simcall->issuer, file, fullpath);
208 }
209
210 int SIMIX_file_move(smx_process_t process, smx_file_t file, const char* fullpath)
211 {
212   sg_host_t host = process->host;
213   return  surf_host_file_move(host, file->surf_file, fullpath);
214 }
215
216 sg_size_t SIMIX_storage_get_size(smx_storage_t storage){
217   return surf_storage_get_size(storage);
218 }
219
220 sg_size_t simcall_HANDLER_storage_get_free_size(smx_simcall_t simcall, smx_storage_t storage)
221 {
222   return SIMIX_storage_get_free_size(simcall->issuer, storage);
223 }
224
225 sg_size_t SIMIX_storage_get_free_size(smx_process_t process, smx_storage_t storage)
226 {
227   return  surf_storage_get_free_size(storage);
228 }
229
230 sg_size_t simcall_HANDLER_storage_get_used_size(smx_simcall_t simcall, smx_storage_t storage)
231 {
232   return SIMIX_storage_get_used_size(simcall->issuer, storage);
233 }
234
235 sg_size_t SIMIX_storage_get_used_size(smx_process_t process, smx_storage_t storage)
236 {
237   return  surf_storage_get_used_size(storage);
238 }
239
240 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
241   return surf_storage_get_properties(storage);
242 }
243
244 const char* SIMIX_storage_get_name(smx_storage_t storage){
245   return sg_storage_name(storage);
246 }
247
248 xbt_dict_t SIMIX_storage_get_content(smx_storage_t storage){
249   return surf_storage_get_content(storage);
250 }
251
252 const char* SIMIX_storage_get_host(smx_storage_t storage){
253   return surf_storage_get_host(storage);
254 }
255
256 void SIMIX_post_io(smx_synchro_t synchro)
257 {
258   xbt_fifo_item_t i;
259   smx_simcall_t simcall;
260
261   simgrid::simix::Io *io = static_cast<simgrid::simix::Io*>(synchro);
262
263   xbt_fifo_foreach(synchro->simcalls,i,simcall,smx_simcall_t) {
264     switch (simcall->call) {
265     case SIMCALL_FILE_OPEN: {
266       smx_file_t tmp = xbt_new(s_smx_file_t,1);
267       tmp->surf_file = surf_storage_action_get_file(io->surf_io);
268       simcall_file_open__set__result(simcall, tmp);
269       break;
270     }
271     case SIMCALL_FILE_CLOSE:
272       xbt_free(simcall_file_close__get__fd(simcall));
273       simcall_file_close__set__result(simcall, 0);
274       break;
275     case SIMCALL_FILE_WRITE:
276       simcall_file_write__set__result(simcall, io->surf_io->getCost());
277       break;
278
279     case SIMCALL_FILE_READ:
280       simcall_file_read__set__result(simcall, io->surf_io->getCost());
281       break;
282
283     default:
284       break;
285     }
286   }
287
288   switch (io->surf_io->getState()) {
289
290     case simgrid::surf::Action::State::failed:
291       synchro->state = SIMIX_FAILED;
292       break;
293
294     case simgrid::surf::Action::State::done:
295       synchro->state = SIMIX_DONE;
296       break;
297
298     default:
299       THROW_IMPOSSIBLE;
300       break;
301   }
302
303   SIMIX_io_finish(synchro);
304 }
305
306 void SIMIX_io_destroy(smx_synchro_t synchro)
307 {
308   simgrid::simix::Io *io = static_cast<simgrid::simix::Io*>(synchro);
309   XBT_DEBUG("Destroy synchro %p", synchro);
310   if (io->surf_io)
311     io->surf_io->unref();
312   delete io;
313 }
314
315 void SIMIX_io_finish(smx_synchro_t synchro)
316 {
317   xbt_fifo_item_t item;
318   smx_simcall_t simcall;
319
320   xbt_fifo_foreach(synchro->simcalls, item, simcall, smx_simcall_t) {
321
322     switch (synchro->state) {
323
324       case SIMIX_DONE:
325         /* do nothing, synchro done */
326         break;
327
328       case SIMIX_FAILED:
329         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
330         break;
331
332       case SIMIX_CANCELED:
333         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
334         break;
335
336       default:
337         xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d",
338             (int)synchro->state);
339     }
340
341     if (simcall->issuer->host->isOff()) {
342       simcall->issuer->context->iwannadie = 1;
343     }
344
345     simcall->issuer->waiting_synchro = NULL;
346     SIMIX_simcall_answer(simcall);
347   }
348
349   /* We no longer need it */
350   SIMIX_io_destroy(synchro);
351 }