[Kimchi-devel] [PATCH] Improve code to retrieve the number of host CPUs

Aline Manera alinefm at linux.vnet.ibm.com
Tue Apr 28 14:21:51 UTC 2015


Reviewed-by: Aline Manera <alinefm at linux.vnet.ibm.com>

On 27/04/2015 21:50, Jose Ricardo Ziviani wrote:
>   - Retrieving the number of cpus using psinfo is unstable, there
>     are three (at least) different ways to do it that changes from
>     version to version. This code improves the way such functions
>     are called to avoid any surprise in future.
>
> Signed-off-by: Jose Ricardo Ziviani <joserz at linux.vnet.ibm.com>
> ---
>   src/kimchi/model/host.py | 17 +++++++++++------
>   1 file changed, 11 insertions(+), 6 deletions(-)
>
> diff --git a/src/kimchi/model/host.py b/src/kimchi/model/host.py
> index 293c4ce..b2fa379 100644
> --- a/src/kimchi/model/host.py
> +++ b/src/kimchi/model/host.py
> @@ -102,15 +102,20 @@ class HostModel(object):
>           return res
>
>       def lookup(self, *name):
> -        cpus = 0
> +        cpus = psutil.NUM_CPUS
>
> -        # Only newer psutil versions have a portable method to get
> -        # the number of cpus
> -        try:
> +        # psutil is unstable on how to get the number of
> +        # cpus, different versions call it differently
> +        if hasattr(psutil, 'cpu_count'):
>               cpus = psutil.cpu_count()
>
> -        except AttributeError:
> -            cpus = psutil._psplatform.get_num_cpus()
> +        elif hasattr(psutil, '_psplatform'):
> +            for method_name in ['_get_num_cpus', 'get_num_cpus']:
> +
> +                method = getattr(psutil._psplatform, method_name, None)
> +                if method is not None:
> +                    cpus = method()
> +                    break
>
>           self.host_info['cpus'] = cpus
>           self.host_info['memory'] = psutil.phymem_usage().total




More information about the Kimchi-devel mailing list