Skip to content

Commit 13978a6

Browse files
committed
fix: Fix inf loop in logger
1 parent ce2b49a commit 13978a6

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

src/simulator/diffraction_generator.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,29 +77,34 @@ def __init__(self, total: int, label: str, step_pct: float = 10.0):
7777
logging.info(f"{self.label}: 0% complete (0/{self.total}) | ips=0.00 avg_ips=0.00")
7878

7979
def update(self, n: int = 1) -> None:
80+
# Increment and clamp to total to avoid pct > 100
8081
self.completed += n
82+
if self.completed > self.total:
83+
self.completed = self.total
84+
8185
pct = (self.completed / self.total) * 100.0
82-
# compute next threshold based on tick index, guard at 100%
83-
threshold = min(100.0, (self._tick_idx + 1) * self.step_pct)
84-
while pct >= threshold:
86+
87+
max_tick = int(min(100.0, pct) // self.step_pct)
88+
if max_tick > self._tick_idx:
89+
# Log only the latest crossed threshold
90+
capped = min(100.0, max_tick * self.step_pct)
91+
8592
now = time.monotonic()
8693
dt_inst = max(now - self._last_time, 1e-9)
8794
dt_avg = max(now - self._start_time, 1e-9)
8895
inst_ips = (self.completed - self._last_count) / dt_inst
8996
avg_ips = self.completed / dt_avg
90-
# choose percent formatting based on step size
91-
if self.step_pct >= 1.0:
92-
pct_str = f"{int(threshold)}%"
93-
else:
94-
pct_str = f"{threshold:.1f}%"
97+
98+
pct_str = f"{capped:.1f}%"
99+
95100
logging.info(
96101
f"{self.label}: {pct_str} complete ({self.completed}/{self.total}) | ips={inst_ips:.2f} avg_ips={avg_ips:.2f}"
97102
)
98-
# reset window
103+
104+
# Reset window and advance tick index to latest crossed
99105
self._last_time = now
100106
self._last_count = self.completed
101-
self._tick_idx += 1
102-
threshold = min(100.0, (self._tick_idx + 1) * self.step_pct)
107+
self._tick_idx = max_tick
103108

104109
class DiffractionGenerator:
105110
"""

0 commit comments

Comments
 (0)