[PATCH 4/4] lxc: Count max VCPUs based on cpuset.cpus in native config.

Daniel Henrique Barboza danielhb413 at gmail.com
Wed Feb 19 12:29:49 UTC 2020



On 2/16/20 2:11 PM, Julio Faracco wrote:
> Native config files sometimes can setup cpuset.cpus to pin som CPUs.

typo: s/som/some


Everything else LGTM.


> Before this, LXC was using a fixed number of 1 VCPU. After this commit,
> XML definition will generate a dynamic number of VCPUs based on that
> cgroup attribute.
> 
> Signed-off-by: Julio Faracco <jcfaracco at gmail.com>
> ---
>   src/lxc/lxc_container.c                       | 23 ++++++++++++++++++
>   src/lxc/lxc_container.h                       |  2 ++
>   src/lxc/lxc_native.c                          | 24 +++++++++++++++++--
>   .../lxcconf2xml-cpusettune.xml                |  2 +-
>   4 files changed, 48 insertions(+), 3 deletions(-)
> 
> diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c
> index 1a2c97c9f4..d3934c8c03 100644
> --- a/src/lxc/lxc_container.c
> +++ b/src/lxc/lxc_container.c
> @@ -2493,3 +2493,26 @@ int lxcContainerChown(virDomainDefPtr def, const char *path)
>   
>       return 0;
>   }
> +
> +
> +int lxcContainerGetMaxCpusInCpuset(const char *cpuset)
> +{
> +    const char *c = cpuset;
> +    int max_cpu = 0;
> +
> +    while (c) {
> +        int a, b, ret;
> +
> +        ret = sscanf(c, "%d-%d", &a, &b);
> +        if (ret == 1)
> +            max_cpu++;
> +        else if (ret == 2)
> +            max_cpu +=  a > b ? a - b + 1 : b - a + 1;
> +
> +        if (!(c = strchr(c+1, ',')))
> +            break;
> +        c++;
> +    }
> +
> +    return max_cpu;
> +}
> diff --git a/src/lxc/lxc_container.h b/src/lxc/lxc_container.h
> index 94a6c5309c..6f112e0667 100644
> --- a/src/lxc/lxc_container.h
> +++ b/src/lxc/lxc_container.h
> @@ -63,3 +63,5 @@ virArch lxcContainerGetAlt32bitArch(virArch arch);
>   int lxcContainerChown(virDomainDefPtr def, const char *path);
>   
>   bool lxcIsBasicMountLocation(const char *path);
> +
> +int lxcContainerGetMaxCpusInCpuset(const char *cpuset);
> diff --git a/src/lxc/lxc_native.c b/src/lxc/lxc_native.c
> index 02d2bf33e4..409bf00bd2 100644
> --- a/src/lxc/lxc_native.c
> +++ b/src/lxc/lxc_native.c
> @@ -993,6 +993,24 @@ lxcSetCpusetTune(virDomainDefPtr def, virConfPtr properties)
>       return 0;
>   }
>   
> +
> +static int
> +lxcGetVCpuMax(virConfPtr properties)
> +{
> +    g_autofree char *value = NULL;
> +    int vcpumax = 1;
> +
> +    if (virConfGetValueString(properties, "lxc.cgroup.cpuset.cpus",
> +                              &value) > 0) {
> +        vcpumax = lxcContainerGetMaxCpusInCpuset(value);
> +        if (vcpumax > 0)
> +            return vcpumax;
> +    }
> +
> +    return vcpumax;
> +}
> +
> +
>   static int
>   lxcBlkioDeviceWalkCallback(const char *name, virConfValuePtr value, void *data)
>   {
> @@ -1132,6 +1150,7 @@ lxcParseConfigString(const char *config,
>       virDomainDefPtr vmdef = NULL;
>       g_autoptr(virConf) properties = NULL;
>       g_autofree char *value = NULL;
> +    int vcpumax;
>   
>       if (!(properties = virConfReadString(config, VIR_CONF_FLAG_LXC_FORMAT)))
>           return NULL;
> @@ -1155,10 +1174,11 @@ lxcParseConfigString(const char *config,
>   
>       /* Value not handled by the LXC driver, setting to
>        * minimum required to make XML parsing pass */
> -    if (virDomainDefSetVcpusMax(vmdef, 1, xmlopt) < 0)
> +    vcpumax = lxcGetVCpuMax(properties);
> +    if (virDomainDefSetVcpusMax(vmdef, vcpumax, xmlopt) < 0)
>           goto error;
>   
> -    if (virDomainDefSetVcpus(vmdef, 1) < 0)
> +    if (virDomainDefSetVcpus(vmdef, vcpumax) < 0)
>           goto error;
>   
>       vmdef->nfss = 0;
> diff --git a/tests/lxcconf2xmldata/lxcconf2xml-cpusettune.xml b/tests/lxcconf2xmldata/lxcconf2xml-cpusettune.xml
> index 6df089d00f..a1fec12d9b 100644
> --- a/tests/lxcconf2xmldata/lxcconf2xml-cpusettune.xml
> +++ b/tests/lxcconf2xmldata/lxcconf2xml-cpusettune.xml
> @@ -3,7 +3,7 @@
>     <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
>     <memory unit='KiB'>65536</memory>
>     <currentMemory unit='KiB'>65536</currentMemory>
> -  <vcpu placement='static' cpuset='1-2,5-7'>1</vcpu>
> +  <vcpu placement='static' cpuset='1-2,5-7'>5</vcpu>
>     <numatune>
>       <memory mode='strict' nodeset='1-4'/>
>     </numatune>
> 




More information about the libvir-list mailing list