From patchwork Fri Apr 8 02:23:24 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Faggioli X-Patchwork-Id: 8779681 Return-Path: X-Original-To: patchwork-xen-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 939F5C0553 for ; Fri, 8 Apr 2016 02:25:35 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 948D72025B for ; Fri, 8 Apr 2016 02:25:34 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8E9FE20263 for ; Fri, 8 Apr 2016 02:25:33 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1aoM5C-00053g-8i; Fri, 08 Apr 2016 02:23:38 +0000 Received: from mail6.bemta6.messagelabs.com ([85.158.143.247]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1aoM5A-00053S-U7 for xen-devel@lists.xenproject.org; Fri, 08 Apr 2016 02:23:37 +0000 Received: from [85.158.143.35] by server-2.bemta-6.messagelabs.com id E3/98-09532-82617075; Fri, 08 Apr 2016 02:23:36 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrKIsWRWlGSWpSXmKPExsXiVRvkqKsuxh5 u8OKJqcX3LZOZHBg9Dn+4whLAGMWamZeUX5HAmvF1ygKmgs+qFWdu32ZuYNwj3cXIxSEkMJVR 4v7Mb2wgDovAGlaJX4vfsHcxcnJICFxilWjtsIKwYyR2vGxjgrBLJXac7GYBsYUEVCRubl/FB DFpMZNEx9aXbCAJYQE9iSNHf7BD2GES8w88BIuzCRhIvNmxlxXEFhFQkri3ajLYUGaBSon1jX +B6jmArlCV2Pi+FCTMK2An8fraVLBdnAL2Etf/3mMGKRECil99IAcSFhWQk1h5uYUVolxQ4uT MJywgJcwCmhLrd+lDDJeX2P52DvMERpFZSKpmIVTNQlK1gJF5FaN6cWpRWWqRrrFeUlFmekZJ bmJmjq6hgZlebmpxcWJ6ak5iUrFecn7uJkZg4DMAwQ7Gjn9OhxglOZiURHl9+NjDhfiS8lMqM xKLM+KLSnNSiw8xynBwKEnwThcBygkWpaanVqRl5gBjECYtwcGjJMI7DSTNW1yQmFucmQ6ROs VozLFlwY21TBxbpt5byyTEkpeflyolzjsBpFQApDSjNA9uECw1XGKUlRLmZQQ6TYinILUoN7M EVf4VozgHo5Iw71eQKTyZeSVw+14BncIEdMoFfjaQU0oSEVJSDYwtYouq1RQVLy48+m23hUXh nHftIiI7Jmypmdr3fGFSVlj5BoYt0bxiAlG6N8Oc6uY4f1O9KZdTeXnxm+tTP24uvX/v/AGbN R7nrU+0mE5sEP0tOcPn9/ZuLZZtuYvrzh2vOfP+gs6pyc4m0b9V/j3p27XeQMRFYlv5o8vf5z l3Hsz+GJd1SLtOiaU4I9FQi7moOBEAZ3g5JwgDAAA= X-Env-Sender: raistlin.df@gmail.com X-Msg-Ref: server-14.tower-21.messagelabs.com!1460082215!8117599!1 X-Originating-IP: [74.125.82.65] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 8.28; banners=-,-,- X-VirusChecked: Checked Received: (qmail 43041 invoked from network); 8 Apr 2016 02:23:35 -0000 Received: from mail-wm0-f65.google.com (HELO mail-wm0-f65.google.com) (74.125.82.65) by server-14.tower-21.messagelabs.com with AES128-GCM-SHA256 encrypted SMTP; 8 Apr 2016 02:23:35 -0000 Received: by mail-wm0-f65.google.com with SMTP id n3so956340wmn.1 for ; Thu, 07 Apr 2016 19:23:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:subject:from:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=GiIimIYwMiTkC2T4wdh9eKXK6FAhqoTrvN0WMuXaWPY=; b=nTg0IBfWOwdrWrCFA/KPi12HjnEjwqyL/+WAiny3aGfJSrlovbqCVtkhbjpyHJ+8Jc S6KVXH5Dm4c9LyBtVXrMJpU2mSF3umsk791Ruk00Wk9ozsIaa2bxben0bI7L2K40oQup vxOXx2jHhPj1PbZOdW30LPWTWYdXuFM8CPwvrmW7BxYAYZCeaSOl+VUQfHul38YhZKjV TliInaUY0UfQ1o3o+N2YGub+OzvmWUBQKvp0NTn3rPhQbLGvX6/ZUBSgEBwDBuR1/Oq5 TCWZVOCMxtaFgdArVom3qESIYJB+83k+Hhiz1k37XRLUp7P7AkwSGj78ghIC6gnPXZVE gsAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:subject:from:to:cc:date:message-id :in-reply-to:references:user-agent:mime-version :content-transfer-encoding; bh=GiIimIYwMiTkC2T4wdh9eKXK6FAhqoTrvN0WMuXaWPY=; b=R4kMBmsoasnHr0Ez3/Nx4oveXc+asuNC5ChQjt+wA9hnStk5slEylFmpKGNnGHbjjJ KnNUJfa6k8MkmDRrWJtt+f28kVN859CzwCcyFLZSFPhm1aLXeLmydw24BvBtZRvwwUIx 3a+iSsUdAqdOl2L5lZaOfFHTo8EXrPF4wuWBlMDeck7+s3N1J31X85vG9BA+tR1eSI4F BQ+q0ax49ZppTormSqoAk6RWN7zS92gvLIBrDaoCHGgWy8cWVC5FvF1NNrgB0oDfhXPZ p+muVNQ/bA953MJqhrhdBUh4WNH5+0HD2D1QMyT67srprhmmF1FMjiY2v9UxaBYdUXMf b2Rg== X-Gm-Message-State: AD7BkJKftXD6iNXhNJPM8h53CNuSQFw0ohet1WANW+EeC1OO91VlYDlOmyYvRo42SgcnwA== X-Received: by 10.28.214.6 with SMTP id n6mr776962wmg.49.1460082213589; Thu, 07 Apr 2016 19:23:33 -0700 (PDT) Received: from Solace.fritz.box (net-37-116-155-252.cust.vodafonedsl.it. [37.116.155.252]) by smtp.gmail.com with ESMTPSA id i201sm699460wmf.23.2016.04.07.19.23.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 07 Apr 2016 19:23:32 -0700 (PDT) From: Dario Faggioli To: xen-devel@lists.xenproject.org Date: Fri, 08 Apr 2016 04:23:24 +0200 Message-ID: <20160408022324.9058.21614.stgit@Solace.fritz.box> In-Reply-To: <20160408022241.9058.86808.stgit@Solace.fritz.box> References: <20160408022241.9058.86808.stgit@Solace.fritz.box> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Cc: Wei Liu , Ian Jackson , Harmandeep Kaur Subject: [Xen-devel] [PATCH v3 1/6] xl: improve return and exit codes of memory related functions X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Harmandeep Kaur by making them more consistent with other examples in xl. While there, make freemem() of boolean return type, which looks more natural, and add comment explaining why parse_mem_size_kb() needs to diverge from the pattern. Signed-off-by: Harmandeep Kaur Signed-off-by: Dario Faggioli --- Cc: Ian Jackson Cc: Wei Liu --- v3: Shorten changelog. Make freemem() boolean return type. v2: Add comment to explain return vaule of parse_mem_size_kb(). Add freemem() and main_sharing(). Remove find_domain(). --- tools/libxl/xl_cmdimpl.c | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c index 2ee6c74..9612b00 100644 --- a/tools/libxl/xl_cmdimpl.c +++ b/tools/libxl/xl_cmdimpl.c @@ -2680,40 +2680,45 @@ static int preserve_domain(uint32_t *r_domid, libxl_event *event, return rc == 0 ? 1 : 0; } -static int freemem(uint32_t domid, libxl_domain_build_info *b_info) +/* + * Returns false if memory can't be freed, but also if we encounter errors. + * Returns true in case there is already, or we manage to free it, enough + * memory, but also if autoballoon is false. + */ +static bool freemem(uint32_t domid, libxl_domain_build_info *b_info) { int rc, retries = 3; uint32_t need_memkb, free_memkb; if (!autoballoon) - return 0; + return true; rc = libxl_domain_need_memory(ctx, b_info, &need_memkb); if (rc < 0) - return rc; + return false; do { rc = libxl_get_free_memory(ctx, &free_memkb); if (rc < 0) - return rc; + return false; if (free_memkb >= need_memkb) - return 0; + return true; rc = libxl_set_memory_target(ctx, 0, free_memkb - need_memkb, 1, 0); if (rc < 0) - return rc; + return false; /* wait until dom0 reaches its target, as long as we are making * progress */ rc = libxl_wait_for_memory_target(ctx, 0, 10); if (rc < 0) - return rc; + return false; retries--; } while (retries > 0); - return ERROR_NOMEM; + return false; } static void autoconnect_console(libxl_ctx *ctx_ignored, @@ -2980,8 +2985,7 @@ start: goto error_out; if (domid_soft_reset == INVALID_DOMID) { - ret = freemem(domid, &d_config.b_info); - if (ret < 0) { + if (!freemem(domid, &d_config.b_info)) { fprintf(stderr, "failed to free memory for the domain\n"); ret = ERROR_FAIL; goto error_out; @@ -3245,6 +3249,7 @@ void help(const char *command) } } +/* Returns -1 on failure; the amount of memory on success. */ static int64_t parse_mem_size_kb(const char *mem) { char *endptr; @@ -3391,7 +3396,7 @@ static int set_memory_max(uint32_t domid, const char *mem) memorykb = parse_mem_size_kb(mem); if (memorykb == -1) { fprintf(stderr, "invalid memory size: %s\n", mem); - exit(3); + exit(EXIT_FAILURE); } if (libxl_domain_setmaxmem(ctx, domid, memorykb)) { @@ -3425,7 +3430,7 @@ static int set_memory_target(uint32_t domid, const char *mem) memorykb = parse_mem_size_kb(mem); if (memorykb == -1) { fprintf(stderr, "invalid memory size: %s\n", mem); - exit(3); + exit(EXIT_FAILURE); } if (libxl_set_memory_target(ctx, domid, memorykb, 0, /* enforce */ 1)) { @@ -6132,7 +6137,7 @@ int main_sharing(int argc, char **argv) info = libxl_list_domain(ctx, &nb_domain); if (!info) { fprintf(stderr, "libxl_list_domain failed.\n"); - return 1; + return EXIT_FAILURE; } info_free = info; } else if (optind == argc-1) { @@ -6141,17 +6146,17 @@ int main_sharing(int argc, char **argv) if (rc == ERROR_DOMAIN_NOTFOUND) { fprintf(stderr, "Error: Domain \'%s\' does not exist.\n", argv[optind]); - return -rc; + return EXIT_FAILURE; } if (rc) { fprintf(stderr, "libxl_domain_info failed (code %d).\n", rc); - return -rc; + return EXIT_FAILURE; } info = &info_buf; nb_domain = 1; } else { help("sharing"); - return 2; + return EXIT_FAILURE; } sharing(info, nb_domain); @@ -6161,7 +6166,7 @@ int main_sharing(int argc, char **argv) else libxl_dominfo_dispose(info); - return 0; + return EXIT_SUCCESS; } static int sched_domain_get(libxl_scheduler sched, int domid,