Logo AND Algorithmique Numérique Distribuée

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