Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d6c462e5f722030093b5a87c49cc125c47f8d9f8
[simgrid.git] / src / surf / surf.hpp
1 //using namespace generic;
2
3 #ifndef SURF_MODEL_H_
4 #define SURF_MODEL_H_
5
6 #include <xbt.h>
7 #include <string>
8 #include <vector>
9 #include <memory>
10 #include <boost/smart_ptr.hpp>
11 #include <boost/function.hpp>
12 #include <boost/functional/factory.hpp>
13 #include <boost/bind.hpp>
14 #include "surf/trace_mgr.h"
15
16 extern tmgr_history_t history;
17
18 using namespace std;
19
20 /***********
21  * Classes *
22  ***********/
23 class Model;
24 typedef Model* ModelPtr;
25
26 class Resource;
27 typedef Resource* ResourcePtr;
28 typedef boost::function<void (ResourcePtr r)> ResourceCallback;
29                         
30 class Action;
31 typedef Action* ActionPtr;
32 typedef boost::function<void (ActionPtr a)> ActionCallback;
33
34 /*********
35  * Model *
36  *********/
37 class Model {
38 public:
39   Model(string name) {
40     m_name = name;
41     m_resOnCB = m_resOffCB= 0;
42     m_actSuspendCB = m_actCancelCB = m_actResumeCB = 0;
43   }
44   virtual ~Model() {}
45   ResourcePtr createResource(string name);
46   ActionPtr createAction(double _cost, bool _failed);
47
48   string getName() {return m_name;};
49
50   void addTurnedOnCallback(ResourceCallback rc);
51   void notifyResourceTurnedOn(ResourcePtr r);
52
53   void addTurnedOffCallback(ResourceCallback rc);  
54   void notifyResourceTurnedOff(ResourcePtr r);
55
56   void addActionCancelCallback(ActionCallback ac);
57   void notifyActionCancel(ActionPtr a);
58   void addActionResumeCallback(ActionCallback ac);
59   void notifyActionResume(ActionPtr a);
60   void addActionSuspendCallback(ActionCallback ac);  
61   void notifyActionSuspend(ActionPtr a);
62
63   xbt_swag_t p_readyActionSet; /**< Actions in state SURF_ACTION_READY */
64   xbt_swag_t runningActionSet; /**< Actions in state SURF_ACTION_RUNNING */
65   xbt_swag_t failedActionSet; /**< Actions in state SURF_ACTION_FAILED */
66   xbt_swag_t doneActionSet; /**< Actions in state SURF_ACTION_DONE */
67
68 protected:
69   std::vector<ActionPtr> m_failedActions, m_runningActions;
70
71 private:
72   string m_name;
73   ResourceCallback m_resOnCB, m_resOffCB;
74   ActionCallback m_actCancelCB, m_actSuspendCB, m_actResumeCB;
75 };
76
77 /************
78  * Resource *
79  ************/
80 class Resource {
81 public:
82   Resource(ModelPtr model, string name, xbt_dict_t properties):
83           m_name(name),m_running(true),p_model(model),m_properties(properties) {};
84   virtual ~Resource() {};
85
86   bool isOn();
87   void turnOn();
88   void turnOff();
89   void setName(string name);
90   string getName();
91   ModelPtr getModel() {return p_model;};
92   void printModel() { std::cout << p_model->getName() << "<<plop"<<std::endl;};
93  
94 protected:
95   ModelPtr p_model;
96   string m_name;
97   xbt_dict_t m_properties;
98
99 private:
100   bool m_running;  
101 };
102
103 /**********
104  * Action *
105  **********/
106
107 /** \ingroup SURF_actions
108  *  \brief Action states
109  *
110  *  Action states.
111  *
112  *  \see surf_action_t, surf_action_state_t
113  */
114 typedef enum {
115   SURF_ACTION_READY = 0,        /**< Ready        */
116   SURF_ACTION_RUNNING,          /**< Running      */
117   SURF_ACTION_FAILED,           /**< Task Failure */
118   SURF_ACTION_DONE,             /**< Completed    */
119   SURF_ACTION_TO_FREE,          /**< Action to free in next cleanup */
120   SURF_ACTION_NOT_IN_THE_SYSTEM
121                                 /**< Not in the system anymore. Why did you ask ? */
122 } e_surf_action_state_t;
123
124 typedef enum {
125   UM_FULL,
126   UM_LAZY,
127   UM_UNDEFINED
128 } e_UM_t;
129
130 class Action {
131 public:
132   Action(ModelPtr model, double cost, bool failed):
133          m_cost(cost),p_model(model),m_failed(failed)
134   {
135     m_priority = m_start = m_finish = m_maxDuration = -1.0;
136     m_start = 10;//surf_get_clock();
137     m_suspended = false;
138   };
139   virtual ~Action() {};
140   
141   virtual e_surf_action_state_t getState()=0; /**< Return the state of an action */
142   virtual void setState(e_surf_action_state_t state)=0; /**< Change an action state*/
143   virtual double getStartTime()=0; /**< Return the start time of an action */
144   virtual double getFinishTime()=0; /**< Return the finish time of an action */
145   virtual int unref()=0;     /**< Specify that we don't use that action anymore. Returns true if the action was destroyed and false if someone still has references on it. */
146   virtual void cancel()=0;     /**< Cancel a running action */
147   virtual void recycle()=0;     /**< Recycle an action */
148   void suspend();     /**< Suspend an action */
149   void resume();     /**< Resume a suspended action */
150   bool isSuspended();     /**< Return whether an action is suspended */
151   void setMaxDuration(double duration);     /**< Set the max duration of an action*/
152   void setPriority(double priority);     /**< Set the priority of an action */
153 #ifdef HAVE_TRACING
154   void setCategory(const char *category); /**< Set the category of an action */
155 #endif
156   double getRemains();     /**< Get the remains of an action */
157 #ifdef HAVE_LATENCY_BOUND_TRACKING
158   int getLatencyLimited();     /**< Return 1 if action is limited by latency, 0 otherwise */
159 #endif
160
161
162 protected:
163   ModelPtr p_model;  
164   int    m_cost;
165   bool   m_failed, m_suspended;
166   double m_priority;
167   double m_maxDuration;
168   double m_start, m_finish;
169
170 private:
171   int resourceUsed(void *resource_id);
172   /* Share the resources to the actions and return in how much time
173      the next action may terminate */
174   double shareResources(double now);
175   /* Update the actions' state */
176   void updateActionsState(double now, double delta);
177   void updateResourceState(void *id, tmgr_trace_event_t event_type,
178                                  double value, double time);
179   void finalize(void);
180
181   lmm_system_t p_maxminSystem;
182   e_UM_t p_updateMechanism;
183   xbt_swag_t p_modifiedSet;
184   xbt_heap_t p_actionHeap;
185   int m_selectiveUpdate;
186 };
187
188 #endif /* SURF_MODEL_H_ */