Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove unnecessry files
[simgrid.git] / src / surf / surf_interface.cpp
1 #include "surf.hpp"
2 #include "workstation.hpp"
3 #include "network.hpp"
4 #include "surf_routing_cluster.hpp"
5 #include "instr/instr_private.h"
6
7 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_kernel);
8
9 /*********
10  * TOOLS *
11  *********/
12 extern double NOW;
13
14 static CpuPtr get_casted_cpu(surf_resource_t resource){
15   return dynamic_cast<CpuPtr>(static_cast<ResourcePtr>(surf_cpu_resource_priv(resource)));
16 }
17
18 static WorkstationCLM03Ptr get_casted_workstation(surf_resource_t resource){
19   return dynamic_cast<WorkstationCLM03Ptr>(static_cast<ResourcePtr>(surf_workstation_resource_priv(resource)));
20 }
21
22 char *surf_routing_edge_name(sg_routing_edge_t edge){
23   return edge->p_name;
24 }
25
26 #ifdef CONTEXT_THREADS
27 static xbt_parmap_t surf_parmap = NULL; /* parallel map on models */
28 #endif
29
30 static double *surf_mins = NULL; /* return value of share_resources for each model */
31 static int surf_min_index;       /* current index in surf_mins */
32 static double surf_min;               /* duration determined by surf_solve */
33
34 void surf_presolve(void)
35 {
36   double next_event_date = -1.0;
37   tmgr_trace_event_t event = NULL;
38   double value = -1.0;
39   ResourcePtr resource = NULL;
40   ModelPtr model = NULL;
41   unsigned int iter;
42
43   XBT_DEBUG
44       ("First Run! Let's \"purge\" events and put models in the right state");
45   while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
46     if (next_event_date > NOW)
47       break;
48     while ((event =
49             tmgr_history_get_next_event_leq(history, next_event_date,
50                                             &value,
51                                             (void **) &resource))) {
52       if (value >= 0){
53         resource->updateState(event, value, NOW);
54       }
55     }
56   }
57   xbt_dynar_foreach(model_list, iter, model)
58       model->updateActionsState(NOW, 0.0);
59 }
60
61 static void surf_share_resources(surf_model_t model)
62 {
63   double next_action_end = -1.0;
64   int i = __sync_fetch_and_add(&surf_min_index, 1);
65   if (strcmp(model->m_name.c_str(), "network NS3")) {
66     XBT_DEBUG("Running for Resource [%s]", model->m_name.c_str());
67     next_action_end = model->shareResources(NOW);
68     XBT_DEBUG("Resource [%s] : next action end = %f",
69         model->m_name.c_str(), next_action_end);
70   }
71   surf_mins[i] = next_action_end;
72 }
73
74 static void surf_update_actions_state(surf_model_t model)
75 {
76   model->updateActionsState(NOW, surf_min);
77 }
78
79 double surf_solve(double max_date)
80 {
81   surf_min = -1.0; /* duration */
82   double next_event_date = -1.0;
83   double model_next_action_end = -1.0;
84   double value = -1.0;
85   ResourcePtr resource = NULL;
86   ModelPtr model = NULL;
87   tmgr_trace_event_t event = NULL;
88   unsigned int iter;
89
90   if(!host_that_restart)
91     host_that_restart = xbt_dynar_new(sizeof(char*), NULL);
92
93   if (max_date != -1.0 && max_date != NOW) {
94     surf_min = max_date - NOW;
95   }
96
97   XBT_DEBUG("Looking for next action end for all models except NS3");
98
99   if (surf_mins == NULL) {
100     surf_mins = xbt_new(double, xbt_dynar_length(model_list));
101   }
102   surf_min_index = 0;
103
104   /* sequential version */
105   xbt_dynar_foreach(model_list, iter, model) {
106     surf_share_resources(static_cast<ModelPtr>(model));
107   }
108
109   unsigned i;
110   for (i = 0; i < xbt_dynar_length(model_list); i++) {
111     if ((surf_min < 0.0 || surf_mins[i] < surf_min)
112         && surf_mins[i] >= 0.0) {
113       surf_min = surf_mins[i];
114     }
115   }
116
117   XBT_DEBUG("Min for resources (remember that NS3 don't update that value) : %f", surf_min);
118
119   XBT_DEBUG("Looking for next trace event");
120
121   do {
122     XBT_DEBUG("Next TRACE event : %f", next_event_date);
123
124     next_event_date = tmgr_history_next_date(history);
125
126     if(!strcmp(surf_network_model->m_name.c_str(), "network NS3")){//FIXME: add surf_network_model->m_name &&
127       if(next_event_date!=-1.0 && surf_min!=-1.0) {
128         surf_min = MIN(next_event_date - NOW, surf_min);
129       } else{
130         surf_min = MAX(next_event_date - NOW, surf_min);
131       }
132
133       XBT_DEBUG("Run for network at most %f", surf_min);
134       // run until min or next flow
135       model_next_action_end = surf_network_model->shareResources(surf_min);
136
137       XBT_DEBUG("Min for network : %f", model_next_action_end);
138       if(model_next_action_end>=0.0)
139         surf_min = model_next_action_end;
140     }
141
142     if (next_event_date < 0.0) {
143       XBT_DEBUG("no next TRACE event. Stop searching for it");
144       break;
145     }
146
147     if ((surf_min == -1.0) || (next_event_date > NOW + surf_min)) break;
148
149     XBT_DEBUG("Updating models (min = %g, NOW = %g, next_event_date = %g)", surf_min, NOW, next_event_date);
150     while ((event =
151             tmgr_history_get_next_event_leq(history, next_event_date,
152                                             &value,
153                                             (void **) &resource))) {
154       if (resource->isUsed() || xbt_dict_get_or_null(watched_hosts_lib, resource->m_name)) {
155         surf_min = next_event_date - NOW;
156         XBT_DEBUG
157             ("This event will modify model state. Next event set to %f",
158              surf_min);
159       }
160       /* update state of model_obj according to new value. Does not touch lmm.
161          It will be modified if needed when updating actions */
162       XBT_DEBUG("Calling update_resource_state for resource %s with min %lf",
163              resource->m_name, surf_min);
164       resource->updateState(event, value, next_event_date);
165     }
166   } while (1);
167
168   /* FIXME: Moved this test to here to avoid stopping simulation if there are actions running on cpus and all cpus are with availability = 0.
169    * This may cause an infinite loop if one cpu has a trace with periodicity = 0 and the other a trace with periodicity > 0.
170    * The options are: all traces with same periodicity(0 or >0) or we need to change the way how the events are managed */
171   if (surf_min == -1.0) {
172   XBT_DEBUG("No next event at all. Bail out now.");
173     return -1.0;
174   }
175
176   XBT_DEBUG("Duration set to %f", surf_min);
177
178   NOW = NOW + surf_min;
179
180   /* sequential version */
181   xbt_dynar_foreach(model_list, iter, model) {
182     surf_update_actions_state(model);
183   }
184
185 #ifdef HAVE_TRACING
186   TRACE_paje_dump_buffer (0);
187 #endif
188
189   return surf_min;
190 }
191
192 XBT_INLINE double surf_get_clock(void)
193 {
194   return NOW;
195 }
196
197 void routing_get_route_and_latency(sg_routing_edge_t src, sg_routing_edge_t dst,
198                               xbt_dynar_t * route, double *latency){
199   routing_platf->getRouteAndLatency(src, dst, route, latency);
200 }
201
202 /*********
203  * MODEL *
204  *********/
205
206 void *surf_as_cluster_get_backbone(AS_t as){
207   return static_cast<AsClusterPtr>(as)->p_backbone;
208 }
209
210 void surf_as_cluster_set_backbone(AS_t as, void* backbone){
211   static_cast<AsClusterPtr>(as)->p_backbone = dynamic_cast<NetworkCm02LinkPtr>(static_cast<ResourcePtr>(backbone));
212 }
213
214 const char *surf_model_name(surf_model_t model){
215   return model->m_name.c_str();
216 }
217
218 xbt_swag_t surf_model_done_action_set(surf_model_t model){
219   return model->p_doneActionSet;
220 }
221
222 xbt_swag_t surf_model_failed_action_set(surf_model_t model){
223   return model->p_failedActionSet;
224 }
225
226 xbt_swag_t surf_model_ready_action_set(surf_model_t model){
227   return model->p_readyActionSet;
228 }
229
230 xbt_swag_t surf_model_running_action_set(surf_model_t model){
231   return model->p_runningActionSet;
232 }
233
234 surf_action_t surf_workstation_model_execute_parallel_task(surf_workstation_model_t model,
235                                                     int workstation_nb,
236                                             void **workstation_list,
237                                             double *computation_amount,
238                                             double *communication_amount,
239                                             double rate){
240   return static_cast<ActionPtr>(model->executeParallelTask(workstation_nb, workstation_list, computation_amount, communication_amount, rate));
241 }
242
243 surf_action_t surf_workstation_model_communicate(surf_workstation_model_t model, surf_resource_t src, surf_resource_t dst, double size, double rate){
244   return model->communicate(get_casted_workstation(src), get_casted_workstation(dst), size, rate);
245 }
246
247 xbt_dynar_t surf_workstation_model_get_route(surf_workstation_model_t model,
248                                                      surf_resource_t src, surf_resource_t dst){
249   return model->getRoute(get_casted_workstation(src), get_casted_workstation(dst));
250 }
251
252 surf_action_t surf_network_model_communicate(surf_network_model_t model, sg_routing_edge_t src, sg_routing_edge_t dst, double size, double rate){
253   return model->communicate(src, dst, size, rate);
254 }
255
256 const char *surf_resource_name(surf_cpp_resource_t resource){
257   return resource->m_name;
258 }
259
260 xbt_dict_t surf_resource_get_properties(surf_cpp_resource_t resource){
261   return resource->getProperties();
262 }
263
264 e_surf_resource_state_t surf_resource_get_state(surf_cpp_resource_t resource){
265   return resource->getState();
266 }
267
268 surf_action_t surf_workstation_sleep(surf_resource_t resource, double duration){
269   return get_casted_workstation(resource)->sleep(duration);
270 }
271
272 double surf_workstation_get_speed(surf_resource_t resource, double load){
273   return get_casted_workstation(resource)->getSpeed(load);
274 }
275
276 double surf_workstation_get_available_speed(surf_resource_t resource){
277   return get_casted_workstation(resource)->getAvailableSpeed();
278 }
279
280 int surf_workstation_get_core(surf_resource_t resource){
281   return get_casted_workstation(resource)->getCore();
282 }
283
284 surf_action_t surf_workstation_execute(surf_resource_t resource, double size){
285   return get_casted_workstation(resource)->execute(size);
286 }
287
288 double surf_workstation_get_current_power_peak(surf_resource_t resource){
289   return get_casted_workstation(resource)->getCurrentPowerPeak();
290 }
291
292 double surf_workstation_get_power_peak_at(surf_resource_t resource, int pstate_index){
293   return get_casted_workstation(resource)->getPowerPeakAt(pstate_index);
294 }
295
296 int surf_workstation_get_nb_pstates(surf_resource_t resource){
297   return get_casted_workstation(resource)->getNbPstates();
298 }
299
300 void surf_workstation_set_power_peak_at(surf_resource_t resource, int pstate_index){
301   return get_casted_workstation(resource)->setPowerPeakAt(pstate_index);
302 }
303
304 double surf_workstation_get_consumed_energy(surf_resource_t resource){
305   return get_casted_workstation(resource)->getConsumedEnergy();
306 }
307
308 xbt_dict_t surf_workstation_get_storage_list(surf_resource_t workstation){
309   return get_casted_workstation(workstation)->getStorageList();
310 }
311 surf_action_t surf_workstation_open(surf_resource_t workstation, const char* mount, const char* path){
312   return get_casted_workstation(workstation)->open(mount, path);
313 }
314
315 surf_action_t surf_workstation_close(surf_resource_t workstation, surf_file_t fd){
316   return get_casted_workstation(workstation)->close(fd);
317 }
318
319 int surf_workstation_unlink(surf_resource_t workstation, surf_file_t fd){
320   return get_casted_workstation(workstation)->unlink(fd);
321 }
322
323 surf_action_t surf_workstation_ls(surf_resource_t workstation, const char* mount, const char *path){
324   return get_casted_workstation(workstation)->ls(mount, path);
325 }
326
327 size_t surf_workstation_get_size(surf_resource_t workstation, surf_file_t fd){
328   return get_casted_workstation(workstation)->getSize(fd);
329 }
330
331 surf_action_t surf_workstation_read(surf_resource_t resource, surf_file_t fd, sg_storage_size_t size){
332   return get_casted_workstation(resource)->read(fd, size);
333 }
334
335 surf_action_t surf_workstation_write(surf_resource_t resource, surf_file_t fd, sg_storage_size_t size){
336   return get_casted_workstation(resource)->write(fd, size);
337 }
338
339 xbt_dynar_t surf_workstation_get_info(surf_resource_t resource, surf_file_t fd){
340   return get_casted_workstation(resource)->getInfo(fd);
341 }
342
343 sg_storage_size_t surf_workstation_get_free_size(surf_resource_t resource, const char* name){
344   return get_casted_workstation(resource)->getFreeSize(name);
345 }
346 sg_storage_size_t surf_workstation_get_used_size(surf_resource_t resource, const char* name){
347   return get_casted_workstation(resource)->getUsedSize(name);
348 }
349
350 int surf_network_link_is_shared(surf_cpp_resource_t link){
351   return dynamic_cast<NetworkCm02LinkPtr>(link)->isShared();
352 }
353
354 double surf_network_link_get_bandwidth(surf_cpp_resource_t link){
355   return dynamic_cast<NetworkCm02LinkPtr>(link)->getBandwidth();
356 }
357
358 double surf_network_link_get_latency(surf_cpp_resource_t link){
359   return dynamic_cast<NetworkCm02LinkPtr>(link)->getLatency();
360 }
361
362 xbt_dict_t surf_storage_get_content(surf_resource_t resource){
363   return dynamic_cast<StoragePtr>(static_cast<ResourcePtr>(surf_storage_resource_priv(resource)))->getContent();
364 }
365
366 sg_storage_size_t surf_storage_get_size(surf_resource_t resource){
367   return dynamic_cast<StoragePtr>(static_cast<ResourcePtr>(surf_storage_resource_priv(resource)))->getSize();
368 }
369
370 surf_action_t surf_cpu_execute(surf_resource_t cpu, double size){
371   return get_casted_cpu(cpu)->execute(size);
372 }
373
374 surf_action_t surf_cpu_sleep(surf_resource_t cpu, double duration){
375   return get_casted_cpu(cpu)->sleep(duration);
376 }
377
378 double surf_action_get_start_time(surf_action_t action){
379   return action->m_start;
380 }
381
382 double surf_action_get_finish_time(surf_action_t action){
383   return action->m_finish;
384 }
385
386 double surf_action_get_remains(surf_action_t action){
387   return action->getRemains();
388 }
389
390 void surf_action_unref(surf_action_t action){
391   action->unref();
392 }
393
394 void surf_action_suspend(surf_action_t action){
395   action->suspend();
396 }
397
398 void surf_action_resume(surf_action_t action){
399   action->resume();
400 }
401
402 void surf_action_cancel(surf_action_t action){
403   action->cancel();
404 }
405
406 void surf_action_set_priority(surf_action_t action, double priority){
407   action->setPriority(priority);
408 }
409
410 void surf_action_set_category(surf_action_t action, const char *category){
411   action->setCategory(category);
412 }
413
414 void *surf_action_get_data(surf_action_t action){
415   return action->p_data;
416 }
417
418 void surf_action_set_data(surf_action_t action, void *data){
419   action->p_data = data;
420 }
421
422 e_surf_action_state_t surf_action_get_state(surf_action_t action){
423   return action->getState();
424 }
425
426 int surf_action_get_cost(surf_action_t action){
427   return action->m_cost;
428 }
429
430 surf_file_t surf_storage_action_get_file(surf_action_t action){
431   return dynamic_cast<StorageActionPtr>(action)->p_file;
432 }
433
434 xbt_dict_t surf_storage_action_get_ls_dict(surf_action_t action){
435   return dynamic_cast<StorageActionPtr>(action)->p_lsDict;
436 }