Logo AND Algorithmique Numérique Distribuée

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