Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Explicit instanciation of xbt::Extendable<> specializations.
[simgrid.git] / src / s4u / s4u_Storage.cpp
1 /* Copyright (c) 2006-2020. The SimGrid Team. All rights reserved.          */
2
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. */
5
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"
12
13 namespace simgrid {
14
15 template class xbt::Extendable<s4u::Storage>;
16
17 namespace s4u {
18
19 xbt::signal<void(Storage&)> Storage::on_creation;
20 xbt::signal<void(Storage const&)> Storage::on_destruction;
21 xbt::signal<void(Storage const&)> Storage::on_state_change;
22
23 Storage::Storage(const std::string& name, kernel::resource::StorageImpl* pimpl) : pimpl_(pimpl), name_(name)
24 {
25   Engine::get_instance()->storage_register(name_, this);
26 }
27
28 Storage* Storage::by_name(const std::string& name)
29 {
30   return Engine::get_instance()->storage_by_name(name);
31 }
32
33 Storage* Storage::by_name_or_null(const std::string& name)
34 {
35   return Engine::get_instance()->storage_by_name_or_null(name);
36 }
37
38 const char* Storage::get_type() const
39 {
40   return pimpl_->typeId_.c_str();
41 }
42
43 const std::unordered_map<std::string, std::string>* Storage::get_properties() const
44 {
45   return pimpl_->get_properties();
46 }
47
48 const char* Storage::get_property(const std::string& key) const
49 {
50   return this->pimpl_->get_property(key);
51 }
52
53 void Storage::set_property(const std::string& key, const std::string& value)
54 {
55   kernel::actor::simcall([this, &key, &value] { this->pimpl_->set_property(key, value); });
56 }
57
58 IoPtr Storage::io_init(sg_size_t size, Io::OpType type)
59 {
60   return IoPtr(new Io(this, size, type));
61 }
62
63 IoPtr Storage::read_async(sg_size_t size)
64 {
65   return IoPtr(io_init(size, Io::OpType::READ))->start();
66 }
67
68 sg_size_t Storage::read(sg_size_t size)
69 {
70   return IoPtr(io_init(size, Io::OpType::READ))->start()->wait()->get_performed_ioops();
71 }
72
73 IoPtr Storage::write_async(sg_size_t size)
74 {
75   return IoPtr(io_init(size, Io::OpType::WRITE)->start());
76 }
77
78 sg_size_t Storage::write(sg_size_t size)
79 {
80   return IoPtr(io_init(size, Io::OpType::WRITE))->start()->wait()->get_performed_ioops();
81 }
82
83 } // namespace s4u
84 } // namespace simgrid
85
86 /* **************************** Public C interface *************************** */
87
88 /** @addtogroup sg_storage_management
89  * (#sg_storage_t) and the functions for managing it.
90  */
91
92 /** @ingroup sg_storage_management
93  *
94  * @brief Returns the name of the #sg_storage_t.
95  *
96  * This functions checks whether a storage is a valid pointer or not and return its name.
97  */
98 const char* sg_storage_get_name(const_sg_storage_t storage)
99 {
100   xbt_assert((storage != nullptr), "Invalid parameters");
101   return storage->get_cname();
102 }
103
104 const char* sg_storage_get_host(const_sg_storage_t storage)
105 {
106   xbt_assert((storage != nullptr), "Invalid parameters");
107   return storage->get_host()->get_cname();
108 }
109
110 /** @ingroup sg_storage_management
111  * @brief Returns a xbt_dict_t consisting of the list of properties assigned to this storage
112  * @param storage a storage
113  * @return a dict containing the properties
114  */
115 xbt_dict_t sg_storage_get_properties(const_sg_storage_t storage)
116 {
117   xbt_assert((storage != nullptr), "Invalid parameters (storage is nullptr)");
118   xbt_dict_t as_dict                        = xbt_dict_new_homogeneous(xbt_free_f);
119   const std::unordered_map<std::string, std::string>* props = storage->get_properties();
120   if (props == nullptr)
121     return nullptr;
122   for (auto const& elm : *props) {
123     xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()));
124   }
125   return as_dict;
126 }
127
128 /** @ingroup sg_storage_management
129  * @brief Change the value of a given storage property
130  *
131  * @param storage a storage
132  * @param name a property name
133  * @param value what to change the property to
134  */
135 void sg_storage_set_property_value(sg_storage_t storage, const char* name, const char* value)
136 {
137   storage->set_property(name, value);
138 }
139
140 /** @ingroup sg_storage_management
141  * @brief Returns the value of a given storage property
142  *
143  * @param storage a storage
144  * @param name a property name
145  * @return value of a property (or nullptr if property not set)
146  */
147 const char* sg_storage_get_property_value(const_sg_storage_t storage, const char* name)
148 {
149   return storage->get_property(name);
150 }
151
152 /** @ingroup sg_storage_management
153  * @brief Finds a sg_storage_t using its name.
154  * @param name the name of a storage
155  * @return the corresponding storage
156  */
157 sg_storage_t sg_storage_get_by_name(const char* name)
158 {
159   return simgrid::s4u::Storage::by_name(name);
160 }
161
162 /** @ingroup sg_storage_management
163  * @brief Returns a dynar containing all the storage elements declared at a given point of time
164  */
165 xbt_dynar_t sg_storages_as_dynar()
166 {
167   std::vector<simgrid::s4u::Storage*> storage_list = simgrid::s4u::Engine::get_instance()->get_all_storages();
168   xbt_dynar_t res                                  = xbt_dynar_new(sizeof(sg_storage_t), nullptr);
169   for (auto const& s : storage_list)
170     xbt_dynar_push(res, &s);
171   return res;
172 }
173
174 void* sg_storage_get_data(const_sg_storage_t storage)
175 {
176   xbt_assert((storage != nullptr), "Invalid parameters");
177   return storage->get_data();
178 }
179
180 void sg_storage_set_data(sg_storage_t storage, void* data)
181 {
182   storage->set_data(data);
183 }
184
185 sg_size_t sg_storage_read(sg_storage_t storage, sg_size_t size)
186 {
187   return storage->read(size);
188 }
189
190 sg_size_t sg_storage_write(sg_storage_t storage, sg_size_t size)
191 {
192   return storage->write(size);
193 }