From 8a4ee6257836fd37e8e72cdf4c7f2f1ee85babea Mon Sep 17 00:00:00 2001 From: Pierre Veyre Date: Fri, 8 Nov 2013 18:47:50 +0100 Subject: [PATCH] Update MSG_storage_file_rename Add a msg storage example (cherry picked from commit c9e8dd1a590fecb704c4114633b881848c1ef7e9) --- examples/msg/io/storage.c | 156 ++++++++++++++++++++++++++++++++++ include/msg/msg.h | 6 +- include/simgrid/simix.h | 2 +- src/include/surf/surf.h | 2 +- src/msg/msg_io.c | 41 +++++---- src/simix/smx_io.c | 11 +-- src/simix/smx_io_private.h | 4 +- src/simix/smx_smurf_private.h | 2 +- src/simix/smx_user.c | 4 +- src/surf/storage.c | 17 ++++ src/surf/workstation.c | 29 ------- 11 files changed, 207 insertions(+), 67 deletions(-) create mode 100644 examples/msg/io/storage.c diff --git a/examples/msg/io/storage.c b/examples/msg/io/storage.c new file mode 100644 index 0000000000..c011805d08 --- /dev/null +++ b/examples/msg/io/storage.c @@ -0,0 +1,156 @@ +/* Copyright (c) 2006-2013. The SimGrid Team. + * All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +/********************* Files and Storage handling **************************** + * This example implements all main storage and file functions of the MSG API + * + * Scenario : + * - display information on the disks mounted by the current host + * - create a 200,000 bytes file + * - completely read the created file + * - write 100,000 more bytes in the file + * - rename the created file + * - attach some user data to a disk + * - dump disk's contents + * +******************************************************************************/ + +#include "msg/msg.h" +#include "xbt/log.h" + + /* To use PRIu64 format specifier for printing uint64_t (sg_storage_size_t) */ +#include + +XBT_LOG_NEW_DEFAULT_CATEGORY(storage,"Messages specific for this simulation"); + +int host(int argc, char *argv[]){ + const char* host_name = MSG_host_get_name(MSG_host_self()); + + // display information on the disks mounted by the current host + XBT_INFO("*** Storage info on %s ***", host_name); + + xbt_dict_cursor_t cursor = NULL; + char* mount_name; + char* storage_name; + msg_storage_t storage; + + // Retrieve all mount points of current host + xbt_dict_t storage_list = MSG_host_get_storage_list(MSG_host_self()); + + xbt_dict_foreach(storage_list,cursor,mount_name,storage_name) { + // For each disk mounted on host + + XBT_INFO("Storage name: %s, mount name: %s", storage_name, mount_name); + storage = MSG_storage_get_by_name(storage_name); + + // Retrieve disk's information + sg_storage_size_t free_size = MSG_storage_get_free_size(mount_name); + sg_storage_size_t used_size = MSG_storage_get_used_size(mount_name); + sg_storage_size_t size = MSG_storage_get_size(storage); + + XBT_INFO("Total size: %"PRIu64" bytes", size); + XBT_INFO("Free size: %"PRIu64" bytes", free_size); + XBT_INFO("Used size: %"PRIu64" bytes", used_size); + } + xbt_free(storage_list); + + + // Create a 200,000 bytes file named './tmp/data.txt' on /sd1 + + char* mount = xbt_strdup("/sd1"); + char* file_name = xbt_strdup("./tmp/data.txt"); + msg_file_t file = NULL; + sg_storage_size_t write, read, file_size; + + // Open an non-existing file amounts to create it! + file = MSG_file_open(mount, file_name, NULL); + write = MSG_file_write(file, 200000); // Write 200,000 bytes + XBT_INFO("Create a %"PRIu64" bytes file named '%s' on /sd1", write, file_name); + MSG_file_dump(file); + + // check that sizes have changed + XBT_INFO("Total size: %"PRIu64" bytes", MSG_storage_get_free_size("/sd1")); + XBT_INFO("Free size: %"PRIu64" bytes", MSG_storage_get_used_size("/sd1")); + + + // Now retrieve the size of created file and read it completely + file_size = MSG_file_get_size(file); + read = MSG_file_read(file, file_size); + XBT_INFO("Read %"PRIu64" bytes on %s", read, file_name); + + // Now write 100,000 more bytes in tmp/data.txt + write = MSG_file_write(file, 100000); // Write 100,000 bytes + XBT_INFO("Write %"PRIu64" more bytes on %s", write, file_name); + MSG_file_dump(file); + + MSG_file_close(file); + free(mount); + free(file_name); + + // Now rename file from /tmp/data.txt to /tmp/simgrid.readme + msg_storage_t st = MSG_storage_get_by_name("/sd1"); + MSG_storage_file_rename(st, "/tmp/data.txt", "/tmp/simgrid.readme"); + + // Now attach some user data to disk1 + storage_name = xbt_strdup("disk1"); + XBT_INFO("*** Get/set data for storage element: %s ***",storage_name); + + storage = MSG_storage_get_by_name(storage_name); + char *data = MSG_storage_get_data(storage); + + XBT_INFO("Get data: '%s'", data); + + MSG_storage_set_data(storage,strdup("Some user data")); + data = MSG_storage_get_data(storage); + XBT_INFO("Set and get data: '%s'", data); + free(storage_name); + + + // Dump disks contents + XBT_INFO("*** Dump content of %s ***",MSG_host_get_name(MSG_host_self())); + xbt_dict_t contents = NULL; + contents = MSG_host_get_storage_content(MSG_host_self()); // contents is a dict of dicts + xbt_dict_cursor_t curs, curs2 = NULL; + char* mountname; + xbt_dict_t content; + char* path; + sg_storage_size_t *size; + xbt_dict_foreach(contents, curs, mountname, content){ + XBT_INFO("Print the content of mount point: %s",mountname); + xbt_dict_foreach(content,curs2,path,size){ + XBT_INFO("%s size: %"PRIu64" bytes", path,*((sg_storage_size_t*)size)); + } + } + + return 1; +} + + +int main(int argc, char *argv[]) +{ + + MSG_init(&argc, argv); + /* Check the arguments */ + if (argc < 3) { + printf("Usage: %s platform_file deployment_file \n", argv[0]); + return -1; + } + + const char *platform_file = argv[1]; + const char *deployment_file = argv[2]; + + MSG_create_environment(platform_file); + MSG_function_register("host", host); + MSG_launch_application(deployment_file); + + msg_error_t res = MSG_main(); + XBT_INFO("Simulated time: %g", MSG_get_clock()); + + if (res == MSG_OK) + return 0; + else + return 1; +} diff --git a/include/msg/msg.h b/include/msg/msg.h index 5976cc324a..4a845f3601 100644 --- a/include/msg/msg.h +++ b/include/msg/msg.h @@ -92,9 +92,7 @@ XBT_PUBLIC(sg_storage_size_t) MSG_file_get_size(msg_file_t fd); XBT_PUBLIC(void) MSG_file_dump(msg_file_t fd); XBT_PUBLIC(int) MSG_file_unlink(msg_file_t fd); XBT_PUBLIC(xbt_dict_t) MSG_file_ls(const char *mount, const char *path); -XBT_PUBLIC(msg_error_t) MSG_file_move (msg_file_t fd, msg_host_t dest, char* mount, char* fullname); -XBT_PUBLIC(msg_error_t) MSG_file_seek (msg_file_t fd, sg_storage_size_t offset, int whence); -XBT_PUBLIC(msg_error_t) MSG_file_rename (msg_file_t fd, const char* new_name); +XBT_PUBLIC(msg_error_t) MSG_file_seek(msg_file_t fd, sg_storage_size_t offset, int whence); XBT_PUBLIC(void) __MSG_file_get_info(msg_file_t fd); /************************** Storage handling ***********************************/ XBT_PUBLIC(msg_host_t) MSG_get_storage_by_name(const char *name); @@ -109,6 +107,8 @@ XBT_PUBLIC(msg_error_t) MSG_storage_set_data(msg_storage_t host, void *data); XBT_PUBLIC(void *) MSG_storage_get_data(msg_storage_t storage); XBT_PUBLIC(xbt_dict_t) MSG_storage_get_content(msg_storage_t storage); XBT_PUBLIC(sg_storage_size_t) MSG_storage_get_size(msg_storage_t storage); +XBT_PUBLIC(msg_error_t) MSG_storage_file_move(msg_file_t fd, msg_host_t dest, char* mount, char* fullname); +XBT_PUBLIC(msg_error_t) MSG_storage_file_rename(msg_storage_t storage, const char* src, const char* dest); /************************** AS Router handling ************************************/ XBT_PUBLIC(const char *) MSG_as_router_get_property_value(const char* asr, const char *name); XBT_PUBLIC(xbt_dict_t) MSG_as_router_get_properties(const char* asr); diff --git a/include/simgrid/simix.h b/include/simgrid/simix.h index b595bf6ee7..07b4052dc2 100644 --- a/include/simgrid/simix.h +++ b/include/simgrid/simix.h @@ -489,7 +489,6 @@ XBT_PUBLIC(int) simcall_file_unlink(smx_file_t fd); XBT_PUBLIC(xbt_dict_t) simcall_file_ls(const char* mount, const char* path); XBT_PUBLIC(sg_storage_size_t) simcall_file_get_size(smx_file_t fd); XBT_PUBLIC(xbt_dynar_t) simcall_file_get_info(smx_file_t fd); -XBT_PUBLIC(void) simcall_file_rename(smx_file_t fd, const char* new_name); /***************************** Storage **********************************/ XBT_PUBLIC(sg_storage_size_t) simcall_storage_get_free_size (const char* name); XBT_PUBLIC(sg_storage_size_t) simcall_storage_get_used_size (const char* name); @@ -500,6 +499,7 @@ XBT_PUBLIC(xbt_dict_t) SIMIX_storage_get_content(smx_storage_t storage); XBT_PUBLIC(xbt_dict_t) simcall_storage_get_content(smx_storage_t storage); XBT_PUBLIC(const char*) SIMIX_storage_get_name(smx_host_t host); XBT_PUBLIC(sg_storage_size_t) SIMIX_storage_get_size(smx_storage_t storage); +XBT_PUBLIC(void) simcall_storage_file_rename(smx_storage_t storage, const char* src, const char* dest); /************************** AS router **********************************/ XBT_PUBLIC(xbt_dict_t) SIMIX_asr_get_properties(const char *name); /************************** AS router simcalls ***************************/ diff --git a/src/include/surf/surf.h b/src/include/surf/surf.h index cce0dd9cbc..9de02bec77 100644 --- a/src/include/surf/surf.h +++ b/src/include/surf/surf.h @@ -238,6 +238,7 @@ typedef struct surf_storage_model_extension_public { surf_action_t(*stat) (void *storage, surf_file_t fd); surf_action_t(*ls) (void *storage, const char *path); xbt_dict_t(*get_properties) (const void *storage); + void (*rename) (const void *storage, const char *src, const char *dest); xbt_dict_t(*get_content) (void *storage); sg_storage_size_t(*get_size) (void *storage); } s_surf_model_extension_storage_t; @@ -287,7 +288,6 @@ typedef struct surf_workstation_model_extension_public { surf_action_t(*ls) (void *workstation, const char* mount, const char *path); sg_storage_size_t (*get_size) (void *workstation, surf_file_t fd); xbt_dynar_t (*get_info) (void *workstation, surf_file_t fd); - void (*rename) (void *workstation, surf_file_t fd, const char *new_name); int (*link_shared) (const void *link); xbt_dict_t(*get_properties) (const void *resource); void (*add_traces) (void); diff --git a/src/msg/msg_io.c b/src/msg/msg_io.c index 1aa60efaba..0b816a15e7 100644 --- a/src/msg/msg_io.c +++ b/src/msg/msg_io.c @@ -191,18 +191,6 @@ xbt_dict_t MSG_file_ls(const char *mount, const char *path) return simcall_file_ls(mount, path); } -/* - * Move a file to another location. Depending on the values of dest, dest, mount, - * and fullname, this move can be local or remote and, within a host, on the same - * mounted disk or between mounted disks. - * - */ -msg_error_t MSG_file_move (msg_file_t fd, msg_host_t dest, char* mount, char* fullname) -{ - THROW_UNIMPLEMENTED; - return MSG_OK; -} - /* * Set the file position indicator in the msg_file_t by adding offset bytes * to the position specified by whence (either SEEK_SET, SEEK_CUR, or SEEK_END). @@ -213,15 +201,6 @@ msg_error_t MSG_file_seek (msg_file_t fd, sg_storage_size_t offset, int whence) return MSG_OK; } -/* - * Rename the file in the contents of its associated storage. - */ -msg_error_t MSG_file_rename (msg_file_t fd, const char* new_name) -{ - simcall_file_rename(fd->simdata->smx_file, new_name); - return MSG_OK; -} - /********************************* Storage **************************************/ /** @addtogroup msg_storage_management @@ -370,3 +349,23 @@ sg_storage_size_t MSG_storage_get_size(msg_storage_t storage) return SIMIX_storage_get_size(storage); } +/* + * Rename the file in the contents of its associated storage. + */ +msg_error_t MSG_storage_file_rename(msg_storage_t storage, const char* src, const char* dest) +{ + simcall_storage_file_rename(storage, src, dest); + return MSG_OK; +} + +/* + * Move a file to another location. Depending on the values of dest, dest, mount, + * and fullname, this move can be local or remote and, within a host, on the same + * mounted disk or between mounted disks. + * + */ +msg_error_t MSG_storage_file_move (msg_file_t fd, msg_host_t dest, char* mount, char* fullname) +{ + THROW_UNIMPLEMENTED; + return MSG_OK; +} diff --git a/src/simix/smx_io.c b/src/simix/smx_io.c index aa0d016316..6755bba356 100644 --- a/src/simix/smx_io.c +++ b/src/simix/smx_io.c @@ -300,19 +300,16 @@ xbt_dynar_t SIMIX_file_get_info(smx_process_t process, smx_file_t fd) fd->surf_file); } -void SIMIX_pre_file_rename(smx_simcall_t simcall, smx_file_t fd, const char* new_name) +void SIMIX_pre_storage_file_rename(smx_simcall_t simcall, smx_storage_t storage, const char* src, const char* dest) { - return SIMIX_file_rename(simcall->issuer, fd, new_name); + return SIMIX_storage_file_rename(simcall->issuer, storage, src, dest); } -void SIMIX_file_rename(smx_process_t process, smx_file_t fd, const char* new_name) +void SIMIX_storage_file_rename(smx_process_t process, smx_storage_t storage, const char* src, const char* dest) { - smx_host_t host = process->smx_host; - return surf_workstation_model->extension.workstation.rename(host, - fd->surf_file, new_name); + return surf_workstation_model->extension.storage.rename(storage, src, dest); } - sg_storage_size_t SIMIX_pre_storage_get_free_size(smx_simcall_t simcall, const char* name) { return SIMIX_storage_get_free_size(simcall->issuer, name); diff --git a/src/simix/smx_io_private.h b/src/simix/smx_io_private.h index 2e01b20fb9..864656b943 100644 --- a/src/simix/smx_io_private.h +++ b/src/simix/smx_io_private.h @@ -34,7 +34,7 @@ void SIMIX_pre_file_ls(smx_simcall_t simcall, const char* mount, const char* path); sg_storage_size_t SIMIX_pre_file_get_size(smx_simcall_t simcall, smx_file_t fd); xbt_dynar_t SIMIX_pre_file_get_info(smx_simcall_t simcall, smx_file_t fd); -void SIMIX_pre_file_rename(smx_simcall_t simcall, smx_file_t fd, const char* new_name); +void SIMIX_pre_storage_file_rename(smx_simcall_t simcall,smx_storage_t storage, const char* src, const char* dest); void* SIMIX_file_get_data(smx_file_t fd); void SIMIX_file_set_data(smx_file_t fd, void *data); @@ -48,7 +48,7 @@ smx_action_t SIMIX_file_ls(smx_process_t process, const char *mount, const char *path); sg_storage_size_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd); xbt_dynar_t SIMIX_file_get_info(smx_process_t process, smx_file_t fd); -void SIMIX_file_rename(smx_process_t process, smx_file_t fd, const char* new_name); +void SIMIX_storage_file_rename(smx_process_t process, smx_storage_t storage, const char* src, const char* dest); sg_storage_size_t SIMIX_pre_storage_get_free_size(smx_simcall_t simcall,const char* name); sg_storage_size_t SIMIX_storage_get_free_size(smx_process_t process,const char* name); diff --git a/src/simix/smx_smurf_private.h b/src/simix/smx_smurf_private.h index f3b01fdf57..a57a798ce8 100644 --- a/src/simix/smx_smurf_private.h +++ b/src/simix/smx_smurf_private.h @@ -357,7 +357,7 @@ ACTION(SIMCALL_FILE_UNLINK, file_unlink, WITH_ANSWER, TINT(result), TSPEC(fd, sm ACTION(SIMCALL_FILE_LS, file_ls, WITHOUT_ANSWER, TSPEC(result, xbt_dict_t), TSTRING(mount), TSTRING(path)) sep \ ACTION(SIMCALL_FILE_GET_SIZE, file_get_size, WITH_ANSWER, TSIZE(result), TSPEC(fd, smx_file_t)) sep \ ACTION(SIMCALL_FILE_GET_INFO, file_get_info, WITH_ANSWER, TSPEC(result, xbt_dynar_t), TSPEC(fd, smx_file_t)) sep \ -ACTION(SIMCALL_FILE_RENAME, file_rename, WITH_ANSWER, TVOID(result), TSPEC(fd, smx_file_t), TSTRING(new_name)) sep \ +ACTION(SIMCALL_STORAGE_FILE_RENAME, storage_file_rename, WITH_ANSWER, TVOID(result), TSPEC(storage, smx_storage_t), TSTRING(src), TSTRING(dest)) sep \ ACTION(SIMCALL_STORAGE_GET_FREE_SIZE, storage_get_free_size, WITH_ANSWER, TSIZE(result), TSTRING(name)) sep \ ACTION(SIMCALL_STORAGE_GET_USED_SIZE, storage_get_used_size, WITH_ANSWER, TSIZE(result), TSTRING(name)) sep \ ACTION(SIMCALL_STORAGE_GET_PROPERTIES, storage_get_properties, WITH_ANSWER, TSPEC(result, xbt_dict_t), TSPEC(storage, smx_storage_t)) sep \ diff --git a/src/simix/smx_user.c b/src/simix/smx_user.c index a8d7f4cc12..9694523f1c 100644 --- a/src/simix/smx_user.c +++ b/src/simix/smx_user.c @@ -1277,9 +1277,9 @@ xbt_dynar_t simcall_file_get_info(smx_file_t fd) * \ingroup simix_file_management * */ -void simcall_file_rename(smx_file_t fd, const char* new_name) +void simcall_storage_file_rename(smx_storage_t storage, const char* src, const char* dest) { - return simcall_BODY_file_rename(fd, new_name); + return simcall_BODY_storage_file_rename(storage, src, dest); } /** diff --git a/src/surf/storage.c b/src/surf/storage.c index 53691367bd..79974c7eb2 100644 --- a/src/surf/storage.c +++ b/src/surf/storage.c @@ -226,6 +226,22 @@ static sg_storage_size_t storage_get_size(void *storage){ return ((storage_t)storage_resource)->size; } +static void storage_file_rename(const void *storage, const char *src, const char *dest) +{ + void *storage_resource = surf_storage_resource_priv(storage); + + sg_storage_size_t *psize; + psize = (sg_storage_size_t*) xbt_dict_get_or_null(((storage_t)storage_resource)->content,src); + if (psize){// src file exists + xbt_dict_remove(((storage_t)storage_resource)->content, src); + xbt_dict_set(((storage_t)storage_resource)->content, dest, psize,NULL); + XBT_DEBUG("Change file name from %s to %s, size '%" PRIu64 "'",src, dest, *psize); + } + else + XBT_DEBUG("File %s doesn't exist",src); +} + + static void* storage_create_resource(const char* id, const char* model, const char* type_id, const char* content_name, const char* content_type, xbt_dict_t properties){ storage_t storage = NULL; @@ -538,6 +554,7 @@ static void surf_storage_model_init_internal(void) surf_storage_model->extension.storage.get_properties = storage_get_properties; surf_storage_model->extension.storage.get_content = storage_get_content; surf_storage_model->extension.storage.get_size = storage_get_size; + surf_storage_model->extension.storage.rename = storage_file_rename; if (!storage_maxmin_system) { storage_maxmin_system = lmm_system_new(storage_selective_update); } diff --git a/src/surf/workstation.c b/src/surf/workstation.c index c986d51f21..69255a4ec7 100644 --- a/src/surf/workstation.c +++ b/src/surf/workstation.c @@ -474,34 +474,6 @@ static xbt_dynar_t ws_file_get_info(void *workstation, surf_file_t fd) return info; } -static void ws_file_rename(void *workstation, surf_file_t fd, const char* new_name) -{ - storage_t storage = find_storage_on_mount_list(workstation, fd->mount); - - const char* old_full_name = fd->name; - xbt_dynar_t dyn = NULL; - const char* separator; - const char* ctype = storage->content_type; - - // TODO: PV: use an enum and a switch case to manage content type properly - if(!strcmp(ctype, "txt_unix")) - separator = strdup("/"); - else - separator = strdup("\\"); - - // Split file with separator and replace file name - dyn = xbt_str_split(old_full_name, separator); - xbt_dynar_pop_ptr(dyn); - xbt_dynar_push(dyn, &new_name); - char *new_full_name = xbt_str_join(dyn, separator); - - sg_storage_size_t *psize; - psize = (sg_storage_size_t*) xbt_dict_get_or_null(storage->content,old_full_name); - xbt_dict_remove(storage->content, old_full_name); - xbt_dict_set(storage->content,new_full_name,psize,NULL); - XBT_DEBUG("Change file name from %s to %s, size '%" PRIu64 "'",fd->name, new_full_name, *psize); -} - static sg_storage_size_t ws_storage_get_free_size(void *workstation,const char* name) { storage_t st = find_storage_on_mount_list(workstation, name); @@ -580,7 +552,6 @@ static void surf_workstation_model_init_internal(void) surf_workstation_model->extension.workstation.ls = ws_action_ls; surf_workstation_model->extension.workstation.get_size = ws_file_get_size; surf_workstation_model->extension.workstation.get_info = ws_file_get_info; - surf_workstation_model->extension.workstation.rename = ws_file_rename; surf_workstation_model->extension.workstation.get_free_size = ws_storage_get_free_size; surf_workstation_model->extension.workstation.get_used_size = ws_storage_get_used_size; surf_workstation_model->extension.workstation.get_storage_list = ws_get_storage_list; -- 2.20.1