Logo AND Algorithmique Numérique Distribuée

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