Logo AND Algorithmique Numérique Distribuée

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