Logo AND Algorithmique Numérique Distribuée

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