Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add explicit initialization for the f77 field.
[simgrid.git] / src / surf / surf_c_bindings.cpp
1 /* Copyright (c) 2013-2014. 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 "surf_interface.hpp"
8 #include "workstation_interface.hpp"
9 #include "vm_workstation_interface.hpp"
10 #include "network_interface.hpp"
11 #include "surf_routing_cluster.hpp"
12 #include "instr/instr_private.h"
13 #include "plugins/energy.hpp"
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_kernel);
16
17 /*********
18  * TOOLS *
19  *********/
20
21 static CpuPtr get_casted_cpu(surf_resource_t resource){
22   return static_cast<CpuPtr>(surf_cpu_resource_priv(resource));
23 }
24
25 static WorkstationPtr get_casted_workstation(surf_resource_t resource){
26   return static_cast<WorkstationPtr>(surf_workstation_resource_priv(resource));
27 }
28
29 static WorkstationVMPtr get_casted_vm_workstation(surf_resource_t resource){
30   return static_cast<WorkstationVMPtr>(surf_workstation_resource_priv(resource));
31 }
32
33 char *surf_routing_edge_name(sg_routing_edge_t edge){
34   return edge->getName();
35 }
36
37 #ifdef CONTEXT_THREADS
38 //FIXME:keeporremove static xbt_parmap_t surf_parmap = NULL; /* parallel map on models */
39 #endif
40
41 extern double NOW;
42 extern double *surf_mins; /* return value of share_resources for each model */
43 extern int surf_min_index;       /* current index in surf_mins */
44 extern double surf_min;               /* duration determined by surf_solve */
45
46 void surf_presolve(void)
47 {
48   double next_event_date = -1.0;
49   tmgr_trace_event_t event = NULL;
50   double value = -1.0;
51   ResourcePtr resource = NULL;
52   ModelPtr model = NULL;
53   unsigned int iter;
54
55   XBT_DEBUG
56       ("First Run! Let's \"purge\" events and put models in the right state");
57   while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
58     if (next_event_date > NOW)
59       break;
60     while ((event =
61             tmgr_history_get_next_event_leq(history, next_event_date,
62                                             &value,
63                                             (void **) &resource))) {
64       if (value >= 0){
65         resource->updateState(event, value, NOW);
66       }
67     }
68   }
69   xbt_dynar_foreach(model_list, iter, model)
70       model->updateActionsState(NOW, 0.0);
71 }
72
73 static void surf_share_resources(surf_model_t model)
74 {
75   double next_action_end = -1.0;
76   int i = __sync_fetch_and_add(&surf_min_index, 1);
77   if (strcmp(model->getName(), "network NS3")) {
78     XBT_DEBUG("Running for Resource [%s]", model->getName());
79     next_action_end = model->shareResources(NOW);
80     XBT_DEBUG("Resource [%s] : next action end = %f",
81         model->getName(), next_action_end);
82   }
83   surf_mins[i] = next_action_end;
84 }
85
86 static void surf_update_actions_state(surf_model_t model)
87 {
88   model->updateActionsState(NOW, surf_min);
89 }
90
91 double surf_solve(double max_date)
92 {
93   surf_min = -1.0; /* duration */
94   double next_event_date = -1.0;
95   double model_next_action_end = -1.0;
96   double value = -1.0;
97   ResourcePtr resource = NULL;
98   ModelPtr model = NULL;
99   tmgr_trace_event_t event = NULL;
100   unsigned int iter;
101
102   if(!host_that_restart)
103     host_that_restart = xbt_dynar_new(sizeof(char*), NULL);
104
105   if (max_date != -1.0 && max_date != NOW) {
106     surf_min = max_date - NOW;
107   }
108
109   XBT_DEBUG("Looking for next action end for all models except NS3");
110
111   if (surf_mins == NULL) {
112     surf_mins = xbt_new(double, xbt_dynar_length(model_list_invoke));
113   }
114   surf_min_index = 0;
115
116   /* sequential version */
117   xbt_dynar_foreach(model_list_invoke, iter, model) {
118     surf_share_resources(static_cast<ModelPtr>(model));
119   }
120
121   unsigned i;
122   for (i = 0; i < xbt_dynar_length(model_list_invoke); i++) {
123     if ((surf_min < 0.0 || surf_mins[i] < surf_min)
124         && surf_mins[i] >= 0.0) {
125       surf_min = surf_mins[i];
126     }
127   }
128
129   XBT_DEBUG("Min for resources (remember that NS3 don't update that value) : %f", surf_min);
130
131   XBT_DEBUG("Looking for next trace event");
132
133   do {
134     XBT_DEBUG("Next TRACE event : %f", next_event_date);
135
136     next_event_date = tmgr_history_next_date(history);
137
138     if(!strcmp(surf_network_model->getName(), "network NS3")){
139       if(next_event_date!=-1.0 && surf_min!=-1.0) {
140         surf_min = MIN(next_event_date - NOW, surf_min);
141       } else{
142         surf_min = MAX(next_event_date - NOW, surf_min);
143       }
144
145       XBT_DEBUG("Run for network at most %f", surf_min);
146       // run until min or next flow
147       model_next_action_end = surf_network_model->shareResources(surf_min);
148
149       XBT_DEBUG("Min for network : %f", model_next_action_end);
150       if(model_next_action_end>=0.0)
151         surf_min = model_next_action_end;
152     }
153
154     if (next_event_date < 0.0) {
155       XBT_DEBUG("no next TRACE event. Stop searching for it");
156       break;
157     }
158
159     if ((surf_min == -1.0) || (next_event_date > NOW + surf_min)) break;
160
161     XBT_DEBUG("Updating models (min = %g, NOW = %g, next_event_date = %g)", surf_min, NOW, next_event_date);
162     while ((event =
163             tmgr_history_get_next_event_leq(history, next_event_date,
164                                             &value,
165                                             (void **) &resource))) {
166       if (resource->isUsed() || xbt_dict_get_or_null(watched_hosts_lib, resource->getName())) {
167         surf_min = next_event_date - NOW;
168         XBT_DEBUG
169             ("This event will modify model state. Next event set to %f",
170              surf_min);
171       }
172       /* update state of model_obj according to new value. Does not touch lmm.
173          It will be modified if needed when updating actions */
174       XBT_DEBUG("Calling update_resource_state for resource %s with min %f",
175              resource->getName(), surf_min);
176       resource->updateState(event, value, next_event_date);
177     }
178   } while (1);
179
180   /* FIXME: Moved this test to here to avoid stopping simulation if there are actions running on cpus and all cpus are with availability = 0.
181    * This may cause an infinite loop if one cpu has a trace with periodicity = 0 and the other a trace with periodicity > 0.
182    * The options are: all traces with same periodicity(0 or >0) or we need to change the way how the events are managed */
183   if (surf_min == -1.0) {
184   XBT_DEBUG("No next event at all. Bail out now.");
185     return -1.0;
186   }
187
188   XBT_DEBUG("Duration set to %f", surf_min);
189
190   NOW = NOW + surf_min;
191   /* FIXME: model_list or model_list_invoke? revisit here later */
192   /* sequential version */
193   xbt_dynar_foreach(model_list, iter, model) {
194     surf_update_actions_state(model);
195   }
196
197 #ifdef HAVE_TRACING
198   TRACE_paje_dump_buffer (0);
199 #endif
200
201   return surf_min;
202 }
203
204 void routing_get_route_and_latency(sg_routing_edge_t src, sg_routing_edge_t dst,
205                               xbt_dynar_t * route, double *latency){
206   routing_platf->getRouteAndLatency(src, dst, route, latency);
207 }
208
209 /*********
210  * MODEL *
211  *********/
212
213 surf_model_t surf_resource_model(const void *host, int level) {
214   /* If level is SURF_WKS_LEVEL, ws is a workstation_CLM03 object. It has
215    * surf_resource at the generic_resource field. */
216   ResourcePtr ws = static_cast<ResourcePtr>(xbt_lib_get_level((xbt_dictelm_t) host, level));
217   return ws->getModel();
218 }
219
220 void *surf_as_cluster_get_backbone(AS_t as){
221   return static_cast<AsClusterPtr>(as)->p_backbone;
222 }
223
224 void surf_as_cluster_set_backbone(AS_t as, void* backbone){
225   static_cast<AsClusterPtr>(as)->p_backbone = static_cast<NetworkLinkPtr>(backbone);
226 }
227
228 const char *surf_model_name(surf_model_t model){
229   return model->getName();
230 }
231
232 surf_action_t surf_model_extract_done_action_set(surf_model_t model){
233   if (model->getDoneActionSet()->empty())
234         return NULL;
235   surf_action_t res = &model->getDoneActionSet()->front();
236   model->getDoneActionSet()->pop_front();
237   return res;
238 }
239
240 surf_action_t surf_model_extract_failed_action_set(surf_model_t model){
241   if (model->getFailedActionSet()->empty())
242         return NULL;
243   surf_action_t res = &model->getFailedActionSet()->front();
244   model->getFailedActionSet()->pop_front();
245   return res;
246 }
247
248 surf_action_t surf_model_extract_ready_action_set(surf_model_t model){
249   if (model->getReadyActionSet()->empty())
250         return NULL;
251   surf_action_t res = &model->getReadyActionSet()->front();
252   model->getReadyActionSet()->pop_front();
253   return res;
254 }
255
256 surf_action_t surf_model_extract_running_action_set(surf_model_t model){
257   if (model->getRunningActionSet()->empty())
258         return NULL;
259   surf_action_t res = &model->getRunningActionSet()->front();
260   model->getRunningActionSet()->pop_front();
261   return res;
262 }
263
264 int surf_model_running_action_set_size(surf_model_t model){
265   return model->getRunningActionSet()->size();
266 }
267
268 surf_action_t surf_workstation_model_execute_parallel_task(surf_workstation_model_t model,
269                                                     int workstation_nb,
270                                             void **workstation_list,
271                                             double *computation_amount,
272                                             double *communication_amount,
273                                             double rate){
274   return static_cast<ActionPtr>(model->executeParallelTask(workstation_nb, workstation_list, computation_amount, communication_amount, rate));
275 }
276
277 surf_action_t surf_workstation_model_communicate(surf_workstation_model_t model, surf_resource_t src, surf_resource_t dst, double size, double rate){
278   return model->communicate(get_casted_workstation(src), get_casted_workstation(dst), size, rate);
279 }
280
281 xbt_dynar_t surf_workstation_model_get_route(surf_workstation_model_t /*model*/,
282                                              surf_resource_t src, surf_resource_t dst){
283   xbt_dynar_t route = NULL;
284   routing_platf->getRouteAndLatency(get_casted_workstation(src)->p_netElm,
285                                             get_casted_workstation(dst)->p_netElm, &route, NULL);
286   return route;
287 }
288
289 void surf_vm_workstation_model_create(const char *name, surf_resource_t ind_phys_host){
290   surf_vm_workstation_model->createResource(name, ind_phys_host);
291 }
292
293 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){
294   return model->communicate(src, dst, size, rate);
295 }
296
297 const char *surf_resource_name(surf_cpp_resource_t resource){
298   return resource->getName();
299 }
300
301 xbt_dict_t surf_resource_get_properties(surf_cpp_resource_t resource){
302   return resource->getProperties();
303 }
304
305 e_surf_resource_state_t surf_resource_get_state(surf_cpp_resource_t resource){
306   return resource->getState();
307 }
308
309 void surf_resource_set_state(surf_cpp_resource_t resource, e_surf_resource_state_t state){
310   resource->setState(state);
311 }
312
313 surf_action_t surf_workstation_sleep(surf_resource_t resource, double duration){
314   return get_casted_workstation(resource)->sleep(duration);
315 }
316
317 double surf_workstation_get_speed(surf_resource_t resource, double load){
318   return get_casted_workstation(resource)->getSpeed(load);
319 }
320
321 double surf_workstation_get_available_speed(surf_resource_t resource){
322   return get_casted_workstation(resource)->getAvailableSpeed();
323 }
324
325 int surf_workstation_get_core(surf_resource_t resource){
326   return get_casted_workstation(resource)->getCore();
327 }
328
329 surf_action_t surf_workstation_execute(surf_resource_t resource, double size){
330   return get_casted_workstation(resource)->execute(size);
331 }
332
333 double surf_workstation_get_current_power_peak(surf_resource_t resource){
334   return get_casted_workstation(resource)->getCurrentPowerPeak();
335 }
336
337 double surf_workstation_get_power_peak_at(surf_resource_t resource, int pstate_index){
338   return get_casted_workstation(resource)->getPowerPeakAt(pstate_index);
339 }
340
341 int surf_workstation_get_nb_pstates(surf_resource_t resource){
342   return get_casted_workstation(resource)->getNbPstates();
343 }
344
345 void surf_workstation_set_power_peak_at(surf_resource_t resource, int pstate_index){
346   return get_casted_workstation(resource)->setPowerPeakAt(pstate_index);
347 }
348
349 double surf_workstation_get_consumed_energy(surf_resource_t resource){
350   xbt_assert(surf_energy!=NULL, "The Energy plugin is not active.");
351   std::map<CpuPtr, CpuEnergyPtr>::iterator cpuIt = surf_energy->find(get_casted_workstation(resource)->p_cpu);
352   return cpuIt->second->getConsumedEnergy();
353 }
354
355 xbt_dict_t surf_workstation_get_mounted_storage_list(surf_resource_t workstation){
356   return get_casted_workstation(workstation)->getMountedStorageList();
357 }
358
359 xbt_dynar_t surf_workstation_get_attached_storage_list(surf_resource_t workstation){
360   return get_casted_workstation(workstation)->getAttachedStorageList();
361 }
362
363 surf_action_t surf_workstation_open(surf_resource_t workstation, const char* mount, const char* path){
364   return get_casted_workstation(workstation)->open(mount, path);
365 }
366
367 surf_action_t surf_workstation_close(surf_resource_t workstation, surf_file_t fd){
368   return get_casted_workstation(workstation)->close(fd);
369 }
370
371 int surf_workstation_unlink(surf_resource_t workstation, surf_file_t fd){
372   return get_casted_workstation(workstation)->unlink(fd);
373 }
374
375 surf_action_t surf_workstation_ls(surf_resource_t workstation, const char* mount, const char *path){
376   return get_casted_workstation(workstation)->ls(mount, path);
377 }
378
379 size_t surf_workstation_get_size(surf_resource_t workstation, surf_file_t fd){
380   return get_casted_workstation(workstation)->getSize(fd);
381 }
382
383 surf_action_t surf_workstation_read(surf_resource_t resource, surf_file_t fd, sg_size_t size){
384   return get_casted_workstation(resource)->read(fd, size);
385 }
386
387 surf_action_t surf_workstation_write(surf_resource_t resource, surf_file_t fd, sg_size_t size){
388   return get_casted_workstation(resource)->write(fd, size);
389 }
390
391 xbt_dynar_t surf_workstation_get_info(surf_resource_t resource, surf_file_t fd){
392   return get_casted_workstation(resource)->getInfo(fd);
393 }
394
395 sg_size_t surf_workstation_get_free_size(surf_resource_t resource, const char* name){
396   return get_casted_workstation(resource)->getFreeSize(name);
397 }
398
399 sg_size_t surf_workstation_get_used_size(surf_resource_t resource, const char* name){
400   return get_casted_workstation(resource)->getUsedSize(name);
401 }
402
403 size_t surf_workstation_file_tell(surf_resource_t workstation, surf_file_t fd){
404   return get_casted_workstation(workstation)->fileTell(fd);
405 }
406
407 int surf_workstation_file_seek(surf_resource_t workstation, surf_file_t fd, sg_size_t offset, int origin){
408   return get_casted_workstation(workstation)->fileSeek(fd, offset, origin);
409 }
410
411 xbt_dynar_t surf_workstation_get_vms(surf_resource_t resource){
412   return get_casted_workstation(resource)->getVms();
413 }
414
415 void surf_workstation_get_params(surf_resource_t resource, ws_params_t params){
416   get_casted_workstation(resource)->getParams(params);
417 }
418
419 void surf_workstation_set_params(surf_resource_t resource, ws_params_t params){
420   get_casted_workstation(resource)->setParams(params);
421 }
422
423 void surf_vm_workstation_destroy(surf_resource_t resource){
424   delete get_casted_vm_workstation(resource);
425 }
426
427 void surf_vm_workstation_suspend(surf_resource_t resource){
428   get_casted_vm_workstation(resource)->suspend();
429 }
430
431 void surf_vm_workstation_resume(surf_resource_t resource){
432   get_casted_vm_workstation(resource)->resume();
433 }
434
435 void surf_vm_workstation_save(surf_resource_t resource){
436   get_casted_vm_workstation(resource)->save();
437 }
438
439 void surf_vm_workstation_restore(surf_resource_t resource){
440   get_casted_vm_workstation(resource)->restore();
441 }
442
443 void surf_vm_workstation_migrate(surf_resource_t resource, surf_resource_t ind_vm_ws_dest){
444   get_casted_vm_workstation(resource)->migrate(ind_vm_ws_dest);
445 }
446
447 surf_resource_t surf_vm_workstation_get_pm(surf_resource_t resource){
448   return get_casted_vm_workstation(resource)->getPm();
449 }
450
451 void surf_vm_workstation_set_bound(surf_resource_t resource, double bound){
452   return get_casted_vm_workstation(resource)->setBound(bound);
453 }
454
455 void surf_vm_workstation_set_affinity(surf_resource_t resource, surf_resource_t cpu, unsigned long mask){
456   return get_casted_vm_workstation(resource)->setAffinity(get_casted_cpu(cpu), mask);
457 }
458
459 int surf_network_link_is_shared(surf_cpp_resource_t link){
460   return static_cast<NetworkLinkPtr>(link)->isShared();
461 }
462
463 double surf_network_link_get_bandwidth(surf_cpp_resource_t link){
464   return static_cast<NetworkLinkPtr>(link)->getBandwidth();
465 }
466
467 double surf_network_link_get_latency(surf_cpp_resource_t link){
468   return static_cast<NetworkLinkPtr>(link)->getLatency();
469 }
470
471 xbt_dict_t surf_storage_get_content(surf_resource_t resource){
472   return static_cast<StoragePtr>(surf_storage_resource_priv(resource))->getContent();
473 }
474
475 sg_size_t surf_storage_get_size(surf_resource_t resource){
476   return static_cast<StoragePtr>(surf_storage_resource_priv(resource))->getSize();
477 }
478
479 const char* surf_storage_get_host(surf_resource_t resource){
480   return static_cast<StoragePtr>(surf_storage_resource_priv(resource))->p_attach;
481 }
482
483 void surf_storage_rename(surf_resource_t resource, const char* src, const char* dest){
484   static_cast<StoragePtr>(surf_storage_resource_priv(resource))->rename(src, dest);
485 }
486
487 surf_action_t surf_cpu_execute(surf_resource_t cpu, double size){
488   return get_casted_cpu(cpu)->execute(size);
489 }
490
491 surf_action_t surf_cpu_sleep(surf_resource_t cpu, double duration){
492   return get_casted_cpu(cpu)->sleep(duration);
493 }
494
495 double surf_action_get_start_time(surf_action_t action){
496   return action->getStartTime();
497 }
498
499 double surf_action_get_finish_time(surf_action_t action){
500   return action->getFinishTime();
501 }
502
503 double surf_action_get_remains(surf_action_t action){
504   return action->getRemains();
505 }
506
507 void surf_action_unref(surf_action_t action){
508   action->unref();
509 }
510
511 void surf_action_suspend(surf_action_t action){
512   action->suspend();
513 }
514
515 void surf_action_resume(surf_action_t action){
516   action->resume();
517 }
518
519 void surf_action_cancel(surf_action_t action){
520   action->cancel();
521 }
522
523 void surf_action_set_priority(surf_action_t action, double priority){
524   action->setPriority(priority);
525 }
526
527 void surf_action_set_category(surf_action_t action, const char *category){
528   action->setCategory(category);
529 }
530
531 void *surf_action_get_data(surf_action_t action){
532   return action->getData();
533 }
534
535 void surf_action_set_data(surf_action_t action, void *data){
536   action->setData(data);
537 }
538
539 e_surf_action_state_t surf_action_get_state(surf_action_t action){
540   return action->getState();
541 }
542
543 double surf_action_get_cost(surf_action_t action){
544   return action->getCost();
545 }
546
547 void surf_cpu_action_set_affinity(surf_action_t action, surf_resource_t cpu, unsigned long mask) {
548   static_cast<CpuActionPtr>(action)->setAffinity(get_casted_cpu(cpu), mask);
549 }
550
551 void surf_cpu_action_set_bound(surf_action_t action, double bound) {
552   static_cast<CpuActionPtr>(action)->setBound(bound);
553 }
554
555 #ifdef HAVE_LATENCY_BOUND_TRACKING
556 double surf_network_action_get_latency_limited(surf_action_t action) {
557   return static_cast<NetworkActionPtr>(action)->getLatencyLimited();
558 }
559 #endif
560
561 surf_file_t surf_storage_action_get_file(surf_action_t action){
562   return static_cast<StorageActionPtr>(action)->p_file;
563 }
564
565 xbt_dict_t surf_storage_action_get_ls_dict(surf_action_t action){
566   return static_cast<StorageActionPtr>(action)->p_lsDict;
567 }