generate_c_testbench.py 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. memalign_pattern = re.compile(r'dmmlib - ma (0x\w+) (\d+) (\d+)')
  11. free_pattern = re.compile(r'dmmlib - f (0x\w+)')
  12. for line in input_file:
  13. malloc_match = malloc_pattern.search(line)
  14. if malloc_match:
  15. if malloc_match.group(1) not in active_memory_requests:
  16. active_memory_requests[malloc_match.group(1)] = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(6))
  17. variable_declaration = ''.join([' void *var_', active_memory_requests[malloc_match.group(1)], ';\n'])
  18. output_file.write(variable_declaration)
  19. new_line = ''.join([' var_', active_memory_requests[malloc_match.group(1)], ' = malloc(', malloc_match.group(2), ');\n'])
  20. output_file.write(new_line)
  21. new_line = ''.join([' var_', active_memory_requests[malloc_match.group(1)], ' = memset(var_', active_memory_requests[malloc_match.group(1)], ', 0, ', malloc_match.group(2), ');\n'])
  22. output_file.write(new_line)
  23. realloc_match = realloc_pattern.search(line)
  24. if realloc_match:
  25. if realloc_match.group(2) not in active_memory_requests:
  26. active_memory_requests[realloc_match.group(2)] = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(6))
  27. variable_declaration = ''.join([' void *var_', active_memory_requests[realloc_match.group(2)], ';\n'])
  28. output_file.write(variable_declaration)
  29. new_line = ''.join([' var_', active_memory_requests[realloc_match.group(2)], ' = realloc(', realloc_match.group(1), ', ', realloc_match.group(3), ');\n'])
  30. output_file.write(new_line)
  31. new_line = ''.join([' var_', active_memory_requests[realloc_match.group(2)], ' = memset(var_', active_memory_requests[realloc_match.group(2)], ', 0, ', realloc_match.group(3), ');\n'])
  32. output_file.write(new_line)
  33. memalign_match = memalign_pattern.search(line)
  34. if memalign_match:
  35. if memalign_match.group(1) not in active_memory_requests:
  36. active_memory_requests[memalign_match.group(1)] = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(6))
  37. variable_declaration = ''.join([' void *var_', active_memory_requests[memalign_match.group(1)], ';\n'])
  38. output_file.write(variable_declaration)
  39. new_line = ''.join([' posix_memalign(&var_', active_memory_requests[memalign_match.group(1)], ', ', memalign_match.group(2), ', ', memalign_match.group(3), ');\n'])
  40. output_file.write(new_line)
  41. new_line = ''.join([' var_', active_memory_requests[memalign_match.group(1)], ' = memset(var_', active_memory_requests[memalign_match.group(1)], ', 0, ', memalign_match.group(3), ');\n'])
  42. output_file.write(new_line)
  43. free_match = free_pattern.search(line)
  44. if free_match:
  45. if free_match.group(1) in active_memory_requests:
  46. new_line = ''.join([' free(var_', active_memory_requests[free_match.group(1)], ');\n'])
  47. del active_memory_requests[free_match.group(1)]
  48. output_file.write(new_line)
  49. else:
  50. print "Warning: free call without an actual allocation beforehands"
  51. input_file.close()
  52. def main():
  53. parser = argparse.ArgumentParser(
  54. description='''Generate a C file of dynamic memory operations from a dmmlib
  55. trace''')
  56. parser.add_argument('tracefile', type=argparse.FileType('rt'),
  57. help='dmmlib trace from the application')
  58. parser.add_argument('-o', '--output', metavar='outputfile', type=argparse.FileType('wt'),
  59. help='generated C file, will use output.c if not available')
  60. try:
  61. args = parser.parse_args()
  62. except IOError, msg:
  63. parser.error(str(msg))
  64. if args.output is None:
  65. args.output = open('output.c', 'wt')
  66. template_file = open('benchmark_template.c', 'rt')
  67. for line in template_file:
  68. if line == '$instructions\n':
  69. parse_memory_trace(args.tracefile, args.output)
  70. else:
  71. args.output.write(line)
  72. args.output.close()
  73. if __name__ == "__main__":
  74. main()