Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update workstation handling in SimDag. Change the prefix of SimDag functions with...
[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_resource_t surf_timer_resource = 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->resource = (surf_resource_t) surf_timer_resource;
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_resource->common_public->states.ready_action_set);
182   xbt_swag_free(surf_timer_resource->common_public->states.
183                 running_action_set);
184   xbt_swag_free(surf_timer_resource->common_public->states.
185                 failed_action_set);
186   xbt_swag_free(surf_timer_resource->common_public->states.done_action_set);
187   free(surf_timer_resource->common_public);
188   free(surf_timer_resource->common_private);
189   free(surf_timer_resource->extension_public);
190
191   free(surf_timer_resource);
192   surf_timer_resource = NULL;
193 }
194
195 static void surf_timer_resource_init_internal(void)
196 {
197   s_surf_action_t action;
198
199   surf_timer_resource = xbt_new0(s_surf_timer_resource_t, 1);
200
201   surf_timer_resource->common_private =
202       xbt_new0(s_surf_resource_private_t, 1);
203   surf_timer_resource->common_public = xbt_new0(s_surf_resource_public_t, 1);
204
205   surf_timer_resource->extension_public =
206       xbt_new0(s_surf_timer_resource_extension_public_t, 1);
207
208   surf_timer_resource->common_public->states.ready_action_set =
209       xbt_swag_new(xbt_swag_offset(action, state_hookup));
210   surf_timer_resource->common_public->states.running_action_set =
211       xbt_swag_new(xbt_swag_offset(action, state_hookup));
212   surf_timer_resource->common_public->states.failed_action_set =
213       xbt_swag_new(xbt_swag_offset(action, state_hookup));
214   surf_timer_resource->common_public->states.done_action_set =
215       xbt_swag_new(xbt_swag_offset(action, state_hookup));
216
217   surf_timer_resource->common_public->name_service = name_service;
218   surf_timer_resource->common_public->get_resource_name = get_resource_name;
219   surf_timer_resource->common_public->action_get_state =
220       surf_action_get_state;
221   surf_timer_resource->common_public->action_free = action_free;
222   surf_timer_resource->common_public->action_cancel = action_cancel;
223   surf_timer_resource->common_public->action_recycle = action_recycle;
224   surf_timer_resource->common_public->action_change_state =
225       action_change_state;
226   surf_timer_resource->common_public->action_set_data = surf_action_set_data;
227   surf_timer_resource->common_public->name = "TIMER";
228
229   surf_timer_resource->common_private->resource_used = resource_used;
230   surf_timer_resource->common_private->share_resources = share_resources;
231   surf_timer_resource->common_private->update_actions_state =
232       update_actions_state;
233   surf_timer_resource->common_private->update_resource_state =
234       update_resource_state;
235   surf_timer_resource->common_private->finalize = finalize;
236
237   surf_timer_resource->common_public->suspend = action_suspend;
238   surf_timer_resource->common_public->resume = action_resume;
239   surf_timer_resource->common_public->is_suspended = action_is_suspended;
240
241   surf_timer_resource->extension_public->set = set;
242   surf_timer_resource->extension_public->get = get;
243
244   {
245     s_command_t var;
246     command_pending = xbt_swag_new(xbt_swag_offset(var, command_set_hookup));
247     command_to_run  = xbt_swag_new(xbt_swag_offset(var, command_set_hookup));
248   }
249
250   empty_trace = tmgr_empty_trace_new();
251   timer_heap = xbt_heap_new(8, NULL);
252
253   xbt_assert0(maxmin_system, "surf_init has to be called first!");
254 }
255
256 void surf_timer_resource_init(const char *filename)
257 {
258   if (surf_timer_resource)
259     return;
260   surf_timer_resource_init_internal();
261   xbt_dynar_push(resource_list, &surf_timer_resource);
262 }