Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remote copy and move in the plugin
[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 /** \ingroup msg_file
23  * \brief Read a file (local or remote)
24  *
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
28  */
29 sg_size_t MSG_file_read(msg_file_t fd, sg_size_t size)
30 {
31   sg_size_t read_size;
32
33   if (fd->size() == 0) /* Nothing to read, return */
34     return 0;
35
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);
40
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};
47
48     msg_task_t task = MSG_parallel_task_create("file transfer for read", 2, m_host_list, flops_amount, bytes_amount,
49                       nullptr);
50     msg_error_t transfer = MSG_parallel_task_execute(task);
51     MSG_task_destroy(task);
52
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!");
58
59       return -1;
60     }
61   }
62   return read_size;
63 }
64
65 /** \ingroup msg_file
66  * \brief Write into a file (local or remote)
67  *
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
71  */
72 sg_size_t MSG_file_write(msg_file_t fd, sg_size_t size)
73 {
74   if (size == 0) /* Nothing to write, return */
75     return 0;
76
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();
80
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};
87
88     msg_task_t task = MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount,
89                                                nullptr);
90     msg_error_t transfer = MSG_parallel_task_execute(task);
91     MSG_task_destroy(task);
92
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!");
98
99       return -1;
100     }
101   }
102   /* Write file on local or remote host */
103   sg_size_t write_size = fd->write(size);
104
105   return write_size;
106 }
107
108
109 /********************************* Storage **************************************/
110 /** @addtogroup msg_storage_management
111  * (#msg_storage_t) and the functions for managing it.
112  */
113
114 /** \ingroup msg_storage_management
115  *
116  * \brief Returns the name of the #msg_storage_t.
117  *
118  * This functions checks whether a storage is a valid pointer or not and return its name.
119  */
120 const char* MSG_storage_get_name(msg_storage_t storage)
121 {
122   xbt_assert((storage != nullptr), "Invalid parameters");
123   return storage->getCname();
124 }
125
126 const char* MSG_storage_get_host(msg_storage_t storage)
127 {
128   xbt_assert((storage != nullptr), "Invalid parameters");
129   return storage->getHost()->getCname();
130 }
131
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
136  */
137 xbt_dict_t MSG_storage_get_properties(msg_storage_t storage)
138 {
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)
143     return nullptr;
144   for (auto const& elm : *props) {
145     xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
146   }
147   return as_dict;
148 }
149
150 /** \ingroup msg_storage_management
151  * \brief Change the value of a given storage property
152  *
153  * \param storage a storage
154  * \param name a property name
155  * \param value what to change the property to
156  */
157 void MSG_storage_set_property_value(msg_storage_t storage, const char* name, char* value)
158 {
159   storage->setProperty(name, value);
160 }
161
162 /** \ingroup m_storage_management
163  * \brief Returns the value of a given storage property
164  *
165  * \param storage a storage
166  * \param name a property name
167  * \return value of a property (or nullptr if property not set)
168  */
169 const char *MSG_storage_get_property_value(msg_storage_t storage, const char *name)
170 {
171   return storage->getProperty(name);
172 }
173
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
178  */
179 msg_storage_t MSG_storage_get_by_name(const char *name)
180 {
181   return simgrid::s4u::Storage::byName(name);
182 }
183
184 /** \ingroup msg_storage_management
185  * \brief Returns a dynar containing all the storage elements declared at a given point of time
186  */
187 xbt_dynar_t MSG_storages_as_dynar()
188 {
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));
193   delete storage_map;
194   return res;
195 }
196
197 void* MSG_storage_get_data(msg_storage_t storage)
198 {
199   xbt_assert((storage != nullptr), "Invalid parameters");
200   return storage->getUserdata();
201 }
202
203 msg_error_t MSG_storage_set_data(msg_storage_t storage, void *data)
204 {
205   storage->setUserdata(data);
206   return MSG_OK;
207 }
208
209 sg_size_t MSG_storage_read(msg_storage_t storage, sg_size_t size)
210 {
211   return storage->read(size);
212 }
213
214 sg_size_t MSG_storage_write(msg_storage_t storage, sg_size_t size)
215 {
216   return storage->write(size);
217 }
218
219 }