Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b5f2a2b21ea8c9fa6505249643fed16760c20e0e
[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
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
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
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
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
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
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
140   /* Find the host where the file is physically located (remote or local)*/
141   msg_storage_t storage_src =(msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId);
142   msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
143   msg_host_t attached_host = MSG_host_by_name(storage_priv_src->hostname);
144
145   if(strcmp(storage_priv_src->hostname, MSG_host_get_name(MSG_host_self()))){
146     /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
147     XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", storage_priv_src->hostname, size);
148     msg_host_t *m_host_list = NULL;
149     m_host_list = (msg_host_t*) calloc(2, sizeof(msg_host_t));
150
151     m_host_list[0] = MSG_host_self();
152     m_host_list[1] = attached_host;
153     double flops_amount[] = { 0, 0 };
154     double bytes_amount[] = { 0, (double)size, 0, 0 };
155
156     msg_task_t task = MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount,
157                                                NULL);
158     msg_error_t transfer = MSG_parallel_task_execute(task);
159     MSG_task_destroy(task);
160     free(m_host_list);
161     if(transfer != MSG_OK){
162       if (transfer == MSG_HOST_FAILURE)
163         XBT_WARN("Transfer error, %s remote host just turned off!", MSG_host_get_name(attached_host));
164       if (transfer == MSG_TASK_CANCELED)
165         XBT_WARN("Transfer error, task has been canceled!");
166
167       return -1;
168     }
169   }
170   /* Write file on local or remote host */
171   sg_size_t offset = simcall_file_tell(file_priv->simdata->smx_file);
172   sg_size_t write_size = simcall_file_write(file_priv->simdata->smx_file, size, attached_host);
173   file_priv->size = offset+write_size;
174
175   return write_size;
176 }
177
178 /** \ingroup msg_file
179  * \brief Opens the file whose name is the string pointed to by path
180  *
181  * \param fullpath is the file location on the storage
182  * \param data user data to attach to the file
183  *
184  * \return An #msg_file_t associated to the file
185  */
186 msg_file_t MSG_file_open(const char* fullpath, void* data)
187 {
188   char *name;
189   msg_file_priv_t priv = xbt_new(s_msg_file_priv_t, 1);
190   priv->data = data;
191   priv->fullpath = xbt_strdup(fullpath);
192   priv->simdata = xbt_new0(s_simdata_file_t,1);
193   priv->simdata->smx_file = simcall_file_open(fullpath, MSG_host_self());
194   priv->desc_id = __MSG_host_get_file_descriptor_id(MSG_host_self());
195
196   name = bprintf("%s:%s:%d", priv->fullpath, MSG_host_get_name(MSG_host_self()), priv->desc_id);
197
198   xbt_lib_set(file_lib, name, MSG_FILE_LEVEL, priv);
199   msg_file_t fd = (msg_file_t) xbt_lib_get_elm_or_null(file_lib, name);
200   __MSG_file_get_info(fd);
201   xbt_free(name);
202
203   return fd;
204 }
205
206 /** \ingroup msg_file
207  * \brief Close the file
208  *
209  * \param fd is the file to close
210  * \return 0 on success or 1 on error
211  */
212 int MSG_file_close(msg_file_t fd)
213 {
214   char *name;
215   msg_file_priv_t priv = MSG_file_priv(fd);
216   if (priv->data)
217     xbt_free(priv->data);
218
219   int res = simcall_file_close(priv->simdata->smx_file, MSG_host_self());
220   name = bprintf("%s:%s:%d", priv->fullpath, MSG_host_get_name(MSG_host_self()), priv->desc_id);
221   __MSG_host_release_file_descriptor_id(MSG_host_self(), priv->desc_id);
222   xbt_lib_unset(file_lib, name, MSG_FILE_LEVEL, 1);
223   xbt_free(name);
224   return res;
225 }
226
227 /** \ingroup msg_file
228  * \brief Unlink the file pointed by fd
229  *
230  * \param fd is the file descriptor (#msg_file_t)
231  * \return 0 on success or 1 on error
232  */
233 msg_error_t MSG_file_unlink(msg_file_t fd)
234 {
235   msg_file_priv_t file_priv = MSG_file_priv(fd);
236   /* Find the host where the file is physically located (remote or local)*/
237   msg_storage_t storage_src =
238       (msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId);
239   msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
240   msg_host_t attached_host = MSG_host_by_name(storage_priv_src->hostname);
241   int res = simcall_file_unlink(file_priv->simdata->smx_file, attached_host);
242   return (msg_error_t) res;
243 }
244
245 /** \ingroup msg_file
246  * \brief Return the size of a file
247  *
248  * \param fd is the file descriptor (#msg_file_t)
249  * \return the size of the file (as a #sg_size_t)
250  */
251 sg_size_t MSG_file_get_size(msg_file_t fd){
252   msg_file_priv_t priv = MSG_file_priv(fd);
253   return simcall_file_get_size(priv->simdata->smx_file);
254 }
255
256 /**
257  * \ingroup msg_file
258  * \brief Set the file position indicator in the msg_file_t by adding offset bytes
259  * to the position specified by origin (either SEEK_SET, SEEK_CUR, or SEEK_END).
260  *
261  * \param fd : file object that identifies the stream
262  * \param offset : number of bytes to offset from origin
263  * \param origin : Position used as reference for the offset. It is specified by one of the following constants defined
264  *                 in \<stdio.h\> exclusively to be used as arguments for this function (SEEK_SET = beginning of file,
265  *                 SEEK_CUR = current position of the file pointer, SEEK_END = end of file)
266  * \return If successful, the function returns MSG_OK (=0). Otherwise, it returns MSG_TASK_CANCELED (=8).
267  */
268 msg_error_t MSG_file_seek(msg_file_t fd, sg_offset_t offset, int origin)
269 {
270   msg_file_priv_t priv = MSG_file_priv(fd);
271   return (msg_error_t) simcall_file_seek(priv->simdata->smx_file, offset, origin);
272 }
273
274 /**
275  * \ingroup msg_file
276  * \brief Returns the current value of the position indicator of the file
277  *
278  * \param fd : file object that identifies the stream
279  * \return On success, the current value of the position indicator is returned.
280  *
281  */
282 sg_size_t MSG_file_tell(msg_file_t fd)
283 {
284   msg_file_priv_t priv = MSG_file_priv(fd);
285   return simcall_file_tell(priv->simdata->smx_file);
286 }
287
288 const char *MSG_file_get_name(msg_file_t fd) {
289   xbt_assert((fd != NULL), "Invalid parameters");
290   msg_file_priv_t priv = MSG_file_priv(fd);
291   return priv->fullpath;
292 }
293
294 /**
295  * \ingroup msg_file
296  * \brief Move a file to another location on the *same mount point*.
297  *
298  */
299 msg_error_t MSG_file_move (msg_file_t fd, const char* fullpath)
300 {
301   msg_file_priv_t priv = MSG_file_priv(fd);
302   return (msg_error_t) simcall_file_move(priv->simdata->smx_file, fullpath);
303 }
304
305 /**
306  * \ingroup msg_file
307  * \brief Copy a file to another location on a remote host.
308  * \param file : the file to move
309  * \param host : the remote host where the file has to be copied
310  * \param fullpath : the complete path destination on the remote host
311  * \return If successful, the function returns MSG_OK. Otherwise, it returns MSG_TASK_CANCELED.
312  */
313 msg_error_t MSG_file_rcopy (msg_file_t file, msg_host_t host, const char* fullpath)
314 {
315   msg_file_priv_t file_priv = MSG_file_priv(file);
316   sg_size_t read_size;
317
318   /* Find the host where the file is physically located and read it */
319   msg_storage_t storage_src =(msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId);
320   msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
321   msg_host_t attached_host = MSG_host_by_name(storage_priv_src->hostname);
322   MSG_file_seek(file, 0, SEEK_SET);
323   read_size = simcall_file_read(file_priv->simdata->smx_file, file_priv->size, attached_host);
324
325   /* Find the real host destination where the file will be physically stored */
326   xbt_dict_cursor_t cursor = NULL;
327   msg_storage_t storage_dest = NULL;
328   msg_host_t host_dest;
329   size_t longest_prefix_length = 0;
330
331   xbt_dict_t storage_list = host->mountedStoragesAsDict();
332   char *mount_name, *storage_name;
333   xbt_dict_foreach(storage_list,cursor,mount_name,storage_name){
334     char* file_mount_name = (char *) xbt_malloc ((strlen(mount_name)+1));
335     strncpy(file_mount_name,fullpath,strlen(mount_name)+1);
336     file_mount_name[strlen(mount_name)] = '\0';
337
338     if(!strcmp(file_mount_name,mount_name) && strlen(mount_name)>longest_prefix_length){
339       /* The current mount name is found in the full path and is bigger than the previous*/
340       longest_prefix_length = strlen(mount_name);
341       storage_dest = (msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, storage_name);
342     }
343     free(file_mount_name);
344   }
345   xbt_dict_free(&storage_list);
346
347   char* host_name_dest = nullptr;
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   }else{
354     XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, sg_host_get_name(host));
355     return MSG_TASK_CANCELED;
356   }
357
358   XBT_DEBUG("Initiate data transfer of %llu bytes between %s and %s.", read_size, storage_priv_src->hostname,
359             host_name_dest);
360   msg_host_t *m_host_list = NULL;
361   m_host_list = (msg_host_t*) calloc(2, sizeof(msg_host_t));
362
363   m_host_list[0] = attached_host;
364   m_host_list[1] = host_dest;
365   double flops_amount[] = { 0, 0 };
366   double bytes_amount[] = { 0, (double)read_size, 0, 0 };
367
368   msg_task_t task =
369       MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount, NULL);
370   msg_error_t transfer = MSG_parallel_task_execute(task);
371   MSG_task_destroy(task);
372   free(m_host_list);
373   if(transfer != MSG_OK){
374     if (transfer == MSG_HOST_FAILURE)
375       XBT_WARN("Transfer error, %s remote host just turned off!", host_name_dest);
376     if (transfer == MSG_TASK_CANCELED)
377       XBT_WARN("Transfer error, task has been canceled!");
378
379     return (msg_error_t) -1;
380   }
381
382   /* Create file on remote host, write it and close it */
383   smx_file_t smx_file = simcall_file_open(fullpath, host_dest);
384   simcall_file_write(smx_file, read_size, host_dest);
385   simcall_file_close(smx_file, host_dest);
386   return MSG_OK;
387 }
388
389 /**
390  * \ingroup msg_file
391  * \brief Move a file to another location on a remote host.
392  * \param file : the file to move
393  * \param host : the remote host where the file has to be moved
394  * \param fullpath : the complete path destination on the remote host
395  * \return If successful, the function returns MSG_OK. Otherwise, it returns MSG_TASK_CANCELED.
396  */
397 msg_error_t MSG_file_rmove (msg_file_t file, msg_host_t host, const char* fullpath)
398 {
399   msg_error_t res = MSG_file_rcopy(file, host, fullpath);
400   MSG_file_unlink(file);
401   return res;
402 }
403
404 /**
405  * \brief Destroys a file (internal call only)
406  */
407 void __MSG_file_destroy(msg_file_priv_t file) {
408   xbt_free(file->fullpath);
409   xbt_free(file->simdata);
410   xbt_free(file);
411 }
412
413 /********************************* Storage **************************************/
414 /** @addtogroup msg_storage_management
415  * (#msg_storage_t) and the functions for managing it.
416  */
417
418 msg_storage_t __MSG_storage_create(smx_storage_t storage)
419 {
420   const char *name = SIMIX_storage_get_name(storage);
421   const char *host = SIMIX_storage_get_host(storage);
422   msg_storage_priv_t storage_private = xbt_new0(s_msg_storage_priv_t, 1);
423   storage_private->hostname = host;
424   xbt_lib_set(storage_lib,name,MSG_STORAGE_LEVEL,storage_private);
425   return xbt_lib_get_elm_or_null(storage_lib, name);
426 }
427
428 /**
429  * \brief Destroys a storage (internal call only)
430  */
431 void __MSG_storage_destroy(msg_storage_priv_t storage) {
432   free(storage);
433 }
434
435 /** \ingroup msg_storage_management
436  *
437  * \brief Returns the name of the #msg_storage_t.
438  *
439  * This functions checks whether a storage is a valid pointer or not and return its name.
440  */
441 const char *MSG_storage_get_name(msg_storage_t storage) {
442   xbt_assert((storage != NULL), "Invalid parameters");
443   return SIMIX_storage_get_name(storage);
444 }
445
446 /** \ingroup msg_storage_management
447  * \brief Returns the free space size of a storage element
448  * \param storage a storage
449  * \return the free space size of the storage element (as a #sg_size_t)
450  */
451 sg_size_t MSG_storage_get_free_size(msg_storage_t storage){
452   return simcall_storage_get_free_size(storage);
453 }
454
455 /** \ingroup msg_storage_management
456  * \brief Returns the used space size of a storage element
457  * \param storage a storage
458  * \return the used space size of the storage element (as a #sg_size_t)
459  */
460 sg_size_t MSG_storage_get_used_size(msg_storage_t storage){
461   return simcall_storage_get_used_size(storage);
462 }
463
464 /** \ingroup msg_storage_management
465  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this storage
466  * \param storage a storage
467  * \return a dict containing the properties
468  */
469 xbt_dict_t MSG_storage_get_properties(msg_storage_t storage)
470 {
471   xbt_assert((storage != NULL), "Invalid parameters (storage is NULL)");
472   return (simcall_storage_get_properties(storage));
473 }
474
475 /** \ingroup msg_storage_management
476  * \brief Change the value of a given storage property
477  *
478  * \param storage a storage
479  * \param name a property name
480  * \param value what to change the property to
481  * \param free_ctn the freeing function to use to kill the value on need
482  */
483 void MSG_storage_set_property_value(msg_storage_t storage, const char *name, char *value,void_f_pvoid_t free_ctn) {
484   xbt_dict_set(MSG_storage_get_properties(storage), name, value,free_ctn);
485 }
486
487 /** \ingroup m_storage_management
488  * \brief Returns the value of a given storage property
489  *
490  * \param storage a storage
491  * \param name a property name
492  * \return value of a property (or NULL if property not set)
493  */
494 const char *MSG_storage_get_property_value(msg_storage_t storage, const char *name)
495 {
496   return (char*) xbt_dict_get_or_null(MSG_storage_get_properties(storage), name);
497 }
498
499 /** \ingroup msg_storage_management
500  * \brief Finds a msg_storage_t using its name.
501  * \param name the name of a storage
502  * \return the corresponding storage
503  */
504 msg_storage_t MSG_storage_get_by_name(const char *name)
505 {
506   return (msg_storage_t) xbt_lib_get_elm_or_null(storage_lib,name);
507 }
508
509 /** \ingroup msg_storage_management
510  * \brief Returns a dynar containing all the storage elements declared at a given point of time
511  */
512 xbt_dynar_t MSG_storages_as_dynar(void) {
513   xbt_lib_cursor_t cursor;
514   char *key;
515   void **data;
516   xbt_dynar_t res = xbt_dynar_new(sizeof(msg_storage_t),NULL);
517
518   xbt_lib_foreach(storage_lib, cursor, key, data) {
519     if(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), MSG_STORAGE_LEVEL) != NULL) {
520       xbt_dictelm_t elm = xbt_dict_cursor_get_elm(cursor);
521       xbt_dynar_push(res, &elm);
522     }
523   }
524   return res;
525 }
526
527 /** \ingroup msg_storage_management
528  *
529  * \brief Set the user data of a #msg_storage_t.
530  * This functions attach \a data to \a storage if possible.
531  */
532 msg_error_t MSG_storage_set_data(msg_storage_t storage, void *data)
533 {
534   msg_storage_priv_t priv = MSG_storage_priv(storage);
535   priv->data = data;
536   return MSG_OK;
537 }
538
539 /** \ingroup m_host_management
540  *
541  * \brief Returns the user data of a #msg_storage_t.
542  *
543  * This functions checks whether \a storage is a valid pointer and returns its associate user data if possible.
544  */
545 void *MSG_storage_get_data(msg_storage_t storage)
546 {
547   xbt_assert((storage != NULL), "Invalid parameters");
548   msg_storage_priv_t priv = MSG_storage_priv(storage);
549   return priv->data;
550 }
551
552 /** \ingroup msg_storage_management
553  *
554  * \brief Returns the content (file list) of a #msg_storage_t.
555  * \param storage a storage
556  * \return The content of this storage element as a dict (full path file => size)
557  */
558 xbt_dict_t MSG_storage_get_content(msg_storage_t storage)
559 {
560   return SIMIX_storage_get_content(storage);
561 }
562
563 /** \ingroup msg_storage_management
564  *
565  * \brief Returns the size of a #msg_storage_t.
566  * \param storage a storage
567  * \return The size of the storage
568  */
569 sg_size_t MSG_storage_get_size(msg_storage_t storage)
570 {
571   return SIMIX_storage_get_size(storage);
572 }
573
574 /** \ingroup msg_storage_management
575  *
576  * \brief Returns the host name the storage is attached to
577  *
578  * This functions checks whether a storage is a valid pointer or not and return its name.
579  */
580 const char *MSG_storage_get_host(msg_storage_t storage) {
581   xbt_assert((storage != NULL), "Invalid parameters");
582   msg_storage_priv_t priv = MSG_storage_priv(storage);
583   return priv->hostname;
584 }