1 /* Copyright (c) 2004-2015. 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.
22 /********************************* File **************************************/
23 void __MSG_file_get_info(msg_file_t fd){
25 msg_file_priv_t priv = MSG_file_priv(fd);
26 xbt_dynar_t info = simcall_file_get_info(priv->simdata->smx_file);
29 priv->content_type = xbt_dynar_pop_as(info, char *);
30 priv->storage_type = xbt_dynar_pop_as(info, char *);
31 priv->storageId = xbt_dynar_pop_as(info, char *);
32 priv->mount_point = xbt_dynar_pop_as(info, char *);
33 psize = xbt_dynar_pop_as(info, sg_size_t*);
36 xbt_dynar_free_container(&info);
39 /** \ingroup msg_file_management
41 * \brief Set the user data of a #msg_file_t.
43 * This functions checks whether some data has already been associated to \a file
44 or not and attach \a data to \a file if it is possible.
46 msg_error_t MSG_file_set_data(msg_file_t fd, void *data)
48 msg_file_priv_t priv = MSG_file_priv(fd);
53 /** \ingroup msg_file_management
55 * \brief Return the user data of a #msg_file_t.
57 * This functions checks whether \a file is a valid pointer or not and return
58 the user data associated to \a file if it is possible.
60 void *MSG_file_get_data(msg_file_t fd)
62 msg_file_priv_t priv = MSG_file_priv(fd);
66 /** \ingroup msg_file_management
67 * \brief Display information related to a file descriptor
69 * \param fd is a the file descriptor
72 void MSG_file_dump (msg_file_t fd){
73 /* Update the cached information first */
74 __MSG_file_get_info(fd);
76 msg_file_priv_t priv = MSG_file_priv(fd);
77 XBT_INFO("File Descriptor information:\n"
78 "\t\tFull path: '%s'\n"
80 "\t\tMount point: '%s'\n"
81 "\t\tStorage Id: '%s'\n"
82 "\t\tStorage Type: '%s'\n"
83 "\t\tContent Type: '%s'\n"
84 "\t\tFile Descriptor Id: %d",
85 priv->fullpath, priv->size, priv->mount_point,
86 priv->storageId, priv->storage_type,
87 priv->content_type, priv->desc_id);
90 /** \ingroup msg_file_management
91 * \brief Read a file (local or remote)
93 * \param size of the file to read
94 * \param fd is a the file descriptor
95 * \return the number of bytes successfully read or -1 if an error occurred
97 sg_size_t MSG_file_read(msg_file_t fd, sg_size_t size)
99 msg_file_priv_t file_priv = MSG_file_priv(fd);
102 /* Find the host where the file is physically located and read it */
103 msg_storage_t storage_src =(msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId);
104 msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
105 msg_host_t attached_host = MSG_host_by_name(storage_priv_src->hostname);
106 read_size = simcall_file_read(file_priv->simdata->smx_file, size, attached_host);
108 if(strcmp(storage_priv_src->hostname, MSG_host_get_name(MSG_host_self()))){
109 /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
110 XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", storage_priv_src->hostname, read_size);
111 msg_host_t *m_host_list = NULL;
112 m_host_list = calloc(2, sizeof(msg_host_t));
114 m_host_list[0] = MSG_host_self();
115 m_host_list[1] = attached_host;
116 double flops_amount[] = { 0, 0 };
117 double bytes_amount[] = { 0, 0, (double)read_size, 0 };
119 msg_task_t task = MSG_parallel_task_create("file transfer for read", 2, m_host_list, flops_amount, bytes_amount, NULL);
120 msg_error_t transfer = MSG_parallel_task_execute(task);
121 MSG_task_destroy(task);
123 if(transfer != MSG_OK){
124 if (transfer == MSG_HOST_FAILURE)
125 XBT_WARN("Transfer error, %s remote host just turned off!", MSG_host_get_name(attached_host));
126 if (transfer == MSG_TASK_CANCELED)
127 XBT_WARN("Transfer error, task has been canceled!");
135 /** \ingroup msg_file_management
136 * \brief Write into a file (local or remote)
138 * \param size of the file to write
139 * \param fd is a the file descriptor
140 * \return the number of bytes successfully write or -1 if an error occurred
142 sg_size_t MSG_file_write(msg_file_t fd, sg_size_t size)
144 msg_file_priv_t file_priv = MSG_file_priv(fd);
145 sg_size_t write_size, offset;
147 /* Find the host where the file is physically located (remote or local)*/
148 msg_storage_t storage_src =(msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId);
149 msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
150 msg_host_t attached_host = MSG_host_by_name(storage_priv_src->hostname);
152 if(strcmp(storage_priv_src->hostname, MSG_host_get_name(MSG_host_self()))){
153 /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
154 XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", storage_priv_src->hostname, size);
155 msg_host_t *m_host_list = NULL;
156 m_host_list = calloc(2, sizeof(msg_host_t));
158 m_host_list[0] = MSG_host_self();
159 m_host_list[1] = attached_host;
160 double flops_amount[] = { 0, 0 };
161 double bytes_amount[] = { 0, (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, NULL);
164 msg_error_t transfer = MSG_parallel_task_execute(task);
165 MSG_task_destroy(task);
167 if(transfer != MSG_OK){
168 if (transfer == MSG_HOST_FAILURE)
169 XBT_WARN("Transfer error, %s remote host just turned off!", MSG_host_get_name(attached_host));
170 if (transfer == MSG_TASK_CANCELED)
171 XBT_WARN("Transfer error, task has been canceled!");
176 /* Write file on local or remote host */
177 offset = simcall_file_tell(file_priv->simdata->smx_file);
178 write_size = simcall_file_write(file_priv->simdata->smx_file, size, attached_host);
179 file_priv->size = offset+write_size;
184 /** \ingroup msg_file_management
185 * \brief Opens the file whose name is the string pointed to by path
187 * \param fullpath is the file location on the storage
188 * \param data user data to attach to the file
190 * \return An #msg_file_t associated to the file
192 msg_file_t MSG_file_open(const char* fullpath, void* data)
195 msg_file_priv_t priv = xbt_new(s_msg_file_priv_t, 1);
197 priv->fullpath = xbt_strdup(fullpath);
198 priv->simdata = xbt_new0(s_simdata_file_t,1);
199 priv->simdata->smx_file = simcall_file_open(fullpath, MSG_host_self());
200 priv->desc_id = __MSG_host_get_file_descriptor_id(MSG_host_self());
202 name = bprintf("%s:%s:%d", priv->fullpath, MSG_host_get_name(MSG_host_self()),
205 xbt_lib_set(file_lib, name, MSG_FILE_LEVEL, priv);
206 msg_file_t fd = (msg_file_t) xbt_lib_get_elm_or_null(file_lib, name);
207 __MSG_file_get_info(fd);
213 /** \ingroup msg_file_management
214 * \brief Close the file
216 * \param fd is the file to close
217 * \return 0 on success or 1 on error
219 int MSG_file_close(msg_file_t fd)
222 msg_file_priv_t priv = MSG_file_priv(fd);
224 xbt_free(priv->data);
226 int res = simcall_file_close(priv->simdata->smx_file, MSG_host_self());
227 name = bprintf("%s:%s:%d", priv->fullpath, MSG_host_get_name(MSG_host_self()),
229 __MSG_host_release_file_descriptor_id(MSG_host_self(), priv->desc_id);
230 xbt_lib_unset(file_lib, name, MSG_FILE_LEVEL, 1);
235 /** \ingroup msg_file_management
236 * \brief Unlink the file pointed by fd
238 * \param fd is the file descriptor (#msg_file_t)
239 * \return 0 on success or 1 on error
241 msg_error_t MSG_file_unlink(msg_file_t fd)
243 msg_file_priv_t file_priv = MSG_file_priv(fd);
244 /* Find the host where the file is physically located (remote or local)*/
245 msg_storage_t storage_src =
246 (msg_storage_t) xbt_lib_get_elm_or_null(storage_lib,
247 file_priv->storageId);
248 msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
249 msg_host_t attached_host = MSG_host_by_name(storage_priv_src->hostname);
250 int res = simcall_file_unlink(file_priv->simdata->smx_file, attached_host);
254 /** \ingroup msg_file_management
255 * \brief Return the size of a file
257 * \param fd is the file descriptor (#msg_file_t)
258 * \return the size of the file (as a #sg_size_t)
260 sg_size_t MSG_file_get_size(msg_file_t fd){
261 msg_file_priv_t priv = MSG_file_priv(fd);
262 return simcall_file_get_size(priv->simdata->smx_file);
266 * \ingroup msg_file_management
267 * \brief Set the file position indicator in the msg_file_t by adding offset bytes
268 * to the position specified by origin (either SEEK_SET, SEEK_CUR, or SEEK_END).
270 * \param fd : file object that identifies the stream
271 * \param offset : number of bytes to offset from origin
272 * \param origin : Position used as reference for the offset. It is specified by
273 * one of the following constants defined in \<stdio.h\> exclusively to be used as
274 * arguments for this function (SEEK_SET = beginning of file, SEEK_CUR = current
275 * position of the file pointer, SEEK_END = end of file)
277 * \return If successful, the function returns MSG_OK (=0). Otherwise, it returns
278 * MSG_TASK_CANCELED (=8).
281 msg_error_t MSG_file_seek(msg_file_t fd, sg_offset_t offset, int origin)
283 msg_file_priv_t priv = MSG_file_priv(fd);
284 return simcall_file_seek(priv->simdata->smx_file, offset, origin);
288 * \ingroup msg_file_management
289 * \brief Returns the current value of the position indicator of the file
291 * \param fd : file object that identifies the stream
292 * \return On success, the current value of the position indicator is returned.
295 sg_size_t MSG_file_tell(msg_file_t fd)
297 msg_file_priv_t priv = MSG_file_priv(fd);
298 return simcall_file_tell(priv->simdata->smx_file);
301 const char *MSG_file_get_name(msg_file_t fd) {
302 xbt_assert((fd != NULL), "Invalid parameters");
303 msg_file_priv_t priv = MSG_file_priv(fd);
304 return priv->fullpath;
308 * \ingroup msg_file_management
309 * \brief Move a file to another location on the *same mount point*.
312 msg_error_t MSG_file_move (msg_file_t fd, const char* fullpath)
314 msg_file_priv_t priv = MSG_file_priv(fd);
315 return simcall_file_move(priv->simdata->smx_file, fullpath);
319 * \ingroup msg_file_management
320 * \brief Copy a file to another location on a remote host.
321 * \param file : the file to move
322 * \param host : the remote host where the file has to be copied
323 * \param fullpath : the complete path destination on the remote host
324 * \return If successful, the function returns MSG_OK. Otherwise, it returns
327 msg_error_t MSG_file_rcopy (msg_file_t file, msg_host_t host, const char* fullpath)
329 msg_file_priv_t file_priv = MSG_file_priv(file);
332 /* Find the host where the file is physically located and read it */
333 msg_storage_t storage_src =(msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId);
334 msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
335 msg_host_t attached_host = MSG_host_by_name(storage_priv_src->hostname);
336 MSG_file_seek(file, 0, SEEK_SET);
337 read_size = simcall_file_read(file_priv->simdata->smx_file, file_priv->size, attached_host);
339 /* Find the real host destination where the file will be physically stored */
340 xbt_dict_cursor_t cursor = NULL;
341 char *mount_name, *storage_name, *file_mount_name, *host_name_dest;
342 msg_storage_t storage_dest = NULL;
343 msg_host_t host_dest;
344 size_t longest_prefix_length = 0;
346 xbt_dict_t storage_list = simcall_host_get_mounted_storage_list(host);
347 xbt_dict_foreach(storage_list,cursor,mount_name,storage_name){
348 file_mount_name = (char *) xbt_malloc ((strlen(mount_name)+1));
349 strncpy(file_mount_name,fullpath,strlen(mount_name)+1);
350 file_mount_name[strlen(mount_name)] = '\0';
352 if(!strcmp(file_mount_name,mount_name) && strlen(mount_name)>longest_prefix_length){
353 /* The current mount name is found in the full path and is bigger than the previous*/
354 longest_prefix_length = strlen(mount_name);
355 storage_dest = (msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, storage_name);
357 free(file_mount_name);
359 xbt_dict_free(&storage_list);
361 if(longest_prefix_length>0){
362 /* Mount point found, retrieve the host the storage is attached to */
363 msg_storage_priv_t storage_dest_priv = MSG_storage_priv(storage_dest);
364 host_name_dest = (char*)storage_dest_priv->hostname;
365 host_dest = MSG_host_by_name(host_name_dest);
368 XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, SIMIX_host_get_name(host));
369 return MSG_TASK_CANCELED;
372 XBT_DEBUG("Initiate data transfer of %llu bytes between %s and %s.", read_size, storage_priv_src->hostname, host_name_dest);
373 msg_host_t *m_host_list = NULL;
374 m_host_list = calloc(2, sizeof(msg_host_t));
376 m_host_list[0] = attached_host;
377 m_host_list[1] = host_dest;
378 double flops_amount[] = { 0, 0 };
379 double bytes_amount[] = { 0, (double)read_size, 0, 0 };
381 msg_task_t task = MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount, NULL);
382 msg_error_t transfer = MSG_parallel_task_execute(task);
383 MSG_task_destroy(task);
385 if(transfer != MSG_OK){
386 if (transfer == MSG_HOST_FAILURE)
387 XBT_WARN("Transfer error, %s remote host just turned off!", host_name_dest);
388 if (transfer == MSG_TASK_CANCELED)
389 XBT_WARN("Transfer error, task has been canceled!");
394 /* Create file on remote host, write it and close it */
395 smx_file_t smx_file = simcall_file_open(fullpath, host_dest);
396 simcall_file_write(smx_file, read_size, host_dest);
397 simcall_file_close(smx_file, host_dest);
403 * \ingroup msg_file_management
404 * \brief Move a file to another location on a remote host.
405 * \param file : the file to move
406 * \param host : the remote host where the file has to be moved
407 * \param fullpath : the complete path destination on the remote host
408 * \return If successful, the function returns MSG_OK. Otherwise, it returns
411 msg_error_t MSG_file_rmove (msg_file_t file, msg_host_t host, const char* fullpath)
413 msg_error_t res = MSG_file_rcopy(file, host, fullpath);
414 MSG_file_unlink(file);
419 * \brief Destroys a file (internal call only)
421 void __MSG_file_destroy(msg_file_priv_t file) {
422 xbt_free(file->fullpath);
423 xbt_free(file->simdata);
426 /********************************* Storage **************************************/
428 /** @addtogroup msg_storage_management
429 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Storages" --> \endhtmlonly
430 * (#msg_storage_t) and the functions for managing it.
434 msg_storage_t __MSG_storage_create(smx_storage_t storage)
436 const char *name = SIMIX_storage_get_name(storage);
437 const char *host = SIMIX_storage_get_host(storage);
438 msg_storage_priv_t storage_private = xbt_new0(s_msg_storage_priv_t, 1);
439 storage_private->hostname = host;
440 xbt_lib_set(storage_lib,name,MSG_STORAGE_LEVEL,storage_private);
441 return xbt_lib_get_elm_or_null(storage_lib, name);
445 * \brief Destroys a storage (internal call only)
447 void __MSG_storage_destroy(msg_storage_priv_t storage) {
452 /** \ingroup msg_storage_management
454 * \brief Returns the name of the #msg_storage_t.
456 * This functions checks whether a storage is a valid pointer or not and return its name.
458 const char *MSG_storage_get_name(msg_storage_t storage) {
459 xbt_assert((storage != NULL), "Invalid parameters");
460 return SIMIX_storage_get_name(storage);
463 /** \ingroup msg_storage_management
464 * \brief Returns the free space size of a storage element
465 * \param storage a storage
466 * \return the free space size of the storage element (as a #sg_size_t)
468 sg_size_t MSG_storage_get_free_size(msg_storage_t storage){
469 return simcall_storage_get_free_size(storage);
472 /** \ingroup msg_storage_management
473 * \brief Returns the used space size of a storage element
474 * \param storage a storage
475 * \return the used space size of the storage element (as a #sg_size_t)
477 sg_size_t MSG_storage_get_used_size(msg_storage_t storage){
478 return simcall_storage_get_used_size(storage);
481 /** \ingroup msg_storage_management
482 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this storage
483 * \param storage a storage
484 * \return a dict containing the properties
486 xbt_dict_t MSG_storage_get_properties(msg_storage_t storage)
488 xbt_assert((storage != NULL), "Invalid parameters (storage is NULL)");
489 return (simcall_storage_get_properties(storage));
492 /** \ingroup msg_storage_management
493 * \brief Change the value of a given storage property
495 * \param storage a storage
496 * \param name a property name
497 * \param value what to change the property to
498 * \param free_ctn the freeing function to use to kill the value on need
500 void MSG_storage_set_property_value(msg_storage_t storage, const char *name, char *value,void_f_pvoid_t free_ctn) {
501 xbt_dict_set(MSG_storage_get_properties(storage), name, value,free_ctn);
504 /** \ingroup m_storage_management
505 * \brief Returns the value of a given storage property
507 * \param storage a storage
508 * \param name a property name
509 * \return value of a property (or NULL if property not set)
511 const char *MSG_storage_get_property_value(msg_storage_t storage, const char *name)
513 return xbt_dict_get_or_null(MSG_storage_get_properties(storage), name);
517 /** \ingroup msg_storage_management
518 * \brief Finds a msg_storage_t using its name.
519 * \param name the name of a storage
520 * \return the corresponding storage
522 msg_storage_t MSG_storage_get_by_name(const char *name)
524 return (msg_storage_t) xbt_lib_get_elm_or_null(storage_lib,name);
527 /** \ingroup msg_storage_management
528 * \brief Returns a dynar containing all the storage elements declared at a given point of time
531 xbt_dynar_t MSG_storages_as_dynar(void) {
533 xbt_lib_cursor_t cursor;
536 xbt_dynar_t res = xbt_dynar_new(sizeof(msg_storage_t),NULL);
538 xbt_lib_foreach(storage_lib, cursor, key, data) {
539 if(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), MSG_STORAGE_LEVEL) != NULL) {
540 xbt_dictelm_t elm = xbt_dict_cursor_get_elm(cursor);
541 xbt_dynar_push(res, &elm);
547 /** \ingroup msg_storage_management
549 * \brief Set the user data of a #msg_storage_t.
550 * This functions checks whether some data has already been associated to \a storage
551 or not and attach \a data to \a storage if it is possible.
553 msg_error_t MSG_storage_set_data(msg_storage_t storage, void *data)
555 msg_storage_priv_t priv = MSG_storage_priv(storage);
560 /** \ingroup m_host_management
562 * \brief Returns the user data of a #msg_storage_t.
564 * This functions checks whether \a storage is a valid pointer or not and returns
565 the user data associated to \a storage if it is possible.
567 void *MSG_storage_get_data(msg_storage_t storage)
569 xbt_assert((storage != NULL), "Invalid parameters");
570 msg_storage_priv_t priv = MSG_storage_priv(storage);
574 /** \ingroup msg_storage_management
576 * \brief Returns the content (file list) of a #msg_storage_t.
577 * \param storage a storage
578 * \return The content of this storage element as a dict (full path file => size)
580 xbt_dict_t MSG_storage_get_content(msg_storage_t storage)
582 return SIMIX_storage_get_content(storage);
585 /** \ingroup msg_storage_management
587 * \brief Returns the size of a #msg_storage_t.
588 * \param storage a storage
589 * \return The size of the storage
591 sg_size_t MSG_storage_get_size(msg_storage_t storage)
593 return SIMIX_storage_get_size(storage);
596 /** \ingroup msg_storage_management
598 * \brief Returns the host name the storage is attached to
600 * This functions checks whether a storage is a valid pointer or not and return its name.
602 const char *MSG_storage_get_host(msg_storage_t storage) {
603 xbt_assert((storage != NULL), "Invalid parameters");
604 msg_storage_priv_t priv = MSG_storage_priv(storage);
605 return priv->hostname;