profiler.py 6.1 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
if sys.version_info.major == 2:
    import Queue
elif sys.version_info.major == 3:
    import queue as Queue
else:
    raise Exception("Error Python version")
B
barriery 已提交
25
from time import time as _time
B
barriery 已提交
26
import time
B
barrierye 已提交
27
import threading
B
barriery 已提交
28
import multiprocessing
29

W
wangjiawei04 已提交
30
_LOGGER = logging.getLogger()
B
barrierye 已提交
31

D
dongdaxiang 已提交
32

B
barriery 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
class PerformanceTracer(object):
    def __init__(self, interval_s=1):
        self._data_buffer = multiprocessing.Manager().Queue()
        self._interval_s = interval_s
        self._proc = None
        self._channels = []
        self._trace_filename = os.path.join("PipelineServingLogs", "INDEX.log")

    def data_buffer(self):
        return self._data_buffer

    def start(self):
        self._proc = multiprocessing.Process(
            target=self._trace_func, args=(self._channels, ))
        self._proc.daemon = True
        self._proc.start()

    def set_channels(self, channels):
        self._channels = channels

    def _trace_func(self, channels):
        trace_file = open(self._trace_filename, "a")
        actions = ["prep", "midp", "postp"]
        tag_dict = {}
        while True:
            op_cost = {}
            trace_file.write("==========================")

            # op
            while not self._data_buffer.empty():
                name, action, stage, timestamp = self._data_buffer.get()
                tag = "{}_{}".format(name, action)
                if tag in tag_dict:
                    assert stage == 1
                    start_timestamp = tag_dict.pop(tag)
                    cost = timestamp - start_timestamp
                    if name not in op_cost:
                        op_cost[name] = {}
                    if action not in op_cost[name]:
                        op_cost[name][action] = []
                    op_cost[name][action].append(cost)
                else:
                    assert stage == 0
                    tag_dict[tag] = timestamp

            for name in op_cost:
                tot_cost, cal_cost = 0.0, 0.0
                for action, costs in op_cost[name].items():
                    op_cost[name][action] = sum(costs) / (1e3 * len(costs))
                    tot_cost += op_cost[name][action]

                msg = ", ".join([
                    "{}[{} ms]".format(action, cost)
                    for action, cost in op_cost[name].items()
                ])

                for action in actions:
                    if action in op_cost[name]:
                        cal_cost += op_cost[name][action]

                trace_file.write("Op({}) {}".format(name, msg))
                if name != "DAG":
                    trace_file.write("Op({}) idle[{}]".format(
                        name, 1 - 1.0 * cal_cost / tot_cost))

            # channel
            for channel in channels:
                trace_file.write("Channel({}) size[{}]".format(channel.name,
                                                               channel.size()))
            time.sleep(self._interval_s)


B
barriery 已提交
105
class UnsafeTimeProfiler(object):
106 107
    """ thread unsafe profiler """

B
barriery 已提交
108 109 110 111 112 113 114 115 116 117 118 119
    def __init__(self):
        self.pid = os.getpid()
        self.print_head = 'PROFILE\tpid:{}\t'.format(self.pid)
        self.time_record = [self.print_head]
        self._enable = False

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

    def record(self, name):
        if self._enable is False:
            return
B
barriery 已提交
120 121 122
        timestamp = int(round(_time() * 1000000))
        self.time_record.append('{}:{} '.format(name, timestamp))
        return timestamp
B
barriery 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

    def print_profile(self):
        if self._enable is False:
            return
        sys.stderr.write(self.gen_profile_str())

    def gen_profile_str(self):
        if self._enable is False:
            return
        self.time_record.append('\n')
        profile_str = ''.join(self.time_record)
        self.time_record = [self.print_head]
        return profile_str


D
dongdaxiang 已提交
138 139 140 141 142 143
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 已提交
144
        self._lock = threading.Lock()
D
dongdaxiang 已提交
145 146 147 148 149 150 151

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

    def record(self, name_with_tag):
        if self._enable is False:
            return
B
barriery 已提交
152
        timestamp = int(round(_time() * 1000000))
D
dongdaxiang 已提交
153 154 155
        name_with_tag = name_with_tag.split("_")
        tag = name_with_tag[-1]
        name = '_'.join(name_with_tag[:-1])
B
barrierye 已提交
156 157
        with self._lock:
            self._time_record.put((name, tag, timestamp))
B
barriery 已提交
158
        return timestamp
D
dongdaxiang 已提交
159 160

    def print_profile(self):
161 162
        if self._enable is False:
            return
B
barrierye 已提交
163 164 165
        sys.stderr.write(self.gen_profile_str())

    def gen_profile_str(self):
D
dongdaxiang 已提交
166 167 168 169
        if self._enable is False:
            return
        print_str = self._print_head
        tmp = {}
B
barrierye 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182
        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 已提交
183
            return print_str