1 /* Copyright (c) 2007-2010, 2012-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "smx_private.h"
8 //#include "surf/storage_private.h"
9 #include "xbt/sysdep.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix,
15 "Logging specific to SIMIX (io)");
19 * \brief Internal function to create a SIMIX storage.
20 * \param name name of the storage to create
21 * \param storage the SURF storage to encapsulate
22 * \param data some user data (may be NULL)
24 smx_storage_t SIMIX_storage_create(const char *name, void *storage, void *data)
26 smx_storage_priv_t smx_storage = xbt_new0(s_smx_storage_priv_t, 1);
28 smx_storage->data = data;
30 /* Update global variables */
31 xbt_lib_set(storage_lib,name,SIMIX_STORAGE_LEVEL,smx_storage);
32 return xbt_lib_get_elm_or_null(storage_lib, name);
36 * \brief Internal function to destroy a SIMIX storage.
38 * \param s the host to destroy (a smx_storage_t)
40 void SIMIX_storage_destroy(void *s)
42 smx_storage_priv_t storage = (smx_storage_priv_t) s;
44 xbt_assert((storage != NULL), "Invalid parameters");
48 /* Clean storage structure */
53 void simcall_HANDLER_file_read(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, smx_host_t host)
55 smx_synchro_t synchro = SIMIX_file_read(fd, size, host);
56 xbt_fifo_push(synchro->simcalls, simcall);
57 simcall->issuer->waiting_synchro = synchro;
60 smx_synchro_t SIMIX_file_read(smx_file_t fd, sg_size_t size, smx_host_t host)
62 smx_synchro_t synchro;
64 /* check if the host is active */
65 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
66 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
70 synchro = xbt_mallocator_get(simix_global->synchro_mallocator);
71 synchro->type = SIMIX_SYNC_IO;
74 synchro->category = NULL;
77 synchro->io.host = host;
78 synchro->io.surf_io = surf_workstation_read(host, fd->surf_file, size);
80 surf_action_set_data(synchro->io.surf_io, synchro);
81 XBT_DEBUG("Create io synchro %p", synchro);
87 void simcall_HANDLER_file_write(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, smx_host_t host)
89 smx_synchro_t synchro = SIMIX_file_write(fd, size, host);
90 xbt_fifo_push(synchro->simcalls, simcall);
91 simcall->issuer->waiting_synchro = synchro;
94 smx_synchro_t SIMIX_file_write(smx_file_t fd, sg_size_t size, smx_host_t host)
96 smx_synchro_t synchro;
98 /* check if the host is active */
99 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
100 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
104 synchro = xbt_mallocator_get(simix_global->synchro_mallocator);
105 synchro->type = SIMIX_SYNC_IO;
106 synchro->name = NULL;
108 synchro->category = NULL;
111 synchro->io.host = host;
112 synchro->io.surf_io = surf_workstation_write(host, fd->surf_file, size);
114 surf_action_set_data(synchro->io.surf_io, synchro);
115 XBT_DEBUG("Create io synchro %p", synchro);
121 void simcall_HANDLER_file_open(smx_simcall_t simcall, const char* fullpath, smx_host_t host)
123 smx_synchro_t synchro = SIMIX_file_open(fullpath, host);
124 xbt_fifo_push(synchro->simcalls, simcall);
125 simcall->issuer->waiting_synchro = synchro;
128 smx_synchro_t SIMIX_file_open(const char* fullpath, smx_host_t host)
130 smx_synchro_t synchro;
132 /* check if the host is active */
133 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
134 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
138 synchro = xbt_mallocator_get(simix_global->synchro_mallocator);
139 synchro->type = SIMIX_SYNC_IO;
140 synchro->name = NULL;
142 synchro->category = NULL;
145 synchro->io.host = host;
146 synchro->io.surf_io = surf_workstation_open(host, fullpath);
148 surf_action_set_data(synchro->io.surf_io, synchro);
149 XBT_DEBUG("Create io synchro %p", synchro);
155 void simcall_HANDLER_file_close(smx_simcall_t simcall, smx_file_t fd, smx_host_t host)
157 smx_synchro_t synchro = SIMIX_file_close(fd, host);
158 xbt_fifo_push(synchro->simcalls, simcall);
159 simcall->issuer->waiting_synchro = synchro;
162 smx_synchro_t SIMIX_file_close(smx_file_t fd, smx_host_t host)
164 smx_synchro_t synchro;
166 /* check if the host is active */
167 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
168 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
172 synchro = xbt_mallocator_get(simix_global->synchro_mallocator);
173 synchro->type = SIMIX_SYNC_IO;
174 synchro->name = NULL;
176 synchro->category = NULL;
179 synchro->io.host = host;
180 synchro->io.surf_io = surf_workstation_close(host, fd->surf_file);
182 surf_action_set_data(synchro->io.surf_io, synchro);
183 XBT_DEBUG("Create io synchro %p", synchro);
190 int SIMIX_file_unlink(smx_file_t fd, smx_host_t host)
192 /* check if the host is active */
193 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
194 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
198 int res = surf_workstation_unlink(host, fd->surf_file);
203 sg_size_t simcall_HANDLER_file_get_size(smx_simcall_t simcall, smx_file_t fd)
205 return SIMIX_file_get_size(simcall->issuer, fd);
208 sg_size_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd)
210 smx_host_t host = process->smx_host;
211 return surf_workstation_get_size(host, fd->surf_file);
214 sg_size_t simcall_HANDLER_file_tell(smx_simcall_t simcall, smx_file_t fd)
216 return SIMIX_file_tell(simcall->issuer, fd);
219 sg_size_t SIMIX_file_tell(smx_process_t process, smx_file_t fd)
221 smx_host_t host = process->smx_host;
222 return surf_workstation_file_tell(host, fd->surf_file);
226 xbt_dynar_t simcall_HANDLER_file_get_info(smx_simcall_t simcall, smx_file_t fd)
228 return SIMIX_file_get_info(simcall->issuer, fd);
231 xbt_dynar_t SIMIX_file_get_info(smx_process_t process, smx_file_t fd)
233 smx_host_t host = process->smx_host;
234 return surf_workstation_get_info(host, fd->surf_file);
237 int simcall_HANDLER_file_seek(smx_simcall_t simcall, smx_file_t fd, sg_offset_t offset, int origin)
239 return SIMIX_file_seek(simcall->issuer, fd, offset, origin);
242 int SIMIX_file_seek(smx_process_t process, smx_file_t fd, sg_offset_t offset, int origin)
244 smx_host_t host = process->smx_host;
245 return surf_workstation_file_seek(host, fd->surf_file, offset, origin);
248 int simcall_HANDLER_file_move(smx_simcall_t simcall, smx_file_t file, const char* fullpath)
250 return SIMIX_file_move(simcall->issuer, file, fullpath);
253 int SIMIX_file_move(smx_process_t process, smx_file_t file, const char* fullpath)
255 smx_host_t host = process->smx_host;
256 return surf_workstation_file_move(host, file->surf_file, fullpath);
259 sg_size_t SIMIX_storage_get_size(smx_storage_t storage){
260 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
261 return surf_storage_get_size(storage);
264 sg_size_t simcall_HANDLER_storage_get_free_size(smx_simcall_t simcall, smx_storage_t storage)
266 return SIMIX_storage_get_free_size(simcall->issuer, storage);
269 sg_size_t SIMIX_storage_get_free_size(smx_process_t process, smx_storage_t storage)
271 return surf_storage_get_free_size(storage);
274 sg_size_t simcall_HANDLER_storage_get_used_size(smx_simcall_t simcall, smx_storage_t storage)
276 return SIMIX_storage_get_used_size(simcall->issuer, storage);
279 sg_size_t SIMIX_storage_get_used_size(smx_process_t process, smx_storage_t storage)
281 return surf_storage_get_used_size(storage);
284 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
285 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
286 return surf_resource_get_properties(surf_storage_resource_priv(storage));
289 const char* SIMIX_storage_get_name(smx_storage_t storage){
290 xbt_assert((storage != NULL), "Invalid parameters");
291 return sg_storage_name(storage);
294 xbt_dict_t SIMIX_storage_get_content(smx_storage_t storage){
295 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
296 return surf_storage_get_content(storage);
299 const char* SIMIX_storage_get_host(smx_storage_t storage){
300 xbt_assert((storage != NULL), "Invalid parameters");
301 return surf_storage_get_host(storage);
304 void SIMIX_post_io(smx_synchro_t synchro)
307 smx_simcall_t simcall;
309 xbt_fifo_foreach(synchro->simcalls,i,simcall,smx_simcall_t) {
310 switch (simcall->call) {
311 case SIMCALL_FILE_OPEN: {
312 smx_file_t tmp = xbt_new(s_smx_file_t,1);
313 tmp->surf_file = surf_storage_action_get_file(synchro->io.surf_io);
314 simcall_file_open__set__result(simcall, tmp);
317 case SIMCALL_FILE_CLOSE:
318 xbt_free(simcall_file_close__get__fd(simcall));
319 simcall_file_close__set__result(simcall, 0);
321 case SIMCALL_FILE_WRITE:
322 simcall_file_write__set__result(simcall, surf_action_get_cost(synchro->io.surf_io));
325 case SIMCALL_FILE_READ:
326 simcall_file_read__set__result(simcall, surf_action_get_cost(synchro->io.surf_io));
334 switch (surf_action_get_state(synchro->io.surf_io)) {
336 case SURF_ACTION_FAILED:
337 synchro->state = SIMIX_FAILED;
340 case SURF_ACTION_DONE:
341 synchro->state = SIMIX_DONE;
349 SIMIX_io_finish(synchro);
352 void SIMIX_io_destroy(smx_synchro_t synchro)
354 XBT_DEBUG("Destroy synchro %p", synchro);
355 if (synchro->io.surf_io)
356 surf_action_unref(synchro->io.surf_io);
357 xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
360 void SIMIX_io_finish(smx_synchro_t synchro)
362 xbt_fifo_item_t item;
363 smx_simcall_t simcall;
365 xbt_fifo_foreach(synchro->simcalls, item, simcall, smx_simcall_t) {
367 switch (synchro->state) {
370 /* do nothing, synchro done */
374 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
378 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
382 xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d",
383 (int)synchro->state);
386 if (surf_resource_get_state(surf_workstation_resource_priv(simcall->issuer->smx_host)) != SURF_RESOURCE_ON) {
387 simcall->issuer->context->iwannadie = 1;
390 simcall->issuer->waiting_synchro = NULL;
391 SIMIX_simcall_answer(simcall);
394 /* We no longer need it */
395 SIMIX_io_destroy(synchro);