Logo AND Algorithmique Numérique Distribuée

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