~jocave/checkbox/hybrid-amd-gpu-mods

« back to all changes in this revision

Viewing changes to checkbox-old/scripts/cpu_topology

  • Committer: Zygmunt Krynicki
  • Date: 2013-05-29 07:50:30 UTC
  • mto: This revision was merged to the branch mainline in revision 2153.
  • Revision ID: zygmunt.krynicki@canonical.com-20130529075030-ngwz245hs2u3y6us
checkbox: move current checkbox code into checkbox-old

This patch cleans up the top-level directory of the project into dedicated
sub-project directories. One for checkbox-old (the current checkbox and all the
associated stuff), one for plainbox and another for checkbox-ng.

There are some associated changes, such as updating the 'source' mode of
checkbox provider in plainbox, and fixing paths in various test scripts that we
have.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python3
 
2
'''
 
3
cpu_topology
 
4
Written by Jeffrey Lane <jeffrey.lane@canonical.com>
 
5
'''
 
6
import sys
 
7
import os
 
8
 
 
9
 
 
10
class proc_cpuinfo():
 
11
    '''
 
12
    Class to get and handle information from /proc/cpuinfo
 
13
    Creates a dictionary of data gleaned from that file.
 
14
    '''
 
15
    def __init__(self):
 
16
        self.cpuinfo = {}
 
17
        cpu_fh = open('/proc/cpuinfo', 'r')
 
18
        try:
 
19
            temp = cpu_fh.readlines()
 
20
        finally:
 
21
            cpu_fh.close()
 
22
 
 
23
        for i in temp:
 
24
            if i.startswith('processor'):
 
25
                key = 'cpu' + (i.split(':')[1].strip())
 
26
                self.cpuinfo[key] = {'core_id':'', 'physical_package_id':''}
 
27
            elif i.startswith('core id'):
 
28
                self.cpuinfo[key].update({'core_id': i.split(':')[1].strip()})
 
29
            elif i.startswith('physical id'):
 
30
                self.cpuinfo[key].update({'physical_package_id':
 
31
                                          i.split(':')[1].strip()})
 
32
            else:
 
33
                continue
 
34
 
 
35
 
 
36
class sysfs_cpu():
 
37
    '''
 
38
    Class to get and handle information from sysfs as relates to CPU topology
 
39
    Creates an informational class to present information on various CPUs
 
40
    '''
 
41
 
 
42
    def __init__(self, proc):
 
43
        self.syscpu = {}
 
44
        self.path = '/sys/devices/system/cpu/' + proc + '/topology'
 
45
        items = ['core_id', 'physical_package_id']
 
46
        for i in items:
 
47
            syscpu_fh = open(os.path.join(self.path, i), 'r')
 
48
            try:
 
49
                self.syscpu[i] = syscpu_fh.readline().strip()
 
50
            finally:
 
51
                syscpu_fh.close()
 
52
 
 
53
 
 
54
def compare(proc_cpu, sys_cpu):
 
55
    cpu_map = {}
 
56
    '''
 
57
    If there is only 1 CPU the test don't look for core_id
 
58
    and physical_package_id because those information are absent in
 
59
    /proc/cpuinfo on singlecore system
 
60
    '''
 
61
    for key in proc_cpu.keys():
 
62
        if 'cpu1' not in proc_cpu:
 
63
            cpu_map[key] = True
 
64
        else:
 
65
            for subkey in proc_cpu[key].keys():
 
66
                if proc_cpu[key][subkey] == sys_cpu[key][subkey]:
 
67
                    cpu_map[key] = True
 
68
                else:
 
69
                    cpu_map[key] = False
 
70
    return cpu_map
 
71
 
 
72
 
 
73
def main():
 
74
    cpuinfo = proc_cpuinfo()
 
75
    sys_cpu = {}
 
76
    keys = cpuinfo.cpuinfo.keys()
 
77
    for k in keys:
 
78
        sys_cpu[k] = sysfs_cpu(k).syscpu
 
79
    cpu_map = compare(cpuinfo.cpuinfo, sys_cpu)
 
80
    if False in cpu_map.values() or len(cpu_map) < 1:
 
81
        print("FAIL: CPU Topology is incorrect", file=sys.stderr)
 
82
        print("-" * 52, file=sys.stderr)
 
83
        print("{0}{1}".format("/proc/cpuinfo".center(30), "sysfs".center(25)),
 
84
                file=sys.stderr)
 
85
        print("{0}{1}{2}{3}{1}{2}".format(
 
86
                "CPU".center(6),
 
87
                "Physical ID".center(13),
 
88
                "Core ID".center(9),
 
89
                "|".center(3)), file=sys.stderr)
 
90
        for key in sorted(sys_cpu.keys()):
 
91
            print("{0}{1}{2}{3}{4}{5}".format(
 
92
                  key.center(6),
 
93
                  cpuinfo.cpuinfo[key]['physical_package_id'].center(13),
 
94
                  cpuinfo.cpuinfo[key]['core_id'].center(9),
 
95
                  "|".center(3),
 
96
                  sys_cpu[key]['physical_package_id'].center(13),
 
97
                  sys_cpu[key]['core_id'].center(9)), file=sys.stderr)
 
98
        return 1
 
99
    else:
 
100
        return 0
 
101
 
 
102
if __name__ == '__main__':
 
103
    sys.exit(main())