1 /* Copyright (c) 2007, 2008, 2009, 2010. 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 "xbt/sysdep.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix,
14 "Logging specific to SIMIX (io)");
18 void SIMIX_pre_file_read(smx_simcall_t simcall, void *ptr, size_t size,
19 size_t nmemb, smx_file_t stream)
21 smx_action_t action = SIMIX_file_read(simcall->issuer, ptr, size, nmemb, stream);
22 xbt_fifo_push(action->simcalls, simcall);
23 simcall->issuer->waiting_action = action;
26 smx_action_t SIMIX_file_read(smx_process_t process, void* ptr, size_t size, size_t nmemb, smx_file_t stream)
29 smx_host_t host = process->smx_host;
31 /* check if the host is active */
32 if (surf_workstation_model->extension.
33 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
34 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
38 action = xbt_mallocator_get(simix_global->action_mallocator);
39 action->type = SIMIX_ACTION_IO;
42 action->category = NULL;
45 action->io.host = host;
46 action->io.surf_io = surf_workstation_model->extension.workstation.read(host->host, ptr, size, nmemb, stream->surf_file);
48 surf_workstation_model->action_data_set(action->io.surf_io, action);
49 XBT_DEBUG("Create io action %p", action);
55 void SIMIX_pre_file_write(smx_simcall_t simcall, const void *ptr, size_t size,
56 size_t nmemb, smx_file_t stream)
58 smx_action_t action = SIMIX_file_write(simcall->issuer, ptr, size, nmemb, stream);
59 xbt_fifo_push(action->simcalls, simcall);
60 simcall->issuer->waiting_action = action;
63 smx_action_t SIMIX_file_write(smx_process_t process, const void* ptr, size_t size, size_t nmemb, smx_file_t stream)
66 smx_host_t host = process->smx_host;
68 /* check if the host is active */
69 if (surf_workstation_model->extension.
70 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
71 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
75 action = xbt_mallocator_get(simix_global->action_mallocator);
76 action->type = SIMIX_ACTION_IO;
79 action->category = NULL;
82 action->io.host = host;
83 action->io.surf_io = surf_workstation_model->extension.workstation.write(host->host, ptr, size, nmemb, stream->surf_file);
85 surf_workstation_model->action_data_set(action->io.surf_io, action);
86 XBT_DEBUG("Create io action %p", action);
92 void SIMIX_pre_file_open(smx_simcall_t simcall, const char* mount,
93 const char* path, const char* mode)
95 smx_action_t action = SIMIX_file_open(simcall->issuer, mount, path, mode);
96 xbt_fifo_push(action->simcalls, simcall);
97 simcall->issuer->waiting_action = action;
100 smx_action_t SIMIX_file_open(smx_process_t process ,const char* mount, const char* path, const char* mode)
103 smx_host_t host = process->smx_host;
105 /* check if the host is active */
106 if (surf_workstation_model->extension.
107 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
108 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
112 action = xbt_mallocator_get(simix_global->action_mallocator);
113 action->type = SIMIX_ACTION_IO;
116 action->category = NULL;
119 action->io.host = host;
120 action->io.surf_io = surf_workstation_model->extension.workstation.open(host->host, mount, path, mode);
122 surf_workstation_model->action_data_set(action->io.surf_io, action);
123 XBT_DEBUG("Create io action %p", action);
129 void SIMIX_pre_file_close(smx_simcall_t simcall, smx_file_t fp)
131 smx_action_t action = SIMIX_file_close(simcall->issuer, fp);
132 xbt_fifo_push(action->simcalls, simcall);
133 simcall->issuer->waiting_action = action;
136 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fp)
139 smx_host_t host = process->smx_host;
141 /* check if the host is active */
142 if (surf_workstation_model->extension.
143 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
144 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
148 action = xbt_mallocator_get(simix_global->action_mallocator);
149 action->type = SIMIX_ACTION_IO;
152 action->category = NULL;
155 action->io.host = host;
156 action->io.surf_io = surf_workstation_model->extension.workstation.close(host->host, fp->surf_file);
158 surf_workstation_model->action_data_set(action->io.surf_io, action);
159 XBT_DEBUG("Create io action %p", action);
165 void SIMIX_pre_file_stat(smx_simcall_t simcall, smx_file_t fd, s_file_stat_t *buf)
167 smx_action_t action = SIMIX_file_stat(simcall->issuer, fd, *buf);
168 xbt_fifo_push(action->simcalls, simcall);
169 simcall->issuer->waiting_action = action;
172 smx_action_t SIMIX_file_stat(smx_process_t process, smx_file_t fd, s_file_stat_t buf)
175 smx_host_t host = process->smx_host;
176 /* check if the host is active */
177 if (surf_workstation_model->extension.
178 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
179 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
183 action = xbt_mallocator_get(simix_global->action_mallocator);
184 action->type = SIMIX_ACTION_IO;
187 action->category = NULL;
190 action->io.host = host;
191 action->io.surf_io = surf_workstation_model->extension.workstation.stat(host->host, fd->surf_file);
193 surf_workstation_model->action_data_set(action->io.surf_io, action);
194 XBT_DEBUG("Create io action %p", action);
200 void SIMIX_pre_file_unlink(smx_simcall_t simcall, smx_file_t fd)
202 smx_action_t action = SIMIX_file_unlink(simcall->issuer, fd);
203 xbt_fifo_push(action->simcalls, simcall);
204 simcall->issuer->waiting_action = action;
207 smx_action_t SIMIX_file_unlink(smx_process_t process, smx_file_t fd)
210 smx_host_t host = process->smx_host;
211 /* check if the host is active */
212 if (surf_workstation_model->extension.
213 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
214 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
218 action = xbt_mallocator_get(simix_global->action_mallocator);
219 action->type = SIMIX_ACTION_IO;
222 action->category = NULL;
225 action->io.host = host;
226 action->io.surf_io = surf_workstation_model->extension.workstation.unlink(host->host, fd->surf_file);
228 surf_workstation_model->action_data_set(action->io.surf_io, action);
229 XBT_DEBUG("Create io action %p", action);
235 void SIMIX_pre_file_ls(smx_simcall_t simcall,
236 const char* mount, const char* path)
238 smx_action_t action = SIMIX_file_ls(simcall->issuer, mount, path);
239 xbt_fifo_push(action->simcalls, simcall);
240 simcall->issuer->waiting_action = action;
242 smx_action_t SIMIX_file_ls(smx_process_t process, const char* mount, const char *path)
245 smx_host_t host = process->smx_host;
246 /* check if the host is active */
247 if (surf_workstation_model->extension.workstation.get_state(host->host) != SURF_RESOURCE_ON) {
248 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
252 action = xbt_mallocator_get(simix_global->action_mallocator);
253 action->type = SIMIX_ACTION_IO;
256 action->category = NULL;
259 action->io.host = host;
260 action->io.surf_io = surf_workstation_model->extension.workstation.ls(host->host,mount,path);
262 surf_workstation_model->action_data_set(action->io.surf_io, action);
263 XBT_DEBUG("Create io action %p", action);
267 static void free_file_stat(void *p)
274 xbt_free(fs->user_rights);
278 void SIMIX_post_io(smx_action_t action)
281 smx_simcall_t simcall;
283 xbt_dict_cursor_t cursor = NULL;
284 s_file_stat_t *dst = NULL;
285 s_file_stat_t *src = NULL;
287 xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
288 switch (simcall->call) {
289 case SIMCALL_FILE_OPEN:;
290 smx_file_t tmp = xbt_new(s_smx_file_t,1);
291 tmp->surf_file = (action->io.surf_io)->file;
292 simcall_file_open__set__result(simcall, tmp);
295 case SIMCALL_FILE_CLOSE:
296 xbt_free(simcall_file_close__get__fp(simcall));
297 simcall_file_close__set__result(simcall, 0);
300 case SIMCALL_FILE_WRITE:
301 simcall_file_write__set__result(simcall, (action->io.surf_io)->cost);
304 case SIMCALL_FILE_READ:
305 simcall_file_read__set__result(simcall, (action->io.surf_io)->cost);
308 case SIMCALL_FILE_STAT:
309 simcall_file_stat__set__result(simcall, 0);
310 dst = simcall_file_stat__get__buf(simcall);
311 src = &((action->io.surf_io)->stat);
312 file_stat_copy(src,dst);
315 case SIMCALL_FILE_UNLINK:
316 xbt_free(simcall_file_unlink__get__fd(simcall));
317 simcall_file_unlink__set__result(simcall, 0);
320 case SIMCALL_FILE_LS:
321 xbt_dict_foreach((action->io.surf_io)->ls_dict,cursor,key, src){
322 // if there is a stat we have to duplicate it
324 dst = xbt_new0(s_file_stat_t,1);
325 file_stat_copy(src, dst);
326 xbt_dict_set((action->io.surf_io)->ls_dict,key,dst,free_file_stat);
329 simcall_file_ls__set__result(simcall, (action->io.surf_io)->ls_dict);
337 switch (surf_workstation_model->action_state_get(action->io.surf_io)) {
339 case SURF_ACTION_FAILED:
340 action->state = SIMIX_FAILED;
343 case SURF_ACTION_DONE:
344 action->state = SIMIX_DONE;
352 SIMIX_io_finish(action);
355 void SIMIX_io_destroy(smx_action_t action)
357 XBT_DEBUG("Destroy action %p", action);
358 if (action->io.surf_io)
359 action->io.surf_io->model_type->action_unref(action->io.surf_io);
360 xbt_mallocator_release(simix_global->action_mallocator, action);
363 void SIMIX_io_finish(smx_action_t action)
365 xbt_fifo_item_t item;
366 smx_simcall_t simcall;
368 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
370 switch (action->state) {
373 /* do nothing, action done */
377 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
381 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
385 xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
389 if (surf_workstation_model->extension.
390 workstation.get_state(simcall->issuer->smx_host->host) != SURF_RESOURCE_ON) {
391 simcall->issuer->context->iwannadie = 1;
394 simcall->issuer->waiting_action = NULL;
395 SIMIX_simcall_answer(simcall);
398 /* We no longer need it */
399 SIMIX_io_destroy(action);