memory_peaks.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import sqlite3
  2. db_path = 'benchmarks/Results/Agents/Memory/HO.sqlite'
  3. # db_path = 'benchmarks/Results/PyPDEVS/Memory/parallel/HI.sqlite'
  4. # db_path = 'benchmarks/Results/PyPDEVS/Memory/minimal/LI.sqlite'
  5. process_name = 'julia'
  6. memory_unit = "MB" # Options: Bytes, KB, MB, GB, KiB, MiB, GiB
  7. time_unit = "s" # Options: ms, s, m, h
  8. min_prominence_pages = 250
  9. PAGE_SIZE = 4096
  10. TIME_UNITS = {"ms": 0.001, "s": 1, "m": 60, "h": 3600}
  11. MEMORY_UNITS = {
  12. "Bytes": 1, "KB": 1000, "MB": 1000 ** 2, "GB": 1000 ** 3,
  13. "KiB": 1024, "MiB": 1024 ** 2, "GiB": 1024 ** 3
  14. }
  15. def find_local_maxima(timestamps, values, min_prominence):
  16. """Detects local maxima across flat plateaus in pure, JIT-friendly Python."""
  17. peaks = []
  18. n = len(values)
  19. if n < 3:
  20. return peaks
  21. plateau_start_idx = None
  22. is_climbing = False
  23. for i in range(1, n - 1):
  24. prev_v = values[i - 1]
  25. curr_v = values[i]
  26. next_v = values[i + 1]
  27. if curr_v > prev_v:
  28. is_climbing = True
  29. plateau_start_idx = i
  30. elif curr_v < prev_v:
  31. is_climbing = False
  32. if is_climbing and curr_v > next_v:
  33. peak_idx = plateau_start_idx if plateau_start_idx is not None else i
  34. if curr_v - prev_v >= min_prominence or curr_v - next_v >= min_prominence:
  35. peaks.append(peak_idx)
  36. is_climbing = False
  37. plateau_start_idx = None
  38. elif curr_v == next_v:
  39. pass
  40. else:
  41. plateau_start_idx = None
  42. last_idx = n - 1
  43. last_v = values[last_idx]
  44. penultimate_v = values[last_idx - 1]
  45. # actively climbing until end
  46. if last_v > penultimate_v:
  47. if last_v - penultimate_v >= min_prominence:
  48. peaks.append(last_idx)
  49. # climbed into a plateau
  50. elif last_v == penultimate_v and is_climbing and plateau_start_idx is not None:
  51. base_v = values[plateau_start_idx - 1]
  52. if last_v - base_v >= min_prominence:
  53. peaks.append(plateau_start_idx)
  54. return peaks
  55. return peaks
  56. conn = sqlite3.connect(db_path)
  57. cursor = conn.cursor()
  58. query = """
  59. SELECT ts, stat_rss
  60. FROM record
  61. WHERE stat_comm = ?
  62. ORDER BY ts; \
  63. """
  64. cursor.execute(query, (process_name,))
  65. rows = cursor.fetchall()
  66. conn.close()
  67. if not rows:
  68. print(f"No records found for process '{process_name}'.")
  69. else:
  70. timestamps = [r[0] for r in rows]
  71. rss_values = [r[1] for r in rows]
  72. start_time = timestamps[0]
  73. peak_indices = find_local_maxima(timestamps, rss_values, min_prominence=min_prominence_pages)
  74. print(f"--- Peak RSS Report for '{process_name}' ---")
  75. print(f"Time Unit: {time_unit} (Relative to launch) | Memory Unit: {memory_unit}\n")
  76. for idx in peak_indices:
  77. raw_ts = timestamps[idx]
  78. raw_rss = rss_values[idx]
  79. relative_time = (raw_ts - start_time) / TIME_UNITS[time_unit]
  80. converted_mem = (raw_rss * PAGE_SIZE) / MEMORY_UNITS[memory_unit]
  81. print(f"Time: {relative_time:7.3f} {time_unit} | Peak Memory: {converted_mem:7.2f} {memory_unit}")