Logo AND Algorithmique Numérique Distribuée

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