Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Avoid depending on C++11 stuff when including C/SMPI headers
[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/ex.hpp>
10 #include "xbt/sysdep.h"
11 #include "xbt/log.h"
12 #include "xbt/dict.h"
13 #include "mc/mc.h"
14
15 #include "src/simix/SynchroIo.hpp"
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
18
19
20 /**
21  * \brief Internal function to create a SIMIX storage.
22  * \param name name of the storage to create
23  * \param storage the SURF storage to encapsulate
24  * \param data some user data (may be nullptr)
25  */
26 smx_storage_t SIMIX_storage_create(const char *name, void *storage, void *data)
27 {
28   smx_storage_priv_t smx_storage = xbt_new0(s_smx_storage_priv_t, 1);
29
30   smx_storage->data = data;
31
32   /* Update global variables */
33   xbt_lib_set(storage_lib,name,SIMIX_STORAGE_LEVEL,smx_storage);
34   return xbt_lib_get_elm_or_null(storage_lib, name);
35 }
36
37 /**
38  * \brief Internal function to destroy a SIMIX storage.
39  *
40  * \param s the host to destroy (a smx_storage_t)
41  */
42 void SIMIX_storage_destroy(void *s)
43 {
44   smx_storage_priv_t storage = (smx_storage_priv_t) s;
45
46   xbt_assert((storage != nullptr), "Invalid parameters");
47   if (storage->data)
48     free(storage->data);
49
50   /* Clean storage structure */
51   free(storage);
52 }
53
54 //SIMIX FILE READ
55 void simcall_HANDLER_file_read(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
56 {
57   smx_synchro_t synchro = SIMIX_file_read(fd, size, host);
58   synchro->simcalls.push_back(simcall);
59   simcall->issuer->waiting_synchro = synchro;
60 }
61
62 smx_synchro_t SIMIX_file_read(smx_file_t fd, sg_size_t size, sg_host_t host)
63 {
64   /* check if the host is active */
65   if (host->isOff())
66     THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
67
68
69   simgrid::simix::Io *synchro = new simgrid::simix::Io();
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   synchro->simcalls.push_back(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->host = host;
94   synchro->surf_io = surf_host_write(host, fd->surf_file, size);
95   synchro->surf_io->setData(synchro);
96   XBT_DEBUG("Create io synchro %p", synchro);
97
98   return synchro;
99 }
100
101 //SIMIX FILE OPEN
102 void simcall_HANDLER_file_open(smx_simcall_t simcall, const char* fullpath, sg_host_t host)
103 {
104   smx_synchro_t synchro = SIMIX_file_open(fullpath, host);
105   synchro->simcalls.push_back(simcall);
106   simcall->issuer->waiting_synchro = synchro;
107 }
108
109 smx_synchro_t SIMIX_file_open(const char* fullpath, sg_host_t host)
110 {
111   if (host->isOff())
112     THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
113
114   simgrid::simix::Io *synchro = new simgrid::simix::Io();
115   synchro->host = host;
116   synchro->surf_io = surf_host_open(host, fullpath);
117   synchro->surf_io->setData(synchro);
118   XBT_DEBUG("Create io synchro %p", synchro);
119
120   return synchro;
121 }
122
123 //SIMIX FILE CLOSE
124 void simcall_HANDLER_file_close(smx_simcall_t simcall, smx_file_t fd, sg_host_t host)
125 {
126   smx_synchro_t synchro = SIMIX_file_close(fd, host);
127   synchro->simcalls.push_back(simcall);
128   simcall->issuer->waiting_synchro = synchro;
129 }
130
131 smx_synchro_t SIMIX_file_close(smx_file_t fd, sg_host_t host)
132 {
133   if (host->isOff())
134     THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
135
136   simgrid::simix::Io *synchro = new simgrid::simix::Io();
137   synchro->host = host;
138   synchro->surf_io = surf_host_close(host, fd->surf_file);
139   synchro->surf_io->setData(synchro);
140   XBT_DEBUG("Create io synchro %p", synchro);
141
142   return synchro;
143 }
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", sg_host_get_name(host));
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_process_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_process_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_process_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_process_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_process_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 sg_size_t SIMIX_storage_get_size(smx_storage_t storage){
214   return surf_storage_get_size(storage);
215 }
216
217 sg_size_t simcall_HANDLER_storage_get_free_size(smx_simcall_t simcall, smx_storage_t storage)
218 {
219   return SIMIX_storage_get_free_size(simcall->issuer, storage);
220 }
221
222 sg_size_t SIMIX_storage_get_free_size(smx_process_t process, smx_storage_t storage)
223 {
224   return  surf_storage_get_free_size(storage);
225 }
226
227 sg_size_t simcall_HANDLER_storage_get_used_size(smx_simcall_t simcall, smx_storage_t storage)
228 {
229   return SIMIX_storage_get_used_size(simcall->issuer, storage);
230 }
231
232 sg_size_t SIMIX_storage_get_used_size(smx_process_t process, smx_storage_t storage)
233 {
234   return  surf_storage_get_used_size(storage);
235 }
236
237 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
238   return surf_storage_get_properties(storage);
239 }
240
241 const char* SIMIX_storage_get_name(smx_storage_t storage){
242   return sg_storage_name(storage);
243 }
244
245 xbt_dict_t SIMIX_storage_get_content(smx_storage_t storage){
246   return surf_storage_get_content(storage);
247 }
248
249 const char* SIMIX_storage_get_host(smx_storage_t storage){
250   return surf_storage_get_host(storage);
251 }
252
253 void SIMIX_post_io(smx_synchro_t synchro)
254 {
255 }
256
257 void SIMIX_io_destroy(smx_synchro_t synchro)
258 {
259   simgrid::simix::Io *io = static_cast<simgrid::simix::Io*>(synchro);
260   XBT_DEBUG("Destroy synchro %p", synchro);
261   if (io->surf_io)
262     io->surf_io->unref();
263   delete io;
264 }
265
266 void SIMIX_io_finish(smx_synchro_t synchro)
267 {
268   for (smx_simcall_t simcall : synchro->simcalls) {
269
270     switch (synchro->state) {
271
272       case SIMIX_DONE:
273         /* do nothing, synchro done */
274         break;
275
276       case SIMIX_FAILED:
277         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
278         break;
279
280       case SIMIX_CANCELED:
281         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
282         break;
283
284       default:
285         xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d",
286             (int)synchro->state);
287     }
288
289     if (simcall->issuer->host->isOff()) {
290       simcall->issuer->context->iwannadie = 1;
291     }
292
293     simcall->issuer->waiting_synchro = nullptr;
294     SIMIX_simcall_answer(simcall);
295   }
296
297   /* We no longer need it */
298   SIMIX_io_destroy(synchro);
299 }