lmr | 4c607f2 | 2009-06-02 11:50:38 +0000 | [diff] [blame] | 1 | import os, subprocess, re, commands, logging |
mbligh | 53da18e | 2009-01-05 21:13:26 +0000 | [diff] [blame] | 2 | from autotest_lib.client.bin import utils, test |
| 3 | from autotest_lib.client.common_lib import error |
mbligh | 2b8cd19 | 2008-11-20 17:27:17 +0000 | [diff] [blame] | 4 | |
| 5 | class memory_api(test.test): |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 6 | version = 1 |
mbligh | 2b8cd19 | 2008-11-20 17:27:17 +0000 | [diff] [blame] | 7 | |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 8 | def setup(self): |
Eric Li | 8a12e80 | 2011-02-17 14:24:13 -0800 | [diff] [blame] | 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"), |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 13 | os.path.join(self.tmpdir, "memory_api"))) |
Eric Li | 8a12e80 | 2011-02-17 14:24:13 -0800 | [diff] [blame] | 14 | utils.system("%s %s -o %s" % |
| 15 | (utils.get_cc(), |
| 16 | os.path.join(self.bindir, "mremaps.c"), |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 17 | os.path.join(self.tmpdir, "mremaps"))) |
mbligh | 2b8cd19 | 2008-11-20 17:27:17 +0000 | [diff] [blame] | 18 | |
| 19 | |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 20 | def initialize(self): |
| 21 | self.job.require_gcc() |
mbligh | 2b8cd19 | 2008-11-20 17:27:17 +0000 | [diff] [blame] | 22 | |
| 23 | |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 24 | def run_once(self, memsize = "1000000000", args=''): |
mbligh | 2b8cd19 | 2008-11-20 17:27:17 +0000 | [diff] [blame] | 25 | |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 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]+)") |
mbligh | 2b8cd19 | 2008-11-20 17:27:17 +0000 | [diff] [blame] | 28 | |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 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 |
mbligh | 2b8cd19 | 2008-11-20 17:27:17 +0000 | [diff] [blame] | 41 | if m: |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 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 |
mbligh | 2b8cd19 | 2008-11-20 17:27:17 +0000 | [diff] [blame] | 59 | |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 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() |
mbligh | 2b8cd19 | 2008-11-20 17:27:17 +0000 | [diff] [blame] | 71 | |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 72 | if p1.poll() != 0: |
| 73 | raise error.TestFail("Unexpected application abort") |
mbligh | 2b8cd19 | 2008-11-20 17:27:17 +0000 | [diff] [blame] | 74 | |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 75 | utils.system('%s/mremaps ' % self.tmpdir + '100000000') |