/************************** File handling ***********************************/
XBT_PUBLIC(sg_size_t) MSG_file_read(msg_file_t fd, sg_size_t size);
XBT_PUBLIC(sg_size_t) MSG_file_write(msg_file_t fd, sg_size_t size);
-XBT_PUBLIC(msg_file_t) MSG_file_open(const char* fullpath, void* data);
-XBT_PUBLIC(int) MSG_file_close(msg_file_t fd);
-XBT_PUBLIC(msg_error_t) MSG_file_move(msg_file_t fd, const char* fullpath);
XBT_PUBLIC(msg_error_t) MSG_file_rcopy(msg_file_t fd, msg_host_t host, const char* fullpath);
XBT_PUBLIC(msg_error_t) MSG_file_rmove(msg_file_t fd, msg_host_t host, const char* fullpath);
/************************** Storage handling ***********************************/
SG_BEGIN_DECL()
XBT_PUBLIC(void) sg_storage_file_system_init();
+XBT_PUBLIC(sg_file_t) sg_file_open(const char* fullpath, void* data);
+XBT_PUBLIC(void) sg_file_close(sg_file_t fd);
XBT_PUBLIC(const char*) sg_file_get_name(sg_file_t fd);
XBT_PUBLIC(sg_size_t) sg_file_get_size(sg_file_t fd);
XBT_PUBLIC(xbt_dict_t) sg_host_get_storage_content(sg_host_t host);
+#define MSG_file_open(fullpath, data) sg_file_open(fullpath, data)
+#define MSG_file_close(fd) sg_file_close(fd)
#define MSG_file_get_name(fd) sg_file_get_name(fd)
#define MSG_file_get_size(fd) sg_file_get_size(fd)
#define MSG_file_dump(fd) sg_file_dump(fd)
SIMIX_function_register_process_create(MSG_process_create_from_SIMIX);
SIMIX_function_register_process_cleanup(MSG_process_cleanup_from_SIMIX);
-
- simgrid::MsgHostExt::EXTENSION_ID = simgrid::s4u::Host::extension_create<simgrid::MsgHostExt>();
- simgrid::s4u::Host::onCreation.connect([](simgrid::s4u::Host& host) {
- host.extension_set<simgrid::MsgHostExt>(new simgrid::MsgHostExt());
- });
}
if(MC_is_active()){
XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(msg);
-simgrid::xbt::Extension<simgrid::s4u::Host, simgrid::MsgHostExt> simgrid::MsgHostExt::EXTENSION_ID;
-
extern "C" {
-int sg_storage_max_file_descriptors = 1024;
-
/** @addtogroup m_host_management
* (#msg_host_t) and the functions for managing it.
*
* \see #msg_file_t
*/
-static int MSG_host_get_file_descriptor_id(msg_host_t host)
-{
- simgrid::MsgHostExt* priv = host->extension<simgrid::MsgHostExt>();
- if (priv->file_descriptor_table == nullptr) {
- priv->file_descriptor_table = new std::vector<int>(sg_storage_max_file_descriptors);
- std::iota(priv->file_descriptor_table->rbegin(), priv->file_descriptor_table->rend(), 0); // Fill with ..., 1, 0.
- }
- xbt_assert(not priv->file_descriptor_table->empty(), "Too much files are opened! Some have to be closed.");
- int desc = priv->file_descriptor_table->back();
- priv->file_descriptor_table->pop_back();
- return desc;
-}
-
-static void MSG_host_release_file_descriptor_id(msg_host_t host, int id)
-{
- host->extension<simgrid::MsgHostExt>()->file_descriptor_table->push_back(id);
-}
-
/** \ingroup msg_file
* \brief Read a file (local or remote)
*
return write_size;
}
-/** \ingroup msg_file
- * \brief Opens the file whose name is the string pointed to by path
- *
- * \param fullpath is the file location on the storage
- * \param data user data to attach to the file
- *
- * \return An #msg_file_t associated to the file
- */
-msg_file_t MSG_file_open(const char* fullpath, void* data)
-{
- msg_file_t fd = new simgrid::s4u::File(fullpath, MSG_host_self());
- fd->desc_id = MSG_host_get_file_descriptor_id(MSG_host_self());
- fd->setUserdata(data);
- return fd;
-}
-
-/** \ingroup msg_file
- * \brief Close the file
- *
- * \param fd is the file to close
- * \return 0 on success or 1 on error
- */
-int MSG_file_close(msg_file_t fd)
-{
- MSG_host_release_file_descriptor_id(MSG_host_self(), fd->desc_id);
- delete fd;
-
- return MSG_OK;
-}
-
/**
* \ingroup msg_file
* \brief Copy a file to another location on a remote host.
/* Find the host where the file is physically located and read it */
msg_storage_t storage_src = file->localStorage;
msg_host_t src_host = storage_src->getHost();
- MSG_file_seek(file, 0, SEEK_SET);
+ file->seek(0, SEEK_SET);
sg_size_t read_size = file->read(file->size());
/* Find the host that owns the storage where the file has to be copied */
msg_error_t MSG_file_rmove (msg_file_t file, msg_host_t host, const char* fullpath)
{
msg_error_t res = MSG_file_rcopy(file, host, fullpath);
- MSG_file_unlink(file);
+ file->unlink();
return res;
}
#include <atomic>
/**************** datatypes **********************************/
-/**************************** Host Extension *********************************/
-namespace simgrid {
-class MsgHostExt {
-public:
- static simgrid::xbt::Extension<s4u::Host, MsgHostExt> EXTENSION_ID;
-
- ~MsgHostExt() { delete file_descriptor_table; }
- std::vector<int>* file_descriptor_table = nullptr; // Created lazily on need
-};
-}
/********************************* Task **************************************/
struct s_simdata_task_t {
public:
File(std::string fullpath, void* userdata);
File(std::string fullpath, sg_host_t host, void* userdata);
- ~File() = default;
+ ~File();
/** Retrieves the path to the file */
const char* getPath() { return fullpath_.c_str(); }
class FileSystemStorageExt {
public:
- static simgrid::xbt::Extension<simgrid::s4u::Storage, FileSystemStorageExt> EXTENSION_ID;
- explicit FileSystemStorageExt(simgrid::s4u::Storage* ptr);
+ static simgrid::xbt::Extension<Storage, FileSystemStorageExt> EXTENSION_ID;
+ explicit FileSystemStorageExt(Storage* ptr);
~FileSystemStorageExt();
std::map<std::string, sg_size_t>* parseContent(std::string filename);
std::map<std::string, sg_size_t>* getContent() { return content_; }
sg_size_t usedSize_ = 0;
sg_size_t size_ = 0;
};
+
+class FileDescriptorHostExt {
+public:
+ static simgrid::xbt::Extension<Host, FileDescriptorHostExt> EXTENSION_ID;
+ FileDescriptorHostExt() = default;
+ ~FileDescriptorHostExt() { delete file_descriptor_table; }
+ std::vector<int>* file_descriptor_table = nullptr; // Created lazily on need
+};
}
} // namespace simgrid::s4u
#include <fstream>
XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_file, "S4U files");
+int sg_storage_max_file_descriptors = 1024;
namespace simgrid {
namespace s4u {
-simgrid::xbt::Extension<s4u::Storage, FileSystemStorageExt> FileSystemStorageExt::EXTENSION_ID;
+simgrid::xbt::Extension<Storage, FileSystemStorageExt> FileSystemStorageExt::EXTENSION_ID;
+simgrid::xbt::Extension<Host, FileDescriptorHostExt> FileDescriptorHostExt::EXTENSION_ID;
File::File(std::string fullpath, void* userdata) : File(fullpath, Host::current(), userdata){};
localStorage = st;
+ // assign a file descriptor id to the newly opened File
+ FileDescriptorHostExt* ext = host->extension<simgrid::s4u::FileDescriptorHostExt>();
+ if (ext->file_descriptor_table == nullptr) {
+ ext->file_descriptor_table = new std::vector<int>(sg_storage_max_file_descriptors);
+ std::iota(ext->file_descriptor_table->rbegin(), ext->file_descriptor_table->rend(), 0); // Fill with ..., 1, 0.
+ }
+ xbt_assert(not ext->file_descriptor_table->empty(), "Too much files are opened! Some have to be closed.");
+ desc_id = ext->file_descriptor_table->back();
+ ext->file_descriptor_table->pop_back();
+
XBT_DEBUG("\tOpen file '%s'", path_.c_str());
std::map<std::string, sg_size_t>* content = localStorage->extension<FileSystemStorageExt>()->getContent();
// if file does not exist create an empty file
}
}
+File::~File()
+{
+ Host::current()->extension<simgrid::s4u::FileDescriptorHostExt>()->file_descriptor_table->push_back(desc_id);
+}
+
void File::dump()
{
XBT_INFO("File Descriptor information:\n"
}
using simgrid::s4u::FileSystemStorageExt;
+using simgrid::s4u::FileDescriptorHostExt;
static void onStorageCreation(simgrid::s4u::Storage& st)
{
delete st.extension<FileSystemStorageExt>();
}
+static void onHostCreation(simgrid::s4u::Host& host)
+{
+ host.extension_set<FileDescriptorHostExt>(new FileDescriptorHostExt());
+}
+
/* **************************** Public interface *************************** */
SG_BEGIN_DECL()
void sg_storage_file_system_init()
{
- if (FileSystemStorageExt::EXTENSION_ID.valid())
- return;
+ if (not FileSystemStorageExt::EXTENSION_ID.valid()) {
+ FileSystemStorageExt::EXTENSION_ID = simgrid::s4u::Storage::extension_create<FileSystemStorageExt>();
+ simgrid::s4u::Storage::onCreation.connect(&onStorageCreation);
+ simgrid::s4u::Storage::onDestruction.connect(&onStorageDestruction);
+ }
+
+ if (not FileDescriptorHostExt::EXTENSION_ID.valid()) {
+ FileDescriptorHostExt::EXTENSION_ID = simgrid::s4u::Host::extension_create<FileDescriptorHostExt>();
+ simgrid::s4u::Host::onCreation.connect(&onHostCreation);
+ }
+}
- FileSystemStorageExt::EXTENSION_ID = simgrid::s4u::Storage::extension_create<FileSystemStorageExt>();
+sg_file_t sg_file_open(const char* fullpath, void* data)
+{
+ return new simgrid::s4u::File(fullpath, data);
+}
- simgrid::s4u::Storage::onCreation.connect(&onStorageCreation);
- simgrid::s4u::Storage::onDestruction.connect(&onStorageDestruction);
+void sg_file_close(sg_file_t fd)
+{
+ delete fd;
}
const char* sg_file_get_name(sg_file_t fd)