generate_c_testbench.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env python
  2. import argparse
  3. import re
  4. import random
  5. import string
  6. def parse_memory_trace(input_file, stress_level, 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. if stress_level > 0:
  22. 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'])
  23. output_file.write(new_line)
  24. if stress_level > 1:
  25. new_line = ''.join([' get_raw_blocks(&systemallocator);\n'])
  26. output_file.write(new_line)
  27. realloc_match = realloc_pattern.search(line)
  28. if realloc_match:
  29. if realloc_match.group(2) not in active_memory_requests:
  30. active_memory_requests[realloc_match.group(2)] = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(6))
  31. variable_declaration = ''.join([' void *var_', active_memory_requests[realloc_match.group(2)], ';\n'])
  32. output_file.write(variable_declaration)
  33. new_line = ''.join([' var_',
  34. active_memory_requests[realloc_match.group(2)],
  35. ' = realloc(var_',
  36. active_memory_requests[realloc_match.group(1)], ', ',
  37. realloc_match.group(3), ');\n'])
  38. output_file.write(new_line)
  39. if stress_level > 0:
  40. new_line = ''.join([' var_',
  41. active_memory_requests[realloc_match.group(2)],
  42. ' = memset(var_',
  43. active_memory_requests[realloc_match.group(2)], ', 0, ',
  44. realloc_match.group(3), ');\n'])
  45. output_file.write(new_line)
  46. if stress_level > 1:
  47. new_line = ''.join([' get_raw_blocks(&systemallocator);\n'])
  48. output_file.write(new_line)
  49. memalign_match = memalign_pattern.search(line)
  50. if memalign_match:
  51. if memalign_match.group(1) not in active_memory_requests:
  52. active_memory_requests[memalign_match.group(1)] = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(6))
  53. variable_declaration = ''.join([' void *var_', active_memory_requests[memalign_match.group(1)], ';\n'])
  54. output_file.write(variable_declaration)
  55. new_line = ''.join([' posix_memalign(&var_', active_memory_requests[memalign_match.group(1)], ', ', memalign_match.group(2), ', ', memalign_match.group(3), ');\n'])
  56. output_file.write(new_line)
  57. if stress_level > 0:
  58. 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'])
  59. output_file.write(new_line)
  60. if stress_level > 1:
  61. new_line = ''.join([' get_raw_blocks(&systemallocator);\n'])
  62. output_file.write(new_line)
  63. free_match = free_pattern.search(line)
  64. if free_match:
  65. if free_match.group(1) in active_memory_requests:
  66. new_line = ''.join([' free(var_', active_memory_requests[free_match.group(1)], ');\n'])
  67. del active_memory_requests[free_match.group(1)]
  68. output_file.write(new_line)
  69. if stress_level > 1:
  70. new_line = ''.join([' get_raw_blocks(&systemallocator);\n'])
  71. output_file.write(new_line)
  72. else:
  73. print "Warning: free call without an actual allocation beforehands"
  74. input_file.close()
  75. def main():
  76. parser = argparse.ArgumentParser(
  77. description='''Generate a C file of dynamic memory operations from a
  78. dmmlib trace''')
  79. parser.add_argument('tracefile', type=argparse.FileType('rt'),
  80. help='dmmlib trace from the application')
  81. parser.add_argument('-s', '--stress', metavar='stresslevel',
  82. type=int,
  83. help='stress level, 0 for none, 1 for data and 2 for data and metadata')
  84. parser.add_argument('-o', '--output', metavar='outputfile',
  85. type=argparse.FileType('wt'),
  86. help='generated C file, will use output.c if not available')
  87. try:
  88. args = parser.parse_args()
  89. except IOError, msg:
  90. parser.error(str(msg))
  91. if args.output is None:
  92. args.output = open('output.c', 'wt')
  93. if args.stress is None:
  94. args.stress = 0
  95. if args.stress < 0:
  96. args.stress = 0
  97. if args.stress > 2:
  98. args.stress = 2
  99. template_file = open('benchmark_template.c', 'rt')
  100. for line in template_file:
  101. if line == '$instructions\n':
  102. parse_memory_trace(args.tracefile, args.stress, args.output)
  103. else:
  104. args.output.write(line)
  105. args.output.close()
  106. if __name__ == "__main__":
  107. main()