aboutsummaryrefslogtreecommitdiff
path: root/lava_android_test/hwprofile.py
blob: 1ac7b5066b1895a4cc35e245f698e3e47bae44a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Copyright (c) 2010 Linaro
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import re
import sys
from lava_android_test.adb import ADB

ARM_KEYMAP = {
    'Processor': 'cpu_model_name',
    'Features': 'cpu_features',
    'CPU implementer': 'cpu_implementer',
    'CPU architecture': 'cpu_architecture',
    'CPU variant': 'cpu_variant',
    'CPU part': 'cpu_part',
    'CPU revision': 'cpu_revision',
}

ARM_VALMAP = {
    'CPU implementer': lambda value: int(value, 16),
    'CPU architecture': int,
    'CPU variant': lambda value: int(value, 16),
    'CPU part': lambda value: int(value, 16),
    'CPU revision': int,
}


def _translate_cpuinfo(keymap, valmap, key, value):
    """
    Translate a key and value using keymap and valmap passed in
    """
    newkey = keymap.get(key, key)
    newval = valmap.get(key, lambda x: x)(value)
    return newkey, newval


def get_cpu_devs(adb=ADB()):
    """
    Return a list of CPU devices
    """

    pattern = re.compile('^(?P<key>.+?)\s*:\s*(?P<value>.*)$')
    cpunum = 0
    devices = []
    cpudevs = []
    cpudevs.append({})

    # TODO maybe there is other types
    keymap, valmap = ARM_KEYMAP, ARM_VALMAP

    try:
        (retcode, cpuinfo) = adb.get_shellcmdoutput("cat /proc/cpuinfo")
        if retcode != 0 or cpuinfo is None:
            raise IOError("Faile to get content of file(%s)" % "/proc/cpuinfo")
        for line in cpuinfo:
            match = pattern.match(line)
            if match:
                key, value = match.groups()
                key = key.strip()
                value = value.strip()
                try:
                    key, value = _translate_cpuinfo(keymap, valmap, key, value)
                except ValueError:
                    pass
                if cpudevs[cpunum].get(key):
                    cpunum += 1
                    cpudevs.append({})
                cpudevs[cpunum][key] = value
        for c in range(len(cpudevs)):
            device = {}
            device['device_type'] = 'device.cpu'
            device['description'] = 'Processor #{0}'.format(c)
            device['attributes'] = cpudevs[c]
            devices.append(device)
    except IOError:
        print >> sys.stderr, "WARNING: Could not read cpu information"
    return devices


def get_board_devs(adb=ADB()):
    """
    Return a list of board devices
    """
    devices = []
    attributes = {}
    device = {}

    try:
        (retcode, cpuinfo) = adb.get_shellcmdoutput("cat /proc/cpuinfo")
        if retcode != 0 or cpuinfo is None:
            raise IOError("Faile to get content of file(%s)" % "/proc/cpuinfo")
        pattern = re.compile("^Hardware\s*:\s*(?P<description>.+)$", re.M)
        found = False
        for line in cpuinfo:
            match = pattern.search(line)
            if match:
                found = True
                device['description'] = match.group('description').strip()
        if not found:
            return devices
    except IOError:
        print >> sys.stderr, "WARNING: Could not read board information"
        return devices
    if attributes:
        device['attributes'] = attributes
    device['device_type'] = 'device.board'
    devices.append(device)
    return devices


def get_mem_devs(adb=ADB()):
    """ Return a list of memory devices

    This returns up to two items, one for physical RAM and another for swap
    """
    devices = []

    pattern = re.compile('^(?P<key>.+?)\s*:\s*(?P<value>.+) kB$', re.M)

    try:
        (retcode, meminfo) = adb.get_shellcmdoutput("cat /proc/meminfo")
        if retcode != 0 or meminfo is None:
            raise IOError("Faile to get content of file(%s)" % "/proc/meminfo")
        for line in meminfo:
            match = pattern.search(line)
            if not match:
                continue
            key, value = match.groups()
            key = key.strip()
            value = value.strip()
            if key not in ('MemTotal', 'SwapTotal'):
                continue
            #Kernel reports in 2^10 units
            capacity = int(value) << 10
            if capacity == 0:
                continue
            if key == 'MemTotal':
                kind = 'RAM'
            else:
                kind = 'swap'
            description = "{capacity}MiB of {kind}".format(
                capacity=capacity >> 20, kind=kind)
            device = {}
            device['description'] = description
            device['attributes'] = {'capacity': str(capacity), 'kind': kind}
            device['device_type'] = "device.mem"
            devices.append(device)
    except IOError:
        print >> sys.stderr, "WARNING: Could not read memory information"
    return devices


def get_hardware_context(adb=ADB()):
    """
    Return a dict with all of the hardware profile information gathered
    """
    hardware_context = {}
    devices = []
    devices.extend(get_cpu_devs(adb))
    devices.extend(get_board_devs(adb))
    devices.extend(get_mem_devs(adb))
    hardware_context['devices'] = devices
    return hardware_context