valgrindPlugin.py 5.6 KB

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