~brian-murray/checkbox/brian

« back to all changes in this revision

Viewing changes to registries/cpuinfo.py

  • Committer: Marc Tardif
  • Date: 2009-09-28 01:15:40 UTC
  • Revision ID: marc.tardif@canonical.com-20090928011540-zcxn4q9e34u8legb
Added support for generic cpu information.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
# You should have received a copy of the GNU General Public License
17
17
# along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
18
18
#
 
19
import os
 
20
import posixpath
 
21
 
19
22
from checkbox.lib.cache import cache
20
23
from checkbox.lib.conversion import string_to_type
21
24
 
22
25
from checkbox.properties import Path
23
 
from checkbox.registries.data import DataRegistry
24
26
from checkbox.registries.filename import FilenameRegistry
25
 
from checkbox.registries.link import LinkRegistry
26
 
 
27
 
 
28
 
class ProcessorRegistry(DataRegistry):
29
 
    """Registry for processor information.
30
 
 
31
 
    Each item contained in this registry consists of the information
32
 
    for a single processor in the /proc/cpuinfo file.
33
 
    """
34
 
 
35
 
    def items(self):
36
 
        items = []
37
 
        for line in [l.strip() for l in self.split("\n")]:
38
 
            (key, value) = line.split(":", 1)
39
 
 
40
 
            # Sanitize key so that it can be expressed as
41
 
            # an attribute
42
 
            key = key.strip()
43
 
            key = key.replace(" ", "_")
44
 
            key = key.lower()
45
 
 
46
 
            # Rename processor entry to name
47
 
            if key == "processor":
48
 
                key = "name"
49
 
 
50
 
            # Express value as a list if it is flags
51
 
            value = value.strip()
52
 
            if key == "flags":
53
 
                value = value.split()
 
27
 
 
28
 
 
29
def cpuinfo_to_processor(uname, cpuinfo):
 
30
    # Default values
 
31
    processor = {
 
32
        "platform": uname,
 
33
        "count": 1,
 
34
        "type": uname,
 
35
        "model": uname,
 
36
        "model_number": "",
 
37
        "model_version": "",
 
38
        "model_revision": "",
 
39
        "cache": 0,
 
40
        "bogomips": 0,
 
41
        "speed": -1,
 
42
        "other": ""}
 
43
 
 
44
    # Conversion table
 
45
    platform_to_conversion = {
 
46
        ("i386", "i486", "i586", "i686", "x86_64",): {
 
47
            "type": "vendor_id",
 
48
            "model": "model_name",
 
49
            "model_number": "cpu_family",
 
50
            "model_version": "model",
 
51
            "model_revision": "stepping",
 
52
            "cache": "cache_size",
 
53
            "other": "flags",
 
54
            "speed": "cpu_mhz"},
 
55
        ("alpha", "alphaev6",): {
 
56
            "count": "cpus_detected",
 
57
            "type": "cpu",
 
58
            "model": "cpu_model",
 
59
            "model_number": "cpu_variation",
 
60
            "model_version": ("system_type", "system_variation",),
 
61
            "model_revision": "cpu_revision",
 
62
            "other": "platform_string",
 
63
            "speed": "cycle_frequency_[Hz]"},
 
64
        ("ia64",): {
 
65
            "type": "vendor",
 
66
            "model": "family",
 
67
            "model_version": "archrev",
 
68
            "model_revision": "revision",
 
69
            "other": "features",
 
70
            "speed": "cpu_mhz"},
 
71
        ("ppc64", "ppc",): {
 
72
            "type": "platform",
 
73
            "model": "cpu",
 
74
            "model_version": "revision",
 
75
            "vendor": "machine",
 
76
            "speed": "clock"},
 
77
        ("sparc64", "sparc",): {
 
78
            "count": "ncpus_probed",
 
79
            "type": "type",
 
80
            "model": "cpu",
 
81
            "model_version": "type",
 
82
            "speed": "bogomips"}}
 
83
 
 
84
    processor["count"] = cpuinfo.get("count", 1)
 
85
    processor["bogomips"] = int(round(float(cpuinfo.get("bogomips", "0.0"))))
 
86
    for platform, conversion in platform_to_conversion.items():
 
87
        if uname in platform:
 
88
            for pkey, ckey in conversion.items():
 
89
                if isinstance(ckey, (list, tuple)):
 
90
                    processor[pkey] = "/".join([cpuinfo[k] for k in ckey])
 
91
                elif ckey in cpuinfo:
 
92
                    processor[pkey] = cpuinfo[ckey]
 
93
 
 
94
    # Adjust platform and vendor
 
95
    if uname[0] == "i" and uname[-2:] == "86":
 
96
        processor["platform"] = "i386"
 
97
    elif uname[:5] == "alpha":
 
98
        processor["platform"] = "alpha"
 
99
    elif uname[:5] == "sparc":
 
100
        processor["vendor"] = "sun"
 
101
 
 
102
    # Adjust cache
 
103
    if processor["cache"]:
 
104
        processor["cache"] = string_to_type(processor["cache"])
 
105
 
 
106
    # Adjust speed
 
107
    try:
 
108
        if uname[:5] == "alpha":
 
109
            speed = processor["speed"].split()[0]
 
110
            processor["speed"] = int(round(float(speed))) / 1000000
 
111
        elif uname[:5] == "sparc":
 
112
            speed = processor["speed"]
 
113
            processor["speed"] = int(round(float(speed))) / 2
 
114
        else:
 
115
            if uname[:3] == "ppc":
 
116
                # String is appended with "mhz"
 
117
                speed = processor["speed"][:-3]
54
118
            else:
55
 
                value = string_to_type(value)
56
 
 
57
 
            items.append((key, value))
58
 
 
59
 
        items.append(("processor", LinkRegistry(self)))
60
 
 
61
 
        return items
 
119
                speed = processor["speed"]
 
120
            processor["speed"] = int(round(float(speed)) - 1)
 
121
    except ValueError:
 
122
        processor["speed"] = -1
 
123
 
 
124
    # Adjust count
 
125
    try:
 
126
        processor["count"] = int(processor["count"])
 
127
    except ValueError:
 
128
        processor["count"] = 1
 
129
    else:
 
130
        # There is at least one processor
 
131
        if processor["count"] == 0:
 
132
            processor["count"] = 1
 
133
 
 
134
    return processor
62
135
 
63
136
 
64
137
class CpuinfoRegistry(FilenameRegistry):
71
144
    # Filename where cpuinfo is stored.
72
145
    filename = Path(default="/proc/cpuinfo")
73
146
 
 
147
    # Filename where maximum frequency is stored.
 
148
    frequency_filename = Path(default="/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq")
 
149
 
 
150
    def _get_attributes(self):
 
151
        count = 0
 
152
        attributes = {}
 
153
        for block in self.split("\n\n"):
 
154
            if not block:
 
155
                continue
 
156
 
 
157
            count += 1
 
158
            if count > 1:
 
159
                continue
 
160
 
 
161
            for line in block.split("\n"):
 
162
                if not line:
 
163
                    continue
 
164
 
 
165
                key, value = line.split(":")
 
166
 
 
167
                # Sanitize key and value
 
168
                key = key.strip()
 
169
                key = key.replace(" ", "_")
 
170
                value = value.strip()
 
171
 
 
172
                # Handle bogomips on sparc
 
173
                if key.endswith("Bogo"):
 
174
                    key = "bogomips"
 
175
 
 
176
                attributes[key.lower()] = value
 
177
 
 
178
        attributes["count"] = count
 
179
        return attributes
 
180
 
74
181
    @cache
75
182
    def items(self):
76
 
        items = []
77
 
        for data in [d.strip() for d in self.split("\n\n") if d]:
78
 
            key = len(items)
79
 
            value = ProcessorRegistry(data)
80
 
            items.append((key, value))
81
 
 
82
 
        return items
 
183
        uname = os.uname()[4].lower()
 
184
        attributes = self._get_attributes()
 
185
 
 
186
        processor = cpuinfo_to_processor(uname, attributes)
 
187
 
 
188
        # Check for frequency scaling
 
189
        if posixpath.exists(self.frequency_filename):
 
190
            speed = open(self.frequency_filename).read().strip()
 
191
            processor["speed"] = int(speed) / 1000
 
192
 
 
193
        return processor.items()
83
194
 
84
195
 
85
196
factory = CpuinfoRegistry