Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Doc: Kill formating pragma that are unused since 10 years
[simgrid.git] / src / msg / msg_io.cpp
1 /* Copyright (c) 2004-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 "msg_private.h"
8 #include "xbt/log.h"
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_io, msg, "Logging specific to MSG (io)");
11
12 /** @addtogroup msg_file_management
13  * (#msg_file_t) and the functions for managing it.
14  *
15  *  \see #msg_file_t
16  */
17
18 /********************************* File **************************************/
19 void __MSG_file_get_info(msg_file_t fd){
20
21   msg_file_priv_t priv = MSG_file_priv(fd);
22   xbt_dynar_t info = simcall_file_get_info(priv->simdata->smx_file);
23   sg_size_t *psize;
24
25   priv->content_type = xbt_dynar_pop_as(info, char *);
26   priv->storage_type = xbt_dynar_pop_as(info, char *);
27   priv->storageId = xbt_dynar_pop_as(info, char *);
28   priv->mount_point = xbt_dynar_pop_as(info, char *);
29   psize = xbt_dynar_pop_as(info, sg_size_t*);
30   priv->size = *psize;
31   xbt_free(psize);
32   xbt_dynar_free_container(&info);
33 }
34
35 /** \ingroup msg_file_management
36  *
37  * \brief Set the user data of a #msg_file_t.
38  *
39  * This functions attach \a data to \a file.
40  */
41 msg_error_t MSG_file_set_data(msg_file_t fd, void *data)
42 {
43   msg_file_priv_t priv = MSG_file_priv(fd);
44   priv->data = data;
45   return MSG_OK;
46 }
47
48 /** \ingroup msg_file_management
49  *
50  * \brief Return the user data of a #msg_file_t.
51  *
52  * This functions checks whether \a file is a valid pointer and return the user data associated to \a file if possible.
53  */
54 void *MSG_file_get_data(msg_file_t fd)
55 {
56   msg_file_priv_t priv = MSG_file_priv(fd);
57   return priv->data;
58 }
59
60 /** \ingroup msg_file_management
61  * \brief Display information related to a file descriptor
62  *
63  * \param fd is a the file descriptor
64  */
65 void MSG_file_dump (msg_file_t fd){
66   /* Update the cached information first */
67   __MSG_file_get_info(fd);
68
69   msg_file_priv_t priv = MSG_file_priv(fd);
70   XBT_INFO("File Descriptor information:\n"
71            "\t\tFull path: '%s'\n"
72            "\t\tSize: %llu\n"
73            "\t\tMount point: '%s'\n"
74            "\t\tStorage Id: '%s'\n"
75            "\t\tStorage Type: '%s'\n"
76            "\t\tContent Type: '%s'\n"
77            "\t\tFile Descriptor Id: %d",
78            priv->fullpath, priv->size, priv->mount_point,
79            priv->storageId, priv->storage_type,
80            priv->content_type, priv->desc_id);
81 }
82
83 /** \ingroup msg_file_management
84  * \brief Read a file (local or remote)
85  *
86  * \param size of the file to read
87  * \param fd is a the file descriptor
88  * \return the number of bytes successfully read or -1 if an error occurred
89  */
90 sg_size_t MSG_file_read(msg_file_t fd, sg_size_t size)
91 {
92   msg_file_priv_t file_priv = MSG_file_priv(fd);
93   sg_size_t read_size;
94
95   /* Find the host where the file is physically located and read it */
96   msg_storage_t storage_src =(msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId);
97   msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
98   msg_host_t attached_host = MSG_host_by_name(storage_priv_src->hostname);
99   read_size = simcall_file_read(file_priv->simdata->smx_file, size, attached_host);
100
101   if(strcmp(storage_priv_src->hostname, MSG_host_get_name(MSG_host_self()))){
102     /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
103     XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", storage_priv_src->hostname, read_size);
104     msg_host_t *m_host_list = NULL;
105     m_host_list = (msg_host_t*) calloc(2, sizeof(msg_host_t));
106
107     m_host_list[0] = MSG_host_self();
108     m_host_list[1] = attached_host;
109     double flops_amount[] = { 0, 0 };
110     double bytes_amount[] = { 0, 0, (double)read_size, 0 };
111
112     msg_task_t task = MSG_parallel_task_create("file transfer for read", 2, m_host_list, flops_amount, bytes_amount,
113                       NULL);
114     msg_error_t transfer = MSG_parallel_task_execute(task);
115     MSG_task_destroy(task);
116     free(m_host_list);
117     if(transfer != MSG_OK){
118       if (transfer == MSG_HOST_FAILURE)
119         XBT_WARN("Transfer error, %s remote host just turned off!", MSG_host_get_name(attached_host));
120       if (transfer == MSG_TASK_CANCELED)
121         XBT_WARN("Transfer error, task has been canceled!");
122
123       return -1;
124     }
125   }
126   return read_size;
127 }
128
129 /** \ingroup msg_file_management
130  * \brief Write into a file (local or remote)
131  *
132  * \param size of the file to write
133  * \param fd is a the file descriptor
134  * \return the number of bytes successfully write or -1 if an error occurred
135  */
136 sg_size_t MSG_file_write(msg_file_t fd, sg_size_t size)
137 {
138   msg_file_priv_t file_priv = MSG_file_priv(fd);
139   sg_size_t write_size, offset;
140
141   /* Find the host where the file is physically located (remote or local)*/
142   msg_storage_t storage_src =(msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId);
143   msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
144   msg_host_t attached_host = MSG_host_by_name(storage_priv_src->hostname);
145
146   if(strcmp(storage_priv_src->hostname, MSG_host_get_name(MSG_host_self()))){
147     /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
148     XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", storage_priv_src->hostname, size);
149     msg_host_t *m_host_list = NULL;
150     m_host_list = (msg_host_t*) calloc(2, sizeof(msg_host_t));
151
152     m_host_list[0] = MSG_host_self();
153     m_host_list[1] = attached_host;
154     double flops_amount[] = { 0, 0 };
155     double bytes_amount[] = { 0, (double)size, 0, 0 };
156
157     msg_task_t task = MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount,
158                                                NULL);
159     msg_error_t transfer = MSG_parallel_task_execute(task);
160     MSG_task_destroy(task);
161     free(m_host_list);
162     if(transfer != MSG_OK){
163       if (transfer == MSG_HOST_FAILURE)
164         XBT_WARN("Transfer error, %s remote host just turned off!", MSG_host_get_name(attached_host));
165       if (transfer == MSG_TASK_CANCELED)
166         XBT_WARN("Transfer error, task has been canceled!");
167
168       return -1;
169     }
170   }
171   /* Write file on local or remote host */
172   offset = simcall_file_tell(file_priv->simdata->smx_file);
173   write_size = simcall_file_write(file_priv->simdata->smx_file, size, attached_host);
174   file_priv->size = offset+write_size;
175
176   return write_size;
177 }
178
179 /** \ingroup msg_file_management
180  * \brief Opens the file whose name is the string pointed to by path
181  *
182  * \param fullpath is the file location on the storage
183  * \param data user data to attach to the file
184  *
185  * \return An #msg_file_t associated to the file
186  */
187 msg_file_t MSG_file_open(const char* fullpath, void* data)
188 {
189   char *name;
190   msg_file_priv_t priv = xbt_new(s_msg_file_priv_t, 1);
191   priv->data = data;
192   priv->fullpath = xbt_strdup(fullpath);
193   priv->simdata = xbt_new0(s_simdata_file_t,1);
194   priv->simdata->smx_file = simcall_file_open(fullpath, MSG_host_self());
195   priv->desc_id = __MSG_host_get_file_descriptor_id(MSG_host_self());
196
197   name = bprintf("%s:%s:%d", priv->fullpath, MSG_host_get_name(MSG_host_self()), priv->desc_id);
198
199   xbt_lib_set(file_lib, name, MSG_FILE_LEVEL, priv);
200   msg_file_t fd = (msg_file_t) xbt_lib_get_elm_or_null(file_lib, name);
201   __MSG_file_get_info(fd);
202   xbt_free(name);
203
204   return fd;
205 }
206
207 /** \ingroup msg_file_management
208  * \brief Close the file
209  *
210  * \param fd is the file to close
211  * \return 0 on success or 1 on error
212  */
213 int MSG_file_close(msg_file_t fd)
214 {
215   char *name;
216   msg_file_priv_t priv = MSG_file_priv(fd);
217   if (priv->data)
218     xbt_free(priv->data);
219
220   int res = simcall_file_close(priv->simdata->smx_file, MSG_host_self());
221   name = bprintf("%s:%s:%d", priv->fullpath, MSG_host_get_name(MSG_host_self()), priv->desc_id);
222   __MSG_host_release_file_descriptor_id(MSG_host_self(), priv->desc_id);
223   xbt_lib_unset(file_lib, name, MSG_FILE_LEVEL, 1);
224   xbt_free(name);
225   return res;
226 }
227
228 /** \ingroup msg_file_management
229  * \brief Unlink the file pointed by fd
230  *
231  * \param fd is the file descriptor (#msg_file_t)
232  * \return 0 on success or 1 on error
233  */
234 msg_error_t MSG_file_unlink(msg_file_t fd)
235 {
236   msg_file_priv_t file_priv = MSG_file_priv(fd);
237   /* Find the host where the file is physically located (remote or local)*/
238   msg_storage_t storage_src =
239       (msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId);
240   msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
241   msg_host_t attached_host = MSG_host_by_name(storage_priv_src->hostname);
242   int res = simcall_file_unlink(file_priv->simdata->smx_file, attached_host);
243   return (msg_error_t) res;
244 }
245
246 /** \ingroup msg_file_management
247  * \brief Return the size of a file
248  *
249  * \param fd is the file descriptor (#msg_file_t)
250  * \return the size of the file (as a #sg_size_t)
251  */
252 sg_size_t MSG_file_get_size(msg_file_t fd){
253   msg_file_priv_t priv = MSG_file_priv(fd);
254   return simcall_file_get_size(priv->simdata->smx_file);
255 }
256
257 /**
258  * \ingroup msg_file_management
259  * \brief Set the file position indicator in the msg_file_t by adding offset bytes
260  * to the position specified by origin (either SEEK_SET, SEEK_CUR, or SEEK_END).
261  *
262  * \param fd : file object that identifies the stream
263  * \param offset : number of bytes to offset from origin
264  * \param origin : Position used as reference for the offset. It is specified by one of the following constants defined
265  *                 in \<stdio.h\> exclusively to be used as arguments for this function (SEEK_SET = beginning of file,
266  *                 SEEK_CUR = current position of the file pointer, SEEK_END = end of file)
267  * \return If successful, the function returns MSG_OK (=0). Otherwise, it returns MSG_TASK_CANCELED (=8).
268  */
269 msg_error_t MSG_file_seek(msg_file_t fd, sg_offset_t offset, int origin)
270 {
271   msg_file_priv_t priv = MSG_file_priv(fd);
272   return (msg_error_t) simcall_file_seek(priv->simdata->smx_file, offset, origin);
273 }
274
275 /**
276  * \ingroup msg_file_management
277  * \brief Returns the current value of the position indicator of the file
278  *
279  * \param fd : file object that identifies the stream
280  * \return On success, the current value of the position indicator is returned.
281  *
282  */
283 sg_size_t MSG_file_tell(msg_file_t fd)
284 {
285   msg_file_priv_t priv = MSG_file_priv(fd);
286   return simcall_file_tell(priv->simdata->smx_file);
287 }
288
289 const char *MSG_file_get_name(msg_file_t fd) {
290   xbt_assert((fd != NULL), "Invalid parameters");
291   msg_file_priv_t priv = MSG_file_priv(fd);
292   return priv->fullpath;
293 }
294
295 /**
296  * \ingroup msg_file_management
297  * \brief Move a file to another location on the *same mount point*.
298  *
299  */
300 msg_error_t MSG_file_move (msg_file_t fd, const char* fullpath)
301 {
302   msg_file_priv_t priv = MSG_file_priv(fd);
303   return (msg_error_t) simcall_file_move(priv->simdata->smx_file, fullpath);
304 }
305
306 /**
307  * \ingroup msg_file_management
308  * \brief Copy a file to another location on a remote host.
309  * \param file : the file to move
310  * \param host : the remote host where the file has to be copied
311  * \param fullpath : the complete path destination on the remote host
312  * \return If successful, the function returns MSG_OK. Otherwise, it returns MSG_TASK_CANCELED.
313  */
314 msg_error_t MSG_file_rcopy (msg_file_t file, msg_host_t host, const char* fullpath)
315 {
316   msg_file_priv_t file_priv = MSG_file_priv(file);
317   sg_size_t read_size;
318
319   /* Find the host where the file is physically located and read it */
320   msg_storage_t storage_src =(msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId);
321   msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
322   msg_host_t attached_host = MSG_host_by_name(storage_priv_src->hostname);
323   MSG_file_seek(file, 0, SEEK_SET);
324   read_size = simcall_file_read(file_priv->simdata->smx_file, file_priv->size, attached_host);
325
326   /* Find the real host destination where the file will be physically stored */
327   xbt_dict_cursor_t cursor = NULL;
328   char *mount_name, *storage_name, *file_mount_name, *host_name_dest;
329   msg_storage_t storage_dest = NULL;
330   msg_host_t host_dest;
331   size_t longest_prefix_length = 0;
332
333   xbt_dict_t storage_list = host->mountedStoragesAsDict();
334   xbt_dict_foreach(storage_list,cursor,mount_name,storage_name){
335     file_mount_name = (char *) xbt_malloc ((strlen(mount_name)+1));
336     strncpy(file_mount_name,fullpath,strlen(mount_name)+1);
337     file_mount_name[strlen(mount_name)] = '\0';
338
339     if(!strcmp(file_mount_name,mount_name) && strlen(mount_name)>longest_prefix_length){
340       /* The current mount name is found in the full path and is bigger than the previous*/
341       longest_prefix_length = strlen(mount_name);
342       storage_dest = (msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, storage_name);
343     }
344     free(file_mount_name);
345   }
346   xbt_dict_free(&storage_list);
347
348   if(longest_prefix_length>0){
349     /* Mount point found, retrieve the host the storage is attached to */
350     msg_storage_priv_t storage_dest_priv = MSG_storage_priv(storage_dest);
351     host_name_dest = (char*)storage_dest_priv->hostname;
352     host_dest = MSG_host_by_name(host_name_dest);
353
354   }else{
355     XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, sg_host_get_name(host));
356     return MSG_TASK_CANCELED;
357   }
358
359   XBT_DEBUG("Initiate data transfer of %llu bytes between %s and %s.", read_size, storage_priv_src->hostname,
360             host_name_dest);
361   msg_host_t *m_host_list = NULL;
362   m_host_list = (msg_host_t*) calloc(2, sizeof(msg_host_t));
363
364   m_host_list[0] = attached_host;
365   m_host_list[1] = host_dest;
366   double flops_amount[] = { 0, 0 };
367   double bytes_amount[] = { 0, (double)read_size, 0, 0 };
368
369   msg_task_t task =
370       MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount, NULL);
371   msg_error_t transfer = MSG_parallel_task_execute(task);
372   MSG_task_destroy(task);
373   free(m_host_list);
374   if(transfer != MSG_OK){
375     if (transfer == MSG_HOST_FAILURE)
376       XBT_WARN("Transfer error, %s remote host just turned off!", host_name_dest);
377     if (transfer == MSG_TASK_CANCELED)
378       XBT_WARN("Transfer error, task has been canceled!");
379
380     return (msg_error_t) -1;
381   }
382
383   /* Create file on remote host, write it and close it */
384   smx_file_t smx_file = simcall_file_open(fullpath, host_dest);
385   simcall_file_write(smx_file, read_size, host_dest);
386   simcall_file_close(smx_file, host_dest);
387   return MSG_OK;
388 }
389
390 /**
391  * \ingroup msg_file_management
392  * \brief Move a file to another location on a remote host.
393  * \param file : the file to move
394  * \param host : the remote host where the file has to be moved
395  * \param fullpath : the complete path destination on the remote host
396  * \return If successful, the function returns MSG_OK. Otherwise, it returns MSG_TASK_CANCELED.
397  */
398 msg_error_t MSG_file_rmove (msg_file_t file, msg_host_t host, const char* fullpath)
399 {
400   msg_error_t res = MSG_file_rcopy(file, host, fullpath);
401   MSG_file_unlink(file);
402   return res;
403 }
404
405 /**
406  * \brief Destroys a file (internal call only)
407  */
408 void __MSG_file_destroy(msg_file_priv_t file) {
409   xbt_free(file->fullpath);
410   xbt_free(file->simdata);
411   xbt_free(file);
412 }
413
414 /********************************* Storage **************************************/
415 /** @addtogroup msg_storage_management
416  * (#msg_storage_t) and the functions for managing it.
417  */
418
419 msg_storage_t __MSG_storage_create(smx_storage_t storage)
420 {
421   const char *name = SIMIX_storage_get_name(storage);
422   const char *host = SIMIX_storage_get_host(storage);
423   msg_storage_priv_t storage_private = xbt_new0(s_msg_storage_priv_t, 1);
424   storage_private->hostname = host;
425   xbt_lib_set(storage_lib,name,MSG_STORAGE_LEVEL,storage_private);
426   return xbt_lib_get_elm_or_null(storage_lib, name);
427 }
428
429 /**
430  * \brief Destroys a storage (internal call only)
431  */
432 void __MSG_storage_destroy(msg_storage_priv_t storage) {
433   free(storage);
434 }
435
436 /** \ingroup msg_storage_management
437  *
438  * \brief Returns the name of the #msg_storage_t.
439  *
440  * This functions checks whether a storage is a valid pointer or not and return its name.
441  */
442 const char *MSG_storage_get_name(msg_storage_t storage) {
443   xbt_assert((storage != NULL), "Invalid parameters");
444   return SIMIX_storage_get_name(storage);
445 }
446
447 /** \ingroup msg_storage_management
448  * \brief Returns the free space size of a storage element
449  * \param storage a storage
450  * \return the free space size of the storage element (as a #sg_size_t)
451  */
452 sg_size_t MSG_storage_get_free_size(msg_storage_t storage){
453   return simcall_storage_get_free_size(storage);
454 }
455
456 /** \ingroup msg_storage_management
457  * \brief Returns the used space size of a storage element
458  * \param storage a storage
459  * \return the used space size of the storage element (as a #sg_size_t)
460  */
461 sg_size_t MSG_storage_get_used_size(msg_storage_t storage){
462   return simcall_storage_get_used_size(storage);
463 }
464
465 /** \ingroup msg_storage_management
466  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this storage
467  * \param storage a storage
468  * \return a dict containing the properties
469  */
470 xbt_dict_t MSG_storage_get_properties(msg_storage_t storage)
471 {
472   xbt_assert((storage != NULL), "Invalid parameters (storage is NULL)");
473   return (simcall_storage_get_properties(storage));
474 }
475
476 /** \ingroup msg_storage_management
477  * \brief Change the value of a given storage property
478  *
479  * \param storage a storage
480  * \param name a property name
481  * \param value what to change the property to
482  * \param free_ctn the freeing function to use to kill the value on need
483  */
484 void MSG_storage_set_property_value(msg_storage_t storage, const char *name, char *value,void_f_pvoid_t free_ctn) {
485   xbt_dict_set(MSG_storage_get_properties(storage), name, value,free_ctn);
486 }
487
488 /** \ingroup m_storage_management
489  * \brief Returns the value of a given storage property
490  *
491  * \param storage a storage
492  * \param name a property name
493  * \return value of a property (or NULL if property not set)
494  */
495 const char *MSG_storage_get_property_value(msg_storage_t storage, const char *name)
496 {
497   return (char*) xbt_dict_get_or_null(MSG_storage_get_properties(storage), name);
498 }
499
500 /** \ingroup msg_storage_management
501  * \brief Finds a msg_storage_t using its name.
502  * \param name the name of a storage
503  * \return the corresponding storage
504  */
505 msg_storage_t MSG_storage_get_by_name(const char *name)
506 {
507   return (msg_storage_t) xbt_lib_get_elm_or_null(storage_lib,name);
508 }
509
510 /** \ingroup msg_storage_management
511  * \brief Returns a dynar containing all the storage elements declared at a given point of time
512  */
513 xbt_dynar_t MSG_storages_as_dynar(void) {
514   xbt_lib_cursor_t cursor;
515   char *key;
516   void **data;
517   xbt_dynar_t res = xbt_dynar_new(sizeof(msg_storage_t),NULL);
518
519   xbt_lib_foreach(storage_lib, cursor, key, data) {
520     if(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), MSG_STORAGE_LEVEL) != NULL) {
521       xbt_dictelm_t elm = xbt_dict_cursor_get_elm(cursor);
522       xbt_dynar_push(res, &elm);
523     }
524   }
525   return res;
526 }
527
528 /** \ingroup msg_storage_management
529  *
530  * \brief Set the user data of a #msg_storage_t.
531  * This functions attach \a data to \a storage if possible.
532  */
533 msg_error_t MSG_storage_set_data(msg_storage_t storage, void *data)
534 {
535   msg_storage_priv_t priv = MSG_storage_priv(storage);
536   priv->data = data;
537   return MSG_OK;
538 }
539
540 /** \ingroup m_host_management
541  *
542  * \brief Returns the user data of a #msg_storage_t.
543  *
544  * This functions checks whether \a storage is a valid pointer and returns its associate user data if possible.
545  */
546 void *MSG_storage_get_data(msg_storage_t storage)
547 {
548   xbt_assert((storage != NULL), "Invalid parameters");
549   msg_storage_priv_t priv = MSG_storage_priv(storage);
550   return priv->data;
551 }
552
553 /** \ingroup msg_storage_management
554  *
555  * \brief Returns the content (file list) of a #msg_storage_t.
556  * \param storage a storage
557  * \return The content of this storage element as a dict (full path file => size)
558  */
559 xbt_dict_t MSG_storage_get_content(msg_storage_t storage)
560 {
561   return SIMIX_storage_get_content(storage);
562 }
563
564 /** \ingroup msg_storage_management
565  *
566  * \brief Returns the size of a #msg_storage_t.
567  * \param storage a storage
568  * \return The size of the storage
569  */
570 sg_size_t MSG_storage_get_size(msg_storage_t storage)
571 {
572   return SIMIX_storage_get_size(storage);
573 }
574
575 /** \ingroup msg_storage_management
576  *
577  * \brief Returns the host name the storage is attached to
578  *
579  * This functions checks whether a storage is a valid pointer or not and return its name.
580  */
581 const char *MSG_storage_get_host(msg_storage_t storage) {
582   xbt_assert((storage != NULL), "Invalid parameters");
583   msg_storage_priv_t priv = MSG_storage_priv(storage);
584   return priv->hostname;
585 }