Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename smx_synchro_t to smx_activity_t
[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", sg_host_get_name(host));
71
72
73   simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
74   synchro->host = host;
75   synchro->surf_io = surf_host_read(host, fd->surf_file, size);
76
77   synchro->surf_io->setData(synchro);
78   XBT_DEBUG("Create io synchro %p", synchro);
79
80   return synchro;
81 }
82
83 //SIMIX FILE WRITE
84 void simcall_HANDLER_file_write(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
85 {
86   smx_activity_t synchro = SIMIX_file_write(fd,  size, host);
87   synchro->simcalls.push_back(simcall);
88   simcall->issuer->waiting_synchro = synchro;
89 }
90
91 smx_activity_t SIMIX_file_write(smx_file_t fd, sg_size_t size, sg_host_t host)
92 {
93   if (host->isOff())
94     THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
95
96   simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
97   synchro->host = host;
98   synchro->surf_io = surf_host_write(host, fd->surf_file, size);
99   synchro->surf_io->setData(synchro);
100   XBT_DEBUG("Create io synchro %p", synchro);
101
102   return synchro;
103 }
104
105 //SIMIX FILE OPEN
106 void simcall_HANDLER_file_open(smx_simcall_t simcall, const char* fullpath, sg_host_t host)
107 {
108   smx_activity_t synchro = SIMIX_file_open(fullpath, host);
109   synchro->simcalls.push_back(simcall);
110   simcall->issuer->waiting_synchro = synchro;
111 }
112
113 smx_activity_t SIMIX_file_open(const char* fullpath, sg_host_t host)
114 {
115   if (host->isOff())
116     THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
117
118   simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
119   synchro->host = host;
120   synchro->surf_io = surf_host_open(host, fullpath);
121   synchro->surf_io->setData(synchro);
122   XBT_DEBUG("Create io synchro %p", synchro);
123
124   return synchro;
125 }
126
127 //SIMIX FILE CLOSE
128 void simcall_HANDLER_file_close(smx_simcall_t simcall, smx_file_t fd, sg_host_t host)
129 {
130   smx_activity_t synchro = SIMIX_file_close(fd, host);
131   synchro->simcalls.push_back(simcall);
132   simcall->issuer->waiting_synchro = synchro;
133 }
134
135 smx_activity_t SIMIX_file_close(smx_file_t fd, sg_host_t host)
136 {
137   if (host->isOff())
138     THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
139
140   simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
141   synchro->host = host;
142   synchro->surf_io = surf_host_close(host, fd->surf_file);
143   synchro->surf_io->setData(synchro);
144   XBT_DEBUG("Create io synchro %p", synchro);
145
146   return synchro;
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_io_destroy(smx_activity_t synchro)
257 {
258   simgrid::kernel::activity::Io *io = static_cast<simgrid::kernel::activity::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_activity_t synchro)
266 {
267   for (smx_simcall_t simcall : synchro->simcalls) {
268     switch (synchro->state) {
269       case SIMIX_DONE:
270         /* do nothing, synchro done */
271         break;
272       case SIMIX_FAILED:
273         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
274         break;
275       case SIMIX_CANCELED:
276         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
277         break;
278       default:
279         xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d", static_cast<int>(synchro->state));
280     }
281
282     if (simcall->issuer->host->isOff()) {
283       simcall->issuer->context->iwannadie = 1;
284     }
285
286     simcall->issuer->waiting_synchro = nullptr;
287     SIMIX_simcall_answer(simcall);
288   }
289
290   /* We no longer need it */
291   SIMIX_io_destroy(synchro);
292 }