Logo AND Algorithmique Numérique Distribuée

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