Logo AND Algorithmique Numérique Distribuée

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