Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
factorize some more code so that supernovae works again
[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_type = 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_type->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_type->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 }