1 /* Copyright (c) 2007-2010, 2012-2015. 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, sg_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, sg_host_t host)
62 smx_synchro_t synchro;
64 /* check if the host is active */
65 if (surf_host_get_state(surf_host_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;
73 synchro->category = NULL;
75 synchro->io.host = host;
76 synchro->io.surf_io = surf_host_read(host, fd->surf_file, size);
78 surf_action_set_data(synchro->io.surf_io, synchro);
79 XBT_DEBUG("Create io synchro %p", synchro);
85 void simcall_HANDLER_file_write(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
87 smx_synchro_t synchro = SIMIX_file_write(fd, size, host);
88 xbt_fifo_push(synchro->simcalls, simcall);
89 simcall->issuer->waiting_synchro = synchro;
92 smx_synchro_t SIMIX_file_write(smx_file_t fd, sg_size_t size, sg_host_t host)
94 smx_synchro_t synchro;
96 /* check if the host is active */
97 if (surf_host_get_state(surf_host_resource_priv(host)) != SURF_RESOURCE_ON) {
98 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
102 synchro = xbt_mallocator_get(simix_global->synchro_mallocator);
103 synchro->type = SIMIX_SYNC_IO;
104 synchro->name = NULL;
105 synchro->category = NULL;
107 synchro->io.host = host;
108 synchro->io.surf_io = surf_host_write(host, fd->surf_file, size);
110 surf_action_set_data(synchro->io.surf_io, synchro);
111 XBT_DEBUG("Create io synchro %p", synchro);
117 void simcall_HANDLER_file_open(smx_simcall_t simcall, const char* fullpath, sg_host_t host)
119 smx_synchro_t synchro = SIMIX_file_open(fullpath, host);
120 xbt_fifo_push(synchro->simcalls, simcall);
121 simcall->issuer->waiting_synchro = synchro;
124 smx_synchro_t SIMIX_file_open(const char* fullpath, sg_host_t host)
126 smx_synchro_t synchro;
128 /* check if the host is active */
129 if (surf_host_get_state(surf_host_resource_priv(host)) != SURF_RESOURCE_ON) {
130 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
134 synchro = xbt_mallocator_get(simix_global->synchro_mallocator);
135 synchro->type = SIMIX_SYNC_IO;
136 synchro->name = NULL;
137 synchro->category = NULL;
139 synchro->io.host = host;
140 synchro->io.surf_io = surf_host_open(host, fullpath);
142 surf_action_set_data(synchro->io.surf_io, synchro);
143 XBT_DEBUG("Create io synchro %p", synchro);
149 void simcall_HANDLER_file_close(smx_simcall_t simcall, smx_file_t fd, sg_host_t host)
151 smx_synchro_t synchro = SIMIX_file_close(fd, host);
152 xbt_fifo_push(synchro->simcalls, simcall);
153 simcall->issuer->waiting_synchro = synchro;
156 smx_synchro_t SIMIX_file_close(smx_file_t fd, sg_host_t host)
158 smx_synchro_t synchro;
160 /* check if the host is active */
161 if (surf_host_get_state(surf_host_resource_priv(host)) != SURF_RESOURCE_ON) {
162 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
166 synchro = xbt_mallocator_get(simix_global->synchro_mallocator);
167 synchro->type = SIMIX_SYNC_IO;
168 synchro->name = NULL;
169 synchro->category = NULL;
171 synchro->io.host = host;
172 synchro->io.surf_io = surf_host_close(host, fd->surf_file);
174 surf_action_set_data(synchro->io.surf_io, synchro);
175 XBT_DEBUG("Create io synchro %p", synchro);
182 int SIMIX_file_unlink(smx_file_t fd, sg_host_t host)
184 /* check if the host is active */
185 if (surf_host_get_state(surf_host_resource_priv(host)) != SURF_RESOURCE_ON) {
186 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
190 int res = surf_host_unlink(host, fd->surf_file);
195 sg_size_t simcall_HANDLER_file_get_size(smx_simcall_t simcall, smx_file_t fd)
197 return SIMIX_file_get_size(simcall->issuer, fd);
200 sg_size_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd)
202 sg_host_t host = process->host;
203 return surf_host_get_size(host, fd->surf_file);
206 sg_size_t simcall_HANDLER_file_tell(smx_simcall_t simcall, smx_file_t fd)
208 return SIMIX_file_tell(simcall->issuer, fd);
211 sg_size_t SIMIX_file_tell(smx_process_t process, smx_file_t fd)
213 sg_host_t host = process->host;
214 return surf_host_file_tell(host, fd->surf_file);
218 xbt_dynar_t simcall_HANDLER_file_get_info(smx_simcall_t simcall, smx_file_t fd)
220 return SIMIX_file_get_info(simcall->issuer, fd);
223 xbt_dynar_t SIMIX_file_get_info(smx_process_t process, smx_file_t fd)
225 sg_host_t host = process->host;
226 return surf_host_get_info(host, fd->surf_file);
229 int simcall_HANDLER_file_seek(smx_simcall_t simcall, smx_file_t fd, sg_offset_t offset, int origin)
231 return SIMIX_file_seek(simcall->issuer, fd, offset, origin);
234 int SIMIX_file_seek(smx_process_t process, smx_file_t fd, sg_offset_t offset, int origin)
236 sg_host_t host = process->host;
237 return surf_host_file_seek(host, fd->surf_file, offset, origin);
240 int simcall_HANDLER_file_move(smx_simcall_t simcall, smx_file_t file, const char* fullpath)
242 return SIMIX_file_move(simcall->issuer, file, fullpath);
245 int SIMIX_file_move(smx_process_t process, smx_file_t file, const char* fullpath)
247 sg_host_t host = process->host;
248 return surf_host_file_move(host, file->surf_file, fullpath);
251 sg_size_t SIMIX_storage_get_size(smx_storage_t storage){
252 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
253 return surf_storage_get_size(storage);
256 sg_size_t simcall_HANDLER_storage_get_free_size(smx_simcall_t simcall, smx_storage_t storage)
258 return SIMIX_storage_get_free_size(simcall->issuer, storage);
261 sg_size_t SIMIX_storage_get_free_size(smx_process_t process, smx_storage_t storage)
263 return surf_storage_get_free_size(storage);
266 sg_size_t simcall_HANDLER_storage_get_used_size(smx_simcall_t simcall, smx_storage_t storage)
268 return SIMIX_storage_get_used_size(simcall->issuer, storage);
271 sg_size_t SIMIX_storage_get_used_size(smx_process_t process, smx_storage_t storage)
273 return surf_storage_get_used_size(storage);
276 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
277 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
278 return surf_resource_get_properties(surf_storage_resource_priv(storage));
281 const char* SIMIX_storage_get_name(smx_storage_t storage){
282 xbt_assert((storage != NULL), "Invalid parameters");
283 return sg_storage_name(storage);
286 xbt_dict_t SIMIX_storage_get_content(smx_storage_t storage){
287 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
288 return surf_storage_get_content(storage);
291 const char* SIMIX_storage_get_host(smx_storage_t storage){
292 xbt_assert((storage != NULL), "Invalid parameters");
293 return surf_storage_get_host(storage);
296 void SIMIX_post_io(smx_synchro_t synchro)
299 smx_simcall_t simcall;
301 xbt_fifo_foreach(synchro->simcalls,i,simcall,smx_simcall_t) {
302 switch (simcall->call) {
303 case SIMCALL_FILE_OPEN: {
304 smx_file_t tmp = xbt_new(s_smx_file_t,1);
305 tmp->surf_file = surf_storage_action_get_file(synchro->io.surf_io);
306 simcall_file_open__set__result(simcall, tmp);
309 case SIMCALL_FILE_CLOSE:
310 xbt_free(simcall_file_close__get__fd(simcall));
311 simcall_file_close__set__result(simcall, 0);
313 case SIMCALL_FILE_WRITE:
314 simcall_file_write__set__result(simcall, surf_action_get_cost(synchro->io.surf_io));
317 case SIMCALL_FILE_READ:
318 simcall_file_read__set__result(simcall, surf_action_get_cost(synchro->io.surf_io));
326 switch (surf_action_get_state(synchro->io.surf_io)) {
328 case SURF_ACTION_FAILED:
329 synchro->state = SIMIX_FAILED;
332 case SURF_ACTION_DONE:
333 synchro->state = SIMIX_DONE;
341 SIMIX_io_finish(synchro);
344 void SIMIX_io_destroy(smx_synchro_t synchro)
346 XBT_DEBUG("Destroy synchro %p", synchro);
347 if (synchro->io.surf_io)
348 surf_action_unref(synchro->io.surf_io);
349 xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
352 void SIMIX_io_finish(smx_synchro_t synchro)
354 xbt_fifo_item_t item;
355 smx_simcall_t simcall;
357 xbt_fifo_foreach(synchro->simcalls, item, simcall, smx_simcall_t) {
359 switch (synchro->state) {
362 /* do nothing, synchro done */
366 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
370 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
374 xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d",
375 (int)synchro->state);
378 if (surf_host_get_state(surf_host_resource_priv(simcall->issuer->host)) != SURF_RESOURCE_ON) {
379 simcall->issuer->context->iwannadie = 1;
382 simcall->issuer->waiting_synchro = NULL;
383 SIMIX_simcall_answer(simcall);
386 /* We no longer need it */
387 SIMIX_io_destroy(synchro);