Logo AND Algorithmique Numérique Distribuée

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