Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into clean_events
[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/File.hpp"
8 #include "simgrid/s4u/Host.hpp"
9 #include "simgrid/s4u/Storage.hpp"
10 #include "src/msg/msg_private.h"
11 #include <numeric>
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_io, msg, "Logging specific to MSG (io)");
14
15 SG_BEGIN_DECL()
16
17 /** @addtogroup msg_file
18  * (#msg_file_t) and the functions for managing it.
19  *
20  *  \see #msg_file_t
21  */
22
23 static int MSG_host_get_file_descriptor_id(msg_host_t host)
24 {
25   simgrid::MsgHostExt* priv = host->extension<simgrid::MsgHostExt>();
26   if (priv->file_descriptor_table == nullptr) {
27     priv->file_descriptor_table = new std::vector<int>(sg_storage_max_file_descriptors);
28     std::iota(priv->file_descriptor_table->rbegin(), priv->file_descriptor_table->rend(), 0); // Fill with ..., 1, 0.
29   }
30   xbt_assert(not priv->file_descriptor_table->empty(), "Too much files are opened! Some have to be closed.");
31   int desc = priv->file_descriptor_table->back();
32   priv->file_descriptor_table->pop_back();
33   return desc;
34 }
35
36 static void MSG_host_release_file_descriptor_id(msg_host_t host, int id)
37 {
38   host->extension<simgrid::MsgHostExt>()->file_descriptor_table->push_back(id);
39 }
40
41 /** \ingroup msg_file
42  *
43  * \brief Set the user data of a #msg_file_t.
44  *
45  * This functions attach \a data to \a file.
46  */
47 msg_error_t MSG_file_set_data(msg_file_t fd, void *data)
48 {
49   fd->setUserdata(data);
50   return MSG_OK;
51 }
52
53 /** \ingroup msg_file
54  *
55  * \brief Return the user data of a #msg_file_t.
56  *
57  * This functions checks whether \a file is a valid pointer and return the user data associated to \a file if possible.
58  */
59 void *MSG_file_get_data(msg_file_t fd)
60 {
61   return fd->userdata();
62 }
63
64 /** \ingroup msg_file
65  * \brief Display information related to a file descriptor
66  *
67  * \param fd is a the file descriptor
68  */
69 void MSG_file_dump (msg_file_t fd){
70   XBT_INFO("File Descriptor information:\n"
71            "\t\tFull path: '%s'\n"
72            "\t\tSize: %llu\n"
73            "\t\tMount point: '%s'\n"
74            "\t\tStorage Id: '%s'\n"
75            "\t\tStorage Type: '%s'\n"
76            "\t\tFile Descriptor Id: %d",
77            fd->path(), fd->size(), fd->mount_point, fd->storageId, fd->storage_type, fd->desc_id);
78 }
79
80 /** \ingroup msg_file
81  * \brief Read a file (local or remote)
82  *
83  * \param size of the file to read
84  * \param fd is a the file descriptor
85  * \return the number of bytes successfully read or -1 if an error occurred
86  */
87 sg_size_t MSG_file_read(msg_file_t fd, sg_size_t size)
88 {
89   sg_size_t read_size;
90
91   if (fd->size() == 0) /* Nothing to read, return */
92     return 0;
93
94   /* Find the host where the file is physically located and read it */
95   msg_storage_t storage_src           = simgrid::s4u::Storage::byName(fd->storageId);
96   msg_host_t attached_host            = MSG_host_by_name(storage_src->host());
97   read_size                           = fd->read(size); // TODO re-add attached_host;
98
99   if (strcmp(attached_host->cname(), MSG_host_self()->cname())) {
100     /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
101     XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", attached_host->cname(), read_size);
102     msg_host_t m_host_list[] = {MSG_host_self(), attached_host};
103     double flops_amount[]    = {0, 0};
104     double bytes_amount[]    = {0, 0, static_cast<double>(read_size), 0};
105
106     msg_task_t task = MSG_parallel_task_create("file transfer for read", 2, m_host_list, flops_amount, bytes_amount,
107                       nullptr);
108     msg_error_t transfer = MSG_parallel_task_execute(task);
109     MSG_task_destroy(task);
110
111     if(transfer != MSG_OK){
112       if (transfer == MSG_HOST_FAILURE)
113         XBT_WARN("Transfer error, %s remote host just turned off!", attached_host->cname());
114       if (transfer == MSG_TASK_CANCELED)
115         XBT_WARN("Transfer error, task has been canceled!");
116
117       return -1;
118     }
119   }
120   return read_size;
121 }
122
123 /** \ingroup msg_file
124  * \brief Write into a file (local or remote)
125  *
126  * \param size of the file to write
127  * \param fd is a the file descriptor
128  * \return the number of bytes successfully write or -1 if an error occurred
129  */
130 sg_size_t MSG_file_write(msg_file_t fd, sg_size_t size)
131 {
132   if (size == 0) /* Nothing to write, return */
133     return 0;
134
135   /* Find the host where the file is physically located (remote or local)*/
136   msg_storage_t storage_src = simgrid::s4u::Storage::byName(fd->storageId);
137   msg_host_t attached_host  = MSG_host_by_name(storage_src->host());
138
139   if (strcmp(attached_host->cname(), MSG_host_self()->cname())) {
140     /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
141     XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", attached_host->cname(), size);
142     msg_host_t m_host_list[] = {MSG_host_self(), attached_host};
143     double flops_amount[]    = {0, 0};
144     double bytes_amount[]    = {0, static_cast<double>(size), 0, 0};
145
146     msg_task_t task = MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount,
147                                                nullptr);
148     msg_error_t transfer = MSG_parallel_task_execute(task);
149     MSG_task_destroy(task);
150
151     if(transfer != MSG_OK){
152       if (transfer == MSG_HOST_FAILURE)
153         XBT_WARN("Transfer error, %s remote host just turned off!", attached_host->cname());
154       if (transfer == MSG_TASK_CANCELED)
155         XBT_WARN("Transfer error, task has been canceled!");
156
157       return -1;
158     }
159   }
160   /* Write file on local or remote host */
161   // sg_size_t offset     = fd->tell();
162   sg_size_t write_size = fd->write(size); // TODO readd attached_host;
163
164   return write_size;
165 }
166
167 /** \ingroup msg_file
168  * \brief Opens the file whose name is the string pointed to by path
169  *
170  * \param fullpath is the file location on the storage
171  * \param data user data to attach to the file
172  *
173  * \return An #msg_file_t associated to the file
174  */
175 msg_file_t MSG_file_open(const char* fullpath, void* data)
176 {
177   msg_file_t fd         = new simgrid::s4u::File(fullpath, MSG_host_self());
178   fd->desc_id           = MSG_host_get_file_descriptor_id(MSG_host_self());
179   return fd;
180 }
181
182 /** \ingroup msg_file
183  * \brief Close the file
184  *
185  * \param fd is the file to close
186  * \return 0 on success or 1 on error
187  */
188 int MSG_file_close(msg_file_t fd)
189 {
190   delete fd;
191
192   return MSG_OK;
193 }
194
195 /** \ingroup msg_file
196  * \brief Unlink the file pointed by fd
197  *
198  * \param fd is the file descriptor (#msg_file_t)
199  * \return 0 on success or 1 on error
200  */
201 msg_error_t MSG_file_unlink(msg_file_t fd)
202 {
203   /* Find the host where the file is physically located (remote or local)*/
204   // msg_storage_t storage_src           = simgrid::s4u::Storage::byName(fd->storageId);
205   // msg_host_t attached_host            = MSG_host_by_name(storage_src->host());
206   fd->unlink(); // simcall_file_unlink(fd->simdata->smx_file, attached_host);
207   return MSG_OK;
208 }
209
210 /** \ingroup msg_file
211  * \brief Return the size of a file
212  *
213  * \param fd is the file descriptor (#msg_file_t)
214  * \return the size of the file (as a #sg_size_t)
215  */
216 sg_size_t MSG_file_get_size(msg_file_t fd){
217   return fd->size();
218 }
219
220 /**
221  * \ingroup msg_file
222  * \brief Set the file position indicator in the msg_file_t by adding offset bytes
223  * to the position specified by origin (either SEEK_SET, SEEK_CUR, or SEEK_END).
224  *
225  * \param fd : file object that identifies the stream
226  * \param offset : number of bytes to offset from origin
227  * \param origin : Position used as reference for the offset. It is specified by one of the following constants defined
228  *                 in \<stdio.h\> exclusively to be used as arguments for this function (SEEK_SET = beginning of file,
229  *                 SEEK_CUR = current position of the file pointer, SEEK_END = end of file)
230  * \return If successful, the function returns MSG_OK (=0). Otherwise, it returns MSG_TASK_CANCELED (=8).
231  */
232 msg_error_t MSG_file_seek(msg_file_t fd, sg_offset_t offset, int origin)
233 {
234   fd->seek(offset); // TODO re-add origin
235   return MSG_OK;
236 }
237
238 /**
239  * \ingroup msg_file
240  * \brief Returns the current value of the position indicator of the file
241  *
242  * \param fd : file object that identifies the stream
243  * \return On success, the current value of the position indicator is returned.
244  *
245  */
246 sg_size_t MSG_file_tell(msg_file_t fd)
247 {
248   return fd->tell();
249 }
250
251 const char *MSG_file_get_name(msg_file_t fd) {
252   xbt_assert((fd != nullptr), "Invalid parameters");
253   return fd->path();
254 }
255
256 /**
257  * \ingroup msg_file
258  * \brief Move a file to another location on the *same mount point*.
259  *
260  */
261 msg_error_t MSG_file_move (msg_file_t fd, const char* fullpath)
262 {
263   fd->move(fullpath);
264   return MSG_OK;
265 }
266
267 /**
268  * \ingroup msg_file
269  * \brief Copy a file to another location on a remote host.
270  * \param file : the file to move
271  * \param host : the remote host where the file has to be copied
272  * \param fullpath : the complete path destination on the remote host
273  * \return If successful, the function returns MSG_OK. Otherwise, it returns MSG_TASK_CANCELED.
274  */
275 msg_error_t MSG_file_rcopy (msg_file_t file, msg_host_t host, const char* fullpath)
276 {
277   /* Find the host where the file is physically located and read it */
278   msg_storage_t storage_src = simgrid::s4u::Storage::byName(file->storageId);
279   msg_host_t attached_host  = MSG_host_by_name(storage_src->host());
280   MSG_file_seek(file, 0, SEEK_SET);
281   sg_size_t read_size = file->read(file->size());
282
283   /* Find the real host destination where the file will be physically stored */
284   xbt_dict_cursor_t cursor   = nullptr;
285   msg_storage_t storage_dest = nullptr;
286   msg_host_t host_dest;
287   size_t longest_prefix_length = 0;
288
289   xbt_dict_t storage_list = host->mountedStoragesAsDict();
290   char *mount_name;
291   char *storage_name;
292   xbt_dict_foreach(storage_list,cursor,mount_name,storage_name){
293     char* file_mount_name = static_cast<char*>(xbt_malloc(strlen(mount_name) + 1));
294     strncpy(file_mount_name, fullpath, strlen(mount_name) + 1);
295     file_mount_name[strlen(mount_name)] = '\0';
296
297     if (not strcmp(file_mount_name, mount_name) && strlen(mount_name) > longest_prefix_length) {
298       /* The current mount name is found in the full path and is bigger than the previous*/
299       longest_prefix_length = strlen(mount_name);
300       storage_dest          = simgrid::s4u::Storage::byName(storage_name);
301     }
302     xbt_free(file_mount_name);
303   }
304   xbt_dict_free(&storage_list);
305
306   if(longest_prefix_length>0){
307     /* Mount point found, retrieve the host the storage is attached to */
308     host_dest = MSG_host_by_name(storage_dest->host());
309   }else{
310     XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, host->cname());
311     return MSG_TASK_CANCELED;
312   }
313
314   XBT_DEBUG("Initiate data transfer of %llu bytes between %s and %s.", read_size, attached_host->cname(),
315             storage_dest->host());
316   msg_host_t m_host_list[] = {attached_host, host_dest};
317   double flops_amount[]    = {0, 0};
318   double bytes_amount[]    = {0, static_cast<double>(read_size), 0, 0};
319
320   msg_task_t task =
321       MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount, nullptr);
322   msg_error_t transfer = MSG_parallel_task_execute(task);
323   MSG_task_destroy(task);
324
325   if(transfer != MSG_OK){
326     if (transfer == MSG_HOST_FAILURE)
327       XBT_WARN("Transfer error, %s remote host just turned off!", storage_dest->host());
328     if (transfer == MSG_TASK_CANCELED)
329       XBT_WARN("Transfer error, task has been canceled!");
330
331     return transfer;
332   }
333
334   /* Create file on remote host, write it and close it */
335   smx_file_t smx_file = simcall_file_open(fullpath, host_dest);
336   simcall_file_write(smx_file, read_size, host_dest);
337   simcall_file_close(smx_file, host_dest);
338   return MSG_OK;
339 }
340
341 /**
342  * \ingroup msg_file
343  * \brief Move a file to another location on a remote host.
344  * \param file : the file to move
345  * \param host : the remote host where the file has to be moved
346  * \param fullpath : the complete path destination on the remote host
347  * \return If successful, the function returns MSG_OK. Otherwise, it returns MSG_TASK_CANCELED.
348  */
349 msg_error_t MSG_file_rmove (msg_file_t file, msg_host_t host, const char* fullpath)
350 {
351   msg_error_t res = MSG_file_rcopy(file, host, fullpath);
352   MSG_file_unlink(file);
353   return res;
354 }
355
356 /********************************* Storage **************************************/
357 /** @addtogroup msg_storage_management
358  * (#msg_storage_t) and the functions for managing it.
359  */
360
361 /** \ingroup msg_storage_management
362  *
363  * \brief Returns the name of the #msg_storage_t.
364  *
365  * This functions checks whether a storage is a valid pointer or not and return its name.
366  */
367 const char *MSG_storage_get_name(msg_storage_t storage) {
368   xbt_assert((storage != nullptr), "Invalid parameters");
369   return storage->name();
370 }
371
372 /** \ingroup msg_storage_management
373  * \brief Returns the free space size of a storage element
374  * \param storage a storage
375  * \return the free space size of the storage element (as a #sg_size_t)
376  */
377 sg_size_t MSG_storage_get_free_size(msg_storage_t storage){
378   return storage->sizeFree();
379 }
380
381 /** \ingroup msg_storage_management
382  * \brief Returns the used space size of a storage element
383  * \param storage a storage
384  * \return the used space size of the storage element (as a #sg_size_t)
385  */
386 sg_size_t MSG_storage_get_used_size(msg_storage_t storage){
387   return storage->sizeUsed();
388 }
389
390 /** \ingroup msg_storage_management
391  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this storage
392  * \param storage a storage
393  * \return a dict containing the properties
394  */
395 xbt_dict_t MSG_storage_get_properties(msg_storage_t storage)
396 {
397   xbt_assert((storage != nullptr), "Invalid parameters (storage is nullptr)");
398   return storage->properties();
399 }
400
401 /** \ingroup msg_storage_management
402  * \brief Change the value of a given storage property
403  *
404  * \param storage a storage
405  * \param name a property name
406  * \param value what to change the property to
407  */
408 void MSG_storage_set_property_value(msg_storage_t storage, const char* name, char* value)
409 {
410   storage->setProperty(name, value);
411 }
412
413 /** \ingroup m_storage_management
414  * \brief Returns the value of a given storage property
415  *
416  * \param storage a storage
417  * \param name a property name
418  * \return value of a property (or nullptr if property not set)
419  */
420 const char *MSG_storage_get_property_value(msg_storage_t storage, const char *name)
421 {
422   return storage->property(name);
423 }
424
425 /** \ingroup msg_storage_management
426  * \brief Finds a msg_storage_t using its name.
427  * \param name the name of a storage
428  * \return the corresponding storage
429  */
430 msg_storage_t MSG_storage_get_by_name(const char *name)
431 {
432   return simgrid::s4u::Storage::byName(name);
433 }
434
435 /** \ingroup msg_storage_management
436  * \brief Returns a dynar containing all the storage elements declared at a given point of time
437  */
438 xbt_dynar_t MSG_storages_as_dynar() {
439   xbt_dynar_t res = xbt_dynar_new(sizeof(msg_storage_t),nullptr);
440   for (auto s : *simgrid::s4u::allStorages()) {
441     xbt_dynar_push(res, &(s.second));
442   }
443   return res;
444 }
445
446 /** \ingroup msg_storage_management
447  *
448  * \brief Set the user data of a #msg_storage_t.
449  * This functions attach \a data to \a storage if possible.
450  */
451 msg_error_t MSG_storage_set_data(msg_storage_t storage, void *data)
452 {
453   storage->setUserdata(data);
454   return MSG_OK;
455 }
456
457 /** \ingroup m_host_management
458  *
459  * \brief Returns the user data of a #msg_storage_t.
460  *
461  * This functions checks whether \a storage is a valid pointer and returns its associate user data if possible.
462  */
463 void *MSG_storage_get_data(msg_storage_t storage)
464 {
465   xbt_assert((storage != nullptr), "Invalid parameters");
466   return storage->userdata();
467 }
468
469 /** \ingroup msg_storage_management
470  *
471  * \brief Returns the content (file list) of a #msg_storage_t.
472  * \param storage a storage
473  * \return The content of this storage element as a dict (full path file => size)
474  */
475 xbt_dict_t MSG_storage_get_content(msg_storage_t storage)
476 {
477   std::map<std::string, sg_size_t*>* content = storage->content();
478   xbt_dict_t content_dict = xbt_dict_new_homogeneous(nullptr);
479
480   for (auto entry : *content) {
481     xbt_dict_set(content_dict, entry.first.c_str(), entry.second, nullptr);
482   }
483   return content_dict;
484 }
485
486 /** \ingroup msg_storage_management
487  *
488  * \brief Returns the size of a #msg_storage_t.
489  * \param storage a storage
490  * \return The size of the storage
491  */
492 sg_size_t MSG_storage_get_size(msg_storage_t storage)
493 {
494   return storage->size();
495 }
496
497 /** \ingroup msg_storage_management
498  *
499  * \brief Returns the host name the storage is attached to
500  *
501  * This functions checks whether a storage is a valid pointer or not and return its name.
502  */
503 const char *MSG_storage_get_host(msg_storage_t storage) {
504   xbt_assert((storage != nullptr), "Invalid parameters");
505   return storage->host();
506 }
507
508 SG_END_DECL()