Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
uppercase the s4u header files (+cleanups)
[simgrid.git] / src / msg / msg_io.cpp
1 /* Copyright (c) 2004-2017. 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 #include "src/surf/storage_interface.hpp"
9 #include <numeric>
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_io, msg, "Logging specific to MSG (io)");
12
13 SG_BEGIN_DECL()
14
15 /** @addtogroup msg_file
16  * (#msg_file_t) and the functions for managing it.
17  *
18  *  \see #msg_file_t
19  */
20
21 /********************************* File **************************************/
22 void __MSG_file_get_info(msg_file_t fd){
23
24   xbt_dynar_t info = simcall_file_get_info(fd->simdata->smx_file);
25   sg_size_t *psize;
26
27   fd->content_type = xbt_dynar_pop_as(info, char*);
28   fd->storage_type = xbt_dynar_pop_as(info, char*);
29   fd->storageId    = xbt_dynar_pop_as(info, char*);
30   fd->mount_point  = xbt_dynar_pop_as(info, char*);
31   psize            = xbt_dynar_pop_as(info, sg_size_t*);
32   fd->size         = *psize;
33   xbt_free(psize);
34   xbt_dynar_free_container(&info);
35 }
36
37 static int MSG_host_get_file_descriptor_id(msg_host_t host)
38 {
39   simgrid::MsgHostExt* priv = host->extension<simgrid::MsgHostExt>();
40   if (priv->file_descriptor_table == nullptr) {
41     priv->file_descriptor_table = new std::vector<int>(sg_storage_max_file_descriptors);
42     std::iota(priv->file_descriptor_table->rbegin(), priv->file_descriptor_table->rend(), 0); // Fill with ..., 1, 0.
43   }
44   xbt_assert(!priv->file_descriptor_table->empty(), "Too much files are opened! Some have to be closed.");
45   int desc = priv->file_descriptor_table->back();
46   priv->file_descriptor_table->pop_back();
47   return desc;
48 }
49
50 static void MSG_host_release_file_descriptor_id(msg_host_t host, int id)
51 {
52   host->extension<simgrid::MsgHostExt>()->file_descriptor_table->push_back(id);
53 }
54
55 /** \ingroup msg_file
56  *
57  * \brief Set the user data of a #msg_file_t.
58  *
59  * This functions attach \a data to \a file.
60  */
61 msg_error_t MSG_file_set_data(msg_file_t fd, void *data)
62 {
63   fd->data = data;
64   return MSG_OK;
65 }
66
67 /** \ingroup msg_file
68  *
69  * \brief Return the user data of a #msg_file_t.
70  *
71  * This functions checks whether \a file is a valid pointer and return the user data associated to \a file if possible.
72  */
73 void *MSG_file_get_data(msg_file_t fd)
74 {
75   return fd->data;
76 }
77
78 /** \ingroup msg_file
79  * \brief Display information related to a file descriptor
80  *
81  * \param fd is a the file descriptor
82  */
83 void MSG_file_dump (msg_file_t fd){
84   /* Update the cached information first */
85   __MSG_file_get_info(fd);
86
87   XBT_INFO("File Descriptor information:\n"
88            "\t\tFull path: '%s'\n"
89            "\t\tSize: %llu\n"
90            "\t\tMount point: '%s'\n"
91            "\t\tStorage Id: '%s'\n"
92            "\t\tStorage Type: '%s'\n"
93            "\t\tContent Type: '%s'\n"
94            "\t\tFile Descriptor Id: %d",
95            fd->fullpath, fd->size, fd->mount_point, fd->storageId, fd->storage_type, fd->content_type, fd->desc_id);
96 }
97
98 /** \ingroup msg_file
99  * \brief Read a file (local or remote)
100  *
101  * \param size of the file to read
102  * \param fd is a the file descriptor
103  * \return the number of bytes successfully read or -1 if an error occurred
104  */
105 sg_size_t MSG_file_read(msg_file_t fd, sg_size_t size)
106 {
107   sg_size_t read_size;
108
109   if (fd->size == 0) /* Nothing to read, return */
110     return 0;
111
112   /* Find the host where the file is physically located and read it */
113   msg_storage_t storage_src           = static_cast<msg_storage_t>(xbt_lib_get_elm_or_null(storage_lib, fd->storageId));
114   msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
115   msg_host_t attached_host            = MSG_host_by_name(storage_priv_src->hostname);
116   read_size                           = simcall_file_read(fd->simdata->smx_file, size, attached_host);
117
118   if (strcmp(storage_priv_src->hostname, MSG_host_self()->cname())) {
119     /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
120     XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", storage_priv_src->hostname, read_size);
121     msg_host_t *m_host_list = nullptr;
122     m_host_list = xbt_new0(msg_host_t, 2);
123
124     m_host_list[0] = MSG_host_self();
125     m_host_list[1] = attached_host;
126     double flops_amount[] = { 0, 0};
127     double bytes_amount[] = { 0, 0, static_cast<double>(read_size), 0 };
128
129     msg_task_t task = MSG_parallel_task_create("file transfer for read", 2, m_host_list, flops_amount, bytes_amount,
130                       nullptr);
131     msg_error_t transfer = MSG_parallel_task_execute(task);
132     MSG_task_destroy(task);
133     xbt_free(m_host_list);
134     if(transfer != MSG_OK){
135       if (transfer == MSG_HOST_FAILURE)
136         XBT_WARN("Transfer error, %s remote host just turned off!", attached_host->cname());
137       if (transfer == MSG_TASK_CANCELED)
138         XBT_WARN("Transfer error, task has been canceled!");
139
140       return -1;
141     }
142   }
143   return read_size;
144 }
145
146 /** \ingroup msg_file
147  * \brief Write into a file (local or remote)
148  *
149  * \param size of the file to write
150  * \param fd is a the file descriptor
151  * \return the number of bytes successfully write or -1 if an error occurred
152  */
153 sg_size_t MSG_file_write(msg_file_t fd, sg_size_t size)
154 {
155   if (size == 0) /* Nothing to write, return */
156     return 0;
157
158   /* Find the host where the file is physically located (remote or local)*/
159   msg_storage_t storage_src           = static_cast<msg_storage_t>(xbt_lib_get_elm_or_null(storage_lib, fd->storageId));
160   msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
161   msg_host_t attached_host            = MSG_host_by_name(storage_priv_src->hostname);
162
163   if (strcmp(storage_priv_src->hostname, MSG_host_self()->cname())) {
164     /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
165     XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", storage_priv_src->hostname, size);
166     msg_host_t *m_host_list = nullptr;
167     m_host_list = xbt_new0(msg_host_t, 2);
168
169     m_host_list[0] = MSG_host_self();
170     m_host_list[1] = attached_host;
171     double flops_amount[] = { 0, 0 };
172     double bytes_amount[] = { 0, static_cast<double>(size), 0, 0 };
173
174     msg_task_t task = MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount,
175                                                nullptr);
176     msg_error_t transfer = MSG_parallel_task_execute(task);
177     MSG_task_destroy(task);
178     free(m_host_list);
179     if(transfer != MSG_OK){
180       if (transfer == MSG_HOST_FAILURE)
181         XBT_WARN("Transfer error, %s remote host just turned off!", attached_host->cname());
182       if (transfer == MSG_TASK_CANCELED)
183         XBT_WARN("Transfer error, task has been canceled!");
184
185       return -1;
186     }
187   }
188   /* Write file on local or remote host */
189   sg_size_t offset     = simcall_file_tell(fd->simdata->smx_file);
190   sg_size_t write_size = simcall_file_write(fd->simdata->smx_file, size, attached_host);
191   fd->size             = offset + write_size;
192
193   return write_size;
194 }
195
196 /** \ingroup msg_file
197  * \brief Opens the file whose name is the string pointed to by path
198  *
199  * \param fullpath is the file location on the storage
200  * \param data user data to attach to the file
201  *
202  * \return An #msg_file_t associated to the file
203  */
204 msg_file_t MSG_file_open(const char* fullpath, void* data)
205 {
206   msg_file_t fd         = xbt_new(s_msg_file_priv_t, 1);
207   fd->data              = data;
208   fd->fullpath          = xbt_strdup(fullpath);
209   fd->simdata           = xbt_new0(s_simdata_file_t, 1);
210   fd->simdata->smx_file = simcall_file_open(fullpath, MSG_host_self());
211   fd->desc_id           = MSG_host_get_file_descriptor_id(MSG_host_self());
212
213   __MSG_file_get_info(fd);
214
215   return fd;
216 }
217
218 /** \ingroup msg_file
219  * \brief Close the file
220  *
221  * \param fd is the file to close
222  * \return 0 on success or 1 on error
223  */
224 int MSG_file_close(msg_file_t fd)
225 {
226   if (fd->data)
227     xbt_free(fd->data);
228
229   int res = simcall_file_close(fd->simdata->smx_file, MSG_host_self());
230   MSG_host_release_file_descriptor_id(MSG_host_self(), fd->desc_id);
231   __MSG_file_destroy(fd);
232
233   return res;
234 }
235
236 /** \ingroup msg_file
237  * \brief Unlink the file pointed by fd
238  *
239  * \param fd is the file descriptor (#msg_file_t)
240  * \return 0 on success or 1 on error
241  */
242 msg_error_t MSG_file_unlink(msg_file_t fd)
243 {
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, fd->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(fd->simdata->smx_file, attached_host);
249   __MSG_file_destroy(fd);
250   return static_cast<msg_error_t>(res);
251 }
252
253 /** \ingroup msg_file
254  * \brief Return the size of a file
255  *
256  * \param fd is the file descriptor (#msg_file_t)
257  * \return the size of the file (as a #sg_size_t)
258  */
259 sg_size_t MSG_file_get_size(msg_file_t fd){
260   return simcall_file_get_size(fd->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   return static_cast<msg_error_t>(simcall_file_seek(fd->simdata->smx_file, offset, origin));
278 }
279
280 /**
281  * \ingroup msg_file
282  * \brief Returns the current value of the position indicator of the file
283  *
284  * \param fd : file object that identifies the stream
285  * \return On success, the current value of the position indicator is returned.
286  *
287  */
288 sg_size_t MSG_file_tell(msg_file_t fd)
289 {
290   return simcall_file_tell(fd->simdata->smx_file);
291 }
292
293 const char *MSG_file_get_name(msg_file_t fd) {
294   xbt_assert((fd != nullptr), "Invalid parameters");
295   return fd->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   return static_cast<msg_error_t>(simcall_file_move(fd->simdata->smx_file, fullpath));
306 }
307
308 /**
309  * \ingroup msg_file
310  * \brief Copy a file to another location on a remote host.
311  * \param file : the file to move
312  * \param host : the remote host where the file has to be copied
313  * \param fullpath : the complete path destination on the remote host
314  * \return If successful, the function returns MSG_OK. Otherwise, it returns MSG_TASK_CANCELED.
315  */
316 msg_error_t MSG_file_rcopy (msg_file_t file, msg_host_t host, const char* fullpath)
317 {
318   /* Find the host where the file is physically located and read it */
319   msg_storage_t storage_src = static_cast<msg_storage_t>(xbt_lib_get_elm_or_null(storage_lib, file->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   sg_size_t read_size = simcall_file_read(file->simdata->smx_file, file->size, attached_host);
324
325   /* Find the real host destination where the file will be physically stored */
326   xbt_dict_cursor_t cursor   = nullptr;
327   msg_storage_t storage_dest = nullptr;
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;
333   char *storage_name;
334   xbt_dict_foreach(storage_list,cursor,mount_name,storage_name){
335     char* file_mount_name = static_cast<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     xbt_free(file_mount_name);
345   }
346   xbt_dict_free(&storage_list);
347
348   char* host_name_dest = nullptr;
349   if(longest_prefix_length>0){
350     /* Mount point found, retrieve the host the storage is attached to */
351     msg_storage_priv_t storage_dest_priv = MSG_storage_priv(storage_dest);
352     host_name_dest = (char*)storage_dest_priv->hostname;
353     host_dest = MSG_host_by_name(host_name_dest);
354   }else{
355     XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, host->cname());
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 = nullptr;
362   m_host_list = xbt_new0(msg_host_t, 2);
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, static_cast<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, nullptr);
371   msg_error_t transfer = MSG_parallel_task_execute(task);
372   MSG_task_destroy(task);
373   xbt_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 transfer;
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
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_t file)
409 {
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   msg_storage_priv_t storage_private = xbt_new0(s_msg_storage_priv_t, 1);
423
424   storage_private->name     = SIMIX_storage_get_name(storage);
425   storage_private->hostname = surf_storage_get_host(storage);
426   storage_private->size     = surf_storage_get_size(storage);
427
428   xbt_lib_set(storage_lib, storage_private->name, MSG_STORAGE_LEVEL, storage_private);
429   return xbt_lib_get_elm_or_null(storage_lib, storage_private->name);
430 }
431
432 /**
433  * \brief Destroys a storage (internal call only)
434  */
435 void __MSG_storage_destroy(msg_storage_priv_t storage) {
436   free(storage);
437 }
438
439 /** \ingroup msg_storage_management
440  *
441  * \brief Returns the name of the #msg_storage_t.
442  *
443  * This functions checks whether a storage is a valid pointer or not and return its name.
444  */
445 const char *MSG_storage_get_name(msg_storage_t storage) {
446   xbt_assert((storage != nullptr), "Invalid parameters");
447   msg_storage_priv_t priv = MSG_storage_priv(storage);
448   return priv->name;
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 simgrid::simix::kernelImmediate([storage] { return surf_storage_resource_priv(storage)->getFreeSize(); });
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 simgrid::simix::kernelImmediate([storage] { return surf_storage_resource_priv(storage)->getUsedSize(); });
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   std::map<std::string, sg_size_t*>* content =
566       simgrid::simix::kernelImmediate([storage] { return surf_storage_resource_priv(storage)->getContent(); });
567   xbt_dict_t content_dict = xbt_dict_new_homogeneous(nullptr);
568
569   for (auto entry : *content) {
570     xbt_dict_set(content_dict, entry.first.c_str(), entry.second, nullptr);
571   }
572   return content_dict;
573 }
574
575 /** \ingroup msg_storage_management
576  *
577  * \brief Returns the size of a #msg_storage_t.
578  * \param storage a storage
579  * \return The size of the storage
580  */
581 sg_size_t MSG_storage_get_size(msg_storage_t storage)
582 {
583   msg_storage_priv_t priv = MSG_storage_priv(storage);
584   return priv->size;
585 }
586
587 /** \ingroup msg_storage_management
588  *
589  * \brief Returns the host name the storage is attached to
590  *
591  * This functions checks whether a storage is a valid pointer or not and return its name.
592  */
593 const char *MSG_storage_get_host(msg_storage_t storage) {
594   xbt_assert((storage != nullptr), "Invalid parameters");
595   msg_storage_priv_t priv = MSG_storage_priv(storage);
596   return priv->hostname;
597 }
598
599 SG_END_DECL()