Add an example for unlink.
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)
+ target_link_libraries(file_unlink simgrid m pthread)
else(NOT WIN32)
target_link_libraries(file simgrid)
+ target_link_libraries(file_unlink simgrid)
endif(NOT WIN32)
set(tesh_files
set(examples_src
${examples_src}
${CMAKE_CURRENT_SOURCE_DIR}/file.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/file_unlink.c
PARENT_SCOPE
)
set(bin_files
--- /dev/null
+/* 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;
+
+}
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);
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 */
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;
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,
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;
+}
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;
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;
}
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_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);
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),
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
int result;
} file_stat;
+ struct {
+ smx_file_t fd;
+ int result;
+ } file_unlink;
+
};
} s_smx_simcall_t, *smx_simcall_t;
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 */
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);
case OPEN:
case CLOSE:
case STAT:
+ case UNLINK:
break;
case READ:
lmm_expand(storage_maxmin_system, STORAGE->constraint_read,
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;
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) {
} 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 {
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();
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)