Logo AND Algorithmique Numérique Distribuée

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