1 /* Copyright (c) 2006-2017. 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/msg.h"
7 #include "simgrid/s4u/Host.hpp"
8 #include "simgrid/s4u/Storage.hpp"
9 #include "simgrid/simix.hpp"
10 #include "src/plugins/file_system/FileSystem.hpp"
11 #include "src/surf/StorageImpl.hpp"
12 #include <unordered_map>
16 template class Extendable<simgrid::s4u::Storage>;
21 void getStorageList(std::map<std::string, Storage*>* whereTo)
23 for (auto const& s : *surf::StorageImpl::storagesMap())
24 whereTo->insert({s.first, &(s.second->piface_)}); // Convert each entry into its interface
27 Storage* Storage::byName(std::string name)
29 surf::StorageImpl* res = surf::StorageImpl::byName(name);
35 const std::string& Storage::getName() const
37 return pimpl_->getName();
40 const char* Storage::getCname() const
42 return pimpl_->getCname();
45 const char* Storage::getType()
47 return pimpl_->typeId_.c_str();
50 Host* Storage::getHost()
55 std::map<std::string, std::string>* Storage::getProperties()
57 return simgrid::simix::kernelImmediate([this] { return pimpl_->getProperties(); });
60 const char* Storage::getProperty(std::string key)
62 return this->pimpl_->getProperty(key);
65 void Storage::setProperty(std::string key, std::string value)
67 simgrid::simix::kernelImmediate([this, key, value] { this->pimpl_->setProperty(key, value); });
70 sg_size_t Storage::read(sg_size_t size)
72 return simcall_storage_read(pimpl_, size);
75 sg_size_t Storage::write(sg_size_t size)
77 return simcall_storage_write(pimpl_, size);
83 simgrid::xbt::signal<void(s4u::Storage&)> Storage::onCreation;
84 simgrid::xbt::signal<void(s4u::Storage&)> Storage::onDestruction;
87 } /* namespace simgrid */
89 /* **************************** Public C interface *************************** */
91 /** @addtogroup sg_storage_management
92 * (#sg_storage_t) and the functions for managing it.
95 /** \ingroup sg_storage_management
97 * \brief Returns the name of the #sg_storage_t.
99 * This functions checks whether a storage is a valid pointer or not and return its name.
101 const char* sg_storage_get_name(sg_storage_t storage)
103 xbt_assert((storage != nullptr), "Invalid parameters");
104 return storage->getCname();
107 const char* sg_storage_get_host(sg_storage_t storage)
109 xbt_assert((storage != nullptr), "Invalid parameters");
110 return storage->getHost()->getCname();
113 /** \ingroup sg_storage_management
114 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this storage
115 * \param storage a storage
116 * \return a dict containing the properties
118 xbt_dict_t sg_storage_get_properties(sg_storage_t storage)
120 xbt_assert((storage != nullptr), "Invalid parameters (storage is nullptr)");
121 xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
122 std::map<std::string, std::string>* props = storage->getProperties();
123 if (props == nullptr)
125 for (auto const& elm : *props) {
126 xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
131 /** \ingroup sg_storage_management
132 * \brief Change the value of a given storage property
134 * \param storage a storage
135 * \param name a property name
136 * \param value what to change the property to
138 void sg_storage_set_property_value(sg_storage_t storage, const char* name, char* value)
140 storage->setProperty(name, value);
143 /** \ingroup sg_storage_management
144 * \brief Returns the value of a given storage property
146 * \param storage a storage
147 * \param name a property name
148 * \return value of a property (or nullptr if property not set)
150 const char* sg_storage_get_property_value(sg_storage_t storage, const char* name)
152 return storage->getProperty(name);
155 /** \ingroup sg_storage_management
156 * \brief Finds a sg_storage_t using its name.
157 * \param name the name of a storage
158 * \return the corresponding storage
160 sg_storage_t sg_storage_get_by_name(const char* name)
162 return simgrid::s4u::Storage::byName(name);
165 /** \ingroup sg_storage_management
166 * \brief Returns a dynar containing all the storage elements declared at a given point of time
168 xbt_dynar_t sg_storages_as_dynar()
170 std::map<std::string, simgrid::s4u::Storage*>* storage_list = new std::map<std::string, simgrid::s4u::Storage*>;
171 simgrid::s4u::getStorageList(storage_list);
172 xbt_dynar_t res = xbt_dynar_new(sizeof(sg_storage_t), nullptr);
173 for (auto const& s : *storage_list)
174 xbt_dynar_push(res, &(s.second));
179 void* sg_storage_get_data(sg_storage_t storage)
181 xbt_assert((storage != nullptr), "Invalid parameters");
182 return storage->getUserdata();
185 void sg_storage_set_data(sg_storage_t storage, void* data)
187 storage->setUserdata(data);
190 sg_size_t sg_storage_read(sg_storage_t storage, sg_size_t size)
192 return storage->read(size);
195 sg_size_t sg_storage_write(sg_storage_t storage, sg_size_t size)
197 return storage->write(size);