profiler.py 2.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=doc-string-missing
D
dongdaxiang 已提交
15

16 17
import os
import sys
B
barrierye 已提交
18
import logging
19 20 21 22 23 24 25
if sys.version_info.major == 2:
    import Queue
elif sys.version_info.major == 3:
    import queue as Queue
else:
    raise Exception("Error Python version")
import time
B
barrierye 已提交
26
import threading
27

W
wangjiawei04 已提交
28
_LOGGER = logging.getLogger()
B
barrierye 已提交
29

D
dongdaxiang 已提交
30 31 32 33 34 35 36

class TimeProfiler(object):
    def __init__(self):
        self._pid = os.getpid()
        self._print_head = 'PROFILE\tpid:{}\t'.format(self._pid)
        self._time_record = Queue.Queue()
        self._enable = False
B
barrierye 已提交
37
        self._lock = threading.Lock()
D
dongdaxiang 已提交
38 39 40 41 42 43 44

    def enable(self, enable):
        self._enable = enable

    def record(self, name_with_tag):
        if self._enable is False:
            return
B
barrierye 已提交
45
        timestamp = int(round(time.time() * 1000000))
D
dongdaxiang 已提交
46 47 48
        name_with_tag = name_with_tag.split("_")
        tag = name_with_tag[-1]
        name = '_'.join(name_with_tag[:-1])
B
barrierye 已提交
49 50
        with self._lock:
            self._time_record.put((name, tag, timestamp))
D
dongdaxiang 已提交
51 52

    def print_profile(self):
53 54
        if self._enable is False:
            return
B
barrierye 已提交
55 56 57
        sys.stderr.write(self.gen_profile_str())

    def gen_profile_str(self):
D
dongdaxiang 已提交
58 59 60 61
        if self._enable is False:
            return
        print_str = self._print_head
        tmp = {}
B
barrierye 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74
        with self._lock:
            while not self._time_record.empty():
                name, tag, timestamp = self._time_record.get()
                if name in tmp:
                    ptag, ptimestamp = tmp.pop(name)
                    print_str += "{}_{}:{} ".format(name, ptag, ptimestamp)
                    print_str += "{}_{}:{} ".format(name, tag, timestamp)
                else:
                    tmp[name] = (tag, timestamp)
            print_str = "\n{}\n".format(print_str)
            for name, item in tmp.items():
                tag, timestamp = item
                self._time_record.put((name, tag, timestamp))
B
barrierye 已提交
75
            return print_str