Logo AND Algorithmique Numérique Distribuée

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