Linux Audio

Check our new training course

Loading...
v6.2
  1'''
  2run the command under test, under valgrind and collect memory leak info
  3as a separate test.
  4'''
  5
  6
  7import os
  8import re
  9import signal
 10from string import Template
 11import subprocess
 12import time
 13from TdcPlugin import TdcPlugin
 14from TdcResults import *
 15
 16from tdc_config import *
 17
 18def vp_extract_num_from_string(num_as_string_maybe_with_commas):
 19    return int(num_as_string_maybe_with_commas.replace(',',''))
 20
 21class SubPlugin(TdcPlugin):
 22    def __init__(self):
 23        self.sub_class = 'valgrind/SubPlugin'
 24        self.tap = ''
 25        self._tsr = TestSuiteReport()
 26        super().__init__()
 27
 28    def pre_suite(self, testcount, testidlist):
 29        '''run commands before test_runner goes into a test loop'''
 30        super().pre_suite(testcount, testidlist)
 31        if self.args.verbose > 1:
 32            print('{}.pre_suite'.format(self.sub_class))
 33        if self.args.valgrind:
 34            self._add_to_tap('1..{}\n'.format(self.testcount))
 35
 36    def post_suite(self, index):
 37        '''run commands after test_runner goes into a test loop'''
 38        super().post_suite(index)
 
 39        if self.args.verbose > 1:
 40            print('{}.post_suite'.format(self.sub_class))
 41        #print('{}'.format(self.tap))
 42        for xx in range(index - 1, self.testcount):
 43            res = TestResult('{}-mem'.format(self.testidlist[xx]), 'Test skipped')
 44            res.set_result(ResultState.skip)
 45            res.set_errormsg('Skipped because of prior setup/teardown failure')
 46            self._add_results(res)
 47        if self.args.verbose < 4:
 48            subprocess.check_output('rm -f vgnd-*.log', shell=True)
 49
 50    def add_args(self, parser):
 51        super().add_args(parser)
 52        self.argparser_group = self.argparser.add_argument_group(
 53            'valgrind',
 54            'options for valgrindPlugin (run command under test under Valgrind)')
 55
 56        self.argparser_group.add_argument(
 57            '-V', '--valgrind', action='store_true',
 58            help='Run commands under valgrind')
 59
 60        return self.argparser
 61
 62    def adjust_command(self, stage, command):
 63        super().adjust_command(stage, command)
 64        cmdform = 'list'
 65        cmdlist = list()
 66
 67        if not self.args.valgrind:
 68            return command
 69
 70        if self.args.verbose > 1:
 71            print('{}.adjust_command'.format(self.sub_class))
 72
 73        if not isinstance(command, list):
 74            cmdform = 'str'
 75            cmdlist = command.split()
 76        else:
 77            cmdlist = command
 78
 79        if stage == 'execute':
 80            if self.args.verbose > 1:
 81                print('adjust_command:  stage is {}; inserting valgrind stuff in command [{}] list [{}]'.
 82                      format(stage, command, cmdlist))
 83            cmdlist.insert(0, '--track-origins=yes')
 84            cmdlist.insert(0, '--show-leak-kinds=definite,indirect')
 85            cmdlist.insert(0, '--leak-check=full')
 86            cmdlist.insert(0, '--log-file=vgnd-{}.log'.format(self.args.testid))
 87            cmdlist.insert(0, '-v')  # ask for summary of non-leak errors
 88            cmdlist.insert(0, ENVIR['VALGRIND_BIN'])
 89        else:
 90            pass
 91
 92        if cmdform == 'str':
 93            command = ' '.join(cmdlist)
 94        else:
 95            command = cmdlist
 96
 97        if self.args.verbose > 1:
 98            print('adjust_command:  return command [{}]'.format(command))
 99        return command
100
101    def post_execute(self):
102        if not self.args.valgrind:
103            return
104
105        res = TestResult('{}-mem'.format(self.args.testid),
106              '{} memory leak check'.format(self.args.test_name))
107        if self.args.test_skip:
108            res.set_result(ResultState.skip)
109            res.set_errormsg('Test case designated as skipped.')
110            self._add_results(res)
111            return
112
113        self.definitely_lost_re = re.compile(
114            r'definitely lost:\s+([,0-9]+)\s+bytes in\s+([,0-9]+)\sblocks', re.MULTILINE | re.DOTALL)
115        self.indirectly_lost_re = re.compile(
116            r'indirectly lost:\s+([,0-9]+)\s+bytes in\s+([,0-9]+)\s+blocks', re.MULTILINE | re.DOTALL)
117        self.possibly_lost_re = re.compile(
118            r'possibly lost:\s+([,0-9]+)bytes in\s+([,0-9]+)\s+blocks', re.MULTILINE | re.DOTALL)
119        self.non_leak_error_re = re.compile(
120            r'ERROR SUMMARY:\s+([,0-9]+) errors from\s+([,0-9]+)\s+contexts', re.MULTILINE | re.DOTALL)
121
122        def_num = 0
123        ind_num = 0
124        pos_num = 0
125        nle_num = 0
126
127        # what about concurrent test runs?  Maybe force them to be in different directories?
128        with open('vgnd-{}.log'.format(self.args.testid)) as vfd:
129            content = vfd.read()
130            def_mo = self.definitely_lost_re.search(content)
131            ind_mo = self.indirectly_lost_re.search(content)
132            pos_mo = self.possibly_lost_re.search(content)
133            nle_mo = self.non_leak_error_re.search(content)
134
135            if def_mo:
136                def_num = int(def_mo.group(2))
137            if ind_mo:
138                ind_num = int(ind_mo.group(2))
139            if pos_mo:
140                pos_num = int(pos_mo.group(2))
141            if nle_mo:
142                nle_num = int(nle_mo.group(1))
143
144        mem_results = ''
145        if (def_num > 0) or (ind_num > 0) or (pos_num > 0) or (nle_num > 0):
146            mem_results += 'not '
147            res.set_result(ResultState.fail)
148            res.set_failmsg('Memory leak detected')
149            res.append_failmsg(content)
150        else:
151            res.set_result(ResultState.success)
152
153        self._add_results(res)
154
155
156    def _add_results(self, res):
157        self._tsr.add_resultdata(res)
 
 
 
 
158
159    def _add_to_tap(self, more_tap_output):
160        self.tap += more_tap_output
v4.17
  1'''
  2run the command under test, under valgrind and collect memory leak info
  3as a separate test.
  4'''
  5
  6
  7import os
  8import re
  9import signal
 10from string import Template
 11import subprocess
 12import time
 13from TdcPlugin import TdcPlugin
 
 14
 15from tdc_config import *
 16
 17def vp_extract_num_from_string(num_as_string_maybe_with_commas):
 18    return int(num_as_string_maybe_with_commas.replace(',',''))
 19
 20class SubPlugin(TdcPlugin):
 21    def __init__(self):
 22        self.sub_class = 'valgrind/SubPlugin'
 23        self.tap = ''
 
 24        super().__init__()
 25
 26    def pre_suite(self, testcount, testidlist):
 27        '''run commands before test_runner goes into a test loop'''
 28        super().pre_suite(testcount, testidlist)
 29        if self.args.verbose > 1:
 30            print('{}.pre_suite'.format(self.sub_class))
 31        if self.args.valgrind:
 32            self._add_to_tap('1..{}\n'.format(self.testcount))
 33
 34    def post_suite(self, index):
 35        '''run commands after test_runner goes into a test loop'''
 36        super().post_suite(index)
 37        self._add_to_tap('\n|---\n')
 38        if self.args.verbose > 1:
 39            print('{}.post_suite'.format(self.sub_class))
 40        print('{}'.format(self.tap))
 
 
 
 
 
 41        if self.args.verbose < 4:
 42            subprocess.check_output('rm -f vgnd-*.log', shell=True)
 43
 44    def add_args(self, parser):
 45        super().add_args(parser)
 46        self.argparser_group = self.argparser.add_argument_group(
 47            'valgrind',
 48            'options for valgrindPlugin (run command under test under Valgrind)')
 49
 50        self.argparser_group.add_argument(
 51            '-V', '--valgrind', action='store_true',
 52            help='Run commands under valgrind')
 53
 54        return self.argparser
 55
 56    def adjust_command(self, stage, command):
 57        super().adjust_command(stage, command)
 58        cmdform = 'list'
 59        cmdlist = list()
 60
 61        if not self.args.valgrind:
 62            return command
 63
 64        if self.args.verbose > 1:
 65            print('{}.adjust_command'.format(self.sub_class))
 66
 67        if not isinstance(command, list):
 68            cmdform = 'str'
 69            cmdlist = command.split()
 70        else:
 71            cmdlist = command
 72
 73        if stage == 'execute':
 74            if self.args.verbose > 1:
 75                print('adjust_command:  stage is {}; inserting valgrind stuff in command [{}] list [{}]'.
 76                      format(stage, command, cmdlist))
 77            cmdlist.insert(0, '--track-origins=yes')
 78            cmdlist.insert(0, '--show-leak-kinds=definite,indirect')
 79            cmdlist.insert(0, '--leak-check=full')
 80            cmdlist.insert(0, '--log-file=vgnd-{}.log'.format(self.args.testid))
 81            cmdlist.insert(0, '-v')  # ask for summary of non-leak errors
 82            cmdlist.insert(0, ENVIR['VALGRIND_BIN'])
 83        else:
 84            pass
 85
 86        if cmdform == 'str':
 87            command = ' '.join(cmdlist)
 88        else:
 89            command = cmdlist
 90
 91        if self.args.verbose > 1:
 92            print('adjust_command:  return command [{}]'.format(command))
 93        return command
 94
 95    def post_execute(self):
 96        if not self.args.valgrind:
 97            return
 98
 
 
 
 
 
 
 
 
 99        self.definitely_lost_re = re.compile(
100            r'definitely lost:\s+([,0-9]+)\s+bytes in\s+([,0-9]+)\sblocks', re.MULTILINE | re.DOTALL)
101        self.indirectly_lost_re = re.compile(
102            r'indirectly lost:\s+([,0-9]+)\s+bytes in\s+([,0-9]+)\s+blocks', re.MULTILINE | re.DOTALL)
103        self.possibly_lost_re = re.compile(
104            r'possibly lost:\s+([,0-9]+)bytes in\s+([,0-9]+)\s+blocks', re.MULTILINE | re.DOTALL)
105        self.non_leak_error_re = re.compile(
106            r'ERROR SUMMARY:\s+([,0-9]+) errors from\s+([,0-9]+)\s+contexts', re.MULTILINE | re.DOTALL)
107
108        def_num = 0
109        ind_num = 0
110        pos_num = 0
111        nle_num = 0
112
113        # what about concurrent test runs?  Maybe force them to be in different directories?
114        with open('vgnd-{}.log'.format(self.args.testid)) as vfd:
115            content = vfd.read()
116            def_mo = self.definitely_lost_re.search(content)
117            ind_mo = self.indirectly_lost_re.search(content)
118            pos_mo = self.possibly_lost_re.search(content)
119            nle_mo = self.non_leak_error_re.search(content)
120
121            if def_mo:
122                def_num = int(def_mo.group(2))
123            if ind_mo:
124                ind_num = int(ind_mo.group(2))
125            if pos_mo:
126                pos_num = int(pos_mo.group(2))
127            if nle_mo:
128                nle_num = int(nle_mo.group(1))
129
130        mem_results = ''
131        if (def_num > 0) or (ind_num > 0) or (pos_num > 0) or (nle_num > 0):
132            mem_results += 'not '
 
 
 
 
 
 
 
 
133
134        mem_results += 'ok {} - {}-mem # {}\n'.format(
135            self.args.test_ordinal, self.args.testid, 'memory leak check')
136        self._add_to_tap(mem_results)
137        if mem_results.startswith('not '):
138            print('{}'.format(content))
139            self._add_to_tap(content)
140
141    def _add_to_tap(self, more_tap_output):
142        self.tap += more_tap_output