Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c26d4a556c142c49aaad564ca5cda20ada6aa6f3
[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/Storage.hpp"
9 #include "simgrid/storage.h"
10 #include "src/surf/StorageImpl.hpp"
11
12 #include <string>
13 #include <unordered_map>
14
15 namespace simgrid {
16 namespace xbt {
17 template class Extendable<simgrid::s4u::Storage>;
18 }
19
20 namespace s4u {
21
22 void XBT_ATTRIB_DEPRECATED_v322(
23     "simgrid::s4u::getStorageList() is deprecated in favor of Engine::getAllStorages(). Please switch before v3.22")
24     getStorageList(std::map<std::string, Storage*>* whereTo)
25 {
26   for (auto const& s : simgrid::s4u::Engine::getInstance()->getAllStorages())
27     whereTo->insert({s->get_name(), s});
28 }
29
30 Storage::Storage(std::string name, surf::StorageImpl* pimpl) : pimpl_(pimpl), name_(name)
31 {
32   simgrid::s4u::Engine::getInstance()->add_storage(name, this);
33 }
34
35 Storage* Storage::byName(std::string name)
36 {
37   return Engine::getInstance()->storage_by_name_or_null(name);
38 }
39
40 const std::string& Storage::get_name() const
41 {
42   return name_;
43 }
44
45 const char* Storage::get_cname() const
46 {
47   return name_.c_str();
48 }
49
50 const char* Storage::getType()
51 {
52   return pimpl_->typeId_.c_str();
53 }
54
55 Host* Storage::getHost()
56 {
57   return attached_to_;
58 }
59
60 std::map<std::string, std::string>* Storage::getProperties()
61 {
62   return simgrid::simix::kernelImmediate([this] { return pimpl_->getProperties(); });
63 }
64
65 const char* Storage::getProperty(std::string key)
66 {
67   return this->pimpl_->getProperty(key);
68 }
69
70 void Storage::setProperty(std::string key, std::string value)
71 {
72   simgrid::simix::kernelImmediate([this, key, value] { this->pimpl_->setProperty(key, value); });
73 }
74
75 sg_size_t Storage::read(sg_size_t size)
76 {
77   return simcall_storage_read(pimpl_, size);
78 }
79
80 sg_size_t Storage::write(sg_size_t size)
81 {
82   return simcall_storage_write(pimpl_, size);
83 }
84
85 /*************
86  * Callbacks *
87  *************/
88 simgrid::xbt::signal<void(s4u::Storage&)> Storage::onCreation;
89 simgrid::xbt::signal<void(s4u::Storage&)> Storage::onDestruction;
90
91 } /* namespace s4u */
92 } /* namespace simgrid */
93
94 /* **************************** Public C interface *************************** */
95
96 /** @addtogroup sg_storage_management
97  * (#sg_storage_t) and the functions for managing it.
98  */
99
100 /** \ingroup sg_storage_management
101  *
102  * \brief Returns the name of the #sg_storage_t.
103  *
104  * This functions checks whether a storage is a valid pointer or not and return its name.
105  */
106 const char* sg_storage_get_name(sg_storage_t storage)
107 {
108   xbt_assert((storage != nullptr), "Invalid parameters");
109   return storage->get_cname();
110 }
111
112 const char* sg_storage_get_host(sg_storage_t storage)
113 {
114   xbt_assert((storage != nullptr), "Invalid parameters");
115   return storage->getHost()->get_cname();
116 }
117
118 /** \ingroup sg_storage_management
119  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this storage
120  * \param storage a storage
121  * \return a dict containing the properties
122  */
123 xbt_dict_t sg_storage_get_properties(sg_storage_t storage)
124 {
125   xbt_assert((storage != nullptr), "Invalid parameters (storage is nullptr)");
126   xbt_dict_t as_dict                        = xbt_dict_new_homogeneous(xbt_free_f);
127   std::map<std::string, std::string>* props = storage->getProperties();
128   if (props == nullptr)
129     return nullptr;
130   for (auto const& elm : *props) {
131     xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
132   }
133   return as_dict;
134 }
135
136 /** \ingroup sg_storage_management
137  * \brief Change the value of a given storage property
138  *
139  * \param storage a storage
140  * \param name a property name
141  * \param value what to change the property to
142  */
143 void sg_storage_set_property_value(sg_storage_t storage, const char* name, const char* value)
144 {
145   storage->setProperty(name, value);
146 }
147
148 /** \ingroup sg_storage_management
149  * \brief Returns the value of a given storage property
150  *
151  * \param storage a storage
152  * \param name a property name
153  * \return value of a property (or nullptr if property not set)
154  */
155 const char* sg_storage_get_property_value(sg_storage_t storage, const char* name)
156 {
157   return storage->getProperty(name);
158 }
159
160 /** \ingroup sg_storage_management
161  * \brief Finds a sg_storage_t using its name.
162  * \param name the name of a storage
163  * \return the corresponding storage
164  */
165 sg_storage_t sg_storage_get_by_name(const char* name)
166 {
167   return simgrid::s4u::Storage::byName(name);
168 }
169
170 /** \ingroup sg_storage_management
171  * \brief Returns a dynar containing all the storage elements declared at a given point of time
172  */
173 xbt_dynar_t sg_storages_as_dynar()
174 {
175   std::vector<simgrid::s4u::Storage*> storage_list = simgrid::s4u::Engine::getInstance()->getAllStorages();
176   xbt_dynar_t res                                  = xbt_dynar_new(sizeof(sg_storage_t), nullptr);
177   for (auto const& s : storage_list)
178     xbt_dynar_push(res, &s);
179   return res;
180 }
181
182 void* sg_storage_get_data(sg_storage_t storage)
183 {
184   xbt_assert((storage != nullptr), "Invalid parameters");
185   return storage->getUserdata();
186 }
187
188 void sg_storage_set_data(sg_storage_t storage, void* data)
189 {
190   storage->setUserdata(data);
191 }
192
193 sg_size_t sg_storage_read(sg_storage_t storage, sg_size_t size)
194 {
195   return storage->read(size);
196 }
197
198 sg_size_t sg_storage_write(sg_storage_t storage, sg_size_t size)
199 {
200   return storage->write(size);
201 }