Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
change regex into the tag cluster
[simgrid.git] / src / surf / surf_model_timer.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 "xbt/ex.h"
8 #include "surf_private.h"
9 #include "xbt/dict.h"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_timer, surf,
12                                 "Logging specific to SURF (timer)");
13
14 typedef struct surf_action_timer {
15   s_surf_action_t generic_action;
16 } s_surf_action_timer_t, *surf_action_timer_t;
17
18 typedef struct command {
19   s_surf_resource_t generic_resource;   /* Must remain first, since we add this to a trace */
20   void *function;
21   void *args;
22   s_xbt_swag_hookup_t command_set_hookup;
23   surf_action_timer_t action;
24 } s_command_t, *command_t;
25
26
27 surf_model_t surf_timer_model = NULL;
28 static tmgr_trace_t empty_trace = NULL;
29 static xbt_swag_t command_pending = NULL;
30 static xbt_swag_t command_to_run = NULL;
31 static xbt_heap_t timer_heap = NULL;
32
33 static command_t command_new(void *fun, void *args)
34 {
35   command_t command = xbt_new0(s_command_t, 1);
36
37   command->generic_resource.model = surf_timer_model;
38   command->function = fun;
39   command->args = args;
40   xbt_swag_insert(command, command_pending);
41   command->action =
42     surf_action_new(sizeof(s_surf_action_timer_t), 0, surf_timer_model, 0);
43   return command;
44 }
45
46 static void command_free(command_t command)
47 {
48   if (xbt_swag_belongs(command, command_to_run)) {
49     xbt_swag_remove(command, command_to_run);
50   } else if (xbt_swag_belongs(command, command_pending)) {
51     xbt_swag_remove(command, command_pending);
52   }
53   xbt_swag_remove(command->action,
54                   surf_timer_model->states.running_action_set);
55   xbt_free(command->action);
56   surf_resource_free((surf_resource_t) command);
57   return;
58 }
59
60 static void parse_timer(void)
61 {
62 }
63
64 static void parse_file(const char *file)
65 {
66 }
67
68 static int resource_used(void *resource_id)
69 {
70   return 1;
71 }
72
73 static void timer_action_state_set(surf_action_t action,
74                                    e_surf_action_state_t state)
75 {
76   DIE_IMPOSSIBLE;
77   return;
78 }
79
80 static double share_resources(double now)
81 {
82   if (xbt_heap_size(timer_heap)) {
83     DEBUG1("Share resoure returning %lf", xbt_heap_maxkey(timer_heap));
84     return (xbt_heap_maxkey(timer_heap));
85   } else
86     return -1.0;
87 }
88
89 static void update_actions_state(double now, double delta)
90 {
91   while ((xbt_heap_size(timer_heap)) > 0
92          && (xbt_heap_maxkey(timer_heap) <= now + delta))
93     xbt_heap_pop(timer_heap);
94   return;
95 }
96
97 static void update_resource_state(void *id,
98                                   tmgr_trace_event_t event_type,
99                                   double value, double date)
100 {
101   command_t command = id;
102
103   /* Move this command to the list of commands to execute */
104   xbt_swag_remove(command, command_pending);
105   xbt_swag_insert(command, command_to_run);
106   tmgr_trace_event_free(event_type);
107
108   DEBUG1("Insert command on date %lf", date);
109
110   return;
111 }
112
113 static void set(double date, void *function, void *arg)
114 {
115   command_t command = NULL;
116
117   command = command_new(function, arg);
118
119   tmgr_history_add_trace(history, empty_trace, date, 0, command);
120   xbt_heap_push(timer_heap, NULL, date);
121
122   DEBUG1("Putting value %lf on heap", date);
123 }
124
125
126 static int get(void **function, void **arg)
127 {
128   command_t command = NULL;
129
130   command = xbt_swag_extract(command_to_run);
131   if (command) {
132     *function = command->function;
133     *arg = command->args;
134     command_free(command);
135     return 1;
136   } else {
137     return 0;
138   }
139 }
140
141 static void action_suspend(surf_action_t action)
142 {
143   DIE_IMPOSSIBLE;
144 }
145
146 static void action_resume(surf_action_t action)
147 {
148   DIE_IMPOSSIBLE;
149 }
150
151 static int action_is_suspended(surf_action_t action)
152 {
153   DIE_IMPOSSIBLE;
154   return 0;
155 }
156
157 static void finalize(void)
158 {
159   xbt_heap_free(timer_heap);
160   timer_heap = NULL;
161
162   tmgr_trace_free(empty_trace);
163   empty_trace = NULL;
164
165   xbt_swag_free(command_pending);
166   xbt_swag_free(command_to_run);
167
168   surf_model_exit(surf_timer_model);
169   surf_timer_model = NULL;
170 }
171
172 static void surf_timer_model_init_internal(void)
173 {
174   surf_timer_model = surf_model_init();
175
176   surf_timer_model->name = "TIMER";
177   surf_timer_model->action_state_set = timer_action_state_set;
178
179   surf_timer_model->model_private->resource_used = resource_used;
180   surf_timer_model->model_private->share_resources = share_resources;
181   surf_timer_model->model_private->update_actions_state =
182     update_actions_state;
183   surf_timer_model->model_private->update_resource_state =
184     update_resource_state;
185   surf_timer_model->model_private->finalize = finalize;
186
187   surf_timer_model->suspend = action_suspend;
188   surf_timer_model->resume = action_resume;
189   surf_timer_model->is_suspended = action_is_suspended;
190
191   surf_timer_model->extension.timer.set = set;
192   surf_timer_model->extension.timer.get = get;
193
194   {
195     s_command_t var;
196     command_pending = xbt_swag_new(xbt_swag_offset(var, command_set_hookup));
197     command_to_run = xbt_swag_new(xbt_swag_offset(var, command_set_hookup));
198   }
199
200   empty_trace = tmgr_empty_trace_new();
201   timer_heap = xbt_heap_new(8, NULL);
202 }
203
204 void surf_timer_model_init(const char *filename)
205 {
206   if (surf_timer_model)
207     return;
208   surf_timer_model_init_internal();
209   xbt_dynar_push(model_list, &surf_timer_model);
210 }