From: Frederic Suter Date: Mon, 26 Jun 2017 14:50:01 +0000 (+0200) Subject: WIP. crude surf_file_t to FileImpl conversion X-Git-Tag: v3_17~516 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/8fea398a5a9ca83c4e6009daf964d59869388e6c WIP. crude surf_file_t to FileImpl conversion --- diff --git a/include/simgrid/forward.h b/include/simgrid/forward.h index 96e8194eac..3776800807 100644 --- a/include/simgrid/forward.h +++ b/include/simgrid/forward.h @@ -47,6 +47,7 @@ namespace surf { class LinkImpl; class HostImpl; class StorageImpl; + class FileImpl; } namespace trace_mgr { class trace; diff --git a/src/include/surf/surf.h b/src/include/surf/surf.h index 22b5e44107..191c3508b0 100644 --- a/src/include/surf/surf.h +++ b/src/include/surf/surf.h @@ -54,6 +54,7 @@ typedef simgrid::surf::StorageModel surf_StorageModel; typedef simgrid::surf::Resource surf_Resource; typedef simgrid::surf::HostImpl surf_Host; typedef simgrid::surf::Action surf_Action; +typedef simgrid::surf::FileImpl surf_File; #else @@ -67,6 +68,7 @@ typedef struct surf_StorageModel surf_StorageModel; typedef struct surf_Resource surf_Resource; typedef struct surf_Host surf_Host; typedef struct surf_Action surf_Action; +typedef struct surf_File surf_File; #endif @@ -82,6 +84,7 @@ typedef surf_HostModel *surf_host_model_t; typedef surf_NetworkModel *surf_network_model_t; typedef surf_StorageModel *surf_storage_model_t; typedef surf_Storage* surf_storage_t; +typedef surf_File* surf_file_t; /** @ingroup SURF_c_bindings * \brief Action structure @@ -93,7 +96,6 @@ typedef surf_Storage* surf_storage_t; */ typedef surf_Action *surf_action_t; -typedef struct surf_file *surf_file_t; /** \brief Resource model description */ diff --git a/src/surf/FileImpl.hpp b/src/surf/FileImpl.hpp new file mode 100644 index 0000000000..938a1b5945 --- /dev/null +++ b/src/surf/FileImpl.hpp @@ -0,0 +1,34 @@ +/* Copyright (c) 2017. 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. */ + +#ifndef SRC_SURF_FILEIMPL_HPP_ +#define SRC_SURF_FILEIMPL_HPP_ + +namespace simgrid { +namespace surf { + +class FileImpl { +public: + FileImpl(const char* path, const char* mount, sg_size_t size) : path_(path), mount_point_(mount), size_(size) {} + ~FileImpl() = default; + + std::string name() { return path_; } + const char* cname() { return path_.c_str(); } + const char* mount() { return mount_point_.c_str(); } + sg_size_t size() { return size_; } + void setSize(sg_size_t size) { size_ = size; } + void setPosition(sg_size_t size) { current_position_ = size; } + void incrPosition(sg_size_t incr) { current_position_ += incr; } + sg_size_t tell() { return current_position_; } +private: + std::string path_; + std::string mount_point_; + sg_size_t size_; + sg_size_t current_position_ = 0; +}; +} +} +#endif /* SRC_SURF_FILEIMPL_HPP_ */ diff --git a/src/surf/HostImpl.cpp b/src/surf/HostImpl.cpp index fa047d27f9..9a804338f6 100644 --- a/src/surf/HostImpl.cpp +++ b/src/surf/HostImpl.cpp @@ -4,6 +4,7 @@ * under the terms of the license (GNU LGPL) which comes with this package. */ #include "src/plugins/vm/VirtualMachineImpl.hpp" +#include "src/surf/FileImpl.hpp" #include XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_host, surf, "Logging specific to the SURF host module"); @@ -114,22 +115,22 @@ void HostImpl::getAttachedStorageList(std::vector* storages) Action* HostImpl::close(surf_file_t fd) { - simgrid::surf::StorageImpl* st = findStorageOnMountList(fd->mount); - XBT_DEBUG("CLOSE %s on disk '%s'", fd->name, st->cname()); + simgrid::surf::StorageImpl* st = findStorageOnMountList(fd->mount()); + XBT_DEBUG("CLOSE %s on disk '%s'", fd->cname(), st->cname()); return st->close(fd); } Action* HostImpl::read(surf_file_t fd, sg_size_t size) { - simgrid::surf::StorageImpl* st = findStorageOnMountList(fd->mount); - XBT_DEBUG("READ %s on disk '%s'", fd->name, st->cname()); + simgrid::surf::StorageImpl* st = findStorageOnMountList(fd->mount()); + XBT_DEBUG("READ %s on disk '%s'", fd->cname(), st->cname()); return st->read(fd, size); } Action* HostImpl::write(surf_file_t fd, sg_size_t size) { - simgrid::surf::StorageImpl* st = findStorageOnMountList(fd->mount); - XBT_DEBUG("WRITE %s on disk '%s'", fd->name, st->cname()); + simgrid::surf::StorageImpl* st = findStorageOnMountList(fd->mount()); + XBT_DEBUG("WRITE %s on disk '%s'", fd->cname(), st->cname()); return st->write(fd, size); } @@ -140,17 +141,17 @@ int HostImpl::unlink(surf_file_t fd) return -1; } else { - simgrid::surf::StorageImpl* st = findStorageOnMountList(fd->mount); + simgrid::surf::StorageImpl* st = findStorageOnMountList(fd->mount()); /* Check if the file is on this storage */ - if (st->content_->find(fd->name) == st->content_->end()) { - XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name, st->cname()); + if (st->content_->find(fd->cname()) == st->content_->end()) { + XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->cname(), st->cname()); return -1; } else { - XBT_DEBUG("UNLINK %s on disk '%s'", fd->name, st->cname()); - st->usedSize_ -= fd->size; + XBT_DEBUG("UNLINK %s on disk '%s'", fd->cname(), st->cname()); + st->usedSize_ -= fd->size(); // Remove the file from storage - st->content_->erase(fd->name); + st->content_->erase(fd->cname()); return 0; } @@ -159,25 +160,25 @@ int HostImpl::unlink(surf_file_t fd) sg_size_t HostImpl::getSize(surf_file_t fd) { - return fd->size; + return fd->size(); } sg_size_t HostImpl::fileTell(surf_file_t fd) { - return fd->current_position; + return fd->tell(); } int HostImpl::fileSeek(surf_file_t fd, sg_offset_t offset, int origin) { switch (origin) { case SEEK_SET: - fd->current_position = offset; + fd->setPosition(offset); return 0; case SEEK_CUR: - fd->current_position += offset; + fd->incrPosition(offset); return 0; case SEEK_END: - fd->current_position = fd->size + offset; + fd->setPosition(fd->size() + offset); return 0; default: return -1; @@ -187,21 +188,21 @@ int HostImpl::fileSeek(surf_file_t fd, sg_offset_t offset, int origin) int HostImpl::fileMove(surf_file_t fd, const char* fullpath) { /* Check if the new full path is on the same mount point */ - if (not strncmp((const char*)fd->mount, fullpath, strlen(fd->mount))) { - std::map* content = findStorageOnMountList(fd->mount)->content_; - if (content->find(fd->name) != content->end()) { // src file exists - sg_size_t new_size = content->at(std::string(fd->name)); - content->erase(fd->name); - std::string path = std::string(fullpath).substr(strlen(fd->mount), strlen(fullpath)); + if (not strncmp(fd->mount(), fullpath, strlen(fd->mount()))) { + std::map* content = findStorageOnMountList(fd->mount())->content_; + if (content->find(fd->name()) != content->end()) { // src file exists + sg_size_t new_size = content->at(fd->name()); + content->erase(fd->name()); + std::string path = std::string(fullpath).substr(strlen(fd->mount()), strlen(fullpath)); content->insert({path.c_str(), new_size}); - XBT_DEBUG("Move file from %s to %s, size '%llu'", fd->name, fullpath, new_size); + XBT_DEBUG("Move file from %s to %s, size '%llu'", fd->cname(), fullpath, new_size); return 0; } else { - XBT_WARN("File %s doesn't exist", fd->name); + XBT_WARN("File %s doesn't exist", fd->cname()); return -1; } } else { - XBT_WARN("New full path %s is not on the same mount point: %s. Action has been canceled.", fullpath, fd->mount); + XBT_WARN("New full path %s is not on the same mount point: %s. Action has been canceled.", fullpath, fd->mount()); return -1; } } diff --git a/src/surf/StorageImpl.hpp b/src/surf/StorageImpl.hpp index fbb744956f..4cfb23ddc6 100644 --- a/src/surf/StorageImpl.hpp +++ b/src/surf/StorageImpl.hpp @@ -223,7 +223,7 @@ public: e_surf_action_storage_type_t type_; StorageImpl* storage_; - surf_file_t file_; + FileImpl* file_; double progress_; }; } @@ -239,11 +239,4 @@ typedef struct s_storage_type { } s_storage_type_t; typedef s_storage_type_t* storage_type_t; -typedef struct surf_file { - char* name; - char* mount; - sg_size_t size; - sg_size_t current_position; -} s_surf_file_t; - #endif /* STORAGE_INTERFACE_HPP_ */ diff --git a/src/surf/storage_n11.cpp b/src/surf/storage_n11.cpp index 3d90a4e809..579ea96a7c 100644 --- a/src/surf/storage_n11.cpp +++ b/src/surf/storage_n11.cpp @@ -102,7 +102,7 @@ void StorageN11Model::updateActionsState(double /*now*/, double delta) long int incr = current_progress; XBT_DEBUG("%s:\n\t progress = %.2f, current_progress = %.2f, incr = %ld, lrint(1) = %ld, lrint(2) = %ld", - action->file_->name, action->progress_, current_progress, incr, + action->file_->cname(), action->progress_, current_progress, incr, lrint(action->progress_ + current_progress), lrint(action->progress_) + incr); /* take care of rounding error accumulation */ @@ -112,12 +112,12 @@ void StorageN11Model::updateActionsState(double /*now*/, double delta) action->progress_ += current_progress; action->storage_->usedSize_ += incr; // disk usage - action->file_->current_position += incr; // current_position + action->file_->incrPosition(incr); // current_position // which becomes the new file size - action->file_->size = action->file_->current_position; + action->file_->setSize(action->file_->tell()); - action->storage_->content_->erase(action->file_->name); - action->storage_->content_->insert({action->file_->name, action->file_->size}); + action->storage_->content_->erase(action->file_->cname()); + action->storage_->content_->insert({action->file_->cname(), action->file_->size()}); } action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta); @@ -162,11 +162,7 @@ StorageAction *StorageN11::open(const char* mount, const char* path) content_->insert({path, size}); XBT_DEBUG("File '%s' was not found, file created.",path); } - surf_file_t file = xbt_new0(s_surf_file_t,1); - file->name = xbt_strdup(path); - file->size = size; - file->mount = xbt_strdup(mount); - file->current_position = 0; + FileImpl* file = new FileImpl(path, mount, size); StorageAction* action = new StorageN11Action(model(), 0, isOff(), this, OPEN); action->file_ = file; @@ -176,7 +172,7 @@ StorageAction *StorageN11::open(const char* mount, const char* path) StorageAction *StorageN11::close(surf_file_t fd) { - XBT_DEBUG("\tClose file '%s' size '%llu'", fd->name, fd->size); + XBT_DEBUG("\tClose file '%s' size '%llu'", fd->cname(), fd->size()); // unref write actions from storage for (std::vector::iterator it = writeActions_.begin(); it != writeActions_.end();) { StorageAction *write_action = *it; @@ -187,25 +183,23 @@ StorageAction *StorageN11::close(surf_file_t fd) ++it; } } - free(fd->name); - free(fd->mount); - xbt_free(fd); + delete fd; StorageAction* action = new StorageN11Action(model(), 0, isOff(), this, CLOSE); return action; } StorageAction *StorageN11::read(surf_file_t fd, sg_size_t size) { - if(fd->current_position + size > fd->size){ - if (fd->current_position > fd->size){ + if (fd->tell() + size > fd->size()) { + if (fd->tell() > fd->size()) { size = 0; } else { - size = fd->size - fd->current_position; + size = fd->size() - fd->tell(); } - fd->current_position = fd->size; + fd->setPosition(fd->size()); } else - fd->current_position += size; + fd->incrPosition(size); StorageAction* action = new StorageN11Action(model(), size, isOff(), this, READ); return action; @@ -213,13 +207,12 @@ StorageAction *StorageN11::read(surf_file_t fd, sg_size_t size) StorageAction *StorageN11::write(surf_file_t fd, sg_size_t size) { - char *filename = fd->name; - XBT_DEBUG("\tWrite file '%s' size '%llu/%llu'",filename,size,fd->size); + XBT_DEBUG("\tWrite file '%s' size '%llu/%llu'", fd->cname(), size, fd->size()); StorageAction* action = new StorageN11Action(model(), size, isOff(), this, WRITE); action->file_ = fd; /* Substract the part of the file that might disappear from the used sized on the storage element */ - usedSize_ -= (fd->size - fd->current_position); + usedSize_ -= (fd->size() - fd->tell()); // If the storage is full before even starting to write if(usedSize_==size_) { action->setState(Action::State::failed); diff --git a/src/surf/storage_n11.hpp b/src/surf/storage_n11.hpp index 72703a1093..16016d2361 100644 --- a/src/surf/storage_n11.hpp +++ b/src/surf/storage_n11.hpp @@ -6,6 +6,7 @@ #include +#include "FileImpl.hpp" #include "StorageImpl.hpp" #ifndef STORAGE_N11_HPP_ diff --git a/tools/cmake/DefinePackages.cmake b/tools/cmake/DefinePackages.cmake index 54e622333b..4c2e631cb8 100644 --- a/tools/cmake/DefinePackages.cmake +++ b/tools/cmake/DefinePackages.cmake @@ -53,6 +53,7 @@ set(EXTRA_DIST src/surf/xml/simgrid_dtd.c src/surf/xml/surfxml_sax_cb.cpp + src/surf/FileImpl.hpp src/surf/StorageImpl.hpp src/surf/storage_n11.hpp src/surf/surf_interface.hpp