Logo AND Algorithmique Numérique Distribuée

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