Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
297721c2298905360ba1709b5edafe8232ef83a0
[simgrid.git] / src / surf / surf.cpp
1 #include "surf.hpp"
2 #include "simix/smx_host_private.h"
3
4 XBT_LOG_NEW_CATEGORY(surfpp, "All SURF categories");
5 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surfpp_kernel, surfpp,
6                                 "Logging specific to SURF (kernel)");
7
8 /*********
9  * Utils *
10  *********/
11
12 //TODO:RENAME NOW
13 double NOWW = 0;
14
15 XBT_INLINE double surf_get_clock(void)
16 {
17   return NOWW;
18 }
19
20 /* This function is a pimple that we ought to fix. But it won't be easy.
21  *
22  * The surf_solve() function does properly return the set of actions that changed.
23  * Instead, each model change a global data, and then the caller of surf_solve must
24  * pick into these sets of action_failed and action_done.
25  *
26  * This was not clean but ok as long as we didn't had to restart the processes when the resource comes back up.
27  * We worked by putting sentinel actions on every resources we are interested in,
28  * so that surf informs us if/when the corresponding resource fails.
29  *
30  * But this does not work to get Simix informed of when a resource comes back up, and this is where this pimple comes.
31  * We have a set of resources that are currently down and for which simix needs to know when it comes back up.
32  * And the current function is called *at every simulation step* to sweep over that set, searching for a resource
33  * that was turned back up in the meanwhile. This is UGLY and slow.
34  *
35  * The proper solution would be to not rely on globals for the action_failed and action_done swags.
36  * They must be passed as parameter by the caller (the handling of these actions in simix may let you
37  * think that these two sets can be merged, but their handling in SimDag induce the contrary unless this
38  * simdag code can check by itself whether the action is done of failed -- seems very doable, but yet more
39  * cleanup to do).
40  *
41  * Once surf_solve() is passed the set of actions that changed, you want to add a new set of resources back up
42  * as parameter to this function. You also want to add a boolean field "restart_watched" to each resource, and
43  * make sure that whenever a resource with this field enabled comes back up, it's added to that set so that Simix
44  * sees it and react accordingly. This would kill that need for surf to call simix.
45  *
46  */
47
48 static void remove_watched_host(void *key)
49 {
50   xbt_dict_remove(watched_hosts_lib, *(char**)key);
51 }
52
53 /*TODO: keepit void surf_watched_hosts(void)
54 {
55   char *key;
56   void *_host;
57   smx_host_t host;
58   xbt_dict_cursor_t cursor;
59   xbt_dynar_t hosts = xbt_dynar_new(sizeof(char*), NULL);
60
61   XBT_DEBUG("Check for host SURF_RESOURCE_ON on watched_hosts_lib");
62   xbt_dict_foreach(watched_hosts_lib,cursor,key,_host)
63   {
64     host = (smx_host_t) host;
65     if(SIMIX_host_get_state(host) == SURF_RESOURCE_ON){
66       XBT_INFO("Restart processes on host: %s",SIMIX_host_get_name(host));
67       SIMIX_host_autorestart(host);
68       xbt_dynar_push_as(hosts, char*, key);
69     }
70     else
71       XBT_DEBUG("See SURF_RESOURCE_OFF on host: %s",key);
72   }
73   xbt_dynar_map(hosts, remove_watched_host);
74   xbt_dynar_free(&hosts);
75 }*/
76
77 /*********
78  * Model *
79  *********/
80
81 void Model::addTurnedOnCallback(ResourceCallback rc)
82 {
83   m_resOnCB = rc;
84 }
85
86 void Model::notifyResourceTurnedOn(ResourcePtr r)
87 {
88   m_resOnCB(r);
89 }
90
91 void Model::addTurnedOffCallback(ResourceCallback rc)
92 {
93   m_resOffCB = rc;
94 }
95
96 void Model::notifyResourceTurnedOff(ResourcePtr r)
97 {
98   m_resOffCB(r);
99 }
100
101 void Model::addActionCancelCallback(ActionCallback ac)
102 {
103   m_actCancelCB = ac;
104 }
105
106 void Model::notifyActionCancel(ActionPtr a)
107 {
108   m_actCancelCB(a);
109 }
110
111 void Model::addActionResumeCallback(ActionCallback ac)
112 {
113   m_actResumeCB = ac;
114 }
115
116 void Model::notifyActionResume(ActionPtr a)
117 {
118   m_actResumeCB(a);
119 }
120
121 void Model::addActionSuspendCallback(ActionCallback ac)
122 {
123   m_actSuspendCB = ac;
124 }
125
126 void Model::notifyActionSuspend(ActionPtr a)
127 {
128   m_actSuspendCB(a);
129 }
130
131
132 /************
133  * Resource *
134  ************/
135
136 string Resource::getName() {
137   return m_name;
138 }
139
140 bool Resource::isOn()
141 {
142   return m_running;
143 }
144
145 void Resource::turnOn()
146 {
147   if (!m_running) {
148     m_running = true;
149     p_model->notifyResourceTurnedOn(this);
150   }
151 }
152
153 void Resource::turnOff()
154 {
155   if (m_running) {
156     m_running = false;
157     p_model->notifyResourceTurnedOff(this);
158   }
159 }
160
161 /**********
162  * Action *
163  **********/
164 /*TODO/const char *surf_action_state_names[6] = {
165   "SURF_ACTION_READY",
166   "SURF_ACTION_RUNNING",
167   "SURF_ACTION_FAILED",
168   "SURF_ACTION_DONE",
169   "SURF_ACTION_TO_FREE",
170   "SURF_ACTION_NOT_IN_THE_SYSTEM"
171 };*/
172
173 e_surf_action_state_t Action::getState()
174 {
175   if (p_stateSet ==  p_model->p_readyActionSet)
176     return SURF_ACTION_READY;
177   if (p_stateSet ==  p_model->p_runningActionSet)
178     return SURF_ACTION_RUNNING;
179   if (p_stateSet ==  p_model->p_failedActionSet)
180     return SURF_ACTION_FAILED;
181   if (p_stateSet ==  p_model->p_doneActionSet)
182     return SURF_ACTION_DONE;
183   return SURF_ACTION_NOT_IN_THE_SYSTEM;
184 }
185
186 void Action::setState(e_surf_action_state_t state)
187 {
188   //surf_action_state_t action_state = &(action->model_type->states);
189   XBT_IN("(%p,%s)", this, surf_action_state_names[state]);
190   xbt_swag_remove(this, p_stateSet);
191
192   if (state == SURF_ACTION_READY)
193     p_stateSet = p_model->p_readyActionSet;
194   else if (state == SURF_ACTION_RUNNING)
195     p_stateSet = p_model->p_runningActionSet;
196   else if (state == SURF_ACTION_FAILED)
197     p_stateSet = p_model->p_failedActionSet;
198   else if (state == SURF_ACTION_DONE)
199     p_stateSet = p_model->p_doneActionSet;
200   else
201     p_stateSet = NULL;
202
203   if (p_stateSet)
204     xbt_swag_insert(this, p_stateSet);
205   XBT_OUT();
206 }
207
208 double  Action::getStartTime()
209 {
210   return m_start;
211 }
212
213 double  Action::getFinishTime()
214 {
215   return m_finish;
216 }
217
218 void  Action::setData(void* data)
219 {
220   p_data = data;
221 }
222
223 /*void Action::cancel()
224 {
225   p_model->notifyActionCancel(this);
226 }
227
228 void Action::suspend()
229 {
230   p_model->notifyActionSuspend(this);
231 }
232
233 void Action::resume()
234 {
235   p_model->notifyActionResume(this);
236 }
237
238 bool Action::isSuspended()
239 {
240   return false;
241 }*/
242