Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Prefer the C-ish sg_host_get_name() over sg_host_name()
[simgrid.git] / src / simix / smx_io.c
1 /* Copyright (c) 2007-2010, 2012-2015. 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 /**
18  * \brief Internal function to create a SIMIX storage.
19  * \param name name of the storage to create
20  * \param storage the SURF storage to encapsulate
21  * \param data some user data (may be NULL)
22  */
23 smx_storage_t SIMIX_storage_create(const char *name, void *storage, void *data)
24 {
25   smx_storage_priv_t smx_storage = xbt_new0(s_smx_storage_priv_t, 1);
26
27   smx_storage->data = data;
28
29   /* Update global variables */
30   xbt_lib_set(storage_lib,name,SIMIX_STORAGE_LEVEL,smx_storage);
31   return xbt_lib_get_elm_or_null(storage_lib, name);
32 }
33
34 /**
35  * \brief Internal function to destroy a SIMIX storage.
36  *
37  * \param s the host to destroy (a smx_storage_t)
38  */
39 void SIMIX_storage_destroy(void *s)
40 {
41   smx_storage_priv_t storage = (smx_storage_priv_t) s;
42
43   xbt_assert((storage != NULL), "Invalid parameters");
44   if (storage->data)
45     free(storage->data);
46
47   /* Clean storage structure */
48   free(storage);
49 }
50
51 //SIMIX FILE READ
52 void simcall_HANDLER_file_read(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
53 {
54   smx_synchro_t synchro = SIMIX_file_read(fd, size, host);
55   xbt_fifo_push(synchro->simcalls, simcall);
56   simcall->issuer->waiting_synchro = synchro;
57 }
58
59 smx_synchro_t SIMIX_file_read(smx_file_t fd, sg_size_t size, sg_host_t host)
60 {
61   smx_synchro_t synchro;
62
63   /* check if the host is active */
64   if (surf_host_get_state(surf_host_resource_priv(host)) != SURF_RESOURCE_ON) {
65     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
66            sg_host_get_name(host));
67   }
68
69   synchro = xbt_mallocator_get(simix_global->synchro_mallocator);
70   synchro->type = SIMIX_SYNC_IO;
71   synchro->name = NULL;
72   synchro->category = NULL;
73
74   synchro->io.host = host;
75   synchro->io.surf_io = surf_host_read(host, fd->surf_file, size);
76
77   surf_action_set_data(synchro->io.surf_io, synchro);
78   XBT_DEBUG("Create io synchro %p", synchro);
79
80   return synchro;
81 }
82
83 //SIMIX FILE WRITE
84 void simcall_HANDLER_file_write(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
85 {
86   smx_synchro_t synchro = SIMIX_file_write(fd,  size, host);
87   xbt_fifo_push(synchro->simcalls, simcall);
88   simcall->issuer->waiting_synchro = synchro;
89 }
90
91 smx_synchro_t SIMIX_file_write(smx_file_t fd, sg_size_t size, sg_host_t host)
92 {
93   smx_synchro_t synchro;
94
95   /* check if the host is active */
96   if (surf_host_get_state(surf_host_resource_priv(host)) != SURF_RESOURCE_ON) {
97     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
98            sg_host_get_name(host));
99   }
100
101   synchro = xbt_mallocator_get(simix_global->synchro_mallocator);
102   synchro->type = SIMIX_SYNC_IO;
103   synchro->name = NULL;
104   synchro->category = NULL;
105
106   synchro->io.host = host;
107   synchro->io.surf_io = surf_host_write(host, fd->surf_file, size);
108
109   surf_action_set_data(synchro->io.surf_io, synchro);
110   XBT_DEBUG("Create io synchro %p", synchro);
111
112   return synchro;
113 }
114
115 //SIMIX FILE OPEN
116 void simcall_HANDLER_file_open(smx_simcall_t simcall, const char* fullpath, sg_host_t host)
117 {
118   smx_synchro_t synchro = SIMIX_file_open(fullpath, host);
119   xbt_fifo_push(synchro->simcalls, simcall);
120   simcall->issuer->waiting_synchro = synchro;
121 }
122
123 smx_synchro_t SIMIX_file_open(const char* fullpath, sg_host_t host)
124 {
125   smx_synchro_t synchro;
126
127   /* check if the host is active */
128   if (surf_host_get_state(surf_host_resource_priv(host)) != SURF_RESOURCE_ON) {
129     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
130            sg_host_get_name(host));
131   }
132
133   synchro = xbt_mallocator_get(simix_global->synchro_mallocator);
134   synchro->type = SIMIX_SYNC_IO;
135   synchro->name = NULL;
136   synchro->category = NULL;
137
138   synchro->io.host = host;
139   synchro->io.surf_io = surf_host_open(host, fullpath);
140
141   surf_action_set_data(synchro->io.surf_io, synchro);
142   XBT_DEBUG("Create io synchro %p", synchro);
143
144   return synchro;
145 }
146
147 //SIMIX FILE CLOSE
148 void simcall_HANDLER_file_close(smx_simcall_t simcall, smx_file_t fd, sg_host_t host)
149 {
150   smx_synchro_t synchro = SIMIX_file_close(fd, host);
151   xbt_fifo_push(synchro->simcalls, simcall);
152   simcall->issuer->waiting_synchro = synchro;
153 }
154
155 smx_synchro_t SIMIX_file_close(smx_file_t fd, sg_host_t host)
156 {
157   smx_synchro_t synchro;
158
159   /* check if the host is active */
160   if (surf_host_get_state(surf_host_resource_priv(host)) != SURF_RESOURCE_ON) {
161     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
162            sg_host_get_name(host));
163   }
164
165   synchro = xbt_mallocator_get(simix_global->synchro_mallocator);
166   synchro->type = SIMIX_SYNC_IO;
167   synchro->name = NULL;
168   synchro->category = NULL;
169
170   synchro->io.host = host;
171   synchro->io.surf_io = surf_host_close(host, fd->surf_file);
172
173   surf_action_set_data(synchro->io.surf_io, synchro);
174   XBT_DEBUG("Create io synchro %p", synchro);
175
176   return synchro;
177 }
178
179
180 //SIMIX FILE UNLINK
181 int SIMIX_file_unlink(smx_file_t fd, sg_host_t host)
182 {
183   /* check if the host is active */
184   if (surf_host_get_state(surf_host_resource_priv(host)) != SURF_RESOURCE_ON) {
185     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
186            sg_host_get_name(host));
187   }
188
189   int res = surf_host_unlink(host, fd->surf_file);
190   xbt_free(fd);
191   return !!res;
192 }
193
194 sg_size_t simcall_HANDLER_file_get_size(smx_simcall_t simcall, smx_file_t fd)
195 {
196   return SIMIX_file_get_size(simcall->issuer, fd);
197 }
198
199 sg_size_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd)
200 {
201   sg_host_t host = process->host;
202   return  surf_host_get_size(host, fd->surf_file);
203 }
204
205 sg_size_t simcall_HANDLER_file_tell(smx_simcall_t simcall, smx_file_t fd)
206 {
207   return SIMIX_file_tell(simcall->issuer, fd);
208 }
209
210 sg_size_t SIMIX_file_tell(smx_process_t process, smx_file_t fd)
211 {
212   sg_host_t host = process->host;
213   return  surf_host_file_tell(host, fd->surf_file);
214 }
215
216
217 xbt_dynar_t simcall_HANDLER_file_get_info(smx_simcall_t simcall, smx_file_t fd)
218 {
219   return SIMIX_file_get_info(simcall->issuer, fd);
220 }
221
222 xbt_dynar_t SIMIX_file_get_info(smx_process_t process, smx_file_t fd)
223 {
224   sg_host_t host = process->host;
225   return  surf_host_get_info(host, fd->surf_file);
226 }
227
228 int simcall_HANDLER_file_seek(smx_simcall_t simcall, smx_file_t fd, sg_offset_t offset, int origin)
229 {
230   return SIMIX_file_seek(simcall->issuer, fd, offset, origin);
231 }
232
233 int SIMIX_file_seek(smx_process_t process, smx_file_t fd, sg_offset_t offset, int origin)
234 {
235   sg_host_t host = process->host;
236   return  surf_host_file_seek(host, fd->surf_file, offset, origin);
237 }
238
239 int simcall_HANDLER_file_move(smx_simcall_t simcall, smx_file_t file, const char* fullpath)
240 {
241   return SIMIX_file_move(simcall->issuer, file, fullpath);
242 }
243
244 int SIMIX_file_move(smx_process_t process, smx_file_t file, const char* fullpath)
245 {
246   sg_host_t host = process->host;
247   return  surf_host_file_move(host, file->surf_file, fullpath);
248 }
249
250 sg_size_t SIMIX_storage_get_size(smx_storage_t storage){
251   xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
252   return surf_storage_get_size(storage);
253 }
254
255 sg_size_t simcall_HANDLER_storage_get_free_size(smx_simcall_t simcall, smx_storage_t storage)
256 {
257   return SIMIX_storage_get_free_size(simcall->issuer, storage);
258 }
259
260 sg_size_t SIMIX_storage_get_free_size(smx_process_t process, smx_storage_t storage)
261 {
262   return  surf_storage_get_free_size(storage);
263 }
264
265 sg_size_t simcall_HANDLER_storage_get_used_size(smx_simcall_t simcall, smx_storage_t storage)
266 {
267   return SIMIX_storage_get_used_size(simcall->issuer, storage);
268 }
269
270 sg_size_t SIMIX_storage_get_used_size(smx_process_t process, smx_storage_t storage)
271 {
272   return  surf_storage_get_used_size(storage);
273 }
274
275 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
276   xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
277   return surf_resource_get_properties(surf_storage_resource_priv(storage));
278 }
279
280 const char* SIMIX_storage_get_name(smx_storage_t storage){
281   xbt_assert((storage != NULL), "Invalid parameters");
282   return sg_storage_name(storage);
283 }
284
285 xbt_dict_t SIMIX_storage_get_content(smx_storage_t storage){
286   xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
287   return surf_storage_get_content(storage);
288 }
289
290 const char* SIMIX_storage_get_host(smx_storage_t storage){
291   xbt_assert((storage != NULL), "Invalid parameters");
292   return surf_storage_get_host(storage);
293 }
294
295 void SIMIX_post_io(smx_synchro_t synchro)
296 {
297   xbt_fifo_item_t i;
298   smx_simcall_t simcall;
299
300   xbt_fifo_foreach(synchro->simcalls,i,simcall,smx_simcall_t) {
301     switch (simcall->call) {
302     case SIMCALL_FILE_OPEN: {
303       smx_file_t tmp = xbt_new(s_smx_file_t,1);
304       tmp->surf_file = surf_storage_action_get_file(synchro->io.surf_io);
305       simcall_file_open__set__result(simcall, tmp);
306       break;
307     }
308     case SIMCALL_FILE_CLOSE:
309       xbt_free(simcall_file_close__get__fd(simcall));
310       simcall_file_close__set__result(simcall, 0);
311       break;
312     case SIMCALL_FILE_WRITE:
313       simcall_file_write__set__result(simcall, surf_action_get_cost(synchro->io.surf_io));
314       break;
315
316     case SIMCALL_FILE_READ:
317       simcall_file_read__set__result(simcall, surf_action_get_cost(synchro->io.surf_io));
318       break;
319
320     default:
321       break;
322     }
323   }
324
325   switch (surf_action_get_state(synchro->io.surf_io)) {
326
327     case SURF_ACTION_FAILED:
328       synchro->state = SIMIX_FAILED;
329       break;
330
331     case SURF_ACTION_DONE:
332       synchro->state = SIMIX_DONE;
333       break;
334
335     default:
336       THROW_IMPOSSIBLE;
337       break;
338   }
339
340   SIMIX_io_finish(synchro);
341 }
342
343 void SIMIX_io_destroy(smx_synchro_t synchro)
344 {
345   XBT_DEBUG("Destroy synchro %p", synchro);
346   if (synchro->io.surf_io)
347     surf_action_unref(synchro->io.surf_io);
348   xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
349 }
350
351 void SIMIX_io_finish(smx_synchro_t synchro)
352 {
353   xbt_fifo_item_t item;
354   smx_simcall_t simcall;
355
356   xbt_fifo_foreach(synchro->simcalls, item, simcall, smx_simcall_t) {
357
358     switch (synchro->state) {
359
360       case SIMIX_DONE:
361         /* do nothing, synchro done */
362         break;
363
364       case SIMIX_FAILED:
365         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
366         break;
367
368       case SIMIX_CANCELED:
369         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
370         break;
371
372       default:
373         xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d",
374             (int)synchro->state);
375     }
376
377     if (surf_host_get_state(surf_host_resource_priv(simcall->issuer->host)) != SURF_RESOURCE_ON) {
378       simcall->issuer->context->iwannadie = 1;
379     }
380
381     simcall->issuer->waiting_synchro = NULL;
382     SIMIX_simcall_answer(simcall);
383   }
384
385   /* We no longer need it */
386   SIMIX_io_destroy(synchro);
387 }