xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Juergen Gross <jgross@suse.com>
To: xen-devel@lists.xen.org
Cc: Juergen Gross <jgross@suse.com>,
	wei.liu2@citrix.com, ian.jackson@eu.citrix.com,
	stefano.stabellini@eu.citrix.com
Subject: [PATCH 1/3] tools: add --maxmem parameter to init-xenstore-domain
Date: Mon,  8 Aug 2016 10:28:27 +0200	[thread overview]
Message-ID: <1470644909-22859-2-git-send-email-jgross@suse.com> (raw)
In-Reply-To: <1470644909-22859-1-git-send-email-jgross@suse.com>

Add a parameter to specify the maximum memory size of the xenstore
domain. In case the xenstore domain supports ballooning it will be
capable to adjust its own size according to its memory needs.

The maximum memory size can be specified as an absolute value in
MiB, as a fraction of the host's memory, or as a combination of
both (the maximum of the absolute and the fraction value):

--maxmem <m>             maxmem is <m> MiB
--maxmem <a>/<b>         maxmem is hostmem * a / b
--maxmem <m>:<a>/<b>     maxmem is max(<m> MiB, hostmem * a / b)

Signed-off-by: Juergen Gross <jgross@suse.com>
---
 tools/helpers/init-xenstore-domain.c | 83 +++++++++++++++++++++++++++++++++++-
 1 file changed, 81 insertions(+), 2 deletions(-)

diff --git a/tools/helpers/init-xenstore-domain.c b/tools/helpers/init-xenstore-domain.c
index 53b4b01..a7c97d7 100644
--- a/tools/helpers/init-xenstore-domain.c
+++ b/tools/helpers/init-xenstore-domain.c
@@ -23,6 +23,7 @@ static char *flask;
 static char *param;
 static char *name = "Xenstore";
 static int memory;
+static int maxmem;
 
 static struct option options[] = {
     { "kernel", 1, NULL, 'k' },
@@ -31,6 +32,7 @@ static struct option options[] = {
     { "ramdisk", 1, NULL, 'r' },
     { "param", 1, NULL, 'p' },
     { "name", 1, NULL, 'n' },
+    { "maxmem", 1, NULL, 'M' },
     { NULL, 0, NULL, 0 }
 };
 
@@ -48,7 +50,11 @@ static void usage(void)
 "  --flask <flask-label>      optional flask label of the domain\n"
 "  --ramdisk <ramdisk-file>   optional ramdisk file for the domain\n"
 "  --param <cmdline>          optional additional parameters for the domain\n"
-"  --name <name>              name of the domain (default: Xenstore)\n");
+"  --name <name>              name of the domain (default: Xenstore)\n"
+"  --maxmem <max size>        maximum memory size in the format:\n"
+"                             <MB val>|<a>/<b>|<MB val>:<a>/<b>\n"
+"                             (an absolute value in MB, a fraction a/b of\n"
+"                             the host memory, or the maximum of both)\n");
 }
 
 static int build(xc_interface *xch)
@@ -58,7 +64,7 @@ static int build(xc_interface *xch)
     xen_domain_handle_t handle = { 0 };
     int rv, xs_fd;
     struct xc_dom_image *dom = NULL;
-    int limit_kb = (memory + 1) * 1024;
+    int limit_kb = (maxmem ? : (memory + 1)) * 1024;
 
     xs_fd = open("/dev/xen/xenbus_backend", O_RDWR);
     if ( xs_fd == -1 )
@@ -223,6 +229,63 @@ static int check_domain(xc_interface *xch)
     return 0;
 }
 
+static int parse_maxmem(xc_interface *xch, char *str)
+{
+    xc_physinfo_t info;
+    int rv;
+    unsigned long mb = 0, a = 0, b = 0;
+    unsigned long val;
+    unsigned long *res;
+    char buf[16];
+    char *p;
+    char *s = str;
+
+    rv = xc_physinfo(xch, &info);
+    if ( rv )
+    {
+        fprintf(stderr, "xc_physinfo failed\n");
+        return -1;
+    }
+
+    res = &mb;
+    for (p = s; *p; s = p + 1)
+    {
+        val = strtoul(s, &p, 10);
+        if ( val == 0 || val >= INT_MAX / 1024 )
+            goto err;
+        if ( *p == '/' )
+        {
+            if ( res != &mb || a != 0 )
+                goto err;
+            a = val;
+            res = &b;
+            continue;
+        }
+        if ( *res != 0 )
+            goto err;
+        *res = val;
+        if ( *p != 0 && *p != ':' )
+            goto err;
+        res = &mb;
+    }
+    if ( a && !b )
+        goto err;
+
+    val = a ? info.total_pages * a / (b * 1024 * 1024 / XC_PAGE_SIZE) : 0;
+    if ( val >= INT_MAX / 1024 )
+        goto err;
+
+    maxmem = mb < val ? val : mb;
+    if ( maxmem < memory )
+        maxmem = 0;
+
+    return maxmem;
+
+err:
+    fprintf(stderr, "illegal value for maxmem: %s\n", str);
+    return -1;
+}
+
 static void do_xs_write(struct xs_handle *xsh, char *path, char *val)
 {
     if ( !xs_write(xsh, XBT_NULL, path, val, strlen(val)) )
@@ -244,6 +307,7 @@ int main(int argc, char** argv)
     struct xs_handle *xsh;
     char buf[16];
     int rv, fd;
+    char *maxmem_str = NULL;
 
     while ( (opt = getopt_long(argc, argv, "", options, NULL)) != -1 )
     {
@@ -267,6 +331,9 @@ int main(int argc, char** argv)
         case 'n':
             name = optarg;
             break;
+        case 'M':
+            maxmem_str = optarg;
+            break;
         default:
             usage();
             return 2;
@@ -286,6 +353,16 @@ int main(int argc, char** argv)
         return 1;
     }
 
+    if ( maxmem_str )
+    {
+        maxmem = parse_maxmem(xch, maxmem_str);
+        if ( maxmem < 0 )
+        {
+            xc_interface_close(xch);
+            return 1;
+        }
+    }
+
     rv = check_domain(xch);
 
     if ( !rv )
@@ -314,6 +391,8 @@ int main(int argc, char** argv)
     do_xs_write_dom(xsh, "name", name);
     snprintf(buf, 16, "%d", memory * 1024);
     do_xs_write_dom(xsh, "memory/target", buf);
+    if (maxmem)
+        snprintf(buf, 16, "%d", maxmem * 1024);
     do_xs_write_dom(xsh, "memory/static-max", buf);
     xs_close(xsh);
 
-- 
2.6.6


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

  reply	other threads:[~2016-08-08  8:28 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-08  8:28 [PATCH 0/3] tools: support autoballooning of xenstore domain Juergen Gross
2016-08-08  8:28 ` Juergen Gross [this message]
2016-08-11 17:03   ` [PATCH 1/3] tools: add --maxmem parameter to init-xenstore-domain Wei Liu
2016-08-08  8:28 ` [PATCH 2/3] stubdom: add CONFIG_BALLOON to xenstore config Juergen Gross
2016-08-11 17:03   ` Wei Liu
2016-08-08  8:28 ` [PATCH 3/3] tools: add config parameter for maximum memory of xenstore domain Juergen Gross
2016-08-11 17:03   ` Wei Liu
2016-08-08 14:45 ` [PATCH 0/3] tools: support autoballooning " Ian Jackson
2016-08-08 17:31   ` Juergen Gross
2016-08-24 11:12     ` Wei Liu
2016-08-29  6:22 ` Juergen Gross
2016-08-29  8:52   ` Wei Liu
2016-09-06 10:09     ` [PATCH 0/3] tools: support autoballooning of xenstore domain [and 1 more messages] Ian Jackson
2016-09-06 10:45       ` Wei Liu
2016-09-06 10:01 ` [PATCH 0/3] tools: support autoballooning of xenstore domain Juergen Gross

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1470644909-22859-2-git-send-email-jgross@suse.com \
    --to=jgross@suse.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=stefano.stabellini@eu.citrix.com \
    --cc=wei.liu2@citrix.com \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).