aboutsummaryrefslogtreecommitdiffstats
path: root/library/python/import_tracing/lib/import_tracer.py
blob: ad2391708b75c55aeacd57f453a730b59f9cadbf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import threading
import time
import collections
import library.python.import_tracing.lib.event as events
import library.python.import_tracing.lib.constants as constants


class ImportTracer:
    def __init__(self):
        self.events = collections.OrderedDict()
        self.start_time = time.time()

    def start_event(self, modname, filename, tid=None):
        tid = tid if tid is not None else threading.current_thread().name
        time_from_start = self._get_current_time_from_start()

        new_event = events.Event(
            modname=modname,
            filename=filename,
            tid=tid,
            start_time=time_from_start,
            end_time=None,
        )

        self.events[modname] = new_event

    def finish_event(self, modname, filename, tid=None):
        event = self.events[modname]
        end_time = self._get_current_time_from_start()
        event.end_time = end_time

    def get_events(self, close_not_finished=False):
        end_time = self._get_current_time_from_start()

        for event in self.events.values():
            if close_not_finished and event.end_time is None:
                yield events.Event(
                    modname=event.modname,
                    filename=event.filename,
                    tid=event.tid,
                    start_time=event.start_time,
                    end_time=end_time,
                )
            else:
                yield event

    def _get_current_time_from_start(self):
        return (time.time() - self.start_time) * constants.MCS_IN_SEC