Skip to contentSkip to conversation

xl: allow scaling suffix on memory sizes in mem-set and mem-max

PATCH In review Jeremy Fitzhardinge 3 participants 4 messages May 18, 2010 – May 21, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xl: allow scaling suffix on memory sizes in mem-set and mem-max

Allow mem-set and mem-max to take 'b', 'k', 'm', 'g' and 't' as scaling suffixes for bytes, kilobytes, mega, etc. An unadorned number is still treated as kilobytes so no existing users should be affected.

tools/libxl/xl_cmdimpl.c +36 -11
diff -r baccadfd9418 tools/libxl/xl_cmdimpl.c
@@ -1200,16 +1200,40 @@
     }
 }
 
+static long long int parse_mem_size_kb(char *mem)
+{
+    char *endptr;
+    long long int bytes;
+    long long int scale = 1024;
+
+    bytes = strtoll(mem, &endptr, 10);
+
+    if (strlen(endptr) > 1)
+	return -1;
+
+    switch (*endptr) {
+    case '\0':						break;
+    case 'b':	scale = 1;				break;
+    case 'k':	scale = 1024ll;				break;
+    case 'm':	scale = 1024ll * 1024;			break;
+    case 'g':	scale = 1024ll * 1024 * 1024;		break;
+    case 't':	scale = 1024ll * 1024 * 1024 * 1024;	break;
+    default:
+	return -1;
+    }
+
+    return (bytes * scale) / 1024;
+}
+
 int set_memory_max(char *p, char *mem)
 {
-    char *endptr;
-    uint32_t memorykb;
+    long long int memorykb;
     int rc;
 
     find_domain(p);
 
-    memorykb = strtoul(mem, &endptr, 10);
-    if (*endptr != '\0') {
+    memorykb = parse_mem_size_kb(mem);
+    if (memorykb == -1) {
         fprintf(stderr, "invalid memory size: %s\n", mem);
         exit(3);
     }
@@ -1255,17 +1279,18 @@
 
 void set_memory_target(char *p, char *mem)
 {
-    char *endptr;
-    uint32_t memorykb;
+    long long int memorykb;
 
     find_domain(p);
 
-    memorykb = strtoul(mem, &endptr, 10);
-    if (*endptr != '\0') {
-        fprintf(stderr, "invalid memory size: %s\n", mem);
-        exit(3);
+    memorykb = parse_mem_size_kb(mem);
+
+    if (memorykb == -1)  {
+	fprintf(stderr, "invalid memory size: %s\n", mem);
+	exit(3);
     }
-    printf("setting domid %d memory to : %d\n", domid, memorykb);
+
+    printf("setting domid %d memory to : %lld\n", domid, memorykb);
     libxl_set_memory_target(&ctx, domid, memorykb, /* enforce */ 1);
 }
 
tools/libxl/xl_cmdtable.c +2 -2
diff -r baccadfd9418 tools/libxl/xl_cmdtable.c
@@ -110,12 +110,12 @@
     },
     { "mem-max",
       &main_memmax,
-      "Set the maximum amount reservation for a domain",
+      "Set the maximum amount reservation for a domain.  Units default to kilobytes, but can be suffixed with 'b' (bytes), 'k' (KB), 'm' (MB), 'g' (GB) or 't' (TB)",
       "<Domain> <MemKB>",
     },
     { "mem-set",
       &main_memset,
-      "Set the current memory usage for a domain",
+      "Set the current memory usage for a domain.  Units default to kilobytes, but can be suffixed with 'b' (bytes), 'k' (KB), 'm' (MB), 'g' (GB) or 't' (TB)",
       "<Domain> <MemKB>",
     },
     { "button-press",
#1 lore raw

Hi jeremy,

Quoted 10 lines · Jeremy Fitzhardinge

Allow mem-set and mem-max to take 'b', 'k', 'm', 'g' and 't' as scaling suffixes for bytes, kilobytes, mega, etc. An unadorned number is still treated as kilobytes so no existing users should be affected.

diff -r baccadfd9418 tools/libxl/xl_cmdimpl.c

tools/libxl/xl_cmdimpl.c line 1203

Quoted 4 lines
tools/libxl/xl_cmdimpl.c +1 -0
@@ -1200,16 +1200,40 @@
     }
 }
 
+static long long int parse_mem_size_kb(char *mem)

I think here should use 'uint64_t' which is 'unsigned long long' instead of 'long long int'.

Quoted 93 lines
tools/libxl/xl_cmdimpl.c +35 -11
+{
+    char *endptr;
+    long long int bytes;
+    long long int scale = 1024;
+
+    bytes = strtoll(mem, &endptr, 10);
+
+    if (strlen(endptr) > 1)
+	return -1;
+
+    switch (*endptr) {
+    case '\0':						break;
+    case 'b':	scale = 1;				break;
+    case 'k':	scale = 1024ll;				break;
+    case 'm':	scale = 1024ll * 1024;			break;
+    case 'g':	scale = 1024ll * 1024 * 1024;		break;
+    case 't':	scale = 1024ll * 1024 * 1024 * 1024;	break;
+    default:
+	return -1;
+    }
+
+    return (bytes * scale) / 1024;
+}
+
 int set_memory_max(char *p, char *mem)
 {
-    char *endptr;
-    uint32_t memorykb;
+    long long int memorykb;
     int rc;
 
     find_domain(p);
 
-    memorykb = strtoul(mem, &endptr, 10);
-    if (*endptr != '\0') {
+    memorykb = parse_mem_size_kb(mem);
+    if (memorykb == -1) {
         fprintf(stderr, "invalid memory size: %s\n", mem);
         exit(3);
     }
@@ -1255,17 +1279,18 @@
 
 void set_memory_target(char *p, char *mem)
 {
-    char *endptr;
-    uint32_t memorykb;
+    long long int memorykb;
 
     find_domain(p);
 
-    memorykb = strtoul(mem, &endptr, 10);
-    if (*endptr != '\0') {
-        fprintf(stderr, "invalid memory size: %s\n", mem);
-        exit(3);
+    memorykb = parse_mem_size_kb(mem);
+
+    if (memorykb == -1)  {
+	fprintf(stderr, "invalid memory size: %s\n", mem);
+	exit(3);
     }
-    printf("setting domid %d memory to : %d\n", domid, memorykb);
+
+    printf("setting domid %d memory to : %lld\n", domid, memorykb);
     libxl_set_memory_target(&ctx, domid, memorykb, /* enforce */ 1);
 }
 

diff -r baccadfd9418 tools/libxl/xl_cmdtable.c

tools/libxl/xl_cmdtable.c +2 -2
@@ -110,12 +110,12 @@
     },
     { "mem-max",
       &main_memmax,
-      "Set the maximum amount reservation for a domain",
+      "Set the maximum amount reservation for a domain.  Units default to kilobytes, but can be suffixed with 'b' (bytes), 'k' (KB), 'm' (MB), 'g' (GB) or 't' (TB)",
       "<Domain> <MemKB>",
     },
     { "mem-set",
       &main_memset,
-      "Set the current memory usage for a domain",
+      "Set the current memory usage for a domain.  Units default to kilobytes, but can be suffixed with 'b' (bytes), 'k' (KB), 'm' (MB), 'g' (GB) or 't' (TB)",
       "<Domain> <MemKB>",
     },
     { "button-press",

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

Signature · 2 lines
Regards
Yang Hongyang
#2 lore raw
Quoted 17 lines · Yang Hongyang

Hi jeremy,

Quoted 13 lines · Jeremy Fitzhardinge

Allow mem-set and mem-max to take 'b', 'k', 'm', 'g' and 't' as scaling suffixes for bytes, kilobytes, mega, etc. An unadorned number is still treated as kilobytes so no existing users should be affected.

diff -r baccadfd9418 tools/libxl/xl_cmdimpl.c

tools/libxl/xl_cmdimpl.c +0 -0
@@ -1200,16 +1200,40 @@
     }
 }
 
Quoted 5 lines

tools/libxl/xl_cmdimpl.c line 1232

Quoted 2 lines
tools/libxl/xl_cmdimpl.c +1 -0
+static long long int parse_mem_size_kb(char *mem)
 

I think here should use 'uint64_t' which is 'unsigned long long' instead of 'long long int'.

I want it to be signed so I can use -1 as an error indicator. And I want it to be "long long int" rather than int64_t so that the format specifier for printf is unambiguous (since int64_t can be just "long" on a 64-bit machine).

J

Quoted 97 lines
Quoted 95 lines
tools/libxl/xl_cmdimpl.c +27 -4
+{
+    char *endptr;
+    long long int bytes;
+    long long int scale = 1024;
+
+    bytes = strtoll(mem, &endptr, 10);
+
+    if (strlen(endptr) > 1)
+	return -1;
+
+    switch (*endptr) {
+    case '\0':						break;
+    case 'b':	scale = 1;				break;
+    case 'k':	scale = 1024ll;				break;
+    case 'm':	scale = 1024ll * 1024;			break;
+    case 'g':	scale = 1024ll * 1024 * 1024;		break;
+    case 't':	scale = 1024ll * 1024 * 1024 * 1024;	break;
+    default:
+	return -1;
+    }
+
+    return (bytes * scale) / 1024;
+}
+
 int set_memory_max(char *p, char *mem)
 {
-    char *endptr;
-    uint32_t memorykb;
+    long long int memorykb;
     int rc;
 
     find_domain(p);
 
-    memorykb = strtoul(mem, &endptr, 10);
-    if (*endptr != '\0') {
+    memorykb = parse_mem_size_kb(mem);
+    if (memorykb == -1) {
         fprintf(stderr, "invalid memory size: %s\n", mem);
         exit(3);
     }
tools/libxl/xl_cmdimpl.c +8 -7
@@ -1255,17 +1279,18 @@
 
 void set_memory_target(char *p, char *mem)
 {
-    char *endptr;
-    uint32_t memorykb;
+    long long int memorykb;
 
     find_domain(p);
 
-    memorykb = strtoul(mem, &endptr, 10);
-    if (*endptr != '\0') {
-        fprintf(stderr, "invalid memory size: %s\n", mem);
-        exit(3);
+    memorykb = parse_mem_size_kb(mem);
+
+    if (memorykb == -1)  {
+	fprintf(stderr, "invalid memory size: %s\n", mem);
+	exit(3);
     }
-    printf("setting domid %d memory to : %d\n", domid, memorykb);
+
+    printf("setting domid %d memory to : %lld\n", domid, memorykb);
     libxl_set_memory_target(&ctx, domid, memorykb, /* enforce */ 1);
 }
 

diff -r baccadfd9418 tools/libxl/xl_cmdtable.c

tools/libxl/xl_cmdtable.c +2 -2
@@ -110,12 +110,12 @@
     },
     { "mem-max",
       &main_memmax,
-      "Set the maximum amount reservation for a domain",
+      "Set the maximum amount reservation for a domain.  Units default to kilobytes, but can be suffixed with 'b' (bytes), 'k' (KB), 'm' (MB), 'g' (GB) or 't' (TB)",
       "<Domain> <MemKB>",
     },
     { "mem-set",
       &main_memset,
-      "Set the current memory usage for a domain",
+      "Set the current memory usage for a domain.  Units default to kilobytes, but can be suffixed with 'b' (bytes), 'k' (KB), 'm' (MB), 'g' (GB) or 't' (TB)",
       "<Domain> <MemKB>",
     },
     { "button-press",

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#3 lore raw

Jeremy Fitzhardinge writes ("Re: [Xen-devel] [PATCH] xl: allow scaling suffix on memory sizes in mem-set and mem-max"):

Quoted 4 lines

I want it to be signed so I can use -1 as an error indicator. And I want it to be "long long int" rather than int64_t so that the format specifier for printf is unambiguous (since int64_t can be just "long" on a 64-bit machine).

You can use int64_t and as a format write "%"PRIi64

Ian.