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.
23 * \brief Read a file (local or remote)
25 * \param size of the file to read
26 * \param fd is a the file descriptor
27 * \return the number of bytes successfully read or -1 if an error occurred
29 sg_size_t MSG_file_read(msg_file_t fd, sg_size_t size)
33 if (fd->size() == 0) /* Nothing to read, return */
36 /* Find the host where the file is physically located and read it */
37 msg_storage_t storage_src = fd->localStorage;
38 msg_host_t attached_host = storage_src->getHost();
39 read_size = fd->read(size);
41 if (strcmp(attached_host->getCname(), MSG_host_self()->getCname())) {
42 /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
43 XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", attached_host->getCname(), read_size);
44 msg_host_t m_host_list[] = {MSG_host_self(), attached_host};
45 double flops_amount[] = {0, 0};
46 double bytes_amount[] = {0, 0, static_cast<double>(read_size), 0};
48 msg_task_t task = MSG_parallel_task_create("file transfer for read", 2, m_host_list, flops_amount, bytes_amount,
50 msg_error_t transfer = MSG_parallel_task_execute(task);
51 MSG_task_destroy(task);
53 if(transfer != MSG_OK){
54 if (transfer == MSG_HOST_FAILURE)
55 XBT_WARN("Transfer error, %s remote host just turned off!", attached_host->getCname());
56 if (transfer == MSG_TASK_CANCELED)
57 XBT_WARN("Transfer error, task has been canceled!");
66 * \brief Write into a file (local or remote)
68 * \param size of the file to write
69 * \param fd is a the file descriptor
70 * \return the number of bytes successfully write or -1 if an error occurred
72 sg_size_t MSG_file_write(msg_file_t fd, sg_size_t size)
74 if (size == 0) /* Nothing to write, return */
77 /* Find the host where the file is physically located (remote or local)*/
78 msg_storage_t storage_src = fd->localStorage;
79 msg_host_t attached_host = storage_src->getHost();
81 if (strcmp(attached_host->getCname(), MSG_host_self()->getCname())) {
82 /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
83 XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", attached_host->getCname(), size);
84 msg_host_t m_host_list[] = {MSG_host_self(), attached_host};
85 double flops_amount[] = {0, 0};
86 double bytes_amount[] = {0, static_cast<double>(size), 0, 0};
88 msg_task_t task = MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount,
90 msg_error_t transfer = MSG_parallel_task_execute(task);
91 MSG_task_destroy(task);
93 if(transfer != MSG_OK){
94 if (transfer == MSG_HOST_FAILURE)
95 XBT_WARN("Transfer error, %s remote host just turned off!", attached_host->getCname());
96 if (transfer == MSG_TASK_CANCELED)
97 XBT_WARN("Transfer error, task has been canceled!");
102 /* Write file on local or remote host */
103 sg_size_t write_size = fd->write(size);
109 /********************************* Storage **************************************/
110 /** @addtogroup msg_storage_management
111 * (#msg_storage_t) and the functions for managing it.
114 /** \ingroup msg_storage_management
116 * \brief Returns the name of the #msg_storage_t.
118 * This functions checks whether a storage is a valid pointer or not and return its name.
120 const char* MSG_storage_get_name(msg_storage_t storage)
122 xbt_assert((storage != nullptr), "Invalid parameters");
123 return storage->getCname();
126 const char* MSG_storage_get_host(msg_storage_t storage)
128 xbt_assert((storage != nullptr), "Invalid parameters");
129 return storage->getHost()->getCname();
132 /** \ingroup msg_storage_management
133 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this storage
134 * \param storage a storage
135 * \return a dict containing the properties
137 xbt_dict_t MSG_storage_get_properties(msg_storage_t storage)
139 xbt_assert((storage != nullptr), "Invalid parameters (storage is nullptr)");
140 xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
141 std::map<std::string, std::string>* props = storage->getProperties();
142 if (props == nullptr)
144 for (auto const& elm : *props) {
145 xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
150 /** \ingroup msg_storage_management
151 * \brief Change the value of a given storage property
153 * \param storage a storage
154 * \param name a property name
155 * \param value what to change the property to
157 void MSG_storage_set_property_value(msg_storage_t storage, const char* name, char* value)
159 storage->setProperty(name, value);
162 /** \ingroup m_storage_management
163 * \brief Returns the value of a given storage property
165 * \param storage a storage
166 * \param name a property name
167 * \return value of a property (or nullptr if property not set)
169 const char *MSG_storage_get_property_value(msg_storage_t storage, const char *name)
171 return storage->getProperty(name);
174 /** \ingroup msg_storage_management
175 * \brief Finds a msg_storage_t using its name.
176 * \param name the name of a storage
177 * \return the corresponding storage
179 msg_storage_t MSG_storage_get_by_name(const char *name)
181 return simgrid::s4u::Storage::byName(name);
184 /** \ingroup msg_storage_management
185 * \brief Returns a dynar containing all the storage elements declared at a given point of time
187 xbt_dynar_t MSG_storages_as_dynar()
189 std::map<std::string, simgrid::s4u::Storage*>* storage_map = simgrid::s4u::allStorages();
190 xbt_dynar_t res = xbt_dynar_new(sizeof(msg_storage_t),nullptr);
191 for (auto const& s : *storage_map)
192 xbt_dynar_push(res, &(s.second));
197 void* MSG_storage_get_data(msg_storage_t storage)
199 xbt_assert((storage != nullptr), "Invalid parameters");
200 return storage->getUserdata();
203 msg_error_t MSG_storage_set_data(msg_storage_t storage, void *data)
205 storage->setUserdata(data);
209 sg_size_t MSG_storage_read(msg_storage_t storage, sg_size_t size)
211 return storage->read(size);
214 sg_size_t MSG_storage_write(msg_storage_t storage, sg_size_t size)
216 return storage->write(size);