Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
A few weeks ago, there was a brutal renaming of resource to model. It
[simgrid.git] / src / surf / surf_timer.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2005 Arnaud Legrand. All rights reserved.                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "xbt/ex.h"
9 #include "surf_timer_private.h"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_timer, surf,
12                                 "Logging specific to SURF (timer)");
13
14 surf_timer_model_t surf_timer_model = NULL;
15 static tmgr_trace_t empty_trace = NULL;
16 static xbt_swag_t command_pending = NULL;
17 static xbt_swag_t command_to_run = NULL;
18 static xbt_heap_t timer_heap = NULL;
19
20 static void timer_free(void *timer)
21 {
22   free(timer);
23 }
24
25 static command_t command_new(void *fun, void *args)
26 {
27   command_t command = xbt_new0(s_command_t, 1);
28
29   command->model = (surf_model_t) surf_timer_model;
30   command->function = fun;
31   command->args = args;
32   xbt_swag_insert(command, command_pending);
33   return command;
34 }
35
36 static void command_free(command_t command)
37 {
38   free(command);
39
40   if (xbt_swag_belongs(command, command_to_run)) {
41     xbt_swag_remove(command, command_to_run);
42   } else if (xbt_swag_belongs(command, command_pending)) {
43     xbt_swag_remove(command, command_pending);
44   }
45   return;
46 }
47
48 static void parse_timer(void)
49 {
50 }
51
52 static void parse_file(const char *file)
53 {
54 }
55
56 static void *name_service(const char *name)
57 {
58   DIE_IMPOSSIBLE;
59   return NULL;
60 }
61
62 static const char *get_resource_name(void *resource_id)
63 {
64   DIE_IMPOSSIBLE;
65   return "";
66 }
67
68 static int resource_used(void *resource_id)
69 {
70   return 1;
71 }
72
73 static int action_free(surf_action_t action)
74 {
75   DIE_IMPOSSIBLE;
76   return 1;
77 }
78
79 static void action_cancel(surf_action_t action)
80 {
81   DIE_IMPOSSIBLE;
82   return;
83 }
84
85 static void action_recycle(surf_action_t action)
86 {
87   DIE_IMPOSSIBLE;
88   return;
89 }
90
91 static void action_change_state(surf_action_t action,
92                                 e_surf_action_state_t state)
93 {
94   DIE_IMPOSSIBLE;
95   return;
96 }
97
98 static double share_resources(double now)
99 {
100   if (xbt_heap_size(timer_heap))
101     return (xbt_heap_maxkey(timer_heap));
102   else
103     return -1.0;
104 }
105
106 static void update_actions_state(double now, double delta)
107 {
108   if (xbt_heap_size(timer_heap)) {
109     if (xbt_heap_maxkey(timer_heap) <= now + delta) {
110       xbt_heap_pop(timer_heap);
111     }
112   }
113   return;
114 }
115
116 static void update_resource_state(void *id,
117                                   tmgr_trace_event_t event_type,
118                                   double value)
119 {
120   command_t command = id;
121
122   /* Move this command to the list of commands to execute */
123   xbt_swag_remove(command, command_pending);
124   xbt_swag_insert(command, command_to_run);
125
126   return;
127 }
128
129 static void set(double date, void *function, void *arg)
130 {
131   command_t command = NULL;
132
133   command = command_new(function, arg);
134
135   tmgr_history_add_trace(history, empty_trace, date, 0, command);
136   xbt_heap_push(timer_heap, NULL, date);
137 }
138
139
140 static int get(void **function, void **arg)
141 {
142   command_t command = NULL;
143
144   command = xbt_swag_extract(command_to_run);
145   if (command) {
146     *function = command->function;
147     *arg = command->args;
148     return 1;
149   } else {
150     return 0;
151   }
152 }
153
154 static void action_suspend(surf_action_t action)
155 {
156   DIE_IMPOSSIBLE;
157 }
158
159 static void action_resume(surf_action_t action)
160 {
161   DIE_IMPOSSIBLE;
162 }
163
164 static int action_is_suspended(surf_action_t action)
165 {
166   DIE_IMPOSSIBLE;
167   return 0;
168 }
169
170 static void finalize(void)
171 {
172   xbt_heap_free(timer_heap);
173   timer_heap = NULL;
174
175   tmgr_trace_free(empty_trace);
176   empty_trace = NULL;
177
178   xbt_swag_free(command_pending);
179   xbt_swag_free(command_to_run);
180
181   xbt_swag_free(surf_timer_model->common_public->states.
182                 ready_action_set);
183   xbt_swag_free(surf_timer_model->common_public->states.
184                 running_action_set);
185   xbt_swag_free(surf_timer_model->common_public->states.
186                 failed_action_set);
187   xbt_swag_free(surf_timer_model->common_public->states.
188                 done_action_set);
189   free(surf_timer_model->common_public);
190   free(surf_timer_model->common_private);
191   free(surf_timer_model->extension_public);
192
193   free(surf_timer_model);
194   surf_timer_model = NULL;
195 }
196
197 static void surf_timer_model_init_internal(void)
198 {
199   s_surf_action_t action;
200
201   surf_timer_model = xbt_new0(s_surf_timer_model_t, 1);
202
203   surf_timer_model->common_private =
204       xbt_new0(s_surf_model_private_t, 1);
205   surf_timer_model->common_public =
206       xbt_new0(s_surf_model_public_t, 1);
207
208   surf_timer_model->extension_public =
209       xbt_new0(s_surf_timer_model_extension_public_t, 1);
210
211   surf_timer_model->common_public->states.ready_action_set =
212       xbt_swag_new(xbt_swag_offset(action, state_hookup));
213   surf_timer_model->common_public->states.running_action_set =
214       xbt_swag_new(xbt_swag_offset(action, state_hookup));
215   surf_timer_model->common_public->states.failed_action_set =
216       xbt_swag_new(xbt_swag_offset(action, state_hookup));
217   surf_timer_model->common_public->states.done_action_set =
218       xbt_swag_new(xbt_swag_offset(action, state_hookup));
219
220   surf_timer_model->common_public->name_service = name_service;
221   surf_timer_model->common_public->get_resource_name =
222       get_resource_name;
223   surf_timer_model->common_public->action_get_state =
224       surf_action_get_state;
225   surf_timer_model->common_public->action_free = action_free;
226   surf_timer_model->common_public->action_cancel = action_cancel;
227   surf_timer_model->common_public->action_recycle = action_recycle;
228   surf_timer_model->common_public->action_change_state =
229       action_change_state;
230   surf_timer_model->common_public->action_set_data =
231       surf_action_set_data;
232   surf_timer_model->common_public->name = "TIMER";
233
234   surf_timer_model->common_private->resource_used = resource_used;
235   surf_timer_model->common_private->share_resources = share_resources;
236   surf_timer_model->common_private->update_actions_state =
237       update_actions_state;
238   surf_timer_model->common_private->update_resource_state =
239       update_resource_state;
240   surf_timer_model->common_private->finalize = finalize;
241
242   surf_timer_model->common_public->suspend = action_suspend;
243   surf_timer_model->common_public->resume = action_resume;
244   surf_timer_model->common_public->is_suspended = action_is_suspended;
245
246   surf_timer_model->extension_public->set = set;
247   surf_timer_model->extension_public->get = get;
248
249   {
250     s_command_t var;
251     command_pending =
252         xbt_swag_new(xbt_swag_offset(var, command_set_hookup));
253     command_to_run =
254         xbt_swag_new(xbt_swag_offset(var, command_set_hookup));
255   }
256
257   empty_trace = tmgr_empty_trace_new();
258   timer_heap = xbt_heap_new(8, NULL);
259
260   xbt_assert0(maxmin_system, "surf_init has to be called first!");
261 }
262
263 void surf_timer_model_init(const char *filename)
264 {
265   if (surf_timer_model)
266     return;
267   surf_timer_model_init_internal();
268   xbt_dynar_push(model_list, &surf_timer_model);
269 }