Logo AND Algorithmique Numérique Distribuée

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