Logo AND Algorithmique Numérique Distribuée

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