xl: Add command 'xl mem-max'
authorKeir Fraser <keir.fraser@citrix.com>
Wed, 12 May 2010 07:52:59 +0000 (08:52 +0100)
committerKeir Fraser <keir.fraser@citrix.com>
Wed, 12 May 2010 07:52:59 +0000 (08:52 +0100)
Add subcommand 'xl mem-max', can be used to set static max memory

Signed-off-by: Yu Zhiguo <yuzg@cn.fujitsu.com>
tools/libxl/libxl.c
tools/libxl/libxl.h
tools/libxl/xl_cmdimpl.c
tools/libxl/xl_cmdimpl.h
tools/libxl/xl_cmdtable.c

index e031582083b0cc4f76f71ed04ad5d42bd34ebb0b..176ea89af378c27df624285d16df24faf7be6ee0 100644 (file)
@@ -2346,6 +2346,39 @@ int libxl_device_pci_shutdown(struct libxl_ctx *ctx, uint32_t domid)
     return 0;
 }
 
+int libxl_domain_setmaxmem(struct libxl_ctx *ctx, uint32_t domid, uint32_t max_memkb)
+{
+    char *mem, *endptr;
+    uint32_t memorykb;
+    char *dompath = libxl_xs_get_dompath(ctx, domid);
+    int rc;
+
+    mem = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/target", dompath));
+    if (!mem) {
+        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "cannot get memory info from %s/memory/target\n", dompath);
+        return 1;
+    }
+    memorykb = strtoul(mem, &endptr, 10);
+    if (*endptr != '\0') {
+        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "invalid memory %s from %s/memory/target\n", mem, dompath);
+        return 1;
+    }
+
+    if (max_memkb < memorykb) {
+        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "memory_static_max must be greater than or or equal to memory_dynamic_max\n");
+        return 1;
+    }
+
+    rc = xc_domain_setmaxmem(ctx->xch, domid, max_memkb);
+    if (rc != 0)
+        return rc;
+
+    if (domid != 0)
+        libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/static-max", dompath), "%lu", max_memkb);
+
+    return rc;
+}
+
 int libxl_set_memory_target(struct libxl_ctx *ctx, uint32_t domid, uint32_t target_memkb)
 {
     int rc = 0;
index ffed896f454d635eb6a1f88b2f519a287c96dbde..5d300c6aca0e292dbfb67633821b0f1b0428ec89 100644 (file)
@@ -339,6 +339,7 @@ int libxl_domain_rename(struct libxl_ctx *ctx, uint32_t domid,
 int libxl_domain_pause(struct libxl_ctx *ctx, uint32_t domid);
 int libxl_domain_unpause(struct libxl_ctx *ctx, uint32_t domid);
 
+int libxl_domain_setmaxmem(struct libxl_ctx *ctx, uint32_t domid, uint32_t target_memkb);
 int libxl_set_memory_target(struct libxl_ctx *ctx, uint32_t domid, uint32_t target_memkb);
 
 int libxl_console_attach(struct libxl_ctx *ctx, uint32_t domid, int cons_num);
index ddb0bfd1323ec8e6d6826ace1be7bd1fc6c80d45..e9a98a819d8d6bd2f3c52fa7283773290b3eabac 100644 (file)
@@ -1187,6 +1187,59 @@ void help(char *command)
     }
 }
 
+int set_memory_max(char *p, char *mem)
+{
+    char *endptr;
+    uint32_t memorykb;
+    int rc;
+
+    find_domain(p);
+
+    memorykb = strtoul(mem, &endptr, 10);
+    if (*endptr != '\0') {
+        fprintf(stderr, "invalid memory size: %s\n", mem);
+        exit(3);
+    }
+
+    rc = libxl_domain_setmaxmem(&ctx, domid, memorykb);
+
+    return rc;
+}
+
+int main_memmax(int argc, char **argv)
+{
+    int opt = 0;
+    char *p = NULL, *mem;
+    int rc;
+
+    while ((opt = getopt(argc, argv, "h")) != -1) {
+        switch (opt) {
+        case 'h':
+            help("mem-max");
+            exit(0);
+        default:
+            fprintf(stderr, "option not supported\n");
+            break;
+        }
+    }
+    if (optind >= argc - 1) {
+        help("mem-max");
+        exit(2);
+    }
+
+    p = argv[optind];
+    mem = argv[optind + 1];
+
+    rc = set_memory_max(p, mem);
+    if (rc) {
+        fprintf(stderr, "cannot set domid %d static max memory to : %s\n", domid, mem);
+        exit(1);
+    }
+
+    printf("setting domid %d static max memory to : %s\n", domid, mem);
+    exit(0);
+}
+
 void set_memory_target(char *p, char *mem)
 {
     char *endptr;
index 3c7b9fd1f6b6ab3e27ba5b536746800cc11a8b4b..6f2a1fa6570560d44dce55ae20459247c066b74a 100644 (file)
@@ -33,6 +33,7 @@ int main_create(int argc, char **argv);
 int main_button_press(int argc, char **argv);
 int main_vcpupin(int argc, char **argv);
 int main_vcpuset(int argc, char **argv);
+int main_memmax(int argc, char **argv);
 int main_memset(int argc, char **argv);
 int main_sched_credit(int argc, char **argv);
 int main_domid(int argc, char **argv);
index 3609e1b93fe36b54a1c5a712855d0be22ffeb4b9..5d3cea32501facbc821934d14d9f11f9b43a15bc 100644 (file)
@@ -108,6 +108,11 @@ struct cmd_spec cmd_table[] = {
       "Eject a cdrom from a guest's cd drive",
       "<Domain> <VirtualDevice>",
     },
+    { "mem-max",
+      &main_memmax,
+      "Set the maximum amount reservation for a domain",
+      "<Domain> <MemKB>",
+    },
     { "mem-set",
       &main_memset,
       "Set the current memory usage for a domain",