Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1d16e715f0569495d2b8b0beeb88829fc9e5986f
[simgrid.git] / src / simix / smx_io.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. 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 "private.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/dict.h"
11 #include "mc/mc.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix,
14                                 "Logging specific to SIMIX (io)");
15
16 void SIMIX_pre_file_read(smx_req_t req)
17 {
18   smx_action_t action = SIMIX_file_read(req->issuer, req->file_read.name);
19   xbt_fifo_push(action->request_list, req);
20   req->issuer->waiting_action = action;
21 }
22
23 smx_action_t SIMIX_file_read(smx_process_t process, char* name)
24 {
25   smx_action_t action;
26   smx_host_t host = process->smx_host;
27
28   /* check if the host is active */
29   if (surf_workstation_model->extension.
30       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
31     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
32            host->name);
33   }
34
35   action = xbt_mallocator_get(simix_global->action_mallocator);
36   action->type = SIMIX_ACTION_IO;
37   action->name = NULL;
38 #ifdef HAVE_TRACING
39   action->category = NULL;
40 #endif
41
42   action->io.host = host;
43   //  TODO in surf model disk???
44   //  action->io.surf_io = surf_workstation_model->extension.disk.read(host->host, name),
45   action->io.surf_io = surf_workstation_model->extension.workstation.sleep(host->host, 1.0);
46
47   surf_workstation_model->action_data_set(action->io.surf_io, action);
48   XBT_DEBUG("Create io action %p", action);
49
50   return action;
51 }
52
53 void SIMIX_post_io(smx_action_t action)
54 {
55   switch (surf_workstation_model->action_state_get(action->io.surf_io)) {
56
57     case SURF_ACTION_FAILED:
58       action->state = SIMIX_FAILED;
59       break;
60
61     case SURF_ACTION_DONE:
62       action->state = SIMIX_DONE;
63       break;
64
65     default:
66       THROW_IMPOSSIBLE;
67       break;
68   }
69
70   SIMIX_io_finish(action);
71 }
72
73 void SIMIX_io_destroy(smx_action_t action)
74 {
75   XBT_DEBUG("Destroy action %p", action);
76   if (action->io.surf_io)
77     action->io.surf_io->model_type->action_unref(action->io.surf_io);
78   xbt_mallocator_release(simix_global->action_mallocator, action);
79 }
80
81 void SIMIX_io_finish(smx_action_t action)
82 {
83   volatile xbt_fifo_item_t item;
84   smx_req_t req;
85
86   xbt_fifo_foreach(action->request_list, item, req, smx_req_t) {
87
88     switch (action->state) {
89
90       case SIMIX_DONE:
91         /* do nothing, action done */
92         break;
93
94       case SIMIX_FAILED:
95         TRY {
96           THROWF(io_error, 0, "IO failed");
97         }
98         CATCH(req->issuer->running_ctx->exception) {
99           req->issuer->doexception = 1;
100         }
101       break;
102
103       case SIMIX_CANCELED:
104         TRY {
105           THROWF(cancel_error, 0, "Canceled");
106         }
107         CATCH(req->issuer->running_ctx->exception) {
108           req->issuer->doexception = 1;
109         }
110         break;
111
112       default:
113         xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
114             action->state);
115     }
116     req->issuer->waiting_action = NULL;
117     SIMIX_request_answer(req);
118   }
119
120   /* We no longer need it */
121   SIMIX_io_destroy(action);
122 }