Logo AND Algorithmique Numérique Distribuée

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