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 "../surf/StorageImpl.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 /********************************* File **************************************/
23 void __MSG_file_get_info(msg_file_t fd){
25 xbt_dynar_t info = simcall_file_get_info(fd->simdata->smx_file);
28 fd->storage_type = xbt_dynar_pop_as(info, char*);
29 fd->storageId = xbt_dynar_pop_as(info, char*);
30 fd->mount_point = xbt_dynar_pop_as(info, char*);
31 psize = xbt_dynar_pop_as(info, sg_size_t*);
34 xbt_dynar_free_container(&info);
37 static int MSG_host_get_file_descriptor_id(msg_host_t host)
39 simgrid::MsgHostExt* priv = host->extension<simgrid::MsgHostExt>();
40 if (priv->file_descriptor_table == nullptr) {
41 priv->file_descriptor_table = new std::vector<int>(sg_storage_max_file_descriptors);
42 std::iota(priv->file_descriptor_table->rbegin(), priv->file_descriptor_table->rend(), 0); // Fill with ..., 1, 0.
44 xbt_assert(not priv->file_descriptor_table->empty(), "Too much files are opened! Some have to be closed.");
45 int desc = priv->file_descriptor_table->back();
46 priv->file_descriptor_table->pop_back();
50 static void MSG_host_release_file_descriptor_id(msg_host_t host, int id)
52 host->extension<simgrid::MsgHostExt>()->file_descriptor_table->push_back(id);
57 * \brief Set the user data of a #msg_file_t.
59 * This functions attach \a data to \a file.
61 msg_error_t MSG_file_set_data(msg_file_t fd, void *data)
69 * \brief Return the user data of a #msg_file_t.
71 * This functions checks whether \a file is a valid pointer and return the user data associated to \a file if possible.
73 void *MSG_file_get_data(msg_file_t fd)
79 * \brief Display information related to a file descriptor
81 * \param fd is a the file descriptor
83 void MSG_file_dump (msg_file_t fd){
84 /* Update the cached information first */
85 __MSG_file_get_info(fd);
87 XBT_INFO("File Descriptor information:\n"
88 "\t\tFull path: '%s'\n"
90 "\t\tMount point: '%s'\n"
91 "\t\tStorage Id: '%s'\n"
92 "\t\tStorage Type: '%s'\n"
93 "\t\tFile Descriptor Id: %d",
94 fd->fullpath, fd->size, fd->mount_point, fd->storageId, fd->storage_type, fd->desc_id);
98 * \brief Read a file (local or remote)
100 * \param size of the file to read
101 * \param fd is a the file descriptor
102 * \return the number of bytes successfully read or -1 if an error occurred
104 sg_size_t MSG_file_read(msg_file_t fd, sg_size_t size)
108 if (fd->size == 0) /* Nothing to read, return */
111 /* Find the host where the file is physically located and read it */
112 msg_storage_t storage_src = simgrid::s4u::Storage::byName(fd->storageId);
113 msg_host_t attached_host = MSG_host_by_name(storage_src->host());
114 read_size = simcall_file_read(fd->simdata->smx_file, size, attached_host);
116 if (strcmp(storage_src->host(), MSG_host_self()->cname())) {
117 /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
118 XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", storage_src->host(), read_size);
119 msg_host_t m_host_list[] = {MSG_host_self(), attached_host};
120 double flops_amount[] = {0, 0};
121 double bytes_amount[] = {0, 0, static_cast<double>(read_size), 0};
123 msg_task_t task = MSG_parallel_task_create("file transfer for read", 2, m_host_list, flops_amount, bytes_amount,
125 msg_error_t transfer = MSG_parallel_task_execute(task);
126 MSG_task_destroy(task);
128 if(transfer != MSG_OK){
129 if (transfer == MSG_HOST_FAILURE)
130 XBT_WARN("Transfer error, %s remote host just turned off!", attached_host->cname());
131 if (transfer == MSG_TASK_CANCELED)
132 XBT_WARN("Transfer error, task has been canceled!");
140 /** \ingroup msg_file
141 * \brief Write into a file (local or remote)
143 * \param size of the file to write
144 * \param fd is a the file descriptor
145 * \return the number of bytes successfully write or -1 if an error occurred
147 sg_size_t MSG_file_write(msg_file_t fd, sg_size_t size)
149 if (size == 0) /* Nothing to write, return */
152 /* Find the host where the file is physically located (remote or local)*/
153 msg_storage_t storage_src = simgrid::s4u::Storage::byName(fd->storageId);
154 msg_host_t attached_host = MSG_host_by_name(storage_src->host());
156 if (strcmp(storage_src->host(), MSG_host_self()->cname())) {
157 /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
158 XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", storage_src->host(), size);
159 msg_host_t m_host_list[] = {MSG_host_self(), attached_host};
160 double flops_amount[] = {0, 0};
161 double bytes_amount[] = {0, static_cast<double>(size), 0, 0};
163 msg_task_t task = MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount,
165 msg_error_t transfer = MSG_parallel_task_execute(task);
166 MSG_task_destroy(task);
168 if(transfer != MSG_OK){
169 if (transfer == MSG_HOST_FAILURE)
170 XBT_WARN("Transfer error, %s remote host just turned off!", attached_host->cname());
171 if (transfer == MSG_TASK_CANCELED)
172 XBT_WARN("Transfer error, task has been canceled!");
177 /* Write file on local or remote host */
178 sg_size_t offset = simcall_file_tell(fd->simdata->smx_file);
179 sg_size_t write_size = simcall_file_write(fd->simdata->smx_file, size, attached_host);
180 fd->size = offset + write_size;
185 /** \ingroup msg_file
186 * \brief Opens the file whose name is the string pointed to by path
188 * \param fullpath is the file location on the storage
189 * \param data user data to attach to the file
191 * \return An #msg_file_t associated to the file
193 msg_file_t MSG_file_open(const char* fullpath, void* data)
195 msg_file_t fd = xbt_new(s_msg_file_priv_t, 1);
197 fd->fullpath = xbt_strdup(fullpath);
198 fd->simdata = xbt_new0(s_simdata_file_t, 1);
199 fd->simdata->smx_file = simcall_file_open(fullpath, MSG_host_self());
200 fd->desc_id = MSG_host_get_file_descriptor_id(MSG_host_self());
202 __MSG_file_get_info(fd);
207 /** \ingroup msg_file
208 * \brief Close the file
210 * \param fd is the file to close
211 * \return 0 on success or 1 on error
213 int MSG_file_close(msg_file_t fd)
218 int res = simcall_file_close(fd->simdata->smx_file, MSG_host_self());
219 MSG_host_release_file_descriptor_id(MSG_host_self(), fd->desc_id);
220 __MSG_file_destroy(fd);
225 /** \ingroup msg_file
226 * \brief Unlink the file pointed by fd
228 * \param fd is the file descriptor (#msg_file_t)
229 * \return 0 on success or 1 on error
231 msg_error_t MSG_file_unlink(msg_file_t fd)
233 /* Find the host where the file is physically located (remote or local)*/
234 msg_storage_t storage_src = simgrid::s4u::Storage::byName(fd->storageId);
235 msg_host_t attached_host = MSG_host_by_name(storage_src->host());
236 int res = simcall_file_unlink(fd->simdata->smx_file, attached_host);
237 __MSG_file_destroy(fd);
238 return static_cast<msg_error_t>(res);
241 /** \ingroup msg_file
242 * \brief Return the size of a file
244 * \param fd is the file descriptor (#msg_file_t)
245 * \return the size of the file (as a #sg_size_t)
247 sg_size_t MSG_file_get_size(msg_file_t fd){
248 return simcall_file_get_size(fd->simdata->smx_file);
253 * \brief Set the file position indicator in the msg_file_t by adding offset bytes
254 * to the position specified by origin (either SEEK_SET, SEEK_CUR, or SEEK_END).
256 * \param fd : file object that identifies the stream
257 * \param offset : number of bytes to offset from origin
258 * \param origin : Position used as reference for the offset. It is specified by one of the following constants defined
259 * in \<stdio.h\> exclusively to be used as arguments for this function (SEEK_SET = beginning of file,
260 * SEEK_CUR = current position of the file pointer, SEEK_END = end of file)
261 * \return If successful, the function returns MSG_OK (=0). Otherwise, it returns MSG_TASK_CANCELED (=8).
263 msg_error_t MSG_file_seek(msg_file_t fd, sg_offset_t offset, int origin)
265 return static_cast<msg_error_t>(simcall_file_seek(fd->simdata->smx_file, offset, origin));
270 * \brief Returns the current value of the position indicator of the file
272 * \param fd : file object that identifies the stream
273 * \return On success, the current value of the position indicator is returned.
276 sg_size_t MSG_file_tell(msg_file_t fd)
278 return simcall_file_tell(fd->simdata->smx_file);
281 const char *MSG_file_get_name(msg_file_t fd) {
282 xbt_assert((fd != nullptr), "Invalid parameters");
288 * \brief Move a file to another location on the *same mount point*.
291 msg_error_t MSG_file_move (msg_file_t fd, const char* fullpath)
293 return static_cast<msg_error_t>(simcall_file_move(fd->simdata->smx_file, fullpath));
298 * \brief Copy a file to another location on a remote host.
299 * \param file : the file to move
300 * \param host : the remote host where the file has to be copied
301 * \param fullpath : the complete path destination on the remote host
302 * \return If successful, the function returns MSG_OK. Otherwise, it returns MSG_TASK_CANCELED.
304 msg_error_t MSG_file_rcopy (msg_file_t file, msg_host_t host, const char* fullpath)
306 /* Find the host where the file is physically located and read it */
307 msg_storage_t storage_src = simgrid::s4u::Storage::byName(file->storageId);
308 msg_host_t attached_host = MSG_host_by_name(storage_src->host());
309 MSG_file_seek(file, 0, SEEK_SET);
310 sg_size_t read_size = simcall_file_read(file->simdata->smx_file, file->size, attached_host);
312 /* Find the real host destination where the file will be physically stored */
313 xbt_dict_cursor_t cursor = nullptr;
314 msg_storage_t storage_dest = nullptr;
315 msg_host_t host_dest;
316 size_t longest_prefix_length = 0;
318 xbt_dict_t storage_list = host->mountedStoragesAsDict();
321 xbt_dict_foreach(storage_list,cursor,mount_name,storage_name){
322 char* file_mount_name = static_cast<char*>(xbt_malloc(strlen(mount_name) + 1));
323 strncpy(file_mount_name, fullpath, strlen(mount_name) + 1);
324 file_mount_name[strlen(mount_name)] = '\0';
326 if (not strcmp(file_mount_name, mount_name) && strlen(mount_name) > longest_prefix_length) {
327 /* The current mount name is found in the full path and is bigger than the previous*/
328 longest_prefix_length = strlen(mount_name);
329 storage_dest = simgrid::s4u::Storage::byName(storage_name);
331 xbt_free(file_mount_name);
333 xbt_dict_free(&storage_list);
335 if(longest_prefix_length>0){
336 /* Mount point found, retrieve the host the storage is attached to */
337 host_dest = MSG_host_by_name(storage_dest->host());
339 XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, host->cname());
340 return MSG_TASK_CANCELED;
343 XBT_DEBUG("Initiate data transfer of %llu bytes between %s and %s.", read_size, storage_src->host(),
344 storage_dest->host());
345 msg_host_t m_host_list[] = {attached_host, host_dest};
346 double flops_amount[] = {0, 0};
347 double bytes_amount[] = {0, static_cast<double>(read_size), 0, 0};
350 MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount, nullptr);
351 msg_error_t transfer = MSG_parallel_task_execute(task);
352 MSG_task_destroy(task);
354 if(transfer != MSG_OK){
355 if (transfer == MSG_HOST_FAILURE)
356 XBT_WARN("Transfer error, %s remote host just turned off!", storage_dest->host());
357 if (transfer == MSG_TASK_CANCELED)
358 XBT_WARN("Transfer error, task has been canceled!");
363 /* Create file on remote host, write it and close it */
364 smx_file_t smx_file = simcall_file_open(fullpath, host_dest);
365 simcall_file_write(smx_file, read_size, host_dest);
366 simcall_file_close(smx_file, host_dest);
372 * \brief Move a file to another location on a remote host.
373 * \param file : the file to move
374 * \param host : the remote host where the file has to be moved
375 * \param fullpath : the complete path destination on the remote host
376 * \return If successful, the function returns MSG_OK. Otherwise, it returns MSG_TASK_CANCELED.
378 msg_error_t MSG_file_rmove (msg_file_t file, msg_host_t host, const char* fullpath)
380 msg_error_t res = MSG_file_rcopy(file, host, fullpath);
381 MSG_file_unlink(file);
386 * \brief Destroys a file (internal call only)
388 void __MSG_file_destroy(msg_file_t file)
390 xbt_free(file->fullpath);
391 xbt_free(file->simdata);
395 /********************************* Storage **************************************/
396 /** @addtogroup msg_storage_management
397 * (#msg_storage_t) and the functions for managing it.
400 /** \ingroup msg_storage_management
402 * \brief Returns the name of the #msg_storage_t.
404 * This functions checks whether a storage is a valid pointer or not and return its name.
406 const char *MSG_storage_get_name(msg_storage_t storage) {
407 xbt_assert((storage != nullptr), "Invalid parameters");
408 return storage->name();
411 /** \ingroup msg_storage_management
412 * \brief Returns the free space size of a storage element
413 * \param storage a storage
414 * \return the free space size of the storage element (as a #sg_size_t)
416 sg_size_t MSG_storage_get_free_size(msg_storage_t storage){
417 return storage->sizeFree();
420 /** \ingroup msg_storage_management
421 * \brief Returns the used space size of a storage element
422 * \param storage a storage
423 * \return the used space size of the storage element (as a #sg_size_t)
425 sg_size_t MSG_storage_get_used_size(msg_storage_t storage){
426 return storage->sizeUsed();
429 /** \ingroup msg_storage_management
430 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this storage
431 * \param storage a storage
432 * \return a dict containing the properties
434 xbt_dict_t MSG_storage_get_properties(msg_storage_t storage)
436 xbt_assert((storage != nullptr), "Invalid parameters (storage is nullptr)");
437 return storage->properties();
440 /** \ingroup msg_storage_management
441 * \brief Change the value of a given storage property
443 * \param storage a storage
444 * \param name a property name
445 * \param value what to change the property to
447 void MSG_storage_set_property_value(msg_storage_t storage, const char* name, char* value)
449 storage->setProperty(name, value);
452 /** \ingroup m_storage_management
453 * \brief Returns the value of a given storage property
455 * \param storage a storage
456 * \param name a property name
457 * \return value of a property (or nullptr if property not set)
459 const char *MSG_storage_get_property_value(msg_storage_t storage, const char *name)
461 return storage->property(name);
464 /** \ingroup msg_storage_management
465 * \brief Finds a msg_storage_t using its name.
466 * \param name the name of a storage
467 * \return the corresponding storage
469 msg_storage_t MSG_storage_get_by_name(const char *name)
471 return simgrid::s4u::Storage::byName(name);
474 /** \ingroup msg_storage_management
475 * \brief Returns a dynar containing all the storage elements declared at a given point of time
477 xbt_dynar_t MSG_storages_as_dynar() {
478 xbt_dynar_t res = xbt_dynar_new(sizeof(msg_storage_t),nullptr);
479 for (auto s : *simgrid::s4u::allStorages()) {
480 xbt_dynar_push(res, &(s.second));
485 /** \ingroup msg_storage_management
487 * \brief Set the user data of a #msg_storage_t.
488 * This functions attach \a data to \a storage if possible.
490 msg_error_t MSG_storage_set_data(msg_storage_t storage, void *data)
492 storage->setUserdata(data);
496 /** \ingroup m_host_management
498 * \brief Returns the user data of a #msg_storage_t.
500 * This functions checks whether \a storage is a valid pointer and returns its associate user data if possible.
502 void *MSG_storage_get_data(msg_storage_t storage)
504 xbt_assert((storage != nullptr), "Invalid parameters");
505 return storage->userdata();
508 /** \ingroup msg_storage_management
510 * \brief Returns the content (file list) of a #msg_storage_t.
511 * \param storage a storage
512 * \return The content of this storage element as a dict (full path file => size)
514 xbt_dict_t MSG_storage_get_content(msg_storage_t storage)
516 std::map<std::string, sg_size_t*>* content = storage->content();
517 xbt_dict_t content_dict = xbt_dict_new_homogeneous(nullptr);
519 for (auto entry : *content) {
520 xbt_dict_set(content_dict, entry.first.c_str(), entry.second, nullptr);
525 /** \ingroup msg_storage_management
527 * \brief Returns the size of a #msg_storage_t.
528 * \param storage a storage
529 * \return The size of the storage
531 sg_size_t MSG_storage_get_size(msg_storage_t storage)
533 return storage->size();
536 /** \ingroup msg_storage_management
538 * \brief Returns the host name the storage is attached to
540 * This functions checks whether a storage is a valid pointer or not and return its name.
542 const char *MSG_storage_get_host(msg_storage_t storage) {
543 xbt_assert((storage != nullptr), "Invalid parameters");
544 return storage->host();