Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add function to unlink a file.
authornavarro <navarro@caraja.(none)>
Tue, 31 Jul 2012 15:43:23 +0000 (17:43 +0200)
committernavarro <navarro@caraja.(none)>
Tue, 31 Jul 2012 15:44:01 +0000 (17:44 +0200)
Add an example for unlink.

14 files changed:
examples/msg/io/CMakeLists.txt
examples/msg/io/file_unlink.c [new file with mode: 0644]
include/msg/msg.h
include/simgrid/simix.h
src/include/surf/surf.h
src/msg/msg_io.c
src/simix/smx_io.c
src/simix/smx_io_private.h
src/simix/smx_smurf.c
src/simix/smx_smurf_private.h
src/simix/smx_user.c
src/surf/storage.c
src/surf/storage_private.h
src/surf/workstation.c

index fbee5bb..7036f56 100644 (file)
@@ -3,12 +3,15 @@ cmake_minimum_required(VERSION 2.6)
 set(EXECUTABLE_OUTPUT_PATH "${CMAKE_CURRENT_BINARY_DIR}")
 
 add_executable(file file.c)
 set(EXECUTABLE_OUTPUT_PATH "${CMAKE_CURRENT_BINARY_DIR}")
 
 add_executable(file file.c)
+add_executable(file_unlink file_unlink.c)
 
 ### Add definitions for compile
 if(NOT WIN32)
   target_link_libraries(file simgrid m pthread)
 
 ### Add definitions for compile
 if(NOT WIN32)
   target_link_libraries(file simgrid m pthread)
+  target_link_libraries(file_unlink simgrid m pthread)
 else(NOT WIN32)
   target_link_libraries(file simgrid)
 else(NOT WIN32)
   target_link_libraries(file simgrid)
+  target_link_libraries(file_unlink simgrid)
 endif(NOT WIN32)
 
 set(tesh_files
 endif(NOT WIN32)
 
 set(tesh_files
@@ -23,6 +26,7 @@ set(xml_files
 set(examples_src
   ${examples_src}
   ${CMAKE_CURRENT_SOURCE_DIR}/file.c
 set(examples_src
   ${examples_src}
   ${CMAKE_CURRENT_SOURCE_DIR}/file.c
+  ${CMAKE_CURRENT_SOURCE_DIR}/file_unlink.c
   PARENT_SCOPE
   )
 set(bin_files
   PARENT_SCOPE
   )
 set(bin_files
diff --git a/examples/msg/io/file_unlink.c b/examples/msg/io/file_unlink.c
new file mode 100644 (file)
index 0000000..43a54fc
--- /dev/null
@@ -0,0 +1,101 @@
+/* Copyright (c) 2008, 2009, 2010. 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. */
+
+/** @addtogroup MSG_examples
+ * 
+ * @subsection MSG_ex_resources Other resource kinds
+ * 
+ * This section contains some sparse examples of how to use the other
+ * kind of resources, such as disk or GPU. These resources are quite
+ * experimental for now, but here we go anyway.
+ * 
+ * - <b>io/file.c</b> Example with the disk resource
+ */
+
+#define FILENAME1 "./doc/simgrid/examples/platforms/g5k.xml"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "msg/msg.h"
+#include "surf/surf_private.h"
+
+int host(int argc, char *argv[]);
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(io_file,
+                             "Messages specific for this io example");
+
+int host(int argc, char *argv[])
+{
+  msg_file_t file = NULL;
+  s_msg_stat_t stat;
+  void *ptr = NULL;
+  char* mount = bprintf("/home");
+  double write;
+
+  // First open
+  XBT_INFO("\tOpen file '%s'",FILENAME1);
+  file = MSG_file_open(mount,FILENAME1,"rw");
+
+  // Print stat
+  MSG_file_stat(file,&stat);
+  XBT_INFO("\tFile stat %s Size %.1f",file->name,stat.size);
+  MSG_file_free_stat(&stat);
+
+  // Unlink the file
+  XBT_INFO("\tUnlink file '%s'",file->name);
+  MSG_file_unlink(file);
+
+  // Re Open the file wich is in fact created
+  XBT_INFO("\tOpen file '%s'",FILENAME1);
+  file = MSG_file_open(mount,FILENAME1,"rw");
+
+  // Print stat
+  MSG_file_stat(file,&stat);
+  XBT_INFO("\tFile stat %s Size %.1f",file->name,stat.size);
+  MSG_file_free_stat(&stat);
+
+  // Write into the new file
+  write = MSG_file_write(ptr,100000,sizeof(char*),file);  // Write for 100Ko
+  XBT_INFO("\tHaving write %.1f \ton %s",write,file->name);
+
+  // Print the stat
+  MSG_file_stat(file,&stat);
+  XBT_INFO("\tFile stat %s Size %.1f",file->name,stat.size);
+  MSG_file_free_stat(&stat);
+
+  // Close the file
+  XBT_INFO("\tClose file '%s'",file->name);
+  MSG_file_close(file);
+
+  free(mount);
+
+  return 0;
+}
+
+int main(int argc, char **argv)
+{
+  int res;
+  MSG_init(&argc, argv);
+  MSG_create_environment(argv[1]);
+  xbt_dynar_t hosts =  MSG_hosts_as_dynar();
+  MSG_function_register("host", host);
+  unsigned long nb_hosts = xbt_dynar_length(hosts);
+  XBT_INFO("Number of host '%lu'",nb_hosts);
+  char* name_host = bprintf("0");
+  MSG_process_create( name_host, host, NULL, xbt_dynar_get_as(hosts,0,msg_host_t) );
+  free(name_host);
+
+  xbt_dynar_free(&hosts);
+
+  res = MSG_main();
+  XBT_INFO("Simulation time %g", MSG_get_clock());
+  MSG_clean();
+  if (res == MSG_OK)
+    return 0;
+  else
+    return 1;
+
+}
index 2d62058..e3b5ac1 100644 (file)
@@ -79,6 +79,7 @@ XBT_PUBLIC(int) MSG_file_close(msg_file_t fp);
 XBT_PUBLIC(int) MSG_file_stat(msg_file_t fd, s_msg_stat_t *buf);
 XBT_PUBLIC(void) MSG_file_free_stat(s_msg_stat_t *stat);
 
 XBT_PUBLIC(int) MSG_file_stat(msg_file_t fd, s_msg_stat_t *buf);
 XBT_PUBLIC(void) MSG_file_free_stat(s_msg_stat_t *stat);
 
+XBT_PUBLIC(int) MSG_file_unlink(msg_file_t fd);
 
 /************************** Host handling ***********************************/
 XBT_PUBLIC(msg_error_t) MSG_host_set_data(msg_host_t host, void *data);
 
 /************************** Host handling ***********************************/
 XBT_PUBLIC(msg_error_t) MSG_host_set_data(msg_host_t host, void *data);
index e27cbaa..55a834b 100644 (file)
@@ -454,6 +454,7 @@ XBT_PUBLIC(size_t) simcall_file_write(const void* ptr, size_t size, size_t nmemb
 XBT_PUBLIC(smx_file_t) simcall_file_open(const char* storage, const char* path, const char* mode);
 XBT_PUBLIC(int) simcall_file_close(smx_file_t fp);
 XBT_PUBLIC(int) simcall_file_stat(smx_file_t fd, s_file_stat_t *buf);
 XBT_PUBLIC(smx_file_t) simcall_file_open(const char* storage, const char* path, const char* mode);
 XBT_PUBLIC(int) simcall_file_close(smx_file_t fp);
 XBT_PUBLIC(int) simcall_file_stat(smx_file_t fd, s_file_stat_t *buf);
+XBT_PUBLIC(int) simcall_file_unlink(smx_file_t fd);
 
 SG_END_DECL()
 #endif                          /* _SIMIX_SIMIX_H */
 
 SG_END_DECL()
 #endif                          /* _SIMIX_SIMIX_H */
index 7842c9e..491ea4c 100644 (file)
@@ -223,6 +223,7 @@ typedef struct surf_storage_model_extension_public {
   surf_action_t(*read) (void *storage, void* ptr, double size, size_t nmemb, surf_file_t stream);
   surf_action_t(*write) (void *storage, const void* ptr, size_t size, size_t nmemb, surf_file_t stream);
   surf_action_t(*stat) (void *storage, surf_file_t stream);
   surf_action_t(*read) (void *storage, void* ptr, double size, size_t nmemb, surf_file_t stream);
   surf_action_t(*write) (void *storage, const void* ptr, size_t size, size_t nmemb, surf_file_t stream);
   surf_action_t(*stat) (void *storage, surf_file_t stream);
+  surf_action_t(*unlink) (void *storage, surf_file_t stream);
   void* (*create_resource) (const char* id, const char* model, const char* type_id, const char *content);
 } s_surf_model_extension_storage_t;
 
   void* (*create_resource) (const char* id, const char* model, const char* type_id, const char *content);
 } s_surf_model_extension_storage_t;
 
@@ -256,6 +257,7 @@ typedef struct surf_workstation_model_extension_public {
   surf_action_t(*read) (void *workstation, void* ptr, size_t size, size_t nmemb, surf_file_t stream);
   surf_action_t(*write) (void *workstation, const void* ptr, size_t size, size_t nmemb, surf_file_t stream);
   surf_action_t(*stat) (void *workstation, surf_file_t stream);
   surf_action_t(*read) (void *workstation, void* ptr, size_t size, size_t nmemb, surf_file_t stream);
   surf_action_t(*write) (void *workstation, const void* ptr, size_t size, size_t nmemb, surf_file_t stream);
   surf_action_t(*stat) (void *workstation, surf_file_t stream);
+  surf_action_t(*unlink) (void *workstation, surf_file_t stream);
   int (*link_shared) (const void *link);
    xbt_dict_t(*get_properties) (const void *resource);
   void* (*link_create_resource) (const char *name,
   int (*link_shared) (const void *link);
    xbt_dict_t(*get_properties) (const void *resource);
   void* (*link_create_resource) (const char *name,
index 7d51d15..2b03f67 100644 (file)
@@ -115,3 +115,18 @@ void MSG_file_free_stat(s_msg_stat_t *stat)
   free(stat->user);
   free(stat->user_rights);
 }
   free(stat->user);
   free(stat->user_rights);
 }
+
+/** \ingroup msg_file_management
+ * \brief Unlink the file pointed by fd
+ *
+ * \param fd is the file descriptor (#msg_file_t)
+ * \return 0 on success or 1 on error
+ */
+int MSG_file_unlink(msg_file_t fd)
+{
+  int res = simcall_file_unlink(fd->simdata->smx_file);
+  free(fd->name);
+  xbt_free(fd->simdata);
+  xbt_free(fd);
+  return res;
+}
index 31aa2f6..7e6f53d 100644 (file)
@@ -207,6 +207,42 @@ smx_action_t SIMIX_file_stat(smx_process_t process, smx_file_t fd, s_file_stat_t
   return action;
 }
 
   return action;
 }
 
+//SIMIX FILE UNLINK
+void SIMIX_pre_file_unlink(smx_simcall_t simcall)
+{
+  smx_action_t action = SIMIX_file_unlink(simcall->issuer,
+      simcall->file_unlink.fd);
+  xbt_fifo_push(action->simcalls, simcall);
+  simcall->issuer->waiting_action = action;
+}
+
+smx_action_t SIMIX_file_unlink(smx_process_t process, smx_file_t fd)
+{
+  smx_action_t action;
+  smx_host_t host = process->smx_host;
+  /* check if the host is active */
+  if (surf_workstation_model->extension.
+      workstation.get_state(host->host) != SURF_RESOURCE_ON) {
+    THROWF(host_error, 0, "Host %s failed, you cannot call this function",
+           host->name);
+  }
+
+  action = xbt_mallocator_get(simix_global->action_mallocator);
+  action->type = SIMIX_ACTION_IO;
+  action->name = NULL;
+#ifdef HAVE_TRACING
+  action->category = NULL;
+#endif
+
+  action->io.host = host;
+  action->io.surf_io = surf_workstation_model->extension.workstation.unlink(host->host, fd->surf_file);
+
+  surf_workstation_model->action_data_set(action->io.surf_io, action);
+  XBT_DEBUG("Create io action %p", action);
+
+  return action;
+}
+
 void SIMIX_post_io(smx_action_t action)
 {
   xbt_fifo_item_t i;
 void SIMIX_post_io(smx_action_t action)
 {
   xbt_fifo_item_t i;
@@ -234,6 +270,10 @@ void SIMIX_post_io(smx_action_t action)
       s_file_stat_t *src = &((action->io.surf_io)->stat);
       file_stat_copy(src,dst);
       break;
       s_file_stat_t *src = &((action->io.surf_io)->stat);
       file_stat_copy(src,dst);
       break;
+    case SIMCALL_FILE_UNLINK:
+      xbt_free(simcall->file_unlink.fd);
+      simcall->file_unlink.result = 0;
+      break;
     default:
       break;
     }
     default:
       break;
     }
index c457a02..6f7d361 100644 (file)
@@ -15,12 +15,14 @@ void SIMIX_pre_file_write(smx_simcall_t simcall);
 void SIMIX_pre_file_open(smx_simcall_t simcall);
 void SIMIX_pre_file_close(smx_simcall_t simcall);
 void SIMIX_pre_file_stat(smx_simcall_t simcall);
 void SIMIX_pre_file_open(smx_simcall_t simcall);
 void SIMIX_pre_file_close(smx_simcall_t simcall);
 void SIMIX_pre_file_stat(smx_simcall_t simcall);
+void SIMIX_pre_file_unlink(smx_simcall_t simcall);
 
 smx_action_t SIMIX_file_read(smx_process_t process, void* ptr, size_t size, size_t nmemb, smx_file_t stream);
 smx_action_t SIMIX_file_write(smx_process_t process, const void* ptr, size_t size, size_t nmemb, smx_file_t stream);
 smx_action_t SIMIX_file_open(smx_process_t process, const char* storage, const char* path, const char* mode);
 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fp);
 smx_action_t SIMIX_file_stat(smx_process_t process, smx_file_t fd, s_file_stat_t buf);
 
 smx_action_t SIMIX_file_read(smx_process_t process, void* ptr, size_t size, size_t nmemb, smx_file_t stream);
 smx_action_t SIMIX_file_write(smx_process_t process, const void* ptr, size_t size, size_t nmemb, smx_file_t stream);
 smx_action_t SIMIX_file_open(smx_process_t process, const char* storage, const char* path, const char* mode);
 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fp);
 smx_action_t SIMIX_file_stat(smx_process_t process, smx_file_t fd, s_file_stat_t buf);
+smx_action_t SIMIX_file_unlink(smx_process_t process, smx_file_t fd);
 
 void SIMIX_post_io(smx_action_t action);
 void SIMIX_io_destroy(smx_action_t action);
 
 void SIMIX_post_io(smx_action_t action);
 void SIMIX_io_destroy(smx_action_t action);
index dfb1268..55a6093 100644 (file)
@@ -522,6 +522,10 @@ void SIMIX_simcall_pre(smx_simcall_t simcall, int value)
       SIMIX_pre_file_stat(simcall);
       break;
 
       SIMIX_pre_file_stat(simcall);
       break;
 
+    case SIMCALL_FILE_UNLINK:
+      SIMIX_pre_file_unlink(simcall);
+      break;
+
     case SIMCALL_NONE:
       THROWF(arg_error,0,"Asked to do the noop syscall on %s@%s",
           SIMIX_process_get_name(simcall->issuer),
     case SIMCALL_NONE:
       THROWF(arg_error,0,"Asked to do the noop syscall on %s@%s",
           SIMIX_process_get_name(simcall->issuer),
index 6e83587..30a1018 100644 (file)
@@ -91,7 +91,8 @@ SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_READ),\
 SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_WRITE),\
 SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_OPEN),\
 SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_CLOSE),\
 SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_WRITE),\
 SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_OPEN),\
 SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_CLOSE),\
-SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_STAT)
+SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_STAT), \
+SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_UNLINK)
 
 
 /* SIMCALL_COMM_IS_LATENCY_BOUNDED and SIMCALL_SET_CATEGORY make things complicated
 
 
 /* SIMCALL_COMM_IS_LATENCY_BOUNDED and SIMCALL_SET_CATEGORY make things complicated
@@ -562,6 +563,11 @@ typedef struct s_smx_simcall {
       int result;
     } file_stat;
 
       int result;
     } file_stat;
 
+    struct {
+      smx_file_t fd;
+      int result;
+    } file_unlink;
+
   };
 } s_smx_simcall_t, *smx_simcall_t;
 
   };
 } s_smx_simcall_t, *smx_simcall_t;
 
index b09d08e..c3074c0 100644 (file)
@@ -1465,6 +1465,21 @@ int simcall_file_stat(smx_file_t fd, s_file_stat_t *buf)
   return simcall->file_stat.result;
 }
 
   return simcall->file_stat.result;
 }
 
+/**
+ * \ingroup simix_file_management
+ *
+ */
+int simcall_file_unlink(smx_file_t fd)
+{
+  smx_simcall_t simcall = SIMIX_simcall_mine();
+  simcall->call = SIMCALL_FILE_UNLINK;
+  simcall->file_unlink.fd = fd;
+
+  SIMIX_simcall_push(simcall->issuer);
+
+  return simcall->file_unlink.result;
+}
+
 /* ************************************************************************** */
 
 /** @brief returns a printable string representing a simcall */
 /* ************************************************************************** */
 
 /** @brief returns a printable string representing a simcall */
index 2e868d7..9983513 100644 (file)
@@ -47,6 +47,24 @@ static surf_action_t storage_action_stat(void *storage, surf_file_t stream)
   return action;
 }
 
   return action;
 }
 
+static surf_action_t storage_action_unlink(void *storage, surf_file_t stream)
+{
+  surf_action_t action = storage_action_execute(storage,0, UNLINK);
+
+  // Add memory to storage
+  ((storage_t)storage)->used_size -= stream->content->stat.size;
+
+  // Remove the file from storage
+  xbt_dict_t content_dict = ((storage_t)storage)->content;
+  xbt_dict_remove(content_dict,stream->name);
+
+  free(stream->name);
+  stream->content = NULL;
+  xbt_free(stream);
+
+  return action;
+}
+
 static surf_action_t storage_action_open(void *storage, const char* mount, const char* path, const char* mode)
 {
   XBT_DEBUG("\tOpen file '%s'",path);
 static surf_action_t storage_action_open(void *storage, const char* mount, const char* path, const char* mode)
 {
   XBT_DEBUG("\tOpen file '%s'",path);
@@ -137,6 +155,7 @@ static surf_action_t storage_action_execute (void *storage, double size, e_surf_
   case OPEN:
   case CLOSE:
   case STAT:
   case OPEN:
   case CLOSE:
   case STAT:
+  case UNLINK:
     break;
   case READ:
     lmm_expand(storage_maxmin_system, STORAGE->constraint_read,
     break;
   case READ:
     lmm_expand(storage_maxmin_system, STORAGE->constraint_read,
@@ -146,7 +165,6 @@ static surf_action_t storage_action_execute (void *storage, double size, e_surf_
     lmm_expand(storage_maxmin_system, STORAGE->constraint_write,
                GENERIC_LMM_ACTION(action).variable, 1.0);
     xbt_dynar_push(((storage_t)storage)->write_actions,&action);
     lmm_expand(storage_maxmin_system, STORAGE->constraint_write,
                GENERIC_LMM_ACTION(action).variable, 1.0);
     xbt_dynar_push(((storage_t)storage)->write_actions,&action);
-
     break;
   }
   action->type = type;
     break;
   }
   action->type = type;
@@ -442,6 +460,7 @@ static void surf_storage_model_init_internal(void)
   surf_storage_model->extension.storage.read = storage_action_read;
   surf_storage_model->extension.storage.write = storage_action_write;
   surf_storage_model->extension.storage.stat = storage_action_stat;
   surf_storage_model->extension.storage.read = storage_action_read;
   surf_storage_model->extension.storage.write = storage_action_write;
   surf_storage_model->extension.storage.stat = storage_action_stat;
+  surf_storage_model->extension.storage.unlink = storage_action_unlink;
   surf_storage_model->extension.storage.create_resource = storage_create_resource;
 
   if (!storage_maxmin_system) {
   surf_storage_model->extension.storage.create_resource = storage_create_resource;
 
   if (!storage_maxmin_system) {
index f27d650..5900e8f 100644 (file)
@@ -45,7 +45,7 @@ typedef struct storage {
 } s_storage_t, *storage_t;
 
 typedef enum {
 } s_storage_t, *storage_t;
 
 typedef enum {
-  READ=0, WRITE, STAT, OPEN, CLOSE
+  READ=0, WRITE, STAT, OPEN, CLOSE, UNLINK
 } e_surf_action_storage_type_t;
 
 typedef struct surf_action_storage {
 } e_surf_action_storage_type_t;
 
 typedef struct surf_action_storage {
index 62797b0..c92801b 100644 (file)
@@ -343,6 +343,14 @@ static surf_action_t ws_action_stat(void *workstation, surf_file_t stream)
   return model->extension.storage.stat(st,  stream);
 }
 
   return model->extension.storage.stat(st,  stream);
 }
 
+static surf_action_t ws_action_unlink(void *workstation, surf_file_t stream)
+{
+  storage_t st = find_storage_on_mount_list(workstation, stream->storage);
+  XBT_DEBUG("UNLINK on disk '%s'",st->generic_resource.name);
+  surf_model_t model = st->generic_resource.model;
+  return model->extension.storage.unlink(st,  stream);
+}
+
 static void surf_workstation_model_init_internal(void)
 {
   surf_workstation_model = surf_model_init();
 static void surf_workstation_model_init_internal(void)
 {
   surf_workstation_model = surf_model_init();
@@ -400,6 +408,7 @@ static void surf_workstation_model_init_internal(void)
   surf_workstation_model->extension.workstation.read = ws_action_read;
   surf_workstation_model->extension.workstation.write = ws_action_write;
   surf_workstation_model->extension.workstation.stat = ws_action_stat;
   surf_workstation_model->extension.workstation.read = ws_action_read;
   surf_workstation_model->extension.workstation.write = ws_action_write;
   surf_workstation_model->extension.workstation.stat = ws_action_stat;
+  surf_workstation_model->extension.workstation.unlink = ws_action_unlink;
 }
 
 void surf_workstation_model_init_current_default(void)
 }
 
 void surf_workstation_model_init_current_default(void)