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

« back to all changes in this revision

Viewing changes to scripts/memory_compare

  • Committer: Zygmunt Krynicki
  • Date: 2013-05-17 13:54:25 UTC
  • mto: This revision was merged to the branch mainline in revision 2130.
  • Revision ID: zygmunt.krynicki@canonical.com-20130517135425-cxcenxx5t0qrtbxd
checkbox-ng: add CheckBoxNG sub-project

CheckBoxNG (or lowercase as checkbox-ng, pypi:checkbox-ng) is a clean
implementation of CheckBox on top of PlainBox. It provides a new
executable, 'checkbox' that has some of the same commands that were
previously implemented in the plainbox package.

In particular CheckBoxNG comes with the 'checkbox sru' command
(the same one as in plainbox). Later on this sub-command will be removed
from plainbox.

CheckBoxNG depends on plainbox >= 0.3

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
import os
 
4
import sys
 
5
 
 
6
from checkbox.parsers.dmidecode import DmidecodeParser
 
7
from checkbox.parsers.meminfo import MeminfoParser
 
8
 
 
9
THRESHOLD = 10
 
10
 
 
11
class DmiResult:
 
12
 
 
13
    total_size = 0
 
14
 
 
15
    def addDmiDevice(self, device):
 
16
        form = getattr(device, "form", None)
 
17
 
 
18
        if form and "IMM" in form:
 
19
            try:
 
20
                self.total_size += int(getattr(device, "size", 0))
 
21
            except ValueError:
 
22
                pass
 
23
 
 
24
def get_installed_memory_size():
 
25
    dmi = DmidecodeParser(os.popen('dmidecode'))
 
26
    result = DmiResult()
 
27
 
 
28
    dmi.run(result)
 
29
 
 
30
    return result.total_size
 
31
 
 
32
class MeminfoResult:
 
33
 
 
34
    memtotal = 0
 
35
 
 
36
    def setMemory(self, memory):
 
37
        self.memtotal = memory['total']
 
38
 
 
39
def get_visible_memory_size():
 
40
    parser = MeminfoParser(open('/proc/meminfo'))
 
41
    result = MeminfoResult()
 
42
    parser.run(result)
 
43
 
 
44
    return result.memtotal
 
45
 
 
46
def main():
 
47
    if os.geteuid() != 0:
 
48
        print("This script must be run as root.", file=sys.stderr)
 
49
        return 1
 
50
 
 
51
    installed_memory = get_installed_memory_size()
 
52
    visible_memory = get_visible_memory_size()
 
53
 
 
54
    difference = installed_memory - visible_memory
 
55
    try:
 
56
        percentage = difference / installed_memory * 100
 
57
    except ZeroDivisionError:
 
58
        print("Results:")
 
59
        print("\t/proc/meminfo reports:\t%s kB" % visible_memory, file=sys.stderr)
 
60
        print("\tdmi reports:\t%s kB" % installed_memory, file=sys.stderr)
 
61
        print("\nFAIL: Either dmi or /proc/meminfo returned a memory size of 0 kB", file=sys.stderr)
 
62
        return 1
 
63
    
 
64
    if percentage <= THRESHOLD:
 
65
        print("Results:")
 
66
        print("\t/proc/meminfo reports:\t%s kB" % visible_memory)
 
67
        print("\tdmi reports:\t%s kB" % installed_memory)
 
68
        print("\nPASS: Meminfo reports %d bytes less than DMI, a difference of %.2f%%. This is less than the %d%% variance allowed." % (difference, percentage, THRESHOLD))
 
69
        return 0
 
70
    else:
 
71
        print("Results")
 
72
        print("\t/proc/meminfo reports:\t%s kB" % visible_memory, file=sys.stderr)
 
73
        print("\tdmi reports:\t%s kB" % installed_memory, file=sys.stderr)
 
74
        print("\nFAIL: Meminfo reports %d bytes less than DMI, a difference of %.2f%%. Only a variance of %d%% in reported memory is allowed." % (difference, percentage, THRESHOLD), file=sys.stderr)
 
75
        return 1
 
76
 
 
77
if __name__ == "__main__":
 
78
    sys.exit(main())