~opencompute-developers/opencompute/checkbox

« back to all changes in this revision

Viewing changes to scripts/memory_info

Updated OCP Checkbox to the final release version of Checkbox which is now in Legacy mode.

Also fixed some unittest failures found during the update.

Incremented release version to match the last release version of Checkbox.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python3
2
 
"""
3
 
Copyright (C) 2010-2013 by Cloud Computing Center for Mobile Applications
4
 
Industrial Technology Research Institute
5
 
 
6
 
memory_info
7
 
  1. Use lshw command to gather memory information.
8
 
  2. Testing prerequisites:
9
 
     4 channels DDR3 registered memory interface on each processor 0 
10
 
     and processor 1.
11
 
     2 DDR3 slots per channel per processor. (total of 16 DIMMs 
12
 
                                              on the motherboard)
13
 
  3. The program will output memory module, vendor, size and slot.
14
 
  4. Criteria: Total of 16 DIMMs on the motherboard.
15
 
 
16
 
Authors
17
 
  Nelson Chu <Nelson.Chu@itri.org.tw>
18
 
 
19
 
This program is free software: you can redistribute it and/or modify
20
 
it under the terms of the GNU General Public License version 3,
21
 
as published by the Free Software Foundation.
22
 
 
23
 
This program is distributed in the hope that it will be useful,
24
 
but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 
GNU General Public License for more details.
27
 
 
28
 
You should have received a copy of the GNU General Public License
29
 
along with this program.  If not, see <http://www.gnu.org/licenses/>.
30
 
 
31
 
"""
32
 
 
 
2
 
 
3
import re
33
4
import sys
34
 
import xml.etree.ElementTree as ET
35
 
from subprocess import Popen, PIPE
36
 
 
37
 
def main():
38
 
    attribute = ['description', 'vendor', 'slot', 'size']
39
 
    command = 'lshw -xml'
40
 
    hwinfo_xml = Popen(command, stdout=PIPE, stderr=PIPE,
41
 
                       shell=True).communicate()[0]
42
 
    root = ET.fromstring(hwinfo_xml)
43
 
 
44
 
    memory_list = root.findall(".//clock/..[@class='memory']")
45
 
 
46
 
    if not memory_list:
47
 
        print("Fail: Cannot parse any memory information.")
48
 
        return 10
49
 
 
50
 
    count = 0
51
 
    for dimm in memory_list:
52
 
        if dimm.find('slot') is None:
53
 
            continue
54
 
 
55
 
        for attr in attribute:
56
 
            if dimm.find(attr) is None:
57
 
                print(("Fail: Cannot find memory %s") %attr, file=sys.stderr)
58
 
                return 20
59
 
 
60
 
        count = count +1
61
 
        memory_size = int(dimm.find('size').text) / (1024**3)
62
 
        for attr in attribute:
63
 
            if attr == 'description':
64
 
                print('%s' %(dimm.find(attr).text), end=" ")
65
 
                continue
66
 
            elif attr == 'size':
67
 
                print('%s=%dGB.' %(attr, memory_size))
68
 
                continue
69
 
            print('%s=%s' %(attr, dimm.find(attr).text), end=" ")
70
 
 
71
 
    print("Total number of DIMMs is %s." %(count))
72
 
    if count != 16:
73
 
        print("Fail: Memory DIMM number is not meet the requirement.", 
74
 
              file=sys.stderr)
75
 
        return 20
76
 
 
 
5
 
 
6
 
 
7
def get_meminfo():
 
8
    meminfo = {}
 
9
    for line in open("/proc/meminfo").readlines():
 
10
        match = re.match(r"(.*):\s+(.*)", line)
 
11
        if match:
 
12
            key = match.group(1)
 
13
            value = match.group(2)
 
14
            meminfo[key] = value
 
15
 
 
16
    return meminfo
 
17
 
 
18
 
 
19
def main(args):
 
20
    meminfo = get_meminfo()
 
21
    amount, units = meminfo["MemTotal"].split()
 
22
 
 
23
    amount = float(amount)
 
24
    next_units = {'kB': 'MB',
 
25
                  'MB': 'GB'}
 
26
 
 
27
    while amount > 1024:
 
28
        amount = amount / 1024
 
29
        units = next_units[units]
 
30
 
 
31
    print("%.1f %s" % (amount, units))
77
32
    return 0
78
33
 
79
 
if __name__ == '__main__':
80
 
    sys.exit(main())
 
34
if __name__ == "__main__":
 
35
    sys.exit(main(sys.argv[1:]))