1 /* Copyright (c) 2004-2017. The SimGrid Team. All rights reserved. */
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. */
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"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_io, msg, "Logging specific to MSG (io)");
16 /** @addtogroup msg_file
17 * (#msg_file_t) and the functions for managing it.
22 static int MSG_host_get_file_descriptor_id(msg_host_t host)
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.
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();
35 static void MSG_host_release_file_descriptor_id(msg_host_t host, int id)
37 host->extension<simgrid::MsgHostExt>()->file_descriptor_table->push_back(id);
41 * \brief Read a file (local or remote)
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
47 sg_size_t MSG_file_read(msg_file_t fd, sg_size_t size)
51 if (fd->size() == 0) /* Nothing to read, return */
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);
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};
66 msg_task_t task = MSG_parallel_task_create("file transfer for read", 2, m_host_list, flops_amount, bytes_amount,
68 msg_error_t transfer = MSG_parallel_task_execute(task);
69 MSG_task_destroy(task);
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!");
84 * \brief Write into a file (local or remote)
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
90 sg_size_t MSG_file_write(msg_file_t fd, sg_size_t size)
92 if (size == 0) /* Nothing to write, return */
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();
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};
106 msg_task_t task = MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount,
108 msg_error_t transfer = MSG_parallel_task_execute(task);
109 MSG_task_destroy(task);
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!");
120 /* Write file on local or remote host */
121 sg_size_t write_size = fd->write(size);
126 /** \ingroup msg_file
127 * \brief Opens the file whose name is the string pointed to by path
129 * \param fullpath is the file location on the storage
130 * \param data user data to attach to the file
132 * \return An #msg_file_t associated to the file
134 msg_file_t MSG_file_open(const char* fullpath, void* data)
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);
142 /** \ingroup msg_file
143 * \brief Close the file
145 * \param fd is the file to close
146 * \return 0 on success or 1 on error
148 int MSG_file_close(msg_file_t fd)
150 MSG_host_release_file_descriptor_id(MSG_host_self(), fd->desc_id);
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.
164 msg_error_t MSG_file_rcopy (msg_file_t file, msg_host_t host, const char* fullpath)
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());
172 /* Find the host that owns the storage where the file has to be copied */
173 msg_storage_t storage_dest = nullptr;
175 size_t longest_prefix_length = 0;
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;
186 if (storage_dest != nullptr) {
187 /* Mount point found, retrieve the host the storage is attached to */
188 dst_host = storage_dest->getHost();
190 XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, host->getCname());
191 return MSG_TASK_CANCELED;
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};
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);
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!");
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);
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.
229 msg_error_t MSG_file_rmove (msg_file_t file, msg_host_t host, const char* fullpath)
231 msg_error_t res = MSG_file_rcopy(file, host, fullpath);
232 MSG_file_unlink(file);
236 /********************************* Storage **************************************/
237 /** @addtogroup msg_storage_management
238 * (#msg_storage_t) and the functions for managing it.
241 /** \ingroup msg_storage_management
243 * \brief Returns the name of the #msg_storage_t.
245 * This functions checks whether a storage is a valid pointer or not and return its name.
247 const char* MSG_storage_get_name(msg_storage_t storage)
249 xbt_assert((storage != nullptr), "Invalid parameters");
250 return storage->getCname();
253 const char* MSG_storage_get_host(msg_storage_t storage)
255 xbt_assert((storage != nullptr), "Invalid parameters");
256 return storage->getHost()->getCname();
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
264 xbt_dict_t MSG_storage_get_properties(msg_storage_t storage)
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)
271 for (auto const& elm : *props) {
272 xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
277 /** \ingroup msg_storage_management
278 * \brief Change the value of a given storage property
280 * \param storage a storage
281 * \param name a property name
282 * \param value what to change the property to
284 void MSG_storage_set_property_value(msg_storage_t storage, const char* name, char* value)
286 storage->setProperty(name, value);
289 /** \ingroup m_storage_management
290 * \brief Returns the value of a given storage property
292 * \param storage a storage
293 * \param name a property name
294 * \return value of a property (or nullptr if property not set)
296 const char *MSG_storage_get_property_value(msg_storage_t storage, const char *name)
298 return storage->getProperty(name);
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
306 msg_storage_t MSG_storage_get_by_name(const char *name)
308 return simgrid::s4u::Storage::byName(name);
311 /** \ingroup msg_storage_management
312 * \brief Returns a dynar containing all the storage elements declared at a given point of time
314 xbt_dynar_t MSG_storages_as_dynar()
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));
324 void* MSG_storage_get_data(msg_storage_t storage)
326 xbt_assert((storage != nullptr), "Invalid parameters");
327 return storage->getUserdata();
330 msg_error_t MSG_storage_set_data(msg_storage_t storage, void *data)
332 storage->setUserdata(data);
336 sg_size_t MSG_storage_read(msg_storage_t storage, sg_size_t size)
338 return storage->read(size);
341 sg_size_t MSG_storage_write(msg_storage_t storage, sg_size_t size)
343 return storage->write(size);