Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bc6935bd76b0fd50b5bebfc2ba678736a41797b7
[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 "smx_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
17 //SIMIX FILE READ
18 void SIMIX_pre_file_read(smx_simcall_t simcall)
19 {
20   smx_action_t action = SIMIX_file_read(simcall->issuer,
21       simcall->file_read.storage,
22       simcall->file_read.ptr,
23       simcall->file_read.size,
24       simcall->file_read.nmemb,
25       simcall->file_read.stream);
26   xbt_fifo_push(action->simcalls, simcall);
27   simcall->issuer->waiting_action = action;
28 }
29
30 smx_action_t SIMIX_file_read(smx_process_t process, const char* storage, void* ptr, size_t size, size_t nmemb, m_file_t stream)
31 {
32   smx_action_t action;
33   smx_host_t host = process->smx_host;
34
35   /* check if the host is active */
36   if (surf_workstation_model->extension.
37       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
38     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
39            host->name);
40   }
41
42   action = xbt_mallocator_get(simix_global->action_mallocator);
43   action->type = SIMIX_ACTION_IO;
44   action->name = NULL;
45 #ifdef HAVE_TRACING
46   action->category = NULL;
47 #endif
48
49   action->io.host = host;
50   action->io.surf_io = surf_workstation_model->extension.workstation.read(host->host, storage, ptr, size, nmemb, stream),
51
52   surf_workstation_model->action_data_set(action->io.surf_io, action);
53   XBT_DEBUG("Create io action %p", action);
54
55   return action;
56 }
57
58 //SIMIX FILE WRITE
59 void SIMIX_pre_file_write(smx_simcall_t simcall)
60 {
61   smx_action_t action = SIMIX_file_write(simcall->issuer,
62       simcall->file_write.storage,
63       simcall->file_write.ptr,
64       simcall->file_write.size,
65       simcall->file_write.nmemb,
66       simcall->file_write.stream);
67   xbt_fifo_push(action->simcalls, simcall);
68   simcall->issuer->waiting_action = action;
69 }
70
71 smx_action_t SIMIX_file_write(smx_process_t process, const char* storage, const void* ptr, size_t size, size_t nmemb, m_file_t stream)
72 {
73   smx_action_t action;
74   smx_host_t host = process->smx_host;
75
76   /* check if the host is active */
77   if (surf_workstation_model->extension.
78       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
79     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
80            host->name);
81   }
82
83   action = xbt_mallocator_get(simix_global->action_mallocator);
84   action->type = SIMIX_ACTION_IO;
85   action->name = NULL;
86 #ifdef HAVE_TRACING
87   action->category = NULL;
88 #endif
89
90   action->io.host = host;
91   action->io.surf_io = surf_workstation_model->extension.workstation.write(host->host, storage, ptr, size, nmemb, stream);
92
93   surf_workstation_model->action_data_set(action->io.surf_io, action);
94   XBT_DEBUG("Create io action %p", action);
95
96   return action;
97 }
98
99 //SIMIX FILE OPEN
100 void SIMIX_pre_file_open(smx_simcall_t simcall)
101 {
102   smx_action_t action = SIMIX_file_open(simcall->issuer,
103       simcall->file_open.storage,
104       simcall->file_open.path,
105       simcall->file_open.mode);
106   xbt_fifo_push(action->simcalls, simcall);
107   simcall->issuer->waiting_action = action;
108 }
109
110 smx_action_t SIMIX_file_open(smx_process_t process ,const char* storage, const char* path, const char* mode)
111 {
112   smx_action_t action;
113   smx_host_t host = process->smx_host;
114
115   /* check if the host is active */
116   if (surf_workstation_model->extension.
117       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
118     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
119            host->name);
120   }
121
122   action = xbt_mallocator_get(simix_global->action_mallocator);
123   action->type = SIMIX_ACTION_IO;
124   action->name = NULL;
125 #ifdef HAVE_TRACING
126   action->category = NULL;
127 #endif
128
129   action->io.host = host;
130   action->io.surf_io = surf_workstation_model->extension.workstation.open(host->host, storage, path, mode);
131
132   surf_workstation_model->action_data_set(action->io.surf_io, action);
133   XBT_DEBUG("Create io action %p", action);
134
135   return action;
136 }
137
138 //SIMIX FILE CLOSE
139 void SIMIX_pre_file_close(smx_simcall_t simcall)
140 {
141   smx_action_t action = SIMIX_file_close(simcall->issuer,
142       simcall->file_close.storage,
143       simcall->file_close.fp);
144   xbt_fifo_push(action->simcalls, simcall);
145   simcall->issuer->waiting_action = action;
146 }
147
148 smx_action_t SIMIX_file_close(smx_process_t process ,const char* storage, m_file_t fp)
149 {
150   smx_action_t action;
151   smx_host_t host = process->smx_host;
152
153   /* check if the host is active */
154   if (surf_workstation_model->extension.
155       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
156     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
157            host->name);
158   }
159
160   action = xbt_mallocator_get(simix_global->action_mallocator);
161   action->type = SIMIX_ACTION_IO;
162   action->name = NULL;
163 #ifdef HAVE_TRACING
164   action->category = NULL;
165 #endif
166
167   action->io.host = host;
168   action->io.surf_io = surf_workstation_model->extension.workstation.close(host->host, storage, fp);
169
170   surf_workstation_model->action_data_set(action->io.surf_io, action);
171   XBT_DEBUG("Create io action %p", action);
172
173   return action;
174 }
175
176 //SIMIX FILE STAT
177 void SIMIX_pre_file_stat(smx_simcall_t simcall)
178 {
179   smx_action_t action = SIMIX_file_stat(simcall->issuer,
180       simcall->file_stat.storage,
181       simcall->file_stat.fd,
182       simcall->file_stat.buf);
183   xbt_fifo_push(action->simcalls, simcall);
184   simcall->issuer->waiting_action = action;
185 }
186
187 smx_action_t SIMIX_file_stat(smx_process_t process ,const char* storage, int fd, void* buf)
188 {
189   smx_action_t action;
190   smx_host_t host = process->smx_host;
191
192   /* check if the host is active */
193   if (surf_workstation_model->extension.
194       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
195     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
196            host->name);
197   }
198
199   action = xbt_mallocator_get(simix_global->action_mallocator);
200   action->type = SIMIX_ACTION_IO;
201   action->name = NULL;
202 #ifdef HAVE_TRACING
203   action->category = NULL;
204 #endif
205
206   action->io.host = host;
207   action->io.surf_io = surf_workstation_model->extension.workstation.stat(host->host, storage, fd, buf);
208
209   surf_workstation_model->action_data_set(action->io.surf_io, action);
210   XBT_DEBUG("Create io action %p", action);
211
212   return action;
213 }
214
215 void SIMIX_post_io(smx_action_t action)
216 {
217   switch (surf_workstation_model->action_state_get(action->io.surf_io)) {
218
219     case SURF_ACTION_FAILED:
220       action->state = SIMIX_FAILED;
221       break;
222
223     case SURF_ACTION_DONE:
224       action->state = SIMIX_DONE;
225       break;
226
227     default:
228       THROW_IMPOSSIBLE;
229       break;
230   }
231
232   SIMIX_io_finish(action);
233 }
234
235 void SIMIX_io_destroy(smx_action_t action)
236 {
237   XBT_DEBUG("Destroy action %p", action);
238   if (action->io.surf_io)
239     action->io.surf_io->model_type->action_unref(action->io.surf_io);
240   xbt_mallocator_release(simix_global->action_mallocator, action);
241 }
242
243 void SIMIX_io_finish(smx_action_t action)
244 {
245   xbt_fifo_item_t item;
246   smx_simcall_t simcall;
247
248   xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
249
250     switch (action->state) {
251
252       case SIMIX_DONE:
253         /* do nothing, action done */
254         break;
255
256       case SIMIX_FAILED:
257         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
258         break;
259
260       case SIMIX_CANCELED:
261         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
262         break;
263
264       default:
265         xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
266             (int)action->state);
267     }
268     simcall->issuer->waiting_action = NULL;
269     SIMIX_simcall_answer(simcall);
270   }
271
272   /* We no longer need it */
273   SIMIX_io_destroy(action);
274 }