import_tracer.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import threading
  2. import time
  3. import collections
  4. import library.python.import_tracing.lib.event as events
  5. import library.python.import_tracing.lib.constants as constants
  6. class ImportTracer:
  7. def __init__(self):
  8. self.events = collections.OrderedDict()
  9. self.start_time = time.time()
  10. def start_event(self, modname, filename, tid=None):
  11. tid = tid if tid is not None else threading.current_thread().name
  12. time_from_start = self._get_current_time_from_start()
  13. new_event = events.Event(
  14. modname=modname,
  15. filename=filename,
  16. tid=tid,
  17. start_time=time_from_start,
  18. end_time=None,
  19. )
  20. self.events[modname] = new_event
  21. def finish_event(self, modname, filename, tid=None):
  22. event = self.events[modname]
  23. end_time = self._get_current_time_from_start()
  24. event.end_time = end_time
  25. def get_events(self, close_not_finished=False):
  26. end_time = self._get_current_time_from_start()
  27. for event in self.events.values():
  28. if close_not_finished and event.end_time is None:
  29. yield events.Event(
  30. modname=event.modname,
  31. filename=event.filename,
  32. tid=event.tid,
  33. start_time=event.start_time,
  34. end_time=end_time,
  35. )
  36. else:
  37. yield event
  38. def _get_current_time_from_start(self):
  39. return (time.time() - self.start_time) * constants.MCS_IN_SEC