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"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_io, msg, "Logging specific to MSG (io)");
12 /** @addtogroup msg_file
13 * (#msg_file_t) and the functions for managing it.
18 /********************************* File **************************************/
19 void __MSG_file_get_info(msg_file_t fd){
21 msg_file_priv_t priv = MSG_file_priv(fd);
22 xbt_dynar_t info = simcall_file_get_info(priv->simdata->smx_file);
25 priv->content_type = xbt_dynar_pop_as(info, char *);
26 priv->storage_type = xbt_dynar_pop_as(info, char *);
27 priv->storageId = xbt_dynar_pop_as(info, char *);
28 priv->mount_point = xbt_dynar_pop_as(info, char *);
29 psize = xbt_dynar_pop_as(info, sg_size_t*);
32 xbt_dynar_free_container(&info);
37 * \brief Set the user data of a #msg_file_t.
39 * This functions attach \a data to \a file.
41 msg_error_t MSG_file_set_data(msg_file_t fd, void *data)
43 msg_file_priv_t priv = MSG_file_priv(fd);
50 * \brief Return the user data of a #msg_file_t.
52 * This functions checks whether \a file is a valid pointer and return the user data associated to \a file if possible.
54 void *MSG_file_get_data(msg_file_t fd)
56 msg_file_priv_t priv = MSG_file_priv(fd);
61 * \brief Display information related to a file descriptor
63 * \param fd is a the file descriptor
65 void MSG_file_dump (msg_file_t fd){
66 /* Update the cached information first */
67 __MSG_file_get_info(fd);
69 msg_file_priv_t priv = MSG_file_priv(fd);
70 XBT_INFO("File Descriptor information:\n"
71 "\t\tFull path: '%s'\n"
73 "\t\tMount point: '%s'\n"
74 "\t\tStorage Id: '%s'\n"
75 "\t\tStorage Type: '%s'\n"
76 "\t\tContent Type: '%s'\n"
77 "\t\tFile Descriptor Id: %d",
78 priv->fullpath, priv->size, priv->mount_point,
79 priv->storageId, priv->storage_type,
80 priv->content_type, priv->desc_id);
84 * \brief Read a file (local or remote)
86 * \param size of the file to read
87 * \param fd is a the file descriptor
88 * \return the number of bytes successfully read or -1 if an error occurred
90 sg_size_t MSG_file_read(msg_file_t fd, sg_size_t size)
92 msg_file_priv_t file_priv = MSG_file_priv(fd);
95 /* Find the host where the file is physically located and read it */
96 msg_storage_t storage_src =(msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId);
97 msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
98 msg_host_t attached_host = MSG_host_by_name(storage_priv_src->hostname);
99 read_size = simcall_file_read(file_priv->simdata->smx_file, size, attached_host);
101 if(strcmp(storage_priv_src->hostname, MSG_host_get_name(MSG_host_self()))){
102 /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
103 XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", storage_priv_src->hostname, read_size);
104 msg_host_t *m_host_list = nullptr;
105 m_host_list = (msg_host_t*) calloc(2, sizeof(msg_host_t));
107 m_host_list[0] = MSG_host_self();
108 m_host_list[1] = attached_host;
109 double flops_amount[] = { 0, 0 };
110 double bytes_amount[] = { 0, 0, (double)read_size, 0 };
112 msg_task_t task = MSG_parallel_task_create("file transfer for read", 2, m_host_list, flops_amount, bytes_amount,
114 msg_error_t transfer = MSG_parallel_task_execute(task);
115 MSG_task_destroy(task);
117 if(transfer != MSG_OK){
118 if (transfer == MSG_HOST_FAILURE)
119 XBT_WARN("Transfer error, %s remote host just turned off!", MSG_host_get_name(attached_host));
120 if (transfer == MSG_TASK_CANCELED)
121 XBT_WARN("Transfer error, task has been canceled!");
129 /** \ingroup msg_file
130 * \brief Write into a file (local or remote)
132 * \param size of the file to write
133 * \param fd is a the file descriptor
134 * \return the number of bytes successfully write or -1 if an error occurred
136 sg_size_t MSG_file_write(msg_file_t fd, sg_size_t size)
138 msg_file_priv_t file_priv = MSG_file_priv(fd);
140 /* Find the host where the file is physically located (remote or local)*/
141 msg_storage_t storage_src =(msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId);
142 msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
143 msg_host_t attached_host = MSG_host_by_name(storage_priv_src->hostname);
145 if(strcmp(storage_priv_src->hostname, MSG_host_get_name(MSG_host_self()))){
146 /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
147 XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", storage_priv_src->hostname, size);
148 msg_host_t *m_host_list = nullptr;
149 m_host_list = (msg_host_t*) calloc(2, sizeof(msg_host_t));
151 m_host_list[0] = MSG_host_self();
152 m_host_list[1] = attached_host;
153 double flops_amount[] = { 0, 0 };
154 double bytes_amount[] = { 0, (double)size, 0, 0 };
156 msg_task_t task = MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount,
158 msg_error_t transfer = MSG_parallel_task_execute(task);
159 MSG_task_destroy(task);
161 if(transfer != MSG_OK){
162 if (transfer == MSG_HOST_FAILURE)
163 XBT_WARN("Transfer error, %s remote host just turned off!", MSG_host_get_name(attached_host));
164 if (transfer == MSG_TASK_CANCELED)
165 XBT_WARN("Transfer error, task has been canceled!");
170 /* Write file on local or remote host */
171 sg_size_t offset = simcall_file_tell(file_priv->simdata->smx_file);
172 sg_size_t write_size = simcall_file_write(file_priv->simdata->smx_file, size, attached_host);
173 file_priv->size = offset+write_size;
178 /** \ingroup msg_file
179 * \brief Opens the file whose name is the string pointed to by path
181 * \param fullpath is the file location on the storage
182 * \param data user data to attach to the file
184 * \return An #msg_file_t associated to the file
186 msg_file_t MSG_file_open(const char* fullpath, void* data)
189 msg_file_priv_t priv = xbt_new(s_msg_file_priv_t, 1);
191 priv->fullpath = xbt_strdup(fullpath);
192 priv->simdata = xbt_new0(s_simdata_file_t,1);
193 priv->simdata->smx_file = simcall_file_open(fullpath, MSG_host_self());
194 priv->desc_id = __MSG_host_get_file_descriptor_id(MSG_host_self());
196 name = bprintf("%s:%s:%d", priv->fullpath, MSG_host_get_name(MSG_host_self()), priv->desc_id);
198 xbt_lib_set(file_lib, name, MSG_FILE_LEVEL, priv);
199 msg_file_t fd = (msg_file_t) xbt_lib_get_elm_or_null(file_lib, name);
200 __MSG_file_get_info(fd);
206 /** \ingroup msg_file
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 xbt_free(priv->data);
219 int res = simcall_file_close(priv->simdata->smx_file, MSG_host_self());
220 name = bprintf("%s:%s:%d", priv->fullpath, MSG_host_get_name(MSG_host_self()), priv->desc_id);
221 __MSG_host_release_file_descriptor_id(MSG_host_self(), priv->desc_id);
222 xbt_lib_unset(file_lib, name, MSG_FILE_LEVEL, 1);
227 /** \ingroup msg_file
228 * \brief Unlink the file pointed by fd
230 * \param fd is the file descriptor (#msg_file_t)
231 * \return 0 on success or 1 on error
233 msg_error_t MSG_file_unlink(msg_file_t fd)
235 msg_file_priv_t file_priv = MSG_file_priv(fd);
236 /* Find the host where the file is physically located (remote or local)*/
237 msg_storage_t storage_src =
238 (msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId);
239 msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
240 msg_host_t attached_host = MSG_host_by_name(storage_priv_src->hostname);
241 int res = simcall_file_unlink(file_priv->simdata->smx_file, attached_host);
242 return (msg_error_t) res;
245 /** \ingroup msg_file
246 * \brief Return the size of a file
248 * \param fd is the file descriptor (#msg_file_t)
249 * \return the size of the file (as a #sg_size_t)
251 sg_size_t MSG_file_get_size(msg_file_t fd){
252 msg_file_priv_t priv = MSG_file_priv(fd);
253 return simcall_file_get_size(priv->simdata->smx_file);
258 * \brief Set the file position indicator in the msg_file_t by adding offset bytes
259 * to the position specified by origin (either SEEK_SET, SEEK_CUR, or SEEK_END).
261 * \param fd : file object that identifies the stream
262 * \param offset : number of bytes to offset from origin
263 * \param origin : Position used as reference for the offset. It is specified by one of the following constants defined
264 * in \<stdio.h\> exclusively to be used as arguments for this function (SEEK_SET = beginning of file,
265 * SEEK_CUR = current position of the file pointer, SEEK_END = end of file)
266 * \return If successful, the function returns MSG_OK (=0). Otherwise, it returns MSG_TASK_CANCELED (=8).
268 msg_error_t MSG_file_seek(msg_file_t fd, sg_offset_t offset, int origin)
270 msg_file_priv_t priv = MSG_file_priv(fd);
271 return (msg_error_t) simcall_file_seek(priv->simdata->smx_file, offset, origin);
276 * \brief Returns the current value of the position indicator of the file
278 * \param fd : file object that identifies the stream
279 * \return On success, the current value of the position indicator is returned.
282 sg_size_t MSG_file_tell(msg_file_t fd)
284 msg_file_priv_t priv = MSG_file_priv(fd);
285 return simcall_file_tell(priv->simdata->smx_file);
288 const char *MSG_file_get_name(msg_file_t fd) {
289 xbt_assert((fd != nullptr), "Invalid parameters");
290 msg_file_priv_t priv = MSG_file_priv(fd);
291 return priv->fullpath;
296 * \brief Move a file to another location on the *same mount point*.
299 msg_error_t MSG_file_move (msg_file_t fd, const char* fullpath)
301 msg_file_priv_t priv = MSG_file_priv(fd);
302 return (msg_error_t) simcall_file_move(priv->simdata->smx_file, fullpath);
307 * \brief Copy a file to another location on a remote host.
308 * \param file : the file to move
309 * \param host : the remote host where the file has to be copied
310 * \param fullpath : the complete path destination on the remote host
311 * \return If successful, the function returns MSG_OK. Otherwise, it returns MSG_TASK_CANCELED.
313 msg_error_t MSG_file_rcopy (msg_file_t file, msg_host_t host, const char* fullpath)
315 msg_file_priv_t file_priv = MSG_file_priv(file);
318 /* Find the host where the file is physically located and read it */
319 msg_storage_t storage_src =(msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId);
320 msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
321 msg_host_t attached_host = MSG_host_by_name(storage_priv_src->hostname);
322 MSG_file_seek(file, 0, SEEK_SET);
323 read_size = simcall_file_read(file_priv->simdata->smx_file, file_priv->size, attached_host);
325 /* Find the real host destination where the file will be physically stored */
326 xbt_dict_cursor_t cursor = nullptr;
327 msg_storage_t storage_dest = nullptr;
328 msg_host_t host_dest;
329 size_t longest_prefix_length = 0;
331 xbt_dict_t storage_list = host->mountedStoragesAsDict();
332 char *mount_name, *storage_name;
333 xbt_dict_foreach(storage_list,cursor,mount_name,storage_name){
334 char* file_mount_name = (char *) xbt_malloc ((strlen(mount_name)+1));
335 strncpy(file_mount_name,fullpath,strlen(mount_name)+1);
336 file_mount_name[strlen(mount_name)] = '\0';
338 if(!strcmp(file_mount_name,mount_name) && strlen(mount_name)>longest_prefix_length){
339 /* The current mount name is found in the full path and is bigger than the previous*/
340 longest_prefix_length = strlen(mount_name);
341 storage_dest = (msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, storage_name);
343 free(file_mount_name);
345 xbt_dict_free(&storage_list);
347 char* host_name_dest = nullptr;
348 if(longest_prefix_length>0){
349 /* Mount point found, retrieve the host the storage is attached to */
350 msg_storage_priv_t storage_dest_priv = MSG_storage_priv(storage_dest);
351 host_name_dest = (char*)storage_dest_priv->hostname;
352 host_dest = MSG_host_by_name(host_name_dest);
354 XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, sg_host_get_name(host));
355 return MSG_TASK_CANCELED;
358 XBT_DEBUG("Initiate data transfer of %llu bytes between %s and %s.", read_size, storage_priv_src->hostname,
360 msg_host_t *m_host_list = nullptr;
361 m_host_list = (msg_host_t*) calloc(2, sizeof(msg_host_t));
363 m_host_list[0] = attached_host;
364 m_host_list[1] = host_dest;
365 double flops_amount[] = { 0, 0 };
366 double bytes_amount[] = { 0, (double)read_size, 0, 0 };
369 MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount, nullptr);
370 msg_error_t transfer = MSG_parallel_task_execute(task);
371 MSG_task_destroy(task);
373 if(transfer != MSG_OK){
374 if (transfer == MSG_HOST_FAILURE)
375 XBT_WARN("Transfer error, %s remote host just turned off!", host_name_dest);
376 if (transfer == MSG_TASK_CANCELED)
377 XBT_WARN("Transfer error, task has been canceled!");
379 return (msg_error_t) -1;
382 /* Create file on remote host, write it and close it */
383 smx_file_t smx_file = simcall_file_open(fullpath, host_dest);
384 simcall_file_write(smx_file, read_size, host_dest);
385 simcall_file_close(smx_file, host_dest);
391 * \brief Move a file to another location on a remote host.
392 * \param file : the file to move
393 * \param host : the remote host where the file has to be moved
394 * \param fullpath : the complete path destination on the remote host
395 * \return If successful, the function returns MSG_OK. Otherwise, it returns MSG_TASK_CANCELED.
397 msg_error_t MSG_file_rmove (msg_file_t file, msg_host_t host, const char* fullpath)
399 msg_error_t res = MSG_file_rcopy(file, host, fullpath);
400 MSG_file_unlink(file);
405 * \brief Destroys a file (internal call only)
407 void __MSG_file_destroy(msg_file_priv_t file) {
408 xbt_free(file->fullpath);
409 xbt_free(file->simdata);
413 /********************************* Storage **************************************/
414 /** @addtogroup msg_storage_management
415 * (#msg_storage_t) and the functions for managing it.
418 msg_storage_t __MSG_storage_create(smx_storage_t storage)
420 const char *name = SIMIX_storage_get_name(storage);
421 const char *host = SIMIX_storage_get_host(storage);
422 msg_storage_priv_t storage_private = xbt_new0(s_msg_storage_priv_t, 1);
423 storage_private->hostname = host;
424 xbt_lib_set(storage_lib,name,MSG_STORAGE_LEVEL,storage_private);
425 return xbt_lib_get_elm_or_null(storage_lib, name);
429 * \brief Destroys a storage (internal call only)
431 void __MSG_storage_destroy(msg_storage_priv_t storage) {
435 /** \ingroup msg_storage_management
437 * \brief Returns the name of the #msg_storage_t.
439 * This functions checks whether a storage is a valid pointer or not and return its name.
441 const char *MSG_storage_get_name(msg_storage_t storage) {
442 xbt_assert((storage != nullptr), "Invalid parameters");
443 return SIMIX_storage_get_name(storage);
446 /** \ingroup msg_storage_management
447 * \brief Returns the free space size of a storage element
448 * \param storage a storage
449 * \return the free space size of the storage element (as a #sg_size_t)
451 sg_size_t MSG_storage_get_free_size(msg_storage_t storage){
452 return simcall_storage_get_free_size(storage);
455 /** \ingroup msg_storage_management
456 * \brief Returns the used space size of a storage element
457 * \param storage a storage
458 * \return the used space size of the storage element (as a #sg_size_t)
460 sg_size_t MSG_storage_get_used_size(msg_storage_t storage){
461 return simcall_storage_get_used_size(storage);
464 /** \ingroup msg_storage_management
465 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this storage
466 * \param storage a storage
467 * \return a dict containing the properties
469 xbt_dict_t MSG_storage_get_properties(msg_storage_t storage)
471 xbt_assert((storage != nullptr), "Invalid parameters (storage is nullptr)");
472 return (simcall_storage_get_properties(storage));
475 /** \ingroup msg_storage_management
476 * \brief Change the value of a given storage property
478 * \param storage a storage
479 * \param name a property name
480 * \param value what to change the property to
481 * \param free_ctn the freeing function to use to kill the value on need
483 void MSG_storage_set_property_value(msg_storage_t storage, const char *name, char *value,void_f_pvoid_t free_ctn) {
484 xbt_dict_set(MSG_storage_get_properties(storage), name, value,free_ctn);
487 /** \ingroup m_storage_management
488 * \brief Returns the value of a given storage property
490 * \param storage a storage
491 * \param name a property name
492 * \return value of a property (or nullptr if property not set)
494 const char *MSG_storage_get_property_value(msg_storage_t storage, const char *name)
496 return (char*) xbt_dict_get_or_null(MSG_storage_get_properties(storage), name);
499 /** \ingroup msg_storage_management
500 * \brief Finds a msg_storage_t using its name.
501 * \param name the name of a storage
502 * \return the corresponding storage
504 msg_storage_t MSG_storage_get_by_name(const char *name)
506 return (msg_storage_t) xbt_lib_get_elm_or_null(storage_lib,name);
509 /** \ingroup msg_storage_management
510 * \brief Returns a dynar containing all the storage elements declared at a given point of time
512 xbt_dynar_t MSG_storages_as_dynar(void) {
513 xbt_lib_cursor_t cursor;
516 xbt_dynar_t res = xbt_dynar_new(sizeof(msg_storage_t),nullptr);
518 xbt_lib_foreach(storage_lib, cursor, key, data) {
519 if(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), MSG_STORAGE_LEVEL) != nullptr) {
520 xbt_dictelm_t elm = xbt_dict_cursor_get_elm(cursor);
521 xbt_dynar_push(res, &elm);
527 /** \ingroup msg_storage_management
529 * \brief Set the user data of a #msg_storage_t.
530 * This functions attach \a data to \a storage if possible.
532 msg_error_t MSG_storage_set_data(msg_storage_t storage, void *data)
534 msg_storage_priv_t priv = MSG_storage_priv(storage);
539 /** \ingroup m_host_management
541 * \brief Returns the user data of a #msg_storage_t.
543 * This functions checks whether \a storage is a valid pointer and returns its associate user data if possible.
545 void *MSG_storage_get_data(msg_storage_t storage)
547 xbt_assert((storage != nullptr), "Invalid parameters");
548 msg_storage_priv_t priv = MSG_storage_priv(storage);
552 /** \ingroup msg_storage_management
554 * \brief Returns the content (file list) of a #msg_storage_t.
555 * \param storage a storage
556 * \return The content of this storage element as a dict (full path file => size)
558 xbt_dict_t MSG_storage_get_content(msg_storage_t storage)
560 return SIMIX_storage_get_content(storage);
563 /** \ingroup msg_storage_management
565 * \brief Returns the size of a #msg_storage_t.
566 * \param storage a storage
567 * \return The size of the storage
569 sg_size_t MSG_storage_get_size(msg_storage_t storage)
571 return SIMIX_storage_get_size(storage);
574 /** \ingroup msg_storage_management
576 * \brief Returns the host name the storage is attached to
578 * This functions checks whether a storage is a valid pointer or not and return its name.
580 const char *MSG_storage_get_host(msg_storage_t storage) {
581 xbt_assert((storage != nullptr), "Invalid parameters");
582 msg_storage_priv_t priv = MSG_storage_priv(storage);
583 return priv->hostname;