periodic-stats.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """
  2. periodic-stats.py
  3. Periodically write out all statistics
  4. 1st argument is the interval size in nanoseconds (default is 1e9 = 1 second of simulated time)
  5. 2rd argument, if present will limit the number of snapshots and dynamically remove itermediate data
  6. """
  7. import sim
  8. class PeriodicStats:
  9. def setup(self, args):
  10. args = dict(enumerate((args or '').split(':')))
  11. interval = long(args.get(0, '') or 1000000000)
  12. self.max_snapshots = long(args.get(1, 0))
  13. self.num_snapshots = 0
  14. self.interval = long(interval * sim.util.Time.NS)
  15. self.next_interval = float('inf')
  16. self.in_roi = False
  17. sim.util.Every(self.interval, self.periodic, roi_only = True)
  18. def hook_roi_begin(self):
  19. self.in_roi = True
  20. self.next_interval = sim.stats.time() + self.interval
  21. sim.stats.write('periodic-0')
  22. def hook_roi_end(self):
  23. self.next_interval = float('inf')
  24. self.in_roi = False
  25. def periodic(self, time, time_delta):
  26. if self.max_snapshots and self.num_snapshots > self.max_snapshots:
  27. self.num_snapshots /= 2
  28. for t in range(self.interval, time, self.interval * 2):
  29. sim.util.db_delete('periodic-%d' % t)
  30. self.interval *= 2
  31. if time >= self.next_interval:
  32. self.num_snapshots += 1
  33. sim.stats.write('periodic-%d' % (self.num_snapshots * self.interval))
  34. self.next_interval += self.interval
  35. sim.util.register(PeriodicStats())