+ /* search relative files in the path */
+ xbt_dynar_foreach(surf_path, cpt, path_elm) {
+ buff = bprintf("%s" FILE_DELIM "%s", path_elm, name);
+ file = fopen(buff, mode);
+ free(buff);
+
+ if (file)
+ return file;
+ }
+ return NULL;
+}
+
+void surf_exit(void)
+{
+ unsigned int iter;
+ surf_model_t model = NULL;
+
+ surf_config_finalize();
+
+ xbt_dynar_foreach(model_list, iter, model)
+ model->model_private->finalize();
+ xbt_dynar_free(&model_list);
+ routing_exit();
+
+ if (maxmin_system) {
+ lmm_system_free(maxmin_system);
+ maxmin_system = NULL;
+ }
+ if (history) {
+ tmgr_history_free(history);
+ history = NULL;
+ }
+ surf_action_exit();
+
+ xbt_dynar_free(&surf_path);
+
+ xbt_lib_free(&host_lib);
+ xbt_lib_free(&link_lib);
+ xbt_lib_free(&as_router_lib);
+
+
+ tmgr_finalize();
+ surf_parse_lex_destroy();
+ surf_parse_free_callbacks();
+
+ NOW = 0; /* Just in case the user plans to restart the simulation afterward */
+}
+
+void surf_presolve(void)
+{
+ double next_event_date = -1.0;
+ tmgr_trace_event_t event = NULL;
+ double value = -1.0;
+ surf_resource_t resource = NULL;
+ surf_model_t model = NULL;
+ unsigned int iter;
+
+ XBT_DEBUG
+ ("First Run! Let's \"purge\" events and put models in the right state");
+ while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
+ if (next_event_date > NOW)
+ break;
+ while ((event =
+ tmgr_history_get_next_event_leq(history, next_event_date,
+ &value,
+ (void **) &resource))) {
+ resource->model->model_private->update_resource_state(resource,
+ event, value,
+ NOW);
+ }
+ }
+ xbt_dynar_foreach(model_list, iter, model)
+ model->model_private->update_actions_state(NOW, 0.0);
+}
+
+double surf_solve(double max_date)
+{
+ double min = -1.0; /* duration */
+ double next_event_date = -1.0;
+ double model_next_action_end = -1.0;
+ double value = -1.0;
+ surf_resource_t resource = NULL;
+ surf_model_t model = NULL;
+ tmgr_trace_event_t event = NULL;
+ unsigned int iter;
+
+ if (max_date != -1.0 && max_date != NOW) {
+ min = max_date - NOW;
+ }
+
+ XBT_DEBUG("Looking for next action end for all models except NS3");
+ xbt_dynar_foreach(model_list, iter, model) {
+ if(strcmp(model->name,"network NS3") ){
+ XBT_DEBUG("Running for Resource [%s]", model->name);
+ model_next_action_end = model->model_private->share_resources(NOW);
+ XBT_DEBUG("Resource [%s] : next action end = %f",
+ model->name, model_next_action_end);
+ if (((min < 0.0) || (model_next_action_end < min))
+ && (model_next_action_end >= 0.0))
+ min = model_next_action_end;
+ }
+ }
+
+ XBT_DEBUG("Min for resources (except NS3) : %f", min);
+
+ XBT_DEBUG("Looking for next trace event");
+
+ do {
+ XBT_DEBUG("Next TRACE event : %f", next_event_date);
+
+ next_event_date = tmgr_history_next_date(history);
+
+ if(surf_network_model->name && !strcmp(surf_network_model->name,"network NS3")){
+ if(next_event_date!=-1.0 && min!=-1.0) {
+ min = MIN(next_event_date - NOW, min);
+ } else{
+ min = MAX(next_event_date - NOW, min);
+ }
+
+ XBT_DEBUG("Run for NS3 at most %f", min);
+ // run until min or next flow
+ model_next_action_end = surf_network_model->model_private->share_resources(min);
+
+ XBT_DEBUG("Min for NS3 : %f", model_next_action_end);
+ if(model_next_action_end>=0.0)
+ min = model_next_action_end;
+ }
+
+ if (next_event_date == -1.0) break;
+
+ if ((min != -1.0) && (next_event_date > NOW + min)) break;
+
+ XBT_DEBUG("Updating models");
+ while ((event =
+ tmgr_history_get_next_event_leq(history, next_event_date,
+ &value,
+ (void **) &resource))) {
+ if (resource->model->model_private->resource_used(resource)) {
+ min = next_event_date - NOW;
+ XBT_DEBUG
+ ("This event will modify model state. Next event set to %f",
+ min);
+ }
+ /* update state of model_obj according to new value. Does not touch lmm.
+ It will be modified if needed when updating actions */
+ XBT_DEBUG("Calling update_resource_state for resource %s with min %lf",
+ resource->model->name, min);
+ resource->model->model_private->update_resource_state(resource,
+ event, value,
+ NOW + min);
+ }
+ } while (1);
+
+ /* FIXME: Moved this test to here to avoid stoping simulation if there are actions running on cpus and all cpus are with availability = 0.
+ * This may cause an infinite loop if one cpu has a trace with periodicity = 0 and the other a trace with periodicity > 0.
+ * The options are: all traces with same periodicity(0 or >0) or we need to change the way how the events are managed */
+ if (min < 0.0)
+ return -1.0;
+
+ XBT_DEBUG("Duration set to %f", min);
+
+ NOW = NOW + min;
+
+ xbt_dynar_foreach(model_list, iter, model)
+ model->model_private->update_actions_state(NOW, min);
+
+#ifdef HAVE_TRACING
+ TRACE_paje_dump_buffer (0);
+#endif
+
+ return min;
+}
+
+XBT_INLINE double surf_get_clock(void)
+{
+ return NOW;
+}