Logo AND Algorithmique Numérique Distribuée

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