Change in vdsm[master]: vdsm: introduce cpuinfo module

nsoffer at redhat.com nsoffer at redhat.com
Tue Oct 13 08:21:02 UTC 2015


Nir Soffer has posted comments on this change.

Change subject: vdsm: introduce cpuinfo module
......................................................................


Patch Set 9:

(5 comments)

Partial review

https://gerrit.ovirt.org/#/c/46912/9/lib/vdsm/cpuinfo.py
File lib/vdsm/cpuinfo.py:

Line 28: class Architecture:
Line 29:     X86_64 = 'x86_64'
Line 30:     PPC64 = 'ppc64'
Line 31:     PPC64LE = 'ppc64le'
Line 32:     POWER = (PPC64, PPC64LE)
You have the same information 3 times. This should appear once, either as the class, or as a dict.

_CPU_ARCH_TO_ARCHITECTURE can be created dynamically like this:

    _CPU_ARCH_TO_ARCHITECTURE = {v: k for k, v in A.__dict__.items()
                                 if not k.startswith('_')}

And the names are not consistent, why _ARCHITECTURE_TO_CPU_MAP and not _CPU_ARCH_TO_ARCHITECTURE_MAP? Use the same convention (e.g. remove "_MAP").

Finally, looking at the values, _CPU_ARCH_TO_ARCHITECTURE is actually "value".upper() and _ARCHITECTURE_TO_CPU_MAP is "value".lower()
Line 33: 
Line 34: 
Line 35: _CPU_ARCH_TO_ARCHITECTURE = {
Line 36:     'x86_64': Architecture.X86_64,


Line 38:     'ppc64le': Architecture.PPC64LE
Line 39: }
Line 40: 
Line 41: _ARCHITECTURE_TO_CPU_MAP = {
Line 42:     Architecture.X86_64: 'x86',
x86_64?
Line 43:     Architecture.PPC64: 'ppc64',
Line 44:     Architecture.PPC64LE: 'ppc64',
Line 45: }
Line 46: 


Line 40: 
Line 41: _ARCHITECTURE_TO_CPU_MAP = {
Line 42:     Architecture.X86_64: 'x86',
Line 43:     Architecture.PPC64: 'ppc64',
Line 44:     Architecture.PPC64LE: 'ppc64',
ppc64le?
Line 45: }
Line 46: 
Line 47: _CPUS = defaultdict(dict)
Line 48: 


Line 94: 
Line 95:         for line in cpuinfo:
Line 96:             try:
Line 97:                 key, value = map(str.strip, line.split(':', 1))
Line 98:             except ValueError:
You seem to handle empty lines by hiding invalid key: value pairs.

A better way would to to do:

    with open('/proc/cpuinfo'):
       data = read()

    blobs = data.strip().split('\n\n')

Now we don't expect any empty lines.
Line 99:                 continue
Line 100:             if key == 'processor':
Line 101:                 cpu = int(value)
Line 102:                 continue


Line 98:             except ValueError:
Line 99:                 continue
Line 100:             if key == 'processor':
Line 101:                 cpu = int(value)
Line 102:                 continue
Instead of converting the "processor" key and treating it as the key for a dictionary, why not use a list of dicts?

    cpus = []
    for blob in blobs:
        cpu = {}
        for line in blob.splitlines():
            key, value = line.split(":", 1)
            cpu[key.strip()] = value.strip()
        cpus.append(cpu)

Accessing the cpus is exactly the same:

    cpus[3] -> dict

You also want to split the flags here, not when calling flags(), probably using tuple(flags.split())
Line 103:             else:
Line 104:                 _CPUS[cpu][key] = value
Line 105: 
Line 106: # Parse the cpuinfo right when module loads


-- 
To view, visit https://gerrit.ovirt.org/46912
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: Iaa702b05f3825ebdcfed16d86d39a8c38fcf224c
Gerrit-PatchSet: 9
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Martin Polednik <mpolednik at redhat.com>
Gerrit-Reviewer: Dan Kenigsberg <danken at redhat.com>
Gerrit-Reviewer: Francesco Romani <fromani at redhat.com>
Gerrit-Reviewer: Jenkins CI
Gerrit-Reviewer: Martin Betak <mbetak at redhat.com>
Gerrit-Reviewer: Martin Polednik <mpolednik at redhat.com>
Gerrit-Reviewer: Michal Skrivanek <mskrivan at redhat.com>
Gerrit-Reviewer: Nir Soffer <nsoffer at redhat.com>
Gerrit-Reviewer: automation at ovirt.org
Gerrit-HasComments: Yes


More information about the vdsm-patches mailing list