event_node.h 11.0 KB
Newer Older
1
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

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. */

#pragma once

#include <functional>
#include <list>
#include <map>
#include <string>
#include <vector>

#include "paddle/fluid/platform/enforce.h"
C
chenjian 已提交
24
#include "paddle/fluid/platform/place.h"
25 26 27 28 29 30
#include "paddle/fluid/platform/profiler/output_logger.h"
#include "paddle/fluid/platform/profiler/trace_event.h"

namespace paddle {
namespace platform {

C
chenjian 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
class MemTraceEventNode {
 public:
  // constructor
  explicit MemTraceEventNode(const MemTraceEvent& mem_event)
      : mem_event_(mem_event) {}

  // destructor
  ~MemTraceEventNode();

  // getter
  TracerMemEventType Type() const { return mem_event_.type; }
  uint64_t Addr() const { return mem_event_.addr; }
  uint64_t TimeStampNs() const { return mem_event_.timestamp_ns; }
  uint64_t ProcessId() const { return mem_event_.process_id; }
  uint64_t ThreadId() const { return mem_event_.thread_id; }
  int64_t IncreaseBytes() const { return mem_event_.increase_bytes; }
  std::string Place() const { return mem_event_.place; }
  uint64_t CurrentAllocated() const { return mem_event_.current_allocated; }
  uint64_t CurrentReserved() const { return mem_event_.current_reserved; }
50 51
  uint64_t PeakAllocated() const { return mem_event_.peak_allocated; }
  uint64_t PeakReserved() const { return mem_event_.peak_reserved; }
C
chenjian 已提交
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

  // member function
  void LogMe(BaseLogger* logger) { logger->LogMemTraceEventNode(*this); }

 private:
  // data
  MemTraceEvent mem_event_;
};

class OperatorSupplementEventNode {
 public:
  // constructor
  explicit OperatorSupplementEventNode(
      const OperatorSupplementEvent& op_supplement_event)
      : op_supplement_event_(op_supplement_event) {}
  // destructor
  ~OperatorSupplementEventNode() {}
  // getter
  std::string Name() const { return op_supplement_event_.op_type; }
  uint64_t TimeStampNs() const { return op_supplement_event_.timestamp_ns; }
  std::map<std::string, std::vector<std::vector<int64_t>>>& InputShapes() {
    return op_supplement_event_.input_shapes;
  }
  std::map<std::string, std::vector<std::string>>& Dtypes() {
    return op_supplement_event_.dtypes;
  }
  std::string CallStack() { return op_supplement_event_.callstack; }
  uint64_t ProcessId() const { return op_supplement_event_.process_id; }
  uint64_t ThreadId() const { return op_supplement_event_.thread_id; }

 private:
  // data
  OperatorSupplementEvent op_supplement_event_;
};

87 88 89 90 91 92 93 94
class DeviceTraceEventNode {
 public:
  // constructor
  explicit DeviceTraceEventNode(const DeviceTraceEvent& device_event)
      : device_event_(device_event) {}
  // destructor
  ~DeviceTraceEventNode() {}
  // getter
95 96 97 98 99 100 101 102
  std::string Name() const { return device_event_.name; }
  TracerEventType Type() const { return device_event_.type; }
  uint64_t StartNs() const { return device_event_.start_ns; }
  uint64_t EndNs() const { return device_event_.end_ns; }
  uint64_t DeviceId() const { return device_event_.device_id; }
  uint64_t ContextId() const { return device_event_.context_id; }
  uint64_t StreamId() const { return device_event_.stream_id; }
  uint64_t Duration() const {
103 104
    return device_event_.end_ns - device_event_.start_ns;
  }
105 106
  uint32_t CorrelationId() const { return device_event_.correlation_id; }
  KernelEventInfo KernelInfo() const {
107 108 109 110 111 112 113
    PADDLE_ENFORCE_EQ(
        device_event_.type, TracerEventType::Kernel,
        platform::errors::Unavailable(
            "Can not kernel_info, "
            "TracerEventType in node must be TracerEventType::Kernel."));
    return device_event_.kernel_info;
  }
114
  MemcpyEventInfo MemcpyInfo() const {
115 116 117 118 119 120 121
    PADDLE_ENFORCE_EQ(
        device_event_.type, TracerEventType::Memcpy,
        platform::errors::Unavailable(
            "Can not get memcpy_info, "
            "TracerEventType in node must be TracerEventType::Memcpy."));
    return device_event_.memcpy_info;
  }
122
  MemsetEventInfo MemsetInfo() const {
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    PADDLE_ENFORCE_EQ(
        device_event_.type, TracerEventType::Memset,
        platform::errors::Unavailable(
            "Can not get memset_info, "
            "TracerEventType in node must be TracerEventType::Memset."));
    return device_event_.memset_info;
  }

  // member function
  void LogMe(BaseLogger* logger) { logger->LogDeviceTraceEventNode(*this); }

 private:
  // data
  DeviceTraceEvent device_event_;
};

class CudaRuntimeTraceEventNode {
 public:
  // constructor
  explicit CudaRuntimeTraceEventNode(const RuntimeTraceEvent& runtime_event)
      : runtime_event_(runtime_event) {}
  // destructor
  ~CudaRuntimeTraceEventNode();
  // getter
147 148 149 150 151 152 153
  std::string Name() const { return runtime_event_.name; }
  TracerEventType Type() const { return runtime_event_.type; }
  uint64_t StartNs() const { return runtime_event_.start_ns; }
  uint64_t EndNs() const { return runtime_event_.end_ns; }
  uint64_t ProcessId() const { return runtime_event_.process_id; }
  uint64_t ThreadId() const { return runtime_event_.thread_id; }
  uint64_t Duration() const {
154 155
    return runtime_event_.end_ns - runtime_event_.start_ns;
  }
156 157
  uint32_t CorrelationId() const { return runtime_event_.correlation_id; }
  uint32_t CallbackId() const { return runtime_event_.callback_id; }
158 159 160 161 162
  // member function
  void AddDeviceTraceEventNode(DeviceTraceEventNode* node) {
    device_node_ptrs_.push_back(node);
  }
  void LogMe(BaseLogger* logger) { logger->LogRuntimeTraceEventNode(*this); }
L
liutiexing 已提交
163
  const std::vector<DeviceTraceEventNode*>& GetDeviceTraceEventNodes() const {
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    return device_node_ptrs_;
  }

 private:
  // data
  RuntimeTraceEvent runtime_event_;
  // device events called by this
  std::vector<DeviceTraceEventNode*> device_node_ptrs_;
};

class HostTraceEventNode {
 public:
  // constructor
  explicit HostTraceEventNode(const HostTraceEvent& host_event)
      : host_event_(host_event) {}

  // destructor
  ~HostTraceEventNode();

  // getter
184 185 186 187 188 189 190
  std::string Name() const { return host_event_.name; }
  TracerEventType Type() const { return host_event_.type; }
  uint64_t StartNs() const { return host_event_.start_ns; }
  uint64_t EndNs() const { return host_event_.end_ns; }
  uint64_t ProcessId() const { return host_event_.process_id; }
  uint64_t ThreadId() const { return host_event_.thread_id; }
  uint64_t Duration() const {
191 192 193 194 195 196 197 198
    return host_event_.end_ns - host_event_.start_ns;
  }

  // member function
  void AddChild(HostTraceEventNode* node) { children_.push_back(node); }
  void AddCudaRuntimeNode(CudaRuntimeTraceEventNode* node) {
    runtime_node_ptrs_.push_back(node);
  }
C
chenjian 已提交
199 200 201 202
  void AddMemNode(MemTraceEventNode* node) { mem_node_ptrs_.push_back(node); }
  void SetOperatorSupplementNode(OperatorSupplementEventNode* node) {
    op_supplement_node_ptr_ = node;
  }
L
liutiexing 已提交
203 204 205 206 207
  const std::vector<HostTraceEventNode*>& GetChildren() const {
    return children_;
  }
  const std::vector<CudaRuntimeTraceEventNode*>& GetRuntimeTraceEventNodes()
      const {
208 209
    return runtime_node_ptrs_;
  }
C
chenjian 已提交
210 211 212 213 214 215 216 217
  const std::vector<MemTraceEventNode*>& GetMemTraceEventNodes() const {
    return mem_node_ptrs_;
  }

  OperatorSupplementEventNode* GetOperatorSupplementEventNode() const {
    return op_supplement_node_ptr_;
  }

218 219 220 221 222 223 224 225 226
  void LogMe(BaseLogger* logger) { logger->LogHostTraceEventNode(*this); }

 private:
  // data
  HostTraceEvent host_event_;
  // cuda runtime events called by this
  std::vector<CudaRuntimeTraceEventNode*> runtime_node_ptrs_;
  // host events called by this
  std::vector<HostTraceEventNode*> children_;
C
chenjian 已提交
227 228 229
  // memory events happened in this event period
  std::vector<MemTraceEventNode*> mem_node_ptrs_;
  OperatorSupplementEventNode* op_supplement_node_ptr_ = nullptr;
230 231 232 233 234 235 236
};

class NodeTrees {
 public:
  // constructor
  NodeTrees(const std::list<HostTraceEvent>& host_events,
            const std::list<RuntimeTraceEvent>& runtime_events,
C
chenjian 已提交
237 238 239
            const std::list<DeviceTraceEvent>& device_events,
            const std::list<MemTraceEvent>& mem_events,
            const std::list<OperatorSupplementEvent>& op_supplement_events) {
240 241 242
    std::vector<HostTraceEventNode*> host_event_nodes;
    std::vector<CudaRuntimeTraceEventNode*> runtime_event_nodes;
    std::vector<DeviceTraceEventNode*> device_event_nodes;
C
chenjian 已提交
243 244
    std::vector<MemTraceEventNode*> mem_event_nodes;
    std::vector<OperatorSupplementEventNode*> op_supplement_event_nodes;
245 246 247 248 249 250 251 252 253 254
    // encapsulate event into nodes
    for (auto it = host_events.begin(); it != host_events.end(); ++it) {
      host_event_nodes.push_back(new HostTraceEventNode(*it));
    }
    for (auto it = runtime_events.begin(); it != runtime_events.end(); ++it) {
      runtime_event_nodes.push_back(new CudaRuntimeTraceEventNode(*it));
    }
    for (auto it = device_events.begin(); it != device_events.end(); ++it) {
      device_event_nodes.push_back(new DeviceTraceEventNode(*it));
    }
C
chenjian 已提交
255 256 257 258 259 260 261
    for (auto it = mem_events.begin(); it != mem_events.end(); ++it) {
      mem_event_nodes.push_back(new MemTraceEventNode(*it));
    }
    for (auto it = op_supplement_events.begin();
         it != op_supplement_events.end(); ++it) {
      op_supplement_event_nodes.push_back(new OperatorSupplementEventNode(*it));
    }
262
    // build tree
C
chenjian 已提交
263 264
    BuildTrees(host_event_nodes, runtime_event_nodes, device_event_nodes,
               mem_event_nodes, op_supplement_event_nodes);
265 266 267 268 269 270 271 272 273 274 275 276
  }

  explicit NodeTrees(
      const std::map<uint64_t, HostTraceEventNode*>& thread_event_trees_map)
      : thread_event_trees_map_(thread_event_trees_map) {}

  // destructor
  ~NodeTrees();

  void LogMe(BaseLogger* logger);
  void HandleTrees(std::function<void(HostTraceEventNode*)>,
                   std::function<void(CudaRuntimeTraceEventNode*)>,
C
chenjian 已提交
277 278 279
                   std::function<void(DeviceTraceEventNode*)>,
                   std::function<void(MemTraceEventNode*)>,
                   std::function<void(OperatorSupplementEventNode*)>);
L
liutiexing 已提交
280
  const std::map<uint64_t, HostTraceEventNode*>& GetNodeTrees() const {
281 282 283 284 285 286 287
    return thread_event_trees_map_;
  }
  std::map<uint64_t, std::vector<HostTraceEventNode*>> Traverse(bool bfs) const;

 private:
  std::map<uint64_t, HostTraceEventNode*> thread_event_trees_map_;
  void BuildTrees(const std::vector<HostTraceEventNode*>&,
C
chenjian 已提交
288 289 290 291
                  const std::vector<CudaRuntimeTraceEventNode*>&,
                  const std::vector<DeviceTraceEventNode*>&,
                  const std::vector<MemTraceEventNode*>&,
                  const std::vector<OperatorSupplementEventNode*>&);
292 293
  HostTraceEventNode* BuildTreeRelationship(
      std::vector<HostTraceEventNode*> host_event_nodes,
C
chenjian 已提交
294 295 296
      std::vector<CudaRuntimeTraceEventNode*> runtime_event_nodes,
      std::vector<MemTraceEventNode*> mem_event_nodes,
      std::vector<OperatorSupplementEventNode*> op_supplement_event_nodes);
297 298 299 300
};

}  // namespace platform
}  // namespace paddle