Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f1534035307b1ec2346fc14510d4450436ee324d
[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 %lf",
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   return model->getRoute(get_casted_workstation(src), get_casted_workstation(dst));
284 }
285
286 void surf_vm_workstation_model_create(const char *name, surf_resource_t ind_phys_host){
287   surf_vm_workstation_model->createResource(name, ind_phys_host);
288 }
289
290 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){
291   return model->communicate(src, dst, size, rate);
292 }
293
294 const char *surf_resource_name(surf_cpp_resource_t resource){
295   return resource->getName();
296 }
297
298 xbt_dict_t surf_resource_get_properties(surf_cpp_resource_t resource){
299   return resource->getProperties();
300 }
301
302 e_surf_resource_state_t surf_resource_get_state(surf_cpp_resource_t resource){
303   return resource->getState();
304 }
305
306 void surf_resource_set_state(surf_cpp_resource_t resource, e_surf_resource_state_t state){
307   resource->setState(state);
308 }
309
310 surf_action_t surf_workstation_sleep(surf_resource_t resource, double duration){
311   return get_casted_workstation(resource)->sleep(duration);
312 }
313
314 double surf_workstation_get_speed(surf_resource_t resource, double load){
315   return get_casted_workstation(resource)->getSpeed(load);
316 }
317
318 double surf_workstation_get_available_speed(surf_resource_t resource){
319   return get_casted_workstation(resource)->getAvailableSpeed();
320 }
321
322 int surf_workstation_get_core(surf_resource_t resource){
323   return get_casted_workstation(resource)->getCore();
324 }
325
326 surf_action_t surf_workstation_execute(surf_resource_t resource, double size){
327   return get_casted_workstation(resource)->execute(size);
328 }
329
330 double surf_workstation_get_current_power_peak(surf_resource_t resource){
331   return get_casted_workstation(resource)->getCurrentPowerPeak();
332 }
333
334 double surf_workstation_get_power_peak_at(surf_resource_t resource, int pstate_index){
335   return get_casted_workstation(resource)->getPowerPeakAt(pstate_index);
336 }
337
338 int surf_workstation_get_nb_pstates(surf_resource_t resource){
339   return get_casted_workstation(resource)->getNbPstates();
340 }
341
342 void surf_workstation_set_power_peak_at(surf_resource_t resource, int pstate_index){
343   return get_casted_workstation(resource)->setPowerPeakAt(pstate_index);
344 }
345
346 double surf_workstation_get_consumed_energy(surf_resource_t resource){
347   xbt_assert(surf_energy!=NULL, "The Energy plugin is not active.");
348   std::map<CpuPtr, CpuEnergyPtr>::iterator cpuIt = surf_energy->find(get_casted_workstation(resource)->p_cpu);
349   return cpuIt->second->getConsumedEnergy();
350 }
351
352 xbt_dict_t surf_workstation_get_storage_list(surf_resource_t workstation){
353   return get_casted_workstation(workstation)->getStorageList();
354 }
355
356 surf_action_t surf_workstation_open(surf_resource_t workstation, const char* mount, const char* path){
357   return get_casted_workstation(workstation)->open(mount, path);
358 }
359
360 surf_action_t surf_workstation_close(surf_resource_t workstation, surf_file_t fd){
361   return get_casted_workstation(workstation)->close(fd);
362 }
363
364 int surf_workstation_unlink(surf_resource_t workstation, surf_file_t fd){
365   return get_casted_workstation(workstation)->unlink(fd);
366 }
367
368 surf_action_t surf_workstation_ls(surf_resource_t workstation, const char* mount, const char *path){
369   return get_casted_workstation(workstation)->ls(mount, path);
370 }
371
372 size_t surf_workstation_get_size(surf_resource_t workstation, surf_file_t fd){
373   return get_casted_workstation(workstation)->getSize(fd);
374 }
375
376 surf_action_t surf_workstation_read(surf_resource_t resource, surf_file_t fd, sg_size_t size){
377   return get_casted_workstation(resource)->read(fd, size);
378 }
379
380 surf_action_t surf_workstation_write(surf_resource_t resource, surf_file_t fd, sg_size_t size){
381   return get_casted_workstation(resource)->write(fd, size);
382 }
383
384 xbt_dynar_t surf_workstation_get_info(surf_resource_t resource, surf_file_t fd){
385   return get_casted_workstation(resource)->getInfo(fd);
386 }
387
388 sg_size_t surf_workstation_get_free_size(surf_resource_t resource, const char* name){
389   return get_casted_workstation(resource)->getFreeSize(name);
390 }
391
392 sg_size_t surf_workstation_get_used_size(surf_resource_t resource, const char* name){
393   return get_casted_workstation(resource)->getUsedSize(name);
394 }
395
396 size_t surf_workstation_file_tell(surf_resource_t workstation, surf_file_t fd){
397   return get_casted_workstation(workstation)->fileTell(fd);
398 }
399
400 int surf_workstation_file_seek(surf_resource_t workstation, surf_file_t fd, sg_size_t offset, int origin){
401   return get_casted_workstation(workstation)->fileSeek(fd, offset, origin);
402 }
403
404 xbt_dynar_t surf_workstation_get_vms(surf_resource_t resource){
405   return get_casted_workstation(resource)->getVms();
406 }
407
408 void surf_workstation_get_params(surf_resource_t resource, ws_params_t params){
409   get_casted_workstation(resource)->getParams(params);
410 }
411
412 void surf_workstation_set_params(surf_resource_t resource, ws_params_t params){
413   get_casted_workstation(resource)->setParams(params);
414 }
415
416 void surf_vm_workstation_destroy(surf_resource_t resource){
417   delete get_casted_vm_workstation(resource);
418 }
419
420 void surf_vm_workstation_suspend(surf_resource_t resource){
421   get_casted_vm_workstation(resource)->suspend();
422 }
423
424 void surf_vm_workstation_resume(surf_resource_t resource){
425   get_casted_vm_workstation(resource)->resume();
426 }
427
428 void surf_vm_workstation_save(surf_resource_t resource){
429   get_casted_vm_workstation(resource)->save();
430 }
431
432 void surf_vm_workstation_restore(surf_resource_t resource){
433   get_casted_vm_workstation(resource)->restore();
434 }
435
436 void surf_vm_workstation_migrate(surf_resource_t resource, surf_resource_t ind_vm_ws_dest){
437   get_casted_vm_workstation(resource)->migrate(ind_vm_ws_dest);
438 }
439
440 surf_resource_t surf_vm_workstation_get_pm(surf_resource_t resource){
441   return get_casted_vm_workstation(resource)->getPm();
442 }
443
444 void surf_vm_workstation_set_bound(surf_resource_t resource, double bound){
445   return get_casted_vm_workstation(resource)->setBound(bound);
446 }
447
448 void surf_vm_workstation_set_affinity(surf_resource_t resource, surf_resource_t cpu, unsigned long mask){
449   return get_casted_vm_workstation(resource)->setAffinity(get_casted_cpu(cpu), mask);
450 }
451
452 int surf_network_link_is_shared(surf_cpp_resource_t link){
453   return static_cast<NetworkLinkPtr>(link)->isShared();
454 }
455
456 double surf_network_link_get_bandwidth(surf_cpp_resource_t link){
457   return static_cast<NetworkLinkPtr>(link)->getBandwidth();
458 }
459
460 double surf_network_link_get_latency(surf_cpp_resource_t link){
461   return static_cast<NetworkLinkPtr>(link)->getLatency();
462 }
463
464 xbt_dict_t surf_storage_get_content(surf_resource_t resource){
465   return static_cast<StoragePtr>(surf_storage_resource_priv(resource))->getContent();
466 }
467
468 sg_size_t surf_storage_get_size(surf_resource_t resource){
469   return static_cast<StoragePtr>(surf_storage_resource_priv(resource))->getSize();
470 }
471
472 void surf_storage_rename(surf_resource_t resource, const char* src, const char* dest){
473   static_cast<StoragePtr>(surf_storage_resource_priv(resource))->rename(src, dest);
474 }
475
476 surf_action_t surf_cpu_execute(surf_resource_t cpu, double size){
477   return get_casted_cpu(cpu)->execute(size);
478 }
479
480 surf_action_t surf_cpu_sleep(surf_resource_t cpu, double duration){
481   return get_casted_cpu(cpu)->sleep(duration);
482 }
483
484 double surf_action_get_start_time(surf_action_t action){
485   return action->getStartTime();
486 }
487
488 double surf_action_get_finish_time(surf_action_t action){
489   return action->getFinishTime();
490 }
491
492 double surf_action_get_remains(surf_action_t action){
493   return action->getRemains();
494 }
495
496 void surf_action_unref(surf_action_t action){
497   action->unref();
498 }
499
500 void surf_action_suspend(surf_action_t action){
501   action->suspend();
502 }
503
504 void surf_action_resume(surf_action_t action){
505   action->resume();
506 }
507
508 void surf_action_cancel(surf_action_t action){
509   action->cancel();
510 }
511
512 void surf_action_set_priority(surf_action_t action, double priority){
513   action->setPriority(priority);
514 }
515
516 void surf_action_set_category(surf_action_t action, const char *category){
517   action->setCategory(category);
518 }
519
520 void *surf_action_get_data(surf_action_t action){
521   return action->getData();
522 }
523
524 void surf_action_set_data(surf_action_t action, void *data){
525   action->setData(data);
526 }
527
528 e_surf_action_state_t surf_action_get_state(surf_action_t action){
529   return action->getState();
530 }
531
532 double surf_action_get_cost(surf_action_t action){
533   return action->getCost();
534 }
535
536 void surf_cpu_action_set_affinity(surf_action_t action, surf_resource_t cpu, unsigned long mask) {
537   static_cast<CpuActionPtr>(action)->setAffinity(get_casted_cpu(cpu), mask);
538 }
539
540 void surf_cpu_action_set_bound(surf_action_t action, double bound) {
541   static_cast<CpuActionPtr>(action)->setBound(bound);
542 }
543
544 double surf_network_action_get_latency_limited(surf_action_t action) {
545   return static_cast<NetworkActionPtr>(action)->getLatencyLimited();
546 }
547
548 surf_file_t surf_storage_action_get_file(surf_action_t action){
549   return static_cast<StorageActionPtr>(action)->p_file;
550 }
551
552 xbt_dict_t surf_storage_action_get_ls_dict(surf_action_t action){
553   return static_cast<StorageActionPtr>(action)->p_lsDict;
554 }