Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Initializing logs once and only once.
[simgrid.git] / src / surf / surf.c
1 /* Authors: Arnaud Legrand                                                  */
2
3 /* This program is free software; you can redistribute it and/or modify it
4    under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "surf_private.h"
7
8 static xbt_heap_float_t NOW=0;
9
10 xbt_dynar_t resource_list = NULL;
11 tmgr_history_t history = NULL;
12 lmm_system_t maxmin_system = NULL;
13
14 e_surf_action_state_t surf_action_get_state(surf_action_t action)
15 {
16   surf_action_state_t action_state = &(action->resource_type->common_public->states); 
17   
18   if(action->state_set == action_state->ready_action_set)
19     return SURF_ACTION_READY;
20   if(action->state_set == action_state->running_action_set)
21     return SURF_ACTION_RUNNING;
22   if(action->state_set == action_state->failed_action_set)
23     return SURF_ACTION_FAILED;
24   if(action->state_set == action_state->done_action_set)
25     return SURF_ACTION_DONE;
26   return SURF_ACTION_NOT_IN_THE_SYSTEM;
27 }
28
29 void surf_action_free(surf_action_t * action)
30 {
31   (*action)->resource_type->common_public->action_cancel(*action);
32   xbt_free(*action);
33   *action=NULL;
34 }
35
36 void surf_action_change_state(surf_action_t action, e_surf_action_state_t state)
37 {
38   surf_action_state_t action_state = &(action->resource_type->common_public->states); 
39
40   xbt_swag_remove(action, action->state_set);
41
42   if(state == SURF_ACTION_READY) 
43     action->state_set = action_state->ready_action_set;
44   else if(state == SURF_ACTION_RUNNING)
45     action->state_set = action_state->running_action_set;
46   else if(state == SURF_ACTION_FAILED)
47     action->state_set = action_state->failed_action_set;
48   else if(state == SURF_ACTION_DONE)
49     action->state_set = action_state->done_action_set;
50   else action->state_set = NULL;
51
52   if(action->state_set) xbt_swag_insert(action, action->state_set);
53 }
54
55 void surf_init(void)
56 {
57   xbt_init();
58   if(!resource_list) resource_list = xbt_dynar_new(sizeof(surf_resource_private_t), NULL);
59   if(!history) history = tmgr_history_new();
60   if(!maxmin_system) maxmin_system = lmm_system_new();
61 }
62
63 void surf_finalize(void)
64
65   int i;
66   surf_resource_t resource = NULL;
67
68   xbt_dynar_foreach (resource_list,i,resource) {
69     resource->common_private->finalize();
70   }
71
72   if(maxmin_system) {
73     lmm_system_free(maxmin_system);
74     maxmin_system = NULL;
75   }
76   if(history) {
77     tmgr_history_free(history);
78     history = NULL;
79   }
80   if(resource_list) xbt_dynar_free(&resource_list);
81
82   tmgr_finalize();
83 }
84
85 xbt_heap_float_t surf_solve(void)
86 {
87   static int first_run = 1;
88
89   xbt_heap_float_t min = -1.0;
90   xbt_heap_float_t next_event_date = -1.0;
91   xbt_heap_float_t resource_next_action_end = -1.0;
92   xbt_maxmin_float_t value = -1.0;
93   surf_resource_object_t resource_obj = NULL;
94   surf_resource_t resource = NULL;
95   tmgr_trace_event_t event = NULL;
96   int i;
97
98   if(first_run) {
99     while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
100       if(next_event_date > NOW) break;
101       while ((event = tmgr_history_get_next_event_leq(history, next_event_date,
102                                                       &value, (void **) &resource_obj))) {
103         resource_obj->resource->common_private->update_resource_state(resource_obj,
104                                                                       event, value);
105       }
106     }
107     xbt_dynar_foreach (resource_list,i,resource) {
108       resource->common_private->update_actions_state(NOW, 0.0);
109     }
110     first_run = 0;
111     return 0.0;
112   }
113
114   min = -1.0;
115
116   xbt_dynar_foreach (resource_list,i,resource) {
117     resource_next_action_end = resource->common_private->share_resources(NOW);
118     if(((min<0.0) || (resource_next_action_end<min)) && (resource_next_action_end>=0.0))
119       min = resource_next_action_end;
120   }
121
122   if(min<0.0) return 0.0;
123
124   while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
125     if(next_event_date > NOW+min) break;
126     while ((event=tmgr_history_get_next_event_leq(history, next_event_date,
127                                                   &value, (void **) &resource_obj))) {
128       if(resource_obj->resource->common_private->resource_used(resource_obj)) {
129         min = next_event_date-NOW;
130       }
131       /* update state of resource_obj according to new value. Does not touch lmm.
132          It will be modified if needed when updating actions */
133       resource_obj->resource->common_private->update_resource_state(resource_obj,
134                                                                     event, value);
135     }
136   }
137
138
139   xbt_dynar_foreach (resource_list,i,resource) {
140     resource->common_private->update_actions_state(NOW, min);
141   }
142
143   NOW=NOW+min;
144
145   return min;
146 }
147
148 xbt_heap_float_t surf_get_clock(void)
149 {
150   return NOW;
151 }