Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Introduce sg_storage_size_t type
[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_type = 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_type->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_type->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_type;
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_type;
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_type;
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_type;
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_type;
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_type;
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 #ifdef HAVE_TRACING
285 void surf_action_set_category(surf_action_t action,
286                                     const char *category)
287 {
288   XBT_IN("(%p,%s)", action, category);
289   action->category = xbt_strdup(category);
290   XBT_OUT();
291 }
292 #endif
293
294 void generic_update_action_remaining_lazy( surf_action_lmm_t action, double now)
295 {
296   double delta = 0.0;
297   surf_model_t model = action->generic_action.model_type;
298
299   if(model == surf_network_model)
300   {
301     if (action->suspended != 0)
302       return;
303   }
304   else
305   {
306     xbt_assert(action->generic_action.state_set == model->states.running_action_set,
307         "You're updating an action that is not running.");
308
309       /* bogus priority, skip it */
310     xbt_assert(action->generic_action.priority > 0,
311         "You're updating an action that seems suspended.");
312   }
313
314   delta = now - action->last_update;
315
316   if (action->generic_action.remains > 0) {
317     XBT_DEBUG("Updating action(%p): remains was %lf, last_update was: %lf", action, action->generic_action.remains, action->last_update);
318     double_update(&(action->generic_action.remains),
319         action->last_value * delta);
320
321 #ifdef HAVE_TRACING
322     if (model == surf_cpu_model && TRACE_is_enabled()) {
323       surf_resource_t cpu =
324           lmm_constraint_id(lmm_get_cnst_from_var
325               (model->model_private->maxmin_system,
326                   action->variable, 0));
327       TRACE_surf_host_set_utilization(cpu->name,
328           action->generic_action.category,
329           action->last_value,
330           action->last_update,
331           now - action->last_update);
332     }
333 #endif
334     XBT_DEBUG("Updating action(%p): remains is now %lf", action,
335         action->generic_action.remains);
336   }
337
338   if(model == surf_network_model)
339   {
340     if (((surf_action_t)action)->max_duration != NO_MAX_DURATION)
341       double_update(&(((surf_action_t)action)->max_duration), delta);
342
343     if ((((surf_action_t)action)->remains <= 0) &&
344         (lmm_get_variable_weight(action->variable) > 0)) {
345       ((surf_action_t)action)->finish = surf_get_clock();
346       model->action_state_set((surf_action_t) action,
347           SURF_ACTION_DONE);
348
349       surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
350     } else if (((((surf_action_t)action)->max_duration != NO_MAX_DURATION)
351         && (((surf_action_t)action)->max_duration <= 0))) {
352       ((surf_action_t)action)->finish = surf_get_clock();
353       model->action_state_set((surf_action_t) action,
354           SURF_ACTION_DONE);
355       surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
356     }
357   }
358
359   action->last_update = now;
360   action->last_value = lmm_variable_getvalue(action->variable);
361 }
362
363 double surf_action_get_remains(surf_action_t action)
364 {
365   XBT_IN("(%p)", action);
366   surf_model_t model = action->model_type;
367   /* update remains before return it */
368   if (model->model_private->update_mechanism == UM_LAZY)      /* update remains before return it */
369     generic_update_action_remaining_lazy((surf_action_lmm_t)action, surf_get_clock());
370   XBT_OUT();
371   return action->remains;
372 }
373
374 /**
375  * Update the CPU total energy for a finished action
376  *
377  */
378 void update_resource_energy(surf_model_t model, surf_action_lmm_t action)
379 {
380     if(model == surf_cpu_model){
381         cpu_Cas01_t cpu_model = (cpu_Cas01_t)lmm_constraint_id(lmm_get_cnst_from_var
382                                                                                   (model->model_private->maxmin_system,
383                                                                                                   action->variable, 0));
384
385         if( cpu_model->energy->last_updated < surf_get_clock()) {
386                 double load = lmm_constraint_get_usage(cpu_model->constraint) / cpu_model->power_peak;
387                 cpu_update_energy(cpu_model, load);
388         }
389     }
390 }
391
392
393
394
395 void generic_update_actions_state_lazy(double now, double delta, surf_model_t model)
396 {
397   surf_action_lmm_t action;
398   while ((xbt_heap_size(model->model_private->action_heap) > 0)
399          && (double_equals(xbt_heap_maxkey(model->model_private->action_heap), now))) {
400     action = xbt_heap_pop(model->model_private->action_heap);
401     XBT_DEBUG("Something happened to action %p", action);
402 #ifdef HAVE_TRACING
403     if (TRACE_is_enabled()) {
404       if(model == surf_cpu_model){
405       surf_resource_t cpu =
406           lmm_constraint_id(lmm_get_cnst_from_var
407                             (model->model_private->maxmin_system,
408                              action->variable, 0));
409       TRACE_surf_host_set_utilization(cpu->name,
410                                       ((surf_action_t)action)->category,
411                                       lmm_variable_getvalue(action->variable),
412                                       action->last_update,
413                                       now - action->last_update);
414       }
415       else{
416         int n = lmm_get_number_of_cnst_from_var(model->model_private->maxmin_system, action->variable);
417         unsigned int i;
418         for (i = 0; i < n; i++){
419           lmm_constraint_t constraint = lmm_get_cnst_from_var(model->model_private->maxmin_system,
420                                                               action->variable,
421                                                               i);
422           link_CM02_t link = lmm_constraint_id(constraint);
423           TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
424                                           ((surf_action_t)action)->category,
425                                           (lmm_variable_getvalue(action->variable)*
426                                               lmm_get_cnst_weight_from_var(model->model_private->maxmin_system,
427                                                   action->variable,
428                                                   i)),
429                                           action->last_update,
430                                           now - action->last_update);
431         }
432       }
433     }
434 #endif
435
436     if(model == surf_cpu_model){
437       action->generic_action.finish = surf_get_clock();
438
439       update_resource_energy(model, action);
440
441       /* set the remains to 0 due to precision problems when updating the remaining amount */
442       action->generic_action.remains = 0;
443       surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
444       surf_action_lmm_heap_remove(model->model_private->action_heap,action); //FIXME: strange call since action was already popped
445     }
446     else{
447       // if I am wearing a latency hat
448       if (action->hat == LATENCY) {
449         XBT_DEBUG("Latency paid for action %p. Activating", action);
450         lmm_update_variable_weight(model->model_private->maxmin_system, action->variable,
451             ((surf_action_network_CM02_t)(action))->weight);
452         surf_action_lmm_heap_remove(model->model_private->action_heap,action);
453         action->last_update = surf_get_clock();
454
455         // if I am wearing a max_duration or normal hat
456       } else if (action->hat == MAX_DURATION ||
457           action->hat == NORMAL) {
458         // no need to communicate anymore
459         // assume that flows that reached max_duration have remaining of 0
460         action->generic_action.finish = surf_get_clock();
461         XBT_DEBUG("Action %p finished", action);
462         action->generic_action.remains = 0;
463         ((surf_action_t)action)->finish = surf_get_clock();
464         model->action_state_set((surf_action_t) action,
465                                              SURF_ACTION_DONE);
466         surf_action_lmm_heap_remove(model->model_private->action_heap,action);
467
468         if (model->gap_remove && model == surf_network_model)
469           model->gap_remove(action);
470       }
471     }
472   }
473 #ifdef HAVE_TRACING
474   if (TRACE_is_enabled() && model == surf_cpu_model) {
475     //defining the last timestamp that we can safely dump to trace file
476     //without losing the event ascending order (considering all CPU's)
477     double smaller = -1;
478     xbt_swag_t running_actions = model->states.running_action_set;
479     xbt_swag_foreach(action, running_actions) {
480         if (smaller < 0) {
481           smaller = action->last_update;
482           continue;
483         }
484         if (action->last_update < smaller) {
485           smaller = action->last_update;
486         }
487     }
488     if (smaller > 0) {
489       TRACE_last_timestamp_to_dump = smaller;
490     }
491   }
492 #endif
493   return;
494 }
495
496 void generic_update_actions_state_full(double now, double delta, surf_model_t model)
497 {
498   surf_action_lmm_t action = NULL;
499   surf_action_lmm_t next_action = NULL;
500   xbt_swag_t running_actions = model->states.running_action_set;
501
502   xbt_swag_foreach_safe(action, next_action, running_actions) {
503
504     if(model == surf_network_model)
505     {
506       double deltap = 0.0;
507       deltap = delta;
508       if (((surf_action_network_CM02_t)action)->latency > 0) {
509         if (((surf_action_network_CM02_t)action)->latency > deltap) {
510           double_update(&(((surf_action_network_CM02_t)action)->latency), deltap);
511           deltap = 0.0;
512         } else {
513           double_update(&(deltap), ((surf_action_network_CM02_t)action)->latency);
514           ((surf_action_network_CM02_t)action)->latency = 0.0;
515         }
516         if ((((surf_action_network_CM02_t)action)->latency == 0.0) && !(action->suspended))
517           lmm_update_variable_weight(model->model_private->maxmin_system, action->variable,
518               ((surf_action_network_CM02_t)action)->weight);
519       }
520   #ifdef HAVE_TRACING
521       if (TRACE_is_enabled()) {
522         int n = lmm_get_number_of_cnst_from_var(model->model_private->maxmin_system, action->variable);
523         unsigned int i;
524         for (i = 0; i < n; i++){
525           lmm_constraint_t constraint = lmm_get_cnst_from_var(model->model_private->maxmin_system,
526                                                               action->variable,
527                                                               i);
528           link_CM02_t link = lmm_constraint_id(constraint);
529           TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
530                                           ((surf_action_t)action)->category,
531                                           (lmm_variable_getvalue(action->variable)*
532                                           lmm_get_cnst_weight_from_var(model->model_private->maxmin_system,
533                                               action->variable,
534                                               i)),
535                                           now - delta,
536                                           delta);
537         }
538       }
539   #endif
540       if (!lmm_get_number_of_cnst_from_var
541           (model->model_private->maxmin_system, action->variable)) {
542         /* There is actually no link used, hence an infinite bandwidth.
543          * This happens often when using models like vivaldi.
544          * In such case, just make sure that the action completes immediately.
545          */
546         double_update(&(action->generic_action.remains),
547                       action->generic_action.remains);
548       }
549     }
550     else
551     {
552 #ifdef HAVE_TRACING
553       if (TRACE_is_enabled()) {
554         surf_resource_t x =
555             lmm_constraint_id(lmm_get_cnst_from_var
556                               (model->model_private->maxmin_system,
557                                action->variable, 0));
558
559         TRACE_surf_host_set_utilization(x->name,
560                                         ((surf_action_t)action)->category,
561                                         lmm_variable_getvalue(action->variable),
562                                         now - delta,
563                                         delta);
564         TRACE_last_timestamp_to_dump = now - delta;
565       }
566 #endif
567     }
568
569     double_update(&(action->generic_action.remains),
570                   lmm_variable_getvalue(action->variable) * delta);
571
572
573     if (action->generic_action.max_duration != NO_MAX_DURATION)
574       double_update(&(action->generic_action.max_duration), delta);
575
576
577     if ((action->generic_action.remains <= 0) &&
578         (lmm_get_variable_weight(action->variable) > 0)) {
579       action->generic_action.finish = surf_get_clock();
580       surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
581
582       if (model->gap_remove && model == surf_network_model)
583         model->gap_remove(action);
584     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
585                (action->generic_action.max_duration <= 0)) {
586       action->generic_action.finish = surf_get_clock();
587       surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
588
589       if (model->gap_remove && model == surf_network_model)
590         model->gap_remove(action);
591     }
592
593     update_resource_energy(model, action);
594   }
595
596   return;
597 }