Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
introduce type aid_t for Actor's ID (ie, PID)
[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  * \brief Internal function to create a SIMIX storage.
25  * \param name name of the storage to create
26  * \param storage the SURF storage to encapsulate
27  * \param data some user data (may be nullptr)
28  */
29 smx_storage_t SIMIX_storage_create(const char *name, void *storage, void *data)
30 {
31   smx_storage_priv_t smx_storage = xbt_new0(s_smx_storage_priv_t, 1);
32
33   smx_storage->data = data;
34
35   /* Update global variables */
36   xbt_lib_set(storage_lib,name,SIMIX_STORAGE_LEVEL,smx_storage);
37   return xbt_lib_get_elm_or_null(storage_lib, name);
38 }
39
40 /**
41  * \brief Internal function to destroy a SIMIX storage.
42  *
43  * \param s the host to destroy (a smx_storage_t)
44  */
45 void SIMIX_storage_destroy(void *s)
46 {
47   smx_storage_priv_t storage = static_cast<smx_storage_priv_t>(s);
48
49   xbt_assert((storage != nullptr), "Invalid parameters");
50   if (storage->data)
51     free(storage->data);
52
53   /* Clean storage structure */
54   free(storage);
55 }
56
57 //SIMIX FILE READ
58 void simcall_HANDLER_file_read(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
59 {
60   smx_activity_t synchro = SIMIX_file_read(fd, size, host);
61   synchro->simcalls.push_back(simcall);
62   simcall->issuer->waiting_synchro = synchro;
63 }
64
65 smx_activity_t SIMIX_file_read(smx_file_t fd, sg_size_t size, sg_host_t host)
66 {
67   /* check if the host is active */
68   if (host->isOff())
69     THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
70
71   simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
72   synchro->host = host;
73   synchro->surf_io = surf_host_read(host, fd->surf_file, size);
74
75   synchro->surf_io->setData(synchro);
76   XBT_DEBUG("Create io synchro %p", synchro);
77
78   return synchro;
79 }
80
81 //SIMIX FILE WRITE
82 void simcall_HANDLER_file_write(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
83 {
84   smx_activity_t synchro = SIMIX_file_write(fd,  size, host);
85   synchro->simcalls.push_back(simcall);
86   simcall->issuer->waiting_synchro = synchro;
87 }
88
89 smx_activity_t SIMIX_file_write(smx_file_t fd, sg_size_t size, sg_host_t host)
90 {
91   if (host->isOff())
92     THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
93
94   simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
95   synchro->host = host;
96   synchro->surf_io = surf_host_write(host, fd->surf_file, size);
97   synchro->surf_io->setData(synchro);
98   XBT_DEBUG("Create io synchro %p", synchro);
99
100   return synchro;
101 }
102
103 //SIMIX FILE OPEN
104 void simcall_HANDLER_file_open(smx_simcall_t simcall, const char* fullpath, sg_host_t host)
105 {
106   smx_activity_t synchro = SIMIX_file_open(fullpath, host);
107   synchro->simcalls.push_back(simcall);
108   simcall->issuer->waiting_synchro = synchro;
109 }
110
111 smx_activity_t SIMIX_file_open(const char* fullpath, sg_host_t host)
112 {
113   if (host->isOff())
114     THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
115
116   simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
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_activity_t synchro = SIMIX_file_close(fd, host);
129   synchro->simcalls.push_back(simcall);
130   simcall->issuer->waiting_synchro = synchro;
131 }
132
133 smx_activity_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", host->cname());
137
138   simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
139   synchro->host = host;
140   synchro->surf_io = surf_host_close(host, fd->surf_file);
141   synchro->surf_io->setData(synchro);
142   XBT_DEBUG("Create io synchro %p", synchro);
143
144   return synchro;
145 }
146
147 //SIMIX FILE UNLINK
148 int SIMIX_file_unlink(smx_file_t fd, sg_host_t host)
149 {
150   if (host->isOff())
151     THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
152
153   int res = surf_host_unlink(host, fd->surf_file);
154   xbt_free(fd);
155   return res;
156 }
157
158 sg_size_t simcall_HANDLER_file_get_size(smx_simcall_t simcall, smx_file_t fd)
159 {
160   return SIMIX_file_get_size(simcall->issuer, fd);
161 }
162
163 sg_size_t SIMIX_file_get_size(smx_actor_t process, smx_file_t fd)
164 {
165   sg_host_t host = process->host;
166   return  surf_host_get_size(host, fd->surf_file);
167 }
168
169 sg_size_t simcall_HANDLER_file_tell(smx_simcall_t simcall, smx_file_t fd)
170 {
171   return SIMIX_file_tell(simcall->issuer, fd);
172 }
173
174 sg_size_t SIMIX_file_tell(smx_actor_t process, smx_file_t fd)
175 {
176   sg_host_t host = process->host;
177   return  surf_host_file_tell(host, fd->surf_file);
178 }
179
180
181 xbt_dynar_t simcall_HANDLER_file_get_info(smx_simcall_t simcall, smx_file_t fd)
182 {
183   return SIMIX_file_get_info(simcall->issuer, fd);
184 }
185
186 xbt_dynar_t SIMIX_file_get_info(smx_actor_t process, smx_file_t fd)
187 {
188   sg_host_t host = process->host;
189   return  surf_host_get_info(host, fd->surf_file);
190 }
191
192 int simcall_HANDLER_file_seek(smx_simcall_t simcall, smx_file_t fd, sg_offset_t offset, int origin)
193 {
194   return SIMIX_file_seek(simcall->issuer, fd, offset, origin);
195 }
196
197 int SIMIX_file_seek(smx_actor_t process, smx_file_t fd, sg_offset_t offset, int origin)
198 {
199   sg_host_t host = process->host;
200   return  surf_host_file_seek(host, fd->surf_file, offset, origin);
201 }
202
203 int simcall_HANDLER_file_move(smx_simcall_t simcall, smx_file_t file, const char* fullpath)
204 {
205   return SIMIX_file_move(simcall->issuer, file, fullpath);
206 }
207
208 int SIMIX_file_move(smx_actor_t process, smx_file_t file, const char* fullpath)
209 {
210   sg_host_t host = process->host;
211   return  surf_host_file_move(host, file->surf_file, fullpath);
212 }
213
214 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
215   return surf_storage_get_properties(storage);
216 }
217
218 const char* SIMIX_storage_get_name(smx_storage_t storage){
219   return sg_storage_name(storage);
220 }
221
222 void SIMIX_io_destroy(smx_activity_t synchro)
223 {
224   simgrid::kernel::activity::Io *io = static_cast<simgrid::kernel::activity::Io*>(synchro);
225   XBT_DEBUG("Destroy synchro %p", synchro);
226   if (io->surf_io)
227     io->surf_io->unref();
228   delete io;
229 }
230
231 void SIMIX_io_finish(smx_activity_t synchro)
232 {
233   for (smx_simcall_t simcall : synchro->simcalls) {
234     switch (synchro->state) {
235       case SIMIX_DONE:
236         /* do nothing, synchro done */
237         break;
238       case SIMIX_FAILED:
239         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
240         break;
241       case SIMIX_CANCELED:
242         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
243         break;
244       default:
245         xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d", static_cast<int>(synchro->state));
246     }
247
248     if (simcall->issuer->host->isOff()) {
249       simcall->issuer->context->iwannadie = 1;
250     }
251
252     simcall->issuer->waiting_synchro = nullptr;
253     SIMIX_simcall_answer(simcall);
254   }
255
256   /* We no longer need it */
257   SIMIX_io_destroy(synchro);
258 }