Logo AND Algorithmique Numérique Distribuée

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