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/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"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_io, msg, "Logging specific to MSG (io)");
17 /** @addtogroup msg_file
18 * (#msg_file_t) and the functions for managing it.
24 * \brief Read a file (local or remote)
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
30 sg_size_t MSG_file_read(msg_file_t fd, sg_size_t size)
34 if (fd->size() == 0) /* Nothing to read, return */
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);
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};
49 msg_task_t task = MSG_parallel_task_create("file transfer for read", 2, m_host_list, flops_amount, bytes_amount,
51 msg_error_t transfer = MSG_parallel_task_execute(task);
52 MSG_task_destroy(task);
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!");
67 * \brief Write into a file (local or remote)
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
73 sg_size_t MSG_file_write(msg_file_t fd, sg_size_t size)
75 if (size == 0) /* Nothing to write, return */
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();
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};
89 msg_task_t task = MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount,
91 msg_error_t transfer = MSG_parallel_task_execute(task);
92 MSG_task_destroy(task);
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!");
103 /* Write file on local or remote host */
104 sg_size_t write_size = fd->write(size);
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.
117 msg_error_t MSG_file_rcopy (msg_file_t file, msg_host_t host, const char* fullpath)
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());
125 /* Find the host that owns the storage where the file has to be copied */
126 msg_storage_t storage_dest = nullptr;
128 size_t longest_prefix_length = 0;
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;
139 if (storage_dest != nullptr) {
140 /* Mount point found, retrieve the host the storage is attached to */
141 dst_host = storage_dest->getHost();
143 XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, host->getCname());
144 return MSG_TASK_CANCELED;
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};
153 simgrid::s4u::this_actor::parallel_execute(2, m_host_list, flops_amount, bytes_amount);
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);
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.
170 msg_error_t MSG_file_rmove (msg_file_t file, msg_host_t host, const char* fullpath)
172 msg_error_t res = MSG_file_rcopy(file, host, fullpath);
177 /********************************* Storage **************************************/
178 /** @addtogroup msg_storage_management
179 * (#msg_storage_t) and the functions for managing it.
182 /** \ingroup msg_storage_management
184 * \brief Returns the name of the #msg_storage_t.
186 * This functions checks whether a storage is a valid pointer or not and return its name.
188 const char* MSG_storage_get_name(msg_storage_t storage)
190 xbt_assert((storage != nullptr), "Invalid parameters");
191 return storage->getCname();
194 const char* MSG_storage_get_host(msg_storage_t storage)
196 xbt_assert((storage != nullptr), "Invalid parameters");
197 return storage->getHost()->getCname();
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
205 xbt_dict_t MSG_storage_get_properties(msg_storage_t storage)
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)
212 for (auto const& elm : *props) {
213 xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
218 /** \ingroup msg_storage_management
219 * \brief Change the value of a given storage property
221 * \param storage a storage
222 * \param name a property name
223 * \param value what to change the property to
225 void MSG_storage_set_property_value(msg_storage_t storage, const char* name, char* value)
227 storage->setProperty(name, value);
230 /** \ingroup m_storage_management
231 * \brief Returns the value of a given storage property
233 * \param storage a storage
234 * \param name a property name
235 * \return value of a property (or nullptr if property not set)
237 const char *MSG_storage_get_property_value(msg_storage_t storage, const char *name)
239 return storage->getProperty(name);
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
247 msg_storage_t MSG_storage_get_by_name(const char *name)
249 return simgrid::s4u::Storage::byName(name);
252 /** \ingroup msg_storage_management
253 * \brief Returns a dynar containing all the storage elements declared at a given point of time
255 xbt_dynar_t MSG_storages_as_dynar()
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));
265 void* MSG_storage_get_data(msg_storage_t storage)
267 xbt_assert((storage != nullptr), "Invalid parameters");
268 return storage->getUserdata();
271 msg_error_t MSG_storage_set_data(msg_storage_t storage, void *data)
273 storage->setUserdata(data);
277 sg_size_t MSG_storage_read(msg_storage_t storage, sg_size_t size)
279 return storage->read(size);
282 sg_size_t MSG_storage_write(msg_storage_t storage, sg_size_t size)
284 return storage->write(size);