Logo AND Algorithmique Numérique Distribuée

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