Logo AND Algorithmique Numérique Distribuée

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