debuggers.hg

changeset 20647:2c6a04fdf8fb

libxenlight: implement libxl_set_memory_target

This patch adds a target_memkb parameter to libxl_domain_build_info to
set the target memory for the VM at build time and a new function
called libxl_set_memory_target to dynamically modify the memory target
of a VM at run time. Finally a new command "mem-set" is added to xl
that calls directly libxl_set_memory_target.

Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Dec 09 10:44:11 2009 +0000 (2009-12-09)
parents 4a2fabce4508
children 18342df0f9dc
files tools/libxl/libxl.c tools/libxl/libxl.h tools/libxl/libxl_dom.c tools/libxl/libxl_internal.h tools/libxl/xl.c
line diff
     1.1 --- a/tools/libxl/libxl.c	Wed Dec 09 10:43:33 2009 +0000
     1.2 +++ b/tools/libxl/libxl.c	Wed Dec 09 10:44:11 2009 +0000
     1.3 @@ -935,6 +935,7 @@ static int libxl_create_stubdom(struct l
     1.4      memset(&b_info, 0x00, sizeof(libxl_domain_build_info));
     1.5      b_info.max_vcpus = 1;
     1.6      b_info.max_memkb = 32 * 1024;
     1.7 +    b_info.target_memkb = b_info.max_memkb;
     1.8      b_info.kernel = "/usr/lib/xen/boot/ioemu-stubdom.gz";
     1.9      b_info.u.pv.cmdline = libxl_sprintf(ctx, " -d %d", info->domid);
    1.10      b_info.u.pv.ramdisk = "";
    1.11 @@ -2264,6 +2265,7 @@ void init_build_info(libxl_domain_build_
    1.12      b_info->vpt_align = -1;
    1.13      b_info->max_vcpus = 1;
    1.14      b_info->max_memkb = 32 * 1024;
    1.15 +    b_info->target_memkb = b_info->max_memkb;
    1.16      if (c_info->hvm) {
    1.17          b_info->shadow_memkb = libxl_get_required_shadow_memory(b_info->max_memkb, b_info->max_vcpus);
    1.18          b_info->video_memkb = 8 * 1024;
    1.19 @@ -2356,4 +2358,22 @@ void init_console_info(libxl_device_cons
    1.20          console->build_state = state;
    1.21  }
    1.22  
    1.23 +int libxl_set_memory_target(struct libxl_ctx *ctx, uint32_t domid, uint32_t target_memkb)
    1.24 +{
    1.25 +    int rc = 0;
    1.26 +    uint32_t videoram;
    1.27 +    char *videoram_s = NULL;
    1.28 +    char *dompath = libxl_xs_get_dompath(ctx, domid);
    1.29  
    1.30 +    videoram_s = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/videoram", dompath));
    1.31 +    if (!videoram_s)
    1.32 +        return -1;
    1.33 +    videoram = atoi(videoram_s);
    1.34 +
    1.35 +    libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/target", dompath), "%lu", target_memkb);
    1.36 +    if ((rc = xc_domain_setmaxmem(ctx->xch, domid, target_memkb + LIBXL_MAXMEM_CONSTANT)) != 0) return rc;
    1.37 +    rc = xc_domain_memory_set_pod_target(ctx->xch, domid, (target_memkb - videoram) / 4, NULL, NULL, NULL);
    1.38 +    return rc;
    1.39 +}
    1.40 +
    1.41 +
     2.1 --- a/tools/libxl/libxl.h	Wed Dec 09 10:43:33 2009 +0000
     2.2 +++ b/tools/libxl/libxl.h	Wed Dec 09 10:44:11 2009 +0000
     2.3 @@ -66,6 +66,7 @@ typedef struct {
     2.4      int max_vcpus;
     2.5      int cur_vcpus;
     2.6      uint32_t max_memkb;
     2.7 +    uint32_t target_memkb;
     2.8      uint32_t video_memkb;
     2.9      uint32_t shadow_memkb;
    2.10      const char *kernel;
    2.11 @@ -305,6 +306,8 @@ int libxl_event_get_disk_eject_info(stru
    2.12  int libxl_domain_pause(struct libxl_ctx *ctx, uint32_t domid);
    2.13  int libxl_domain_unpause(struct libxl_ctx *ctx, uint32_t domid);
    2.14  
    2.15 +int libxl_set_memory_target(struct libxl_ctx *ctx, uint32_t domid, uint32_t target_memkb);
    2.16 +
    2.17  int libxl_console_attach(struct libxl_ctx *ctx, uint32_t domid, int cons_num);
    2.18  
    2.19  struct libxl_dominfo * libxl_domain_list(struct libxl_ctx *ctx, int *nb_domain);
     3.1 --- a/tools/libxl/libxl_dom.c	Wed Dec 09 10:43:33 2009 +0000
     3.2 +++ b/tools/libxl/libxl_dom.c	Wed Dec 09 10:44:11 2009 +0000
     3.3 @@ -52,7 +52,7 @@ int build_pre(struct libxl_ctx *ctx, uin
     3.4      if (info->vpt_align != -1)
     3.5          xc_set_hvm_param(ctx->xch, domid, HVM_PARAM_VPT_ALIGN, (unsigned long) info->vpt_align);
     3.6      xc_domain_max_vcpus(ctx->xch, domid, info->max_vcpus);
     3.7 -    xc_domain_setmaxmem(ctx->xch, domid, info->max_memkb + info->video_memkb);
     3.8 +    xc_domain_setmaxmem(ctx->xch, domid, info->target_memkb + LIBXL_MAXMEM_CONSTANT);
     3.9      xc_domain_set_memmap_limit(ctx->xch, domid, 
    3.10              (info->hvm) ? info->max_memkb : 
    3.11              (info->max_memkb + info->u.pv.slack_memkb));
    3.12 @@ -81,7 +81,9 @@ int build_post(struct libxl_ctx *ctx, ui
    3.13      ents[0] = "memory/static-max";
    3.14      ents[1] = libxl_sprintf(ctx, "%d", info->max_memkb);
    3.15      ents[2] = "memory/target";
    3.16 -    ents[3] = libxl_sprintf(ctx, "%d", info->max_memkb); /* PROBABLY WRONG */
    3.17 +    ents[3] = libxl_sprintf(ctx, "%d", info->target_memkb);
    3.18 +    ents[2] = "memory/videoram";
    3.19 +    ents[3] = libxl_sprintf(ctx, "%d", info->video_memkb);
    3.20      ents[4] = "domid";
    3.21      ents[5] = libxl_sprintf(ctx, "%d", domid);
    3.22      ents[6] = "store/port";
    3.23 @@ -145,7 +147,7 @@ int build_hvm(struct libxl_ctx *ctx, uin
    3.24  {
    3.25      int ret;
    3.26  
    3.27 -    ret = xc_hvm_build(ctx->xch, domid, info->max_memkb / 1024, info->kernel);
    3.28 +    ret = xc_hvm_build_target_mem(ctx->xch, domid, (info->max_memkb - info->video_memkb) / 1024, (info->target_memkb - info->video_memkb) / 1024, info->kernel);
    3.29      if (ret) {
    3.30          XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, ret, "hvm building failed");
    3.31          return ERROR_FAIL;
     4.1 --- a/tools/libxl/libxl_internal.h	Wed Dec 09 10:43:33 2009 +0000
     4.2 +++ b/tools/libxl/libxl_internal.h	Wed Dec 09 10:44:11 2009 +0000
     4.3 @@ -31,6 +31,7 @@
     4.4  #define LIBXL_DEVICE_MODEL_START_TIMEOUT 10
     4.5  #define LIBXL_XENCONSOLE_LIMIT 1048576
     4.6  #define LIBXL_XENCONSOLE_PROTOCOL "vt100"
     4.7 +#define LIBXL_MAXMEM_CONSTANT (1 * 1024 * 1024)
     4.8  #define QEMU_SIGNATURE "QemuDeviceModelRecord"
     4.9  
    4.10  #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
     5.1 --- a/tools/libxl/xl.c	Wed Dec 09 10:43:33 2009 +0000
     5.2 +++ b/tools/libxl/xl.c	Wed Dec 09 10:44:11 2009 +0000
     5.3 @@ -85,6 +85,7 @@ static void printf_info(libxl_domain_cre
     5.4      printf("vpt_align: %d\n", b_info->vpt_align);
     5.5      printf("max_vcpus: %d\n", b_info->max_vcpus);
     5.6      printf("max_memkb: %d\n", b_info->max_memkb);
     5.7 +    printf("target_memkb: %d\n", b_info->target_memkb);
     5.8      printf("kernel: %s\n", b_info->kernel);
     5.9      printf("hvm: %d\n", b_info->hvm);
    5.10  
    5.11 @@ -283,8 +284,10 @@ static void parse_config_file(const char
    5.12      if (config_lookup_int (&config, "vcpus", &l) == CONFIG_TRUE)
    5.13          b_info->max_vcpus = l;
    5.14  
    5.15 -    if (config_lookup_int (&config, "memory", &l) == CONFIG_TRUE)
    5.16 +    if (config_lookup_int (&config, "memory", &l) == CONFIG_TRUE) {
    5.17          b_info->max_memkb = l * 1024;
    5.18 +        b_info->target_memkb = b_info->max_memkb;
    5.19 +    }
    5.20  
    5.21      if (config_lookup_int (&config, "shadow_memory", &l) == CONFIG_TRUE)
    5.22          b_info->shadow_memkb = l * 1024;
    5.23 @@ -754,6 +757,7 @@ static void help(char *command)
    5.24          printf(" restore                       restore a domain from a saved state\n\n");
    5.25          printf(" cd-insert                     insert a cdrom into a guest's cd drive\n\n");
    5.26          printf(" cd-eject                      eject a cdrom from a guest's cd drive\n\n");
    5.27 +        printf(" mem-set                       set the current memory usage for a domain\n\n");
    5.28      } else if(!strcmp(command, "create")) {
    5.29          printf("Usage: xl create <ConfigFile> [options] [vars]\n\n");
    5.30          printf("Create a domain based on <ConfigFile>.\n\n");
    5.31 @@ -802,9 +806,54 @@ static void help(char *command)
    5.32      } else if (!strcmp(command, "cd-eject")) {
    5.33          printf("Usage: xl cd-eject <Domain> <VirtualDevice>\n\n");
    5.34          printf("Eject a cdrom from a guest's cd drive.\n\n");
    5.35 +    } else if (!strcmp(command, "mem-set")) {
    5.36 +        printf("Usage: xl mem-set <Domain> <MemKB>\n\n");
    5.37 +        printf("Set the current memory usage for a domain.\n\n");
    5.38      }
    5.39  }
    5.40  
    5.41 +void set_memory_target(char *p, char *mem)
    5.42 +{
    5.43 +    struct libxl_ctx ctx;
    5.44 +    uint32_t domid;
    5.45 +
    5.46 +    libxl_ctx_init(&ctx);
    5.47 +    libxl_ctx_set_log(&ctx, log_callback, NULL);
    5.48 +
    5.49 +    if (libxl_param_to_domid(&ctx, p, &domid) < 0) {
    5.50 +        fprintf(stderr, "%s is an invalid domain identifier\n", p);
    5.51 +        exit(2);
    5.52 +    }
    5.53 +    libxl_set_memory_target(&ctx, domid, atoi(mem));
    5.54 +}
    5.55 +
    5.56 +int main_memset(int argc, char **argv)
    5.57 +{
    5.58 +    int opt = 0;
    5.59 +    char *p = NULL, *mem;
    5.60 +
    5.61 +    while ((opt = getopt(argc, argv, "h:")) != -1) {
    5.62 +        switch (opt) {
    5.63 +        case 'h':
    5.64 +            help("mem-set");
    5.65 +            exit(0);
    5.66 +        default:
    5.67 +            fprintf(stderr, "option not supported\n");
    5.68 +            break;
    5.69 +        }
    5.70 +    }
    5.71 +    if (optind >= argc - 1) {
    5.72 +        help("mem-set");
    5.73 +        exit(2);
    5.74 +    }
    5.75 +
    5.76 +    p = argv[optind];
    5.77 +    mem = argv[optind + 1];
    5.78 +
    5.79 +    set_memory_target(p, mem);
    5.80 +    exit(0);
    5.81 +}
    5.82 +
    5.83  void console(char *p, int cons_num)
    5.84  {
    5.85      struct libxl_ctx ctx;
    5.86 @@ -1434,6 +1483,8 @@ int main(int argc, char **argv)
    5.87          main_cd_insert(argc - 1, argv + 1);
    5.88      } else if (!strcmp(argv[1], "cd-eject")) {
    5.89          main_cd_eject(argc - 1, argv + 1);
    5.90 +    } else if (!strcmp(argv[1], "mem-set")) {
    5.91 +        main_memset(argc - 1, argv + 1);
    5.92      } else if (!strcmp(argv[1], "help")) {
    5.93          if (argc > 2)
    5.94              help(argv[2]);