Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add comments and disable verbose outputs
[simgrid.git] / src / surf / surf_action.c
1 /* Copyright (c) 2009, 2010. 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_private.h"
8 #include "network_private.h"
9 #include "xbt/mallocator.h"
10
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_kernel);
12
13 /*
14  * Generic action
15  */
16
17 const char *surf_action_state_names[6] = {
18   "SURF_ACTION_READY",
19   "SURF_ACTION_RUNNING",
20   "SURF_ACTION_FAILED",
21   "SURF_ACTION_DONE",
22   "SURF_ACTION_TO_FREE",
23   "SURF_ACTION_NOT_IN_THE_SYSTEM"
24 };
25
26 /* Surf actions mallocator */
27 static xbt_mallocator_t action_mallocator = NULL;
28 static int action_mallocator_allocated_size = 0;
29 static void* surf_action_mallocator_new_f(void);
30 #define surf_action_mallocator_free_f xbt_free_f
31 static void surf_action_mallocator_reset_f(void* action);
32
33 /**
34  * \brief Initializes the action module of Surf.
35  */
36 void surf_action_init(void) {
37
38   /* the action mallocator will always provide actions of the following size,
39    * so this size should be set to the maximum size of the surf action structures
40    */
41   action_mallocator_allocated_size = sizeof(s_surf_action_network_CM02_t);
42   action_mallocator = xbt_mallocator_new(65536, surf_action_mallocator_new_f,
43       surf_action_mallocator_free_f, surf_action_mallocator_reset_f);
44 }
45
46 /**
47  * \brief Uninitializes the action module of Surf.
48  */
49 void surf_action_exit(void) {
50
51   xbt_mallocator_free(action_mallocator);
52 }
53
54 static void* surf_action_mallocator_new_f(void) {
55   return xbt_malloc(action_mallocator_allocated_size);
56 }
57
58 static void surf_action_mallocator_reset_f(void* action) {
59   memset(action, 0, action_mallocator_allocated_size);
60 }
61
62 void *surf_action_new(size_t size, double cost, surf_model_t model,
63                       int failed)
64 {
65   xbt_assert(size <= action_mallocator_allocated_size,
66       "Cannot create a surf action of size %zu: the mallocator only provides actions of size %d",
67       size, action_mallocator_allocated_size);
68
69   surf_action_t action = xbt_mallocator_get(action_mallocator);
70   action->refcount = 1;
71   action->cost = cost;
72   action->remains = cost;
73   action->priority = 1.0;
74   action->max_duration = NO_MAX_DURATION;
75   action->start = surf_get_clock();
76   action->finish = -1.0;
77   action->model_obj = model;
78 #ifdef HAVE_TRACING
79   action->category = NULL;
80 #endif
81
82   if (failed)
83     action->state_set = model->states.failed_action_set;
84   else
85     action->state_set = model->states.running_action_set;
86
87   xbt_swag_insert(action, action->state_set);
88
89   return action;
90 }
91
92 e_surf_action_state_t surf_action_state_get(surf_action_t action)
93 {
94   surf_action_state_t action_state = &(action->model_obj->states);
95
96   if (action->state_set == action_state->ready_action_set)
97     return SURF_ACTION_READY;
98   if (action->state_set == action_state->running_action_set)
99     return SURF_ACTION_RUNNING;
100   if (action->state_set == action_state->failed_action_set)
101     return SURF_ACTION_FAILED;
102   if (action->state_set == action_state->done_action_set)
103     return SURF_ACTION_DONE;
104   return SURF_ACTION_NOT_IN_THE_SYSTEM;
105 }
106
107 double surf_action_get_start_time(surf_action_t action)
108 {
109   return action->start;
110 }
111
112 double surf_action_get_finish_time(surf_action_t action)
113 {
114   /* keep the function behavior, some models (cpu_ti) change the finish time before the action end */
115   return action->remains == 0 ? action->finish : -1;
116 }
117
118 XBT_INLINE void surf_action_free(surf_action_t * action)
119 {
120   xbt_mallocator_release(action_mallocator, *action);
121   *action = NULL;
122 }
123
124 void surf_action_state_set(surf_action_t action,
125                            e_surf_action_state_t state)
126 {
127   surf_action_state_t action_state = &(action->model_obj->states);
128   XBT_IN("(%p,%s)", action, surf_action_state_names[state]);
129   xbt_swag_remove(action, action->state_set);
130
131   if (state == SURF_ACTION_READY)
132     action->state_set = action_state->ready_action_set;
133   else if (state == SURF_ACTION_RUNNING)
134     action->state_set = action_state->running_action_set;
135   else if (state == SURF_ACTION_FAILED)
136     action->state_set = action_state->failed_action_set;
137   else if (state == SURF_ACTION_DONE)
138     action->state_set = action_state->done_action_set;
139   else
140     action->state_set = NULL;
141
142   if (action->state_set)
143     xbt_swag_insert(action, action->state_set);
144   XBT_OUT();
145 }
146
147 void surf_action_data_set(surf_action_t action, void *data)
148 {
149   action->data = data;
150 }
151
152 XBT_INLINE void surf_action_ref(surf_action_t action)
153 {
154   action->refcount++;
155 }
156
157 /*
158  * Maxmin action
159  */
160
161 /* added to manage the communication action's heap */
162 void surf_action_lmm_update_index_heap(void *action, int i) {
163   surf_action_lmm_t a = action;
164   a->index_heap = i;
165 }
166 /* insert action on heap using a given key and a hat (heap_action_type)
167  * a hat can be of three types for communications:
168  *
169  * NORMAL = this is a normal heap entry stating the date to finish transmitting
170  * LATENCY = this is a heap entry to warn us when the latency is payed
171  * MAX_DURATION =this is a heap entry to warn us when the max_duration limit is reached
172  */
173 void surf_action_lmm_heap_insert(xbt_heap_t heap, surf_action_lmm_t action, double key,
174     enum heap_action_type hat)
175 {
176   action->hat = hat;
177   xbt_heap_push(heap, action, key);
178 }
179
180 void surf_action_lmm_heap_remove(xbt_heap_t heap, surf_action_lmm_t action)
181 {
182   action->hat = NOTSET;
183   if (action->index_heap >= 0) {
184     xbt_heap_remove(heap, action->index_heap);
185   }
186 }
187
188 void surf_action_cancel(surf_action_t action)
189 {
190   surf_model_t model = action->model_obj;
191   surf_action_state_set(action, SURF_ACTION_FAILED);
192   if (model->model_private->update_mechanism == UM_LAZY) {
193     xbt_swag_remove(action, model->model_private->modified_set);
194     surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
195   }
196   return;
197 }
198
199 int surf_action_unref(surf_action_t action)
200 {
201   surf_model_t model = action->model_obj;
202   action->refcount--;
203   if (!action->refcount) {
204     xbt_swag_remove(action, action->state_set);
205     if (((surf_action_lmm_t) action)->variable)
206       lmm_variable_free(model->model_private->maxmin_system,
207                         ((surf_action_lmm_t) action)->variable);
208     if (model->model_private->update_mechanism == UM_LAZY) {
209       /* remove from heap */
210       surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
211       xbt_swag_remove(action, model->model_private->modified_set);
212     }
213 #ifdef HAVE_TRACING
214     xbt_free(action->category);
215 #endif
216     surf_action_free(&action);
217     return 1;
218   }
219   return 0;
220 }
221
222 void surf_action_suspend(surf_action_t action)
223 {
224   surf_model_t model = action->model_obj;
225   XBT_IN("(%p)", action);
226   if (((surf_action_lmm_t) action)->suspended != 2) {
227     lmm_update_variable_weight(model->model_private->maxmin_system,
228                                ((surf_action_lmm_t) action)->variable,
229                                0.0);
230     ((surf_action_lmm_t) action)->suspended = 1;
231     if (model->model_private->update_mechanism == UM_LAZY)
232       surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
233   }
234   XBT_OUT();
235 }
236
237 void surf_action_resume(surf_action_t action)
238 {
239   surf_model_t model = action->model_obj;
240   XBT_IN("(%p)", action);
241   if (((surf_action_lmm_t) action)->suspended != 2) {
242     lmm_update_variable_weight(model->model_private->maxmin_system,
243                                ((surf_action_lmm_t) action)->variable,
244                                action->priority);
245     ((surf_action_lmm_t) action)->suspended = 0;
246     if (model->model_private->update_mechanism == UM_LAZY)
247       surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
248   }
249   XBT_OUT();
250 }
251
252 int surf_action_is_suspended(surf_action_t action)
253 {
254   return (((surf_action_lmm_t) action)->suspended == 1);
255 }
256
257 void surf_action_set_max_duration(surf_action_t action, double duration)
258 {
259   surf_model_t model = action->model_obj;
260   XBT_IN("(%p,%g)", action, duration);
261   action->max_duration = duration;
262   if (model->model_private->update_mechanism == UM_LAZY)      // remove action from the heap
263     surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
264   XBT_OUT();
265 }
266
267 void surf_action_set_priority(surf_action_t action, double priority)
268 {
269   surf_model_t model = action->model_obj;
270   XBT_IN("(%p,%g)", action, priority);
271   action->priority = priority;
272   lmm_update_variable_weight(model->model_private->maxmin_system,
273                              ((surf_action_lmm_t) action)->variable,
274                              priority);
275
276   if (model->model_private->update_mechanism == UM_LAZY)
277     surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
278   XBT_OUT();
279 }
280
281 void surf_action_set_bound(surf_action_t action, double bound)
282 {
283   surf_model_t model = action->model_obj;
284   XBT_IN("(%p,%g)", action, bound);
285   action->bound = bound;
286   lmm_update_variable_bound(model->model_private->maxmin_system, ((surf_action_lmm_t) action)->variable, bound);
287
288   if (model->model_private->update_mechanism == UM_LAZY)
289     surf_action_lmm_heap_remove(model->model_private->action_heap, (surf_action_lmm_t) action);
290   XBT_OUT();
291 }
292
293 #ifdef HAVE_TRACING
294 void surf_action_set_category(surf_action_t action,
295                                     const char *category)
296 {
297   XBT_IN("(%p,%s)", action, category);
298   action->category = xbt_strdup(category);
299   XBT_OUT();
300 }
301 #endif
302
303 void generic_update_action_remaining_lazy( surf_action_lmm_t action, double now)
304 {
305   double delta = 0.0;
306   surf_model_t model = action->generic_action.model_obj;
307
308   if(model == surf_network_model)
309   {
310     if (action->suspended != 0)
311       return;
312   }
313   else
314   {
315     xbt_assert(action->generic_action.state_set == model->states.running_action_set,
316         "You're updating an action that is not running.");
317
318       /* bogus priority, skip it */
319     xbt_assert(action->generic_action.priority > 0,
320         "You're updating an action that seems suspended.");
321   }
322
323   delta = now - action->last_update;
324
325   if (action->generic_action.remains > 0) {
326     XBT_DEBUG("Updating action(%p): remains was %lf, last_update was: %lf", action, action->generic_action.remains, action->last_update);
327     double_update(&(action->generic_action.remains),
328         action->last_value * delta);
329
330 #ifdef HAVE_TRACING
331     if (model->type == SURF_MODEL_TYPE_CPU && TRACE_is_enabled()) {
332       surf_resource_t cpu =
333           lmm_constraint_id(lmm_get_cnst_from_var
334               (model->model_private->maxmin_system,
335                   action->variable, 0));
336       TRACE_surf_host_set_utilization(cpu->name,
337           action->generic_action.category,
338           action->last_value,
339           action->last_update,
340           now - action->last_update);
341     }
342 #endif
343     XBT_DEBUG("Updating action(%p): remains is now %lf", action,
344         action->generic_action.remains);
345   }
346
347   if(model == surf_network_model)
348   {
349     if (((surf_action_t)action)->max_duration != NO_MAX_DURATION)
350       double_update(&(((surf_action_t)action)->max_duration), delta);
351
352     if ((((surf_action_t)action)->remains <= 0) &&
353         (lmm_get_variable_weight(action->variable) > 0)) {
354       ((surf_action_t)action)->finish = surf_get_clock();
355       model->action_state_set((surf_action_t) action,
356           SURF_ACTION_DONE);
357
358       surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
359     } else if (((((surf_action_t)action)->max_duration != NO_MAX_DURATION)
360         && (((surf_action_t)action)->max_duration <= 0))) {
361       ((surf_action_t)action)->finish = surf_get_clock();
362       model->action_state_set((surf_action_t) action,
363           SURF_ACTION_DONE);
364       surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
365     }
366   }
367
368   action->last_update = now;
369   action->last_value = lmm_variable_getvalue(action->variable);
370 }
371
372 double surf_action_get_remains(surf_action_t action)
373 {
374   XBT_IN("(%p)", action);
375   surf_model_t model = action->model_obj;
376   /* update remains before return it */
377   if (model->model_private->update_mechanism == UM_LAZY)      /* update remains before return it */
378     generic_update_action_remaining_lazy((surf_action_lmm_t)action, surf_get_clock());
379   XBT_OUT();
380   return action->remains;
381 }
382
383 void generic_update_actions_state_lazy(double now, double delta, surf_model_t model)
384 {
385   surf_action_lmm_t action;
386   while ((xbt_heap_size(model->model_private->action_heap) > 0)
387          && (double_equals(xbt_heap_maxkey(model->model_private->action_heap), now))) {
388     action = xbt_heap_pop(model->model_private->action_heap);
389     XBT_DEBUG("Something happened to action %p", action);
390 #ifdef HAVE_TRACING
391     if (TRACE_is_enabled()) {
392       if(model->type == SURF_MODEL_TYPE_CPU){
393       surf_resource_t cpu =
394           lmm_constraint_id(lmm_get_cnst_from_var
395                             (model->model_private->maxmin_system,
396                              action->variable, 0));
397       TRACE_surf_host_set_utilization(cpu->name,
398                                       ((surf_action_t)action)->category,
399                                       lmm_variable_getvalue(action->variable),
400                                       action->last_update,
401                                       now - action->last_update);
402       }
403       else{
404         int n = lmm_get_number_of_cnst_from_var(model->model_private->maxmin_system, action->variable);
405         unsigned int i;
406         for (i = 0; i < n; i++){
407           lmm_constraint_t constraint = lmm_get_cnst_from_var(model->model_private->maxmin_system,
408                                                               action->variable,
409                                                               i);
410           link_CM02_t link = lmm_constraint_id(constraint);
411           TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
412                                           ((surf_action_t)action)->category,
413                                           (lmm_variable_getvalue(action->variable)*
414                                               lmm_get_cnst_weight_from_var(model->model_private->maxmin_system,
415                                                   action->variable,
416                                                   i)),
417                                           action->last_update,
418                                           now - action->last_update);
419         }
420       }
421     }
422 #endif
423
424     if(model->type == SURF_MODEL_TYPE_CPU){
425       action->generic_action.finish = surf_get_clock();
426       XBT_DEBUG("Action %p finished", action);
427
428       /* set the remains to 0 due to precision problems when updating the remaining amount */
429       action->generic_action.remains = 0;
430       surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
431       surf_action_lmm_heap_remove(model->model_private->action_heap,action); //FIXME: strange call since action was already popped
432     }
433     else{
434       // if I am wearing a latency hat
435       if (action->hat == LATENCY) {
436         XBT_DEBUG("Latency paid for action %p. Activating", action);
437         lmm_update_variable_weight(model->model_private->maxmin_system, action->variable,
438             ((surf_action_network_CM02_t)(action))->weight);
439         surf_action_lmm_heap_remove(model->model_private->action_heap,action);
440         action->last_update = surf_get_clock();
441
442         // if I am wearing a max_duration or normal hat
443       } else if (action->hat == MAX_DURATION ||
444           action->hat == NORMAL) {
445         // no need to communicate anymore
446         // assume that flows that reached max_duration have remaining of 0
447         action->generic_action.finish = surf_get_clock();
448         XBT_DEBUG("Action %p finished", action);
449         action->generic_action.remains = 0;
450         ((surf_action_t)action)->finish = surf_get_clock();
451         model->action_state_set((surf_action_t) action,
452                                              SURF_ACTION_DONE);
453         surf_action_lmm_heap_remove(model->model_private->action_heap,action);
454
455         if (model->gap_remove && model == surf_network_model)
456           model->gap_remove(action);
457       }
458     }
459   }
460 #ifdef HAVE_TRACING
461   if (TRACE_is_enabled() && model->type == SURF_MODEL_TYPE_CPU) {
462     //defining the last timestamp that we can safely dump to trace file
463     //without losing the event ascending order (considering all CPU's)
464     double smaller = -1;
465     xbt_swag_t running_actions = model->states.running_action_set;
466     xbt_swag_foreach(action, running_actions) {
467         if (smaller < 0) {
468           smaller = action->last_update;
469           continue;
470         }
471         if (action->last_update < smaller) {
472           smaller = action->last_update;
473         }
474     }
475     if (smaller > 0) {
476       TRACE_last_timestamp_to_dump = smaller;
477     }
478   }
479 #endif
480   return;
481 }
482
483 void generic_update_actions_state_full(double now, double delta, surf_model_t model)
484 {
485   surf_action_lmm_t action = NULL;
486   surf_action_lmm_t next_action = NULL;
487   xbt_swag_t running_actions = model->states.running_action_set;
488
489   xbt_swag_foreach_safe(action, next_action, running_actions) {
490
491     if(model == surf_network_model)
492     {
493       double deltap = 0.0;
494       deltap = delta;
495       if (((surf_action_network_CM02_t)action)->latency > 0) {
496         if (((surf_action_network_CM02_t)action)->latency > deltap) {
497           double_update(&(((surf_action_network_CM02_t)action)->latency), deltap);
498           deltap = 0.0;
499         } else {
500           double_update(&(deltap), ((surf_action_network_CM02_t)action)->latency);
501           ((surf_action_network_CM02_t)action)->latency = 0.0;
502         }
503         if ((((surf_action_network_CM02_t)action)->latency == 0.0) && !(action->suspended))
504           lmm_update_variable_weight(model->model_private->maxmin_system, action->variable,
505               ((surf_action_network_CM02_t)action)->weight);
506       }
507   #ifdef HAVE_TRACING
508       if (TRACE_is_enabled()) {
509         int n = lmm_get_number_of_cnst_from_var(model->model_private->maxmin_system, action->variable);
510         unsigned int i;
511         for (i = 0; i < n; i++){
512           lmm_constraint_t constraint = lmm_get_cnst_from_var(model->model_private->maxmin_system,
513                                                               action->variable,
514                                                               i);
515           link_CM02_t link = lmm_constraint_id(constraint);
516           TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
517                                           ((surf_action_t)action)->category,
518                                           (lmm_variable_getvalue(action->variable)*
519                                           lmm_get_cnst_weight_from_var(model->model_private->maxmin_system,
520                                               action->variable,
521                                               i)),
522                                           now - delta,
523                                           delta);
524         }
525       }
526   #endif
527       if (!lmm_get_number_of_cnst_from_var
528           (model->model_private->maxmin_system, action->variable)) {
529         /* There is actually no link used, hence an infinite bandwidth.
530          * This happens often when using models like vivaldi.
531          * In such case, just make sure that the action completes immediately.
532          */
533         double_update(&(action->generic_action.remains),
534                       action->generic_action.remains);
535       }
536     }
537     else
538     {
539 #ifdef HAVE_TRACING
540       if (TRACE_is_enabled()) {
541         surf_resource_t x =
542             lmm_constraint_id(lmm_get_cnst_from_var
543                               (model->model_private->maxmin_system,
544                                action->variable, 0));
545
546         TRACE_surf_host_set_utilization(x->name,
547                                         ((surf_action_t)action)->category,
548                                         lmm_variable_getvalue(action->variable),
549                                         now - delta,
550                                         delta);
551         TRACE_last_timestamp_to_dump = now - delta;
552       }
553 #endif
554     }
555
556     double_update(&(action->generic_action.remains),
557                   lmm_variable_getvalue(action->variable) * delta);
558
559
560     if (action->generic_action.max_duration != NO_MAX_DURATION)
561       double_update(&(action->generic_action.max_duration), delta);
562
563
564     if ((action->generic_action.remains <= 0) &&
565         (lmm_get_variable_weight(action->variable) > 0)) {
566       action->generic_action.finish = surf_get_clock();
567       surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
568
569       if (model->gap_remove && model == surf_network_model)
570         model->gap_remove(action);
571     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
572                (action->generic_action.max_duration <= 0)) {
573       action->generic_action.finish = surf_get_clock();
574       surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
575
576       if (model->gap_remove && model == surf_network_model)
577         model->gap_remove(action);
578     }
579   }
580
581   return;
582 }