Logo AND Algorithmique Numérique Distribuée

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