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/File.hpp"
7 #include "simgrid/s4u/Host.hpp"
8 #include "simgrid/s4u/Storage.hpp"
9 #include "src/msg/msg_private.h"
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);
42 * \brief Set the user data of a #msg_file_t.
44 * This functions attach \a data to \a file.
46 msg_error_t MSG_file_set_data(msg_file_t fd, void *data)
48 fd->setUserdata(data);
54 * \brief Return the user data of a #msg_file_t.
56 * This functions checks whether \a file is a valid pointer and return the user data associated to \a file if possible.
58 void* MSG_file_get_data(msg_file_t fd)
60 return fd->userdata();
64 * \brief Display information related to a file descriptor
66 * \param fd is a the file descriptor
68 void MSG_file_dump (msg_file_t fd){
69 XBT_INFO("File Descriptor information:\n"
70 "\t\tFull path: '%s'\n"
72 "\t\tMount point: '%s'\n"
73 "\t\tStorage Id: '%s'\n"
74 "\t\tStorage Type: '%s'\n"
75 "\t\tFile Descriptor Id: %d",
76 fd->path(), fd->size(), fd->mount_point.c_str(), fd->storageId, fd->storage_type, fd->desc_id);
80 * \brief Read a file (local or remote)
82 * \param size of the file to read
83 * \param fd is a the file descriptor
84 * \return the number of bytes successfully read or -1 if an error occurred
86 sg_size_t MSG_file_read(msg_file_t fd, sg_size_t size)
90 if (fd->size() == 0) /* Nothing to read, return */
93 /* Find the host where the file is physically located and read it */
94 msg_storage_t storage_src = simgrid::s4u::Storage::byName(fd->storageId);
95 msg_host_t attached_host = storage_src->host();
96 read_size = fd->read(size); // TODO re-add attached_host;
98 if (strcmp(attached_host->cname(), MSG_host_self()->cname())) {
99 /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
100 XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", attached_host->cname(), read_size);
101 msg_host_t m_host_list[] = {MSG_host_self(), attached_host};
102 double flops_amount[] = {0, 0};
103 double bytes_amount[] = {0, 0, static_cast<double>(read_size), 0};
105 msg_task_t task = MSG_parallel_task_create("file transfer for read", 2, m_host_list, flops_amount, bytes_amount,
107 msg_error_t transfer = MSG_parallel_task_execute(task);
108 MSG_task_destroy(task);
110 if(transfer != MSG_OK){
111 if (transfer == MSG_HOST_FAILURE)
112 XBT_WARN("Transfer error, %s remote host just turned off!", attached_host->cname());
113 if (transfer == MSG_TASK_CANCELED)
114 XBT_WARN("Transfer error, task has been canceled!");
122 /** \ingroup msg_file
123 * \brief Write into a file (local or remote)
125 * \param size of the file to write
126 * \param fd is a the file descriptor
127 * \return the number of bytes successfully write or -1 if an error occurred
129 sg_size_t MSG_file_write(msg_file_t fd, sg_size_t size)
131 if (size == 0) /* Nothing to write, return */
134 /* Find the host where the file is physically located (remote or local)*/
135 msg_storage_t storage_src = simgrid::s4u::Storage::byName(fd->storageId);
136 msg_host_t attached_host = storage_src->host();
138 if (strcmp(attached_host->cname(), MSG_host_self()->cname())) {
139 /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
140 XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", attached_host->cname(), size);
141 msg_host_t m_host_list[] = {MSG_host_self(), attached_host};
142 double flops_amount[] = {0, 0};
143 double bytes_amount[] = {0, static_cast<double>(size), 0, 0};
145 msg_task_t task = MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount,
147 msg_error_t transfer = MSG_parallel_task_execute(task);
148 MSG_task_destroy(task);
150 if(transfer != MSG_OK){
151 if (transfer == MSG_HOST_FAILURE)
152 XBT_WARN("Transfer error, %s remote host just turned off!", attached_host->cname());
153 if (transfer == MSG_TASK_CANCELED)
154 XBT_WARN("Transfer error, task has been canceled!");
159 /* Write file on local or remote host */
160 // sg_size_t offset = fd->tell();
161 sg_size_t write_size = fd->write(size); // TODO readd attached_host;
166 /** \ingroup msg_file
167 * \brief Opens the file whose name is the string pointed to by path
169 * \param fullpath is the file location on the storage
170 * \param data user data to attach to the file
172 * \return An #msg_file_t associated to the file
174 msg_file_t MSG_file_open(const char* fullpath, void* data)
176 msg_file_t fd = new simgrid::s4u::File(fullpath, MSG_host_self());
177 fd->desc_id = MSG_host_get_file_descriptor_id(MSG_host_self());
181 /** \ingroup msg_file
182 * \brief Close the file
184 * \param fd is the file to close
185 * \return 0 on success or 1 on error
187 int MSG_file_close(msg_file_t fd)
189 MSG_host_release_file_descriptor_id(MSG_host_self(), fd->desc_id);
195 /** \ingroup msg_file
196 * \brief Unlink the file pointed by fd
198 * \param fd is the file descriptor (#msg_file_t)
199 * \return 0 on success or 1 on error
201 msg_error_t MSG_file_unlink(msg_file_t fd)
203 /* Find the host where the file is physically located (remote or local)*/
204 msg_storage_t storage_src = simgrid::s4u::Storage::byName(fd->storageId);
205 msg_host_t attached_host = storage_src->host();
206 fd->unlink(attached_host);
211 /** \ingroup msg_file
212 * \brief Return the size of a file
214 * \param fd is the file descriptor (#msg_file_t)
215 * \return the size of the file (as a #sg_size_t)
217 sg_size_t MSG_file_get_size(msg_file_t fd)
224 * \brief Set the file position indicator in the msg_file_t by adding offset bytes
225 * to the position specified by origin (either SEEK_SET, SEEK_CUR, or SEEK_END).
227 * \param fd : file object that identifies the stream
228 * \param offset : number of bytes to offset from origin
229 * \param origin : Position used as reference for the offset. It is specified by one of the following constants defined
230 * in \<stdio.h\> exclusively to be used as arguments for this function (SEEK_SET = beginning of file,
231 * SEEK_CUR = current position of the file pointer, SEEK_END = end of file)
232 * \return If successful, the function returns MSG_OK (=0). Otherwise, it returns MSG_TASK_CANCELED (=8).
234 msg_error_t MSG_file_seek(msg_file_t fd, sg_offset_t offset, int origin)
236 fd->seek(offset, origin);
242 * \brief Returns the current value of the position indicator of the file
244 * \param fd : file object that identifies the stream
245 * \return On success, the current value of the position indicator is returned.
248 sg_size_t MSG_file_tell(msg_file_t fd)
253 const char *MSG_file_get_name(msg_file_t fd) {
254 xbt_assert((fd != nullptr), "Invalid parameters");
260 * \brief Move a file to another location on the *same mount point*.
263 msg_error_t MSG_file_move (msg_file_t fd, const char* fullpath)
271 * \brief Copy a file to another location on a remote host.
272 * \param file : the file to move
273 * \param host : the remote host where the file has to be copied
274 * \param fullpath : the complete path destination on the remote host
275 * \return If successful, the function returns MSG_OK. Otherwise, it returns MSG_TASK_CANCELED.
277 msg_error_t MSG_file_rcopy (msg_file_t file, msg_host_t host, const char* fullpath)
279 /* Find the host where the file is physically located and read it */
280 msg_storage_t storage_src = simgrid::s4u::Storage::byName(file->storageId);
281 msg_host_t src_host = storage_src->host();
282 MSG_file_seek(file, 0, SEEK_SET);
283 sg_size_t read_size = file->read(file->size());
285 /* Find the host that owns the storage where the file has to be copied */
286 msg_storage_t storage_dest = nullptr;
288 size_t longest_prefix_length = 0;
290 for (auto elm : host->mountedStorages()) {
291 std::string mount_point = std::string(fullpath).substr(0, elm.first.size());
292 if (mount_point == elm.first && elm.first.length() > longest_prefix_length) {
293 /* The current mount name is found in the full path and is bigger than the previous*/
294 longest_prefix_length = elm.first.length();
295 storage_dest = elm.second;
299 if (storage_dest != nullptr) {
300 /* Mount point found, retrieve the host the storage is attached to */
301 dst_host = storage_dest->host();
303 XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, host->cname());
304 return MSG_TASK_CANCELED;
307 XBT_DEBUG("Initiate data transfer of %llu bytes between %s and %s.", read_size, src_host->cname(),
308 storage_dest->host()->cname());
309 msg_host_t m_host_list[] = {src_host, dst_host};
310 double flops_amount[] = {0, 0};
311 double bytes_amount[] = {0, static_cast<double>(read_size), 0, 0};
314 MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount, nullptr);
315 msg_error_t err = MSG_parallel_task_execute(task);
316 MSG_task_destroy(task);
319 if (err == MSG_HOST_FAILURE)
320 XBT_WARN("Transfer error, %s remote host just turned off!", storage_dest->host()->cname());
321 if (err == MSG_TASK_CANCELED)
322 XBT_WARN("Transfer error, task has been canceled!");
327 /* Create file on remote host, write it and close it */
328 msg_file_t fd = new simgrid::s4u::File(fullpath, dst_host, nullptr);
329 fd->write(read_size, dst_host);
336 * \brief Move a file to another location on a remote host.
337 * \param file : the file to move
338 * \param host : the remote host where the file has to be moved
339 * \param fullpath : the complete path destination on the remote host
340 * \return If successful, the function returns MSG_OK. Otherwise, it returns MSG_TASK_CANCELED.
342 msg_error_t MSG_file_rmove (msg_file_t file, msg_host_t host, const char* fullpath)
344 msg_error_t res = MSG_file_rcopy(file, host, fullpath);
345 MSG_file_unlink(file);
349 /********************************* Storage **************************************/
350 /** @addtogroup msg_storage_management
351 * (#msg_storage_t) and the functions for managing it.
354 /** \ingroup msg_storage_management
356 * \brief Returns the name of the #msg_storage_t.
358 * This functions checks whether a storage is a valid pointer or not and return its name.
360 const char* MSG_storage_get_name(msg_storage_t storage)
362 xbt_assert((storage != nullptr), "Invalid parameters");
363 return storage->name();
366 /** \ingroup msg_storage_management
367 * \brief Returns the free space size of a storage element
368 * \param storage a storage
369 * \return the free space size of the storage element (as a #sg_size_t)
371 sg_size_t MSG_storage_get_free_size(msg_storage_t storage)
373 return storage->sizeFree();
376 /** \ingroup msg_storage_management
377 * \brief Returns the used space size of a storage element
378 * \param storage a storage
379 * \return the used space size of the storage element (as a #sg_size_t)
381 sg_size_t MSG_storage_get_used_size(msg_storage_t storage)
383 return storage->sizeUsed();
386 /** \ingroup msg_storage_management
387 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this storage
388 * \param storage a storage
389 * \return a dict containing the properties
391 xbt_dict_t MSG_storage_get_properties(msg_storage_t storage)
393 xbt_assert((storage != nullptr), "Invalid parameters (storage is nullptr)");
394 return storage->properties();
397 /** \ingroup msg_storage_management
398 * \brief Change the value of a given storage property
400 * \param storage a storage
401 * \param name a property name
402 * \param value what to change the property to
404 void MSG_storage_set_property_value(msg_storage_t storage, const char* name, char* value)
406 storage->setProperty(name, value);
409 /** \ingroup m_storage_management
410 * \brief Returns the value of a given storage property
412 * \param storage a storage
413 * \param name a property name
414 * \return value of a property (or nullptr if property not set)
416 const char *MSG_storage_get_property_value(msg_storage_t storage, const char *name)
418 return storage->property(name);
421 /** \ingroup msg_storage_management
422 * \brief Finds a msg_storage_t using its name.
423 * \param name the name of a storage
424 * \return the corresponding storage
426 msg_storage_t MSG_storage_get_by_name(const char *name)
428 return simgrid::s4u::Storage::byName(name);
431 /** \ingroup msg_storage_management
432 * \brief Returns a dynar containing all the storage elements declared at a given point of time
434 xbt_dynar_t MSG_storages_as_dynar()
436 std::map<std::string, simgrid::s4u::Storage*>* storage_map = simgrid::s4u::allStorages();
437 xbt_dynar_t res = xbt_dynar_new(sizeof(msg_storage_t),nullptr);
438 for (auto s : *storage_map)
439 xbt_dynar_push(res, &(s.second));
444 /** \ingroup msg_storage_management
446 * \brief Set the user data of a #msg_storage_t.
447 * This functions attach \a data to \a storage if possible.
449 msg_error_t MSG_storage_set_data(msg_storage_t storage, void *data)
451 storage->setUserdata(data);
455 /** \ingroup m_host_management
457 * \brief Returns the user data of a #msg_storage_t.
459 * This functions checks whether \a storage is a valid pointer and returns its associate user data if possible.
461 void *MSG_storage_get_data(msg_storage_t storage)
463 xbt_assert((storage != nullptr), "Invalid parameters");
464 return storage->userdata();
467 /** \ingroup msg_storage_management
469 * \brief Returns the content (file list) of a #msg_storage_t.
470 * \param storage a storage
471 * \return The content of this storage element as a dict (full path file => size)
473 xbt_dict_t MSG_storage_get_content(msg_storage_t storage)
475 std::map<std::string, sg_size_t>* content = storage->content();
476 xbt_dict_t content_dict = xbt_dict_new_homogeneous(&free);
478 for (auto entry : *content) {
479 sg_size_t* psize = static_cast<sg_size_t*>(malloc(sizeof(sg_size_t)));
480 *psize = entry.second;
481 xbt_dict_set(content_dict, entry.first.c_str(), psize, nullptr);
486 /** \ingroup msg_storage_management
488 * \brief Returns the size of a #msg_storage_t.
489 * \param storage a storage
490 * \return The size of the storage
492 sg_size_t MSG_storage_get_size(msg_storage_t storage)
494 return storage->size();
497 /** \ingroup msg_storage_management
499 * \brief Returns the host name the storage is attached to
501 * This functions checks whether a storage is a valid pointer or not and return its name.
503 const char* MSG_storage_get_host(msg_storage_t storage)
505 xbt_assert((storage != nullptr), "Invalid parameters");
506 return storage->host()->cname();