1 /* Copyright (c) 2015-2019. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "simgrid/plugins/file_system.h"
7 #include "simgrid/s4u/Actor.hpp"
8 #include "src/surf/HostImpl.hpp"
9 #include "xbt/config.hpp"
12 #include <boost/algorithm/string.hpp>
13 #include <boost/algorithm/string/join.hpp>
14 #include <boost/algorithm/string/split.hpp>
18 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_file, "S4U files");
19 int sg_storage_max_file_descriptors = 1024;
23 simgrid::xbt::Extension<Storage, FileSystemStorageExt> FileSystemStorageExt::EXTENSION_ID;
24 simgrid::xbt::Extension<Host, FileDescriptorHostExt> FileDescriptorHostExt::EXTENSION_ID;
26 File::File(const std::string& fullpath, void* userdata) : File(fullpath, Host::current(), userdata){};
28 File::File(const std::string& fullpath, sg_host_t host, void* userdata) : fullpath_(fullpath), userdata_(userdata)
30 // this cannot fail because we get a xbt_die if the mountpoint does not exist
31 Storage* st = nullptr;
32 size_t longest_prefix_length = 0;
33 XBT_DEBUG("Search for storage name for '%s' on '%s'", fullpath_.c_str(), host->get_cname());
35 for (auto const& mnt : host->get_mounted_storages()) {
36 XBT_DEBUG("See '%s'", mnt.first.c_str());
37 mount_point_ = fullpath_.substr(0, mnt.first.length());
39 if (mount_point_ == mnt.first && mnt.first.length() > longest_prefix_length) {
40 /* The current mount name is found in the full path and is bigger than the previous*/
41 longest_prefix_length = mnt.first.length();
45 if (longest_prefix_length > 0) { /* Mount point found, split fullpath_ into mount_name and path+filename*/
46 mount_point_ = fullpath_.substr(0, longest_prefix_length);
47 path_ = fullpath_.substr(longest_prefix_length, fullpath_.length());
49 xbt_die("Can't find mount point for '%s' on '%s'", fullpath_.c_str(), host->get_cname());
53 // assign a file descriptor id to the newly opened File
54 FileDescriptorHostExt* ext = host->extension<simgrid::s4u::FileDescriptorHostExt>();
55 if (ext->file_descriptor_table == nullptr) {
56 ext->file_descriptor_table.reset(new std::vector<int>(sg_storage_max_file_descriptors));
57 std::iota(ext->file_descriptor_table->rbegin(), ext->file_descriptor_table->rend(), 0); // Fill with ..., 1, 0.
59 xbt_assert(not ext->file_descriptor_table->empty(), "Too much files are opened! Some have to be closed.");
60 desc_id = ext->file_descriptor_table->back();
61 ext->file_descriptor_table->pop_back();
63 XBT_DEBUG("\tOpen file '%s'", path_.c_str());
64 std::map<std::string, sg_size_t>* content = local_storage_->extension<FileSystemStorageExt>()->get_content();
65 // if file does not exist create an empty file
66 auto sz = content->find(path_);
67 if (sz != content->end()) {
71 content->insert({path_, size_});
72 XBT_DEBUG("File '%s' was not found, file created.", path_.c_str());
78 Host::current()->extension<simgrid::s4u::FileDescriptorHostExt>()->file_descriptor_table->push_back(desc_id);
83 XBT_INFO("File Descriptor information:\n"
84 "\t\tFull path: '%s'\n"
86 "\t\tMount point: '%s'\n"
87 "\t\tStorage Id: '%s'\n"
88 "\t\tStorage Type: '%s'\n"
89 "\t\tFile Descriptor Id: %d",
90 get_path(), size_, mount_point_.c_str(), local_storage_->get_cname(), local_storage_->get_type(), desc_id);
93 sg_size_t File::read(sg_size_t size)
95 if (size_ == 0) /* Nothing to read, return */
98 /* Find the host where the file is physically located and read it */
99 Host* host = local_storage_->get_host();
100 XBT_DEBUG("READ %s on disk '%s'", get_path(), local_storage_->get_cname());
101 // if the current position is close to the end of the file, we may not be able to read the requested size
102 sg_size_t read_size = local_storage_->read(std::min(size, size_ - current_position_));
103 current_position_ += read_size;
105 if (host->get_name() != Host::current()->get_name()) {
106 /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
107 XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", host->get_cname(), read_size);
108 std::vector<Host*> m_host_list = {Host::current(), host};
109 std::vector<double> flops_amount = {0., 0.};
110 std::vector<double> bytes_amount = {0., 0., static_cast<double>(read_size), 0.};
112 this_actor::parallel_execute(m_host_list, flops_amount, bytes_amount);
118 /** @brief Write into a file (local or remote)
120 * @param size of the file to write
121 * @return the number of bytes successfully write or -1 if an error occurred
123 sg_size_t File::write(sg_size_t size, int write_inside)
125 if (size == 0) /* Nothing to write, return */
128 /* Find the host where the file is physically located (remote or local)*/
129 Host* host = local_storage_->get_host();
131 if (host->get_name() != Host::current()->get_name()) {
132 /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
133 XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", host->get_cname(), size);
134 std::vector<Host*> m_host_list = {Host::current(), host};
135 std::vector<double> flops_amount = {0, 0};
136 std::vector<double> bytes_amount = {0, static_cast<double>(size), 0, 0};
138 this_actor::parallel_execute(m_host_list, flops_amount, bytes_amount);
141 XBT_DEBUG("WRITE %s on disk '%s'. size '%llu/%llu' '%llu:%llu'", get_path(), local_storage_->get_cname(), size, size_, sg_storage_get_size_used(local_storage_), sg_storage_get_size(local_storage_));
142 // If the storage is full before even starting to write
143 if (sg_storage_get_size_used(local_storage_) >= sg_storage_get_size(local_storage_))
145 /* Substract the part of the file that might disappear from the used sized on the storage element */
146 sg_size_t write_size = local_storage_->write(size);
147 current_position_ += write_size;
149 local_storage_->extension<FileSystemStorageExt>()->decr_used_size(size_ - current_position_);
150 local_storage_->extension<FileSystemStorageExt>()->incr_used_size(write_size);
151 size_ = current_position_;
152 }else if(current_position_>size_){
153 size_ = current_position_;
155 std::map<std::string, sg_size_t>* content = local_storage_->extension<FileSystemStorageExt>()->get_content();
157 content->erase(path_);
158 content->insert({path_, size_});
163 sg_size_t File::size()
168 void File::seek(sg_offset_t offset)
170 current_position_ = offset;
173 void File::seek(sg_offset_t offset, int origin)
177 current_position_ = offset;
180 current_position_ += offset;
183 current_position_ = size_ + offset;
190 sg_size_t File::tell()
192 return current_position_;
195 void File::move(const std::string& fullpath)
197 /* Check if the new full path is on the same mount point */
198 if (fullpath.compare(0, mount_point_.length(), mount_point_) == 0) {
199 std::map<std::string, sg_size_t>* content = local_storage_->extension<FileSystemStorageExt>()->get_content();
200 auto sz = content->find(path_);
201 if (sz != content->end()) { // src file exists
202 sg_size_t new_size = sz->second;
203 content->erase(path_);
204 std::string path = fullpath.substr(mount_point_.length(), fullpath.length());
205 content->insert({path.c_str(), new_size});
206 XBT_DEBUG("Move file from %s to %s, size '%llu'", path_.c_str(), fullpath.c_str(), new_size);
208 XBT_WARN("File %s doesn't exist", path_.c_str());
211 XBT_WARN("New full path %s is not on the same mount point: %s.", fullpath.c_str(), mount_point_.c_str());
217 /* Check if the file is on local storage */
218 std::map<std::string, sg_size_t>* content = local_storage_->extension<FileSystemStorageExt>()->get_content();
220 if (content->find(path_) == content->end()) {
221 XBT_WARN("File %s is not on disk %s. Impossible to unlink", path_.c_str(), local_storage_->get_cname());
224 XBT_DEBUG("UNLINK %s on disk '%s'", path_.c_str(), local_storage_->get_cname());
225 local_storage_->extension<FileSystemStorageExt>()->decr_used_size(size_);
227 // Remove the file from storage
228 content->erase(fullpath_);
234 int File::remote_copy(sg_host_t host, const char* fullpath)
236 /* Find the host where the file is physically located and read it */
237 Storage* storage_src = local_storage_;
238 Host* src_host = storage_src->get_host();
240 XBT_DEBUG("READ %s on disk '%s'", get_path(), local_storage_->get_cname());
241 // if the current position is close to the end of the file, we may not be able to read the requested size
242 sg_size_t read_size = local_storage_->read(size_);
243 current_position_ += read_size;
245 /* Find the host that owns the storage where the file has to be copied */
246 Storage* storage_dest = nullptr;
248 size_t longest_prefix_length = 0;
250 for (auto const& elm : host->get_mounted_storages()) {
251 std::string mount_point = std::string(fullpath).substr(0, elm.first.size());
252 if (mount_point == elm.first && elm.first.length() > longest_prefix_length) {
253 /* The current mount name is found in the full path and is bigger than the previous*/
254 longest_prefix_length = elm.first.length();
255 storage_dest = elm.second;
259 if (storage_dest != nullptr) {
260 /* Mount point found, retrieve the host the storage is attached to */
261 dst_host = storage_dest->get_host();
263 XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, host->get_cname());
267 XBT_DEBUG("Initiate data transfer of %llu bytes between %s and %s.", read_size, src_host->get_cname(),
268 storage_dest->get_host()->get_cname());
269 std::vector<Host*> m_host_list = {src_host, dst_host};
270 std::vector<double> flops_amount = {0, 0};
271 std::vector<double> bytes_amount = {0, static_cast<double>(read_size), 0, 0};
273 this_actor::parallel_execute(m_host_list, flops_amount, bytes_amount);
275 /* Create file on remote host, write it and close it */
276 File* fd = new File(fullpath, dst_host, nullptr);
277 sg_size_t write_size = fd->local_storage_->write(read_size);
278 fd->local_storage_->extension<FileSystemStorageExt>()->incr_used_size(write_size);
279 (*(fd->local_storage_->extension<FileSystemStorageExt>()->get_content()))[path_] = size_;
284 int File::remote_move(sg_host_t host, const char* fullpath)
286 int res = remote_copy(host, fullpath);
291 FileSystemStorageExt::FileSystemStorageExt(simgrid::s4u::Storage* ptr)
293 content_.reset(parse_content(ptr->get_impl()->content_name));
294 size_ = ptr->get_impl()->size_;
297 std::map<std::string, sg_size_t>* FileSystemStorageExt::parse_content(const std::string& filename)
299 if (filename.empty())
302 std::map<std::string, sg_size_t>* parse_content = new std::map<std::string, sg_size_t>();
304 std::ifstream* fs = surf_ifsopen(filename);
307 std::vector<std::string> tokens;
309 std::getline(*fs, line);
311 if (line.length() > 0) {
312 boost::split(tokens, line, boost::is_any_of(" \t"), boost::token_compress_on);
313 xbt_assert(tokens.size() == 2, "Parse error in %s: %s", filename.c_str(), line.c_str());
314 sg_size_t size = std::stoull(tokens.at(1));
317 parse_content->insert({tokens.front(), size});
319 } while (not fs->eof());
321 return parse_content;
326 using simgrid::s4u::FileSystemStorageExt;
327 using simgrid::s4u::FileDescriptorHostExt;
329 static void on_storage_creation(simgrid::s4u::Storage& st)
331 st.extension_set(new FileSystemStorageExt(&st));
334 static void on_host_creation(simgrid::s4u::Host& host)
336 host.extension_set<FileDescriptorHostExt>(new FileDescriptorHostExt());
339 /* **************************** Public interface *************************** */
340 void sg_storage_file_system_init()
342 sg_storage_max_file_descriptors = 1024;
343 simgrid::config::bind_flag(sg_storage_max_file_descriptors, "storage/max_file_descriptors",
344 "Maximum number of concurrently opened files per host. Default is 1024");
346 if (not FileSystemStorageExt::EXTENSION_ID.valid()) {
347 FileSystemStorageExt::EXTENSION_ID = simgrid::s4u::Storage::extension_create<FileSystemStorageExt>();
348 simgrid::s4u::Storage::on_creation.connect(&on_storage_creation);
351 if (not FileDescriptorHostExt::EXTENSION_ID.valid()) {
352 FileDescriptorHostExt::EXTENSION_ID = simgrid::s4u::Host::extension_create<FileDescriptorHostExt>();
353 simgrid::s4u::Host::on_creation.connect(&on_host_creation);
357 sg_file_t sg_file_open(const char* fullpath, void* data)
359 return new simgrid::s4u::File(fullpath, data);
362 sg_size_t sg_file_read(sg_file_t fd, sg_size_t size)
364 return fd->read(size);
367 sg_size_t sg_file_write(sg_file_t fd, sg_size_t size)
369 return fd->write(size);
372 void sg_file_close(sg_file_t fd)
377 const char* sg_file_get_name(sg_file_t fd)
379 xbt_assert((fd != nullptr), "Invalid file descriptor");
380 return fd->get_path();
383 sg_size_t sg_file_get_size(sg_file_t fd)
388 void sg_file_dump(sg_file_t fd)
393 void* sg_file_get_data(sg_file_t fd)
395 return fd->get_userdata();
398 void sg_file_set_data(sg_file_t fd, void* data)
400 fd->set_userdata(data);
404 * @brief Set the file position indicator in the sg_file_t by adding offset bytes
405 * to the position specified by origin (either SEEK_SET, SEEK_CUR, or SEEK_END).
407 * @param fd : file object that identifies the stream
408 * @param offset : number of bytes to offset from origin
409 * @param origin : Position used as reference for the offset. It is specified by one of the following constants defined
410 * in \<stdio.h\> exclusively to be used as arguments for this function (SEEK_SET = beginning of file,
411 * SEEK_CUR = current position of the file pointer, SEEK_END = end of file)
413 void sg_file_seek(sg_file_t fd, sg_offset_t offset, int origin)
415 fd->seek(offset, origin);
418 sg_size_t sg_file_tell(sg_file_t fd)
423 void sg_file_move(sg_file_t fd, const char* fullpath)
428 void sg_file_unlink(sg_file_t fd)
435 * @brief Copy a file to another location on a remote host.
436 * @param file : the file to move
437 * @param host : the remote host where the file has to be copied
438 * @param fullpath : the complete path destination on the remote host
439 * @return If successful, the function returns 0. Otherwise, it returns -1.
441 int sg_file_rcopy(sg_file_t file, sg_host_t host, const char* fullpath)
443 return file->remote_copy(host, fullpath);
447 * @brief Move a file to another location on a remote host.
448 * @param file : the file to move
449 * @param host : the remote host where the file has to be moved
450 * @param fullpath : the complete path destination on the remote host
451 * @return If successful, the function returns 0. Otherwise, it returns -1.
453 int sg_file_rmove(sg_file_t file, sg_host_t host, const char* fullpath)
455 return file->remote_move(host, fullpath);
458 sg_size_t sg_storage_get_size_free(sg_storage_t st)
460 return st->extension<FileSystemStorageExt>()->get_size() - st->extension<FileSystemStorageExt>()->get_used_size();
463 sg_size_t sg_storage_get_size_used(sg_storage_t st)
465 return st->extension<FileSystemStorageExt>()->get_used_size();
468 sg_size_t sg_storage_get_size(sg_storage_t st)
470 return st->extension<FileSystemStorageExt>()->get_size();
473 xbt_dict_t sg_storage_get_content(sg_storage_t storage)
475 std::map<std::string, sg_size_t>* content = storage->extension<simgrid::s4u::FileSystemStorageExt>()->get_content();
476 // Note: ::operator delete is ok here (no destructor called) since the dict elements are of POD type sg_size_t.
477 xbt_dict_t content_as_dict = xbt_dict_new_homogeneous(::operator delete);
479 for (auto const& entry : *content) {
480 sg_size_t* psize = new sg_size_t;
481 *psize = entry.second;
482 xbt_dict_set(content_as_dict, entry.first.c_str(), psize, nullptr);
484 return content_as_dict;
487 xbt_dict_t sg_host_get_storage_content(sg_host_t host)
489 xbt_assert((host != nullptr), "Invalid parameters");
490 xbt_dict_t contents = xbt_dict_new_homogeneous(nullptr);
491 for (auto const& elm : host->get_mounted_storages())
492 xbt_dict_set(contents, elm.first.c_str(), sg_storage_get_content(elm.second), nullptr);