Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
code simplification around Host::onDestruction
[simgrid.git] / src / surf / host_interface.hpp
1 /* Copyright (c) 2004-2015. 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 "surf_interface.hpp"
8 #include "storage_interface.hpp"
9 #include "cpu_interface.hpp"
10 #include "network_interface.hpp"
11 #include "src/surf/PropertyHolder.hpp"
12
13 #include <xbt/base.h>
14
15 #ifndef SURF_HOST_INTERFACE_HPP_
16 #define SURF_HOST_INTERFACE_HPP_
17
18 /***********
19  * Classes *
20  ***********/
21
22 namespace simgrid {
23 namespace surf {
24
25 class XBT_PRIVATE HostModel;
26 class XBT_PRIVATE Host;
27 class XBT_PRIVATE HostAction;
28
29
30 }
31 }
32
33 /*********
34  * Tools *
35  *********/
36 XBT_PUBLIC_DATA(simgrid::surf::HostModel*) surf_host_model;
37 XBT_PUBLIC(void) host_add_traces();
38
39 /*********
40  * Model *
41  *********/
42
43 namespace simgrid {
44 namespace surf {
45
46 /** @ingroup SURF_host_interface
47  * @brief SURF Host model interface class
48  * @details A model is an object which handle the interactions between its Resources and its Actions
49  */
50 class HostModel : public Model{
51 public:
52   HostModel() : Model() {}
53   ~HostModel() {}
54
55   virtual Host *createHost(const char *name, RoutingEdge *net, Cpu *cpu, xbt_dict_t props)=0;
56   void addTraces() override {DIE_IMPOSSIBLE;}
57
58   virtual void adjustWeightOfDummyCpuActions();
59   virtual Action *executeParallelTask(int host_nb,
60                                       sg_host_t *host_list,
61                                                                           double *flops_amount,
62                                                                           double *bytes_amount,
63                                                                           double rate)=0;
64
65   bool shareResourcesIsIdempotent() override {return true;}
66 };
67
68 /************
69  * Resource *
70  ************/
71 /** @ingroup SURF_host_interface
72  * @brief SURF Host interface class
73  * @details An host represents a machine with a aggregation of a Cpu, a RoutingEdge and a Storage
74  */
75 class Host : public simgrid::surf::Resource,
76                  public simgrid::surf::PropertyHolder {
77 public:
78   static simgrid::xbt::Extension<simgrid::Host, Host> EXTENSION_ID;
79
80   /* callbacks */
81   static simgrid::surf::signal<void(Host*)> onCreation;    /** Called on each newly created object */
82   static simgrid::surf::signal<void(Host*)> onDestruction; /** Called just before destructing an object */
83   static simgrid::surf::signal<void(simgrid::surf::Host*, e_surf_resource_state_t, e_surf_resource_state_t)> onStateChange;
84
85 public:
86   static void classInit();
87   /**
88    * @brief Host constructor
89    *
90    * @param model HostModel associated to this Host
91    * @param name The name of the Host
92    * @param props Dictionary of properties associated to this Host
93    * @param storage The Storage associated to this Host
94    * @param netElm The RoutingEdge associated to this Host
95    * @param cpu The Cpu associated to this Host
96    */
97   Host(simgrid::surf::Model *model, const char *name, xbt_dict_t props,
98                       xbt_dynar_t storage, RoutingEdge *netElm, Cpu *cpu);
99
100   /**
101    * @brief Host constructor
102    *
103    * @param model HostModel associated to this Host
104    * @param name The name of the Host
105    * @param props Dictionary of properties associated to this Host
106    * @param constraint The lmm constraint associated to this Host if it is part of a LMM component
107    * @param storage The Storage associated to this Host
108    * @param netElm The RoutingEdge associated to this Host
109    * @param cpu The Cpu associated to this Host
110    */
111   Host(simgrid::surf::Model *model, const char *name, xbt_dict_t props,
112       lmm_constraint_t constraint, xbt_dynar_t storage, RoutingEdge *netElm,
113       Cpu *cpu);
114
115   /* Host destruction logic */
116   /**************************/
117 protected:
118   ~Host();
119 public:
120         void destroy(); // Must be called instead of the destructor
121 private:
122         bool currentlyDestroying_ = false;
123
124
125 public:
126   void attach(simgrid::Host* host);
127   void setState(e_surf_resource_state_t state);
128
129   /**
130    * @brief Execute some quantity of computation
131    *
132    * @param flops_amount The value of the processing amount (in flop) needed to process
133    * @return The CpuAction corresponding to the processing
134    * @see Cpu
135    */
136   virtual Action *execute(double flops_amount)=0;
137
138   /**
139    * @brief Make a process sleep for duration seconds
140    *
141    * @param duration The number of seconds to sleep
142    * @return The CpuAction corresponding to the sleeping
143    * @see Cpu
144    */
145   virtual Action *sleep(double duration)=0;
146
147   /** @brief Return the storage of corresponding mount point */
148   virtual simgrid::surf::Storage *findStorageOnMountList(const char* storage);
149
150   /** @brief Get the xbt_dict_t of mount_point: Storage */
151   virtual xbt_dict_t getMountedStorageList();
152
153   /** @brief Get the xbt_dynar_t of storages attached to the Host */
154   virtual xbt_dynar_t getAttachedStorageList();
155
156   /**
157    * @brief Open a file
158    *
159    * @param fullpath The full path to the file
160    * @return The StorageAction corresponding to the opening
161    */
162   virtual Action *open(const char* fullpath);
163
164   /**
165    * @brief Close a file
166    *
167    * @param fd The file descriptor to close
168    * @return The StorageAction corresponding to the closing
169    */
170   virtual Action *close(surf_file_t fd);
171
172   /**
173    * @brief Unlink a file
174    * @details [long description]
175    *
176    * @param fd [description]
177    * @return [description]
178    */
179   virtual int unlink(surf_file_t fd);
180
181   /**
182    * @brief Get the size in bytes of the file
183    *
184    * @param fd The file descriptor to read
185    * @return The size in bytes of the file
186    */
187   virtual sg_size_t getSize(surf_file_t fd);
188
189   /**
190    * @brief Read a file
191    *
192    * @param fd The file descriptor to read
193    * @param size The size in bytes to read
194    * @return The StorageAction corresponding to the reading
195    */
196   virtual Action *read(surf_file_t fd, sg_size_t size);
197
198   /**
199    * @brief Write a file
200    *
201    * @param fd The file descriptor to write
202    * @param size The size in bytes to write
203    * @return The StorageAction corresponding to the writing
204    */
205   virtual Action *write(surf_file_t fd, sg_size_t size);
206
207   /**
208    * @brief Get the informations of a file descriptor
209    * @details The returned xbt_dynar_t contains:
210    *  - the size of the file,
211    *  - the mount point,
212    *  - the storage name,
213    *  - the storage typeId,
214    *  - the storage content type
215    *
216    * @param fd The file descriptor
217    * @return An xbt_dynar_t with the file informations
218    */
219   virtual xbt_dynar_t getInfo(surf_file_t fd);
220
221   /**
222    * @brief Get the current position of the file descriptor
223    *
224    * @param fd The file descriptor
225    * @return The current position of the file descriptor
226    */
227   virtual sg_size_t fileTell(surf_file_t fd);
228
229   /**
230    * @brief Set the position indicator associated with the file descriptor to a new position
231    * @details [long description]
232    *
233    * @param fd The file descriptor
234    * @param offset The offset from the origin
235    * @param origin Position used as a reference for the offset
236    *  - SEEK_SET: beginning of the file
237    *  - SEEK_CUR: current position indicator
238    *  - SEEK_END: end of the file
239    * @return MSG_OK if successful, otherwise MSG_TASK_CANCELED
240    */
241   virtual int fileSeek(surf_file_t fd, sg_offset_t offset, int origin);
242
243   /**
244    * @brief Move a file to another location on the *same mount point*.
245    * @details [long description]
246    *
247    * @param fd The file descriptor
248    * @param fullpath The new full path
249    * @return MSG_OK if successful, MSG_TASK_CANCELED and a warning if the new
250    * full path is not on the same mount point
251    */
252   virtual int fileMove(surf_file_t fd, const char* fullpath);
253
254 public:
255   xbt_dynar_t p_storage;
256   RoutingEdge *p_netElm;
257   Cpu *p_cpu;
258   simgrid::Host* p_host = nullptr;
259
260   /** @brief Get the list of virtual machines on the current Host */
261   xbt_dynar_t getVms();
262
263   /* common with vm */
264   /** @brief Retrieve a copy of the parameters of that VM/PM
265    *  @details The ramsize and overcommit fields are used on the PM too */
266   void getParams(vm_params_t params);
267   /** @brief Sets the params of that VM/PM */
268   void setParams(vm_params_t params);
269   simgrid::Host* getHost() { return p_host; }
270 private:
271   s_vm_params_t p_params;
272 };
273
274 /**********
275  * Action *
276  **********/
277
278 /** @ingroup SURF_host_interface
279  * @brief SURF host action interface class
280  */
281 class HostAction : public Action {
282 public:
283         static simgrid::surf::signal<void(simgrid::surf::HostAction*, e_surf_action_state_t, e_surf_action_state_t)> onStateChange;
284
285   /**
286    * @brief HostAction constructor
287    *
288    * @param model The HostModel associated to this HostAction
289    * @param cost The cost of this HostAction in [TODO]
290    * @param failed [description]
291    */
292   HostAction(simgrid::surf::Model *model, double cost, bool failed)
293   : Action(model, cost, failed) {}
294
295   /**
296    * @brief HostAction constructor
297    *
298    * @param model The HostModel associated to this HostAction
299    * @param cost The cost of this HostAction in [TODO]
300    * @param failed [description]
301    * @param var The lmm variable associated to this StorageAction if it is part of a LMM component
302    */
303   HostAction(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var)
304   : Action(model, cost, failed, var) {}
305
306   void setState(e_surf_action_state_t state);
307 };
308
309 }
310 }
311
312 #endif /* SURF_Host_INTERFACE_HPP_ */