Logo AND Algorithmique Numérique Distribuée

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