generate_c_testbench.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python
  2. import argparse
  3. import re
  4. import random
  5. import string
  6. def parse_memory_trace(input_file, output_file):
  7. active_memory_requests = {}
  8. malloc_pattern = re.compile(r'dmmlib - m (0x\w+) (\d+)')
  9. realloc_pattern = re.compile(r'dmmlib - r (0x\w+) (0x\w+) (\d+)')
  10. free_pattern = re.compile(r'dmmlib - f (0x\w+)')
  11. for line in input_file:
  12. malloc_match = malloc_pattern.search(line)
  13. if malloc_match:
  14. if malloc_match.group(1) not in active_memory_requests:
  15. active_memory_requests[malloc_match.group(1)] = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(6))
  16. variable_declaration = ''.join([' void *var_', active_memory_requests[malloc_match.group(1)], ';\n'])
  17. output_file.write(variable_declaration)
  18. new_line = ''.join([' var_', active_memory_requests[malloc_match.group(1)], ' = malloc(', malloc_match.group(2), ');\n'])
  19. output_file.write(new_line)
  20. realloc_match = realloc_pattern.search(line)
  21. if realloc_match:
  22. if realloc_match.group(2) not in active_memory_requests:
  23. active_memory_requests[realloc_match.group(2)] = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(6))
  24. variable_declaration = ''.join([' void *var_', active_memory_requests[realloc_match.group(2)], ';\n'])
  25. output_file.write(variable_declaration)
  26. new_line = ''.join([' var_', active_memory_requests[realloc_match.group(2)], ' = realloc(', realloc_match.group(1), ', ', realloc_match.group(3), ');\n'])
  27. output_file.write(new_line)
  28. free_match = free_pattern.search(line)
  29. if free_match:
  30. if free_match.group(1) in active_memory_requests:
  31. new_line = ''.join([' free(var_', active_memory_requests[free_match.group(1)], ');\n'])
  32. del active_memory_requests[free_match.group(1)]
  33. output_file.write(new_line)
  34. else:
  35. print "Warning: free call without an actual allocation beforehands"
  36. input_file.close()
  37. def main():
  38. parser = argparse.ArgumentParser(
  39. description='''Generate a C file of dynamic memory operations from a dmmlib
  40. trace''')
  41. parser.add_argument('tracefile', type=argparse.FileType('rt'),
  42. help='dmmlib trace from the application')
  43. parser.add_argument('-o', '--output', metavar='outputfile', type=argparse.FileType('wt'),
  44. help='generated C file, will use output.c if not available')
  45. try:
  46. args = parser.parse_args()
  47. except IOError, msg:
  48. parser.error(str(msg))
  49. if args.output is None:
  50. args.output = open('output.c', 'wt')
  51. template_file = open('benchmark_template.c', 'rt')
  52. for line in template_file:
  53. if line == '$instructions\n':
  54. parse_memory_trace(args.tracefile, args.output)
  55. else:
  56. args.output.write(line)
  57. args.output.close()
  58. if __name__ == "__main__":
  59. main()