X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/f3507930c130404d05f982cf9fe960fb95f7eb4b..f57edc1d5b4f497883b451fc85d2d653d27a8247:/src/surf/trace_mgr.c diff --git a/src/surf/trace_mgr.c b/src/surf/trace_mgr.c index 9d0039656b..ea18e1a9c4 100644 --- a/src/surf/trace_mgr.c +++ b/src/surf/trace_mgr.c @@ -1,88 +1,97 @@ -/* $Id$ */ - -/* Copyright (c) 2004 Arnaud Legrand. All rights reserved. */ +/* Copyright (c) 2004, 2005, 2007, 2009, 2010. The SimGrid Team. + * 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. */ #include "xbt/sysdep.h" #include "xbt/log.h" +#include "xbt/str.h" #include "xbt/dict.h" #include "trace_mgr_private.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) -{ - tmgr_trace_free(trace); -} -tmgr_history_t tmgr_history_new(void) +XBT_INLINE tmgr_history_t tmgr_history_new(void) { tmgr_history_t h; h = xbt_new0(s_tmgr_history_t, 1); - h->heap = xbt_heap_new(8, xbt_free_f); /* Why 8 ? Well, why not... */ + h->heap = xbt_heap_new(8, xbt_free_f); /* Why 8 ? Well, why not... */ return h; } -void tmgr_history_free(tmgr_history_t h) +XBT_INLINE void tmgr_history_free(tmgr_history_t h) { xbt_heap_free(h->heap); free(h); } -tmgr_trace_t tmgr_trace_new_from_string(const char* id, const char *input, double periodicity) +tmgr_trace_t tmgr_trace_new_from_string(const char *id, const char *input, + double periodicity) { tmgr_trace_t trace = NULL; int linecount = 0; 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) + if (trace) { + XBT_WARN("Ignoring redefinition of trace %s", id); return trace; + } } - if (periodicity <= 0) { - xbt_assert1(0, "Periodicity has to be positive. Your value %lg", periodicity); - } + xbt_assert(periodicity >= 0, + "Invalid periodicity %lg (must be positive)", periodicity); trace = xbt_new0(s_tmgr_trace_t, 1); trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL); - xbt_dynar_t list = xbt_str_split(input,"\n\r"); - - unsigned int cpt; - char * val; + 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; - } - } + linecount++; + xbt_str_trim(val, " \t\n\r\x0B"); + if (val[0] == '#' || val[0] == '\0' || val[0] == '%') + continue; + + if (sscanf(val, "PERIODICITY " "%lg" "\n", &periodicity) == 1) + continue; + + if (sscanf(val, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2) + xbt_die("%s:%d: Syntax error in trace\n%s", id, linecount, input); + + if (last_event) { + if (last_event->delta > event.delta) { + xbt_die("%s:%d: Invalid trace: Events must be sorted, " + "but time %lg > time %lg.\n%s", + id, linecount, last_event->delta, event.delta, input); + } + last_event->delta = event.delta - last_event->delta; + } + xbt_dynar_push(trace->event_list, &event); + last_event = + xbt_dynar_get_ptr(trace->event_list, + xbt_dynar_length(trace->event_list) - 1); } + if (last_event) + last_event->delta = periodicity; if (!trace_list) trace_list = xbt_dict_new(); - xbt_dict_set(trace_list, id, (void *) trace, _tmgr_trace_free); + xbt_dict_set(trace_list, id, (void *) trace, + (void (*)(void *)) tmgr_trace_free); xbt_dynar_free(&list); return trace; @@ -90,71 +99,29 @@ tmgr_trace_t tmgr_trace_new_from_string(const char* id, const char *input, doubl tmgr_trace_t tmgr_trace_new(const char *filename) { - tmgr_trace_t trace = NULL; + char *tstr = 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; + tmgr_trace_t trace = NULL; + + if ((!filename) || (strcmp(filename, "") == 0)) + return NULL; if (trace_list) { trace = xbt_dict_get_or_null(trace_list, filename); - if (trace) + if (trace) { + XBT_WARN("Ignoring redefinition of trace %s", filename); return trace; - } - - if ((f = surf_fopen(filename, "r")) == NULL) { - xbt_assert1(0, "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 " "%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, "%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) { - 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); } - if (periodicity > 0) { - if (last_event) - last_event->delta = periodicity; - } - - if (!trace_list) - trace_list = xbt_dict_new(); - - xbt_dict_set(trace_list, filename, (void *) trace, _tmgr_trace_free); + f = surf_fopen(filename, "r"); + xbt_assert(f != NULL, "Cannot open file '%s' (path=%s)", filename, + xbt_str_join(surf_path, ":")); + tstr = xbt_str_from_file(f); fclose(f); + trace = tmgr_trace_new_from_string(filename, tstr, 0.); + xbt_free(tstr); return trace; } @@ -162,8 +129,6 @@ tmgr_trace_t tmgr_trace_new(const char *filename) 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); @@ -176,7 +141,7 @@ tmgr_trace_t tmgr_empty_trace_new(void) return trace; } -void tmgr_trace_free(tmgr_trace_t trace) +XBT_INLINE void tmgr_trace_free(tmgr_trace_t trace) { if (!trace) return; @@ -185,9 +150,9 @@ void tmgr_trace_free(tmgr_trace_t trace) } 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_t trace, + double start_time, + unsigned int offset, void *model) { tmgr_trace_event_t trace_event = NULL; @@ -196,15 +161,15 @@ tmgr_trace_event_t tmgr_history_add_trace(tmgr_history_t h, trace_event->idx = offset; trace_event->model = model; - xbt_assert0((trace_event->idx < xbt_dynar_length(trace->event_list)), - "You're refering to an event that does not exist!"); + xbt_assert((trace_event->idx < xbt_dynar_length(trace->event_list)), + "You're referring to an event that does not exist!"); xbt_heap_push(h->heap, trace_event, start_time); return trace_event; } -double tmgr_history_next_date(tmgr_history_t h) +XBT_INLINE double tmgr_history_next_date(tmgr_history_t h) { if (xbt_heap_size(h->heap)) return (xbt_heap_maxkey(h->heap)); @@ -213,9 +178,9 @@ double tmgr_history_next_date(tmgr_history_t h) } tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t h, - double date, - double *value, - void **model) + double date, + double *value, + void **model) { double event_date = tmgr_history_next_date(h); tmgr_trace_event_t trace_event = NULL; @@ -237,18 +202,26 @@ tmgr_trace_event_t tmgr_history_get_next_event_leq(tmgr_history_t h, 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 */ + } 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; + } else { /* We don't need this trace_event anymore */ + trace_event->free_me = 1; } return trace_event; } -void tmgr_finalize(void) +XBT_INLINE void tmgr_finalize(void) { xbt_dict_free(&trace_list); } + +int tmgr_trace_event_free(tmgr_trace_event_t trace_event) +{ + if (trace_event->free_me) { + xbt_free(trace_event); + return 1; + } + return 0; +}