blob: ac9cc3728b5b8f543e229756aff96f97a83ba81a [file] [log] [blame]
lmr4c607f22009-06-02 11:50:38 +00001import os, subprocess, re, commands, logging
mbligh53da18e2009-01-05 21:13:26 +00002from autotest_lib.client.bin import utils, test
3from autotest_lib.client.common_lib import error
mbligh2b8cd192008-11-20 17:27:17 +00004
5class memory_api(test.test):
mbligh1ef218d2009-08-03 16:57:56 +00006 version = 1
mbligh2b8cd192008-11-20 17:27:17 +00007
mbligh1ef218d2009-08-03 16:57:56 +00008 def setup(self):
Eric Li8a12e802011-02-17 14:24:13 -08009 os.mkdir(self.tmpdir)
10 utils.system("%s %s -o %s" %
11 (utils.get_cc(),
12 os.path.join(self.bindir, "memory_api.c"),
mbligh1ef218d2009-08-03 16:57:56 +000013 os.path.join(self.tmpdir, "memory_api")))
Eric Li8a12e802011-02-17 14:24:13 -080014 utils.system("%s %s -o %s" %
15 (utils.get_cc(),
16 os.path.join(self.bindir, "mremaps.c"),
mbligh1ef218d2009-08-03 16:57:56 +000017 os.path.join(self.tmpdir, "mremaps")))
mbligh2b8cd192008-11-20 17:27:17 +000018
19
mbligh1ef218d2009-08-03 16:57:56 +000020 def initialize(self):
21 self.job.require_gcc()
mbligh2b8cd192008-11-20 17:27:17 +000022
23
mbligh1ef218d2009-08-03 16:57:56 +000024 def run_once(self, memsize = "1000000000", args=''):
mbligh2b8cd192008-11-20 17:27:17 +000025
mbligh1ef218d2009-08-03 16:57:56 +000026 vma_re = re.compile("([0-9,a-f]+)-([0-9,a-f]+)")
27 memory_re = re.compile("(\d+) bytes @(0x[0-9,a-f]+)")
mbligh2b8cd192008-11-20 17:27:17 +000028
mbligh1ef218d2009-08-03 16:57:56 +000029 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
mbligh2b8cd192008-11-20 17:27:17 +000041 if m:
mbligh1ef218d2009-08-03 16:57:56 +000042 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
mbligh2b8cd192008-11-20 17:27:17 +000059
mbligh1ef218d2009-08-03 16:57:56 +000060 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()
mbligh2b8cd192008-11-20 17:27:17 +000071
mbligh1ef218d2009-08-03 16:57:56 +000072 if p1.poll() != 0:
73 raise error.TestFail("Unexpected application abort")
mbligh2b8cd192008-11-20 17:27:17 +000074
mbligh1ef218d2009-08-03 16:57:56 +000075 utils.system('%s/mremaps ' % self.tmpdir + '100000000')