1 /* Copyright (c) 2004-2016. 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 "src/msg/msg_private.h"
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_io, msg, "Logging specific to MSG (io)");
11 /** @addtogroup msg_file
12 * (#msg_file_t) and the functions for managing it.
17 /********************************* File **************************************/
18 void __MSG_file_get_info(msg_file_t fd){
20 msg_file_priv_t priv = MSG_file_priv(fd);
21 xbt_dynar_t info = simcall_file_get_info(priv->simdata->smx_file);
24 priv->content_type = xbt_dynar_pop_as(info, char *);
25 priv->storage_type = xbt_dynar_pop_as(info, char *);
26 priv->storageId = xbt_dynar_pop_as(info, char *);
27 priv->mount_point = xbt_dynar_pop_as(info, char *);
28 psize = xbt_dynar_pop_as(info, sg_size_t*);
31 xbt_dynar_free_container(&info);
36 * \brief Set the user data of a #msg_file_t.
38 * This functions attach \a data to \a file.
40 msg_error_t MSG_file_set_data(msg_file_t fd, void *data)
42 msg_file_priv_t priv = MSG_file_priv(fd);
49 * \brief Return the user data of a #msg_file_t.
51 * This functions checks whether \a file is a valid pointer and return the user data associated to \a file if possible.
53 void *MSG_file_get_data(msg_file_t fd)
55 msg_file_priv_t priv = MSG_file_priv(fd);
60 * \brief Display information related to a file descriptor
62 * \param fd is a the file descriptor
64 void MSG_file_dump (msg_file_t fd){
65 /* Update the cached information first */
66 __MSG_file_get_info(fd);
68 msg_file_priv_t priv = MSG_file_priv(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\tContent Type: '%s'\n"
76 "\t\tFile Descriptor Id: %d",
77 priv->fullpath, priv->size, priv->mount_point,
78 priv->storageId, priv->storage_type,
79 priv->content_type, priv->desc_id);
83 * \brief Read a file (local or remote)
85 * \param size of the file to read
86 * \param fd is a the file descriptor
87 * \return the number of bytes successfully read or -1 if an error occurred
89 sg_size_t MSG_file_read(msg_file_t fd, sg_size_t size)
91 msg_file_priv_t file_priv = MSG_file_priv(fd);
94 if (file_priv->size == 0) /* Nothing to read, return */
97 /* Find the host where the file is physically located and read it */
98 msg_storage_t storage_src = static_cast<msg_storage_t>(xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId));
99 msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
100 msg_host_t attached_host = MSG_host_by_name(storage_priv_src->hostname);
101 read_size = simcall_file_read(file_priv->simdata->smx_file, size, attached_host);
103 if (strcmp(storage_priv_src->hostname, MSG_host_self()->cname())) {
104 /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
105 XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", storage_priv_src->hostname, read_size);
106 msg_host_t *m_host_list = nullptr;
107 m_host_list = xbt_new0(msg_host_t, 2);
109 m_host_list[0] = MSG_host_self();
110 m_host_list[1] = attached_host;
111 double flops_amount[] = { 0, 0};
112 double bytes_amount[] = { 0, 0, static_cast<double>(read_size), 0 };
114 msg_task_t task = MSG_parallel_task_create("file transfer for read", 2, m_host_list, flops_amount, bytes_amount,
116 msg_error_t transfer = MSG_parallel_task_execute(task);
117 MSG_task_destroy(task);
118 xbt_free(m_host_list);
119 if(transfer != MSG_OK){
120 if (transfer == MSG_HOST_FAILURE)
121 XBT_WARN("Transfer error, %s remote host just turned off!", attached_host->cname());
122 if (transfer == MSG_TASK_CANCELED)
123 XBT_WARN("Transfer error, task has been canceled!");
131 /** \ingroup msg_file
132 * \brief Write into a file (local or remote)
134 * \param size of the file to write
135 * \param fd is a the file descriptor
136 * \return the number of bytes successfully write or -1 if an error occurred
138 sg_size_t MSG_file_write(msg_file_t fd, sg_size_t size)
140 msg_file_priv_t file_priv = MSG_file_priv(fd);
142 if (size == 0) /* Nothing to write, return */
145 /* Find the host where the file is physically located (remote or local)*/
146 msg_storage_t storage_src = static_cast<msg_storage_t>(xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId));
147 msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
148 msg_host_t attached_host = MSG_host_by_name(storage_priv_src->hostname);
150 if (strcmp(storage_priv_src->hostname, MSG_host_self()->cname())) {
151 /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
152 XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", storage_priv_src->hostname, size);
153 msg_host_t *m_host_list = nullptr;
154 m_host_list = xbt_new0(msg_host_t, 2);
156 m_host_list[0] = MSG_host_self();
157 m_host_list[1] = attached_host;
158 double flops_amount[] = { 0, 0 };
159 double bytes_amount[] = { 0, static_cast<double>(size), 0, 0 };
161 msg_task_t task = MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount,
163 msg_error_t transfer = MSG_parallel_task_execute(task);
164 MSG_task_destroy(task);
166 if(transfer != MSG_OK){
167 if (transfer == MSG_HOST_FAILURE)
168 XBT_WARN("Transfer error, %s remote host just turned off!", attached_host->cname());
169 if (transfer == MSG_TASK_CANCELED)
170 XBT_WARN("Transfer error, task has been canceled!");
175 /* Write file on local or remote host */
176 sg_size_t offset = simcall_file_tell(file_priv->simdata->smx_file);
177 sg_size_t write_size = simcall_file_write(file_priv->simdata->smx_file, size, attached_host);
178 file_priv->size = offset+write_size;
183 /** \ingroup msg_file
184 * \brief Opens the file whose name is the string pointed to by path
186 * \param fullpath is the file location on the storage
187 * \param data user data to attach to the file
189 * \return An #msg_file_t associated to the file
191 msg_file_t MSG_file_open(const char* fullpath, void* data)
194 msg_file_priv_t priv = xbt_new(s_msg_file_priv_t, 1);
196 priv->fullpath = xbt_strdup(fullpath);
197 priv->simdata = xbt_new0(s_simdata_file_t,1);
198 priv->simdata->smx_file = simcall_file_open(fullpath, MSG_host_self());
199 priv->desc_id = __MSG_host_get_file_descriptor_id(MSG_host_self());
201 name = bprintf("%s:%s:%d", priv->fullpath, MSG_host_self()->cname(), priv->desc_id);
203 xbt_lib_set(file_lib, name, MSG_FILE_LEVEL, priv);
204 msg_file_t fd = static_cast<msg_file_t>(xbt_lib_get_elm_or_null(file_lib, name));
205 __MSG_file_get_info(fd);
211 /** \ingroup msg_file
212 * \brief Close the file
214 * \param fd is the file to close
215 * \return 0 on success or 1 on error
217 int MSG_file_close(msg_file_t fd)
220 msg_file_priv_t priv = MSG_file_priv(fd);
222 xbt_free(priv->data);
224 int res = simcall_file_close(priv->simdata->smx_file, MSG_host_self());
225 name = bprintf("%s:%s:%d", priv->fullpath, MSG_host_self()->cname(), priv->desc_id);
226 __MSG_host_release_file_descriptor_id(MSG_host_self(), priv->desc_id);
227 xbt_lib_unset(file_lib, name, MSG_FILE_LEVEL, 1);
232 /** \ingroup msg_file
233 * \brief Unlink the file pointed by fd
235 * \param fd is the file descriptor (#msg_file_t)
236 * \return 0 on success or 1 on error
238 msg_error_t MSG_file_unlink(msg_file_t fd)
240 msg_file_priv_t file_priv = MSG_file_priv(fd);
241 /* Find the host where the file is physically located (remote or local)*/
242 msg_storage_t storage_src = static_cast<msg_storage_t>(xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId));
243 msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
244 msg_host_t attached_host = MSG_host_by_name(storage_priv_src->hostname);
245 int res = simcall_file_unlink(file_priv->simdata->smx_file, attached_host);
246 return static_cast<msg_error_t>(res);
249 /** \ingroup msg_file
250 * \brief Return the size of a file
252 * \param fd is the file descriptor (#msg_file_t)
253 * \return the size of the file (as a #sg_size_t)
255 sg_size_t MSG_file_get_size(msg_file_t fd){
256 msg_file_priv_t priv = MSG_file_priv(fd);
257 return simcall_file_get_size(priv->simdata->smx_file);
262 * \brief Set the file position indicator in the msg_file_t by adding offset bytes
263 * to the position specified by origin (either SEEK_SET, SEEK_CUR, or SEEK_END).
265 * \param fd : file object that identifies the stream
266 * \param offset : number of bytes to offset from origin
267 * \param origin : Position used as reference for the offset. It is specified by one of the following constants defined
268 * in \<stdio.h\> exclusively to be used as arguments for this function (SEEK_SET = beginning of file,
269 * SEEK_CUR = current position of the file pointer, SEEK_END = end of file)
270 * \return If successful, the function returns MSG_OK (=0). Otherwise, it returns MSG_TASK_CANCELED (=8).
272 msg_error_t MSG_file_seek(msg_file_t fd, sg_offset_t offset, int origin)
274 msg_file_priv_t priv = MSG_file_priv(fd);
275 return static_cast<msg_error_t>(simcall_file_seek(priv->simdata->smx_file, offset, origin));
280 * \brief Returns the current value of the position indicator of the file
282 * \param fd : file object that identifies the stream
283 * \return On success, the current value of the position indicator is returned.
286 sg_size_t MSG_file_tell(msg_file_t fd)
288 msg_file_priv_t priv = MSG_file_priv(fd);
289 return simcall_file_tell(priv->simdata->smx_file);
292 const char *MSG_file_get_name(msg_file_t fd) {
293 xbt_assert((fd != nullptr), "Invalid parameters");
294 msg_file_priv_t priv = MSG_file_priv(fd);
295 return priv->fullpath;
300 * \brief Move a file to another location on the *same mount point*.
303 msg_error_t MSG_file_move (msg_file_t fd, const char* fullpath)
305 msg_file_priv_t priv = MSG_file_priv(fd);
306 return static_cast<msg_error_t>(simcall_file_move(priv->simdata->smx_file, fullpath));
311 * \brief Copy a file to another location on a remote host.
312 * \param file : the file to move
313 * \param host : the remote host where the file has to be copied
314 * \param fullpath : the complete path destination on the remote host
315 * \return If successful, the function returns MSG_OK. Otherwise, it returns MSG_TASK_CANCELED.
317 msg_error_t MSG_file_rcopy (msg_file_t file, msg_host_t host, const char* fullpath)
319 msg_file_priv_t file_priv = MSG_file_priv(file);
322 /* Find the host where the file is physically located and read it */
323 msg_storage_t storage_src = static_cast<msg_storage_t>(xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId));
324 msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
325 msg_host_t attached_host = MSG_host_by_name(storage_priv_src->hostname);
326 MSG_file_seek(file, 0, SEEK_SET);
327 read_size = simcall_file_read(file_priv->simdata->smx_file, file_priv->size, attached_host);
329 /* Find the real host destination where the file will be physically stored */
330 xbt_dict_cursor_t cursor = nullptr;
331 msg_storage_t storage_dest = nullptr;
332 msg_host_t host_dest;
333 size_t longest_prefix_length = 0;
335 xbt_dict_t storage_list = host->mountedStoragesAsDict();
338 xbt_dict_foreach(storage_list,cursor,mount_name,storage_name){
339 char* file_mount_name = static_cast<char *>(xbt_malloc ((strlen(mount_name)+1)));
340 strncpy(file_mount_name,fullpath,strlen(mount_name)+1);
341 file_mount_name[strlen(mount_name)] = '\0';
343 if(!strcmp(file_mount_name,mount_name) && strlen(mount_name)>longest_prefix_length){
344 /* The current mount name is found in the full path and is bigger than the previous*/
345 longest_prefix_length = strlen(mount_name);
346 storage_dest = (msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, storage_name);
348 xbt_free(file_mount_name);
350 xbt_dict_free(&storage_list);
352 char* host_name_dest = nullptr;
353 if(longest_prefix_length>0){
354 /* Mount point found, retrieve the host the storage is attached to */
355 msg_storage_priv_t storage_dest_priv = MSG_storage_priv(storage_dest);
356 host_name_dest = (char*)storage_dest_priv->hostname;
357 host_dest = MSG_host_by_name(host_name_dest);
359 XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, host->cname());
360 return MSG_TASK_CANCELED;
363 XBT_DEBUG("Initiate data transfer of %llu bytes between %s and %s.", read_size, storage_priv_src->hostname,
365 msg_host_t *m_host_list = nullptr;
366 m_host_list = xbt_new0(msg_host_t, 2);
368 m_host_list[0] = attached_host;
369 m_host_list[1] = host_dest;
370 double flops_amount[] = { 0, 0 };
371 double bytes_amount[] = { 0, static_cast<double>(read_size), 0, 0 };
374 MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount, nullptr);
375 msg_error_t transfer = MSG_parallel_task_execute(task);
376 MSG_task_destroy(task);
377 xbt_free(m_host_list);
378 if(transfer != MSG_OK){
379 if (transfer == MSG_HOST_FAILURE)
380 XBT_WARN("Transfer error, %s remote host just turned off!", host_name_dest);
381 if (transfer == MSG_TASK_CANCELED)
382 XBT_WARN("Transfer error, task has been canceled!");
387 /* Create file on remote host, write it and close it */
388 smx_file_t smx_file = simcall_file_open(fullpath, host_dest);
389 simcall_file_write(smx_file, read_size, host_dest);
390 simcall_file_close(smx_file, host_dest);
396 * \brief Move a file to another location on a remote host.
397 * \param file : the file to move
398 * \param host : the remote host where the file has to be moved
399 * \param fullpath : the complete path destination on the remote host
400 * \return If successful, the function returns MSG_OK. Otherwise, it returns MSG_TASK_CANCELED.
402 msg_error_t MSG_file_rmove (msg_file_t file, msg_host_t host, const char* fullpath)
404 msg_error_t res = MSG_file_rcopy(file, host, fullpath);
405 MSG_file_unlink(file);
410 * \brief Destroys a file (internal call only)
412 void __MSG_file_destroy(msg_file_priv_t file) {
413 xbt_free(file->fullpath);
414 xbt_free(file->simdata);
418 /********************************* Storage **************************************/
419 /** @addtogroup msg_storage_management
420 * (#msg_storage_t) and the functions for managing it.
423 msg_storage_t __MSG_storage_create(smx_storage_t storage)
425 const char *name = SIMIX_storage_get_name(storage);
426 const char *host = SIMIX_storage_get_host(storage);
427 msg_storage_priv_t storage_private = xbt_new0(s_msg_storage_priv_t, 1);
428 storage_private->hostname = host;
429 xbt_lib_set(storage_lib,name,MSG_STORAGE_LEVEL,storage_private);
430 return xbt_lib_get_elm_or_null(storage_lib, name);
434 * \brief Destroys a storage (internal call only)
436 void __MSG_storage_destroy(msg_storage_priv_t storage) {
440 /** \ingroup msg_storage_management
442 * \brief Returns the name of the #msg_storage_t.
444 * This functions checks whether a storage is a valid pointer or not and return its name.
446 const char *MSG_storage_get_name(msg_storage_t storage) {
447 xbt_assert((storage != nullptr), "Invalid parameters");
448 return SIMIX_storage_get_name(storage);
451 /** \ingroup msg_storage_management
452 * \brief Returns the free space size of a storage element
453 * \param storage a storage
454 * \return the free space size of the storage element (as a #sg_size_t)
456 sg_size_t MSG_storage_get_free_size(msg_storage_t storage){
457 return simcall_storage_get_free_size(storage);
460 /** \ingroup msg_storage_management
461 * \brief Returns the used space size of a storage element
462 * \param storage a storage
463 * \return the used space size of the storage element (as a #sg_size_t)
465 sg_size_t MSG_storage_get_used_size(msg_storage_t storage){
466 return simcall_storage_get_used_size(storage);
469 /** \ingroup msg_storage_management
470 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this storage
471 * \param storage a storage
472 * \return a dict containing the properties
474 xbt_dict_t MSG_storage_get_properties(msg_storage_t storage)
476 xbt_assert((storage != nullptr), "Invalid parameters (storage is nullptr)");
477 return (simcall_storage_get_properties(storage));
480 /** \ingroup msg_storage_management
481 * \brief Change the value of a given storage property
483 * \param storage a storage
484 * \param name a property name
485 * \param value what to change the property to
487 void MSG_storage_set_property_value(msg_storage_t storage, const char* name, char* value)
489 xbt_dict_set(MSG_storage_get_properties(storage), name, value, nullptr);
492 /** \ingroup m_storage_management
493 * \brief Returns the value of a given storage property
495 * \param storage a storage
496 * \param name a property name
497 * \return value of a property (or nullptr if property not set)
499 const char *MSG_storage_get_property_value(msg_storage_t storage, const char *name)
501 return static_cast<char*>(xbt_dict_get_or_null(MSG_storage_get_properties(storage), name));
504 /** \ingroup msg_storage_management
505 * \brief Finds a msg_storage_t using its name.
506 * \param name the name of a storage
507 * \return the corresponding storage
509 msg_storage_t MSG_storage_get_by_name(const char *name)
511 return static_cast<msg_storage_t>(xbt_lib_get_elm_or_null(storage_lib,name));
514 /** \ingroup msg_storage_management
515 * \brief Returns a dynar containing all the storage elements declared at a given point of time
517 xbt_dynar_t MSG_storages_as_dynar() {
518 xbt_lib_cursor_t cursor;
521 xbt_dynar_t res = xbt_dynar_new(sizeof(msg_storage_t),nullptr);
523 xbt_lib_foreach(storage_lib, cursor, key, data) {
524 if(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), MSG_STORAGE_LEVEL) != nullptr) {
525 xbt_dictelm_t elm = xbt_dict_cursor_get_elm(cursor);
526 xbt_dynar_push(res, &elm);
532 /** \ingroup msg_storage_management
534 * \brief Set the user data of a #msg_storage_t.
535 * This functions attach \a data to \a storage if possible.
537 msg_error_t MSG_storage_set_data(msg_storage_t storage, void *data)
539 msg_storage_priv_t priv = MSG_storage_priv(storage);
544 /** \ingroup m_host_management
546 * \brief Returns the user data of a #msg_storage_t.
548 * This functions checks whether \a storage is a valid pointer and returns its associate user data if possible.
550 void *MSG_storage_get_data(msg_storage_t storage)
552 xbt_assert((storage != nullptr), "Invalid parameters");
553 msg_storage_priv_t priv = MSG_storage_priv(storage);
557 /** \ingroup msg_storage_management
559 * \brief Returns the content (file list) of a #msg_storage_t.
560 * \param storage a storage
561 * \return The content of this storage element as a dict (full path file => size)
563 xbt_dict_t MSG_storage_get_content(msg_storage_t storage)
565 return SIMIX_storage_get_content(storage);
568 /** \ingroup msg_storage_management
570 * \brief Returns the size of a #msg_storage_t.
571 * \param storage a storage
572 * \return The size of the storage
574 sg_size_t MSG_storage_get_size(msg_storage_t storage)
576 return SIMIX_storage_get_size(storage);
579 /** \ingroup msg_storage_management
581 * \brief Returns the host name the storage is attached to
583 * This functions checks whether a storage is a valid pointer or not and return its name.
585 const char *MSG_storage_get_host(msg_storage_t storage) {
586 xbt_assert((storage != nullptr), "Invalid parameters");
587 msg_storage_priv_t priv = MSG_storage_priv(storage);
588 return priv->hostname;