1 /* Copyright (c) 2004-2014. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "msg_private.h"
9 #include "msg_mailbox.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_io, msg,
12 "Logging specific to MSG (io)");
14 /** @addtogroup msg_file_management
15 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Files" --> \endhtmlonly
16 * (#msg_file_t) and the functions for managing it.
21 /********************************* File **************************************/
22 void __MSG_file_get_info(msg_file_t fd){
24 msg_file_priv_t priv = MSG_file_priv(fd);
25 xbt_dynar_t info = simcall_file_get_info(priv->simdata->smx_file);
28 priv->content_type = xbt_dynar_pop_as(info, char *);
29 priv->storage_type = xbt_dynar_pop_as(info, char *);
30 priv->storageId = xbt_dynar_pop_as(info, char *);
31 priv->mount_point = xbt_dynar_pop_as(info, char *);
32 psize = xbt_dynar_pop_as(info, sg_size_t*);
35 xbt_dynar_free_container(&info);
38 /** \ingroup msg_file_management
40 * \brief Set the user data of a #msg_file_t.
42 * This functions checks whether some data has already been associated to \a file
43 or not and attach \a data to \a file if it is possible.
45 msg_error_t MSG_file_set_data(msg_file_t fd, void *data)
47 msg_file_priv_t priv = MSG_file_priv(fd);
52 /** \ingroup msg_file_management
54 * \brief Return the user data of a #msg_file_t.
56 * This functions checks whether \a file is a valid pointer or not and return
57 the user data associated to \a file if it is possible.
59 void *MSG_file_get_data(msg_file_t fd)
61 msg_file_priv_t priv = MSG_file_priv(fd);
65 /** \ingroup msg_file_management
66 * \brief Display information related to a file descriptor
68 * \param fd is a the file descriptor
71 void MSG_file_dump (msg_file_t fd){
72 /* Update the cached information first */
73 __MSG_file_get_info(fd);
75 msg_file_priv_t priv = MSG_file_priv(fd);
76 XBT_INFO("File Descriptor information:\n"
77 "\t\tFull path: '%s'\n"
79 "\t\tMount point: '%s'\n"
80 "\t\tStorage Id: '%s'\n"
81 "\t\tStorage Type: '%s'\n"
82 "\t\tContent Type: '%s'",
83 priv->fullpath, priv->size, priv->mount_point,
84 priv->storageId, priv->storage_type,
88 /** \ingroup msg_file_management
89 * \brief Read a file (local or remote)
91 * \param size of the file to read
92 * \param fd is a the file descriptor
93 * \return the number of bytes successfully read or -1 if an error occurred
95 sg_size_t MSG_file_read(msg_file_t fd, sg_size_t size)
97 msg_file_priv_t file_priv = MSG_file_priv(fd);
100 /* Find the host where the file is physically located and read it */
101 msg_storage_t storage_src =(msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId);
102 msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
103 msg_host_t attached_host = MSG_get_host_by_name(storage_priv_src->hostname);
104 read_size = simcall_file_read(file_priv->simdata->smx_file, size, attached_host);
106 if(strcmp(storage_priv_src->hostname, MSG_host_get_name(MSG_host_self()))){
107 /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
108 XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", storage_priv_src->hostname, read_size);
109 msg_host_t *m_host_list = NULL;
110 m_host_list = calloc(2, sizeof(msg_host_t));
112 m_host_list[0] = MSG_host_self();
113 m_host_list[1] = attached_host;
114 double computation_amount[] = { 0, 0 };
115 double communication_amount[] = { 0, 0, (double)read_size, 0 };
117 msg_task_t task = MSG_parallel_task_create("file transfer for read", 2, m_host_list, computation_amount, communication_amount, NULL);
118 msg_error_t transfer = MSG_parallel_task_execute(task);
119 MSG_task_destroy(task);
121 if(transfer != MSG_OK){
122 if (transfer == MSG_HOST_FAILURE)
123 XBT_WARN("Transfer error, %s remote host just turned off!", MSG_host_get_name(attached_host));
124 if (transfer == MSG_TASK_CANCELED)
125 XBT_WARN("Transfer error, task has been canceled!");
133 /** \ingroup msg_file_management
134 * \brief Write into a file (local or remote)
136 * \param size of the file to write
137 * \param fd is a the file descriptor
138 * \return the number of bytes successfully write or -1 if an error occurred
140 sg_size_t MSG_file_write(msg_file_t fd, sg_size_t size)
142 msg_file_priv_t file_priv = MSG_file_priv(fd);
143 sg_size_t write_size, offset;
145 /* Find the host where the file is physically located (remote or local)*/
146 msg_storage_t storage_src =(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_get_host_by_name(storage_priv_src->hostname);
150 if(strcmp(storage_priv_src->hostname, MSG_host_get_name(MSG_host_self()))){
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 = NULL;
154 m_host_list = calloc(2, sizeof(msg_host_t));
156 m_host_list[0] = MSG_host_self();
157 m_host_list[1] = attached_host;
158 double computation_amount[] = { 0, 0 };
159 double communication_amount[] = { 0, (double)size, 0, 0 };
161 msg_task_t task = MSG_parallel_task_create("file transfer for write", 2, m_host_list, computation_amount, communication_amount, NULL);
162 msg_error_t transfer = MSG_parallel_task_execute(task);
163 MSG_task_destroy(task);
165 if(transfer != MSG_OK){
166 if (transfer == MSG_HOST_FAILURE)
167 XBT_WARN("Transfer error, %s remote host just turned off!", MSG_host_get_name(attached_host));
168 if (transfer == MSG_TASK_CANCELED)
169 XBT_WARN("Transfer error, task has been canceled!");
174 /* Write file on local or remote host */
175 offset = simcall_file_tell(file_priv->simdata->smx_file);
176 write_size = simcall_file_write(file_priv->simdata->smx_file, size, attached_host);
177 file_priv->size = offset+write_size;
182 /** \ingroup msg_file_management
183 * \brief Opens the file whose name is the string pointed to by path
185 * \param fullpath is the file location on the storage
186 * \param data user data to attach to the file
188 * \return An #msg_file_t associated to the file
190 msg_file_t MSG_file_open(const char* fullpath, void* data)
193 msg_file_priv_t priv = xbt_new(s_msg_file_priv_t, 1);
195 priv->fullpath = xbt_strdup(fullpath);
196 priv->simdata = xbt_new0(s_simdata_file_t,1);
197 priv->simdata->smx_file = simcall_file_open(fullpath, MSG_host_self());
198 name = bprintf("%s:%i:%s",MSG_host_get_name(MSG_host_self()),MSG_process_self_PID(),fullpath);
199 xbt_lib_set(file_lib, name, MSG_FILE_LEVEL, priv);
200 msg_file_t fd = (msg_file_t) xbt_lib_get_elm_or_null(file_lib, name);
201 __MSG_file_get_info(fd);
206 /** \ingroup msg_file_management
207 * \brief Close the file
209 * \param fd is the file to close
210 * \return 0 on success or 1 on error
212 int MSG_file_close(msg_file_t fd)
215 msg_file_priv_t priv = MSG_file_priv(fd);
217 int res = simcall_file_close(priv->simdata->smx_file, MSG_host_self());
218 name = bprintf("%s:%i:%s",MSG_host_get_name(MSG_host_self()),MSG_process_self_PID(),priv->fullpath);
219 xbt_lib_unset(file_lib, name, MSG_FILE_LEVEL, 1);
224 /** \ingroup msg_file_management
225 * \brief Unlink the file pointed by fd
227 * \param fd is the file descriptor (#msg_file_t)
228 * \return 0 on success or 1 on error
230 msg_error_t MSG_file_unlink(msg_file_t fd)
232 msg_file_priv_t file_priv = MSG_file_priv(fd);
233 /* Find the host where the file is physically located (remote or local)*/
234 msg_storage_t storage_src =
235 (msg_storage_t) xbt_lib_get_elm_or_null(storage_lib,
236 file_priv->storageId);
237 msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
238 msg_host_t attached_host = MSG_get_host_by_name(storage_priv_src->hostname);
239 int res = simcall_file_unlink(file_priv->simdata->smx_file, attached_host);
243 /** \ingroup msg_file_management
244 * \brief Return the size of a file
246 * \param fd is the file descriptor (#msg_file_t)
247 * \return the size of the file (as a #sg_size_t)
249 sg_size_t MSG_file_get_size(msg_file_t fd){
250 msg_file_priv_t priv = MSG_file_priv(fd);
251 return simcall_file_get_size(priv->simdata->smx_file);
255 * \ingroup msg_file_management
256 * \brief Set the file position indicator in the msg_file_t by adding offset bytes
257 * to the position specified by origin (either SEEK_SET, SEEK_CUR, or SEEK_END).
259 * \param fd : file object that identifies the stream
260 * \param offset : number of bytes to offset from origin
261 * \param origin : Position used as reference for the offset. It is specified by
262 * one of the following constants defined in \<stdio.h\> exclusively to be used as
263 * arguments for this function (SEEK_SET = beginning of file, SEEK_CUR = current
264 * position of the file pointer, SEEK_END = end of file)
266 * \return If successful, the function returns MSG_OK (=0). Otherwise, it returns
267 * MSG_TASK_CANCELED (=8).
270 msg_error_t MSG_file_seek(msg_file_t fd, sg_offset_t offset, int origin)
272 msg_file_priv_t priv = MSG_file_priv(fd);
273 return simcall_file_seek(priv->simdata->smx_file, offset, origin);
277 * \ingroup msg_file_management
278 * \brief Returns the current value of the position indicator of the file
280 * \param fd : file object that identifies the stream
281 * \return On success, the current value of the position indicator is returned.
284 sg_size_t MSG_file_tell(msg_file_t fd)
286 msg_file_priv_t priv = MSG_file_priv(fd);
287 return simcall_file_tell(priv->simdata->smx_file);
290 const char *MSG_file_get_name(msg_file_t fd) {
291 xbt_assert((fd != NULL), "Invalid parameters");
292 msg_file_priv_t priv = MSG_file_priv(fd);
293 return priv->fullpath;
297 * \ingroup msg_file_management
298 * \brief Move a file to another location on the *same mount point*.
301 msg_error_t MSG_file_move (msg_file_t fd, const char* fullpath)
303 msg_file_priv_t priv = MSG_file_priv(fd);
304 return simcall_file_move(priv->simdata->smx_file, fullpath);
308 * \ingroup msg_file_management
309 * \brief Copy a file to another location on a remote host.
310 * \param file : the file to move
311 * \param host : the remote host where the file has to be copied
312 * \param fullpath : the complete path destination on the remote host
313 * \return If successful, the function returns MSG_OK. Otherwise, it returns
316 msg_error_t MSG_file_rcopy (msg_file_t file, msg_host_t host, const char* fullpath)
318 msg_file_priv_t file_priv = MSG_file_priv(file);
321 /* Find the host where the file is physically located and read it */
322 msg_storage_t storage_src =(msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId);
323 msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
324 msg_host_t attached_host = MSG_get_host_by_name(storage_priv_src->hostname);
325 MSG_file_seek(file, 0, SEEK_SET);
326 read_size = simcall_file_read(file_priv->simdata->smx_file, file_priv->size, attached_host);
328 /* Find the real host destination where the file will be physically stored */
329 xbt_dict_cursor_t cursor = NULL;
330 char *mount_name, *storage_name, *file_mount_name, *host_name_dest;
331 msg_storage_t storage_dest = NULL;
332 msg_host_t host_dest;
333 size_t longest_prefix_length = 0;
335 xbt_dict_t storage_list = simcall_host_get_mounted_storage_list(host);
336 xbt_dict_foreach(storage_list,cursor,mount_name,storage_name){
337 file_mount_name = (char *) xbt_malloc ((strlen(mount_name)+1));
338 strncpy(file_mount_name,fullpath,strlen(mount_name)+1);
339 file_mount_name[strlen(mount_name)] = '\0';
341 if(!strcmp(file_mount_name,mount_name) && strlen(mount_name)>longest_prefix_length){
342 /* The current mount name is found in the full path and is bigger than the previous*/
343 longest_prefix_length = strlen(mount_name);
344 storage_dest = (msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, storage_name);
346 free(file_mount_name);
348 xbt_dict_free(&storage_list);
350 if(longest_prefix_length>0){
351 /* Mount point found, retrieve the host the storage is attached to */
352 msg_storage_priv_t storage_dest_priv = MSG_storage_priv(storage_dest);
353 host_name_dest = (char*)storage_dest_priv->hostname;
354 host_dest = MSG_get_host_by_name(host_name_dest);
357 XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, SIMIX_host_get_name(host));
358 return MSG_TASK_CANCELED;
361 XBT_DEBUG("Initiate data transfer of %llu bytes between %s and %s.", read_size, storage_priv_src->hostname, host_name_dest);
362 msg_host_t *m_host_list = NULL;
363 m_host_list = calloc(2, sizeof(msg_host_t));
365 m_host_list[0] = attached_host;
366 m_host_list[1] = host_dest;
367 double computation_amount[] = { 0, 0 };
368 double communication_amount[] = { 0, (double)read_size, 0, 0 };
370 msg_task_t task = MSG_parallel_task_create("file transfer for write", 2, m_host_list, computation_amount, communication_amount, NULL);
371 msg_error_t transfer = MSG_parallel_task_execute(task);
372 MSG_task_destroy(task);
374 if(transfer != MSG_OK){
375 if (transfer == MSG_HOST_FAILURE)
376 XBT_WARN("Transfer error, %s remote host just turned off!", host_name_dest);
377 if (transfer == MSG_TASK_CANCELED)
378 XBT_WARN("Transfer error, task has been canceled!");
383 /* Create file on remote host, write it and close it */
384 smx_file_t smx_file = simcall_file_open(fullpath, host_dest);
385 simcall_file_write(smx_file, read_size, host_dest);
386 simcall_file_close(smx_file, host_dest);
392 * \ingroup msg_file_management
393 * \brief Move a file to another location on a remote host.
394 * \param file : the file to move
395 * \param host : the remote host where the file has to be moved
396 * \param fullpath : the complete path destination on the remote host
397 * \return If successful, the function returns MSG_OK. Otherwise, it returns
400 msg_error_t MSG_file_rmove (msg_file_t file, msg_host_t host, const char* fullpath)
402 msg_error_t res = MSG_file_rcopy(file, host, fullpath);
403 MSG_file_unlink(file);
408 * \brief Destroys a file (internal call only)
410 void __MSG_file_destroy(msg_file_priv_t file) {
411 xbt_free(file->fullpath);
412 xbt_free(file->simdata);
415 /********************************* Storage **************************************/
417 /** @addtogroup msg_storage_management
418 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Storages" --> \endhtmlonly
419 * (#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) {
441 /** \ingroup msg_storage_management
443 * \brief Returns the name of the #msg_storage_t.
445 * This functions checks whether a storage is a valid pointer or not and return its name.
447 const char *MSG_storage_get_name(msg_storage_t storage) {
448 xbt_assert((storage != NULL), "Invalid parameters");
449 return SIMIX_storage_get_name(storage);
452 /** \ingroup msg_storage_management
453 * \brief Returns the free space size of a storage element
454 * \param storage a storage
455 * \return the free space size of the storage element (as a #sg_size_t)
457 sg_size_t MSG_storage_get_free_size(msg_storage_t storage){
458 return simcall_storage_get_free_size(storage);
461 /** \ingroup msg_storage_management
462 * \brief Returns the used space size of a storage element
463 * \param storage a storage
464 * \return the used space size of the storage element (as a #sg_size_t)
466 sg_size_t MSG_storage_get_used_size(msg_storage_t storage){
467 return simcall_storage_get_used_size(storage);
470 /** \ingroup msg_storage_management
471 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this storage
472 * \param storage a storage
473 * \return a dict containing the properties
475 xbt_dict_t MSG_storage_get_properties(msg_storage_t storage)
477 xbt_assert((storage != NULL), "Invalid parameters (storage is NULL)");
478 return (simcall_storage_get_properties(storage));
481 /** \ingroup msg_storage_management
482 * \brief Change the value of a given storage property
484 * \param storage a storage
485 * \param name a property name
486 * \param value what to change the property to
487 * \param free_ctn the freeing function to use to kill the value on need
489 void MSG_storage_set_property_value(msg_storage_t storage, const char *name, char *value,void_f_pvoid_t free_ctn) {
490 xbt_dict_set(MSG_storage_get_properties(storage), name, value,free_ctn);
493 /** \ingroup msg_storage_management
494 * \brief Finds a msg_storage_t using its name.
495 * \param name the name of a storage
496 * \return the corresponding storage
498 msg_storage_t MSG_storage_get_by_name(const char *name)
500 return (msg_storage_t) xbt_lib_get_elm_or_null(storage_lib,name);
503 /** \ingroup msg_storage_management
504 * \brief Returns a dynar containing all the storage elements declared at a given point of time
507 xbt_dynar_t MSG_storages_as_dynar(void) {
509 xbt_lib_cursor_t cursor;
512 xbt_dynar_t res = xbt_dynar_new(sizeof(msg_storage_t),NULL);
514 xbt_lib_foreach(storage_lib, cursor, key, data) {
515 if(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), MSG_STORAGE_LEVEL) != NULL) {
516 xbt_dictelm_t elm = xbt_dict_cursor_get_elm(cursor);
517 xbt_dynar_push(res, &elm);
523 /** \ingroup msg_storage_management
525 * \brief Set the user data of a #msg_storage_t.
526 * This functions checks whether some data has already been associated to \a storage
527 or not and attach \a data to \a storage if it is possible.
529 msg_error_t MSG_storage_set_data(msg_storage_t storage, void *data)
531 msg_storage_priv_t priv = MSG_storage_priv(storage);
536 /** \ingroup msg_host_management
538 * \brief Returns the user data of a #msg_storage_t.
540 * This functions checks whether \a storage is a valid pointer or not and returns
541 the user data associated to \a storage if it is possible.
543 void *MSG_storage_get_data(msg_storage_t storage)
545 xbt_assert((storage != NULL), "Invalid parameters");
546 msg_storage_priv_t priv = MSG_storage_priv(storage);
550 /** \ingroup msg_storage_management
552 * \brief Returns the content (file list) of a #msg_storage_t.
553 * \param storage a storage
554 * \return The content of this storage element as a dict (full path file => size)
556 xbt_dict_t MSG_storage_get_content(msg_storage_t storage)
558 return SIMIX_storage_get_content(storage);
561 /** \ingroup msg_storage_management
563 * \brief Returns the size of a #msg_storage_t.
564 * \param storage a storage
565 * \return The size of the storage
567 sg_size_t MSG_storage_get_size(msg_storage_t storage)
569 return SIMIX_storage_get_size(storage);
572 /** \ingroup msg_storage_management
574 * \brief Returns the host name the storage is attached to
576 * This functions checks whether a storage is a valid pointer or not and return its name.
578 const char *MSG_storage_get_host(msg_storage_t storage) {
579 xbt_assert((storage != NULL), "Invalid parameters");
580 msg_storage_priv_t priv = MSG_storage_priv(storage);
581 return priv->hostname;