~le-chi-thu/lava-test/add-verbose-argument

« back to all changes in this revision

Viewing changes to abrek/hwprofile.py

  • Committer: Paul Larson
  • Date: 2011-09-22 17:58:01 UTC
  • mfrom: (92.1.4 using-lava-tool-2)
  • Revision ID: paul.larson@canonical.com-20110922175801-l99rky1xxsr6k3fn
Big refactoring branch to make lava-test use lava-tool.  Thanks to
Zygmunt and ChiThu!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (c) 2010 Linaro
2
 
#
3
 
# This program is free software: you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation, either version 3 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
 
16
 
import re
17
 
import sys
18
 
from subprocess import Popen, PIPE
19
 
from utils import read_file, get_machine_type
20
 
 
21
 
INTEL_KEYMAP = {
22
 
    'vendor_id': 'cpu_vendor_name',
23
 
    'cpu family': 'cpu_family',
24
 
    'model': 'cpu_model',
25
 
    'model name': 'cpu_model_name',
26
 
    'stepping': 'cpu_stepping',
27
 
    'cpu MHz': 'cpu_mhz',
28
 
    'flags': 'cpu_features',
29
 
}
30
 
 
31
 
INTEL_VALMAP = {
32
 
    'cpu family': int,
33
 
    'model': int,
34
 
    'stepping': int,
35
 
    'cpu MHz': float,
36
 
}
37
 
 
38
 
ARM_KEYMAP = {
39
 
    'Processor': 'cpu_model_name',
40
 
    'Features': 'cpu_features',
41
 
    'CPU implementer': 'cpu_implementer',
42
 
    'CPU architecture': 'cpu_architecture',
43
 
    'CPU variant': 'cpu_variant',
44
 
    'CPU part': 'cpu_part',
45
 
    'CPU revision': 'cpu_revision',
46
 
}
47
 
 
48
 
ARM_VALMAP = {
49
 
    'CPU implementer': lambda value: int(value, 16),
50
 
    'CPU architecture': int,
51
 
    'CPU variant': lambda value: int(value, 16),
52
 
    'CPU part': lambda value: int(value, 16),
53
 
    'CPU revision': int,
54
 
}
55
 
 
56
 
 
57
 
def _translate_cpuinfo(keymap, valmap, key, value):
58
 
    """
59
 
    Translate a key and value using keymap and valmap passed in
60
 
    """
61
 
    newkey = keymap.get(key, key)
62
 
    newval = valmap.get(key, lambda x: x)(value)
63
 
    return newkey, newval
64
 
 
65
 
def get_cpu_devs():
66
 
    """
67
 
    Return a list of CPU devices
68
 
    """
69
 
    pattern = re.compile('^(?P<key>.+?)\s*:\s*(?P<value>.*)$')
70
 
    cpunum = 0
71
 
    devices = []
72
 
    cpudevs = []
73
 
    cpudevs.append({})
74
 
    machine = get_machine_type()
75
 
    if machine in ('i686', 'x86_64'):
76
 
        keymap, valmap = INTEL_KEYMAP, INTEL_VALMAP
77
 
    elif machine.startswith('arm'):
78
 
        keymap, valmap = ARM_KEYMAP, ARM_VALMAP
79
 
 
80
 
    try:
81
 
        cpuinfo = read_file("/proc/cpuinfo")
82
 
        for line in cpuinfo.splitlines():
83
 
            match = pattern.match(line)
84
 
            if match:
85
 
                key, value = match.groups()
86
 
                key, value = _translate_cpuinfo(keymap, valmap,
87
 
                    key, value)
88
 
                if cpudevs[cpunum].get(key):
89
 
                    cpunum += 1
90
 
                    cpudevs.append({})
91
 
                cpudevs[cpunum][key] = value
92
 
        for c in range(len(cpudevs)):
93
 
            device = {}
94
 
            device['device_type'] = 'device.cpu'
95
 
            device['description'] = 'Processor #{0}'.format(c)
96
 
            device['attributes'] = cpudevs[c]
97
 
            devices.append(device)
98
 
    except IOError:
99
 
        print >> sys.stderr, "WARNING: Could not read cpu information"
100
 
    return devices
101
 
 
102
 
 
103
 
def get_board_devs():
104
 
    """
105
 
    Return a list of board devices
106
 
    """
107
 
    devices = []
108
 
    attributes = {}
109
 
    device = {}
110
 
    machine = get_machine_type()
111
 
    if machine in ('i686', 'x86_64'):
112
 
        try:
113
 
            description = read_file('/sys/class/dmi/id/board_name') or None
114
 
            vendor = read_file('/sys/class/dmi/id/board_vendor') or None
115
 
            version = read_file('/sys/class/dmi/id/board_version') or None
116
 
            if description:
117
 
                device['description'] = description.strip()
118
 
            if vendor:
119
 
                attributes['vendor'] = vendor.strip()
120
 
            if version:
121
 
                attributes['version'] = version.strip()
122
 
        except IOError:
123
 
            print >> sys.stderr, "WARNING: Could not read board information"
124
 
            return devices
125
 
    elif machine.startswith('arm'):
126
 
        try:
127
 
            cpuinfo = read_file("/proc/cpuinfo")
128
 
            if cpuinfo is None:
129
 
                return devices
130
 
            pattern = re.compile("^Hardware\s*:\s*(?P<description>.+)$", re.M)
131
 
            match = pattern.search(cpuinfo)
132
 
            if match is None:
133
 
                return devices
134
 
            device['description'] = match.group('description')
135
 
        except IOError:
136
 
            print >> sys.stderr, "WARNING: Could not read board information"
137
 
            return devices
138
 
    else:
139
 
        return devices
140
 
    if attributes:
141
 
        device['attributes'] = attributes
142
 
    device['device_type'] = 'device.board'
143
 
    devices.append(device)
144
 
    return devices
145
 
 
146
 
def get_mem_devs():
147
 
    """ Return a list of memory devices
148
 
 
149
 
    This returns up to two items, one for physical RAM and another for swap
150
 
    """
151
 
    pattern = re.compile('^(?P<key>.+?)\s*:\s*(?P<value>.+) kB$', re.M)
152
 
 
153
 
    devices = []
154
 
    try:
155
 
        meminfo = read_file("/proc/meminfo")
156
 
        for match in pattern.finditer(meminfo):
157
 
            key, value = match.groups()
158
 
            if key not in ('MemTotal', 'SwapTotal'):
159
 
                continue
160
 
            capacity = int(value) << 10 #Kernel reports in 2^10 units
161
 
            if capacity == 0:
162
 
                continue
163
 
            if key == 'MemTotal':
164
 
                kind = 'RAM'
165
 
            else:
166
 
                kind = 'swap'
167
 
            description = "{capacity}MiB of {kind}".format(
168
 
                capacity=capacity >> 20, kind=kind)
169
 
            device = {}
170
 
            device['description'] = description
171
 
            device['attributes'] = {'capacity': str(capacity), 'kind': kind}
172
 
            device['device_type'] = "device.mem"
173
 
            devices.append(device)
174
 
    except IOError:
175
 
        print >> sys.stderr, "WARNING: Could not read memory information"
176
 
    return devices
177
 
 
178
 
def get_usb_devs():
179
 
    """
180
 
    Return a list of usb devices
181
 
    """
182
 
    pattern = re.compile(
183
 
              "^Bus \d{3} Device \d{3}: ID (?P<vendor_id>[0-9a-f]{4}):"
184
 
              "(?P<product_id>[0-9a-f]{4}) (?P<description>.*)$")
185
 
    devices = []
186
 
    try:
187
 
        for line in Popen('lsusb', stdout=PIPE).communicate()[0].splitlines():
188
 
            match = pattern.match(line)
189
 
            if match:
190
 
                vendor_id, product_id, description = match.groups()
191
 
                attributes = {}
192
 
                device = {}
193
 
                attributes['vendor_id'] = int(vendor_id, 16)
194
 
                attributes['product_id'] = int(product_id, 16)
195
 
                device['attributes'] = attributes
196
 
                device['description'] = description
197
 
                device['device_type'] = 'device.usb'
198
 
                devices.append(device)
199
 
    except OSError:
200
 
        print >> sys.stderr, "WARNING: Could not read usb device information, \
201
 
unable to run lsusb, please install usbutils package"
202
 
    return devices
203
 
 
204
 
def get_hardware_context():
205
 
    """
206
 
    Return a dict with all of the hardware profile information gathered
207
 
    """
208
 
    hardware_context = {}
209
 
    devices = []
210
 
    devices.extend(get_cpu_devs())
211
 
    devices.extend(get_board_devs())
212
 
    devices.extend(get_mem_devs())
213
 
    devices.extend(get_usb_devs())
214
 
    hardware_context['devices'] = devices
215
 
    return hardware_context
216