xl: allow scaling suffix on memory sizes in mem-set and mem-max
authorKeir Fraser <keir.fraser@citrix.com>
Tue, 18 May 2010 10:38:12 +0000 (11:38 +0100)
committerKeir Fraser <keir.fraser@citrix.com>
Tue, 18 May 2010 10:38:12 +0000 (11:38 +0100)
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.

Signed-off-by: Jeremy Fitzhardinge <jeremy.fitzhardinge@citrix.com>
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
tools/libxl/xl_cmdimpl.c
tools/libxl/xl_cmdtable.c

index afadeda1d129b051bcd01bf48b62c1783932d906..b5d2eba0227d681cc0cdee03e7a92be6a46fa3ce 100644 (file)
@@ -1200,16 +1200,45 @@ void help(char *command)
     }
 }
 
-int set_memory_max(char *p, char *mem)
+static int64_t parse_mem_size_kb(char *mem)
 {
     char *endptr;
-    uint32_t memorykb;
+    int64_t kbytes;
+
+    kbytes = strtoll(mem, &endptr, 10);
+
+    if (strlen(endptr) > 1)
+        return -1;
+
+    switch (tolower(*endptr)) {
+    case 't':
+        kbytes <<= 10;
+    case 'g':
+        kbytes <<= 10;
+    case 'm':
+        kbytes <<= 10;
+    case '\0':
+    case 'k':
+        break;
+    case 'b':
+        kbytes >>= 10;
+        break;
+    default:
+        return -1;
+    }
+
+    return kbytes;
+}
+
+int set_memory_max(char *p, char *mem)
+{
+    int64_t 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 +1284,17 @@ int main_memmax(int argc, char **argv)
 
 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') {
+    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);
 }
 
index 68b8279b92ade024badf9b4a96f80cadda07c67a..82cf2ca722809fb646939b9e4de0a264fb83a401 100644 (file)
@@ -110,12 +110,16 @@ struct cmd_spec cmd_table[] = {
     },
     { "mem-max",
       &main_memmax,
-      "Set the maximum amount reservation for a domain",
+      "Set the maximum amount reservation for a domain.\n"
+      "Units default to kilobytes, but can be suffixed with\n"
+      "'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.\n"
+      "Units default to kilobytes, but can be suffixed with\n"
+      "'b' (bytes), 'k' (KB), 'm' (MB), 'g' (GB) or 't' (TB)",
       "<Domain> <MemKB>",
     },
     { "button-press",