Logo AND Algorithmique Numérique Distribuée

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