generate_stack_usage_report.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import os
  2. from pathlib import Path
  3. def collect_stack_usage_info():
  4. stack_usage_data = []
  5. for root, _, files in os.walk("./build/package/pikascript/CMakeFiles/pikascript-core.dir/pikascript-core"):
  6. for file in files:
  7. if file.endswith(".su"):
  8. su_path = Path(root) / file
  9. # print(f"Processing file: {su_path}")
  10. with open(su_path) as f:
  11. for line in f.readlines():
  12. # '/root/pikascript/port/linux/package/pikascript/pikascript-lib/pika_libc/pika_vsnprintf.c:184:38:get_bit_access'
  13. items = line.strip().split()
  14. if len(items) == 3:
  15. location, stack_size, storage_type = items[0], items[1], items[2]
  16. location_items = location.split(":")
  17. if len(location_items) == 4:
  18. file_name, line_number, column_number, function = location_items
  19. else:
  20. print(
  21. f"Skipping line due to incorrect format: {line.strip()}")
  22. continue
  23. stack_usage_data.append(
  24. (file_name, function, int(stack_size), storage_type))
  25. return stack_usage_data
  26. def generate_report(stack_usage_data):
  27. sorted_data = sorted(stack_usage_data, key=lambda x: x[2], reverse=True)
  28. report = "Stack usage report (sorted by stack size):\n\n"
  29. report += "{:<10} {:<40} {:<15}\n".format(
  30. "Stack Size", "Function", "File")
  31. report += "-" * 75 + "\n"
  32. for file_name, function, stack_size, storage_type in sorted_data:
  33. report += "{:<10} {:<40} {:<15}\n".format(
  34. stack_size, function, os.path.basename(file_name))
  35. return report
  36. if __name__ == "__main__":
  37. stack_usage_data = collect_stack_usage_info()
  38. report = generate_report(stack_usage_data)
  39. with open("stack_usage_report.txt", "w") as f:
  40. f.write(report)