Skip to contentSkip to conversation

libxl: support for using 'MmGg' as memory size suffix

RFC PATCH In review Wei Liu 3 participants 5 messages Sep 21, 2013 – Oct 21, 2013

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

RFC PATCH 1/1 libxl: support for using 'MmGg' as memory size suffix

Modify lexer to special-case memory size string. Introduce xlu_cfg_get_memsize to parse memroy size strings.

Cc Ian Jackson <[email protected]>
Cc Ian Campbell <[email protected]>
---
 tools/libxl/libxlu_cfg.c   |   46 ++++++++++++
 tools/libxl/libxlu_cfg_l.c |  176 ++++++++++++++++++++++++--------------------
 tools/libxl/libxlu_cfg_l.h |    8 +-
 tools/libxl/libxlu_cfg_l.l |    4 +
 tools/libxl/libxlutil.h    |    2 +
 tools/libxl/xl_cmdimpl.c   |    8 +-
 6 files changed, 158 insertions(+), 86 deletions(-)
tools/libxl/libxlu_cfg.c +46 -0
index 22adcb0..4f53418 100644
@@ -237,6 +237,52 @@ int xlu_cfg_get_long(const XLU_Config *cfg, const char *n,
     return 0;
 }
 
+int xlu_cfg_get_memsize(const XLU_Config *cfg, const char *n,
+                        long *value_r, int dont_warn) {
+    long l;
+    XLU_ConfigSetting *set;
+    int e;
+    char *ep;
+
+    e= find_atom(cfg,n,&set,dont_warn);  if (e) return e;
+    errno= 0; l= strtol(set->values[0], &ep, 0);
+    e= errno;
+    if (errno) {
+        e= errno;
+        assert(e==EINVAL || e==ERANGE);
+        if (!dont_warn)
+            fprintf(cfg->report,
+                    "%s:%d: warning: parameter `%s' could not be parsed"
+                    " as memory size: %s\n",
+                    cfg->config_source, set->lineno, n, strerror(e));
+        return e;
+    }
+    if ((*ep!='M' && *ep!='m' && *ep!='G' && *ep!='g') ||
+        ep==set->values[0]) {
+        if (!dont_warn)
+            fprintf(cfg->report,
+                    "%s:%d: warning: parameter `%s' is not a valid"
+                    " memory size *ep=%c\n",
+                    cfg->config_source, set->lineno, n, *ep);
+        return EINVAL;
+    }
+
+    /* Nothing to do if the suffix is [Mm]. */
+
+    if (*ep=='G' || *ep=='g') {
+        if (l >= INT_MAX / 1024 && !dont_warn) {
+            fprintf(cfg->report,
+                    "%s:%d: warning: parameter `%s' overflows\n",
+                    cfg->config_source, set->lineno, n);
+            return ERANGE;
+        } else
+            l*= 1024;
+    }
+
+    *value_r= l;
+    return 0;
+}
+
 int xlu_cfg_get_defbool(const XLU_Config *cfg, const char *n, libxl_defbool *b,
                      int dont_warn)
 {
tools/libxl/libxlu_cfg_l.c +96 -80

337 lines not on this page · raw message

tools/libxl/libxlu_cfg_l.h +6 -2
index 4078302..c104172 100644
@@ -276,6 +276,10 @@ int xlu__cfg_yyget_lineno (yyscan_t yyscanner );
 
 void xlu__cfg_yyset_lineno (int line_number ,yyscan_t yyscanner );
 
+int xlu__cfg_yyget_column  (yyscan_t yyscanner );
+
+void xlu__cfg_yyset_column (int column_no ,yyscan_t yyscanner );
+
 YYSTYPE * xlu__cfg_yyget_lval (yyscan_t yyscanner );
 
 void xlu__cfg_yyset_lval (YYSTYPE * yylval_param ,yyscan_t yyscanner );
@@ -350,8 +354,8 @@ extern int xlu__cfg_yylex \
 #undef YY_DECL
 #endif
 
-#line 104 "libxlu_cfg_l.l"
+#line 108 "libxlu_cfg_l.l"
 
-#line 356 "libxlu_cfg_l.h"
+#line 360 "libxlu_cfg_l.h"
 #undef xlu__cfg_yyIN_HEADER
 #endif /* xlu__cfg_yyHEADER_H */
tools/libxl/libxlu_cfg_l.l +4 -0
index e0ea8cf..0b1f299 100644
@@ -60,6 +60,10 @@ void xlu__cfg_yyset_column(int  column_no, yyscan_t yyscanner);
                           yylval->string= xlu__cfgl_strdup(ctx,yytext);
                           GOT(NUMBER);
                         }
+[0-9][0-9a-fx]*[MmGg]   {
+                          yylval->string= xlu__cfgl_strdup(ctx,yytext);
+                          GOT(STRING);
+                        }
 
 [ \t]
 
tools/libxl/libxlutil.h +2 -0
index 0333e55..7531016 100644
@@ -54,6 +54,8 @@ int xlu_cfg_get_long(const XLU_Config*, const char *n, long *value_r,
                      int dont_warn);
 int xlu_cfg_get_defbool(const XLU_Config*, const char *n, libxl_defbool *b,
                      int dont_warn);
+int xlu_cfg_get_memsize(const XLU_Config*, const char *n, long *value_r,
+                        int dont_warn);
 
 int xlu_cfg_get_list(const XLU_Config*, const char *n,
                      XLU_ConfigList **list_r /* may be 0 */,
tools/libxl/xl_cmdimpl.c +4 -4
index 3d7eaad..4e53ac5 100644
@@ -726,12 +726,12 @@ static void parse_config_data(const char *config_source,
         libxl_defbool_set(&b_info->numa_placement, false);
     }
 
-    if (!xlu_cfg_get_long (config, "memory", &l, 0)) {
+    if (!xlu_cfg_get_memsize (config, "memory", &l, 0)) {
         b_info->max_memkb = l * 1024;
         b_info->target_memkb = b_info->max_memkb;
     }
 
-    if (!xlu_cfg_get_long (config, "maxmem", &l, 0))
+    if (!xlu_cfg_get_memsize (config, "maxmem", &l, 0))
         b_info->max_memkb = l * 1024;
 
     libxl_defbool_set(&b_info->claim_mode, claim_mode);
@@ -768,7 +768,7 @@ static void parse_config_data(const char *config_source,
     /* libxl_get_required_shadow_memory() must be called after final values
      * (default or specified) for vcpus and memory are set, because the
      * calculation depends on those values. */
-    b_info->shadow_memkb = !xlu_cfg_get_long(config, "shadow_memory", &l, 0)
+    b_info->shadow_memkb = !xlu_cfg_get_memsize(config, "shadow_memory", &l, 0)
         ? l * 1024
         : libxl_get_required_shadow_memory(b_info->max_memkb,
                                            b_info->max_vcpus);
@@ -810,7 +810,7 @@ static void parse_config_data(const char *config_source,
 
     xlu_cfg_get_defbool(config, "localtime", &b_info->localtime, 0);
 
-    if (!xlu_cfg_get_long (config, "videoram", &l, 0))
+    if (!xlu_cfg_get_memsize (config, "videoram", &l, 0))
         b_info->video_memkb = l * 1024;
 
     switch(b_info->type) {
Signature · 1 line
1.7.10.4
#1 lore raw

tools/libxl/libxlu_cfg_l.l

Quoted 4 lines · Wei Liu
tools/libxl/libxlu_cfg_l.l +0 -0
index e0ea8cf..0b1f299 100644

This will need Ian J's review when he gets back.

tools/libxl/libxlu_cfg_l.l void xlu__cfg_yyset_column(int column_no, yyscan_t yyscanner); · line 63

Quoted 5 lines
tools/libxl/libxlu_cfg_l.l +1 -0
@@ -60,6 +60,10 @@ void xlu__cfg_yyset_column(int  column_no, yyscan_t yyscanner);
                           yylval->string= xlu__cfgl_strdup(ctx,yytext);
                           GOT(NUMBER);
                         }
+[0-9][0-9a-fx]*[MmGg]   {

You copied this from above but "[0-9][0-9a-fx]*" looks odd to me. I think it is intended to accept decimals and hex 0xNNN but it actually ends up accepting 1111xxxxx99999ddddd and nonsensical things like that, doesn't it?

I suppose we will reject such things later.

tools/libxl/libxlu_cfg_l.l void xlu__cfg_yyset_column(int column_no, yyscan_t yyscanner); · line 65

Quoted 2 lines
tools/libxl/libxlu_cfg_l.l +2 -0
+                          yylval->string= xlu__cfgl_strdup(ctx,yytext);
+                          GOT(STRING);

This sort of subtle semantic change GOT(NUMBER)=>(STRING) is what needs Ian's thoughts...

author
#2 lore raw
Quoted 8 lines · Ian Campbell

tools/libxl/libxlu_cfg_l.l

Quoted 4 lines · Wei Liu
tools/libxl/libxlu_cfg_l.l +0 -0
index e0ea8cf..0b1f299 100644

This will need Ian J's review when he gets back.

Quoted 11 lines

tools/libxl/libxlu_cfg_l.l void xlu__cfg_yyset_column(int column_no, yyscan_t yyscanner); · line 63

Quoted 5 lines
tools/libxl/libxlu_cfg_l.l +1 -0
@@ -60,6 +60,10 @@ void xlu__cfg_yyset_column(int  column_no, yyscan_t yyscanner);
                           yylval->string= xlu__cfgl_strdup(ctx,yytext);
                           GOT(NUMBER);
                         }
+[0-9][0-9a-fx]*[MmGg]   {

You copied this from above but "[0-9][0-9a-fx]*" looks odd to me. I think it is intended to accept decimals and hex 0xNNN but it actually ends up accepting 1111xxxxx99999ddddd and nonsensical things like that, doesn't it?

I had the same impression. However strtol() accepts up to base 36 which makes 1111xxxxx99999dddd sensible...

Quoted 8 lines

I suppose we will reject such things later.

tools/libxl/libxlu_cfg_l.l void xlu__cfg_yyset_column(int column_no, yyscan_t yyscanner); · line 65

Quoted 2 lines
tools/libxl/libxlu_cfg_l.l +2 -0
+                          yylval->string= xlu__cfgl_strdup(ctx,yytext);
+                          GOT(STRING);

This sort of subtle semantic change GOT(NUMBER)=>(STRING) is what needs Ian's thoughts...

Right. I haven't figure out what's the best way to handle this.

Wei.

#3 lore raw
In reply to Wei Liu

Wei Liu writes ("[PATCH RFC] libxl: support for using 'MmGg' as memory size suffix"):

Quoted 2 lines

Modify lexer to special-case memory size string. Introduce xlu_cfg_get_memsize to parse memroy size strings.

Urgh, this is really quite an ugly special case.

Perhaps a better approach might be to just permit unquoted strings when the string is vaguely like a token, and return the token type STRING from the lexer in that case.

Quoted 1 line

+ if ((*ep!='M' && *ep!='m' && *ep!='G' && *ep!='g') ||

...

Quoted 1 line

+ if (*ep=='G' || *ep=='g') {

Surely this should be done with switch().

Ian.

author
#4 lore raw
Quoted 8 lines · Ian Jackson

Wei Liu writes ("[PATCH RFC] libxl: support for using 'MmGg' as memory size suffix"):

Quoted 2 lines

Modify lexer to special-case memory size string. Introduce xlu_cfg_get_memsize to parse memroy size strings.

Urgh, this is really quite an ugly special case.

Perhaps a better approach might be to just permit unquoted strings when the string is vaguely like a token, and return the token type

"vaguely" is too vague I'm afraid...

Wei.