Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a77c79b3c328644f42f04e0f4dfbb6127f6e0c85
[simgrid.git] / src / surf / StorageImpl.hpp
1 /* Copyright (c) 2004-2018. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <xbt/base.h>
8 #include <xbt/signal.hpp>
9
10 #include "simgrid/s4u/Storage.hpp"
11 #include "src/kernel/resource/Action.hpp"
12 #include "src/kernel/resource/Model.hpp"
13 #include "src/kernel/resource/Resource.hpp"
14 #include "src/surf/PropertyHolder.hpp"
15 #include "surf_interface.hpp"
16 #include <map>
17
18 #ifndef STORAGE_INTERFACE_HPP_
19 #define STORAGE_INTERFACE_HPP_
20
21 namespace simgrid {
22 namespace surf {
23
24 /***********
25  * Classes *
26  ***********/
27
28 class StorageAction;
29
30 /*************
31  * Callbacks *
32  *************/
33
34 /** @ingroup SURF_callbacks
35  * @brief Callbacks handler which emit the callbacks after Storage creation *
36  * @details Callback functions have the following signature: `void(Storage*)`
37  */
38 XBT_PUBLIC_DATA(simgrid::xbt::signal<void(StorageImpl*)>) storageCreatedCallbacks;
39
40 /** @ingroup SURF_callbacks
41  * @brief Callbacks handler which emit the callbacks after Storage destruction *
42  * @details Callback functions have the following signature: `void(StoragePtr)`
43  */
44 XBT_PUBLIC_DATA(simgrid::xbt::signal<void(StorageImpl*)>) storageDestructedCallbacks;
45
46 /** @ingroup SURF_callbacks
47  * @brief Callbacks handler which emit the callbacks after Storage State changed *
48  * @details Callback functions have the following signature: `void(StorageAction *action, int previouslyOn, int
49  * currentlyOn)`
50  */
51 XBT_PUBLIC_DATA(simgrid::xbt::signal<void(StorageImpl*, int, int)>) storageStateChangedCallbacks;
52
53 /** @ingroup SURF_callbacks
54  * @brief Callbacks handler which emit the callbacks after StorageAction State changed *
55  * @details Callback functions have the following signature: `void(StorageAction *action, simgrid::surf::Action::State
56  * old, simgrid::surf::Action::State current)`
57  */
58 XBT_PUBLIC_DATA(simgrid::xbt::signal<void(StorageAction*, Action::State, Action::State)>)
59 storageActionStateChangedCallbacks;
60
61 /*********
62  * Model *
63  *********/
64 /** @ingroup SURF_storage_interface
65  * @brief SURF storage model interface class
66  * @details A model is an object which handle the interactions between its Resources and its Actions
67  */
68 class StorageModel : public Model {
69 public:
70   StorageModel();
71   ~StorageModel();
72
73   virtual StorageImpl* createStorage(std::string id, std::string type_id, std::string content_name,
74                                      std::string attach) = 0;
75
76   std::vector<StorageImpl*> p_storageList;
77 };
78
79 /************
80  * Resource *
81  ************/
82 /** @ingroup SURF_storage_interface
83  * @brief SURF storage interface class
84  * @details A Storage represent a storage unit (e.g.: hard drive, usb key)
85  */
86 class StorageImpl : public kernel::resource::Resource, public PropertyHolder {
87 public:
88   /** @brief Storage constructor */
89   StorageImpl(Model* model, std::string name, lmm_system_t maxminSystem, double bread, double bwrite,
90               std::string type_id, std::string content_name, sg_size_t size, std::string attach);
91
92   ~StorageImpl() override;
93
94   /** @brief Public interface */
95   s4u::Storage piface_;
96   static StorageImpl* byName(std::string name);
97
98   /** @brief Check if the Storage is used (if an action currently uses its resources) */
99   bool isUsed() override;
100
101   void apply_event(tmgr_trace_event_t event, double value) override;
102
103   void turnOn() override;
104   void turnOff() override;
105
106   /**
107    * @brief Read a file
108    *
109    * @param size The size in bytes to read
110    * @return The StorageAction corresponding to the reading
111    */
112   virtual StorageAction* read(sg_size_t size) = 0;
113
114   /**
115    * @brief Write a file
116    *
117    * @param size The size in bytes to write
118    * @return The StorageAction corresponding to the writing
119    */
120   virtual StorageAction* write(sg_size_t size) = 0;
121   virtual std::string getHost() { return attach_; }
122
123   static std::unordered_map<std::string, StorageImpl*>* storagesMap() { return StorageImpl::storages; }
124
125   kernel::lmm::Constraint* constraintWrite_; /* Constraint for maximum write bandwidth*/
126   kernel::lmm::Constraint* constraintRead_;  /* Constraint for maximum write bandwidth*/
127
128   std::string typeId_;
129   std::string content_name; // Only used at parsing time then goes to the FileSystemExtension
130   sg_size_t size_;          // Only used at parsing time then goes to the FileSystemExtension
131
132 private:
133   static std::unordered_map<std::string, StorageImpl*>* storages;
134   // Name of the host to which this storage is attached. Only used at platform parsing time, then the interface stores
135   // the Host directly.
136   std::string attach_;
137 };
138
139 /**********
140  * Action *
141  **********/
142
143 /** @ingroup SURF_storage_interface
144  * @brief The possible type of action for the storage component
145  */
146 enum e_surf_action_storage_type_t {
147   READ = 0, /**< Read a file */
148   WRITE     /**< Write in a file */
149 };
150
151 /** @ingroup SURF_storage_interface
152  * @brief SURF storage action interface class
153  */
154 class StorageAction : public Action {
155 public:
156   /**
157    * @brief StorageAction constructor
158    *
159    * @param model The StorageModel associated to this StorageAction
160    * @param cost The cost of this  NetworkAction in [TODO]
161    * @param failed [description]
162    * @param storage The Storage associated to this StorageAction
163    * @param type [description]
164    */
165   StorageAction(Model* model, double cost, bool failed, StorageImpl* storage, e_surf_action_storage_type_t type)
166       : Action(model, cost, failed), type_(type), storage_(storage){};
167
168   /**
169  * @brief StorageAction constructor
170  *
171  * @param model The StorageModel associated to this StorageAction
172  * @param cost The cost of this  StorageAction in [TODO]
173  * @param failed [description]
174  * @param var The lmm variable associated to this StorageAction if it is part of a LMM component
175  * @param storage The Storage associated to this StorageAction
176  * @param type [description]
177  */
178   StorageAction(Model* model, double cost, bool failed, kernel::lmm::Variable* var, StorageImpl* storage,
179                 e_surf_action_storage_type_t type)
180       : Action(model, cost, failed, var), type_(type), storage_(storage){};
181
182   void setState(simgrid::surf::Action::State state) override;
183
184   e_surf_action_storage_type_t type_;
185   StorageImpl* storage_;
186 };
187
188 class StorageType {
189 public:
190   std::string id;
191   std::string model;
192   std::string content;
193   std::map<std::string, std::string>* properties;
194   std::map<std::string, std::string>* model_properties;
195   sg_size_t size;
196   StorageType(std::string id, std::string model, std::string content, std::map<std::string, std::string>* properties,
197               std::map<std::string, std::string>* model_properties, sg_size_t size)
198       : id(id), model(model), content(content), properties(properties), model_properties(model_properties), size(size)
199   {
200   }
201 };
202 }
203 }
204
205 #endif /* STORAGE_INTERFACE_HPP_ */