Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Surf Parser
authoralegrand <alegrand@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Thu, 2 Dec 2004 21:03:35 +0000 (21:03 +0000)
committeralegrand <alegrand@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Thu, 2 Dec 2004 21:03:35 +0000 (21:03 +0000)
git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@536 48e7efb5-ca39-0410-a469-dd3cf9ba447f

src/Makefile.am
src/include/surf/surf_parse.h [new file with mode: 0644]
src/surf/cpu.c
src/surf/surf_parse.l [new file with mode: 0644]
src/surf/surf_private.h

index d8c507b..287db3a 100644 (file)
@@ -25,6 +25,7 @@ EXTRA_DIST= \
        surf/trace_mgr_private.h \
        surf/surf_private.h \
        surf/cpu_private.h \
+       surf/surf_parse.l surf/surf_parse.h \
        \
        gras/Transport/transport_interface.h \
        gras/Virtu/virtu_interface.h \
@@ -97,6 +98,7 @@ COMMON_S=\
   surf/maxmin.c                                                              \
   surf/trace_mgr.c                                                           \
   surf/surf.c                                                                \
+  surf/surf_parse.c                                                          \
   surf/cpu.c                                                                 \
   \
   gras/Transport/transport.c          gras/Transport/transport_private.h   gras/Transport/transport_plugin_buf.c  \
@@ -114,6 +116,9 @@ COMMON_S=\
 gras/DataDesc/ddt_parse.yy.c: gras/DataDesc/ddt_parse.yy.l
        @LEX@ -o$@ -Pgras_ddt_parse_ $^
 
+surf/surf_parse.c: surf/surf_parse.l
+       @LEX@ -o$@ -Psurf_parse_ $^
+
 libgrasrl_la_SOURCES= $(COMMON_S) \
   gras/Transport/rl_transport.c  gras/Transport/transport_plugin_tcp.c  gras/Transport/transport_plugin_file.c      \
   \
diff --git a/src/include/surf/surf_parse.h b/src/include/surf/surf_parse.h
new file mode 100644 (file)
index 0000000..547ab6a
--- /dev/null
@@ -0,0 +1,32 @@
+/* Authors: Arnaud Legrand                                                  */
+
+/* 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. */
+
+#ifndef _SURF_SURF_PARSE_H
+#define _SURF_SURF_PARSE_H
+
+typedef enum {
+  TOKEN_EMPTY = 0,
+  TOKEN_LP = 512,
+  TOKEN_RP,
+  TOKEN_BEGIN_SECTION,
+  TOKEN_END_SECTION,
+  TOKEN_CLOSURE,
+  TOKEN_WORD,
+  TOKEN_NEWLINE,
+  TOKEN_ERROR
+} e_surf_token_t;
+
+#define MAX_STR_CONST 1024
+
+extern char *surf_parse_text;
+extern int line_pos;
+extern int char_pos;
+extern int tok_num;
+
+e_surf_token_t surf_parse(void);
+void  surf_parse_open(const char *file);
+void  surf_parse_close(void);
+
+#endif
index abba5cd..3e34704 100644 (file)
@@ -54,14 +54,112 @@ static void *cpu_new(const char *name, xbt_maxmin_float_t power_scale,
   return cpu;
 }
 
+static void find_section(const char* file, const char* section_name)
+{
+  e_surf_token_t token;
+  int found = 0;
+
+  surf_parse_open(file);
+
+  while((token=surf_parse())) {
+    if(token!=TOKEN_BEGIN_SECTION) continue;
+
+    token=surf_parse();
+    xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
+    if(strcmp(surf_parse_text,section_name)==0) found=1;
+
+    token=surf_parse();
+    xbt_assert1((token==TOKEN_CLOSURE),"Parse error line %d",line_pos);
+
+    if(found) return;
+  }
+
+  CRITICAL2("Could not find %s section in %s\n",section_name,file);
+}
+
+static void close_section(const char* section_name)
+{
+  e_surf_token_t token;
+
+  token=surf_parse();
+  xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
+  xbt_assert1((strcmp(surf_parse_text,"CPU")==0), 
+             "Closing section does not match the opening one (%s).", 
+             section_name);
+  
+  token=surf_parse();
+  xbt_assert1((token==TOKEN_CLOSURE),"Parse error line %d",line_pos);
+
+  surf_parse_close();
+}
+
+/*  
+   Semantic:  name       scale     initial     power     initial     state
+                                    power      trace      state      trace
+   
+   Token:   TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD
+   Type:     string      float      float      string     ON/OFF     string
+*/
+
+static void parse_host(void)
+{
+  e_surf_token_t token;
+  char *name = NULL; 
+  xbt_maxmin_float_t power_scale = 0.0;
+  xbt_maxmin_float_t initial_power = 0.0;
+  tmgr_trace_t power_trace = NULL;;
+  e_surf_cpu_state_t initial_state = SURF_CPU_OFF;
+  tmgr_trace_t state_trace = NULL;
+
+  name=xbt_strdup(surf_parse_text);
+
+  token=surf_parse(); /* power_scale */
+  xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
+  xbt_assert2((sscanf(surf_parse_text,XBT_MAXMIN_FLOAT_T, &power_scale)==1),
+    "Parse error line %d : %s not a number",line_pos,surf_parse_text);
+
+  token=surf_parse(); /* initial_power */
+  xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
+  xbt_assert2((sscanf(surf_parse_text,XBT_MAXMIN_FLOAT_T, &initial_power)==1),
+    "Parse error line %d : %s not a number",line_pos,surf_parse_text);
+
+  token=surf_parse(); /* power_trace */
+  xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
+  if(strcmp(surf_parse_text,"")==0) power_trace = NULL;
+  else power_trace = tmgr_trace_new(surf_parse_text);
+
+  token=surf_parse(); /* initial_state */
+  xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
+  if(strcmp(surf_parse_text,"ON")==0) initial_state = SURF_CPU_ON;
+  else if(strcmp(surf_parse_text,"OFF")==0) initial_state = SURF_CPU_OFF;
+  else CRITICAL2("Invalid cpu state (line %d): %s neq ON or OFF\n",line_pos, 
+                surf_parse_text);
+
+  token=surf_parse(); /* state_trace */
+  xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
+  if(strcmp(surf_parse_text,"")==0) state_trace = NULL;
+  else state_trace = tmgr_trace_new(surf_parse_text);
+
+  cpu_new(name, power_scale, initial_power, power_trace, initial_state, state_trace);
+}
+
 static void parse_file(const char *file)
 {
-  tmgr_trace_t trace_A = tmgr_trace_new("trace_A.txt");
-  tmgr_trace_t trace_B = tmgr_trace_new("trace_B.txt");
-  tmgr_trace_t trace_A_failure = tmgr_trace_new("trace_A_failure.txt");
+  e_surf_token_t token;
+
+  find_section(file,"CPU");
+
+  while(1) {
+    token=surf_parse();
+
+    if(token==TOKEN_END_SECTION) break;
+    if(token==TOKEN_NEWLINE) continue;
+    
+    if(token==TOKEN_WORD) parse_host();
+    else CRITICAL1("Parse error line %d\n",line_pos);
+  }
 
-  cpu_new("Cpu A", 100.0, 1.0, trace_A, SURF_CPU_ON, trace_A_failure);
-  cpu_new("Cpu B", 100.0, 1.0, trace_B, SURF_CPU_ON, NULL);
+  close_section("CPU");  
 }
 
 static void *name_service(const char *name)
diff --git a/src/surf/surf_parse.l b/src/surf/surf_parse.l
new file mode 100644 (file)
index 0000000..0d10054
--- /dev/null
@@ -0,0 +1,165 @@
+/* Authors: Arnaud Legrand                                                  */
+
+/* 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. */
+
+%option noyywrap
+%{
+#include "xbt/sysdep.h"
+#include"surf/surf_parse.h"
+#include "xbt/log.h"
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(parse, surf ,"Logging specific to the SURF  module");
+
+  YY_BUFFER_STATE input_buffer;
+  FILE *file_to_parse;
+
+  int line_pos = 1;
+  int char_pos = 0;
+  int tok_num = 0;
+%}
+
+%x comment foo str
+space           [ \t]
+letter          [A-Za-z._-]
+digit           [0-9]
+
+%%
+        int comment_caller=0;
+
+        char string_buf[MAX_STR_CONST];
+        char *string_buf_ptr = NULL;
+
+"//"[^\n]*
+"/*"         {
+             comment_caller = INITIAL;
+             BEGIN(comment);
+             }
+
+<foo>"/*"    {
+             comment_caller = foo;
+             BEGIN(comment);
+             }
+
+<comment>[^*\n]*        /* eat anything that's not a '*' */
+<comment>"*"+[^*/\n]*   /* eat up '*'s not followed by '/'s */
+<comment>\n             {++line_pos;char_pos=0;}
+<comment>"*"+"/"        BEGIN(comment_caller);
+
+\"      string_buf_ptr = string_buf; char_pos++; BEGIN(str);
+
+<str>\"        { /* saw closing quote - all done */
+        BEGIN(INITIAL);
+        *string_buf_ptr = '\0';
+       surf_parse_text=string_buf;
+       char_pos++;
+       return TOKEN_WORD;
+        /* return string constant token type and
+         * value to parser
+         */
+        }
+
+<str>\n        {
+        /* error - unterminated string constant */
+        /* generate error message */
+        }
+
+<str>\\[0-7]{1,3} {
+        /* octal escape sequence */
+        int result;
+
+        (void) sscanf( surf_parse_text + 1, "%o", &result );
+
+        if ( result > 0xff )
+                /* error, constant is out-of-bounds */
+
+        *string_buf_ptr++ = result;
+       char_pos++;
+        }
+
+<str>\\[0-9]+ {
+        /* generate error - bad escape sequence; something
+         * like '\48' or '\0777777'
+         */
+        }
+
+<str>\\n  {*string_buf_ptr++ = '\n';   char_pos++;}
+<str>\\t  {*string_buf_ptr++ = '\t';   char_pos++;}
+<str>\\r  {*string_buf_ptr++ = '\r';   char_pos++;}
+<str>\\b  {*string_buf_ptr++ = '\b';   char_pos++;}
+<str>\\f  {*string_buf_ptr++ = '\f';   char_pos++;}
+
+<str>\\(.|\n)  {*string_buf_ptr++ = surf_parse_text[1];        
+                if(surf_parse_text[1]=='\n') {
+                 ++line_pos;char_pos=0;
+               } else { char_pos++;}
+               }
+
+<str>[^\\\n\"]+        {
+        char *yptr = surf_parse_text;
+
+        while ( *yptr )
+         *string_buf_ptr++ = *yptr++;
+          char_pos++;
+        }
+
+({letter}|{digit})*     { char_pos+= strlen(surf_parse_text); return(TOKEN_WORD);}
+"("                     { char_pos++; return(TOKEN_LP);}
+")"                     { char_pos++;return(TOKEN_RP);}
+"</"                    { char_pos+=2; return(TOKEN_END_SECTION);}
+"<"                     { char_pos++; return(TOKEN_BEGIN_SECTION);}
+">"                     { char_pos++;return(TOKEN_CLOSURE);}
+"\n"                    { line_pos++; char_pos=0; return(TOKEN_NEWLINE);}
+. { char_pos++;}
+%%
+/* {space}+                { return(TOKEN_SPACE);} */
+
+static void __print_val(void) {
+  switch(tok_num) {
+  case TOKEN_LP      : {printf("TOKEN_LP ");break;}
+  case TOKEN_RP      : {printf("TOKEN_RP ");break;}
+  case TOKEN_BEGIN_SECTION : {printf("TOKEN_BEGIN_SECTION ");break;}
+  case TOKEN_END_SECTION : {printf("TOKEN_END_SECTION ");break;}
+  case TOKEN_CLOSURE : {printf("TOKEN_CLOSURE ");break;}
+  case TOKEN_WORD    : {printf("TOKEN_WORD ");break;}
+  case TOKEN_NEWLINE : {printf("TOKEN_NEWLINE\n");return;}
+  case TOKEN_EMPTY : {printf("TOKEN_EMPTY\n");return;}
+  default             : {printf("Unknown token %d\n", tok_num);return;}
+  }
+/*   if(strcmp(surf_parse_text,"")!=0) */
+    printf("-->%s<-- [line %d, pos %d]\n",surf_parse_text,line_pos,char_pos);
+/*   else */
+/*     printf("--><-- [line %d, pos %d]\n",line_pos,char_pos); */
+
+  return;
+}
+
+e_surf_token_t surf_parse(void) {
+  tok_num = surf_parse_lex();
+  __print_val();
+  char_pos += strlen(surf_parse_text);
+  return(tok_num);
+}
+
+void  surf_parse_open(const char *file) {
+  file_to_parse = fopen(file,"r");
+  xbt_assert1((file_to_parse), "Unable to open \"%s\"\n",file)
+  input_buffer = surf_parse__create_buffer( file_to_parse, 10 );
+  surf_parse__switch_to_buffer(input_buffer);
+
+  line_pos = 1;
+  char_pos = 0;
+  tok_num = 0;
+}
+
+void  surf_parse_close(void) {
+  surf_parse__delete_buffer(input_buffer);
+  fclose(file_to_parse);
+
+  line_pos = 1;
+  char_pos = 0;
+  tok_num = 0;
+}
+
+/*  Local variables: */
+/*  mode: c */
+/*  End: */
index 590afa8..0c28c7b 100644 (file)
@@ -10,6 +10,7 @@
 #include "surf/maxmin.h"
 #include "surf/trace_mgr.h"
 #include "xbt/log.h"
+#include "surf/surf_parse.h"
 
 typedef struct surf_resource_private {
   /* Share the resources to the actions and return in hom much time