valgrindPlugin.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 tdc_config import *
  13. def vp_extract_num_from_string(num_as_string_maybe_with_commas):
  14. return int(num_as_string_maybe_with_commas.replace(',',''))
  15. class SubPlugin(TdcPlugin):
  16. def __init__(self):
  17. self.sub_class = 'valgrind/SubPlugin'
  18. self.tap = ''
  19. super().__init__()
  20. def pre_suite(self, testcount, testidlist):
  21. '''run commands before test_runner goes into a test loop'''
  22. super().pre_suite(testcount, testidlist)
  23. if self.args.verbose > 1:
  24. print('{}.pre_suite'.format(self.sub_class))
  25. if self.args.valgrind:
  26. self._add_to_tap('1..{}\n'.format(self.testcount))
  27. def post_suite(self, index):
  28. '''run commands after test_runner goes into a test loop'''
  29. super().post_suite(index)
  30. self._add_to_tap('\n|---\n')
  31. if self.args.verbose > 1:
  32. print('{}.post_suite'.format(self.sub_class))
  33. print('{}'.format(self.tap))
  34. if self.args.verbose < 4:
  35. subprocess.check_output('rm -f vgnd-*.log', shell=True)
  36. def add_args(self, parser):
  37. super().add_args(parser)
  38. self.argparser_group = self.argparser.add_argument_group(
  39. 'valgrind',
  40. 'options for valgrindPlugin (run command under test under Valgrind)')
  41. self.argparser_group.add_argument(
  42. '-V', '--valgrind', action='store_true',
  43. help='Run commands under valgrind')
  44. return self.argparser
  45. def adjust_command(self, stage, command):
  46. super().adjust_command(stage, command)
  47. cmdform = 'list'
  48. cmdlist = list()
  49. if not self.args.valgrind:
  50. return command
  51. if self.args.verbose > 1:
  52. print('{}.adjust_command'.format(self.sub_class))
  53. if not isinstance(command, list):
  54. cmdform = 'str'
  55. cmdlist = command.split()
  56. else:
  57. cmdlist = command
  58. if stage == 'execute':
  59. if self.args.verbose > 1:
  60. print('adjust_command: stage is {}; inserting valgrind stuff in command [{}] list [{}]'.
  61. format(stage, command, cmdlist))
  62. cmdlist.insert(0, '--track-origins=yes')
  63. cmdlist.insert(0, '--show-leak-kinds=definite,indirect')
  64. cmdlist.insert(0, '--leak-check=full')
  65. cmdlist.insert(0, '--log-file=vgnd-{}.log'.format(self.args.testid))
  66. cmdlist.insert(0, '-v') # ask for summary of non-leak errors
  67. cmdlist.insert(0, ENVIR['VALGRIND_BIN'])
  68. else:
  69. pass
  70. if cmdform == 'str':
  71. command = ' '.join(cmdlist)
  72. else:
  73. command = cmdlist
  74. if self.args.verbose > 1:
  75. print('adjust_command: return command [{}]'.format(command))
  76. return command
  77. def post_execute(self):
  78. if not self.args.valgrind:
  79. return
  80. self.definitely_lost_re = re.compile(
  81. r'definitely lost:\s+([,0-9]+)\s+bytes in\s+([,0-9]+)\sblocks', re.MULTILINE | re.DOTALL)
  82. self.indirectly_lost_re = re.compile(
  83. r'indirectly lost:\s+([,0-9]+)\s+bytes in\s+([,0-9]+)\s+blocks', re.MULTILINE | re.DOTALL)
  84. self.possibly_lost_re = re.compile(
  85. r'possibly lost:\s+([,0-9]+)bytes in\s+([,0-9]+)\s+blocks', re.MULTILINE | re.DOTALL)
  86. self.non_leak_error_re = re.compile(
  87. r'ERROR SUMMARY:\s+([,0-9]+) errors from\s+([,0-9]+)\s+contexts', re.MULTILINE | re.DOTALL)
  88. def_num = 0
  89. ind_num = 0
  90. pos_num = 0
  91. nle_num = 0
  92. # what about concurrent test runs? Maybe force them to be in different directories?
  93. with open('vgnd-{}.log'.format(self.args.testid)) as vfd:
  94. content = vfd.read()
  95. def_mo = self.definitely_lost_re.search(content)
  96. ind_mo = self.indirectly_lost_re.search(content)
  97. pos_mo = self.possibly_lost_re.search(content)
  98. nle_mo = self.non_leak_error_re.search(content)
  99. if def_mo:
  100. def_num = int(def_mo.group(2))
  101. if ind_mo:
  102. ind_num = int(ind_mo.group(2))
  103. if pos_mo:
  104. pos_num = int(pos_mo.group(2))
  105. if nle_mo:
  106. nle_num = int(nle_mo.group(1))
  107. mem_results = ''
  108. if (def_num > 0) or (ind_num > 0) or (pos_num > 0) or (nle_num > 0):
  109. mem_results += 'not '
  110. mem_results += 'ok {} - {}-mem # {}\n'.format(
  111. self.args.test_ordinal, self.args.testid, 'memory leak check')
  112. self._add_to_tap(mem_results)
  113. if mem_results.startswith('not '):
  114. print('{}'.format(content))
  115. self._add_to_tap(content)
  116. def _add_to_tap(self, more_tap_output):
  117. self.tap += more_tap_output