Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simdag/dot,simdag/dax] move the test for acyclic graph to the daxloader
[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,
43                       0);
44   return command;
45 }
46
47 static void command_free(command_t command)
48 {
49   if (xbt_swag_belongs(command, command_to_run)) {
50     xbt_swag_remove(command, command_to_run);
51   } else if (xbt_swag_belongs(command, command_pending)) {
52     xbt_swag_remove(command, command_pending);
53   }
54   xbt_swag_remove(command->action,
55                   surf_timer_model->states.running_action_set);
56   xbt_free(command->action);
57   surf_resource_free((surf_resource_t) command);
58   return;
59 }
60
61 static void parse_timer(void)
62 {
63 }
64
65 static void parse_file(const char *file)
66 {
67 }
68
69 static int resource_used(void *resource_id)
70 {
71   return 1;
72 }
73
74 static void timer_action_state_set(surf_action_t action,
75                                    e_surf_action_state_t state)
76 {
77   DIE_IMPOSSIBLE;
78   return;
79 }
80
81 static double share_resources(double now)
82 {
83   if (xbt_heap_size(timer_heap)) {
84     DEBUG1("Share resoure returning %lf", xbt_heap_maxkey(timer_heap));
85     return (xbt_heap_maxkey(timer_heap));
86   } else
87     return -1.0;
88 }
89
90 static void update_actions_state(double now, double delta)
91 {
92   while ((xbt_heap_size(timer_heap)) > 0
93          && (xbt_heap_maxkey(timer_heap) <= now + delta))
94     xbt_heap_pop(timer_heap);
95   return;
96 }
97
98 static void update_resource_state(void *id,
99                                   tmgr_trace_event_t event_type,
100                                   double value, double date)
101 {
102   command_t command = id;
103
104   /* Move this command to the list of commands to execute */
105   xbt_swag_remove(command, command_pending);
106   xbt_swag_insert(command, command_to_run);
107   tmgr_trace_event_free(event_type);
108
109   DEBUG1("Insert command on date %lf", date);
110
111   return;
112 }
113
114 static void set(double date, void *function, void *arg)
115 {
116   command_t command = NULL;
117
118   command = command_new(function, arg);
119
120   tmgr_history_add_trace(history, empty_trace, date, 0, command);
121   xbt_heap_push(timer_heap, NULL, date);
122
123   DEBUG1("Putting value %lf on heap", date);
124 }
125
126
127 static int get(void **function, void **arg)
128 {
129   command_t command = NULL;
130
131   command = xbt_swag_extract(command_to_run);
132   if (command) {
133     *function = command->function;
134     *arg = command->args;
135     command_free(command);
136     return 1;
137   } else {
138     return 0;
139   }
140 }
141
142 static void action_suspend(surf_action_t action)
143 {
144   DIE_IMPOSSIBLE;
145 }
146
147 static void action_resume(surf_action_t action)
148 {
149   DIE_IMPOSSIBLE;
150 }
151
152 static int action_is_suspended(surf_action_t action)
153 {
154   DIE_IMPOSSIBLE;
155   return 0;
156 }
157
158 static void timer_finalize(void)
159 {
160   xbt_heap_free(timer_heap);
161   timer_heap = NULL;
162
163   tmgr_trace_free(empty_trace);
164   empty_trace = NULL;
165
166   xbt_swag_free(command_pending);
167   xbt_swag_free(command_to_run);
168
169   surf_model_exit(surf_timer_model);
170   surf_timer_model = NULL;
171 }
172
173 static void surf_timer_model_init_internal(void)
174 {
175   surf_timer_model = surf_model_init();
176
177   surf_timer_model->name = "TIMER";
178   surf_timer_model->action_state_set = timer_action_state_set;
179
180   surf_timer_model->model_private->resource_used = resource_used;
181   surf_timer_model->model_private->share_resources = share_resources;
182   surf_timer_model->model_private->update_actions_state =
183       update_actions_state;
184   surf_timer_model->model_private->update_resource_state =
185       update_resource_state;
186   surf_timer_model->model_private->finalize = timer_finalize;
187
188   surf_timer_model->suspend = action_suspend;
189   surf_timer_model->resume = action_resume;
190   surf_timer_model->is_suspended = action_is_suspended;
191
192   surf_timer_model->extension.timer.set = set;
193   surf_timer_model->extension.timer.get = get;
194
195   {
196     s_command_t var;
197     command_pending =
198         xbt_swag_new(xbt_swag_offset(var, command_set_hookup));
199     command_to_run =
200         xbt_swag_new(xbt_swag_offset(var, command_set_hookup));
201   }
202
203   empty_trace = tmgr_empty_trace_new();
204   timer_heap = xbt_heap_new(8, NULL);
205 }
206
207 void surf_timer_model_init(const char *filename)
208 {
209   if (surf_timer_model)
210     return;
211   surf_timer_model_init_internal();
212   xbt_dynar_push(model_list, &surf_timer_model);
213 }