Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove useless XBT_DEBUG
[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 "simgrid/s4u/Storage.hpp"
9 #include "src/msg/msg_private.h"
10 #include <numeric>
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_io, msg, "Logging specific to MSG (io)");
13
14 SG_BEGIN_DECL()
15
16 /** @addtogroup msg_file
17  * (#msg_file_t) and the functions for managing it.
18  *
19  *  \see #msg_file_t
20  */
21
22 /********************************* File **************************************/
23 void __MSG_file_get_info(msg_file_t fd){
24
25   xbt_dynar_t info = simcall_file_get_info(fd->simdata->smx_file);
26   sg_size_t *psize;
27
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(not 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\tFile Descriptor Id: %d",
94            fd->fullpath, fd->size, fd->mount_point, fd->storageId, fd->storage_type, fd->desc_id);
95 }
96
97 /** \ingroup msg_file
98  * \brief Read a file (local or remote)
99  *
100  * \param size of the file to read
101  * \param fd is a the file descriptor
102  * \return the number of bytes successfully read or -1 if an error occurred
103  */
104 sg_size_t MSG_file_read(msg_file_t fd, sg_size_t size)
105 {
106   sg_size_t read_size;
107
108   if (fd->size == 0) /* Nothing to read, return */
109     return 0;
110
111   /* Find the host where the file is physically located and read it */
112   msg_storage_t storage_src           = simgrid::s4u::Storage::byName(fd->storageId);
113   msg_host_t attached_host            = MSG_host_by_name(storage_src->host());
114   read_size                           = simcall_file_read(fd->simdata->smx_file, size, attached_host);
115
116   if (strcmp(storage_src->host(), 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_src->host(), 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 = simgrid::s4u::Storage::byName(fd->storageId);
154   msg_host_t attached_host  = MSG_host_by_name(storage_src->host());
155
156   if (strcmp(storage_src->host(), MSG_host_self()->cname())) {
157     /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
158     XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", storage_src->host(), size);
159     msg_host_t m_host_list[] = {MSG_host_self(), attached_host};
160     double flops_amount[]    = {0, 0};
161     double bytes_amount[]    = {0, static_cast<double>(size), 0, 0};
162
163     msg_task_t task = MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount,
164                                                nullptr);
165     msg_error_t transfer = MSG_parallel_task_execute(task);
166     MSG_task_destroy(task);
167
168     if(transfer != MSG_OK){
169       if (transfer == MSG_HOST_FAILURE)
170         XBT_WARN("Transfer error, %s remote host just turned off!", attached_host->cname());
171       if (transfer == MSG_TASK_CANCELED)
172         XBT_WARN("Transfer error, task has been canceled!");
173
174       return -1;
175     }
176   }
177   /* Write file on local or remote host */
178   sg_size_t offset     = simcall_file_tell(fd->simdata->smx_file);
179   sg_size_t write_size = simcall_file_write(fd->simdata->smx_file, size, attached_host);
180   fd->size             = offset + write_size;
181
182   return write_size;
183 }
184
185 /** \ingroup msg_file
186  * \brief Opens the file whose name is the string pointed to by path
187  *
188  * \param fullpath is the file location on the storage
189  * \param data user data to attach to the file
190  *
191  * \return An #msg_file_t associated to the file
192  */
193 msg_file_t MSG_file_open(const char* fullpath, void* data)
194 {
195   msg_file_t fd         = xbt_new(s_msg_file_priv_t, 1);
196   fd->data              = data;
197   fd->fullpath          = xbt_strdup(fullpath);
198   fd->simdata           = xbt_new0(s_simdata_file_t, 1);
199   fd->simdata->smx_file = simcall_file_open(fullpath, MSG_host_self());
200   fd->desc_id           = MSG_host_get_file_descriptor_id(MSG_host_self());
201
202   __MSG_file_get_info(fd);
203
204   return fd;
205 }
206
207 /** \ingroup msg_file
208  * \brief Close the file
209  *
210  * \param fd is the file to close
211  * \return 0 on success or 1 on error
212  */
213 int MSG_file_close(msg_file_t fd)
214 {
215   if (fd->data)
216     xbt_free(fd->data);
217
218   int res = simcall_file_close(fd->simdata->smx_file, MSG_host_self());
219   MSG_host_release_file_descriptor_id(MSG_host_self(), fd->desc_id);
220   __MSG_file_destroy(fd);
221
222   return res;
223 }
224
225 /** \ingroup msg_file
226  * \brief Unlink the file pointed by fd
227  *
228  * \param fd is the file descriptor (#msg_file_t)
229  * \return 0 on success or 1 on error
230  */
231 msg_error_t MSG_file_unlink(msg_file_t fd)
232 {
233   /* Find the host where the file is physically located (remote or local)*/
234   msg_storage_t storage_src           = simgrid::s4u::Storage::byName(fd->storageId);
235   msg_host_t attached_host            = MSG_host_by_name(storage_src->host());
236   int res                             = simcall_file_unlink(fd->simdata->smx_file, attached_host);
237   __MSG_file_destroy(fd);
238   return static_cast<msg_error_t>(res);
239 }
240
241 /** \ingroup msg_file
242  * \brief Return the size of a file
243  *
244  * \param fd is the file descriptor (#msg_file_t)
245  * \return the size of the file (as a #sg_size_t)
246  */
247 sg_size_t MSG_file_get_size(msg_file_t fd){
248   return simcall_file_get_size(fd->simdata->smx_file);
249 }
250
251 /**
252  * \ingroup msg_file
253  * \brief Set the file position indicator in the msg_file_t by adding offset bytes
254  * to the position specified by origin (either SEEK_SET, SEEK_CUR, or SEEK_END).
255  *
256  * \param fd : file object that identifies the stream
257  * \param offset : number of bytes to offset from origin
258  * \param origin : Position used as reference for the offset. It is specified by one of the following constants defined
259  *                 in \<stdio.h\> exclusively to be used as arguments for this function (SEEK_SET = beginning of file,
260  *                 SEEK_CUR = current position of the file pointer, SEEK_END = end of file)
261  * \return If successful, the function returns MSG_OK (=0). Otherwise, it returns MSG_TASK_CANCELED (=8).
262  */
263 msg_error_t MSG_file_seek(msg_file_t fd, sg_offset_t offset, int origin)
264 {
265   return static_cast<msg_error_t>(simcall_file_seek(fd->simdata->smx_file, offset, origin));
266 }
267
268 /**
269  * \ingroup msg_file
270  * \brief Returns the current value of the position indicator of the file
271  *
272  * \param fd : file object that identifies the stream
273  * \return On success, the current value of the position indicator is returned.
274  *
275  */
276 sg_size_t MSG_file_tell(msg_file_t fd)
277 {
278   return simcall_file_tell(fd->simdata->smx_file);
279 }
280
281 const char *MSG_file_get_name(msg_file_t fd) {
282   xbt_assert((fd != nullptr), "Invalid parameters");
283   return fd->fullpath;
284 }
285
286 /**
287  * \ingroup msg_file
288  * \brief Move a file to another location on the *same mount point*.
289  *
290  */
291 msg_error_t MSG_file_move (msg_file_t fd, const char* fullpath)
292 {
293   return static_cast<msg_error_t>(simcall_file_move(fd->simdata->smx_file, fullpath));
294 }
295
296 /**
297  * \ingroup msg_file
298  * \brief Copy a file to another location on a remote host.
299  * \param file : the file to move
300  * \param host : the remote host where the file has to be copied
301  * \param fullpath : the complete path destination on the remote host
302  * \return If successful, the function returns MSG_OK. Otherwise, it returns MSG_TASK_CANCELED.
303  */
304 msg_error_t MSG_file_rcopy (msg_file_t file, msg_host_t host, const char* fullpath)
305 {
306   /* Find the host where the file is physically located and read it */
307   msg_storage_t storage_src = simgrid::s4u::Storage::byName(file->storageId);
308   msg_host_t attached_host  = MSG_host_by_name(storage_src->host());
309   MSG_file_seek(file, 0, SEEK_SET);
310   sg_size_t read_size = simcall_file_read(file->simdata->smx_file, file->size, attached_host);
311
312   /* Find the real host destination where the file will be physically stored */
313   xbt_dict_cursor_t cursor   = nullptr;
314   msg_storage_t storage_dest = nullptr;
315   msg_host_t host_dest;
316   size_t longest_prefix_length = 0;
317
318   xbt_dict_t storage_list = host->mountedStoragesAsDict();
319   char *mount_name;
320   char *storage_name;
321   xbt_dict_foreach(storage_list,cursor,mount_name,storage_name){
322     char* file_mount_name = static_cast<char*>(xbt_malloc(strlen(mount_name) + 1));
323     strncpy(file_mount_name, fullpath, strlen(mount_name) + 1);
324     file_mount_name[strlen(mount_name)] = '\0';
325
326     if (not strcmp(file_mount_name, mount_name) && strlen(mount_name) > longest_prefix_length) {
327       /* The current mount name is found in the full path and is bigger than the previous*/
328       longest_prefix_length = strlen(mount_name);
329       storage_dest          = simgrid::s4u::Storage::byName(storage_name);
330     }
331     xbt_free(file_mount_name);
332   }
333   xbt_dict_free(&storage_list);
334
335   if(longest_prefix_length>0){
336     /* Mount point found, retrieve the host the storage is attached to */
337     host_dest = MSG_host_by_name(storage_dest->host());
338   }else{
339     XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, host->cname());
340     return MSG_TASK_CANCELED;
341   }
342
343   XBT_DEBUG("Initiate data transfer of %llu bytes between %s and %s.", read_size, storage_src->host(),
344             storage_dest->host());
345   msg_host_t m_host_list[] = {attached_host, host_dest};
346   double flops_amount[]    = {0, 0};
347   double bytes_amount[]    = {0, static_cast<double>(read_size), 0, 0};
348
349   msg_task_t task =
350       MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount, nullptr);
351   msg_error_t transfer = MSG_parallel_task_execute(task);
352   MSG_task_destroy(task);
353
354   if(transfer != MSG_OK){
355     if (transfer == MSG_HOST_FAILURE)
356       XBT_WARN("Transfer error, %s remote host just turned off!", storage_dest->host());
357     if (transfer == MSG_TASK_CANCELED)
358       XBT_WARN("Transfer error, task has been canceled!");
359
360     return transfer;
361   }
362
363   /* Create file on remote host, write it and close it */
364   smx_file_t smx_file = simcall_file_open(fullpath, host_dest);
365   simcall_file_write(smx_file, read_size, host_dest);
366   simcall_file_close(smx_file, host_dest);
367   return MSG_OK;
368 }
369
370 /**
371  * \ingroup msg_file
372  * \brief Move a file to another location on a remote host.
373  * \param file : the file to move
374  * \param host : the remote host where the file has to be moved
375  * \param fullpath : the complete path destination on the remote host
376  * \return If successful, the function returns MSG_OK. Otherwise, it returns MSG_TASK_CANCELED.
377  */
378 msg_error_t MSG_file_rmove (msg_file_t file, msg_host_t host, const char* fullpath)
379 {
380   msg_error_t res = MSG_file_rcopy(file, host, fullpath);
381   MSG_file_unlink(file);
382   return res;
383 }
384
385 /**
386  * \brief Destroys a file (internal call only)
387  */
388 void __MSG_file_destroy(msg_file_t file)
389 {
390   xbt_free(file->fullpath);
391   xbt_free(file->simdata);
392   xbt_free(file);
393 }
394
395 /********************************* Storage **************************************/
396 /** @addtogroup msg_storage_management
397  * (#msg_storage_t) and the functions for managing it.
398  */
399
400 /** \ingroup msg_storage_management
401  *
402  * \brief Returns the name of the #msg_storage_t.
403  *
404  * This functions checks whether a storage is a valid pointer or not and return its name.
405  */
406 const char *MSG_storage_get_name(msg_storage_t storage) {
407   xbt_assert((storage != nullptr), "Invalid parameters");
408   return storage->name();
409 }
410
411 /** \ingroup msg_storage_management
412  * \brief Returns the free space size of a storage element
413  * \param storage a storage
414  * \return the free space size of the storage element (as a #sg_size_t)
415  */
416 sg_size_t MSG_storage_get_free_size(msg_storage_t storage){
417   return storage->sizeFree();
418 }
419
420 /** \ingroup msg_storage_management
421  * \brief Returns the used space size of a storage element
422  * \param storage a storage
423  * \return the used space size of the storage element (as a #sg_size_t)
424  */
425 sg_size_t MSG_storage_get_used_size(msg_storage_t storage){
426   return storage->sizeUsed();
427 }
428
429 /** \ingroup msg_storage_management
430  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this storage
431  * \param storage a storage
432  * \return a dict containing the properties
433  */
434 xbt_dict_t MSG_storage_get_properties(msg_storage_t storage)
435 {
436   xbt_assert((storage != nullptr), "Invalid parameters (storage is nullptr)");
437   return storage->properties();
438 }
439
440 /** \ingroup msg_storage_management
441  * \brief Change the value of a given storage property
442  *
443  * \param storage a storage
444  * \param name a property name
445  * \param value what to change the property to
446  */
447 void MSG_storage_set_property_value(msg_storage_t storage, const char* name, char* value)
448 {
449   storage->setProperty(name, value);
450 }
451
452 /** \ingroup m_storage_management
453  * \brief Returns the value of a given storage property
454  *
455  * \param storage a storage
456  * \param name a property name
457  * \return value of a property (or nullptr if property not set)
458  */
459 const char *MSG_storage_get_property_value(msg_storage_t storage, const char *name)
460 {
461   return storage->property(name);
462 }
463
464 /** \ingroup msg_storage_management
465  * \brief Finds a msg_storage_t using its name.
466  * \param name the name of a storage
467  * \return the corresponding storage
468  */
469 msg_storage_t MSG_storage_get_by_name(const char *name)
470 {
471   return simgrid::s4u::Storage::byName(name);
472 }
473
474 /** \ingroup msg_storage_management
475  * \brief Returns a dynar containing all the storage elements declared at a given point of time
476  */
477 xbt_dynar_t MSG_storages_as_dynar() {
478   xbt_dynar_t res = xbt_dynar_new(sizeof(msg_storage_t),nullptr);
479   for (auto s : *simgrid::s4u::allStorages()) {
480     xbt_dynar_push(res, &(s.second));
481   }
482   return res;
483 }
484
485 /** \ingroup msg_storage_management
486  *
487  * \brief Set the user data of a #msg_storage_t.
488  * This functions attach \a data to \a storage if possible.
489  */
490 msg_error_t MSG_storage_set_data(msg_storage_t storage, void *data)
491 {
492   storage->setUserdata(data);
493   return MSG_OK;
494 }
495
496 /** \ingroup m_host_management
497  *
498  * \brief Returns the user data of a #msg_storage_t.
499  *
500  * This functions checks whether \a storage is a valid pointer and returns its associate user data if possible.
501  */
502 void *MSG_storage_get_data(msg_storage_t storage)
503 {
504   xbt_assert((storage != nullptr), "Invalid parameters");
505   return storage->userdata();
506 }
507
508 /** \ingroup msg_storage_management
509  *
510  * \brief Returns the content (file list) of a #msg_storage_t.
511  * \param storage a storage
512  * \return The content of this storage element as a dict (full path file => size)
513  */
514 xbt_dict_t MSG_storage_get_content(msg_storage_t storage)
515 {
516   std::map<std::string, sg_size_t*>* content = storage->content();
517   xbt_dict_t content_dict = xbt_dict_new_homogeneous(nullptr);
518
519   for (auto entry : *content) {
520     xbt_dict_set(content_dict, entry.first.c_str(), entry.second, nullptr);
521   }
522   return content_dict;
523 }
524
525 /** \ingroup msg_storage_management
526  *
527  * \brief Returns the size of a #msg_storage_t.
528  * \param storage a storage
529  * \return The size of the storage
530  */
531 sg_size_t MSG_storage_get_size(msg_storage_t storage)
532 {
533   return storage->size();
534 }
535
536 /** \ingroup msg_storage_management
537  *
538  * \brief Returns the host name the storage is attached to
539  *
540  * This functions checks whether a storage is a valid pointer or not and return its name.
541  */
542 const char *MSG_storage_get_host(msg_storage_t storage) {
543   xbt_assert((storage != nullptr), "Invalid parameters");
544   return storage->host();
545 }
546
547 SG_END_DECL()