Logo AND Algorithmique Numérique Distribuée

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