Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2806561aadc51b455950998d53695e6817aa729c
[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/Actor.hpp"
7 #include "simgrid/s4u/Host.hpp"
8 #include "simgrid/s4u/Storage.hpp"
9 #include "src/msg/msg_private.hpp"
10 #include "src/plugins/file_system/FileSystem.hpp"
11 #include <numeric>
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_io, msg, "Logging specific to MSG (io)");
14
15 extern "C" {
16
17 /** @addtogroup msg_file
18  * (#msg_file_t) and the functions for managing it.
19  *
20  *  \see #msg_file_t
21  */
22
23 /** \ingroup msg_file
24  * \brief Read a file (local or remote)
25  *
26  * \param size of the file to read
27  * \param fd is a the file descriptor
28  * \return the number of bytes successfully read or -1 if an error occurred
29  */
30 sg_size_t MSG_file_read(msg_file_t fd, sg_size_t size)
31 {
32   sg_size_t read_size;
33
34   if (fd->size() == 0) /* Nothing to read, return */
35     return 0;
36
37   /* Find the host where the file is physically located and read it */
38   msg_storage_t storage_src           = fd->localStorage;
39   msg_host_t attached_host            = storage_src->getHost();
40   read_size                           = fd->read(size);
41
42   if (strcmp(attached_host->getCname(), MSG_host_self()->getCname())) {
43     /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
44     XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", attached_host->getCname(), read_size);
45     msg_host_t m_host_list[] = {MSG_host_self(), attached_host};
46     double flops_amount[]    = {0, 0};
47     double bytes_amount[]    = {0, 0, static_cast<double>(read_size), 0};
48
49     msg_task_t task = MSG_parallel_task_create("file transfer for read", 2, m_host_list, flops_amount, bytes_amount,
50                       nullptr);
51     msg_error_t transfer = MSG_parallel_task_execute(task);
52     MSG_task_destroy(task);
53
54     if(transfer != MSG_OK){
55       if (transfer == MSG_HOST_FAILURE)
56         XBT_WARN("Transfer error, %s remote host just turned off!", attached_host->getCname());
57       if (transfer == MSG_TASK_CANCELED)
58         XBT_WARN("Transfer error, task has been canceled!");
59
60       return -1;
61     }
62   }
63   return read_size;
64 }
65
66 /** \ingroup msg_file
67  * \brief Write into a file (local or remote)
68  *
69  * \param size of the file to write
70  * \param fd is a the file descriptor
71  * \return the number of bytes successfully write or -1 if an error occurred
72  */
73 sg_size_t MSG_file_write(msg_file_t fd, sg_size_t size)
74 {
75   if (size == 0) /* Nothing to write, return */
76     return 0;
77
78   /* Find the host where the file is physically located (remote or local)*/
79   msg_storage_t storage_src = fd->localStorage;
80   msg_host_t attached_host  = storage_src->getHost();
81
82   if (strcmp(attached_host->getCname(), MSG_host_self()->getCname())) {
83     /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
84     XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", attached_host->getCname(), size);
85     msg_host_t m_host_list[] = {MSG_host_self(), attached_host};
86     double flops_amount[]    = {0, 0};
87     double bytes_amount[]    = {0, static_cast<double>(size), 0, 0};
88
89     msg_task_t task = MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount,
90                                                nullptr);
91     msg_error_t transfer = MSG_parallel_task_execute(task);
92     MSG_task_destroy(task);
93
94     if(transfer != MSG_OK){
95       if (transfer == MSG_HOST_FAILURE)
96         XBT_WARN("Transfer error, %s remote host just turned off!", attached_host->getCname());
97       if (transfer == MSG_TASK_CANCELED)
98         XBT_WARN("Transfer error, task has been canceled!");
99
100       return -1;
101     }
102   }
103   /* Write file on local or remote host */
104   sg_size_t write_size = fd->write(size);
105
106   return write_size;
107 }
108
109 /**
110  * \ingroup msg_file
111  * \brief Copy a file to another location on a remote host.
112  * \param file : the file to move
113  * \param host : the remote host where the file has to be copied
114  * \param fullpath : the complete path destination on the remote host
115  * \return If successful, the function returns MSG_OK. Otherwise, it returns MSG_TASK_CANCELED.
116  */
117 msg_error_t MSG_file_rcopy (msg_file_t file, msg_host_t host, const char* fullpath)
118 {
119   /* Find the host where the file is physically located and read it */
120   msg_storage_t storage_src = file->localStorage;
121   msg_host_t src_host       = storage_src->getHost();
122   file->seek(0, SEEK_SET);
123   sg_size_t read_size = file->read(file->size());
124
125   /* Find the host that owns the storage where the file has to be copied */
126   msg_storage_t storage_dest = nullptr;
127   msg_host_t dst_host;
128   size_t longest_prefix_length = 0;
129
130   for (auto const& elm : host->getMountedStorages()) {
131     std::string mount_point = std::string(fullpath).substr(0, elm.first.size());
132     if (mount_point == elm.first && elm.first.length() > longest_prefix_length) {
133       /* The current mount name is found in the full path and is bigger than the previous*/
134       longest_prefix_length = elm.first.length();
135       storage_dest          = elm.second;
136     }
137   }
138
139   if (storage_dest != nullptr) {
140     /* Mount point found, retrieve the host the storage is attached to */
141     dst_host = storage_dest->getHost();
142   }else{
143     XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, host->getCname());
144     return MSG_TASK_CANCELED;
145   }
146
147   XBT_DEBUG("Initiate data transfer of %llu bytes between %s and %s.", read_size, src_host->getCname(),
148             storage_dest->getHost()->getCname());
149   msg_host_t m_host_list[] = {src_host, dst_host};
150   double* flops_amount     = new double[2]{0, 0};
151   double* bytes_amount     = new double[4]{0, static_cast<double>(read_size), 0, 0};
152
153   simgrid::s4u::this_actor::parallel_execute(2, m_host_list, flops_amount, bytes_amount);
154
155   /* Create file on remote host, write it and close it */
156   msg_file_t fd = new simgrid::s4u::File(fullpath, dst_host, nullptr);
157   fd->write(read_size);
158   delete fd;
159   return MSG_OK;
160 }
161
162 /**
163  * \ingroup msg_file
164  * \brief Move a file to another location on a remote host.
165  * \param file : the file to move
166  * \param host : the remote host where the file has to be moved
167  * \param fullpath : the complete path destination on the remote host
168  * \return If successful, the function returns MSG_OK. Otherwise, it returns MSG_TASK_CANCELED.
169  */
170 msg_error_t MSG_file_rmove (msg_file_t file, msg_host_t host, const char* fullpath)
171 {
172   msg_error_t res = MSG_file_rcopy(file, host, fullpath);
173   file->unlink();
174   return res;
175 }
176
177 /********************************* Storage **************************************/
178 /** @addtogroup msg_storage_management
179  * (#msg_storage_t) and the functions for managing it.
180  */
181
182 /** \ingroup msg_storage_management
183  *
184  * \brief Returns the name of the #msg_storage_t.
185  *
186  * This functions checks whether a storage is a valid pointer or not and return its name.
187  */
188 const char* MSG_storage_get_name(msg_storage_t storage)
189 {
190   xbt_assert((storage != nullptr), "Invalid parameters");
191   return storage->getCname();
192 }
193
194 const char* MSG_storage_get_host(msg_storage_t storage)
195 {
196   xbt_assert((storage != nullptr), "Invalid parameters");
197   return storage->getHost()->getCname();
198 }
199
200 /** \ingroup msg_storage_management
201  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this storage
202  * \param storage a storage
203  * \return a dict containing the properties
204  */
205 xbt_dict_t MSG_storage_get_properties(msg_storage_t storage)
206 {
207   xbt_assert((storage != nullptr), "Invalid parameters (storage is nullptr)");
208   xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
209   std::map<std::string, std::string>* props = storage->getProperties();
210   if (props == nullptr)
211     return nullptr;
212   for (auto const& elm : *props) {
213     xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
214   }
215   return as_dict;
216 }
217
218 /** \ingroup msg_storage_management
219  * \brief Change the value of a given storage property
220  *
221  * \param storage a storage
222  * \param name a property name
223  * \param value what to change the property to
224  */
225 void MSG_storage_set_property_value(msg_storage_t storage, const char* name, char* value)
226 {
227   storage->setProperty(name, value);
228 }
229
230 /** \ingroup m_storage_management
231  * \brief Returns the value of a given storage property
232  *
233  * \param storage a storage
234  * \param name a property name
235  * \return value of a property (or nullptr if property not set)
236  */
237 const char *MSG_storage_get_property_value(msg_storage_t storage, const char *name)
238 {
239   return storage->getProperty(name);
240 }
241
242 /** \ingroup msg_storage_management
243  * \brief Finds a msg_storage_t using its name.
244  * \param name the name of a storage
245  * \return the corresponding storage
246  */
247 msg_storage_t MSG_storage_get_by_name(const char *name)
248 {
249   return simgrid::s4u::Storage::byName(name);
250 }
251
252 /** \ingroup msg_storage_management
253  * \brief Returns a dynar containing all the storage elements declared at a given point of time
254  */
255 xbt_dynar_t MSG_storages_as_dynar()
256 {
257   std::map<std::string, simgrid::s4u::Storage*>* storage_map = simgrid::s4u::allStorages();
258   xbt_dynar_t res = xbt_dynar_new(sizeof(msg_storage_t),nullptr);
259   for (auto const& s : *storage_map)
260     xbt_dynar_push(res, &(s.second));
261   delete storage_map;
262   return res;
263 }
264
265 void* MSG_storage_get_data(msg_storage_t storage)
266 {
267   xbt_assert((storage != nullptr), "Invalid parameters");
268   return storage->getUserdata();
269 }
270
271 msg_error_t MSG_storage_set_data(msg_storage_t storage, void *data)
272 {
273   storage->setUserdata(data);
274   return MSG_OK;
275 }
276
277 sg_size_t MSG_storage_read(msg_storage_t storage, sg_size_t size)
278 {
279   return storage->read(size);
280 }
281
282 sg_size_t MSG_storage_write(msg_storage_t storage, sg_size_t size)
283 {
284   return storage->write(size);
285 }
286
287 }