1 /* Copyright (c) 2006-2018. 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/s4u/Engine.hpp"
7 #include "simgrid/s4u/Host.hpp"
8 #include "simgrid/s4u/Io.hpp"
9 #include "simgrid/s4u/Storage.hpp"
10 #include "simgrid/storage.h"
11 #include "src/surf/StorageImpl.hpp"
15 template class Extendable<simgrid::s4u::Storage>;
20 simgrid::xbt::signal<void(s4u::Storage&)> Storage::on_creation;
21 simgrid::xbt::signal<void(s4u::Storage&)> Storage::on_destruction;
22 simgrid::xbt::signal<void(s4u::Storage&)> Storage::on_state_change;
24 Storage::Storage(std::string name, surf::StorageImpl* pimpl) : pimpl_(pimpl), name_(name)
26 simgrid::s4u::Engine::get_instance()->storage_register(name, this);
29 Storage* Storage::by_name(std::string name)
31 return Engine::get_instance()->storage_by_name(name);
34 Storage* Storage::by_name_or_null(std::string name)
36 return Engine::get_instance()->storage_by_name_or_null(name);
39 const char* Storage::get_type()
41 return pimpl_->typeId_.c_str();
44 std::unordered_map<std::string, std::string>* Storage::get_properties()
46 return simgrid::simix::simcall([this] { return pimpl_->get_properties(); });
49 const char* Storage::get_property(std::string key)
51 return this->pimpl_->get_property(key);
54 void Storage::set_property(std::string key, std::string value)
56 simgrid::simix::simcall([this, key, value] { this->pimpl_->set_property(key, value); });
59 IoPtr Storage::io_init(sg_size_t size, Io::OpType type)
61 IoPtr res = IoPtr(new Io(size, type));
66 IoPtr Storage::read_async(sg_size_t size)
69 IoPtr res = io_init(size, Io::OpType::READ);
74 sg_size_t Storage::read(sg_size_t size)
76 IoPtr i = io_init(size, Io::OpType::READ);
78 return i->get_performed_ioops();
81 IoPtr Storage::write_async(sg_size_t size)
84 IoPtr res = io_init(size, Io::OpType::WRITE);
89 sg_size_t Storage::write(sg_size_t size)
91 IoPtr i = io_init(size, Io::OpType::WRITE);
93 return i->get_performed_ioops();
97 } /* namespace simgrid */
99 /* **************************** Public C interface *************************** */
101 /** @addtogroup sg_storage_management
102 * (#sg_storage_t) and the functions for managing it.
105 /** @ingroup sg_storage_management
107 * @brief Returns the name of the #sg_storage_t.
109 * This functions checks whether a storage is a valid pointer or not and return its name.
111 const char* sg_storage_get_name(sg_storage_t storage)
113 xbt_assert((storage != nullptr), "Invalid parameters");
114 return storage->get_cname();
117 const char* sg_storage_get_host(sg_storage_t storage)
119 xbt_assert((storage != nullptr), "Invalid parameters");
120 return storage->get_host()->get_cname();
123 /** @ingroup sg_storage_management
124 * @brief Returns a xbt_dict_t consisting of the list of properties assigned to this storage
125 * @param storage a storage
126 * @return a dict containing the properties
128 xbt_dict_t sg_storage_get_properties(sg_storage_t storage)
130 xbt_assert((storage != nullptr), "Invalid parameters (storage is nullptr)");
131 xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
132 std::unordered_map<std::string, std::string>* props = storage->get_properties();
133 if (props == nullptr)
135 for (auto const& elm : *props) {
136 xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
141 /** @ingroup sg_storage_management
142 * @brief Change the value of a given storage property
144 * @param storage a storage
145 * @param name a property name
146 * @param value what to change the property to
148 void sg_storage_set_property_value(sg_storage_t storage, const char* name, const char* value)
150 storage->set_property(name, value);
153 /** @ingroup sg_storage_management
154 * @brief Returns the value of a given storage property
156 * @param storage a storage
157 * @param name a property name
158 * @return value of a property (or nullptr if property not set)
160 const char* sg_storage_get_property_value(sg_storage_t storage, const char* name)
162 return storage->get_property(name);
165 /** @ingroup sg_storage_management
166 * @brief Finds a sg_storage_t using its name.
167 * @param name the name of a storage
168 * @return the corresponding storage
170 sg_storage_t sg_storage_get_by_name(const char* name)
172 return simgrid::s4u::Storage::by_name(name);
175 /** @ingroup sg_storage_management
176 * @brief Returns a dynar containing all the storage elements declared at a given point of time
178 xbt_dynar_t sg_storages_as_dynar()
180 std::vector<simgrid::s4u::Storage*> storage_list = simgrid::s4u::Engine::get_instance()->get_all_storages();
181 xbt_dynar_t res = xbt_dynar_new(sizeof(sg_storage_t), nullptr);
182 for (auto const& s : storage_list)
183 xbt_dynar_push(res, &s);
187 void* sg_storage_get_data(sg_storage_t storage)
189 xbt_assert((storage != nullptr), "Invalid parameters");
190 return storage->get_data();
193 void sg_storage_set_data(sg_storage_t storage, void* data)
195 storage->set_data(data);
198 sg_size_t sg_storage_read(sg_storage_t storage, sg_size_t size)
200 return storage->read(size);
203 sg_size_t sg_storage_write(sg_storage_t storage, sg_size_t size)
205 return storage->write(size);