Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill surf_parse_get_trace() which were a useless wrapper to tmgr_trace_new
[simgrid.git] / src / surf / trace_mgr.c
index c9ac6a9..57d4dae 100644 (file)
@@ -1,13 +1,18 @@
-/* Authors: Arnaud Legrand                                                  */
+/*     $Id$     */
+
+/* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
 
 /* This program is free software; you can redistribute it and/or modify it
  under the terms of the license (GNU LGPL) which comes with this package. */
* under the terms of the license (GNU LGPL) which comes with this package. */
 
 #include "xbt/sysdep.h"
-#include "xbt/error.h"
+#include "xbt/log.h"
+#include "xbt/str.h"
 #include "xbt/dict.h"
 #include "trace_mgr_private.h"
-#include <stdlib.h>
+#include "surf_private.h"
+
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_trace, surf,"Surf trace management");
 
 static xbt_dict_t trace_list = NULL;
 static void _tmgr_trace_free(void *trace)
@@ -17,166 +22,246 @@ static void _tmgr_trace_free(void *trace)
 
 tmgr_history_t tmgr_history_new(void)
 {
-  tmgr_history_t history;
+  tmgr_history_t h;
 
-  history = xbt_new0(s_tmgr_history_t,1);
+  h = xbt_new0(s_tmgr_history_t, 1);
 
-  history->heap = xbt_heap_new(8,NULL); /* Why 8 ? Well, why not... */
+  h->heap = xbt_heap_new(8, xbt_free_f);        /* Why 8 ? Well, why not... */
 
-  return history;
+  return h;
 }
 
-void tmgr_history_free(tmgr_history_t history)
+void tmgr_history_free(tmgr_history_t h)
 {
-  xbt_heap_free(history->heap);
-  xbt_free(history);
+  xbt_heap_free(h->heap);
+  free(h);
 }
 
-tmgr_trace_t tmgr_trace_new(const char *filename)
+tmgr_trace_t tmgr_trace_new_from_string(const char *id, const char *input,
+                                        double periodicity)
 {
   tmgr_trace_t trace = NULL;
-  FILE *f = NULL;
   int linecount = 0;
-  char line[256];
-  xbt_heap_float_t current_time=0.0, previous_time = 0.0;
-  xbt_maxmin_float_t value=-1.0;
-  xbt_heap_float_t periodicity = -1.0; /* No periodicity by default */
   s_tmgr_event_t event;
   tmgr_event_t last_event = NULL;
+  xbt_dynar_t list;
+  unsigned int cpt;
+  char *val;
+
+  if (trace_list) {
+    trace = xbt_dict_get_or_null(trace_list, id);
+    if (trace)
+      return trace;
+  }
 
-  if(trace_list) {
-    xbt_dict_get(trace_list, filename, (void **) &trace);
-    if(trace) return trace;
+  if (periodicity <= 0) {
+    xbt_assert1(0, "Periodicity has to be positive. Your value %lg",
+                periodicity);
+  }
+
+  trace = xbt_new0(s_tmgr_trace_t, 1);
+  trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
+
+  list = xbt_str_split(input, "\n\r");
+
+
+  xbt_dynar_foreach(list, cpt, val) {
+    linecount++;
+    xbt_str_trim(val, " \t\n\r\x0B");
+    if (strlen(val) > 0) {
+      if (sscanf(val, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2) {
+        xbt_assert2(0, "%s\n%d: Syntax error", input, linecount);
+      }
+      if (last_event) {
+        if ((last_event->delta = event.delta - last_event->delta) <= 0) {
+          xbt_assert2(0,
+                      "%s\n%d: Invalid trace value, events have to be sorted",
+                      input, linecount);
+        }
+      }
+      xbt_dynar_push(trace->event_list, &event);
+      last_event =
+        xbt_dynar_get_ptr(trace->event_list,
+                          xbt_dynar_length(trace->event_list) - 1);
+      if (periodicity > 0) {
+        if (last_event)
+          last_event->delta = periodicity;
+      }
+    }
   }
 
-  /* Parsing et création de la trace */
+  if (!trace_list)
+    trace_list = xbt_dict_new();
+
+  xbt_dict_set(trace_list, id, (void *) trace, _tmgr_trace_free);
+
+  xbt_dynar_free(&list);
+  return trace;
+}
 
-  if ((f = fopen(filename, "r")) == NULL) {
-    fprintf(stderr, "Cannot open file '%s'\n", filename);
+tmgr_trace_t tmgr_trace_new(const char *filename)
+{
+  tmgr_trace_t trace = NULL;
+  FILE *f = NULL;
+  int linecount = 0;
+  char line[256];
+  double periodicity = -1.0;    /* No periodicity by default */
+  s_tmgr_event_t event;
+  tmgr_event_t last_event = NULL;
+
+  if ((!filename) || (strcmp(filename, "") == 0))
     return NULL;
+
+  if (trace_list) {
+    trace = xbt_dict_get_or_null(trace_list, filename);
+    if (trace) {
+      WARN1("Ignoring redefinition of trace %s",filename);
+      return trace;
+    }
   }
 
-  trace = xbt_new0(s_tmgr_trace_t,1);
-  trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t),NULL);
+  f = surf_fopen(filename, "r");
+  xbt_assert1(f!=NULL, "Cannot open file '%s'", filename);
+
+  trace = xbt_new0(s_tmgr_trace_t, 1);
+  trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
 
   while (fgets(line, 256, f)) {
     linecount++;
     if ((line[0] == '#') || (line[0] == '\n') || (line[0] == '%'))
       continue;
 
-    if (sscanf(line, "PERIODICITY " XBT_HEAP_FLOAT_T "\n", &(periodicity)) == 1){
-      if(periodicity<=0) {
-       fprintf(stderr, "%s,%d: Syntax error. Periodicity has to be positive\n", 
-               filename, linecount);
-       abort();
-/*     xbt_dynar_free(&(trace->event_list)); */
-/*     xbt_free(trace);         */
-/*     return NULL; */
+    if (sscanf(line, "PERIODICITY " "%lg" "\n", &(periodicity))
+        == 1) {
+      if (periodicity <= 0) {
+        xbt_assert2(0,
+                    "%s,%d: Syntax error. Periodicity has to be positive",
+                    filename, linecount);
       }
       continue;
     }
 
-    if (sscanf(line, XBT_HEAP_FLOAT_T " " XBT_MAXMIN_FLOAT_T "\n", &event.delta, &event.value) != 2){
-      fprintf(stderr, "%s,%d: Syntax error\n", filename, linecount);
-      abort();
-/*       xbt_dynar_free(&(trace->event_list)); */
-/*       xbt_free(trace);       */
-/*       return NULL; */
+    if (sscanf(line, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2) {
+      xbt_assert2(0, "%s,%d: Syntax error", filename, linecount);
     }
-    
-    if(last_event) {
-      if((last_event->delta=event.delta-last_event->delta) <=0) {
-       fprintf(stderr, "%s,%d: Invalid trace value, events have to be sorted\n", 
-               filename, linecount);
-       abort();
+
+    if (last_event) {
+      if ((last_event->delta = event.delta - last_event->delta) <= 0) {
+        xbt_assert2(0,
+                    "%s,%d: Invalid trace value, events have to be sorted",
+                    filename, linecount);
       }
     }
     xbt_dynar_push(trace->event_list, &event);
     last_event = xbt_dynar_get_ptr(trace->event_list,
-                                  xbt_dynar_length(trace->event_list)-1);
-    printf(XBT_HEAP_FLOAT_T " " XBT_MAXMIN_FLOAT_T "\n",
-          event.delta, event.value);
+                                   xbt_dynar_length(trace->event_list) - 1);
   }
 
-  if(periodicity>0) {
-    if(last_event)
-      last_event->delta=periodicity;
+  if (periodicity > 0) {
+    if (last_event)
+      last_event->delta = periodicity;
   }
 
-  if(!trace_list) trace_list =  xbt_dict_new();
+  if (!trace_list)
+    trace_list = xbt_dict_new();
 
   xbt_dict_set(trace_list, filename, (void *) trace, _tmgr_trace_free);
+
+  fclose(f);
+
   return trace;
 }
 
+tmgr_trace_t tmgr_empty_trace_new(void)
+{
+  tmgr_trace_t trace = NULL;
+  /*double periodicity = -1.0;   No periodicity by default; unused variables
+     tmgr_event_t last_event = NULL; */
+  s_tmgr_event_t event;
+
+  trace = xbt_new0(s_tmgr_trace_t, 1);
+  trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL);
+
+  event.delta = 0.0;
+  event.value = 0.0;
+  xbt_dynar_push(trace->event_list, &event);
+
+  return trace;
+}
 
 void tmgr_trace_free(tmgr_trace_t trace)
 {
-  if(!trace) return;
+  if (!trace)
+    return;
   xbt_dynar_free(&(trace->event_list));
-  xbt_free(trace);
+  free(trace);
 }
 
-void tmgr_history_add_trace(tmgr_history_t history, tmgr_trace_t trace, 
-                           xbt_heap_float_t start_time, int offset, 
-                           void *resource)
+tmgr_trace_event_t tmgr_history_add_trace(tmgr_history_t h,
+                                          tmgr_trace_t trace,
+                                          double start_time,
+                                          unsigned int offset, void *model)
 {
   tmgr_trace_event_t trace_event = NULL;
 
-  
-  trace_event = xbt_new0(s_tmgr_trace_event_t,1);
-  trace_event->trace=trace;
-  trace_event->idx=offset;
-  trace_event->resource=resource;
+  trace_event = xbt_new0(s_tmgr_trace_event_t, 1);
+  trace_event->trace = trace;
+  trace_event->idx = offset;
+  trace_event->model = model;
+
+  xbt_assert0((trace_event->idx < xbt_dynar_length(trace->event_list)),
+              "You're referring to an event that does not exist!");
 
-  if(trace_event->idx>= xbt_dynar_length(trace->event_list))
-    abort();
+  xbt_heap_push(h->heap, trace_event, start_time);
 
-  xbt_heap_push(history->heap, trace_event, start_time);
+  return trace_event;
 }
 
-xbt_heap_float_t tmgr_history_next_date(tmgr_history_t history)
+double tmgr_history_next_date(tmgr_history_t h)
 {
-  if(xbt_heap_size(history->heap)) return(xbt_heap_maxkey(history->heap));
-  else return -1.0;
+  if (xbt_heap_size(h->heap))
+    return (xbt_heap_maxkey(h->heap));
+  else
+    return -1.0;
 }
 
-int tmgr_history_get_next_event_leq(tmgr_history_t history,
-                                   xbt_heap_float_t date,
-                                   xbt_maxmin_float_t *value,
-                                   void **resource)
+tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t h,
+                                                   double date,
+                                                   double *value,
+                                                   void **model)
 {
-  xbt_heap_float_t event_date = xbt_heap_maxkey(history->heap);
+  double event_date = tmgr_history_next_date(h);
   tmgr_trace_event_t trace_event = NULL;
   tmgr_event_t event = NULL;
   tmgr_trace_t trace = NULL;
 
-  if(event_date > date) 
-    return 0;
-  
-  if(!(trace_event = xbt_heap_pop(history->heap)))
-    return 0;
-  
-  trace=trace_event->trace;
-  event=xbt_dynar_get_ptr(trace->event_list,
-                         trace_event->idx);
+  if (event_date > date)
+    return NULL;
+
+  if (!(trace_event = xbt_heap_pop(h->heap)))
+    return NULL;
 
+  trace = trace_event->trace;
+  event = xbt_dynar_get_ptr(trace->event_list, trace_event->idx);
 
-  *value=event->value;
-  *resource = trace_event->resource;
+  *value = event->value;
+  *model = trace_event->model;
 
-  if(trace_event->idx<xbt_dynar_length(trace->event_list)-1) {
-    xbt_heap_push(history->heap, trace_event, event_date + 
-                 event->delta);
+  if (trace_event->idx < xbt_dynar_length(trace->event_list) - 1) {
+    xbt_heap_push(h->heap, trace_event, event_date + event->delta);
     trace_event->idx++;
-  } else if(event->delta > 0) { /* Last element, checking for periodicity */
-    xbt_heap_push(history->heap, trace_event, event_date + 
-                 event->delta);
-    trace_event->idx=0;
-  } else { /* We don't need this trace_event anymore */
-    xbt_free(trace_event);
+  } else if (event->delta > 0) {        /* Last element, checking for periodicity */
+    xbt_heap_push(h->heap, trace_event, event_date + event->delta);
+    trace_event->idx = 0;
+  } else {                      /* We don't need this trace_event anymore */
+    free(trace_event);
+    return NULL;
   }
 
-  return 1;
+  return trace_event;
+}
+
+void tmgr_finalize(void)
+{
+  xbt_dict_free(&trace_list);
 }