• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import os, subprocess, re, commands, logging
2from autotest_lib.client.bin import utils, test
3from autotest_lib.client.common_lib import error
4
5class memory_api(test.test):
6    version = 1
7
8    def setup(self):
9        os.mkdir(self.tmpdir)
10        utils.system("%s %s -o %s" %
11                      (utils.get_cc(),
12                       os.path.join(self.bindir, "memory_api.c"),
13                       os.path.join(self.tmpdir, "memory_api")))
14        utils.system("%s %s -o %s" %
15                      (utils.get_cc(),
16                       os.path.join(self.bindir, "mremaps.c"),
17                       os.path.join(self.tmpdir, "mremaps")))
18
19
20    def initialize(self):
21        self.job.require_gcc()
22
23
24    def run_once(self, memsize = "1000000000", args=''):
25
26        vma_re = re.compile("([0-9,a-f]+)-([0-9,a-f]+)")
27        memory_re = re.compile("(\d+) bytes @(0x[0-9,a-f]+)")
28
29        vma_max_shift = 0
30        if os.access("/proc/sys/vm/vma_max_shift", os.R_OK):
31            vma_max_shift = int(
32                      open("/proc/sys/vm/vma_max_shift").read().rstrip())
33        p1 = subprocess.Popen('%s/memory_api ' % self.tmpdir  + memsize,
34                              shell=True, stdin=subprocess.PIPE,
35                              stdout=subprocess.PIPE)
36        while p1.poll() is None:
37            output = p1.stdout.readline().rstrip()
38            m = memory_re.search(output)
39            mem_start = 0
40            mem_len = 0
41            if m:
42                mem_start = int(m.group(2), 16)
43                mem_len = int(m.group(1))
44            else:
45                continue
46            map_output = open("/proc/%s/maps_backing" % p1.pid).readlines()
47            vma_count = 0
48            vma_start = 0
49            vma_len = 0
50            expected_vma_count = 1
51            for line in map_output:
52                m = vma_re.search(line)
53                if m:
54                    vma_start = int("0x%s" % m.group(1),16)
55                    vma_end = int("0x%s" % m.group(2),16)
56                    if ((vma_start >= mem_start) and
57                        (vma_start < (mem_start + mem_len))):
58                        vma_count+=1
59
60            if (('file' not in output) and (vma_max_shift != 0)):
61                expected_vma_count = mem_len >> vma_max_shift
62                if (mem_len % (1 << vma_max_shift)):
63                    expected_vma_count += 1
64            if expected_vma_count != vma_count:
65                raise error.TestFail("VmaCountMismatch")
66            logging.info("%s %s %d %d", hex(mem_start), hex(mem_len), vma_count,
67                         expected_vma_count)
68            if p1.poll() is None:
69                p1.stdin.write("\n")
70                p1.stdin.flush()
71
72        if p1.poll() != 0:
73            raise error.TestFail("Unexpected application abort")
74
75        utils.system('%s/mremaps ' % self.tmpdir  + '100000000')
76