Logo AND Algorithmique Numérique Distribuée

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