trace_event.h 11.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2022 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. */

#pragma once

C
chenjian 已提交
17
#include <map>
18
#include <string>
C
chenjian 已提交
19
#include <vector>
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

namespace paddle {
namespace platform {

enum class TracerEventType {
  // Used to mark operator record
  Operator = 0,
  // Used to mark dataloader record
  Dataloader = 1,
  // Used to mark profile step record
  ProfileStep = 2,
  // Used to mark cuda runtime record returned by cupti
  CudaRuntime = 3,
  // Used to mark kernel computation record returned by cupti
  Kernel = 4,
  // Used to mark memcpy record returned by cupti
  Memcpy = 5,
  // Used to mark memset record returned by cupti
  Memset = 6,
  // Used to mark record defined by user
  UserDefined = 7,
C
chenjian 已提交
41 42 43 44 45 46 47 48 49 50 51 52
  // Used to mark operator detail, (such as infer shape, compute)
  OperatorInner = 8,
  // Used to mark model training or testing perspective, forward process
  Forward = 9,
  // Used to mark model training perspective, backward process
  Backward = 10,
  // Used to mark model training perspective, optimization process
  Optimization = 11,
  // Used to mark distributed training perspective
  Communication = 12,
  // Used to mark python api
  PythonOp = 13,
C
chenjian 已提交
53 54
  // Used to mark python level userdefined
  PythonUserDefined = 14,
F
fwenguang 已提交
55 56
  // Used to mark mlu runtime record returned by cnpapi
  MluRuntime = 15,
57 58 59 60
  // A flag to denote the number of current types
  NumTypes
};

C
chenjian 已提交
61
enum class TracerMemEventType {
62
  // Used to mark memory allocation which is managed by paddle
C
chenjian 已提交
63
  Allocate = 0,
64
  // Used to mark memory free which is managed by paddle
C
chenjian 已提交
65
  Free = 1,
66 67 68 69
  // Used to mark reserved memory allocation which is applied from device.
  ReservedAllocate = 2,
  // Used to mark reserved memory free which is released to device.
  ReservedFree = 3,
C
chenjian 已提交
70 71 72 73
  // A flag to denote the number of current types
  NumTypes
};

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 105 106 107
struct KernelEventInfo {
  // The X-dimension block size for the kernel.
  uint32_t block_x;
  // The Y-dimension block size for the kernel.
  uint32_t block_y;
  // The Z-dimension grid size for the kernel.
  uint32_t block_z;
  // X-dimension of a grid.
  uint32_t grid_x;
  // Y-dimension of a grid.
  uint32_t grid_y;
  // Z-dimension of a grid.
  uint32_t grid_z;
  // The dynamic shared memory reserved for the kernel, in bytes.
  uint32_t dynamic_shared_memory;
  // The static shared memory allocated for the kernel, in bytes.
  uint32_t static_shared_memory;
  // The number of registers required for each thread executing the kernel.
  uint32_t registers_per_thread;
  // The amount of local memory reserved for each thread, in bytes.
  uint32_t local_memory_per_thread;
  // The total amount of local memory reserved for the kernel, in bytes.
  uint32_t local_memory_total;
  // The timestamp when the kernel is queued up in the command buffer, in ns.
  // This timestamp is not collected by default. Use API
  // cuptiActivityEnableLatencyTimestamps() to enable collection.
  uint64_t queued;
  // The timestamp when the command buffer containing the kernel launch is
  // submitted to the GPU, in ns.
  // This timestamp is not collected by default. Use API
  // cuptiActivityEnableLatencyTimestamps() to enable collection.
  uint64_t submitted;
  // The completed timestamp for the kernel execution, in ns.
  uint64_t completed;
108 109 110 111 112

  float blocks_per_sm;
  float warps_per_sm;
  // theoretical achieved occupancy
  float occupancy;
113 114
};

115 116
static constexpr size_t kMemKindMaxLen = 50;

117 118 119 120 121 122
struct MemcpyEventInfo {
  // The number of bytes transferred by the memory copy.
  uint64_t num_bytes;
  // The kind of the memory copy.
  // Each kind represents the source and destination targets of a memory copy.
  // Targets are host, device, and array. Refer to CUpti_ActivityMemcpyKind
123
  char copy_kind[kMemKindMaxLen];
124 125 126
  // The source memory kind read by the memory copy.
  // Each kind represents the type of the memory accessed by a memory
  // operation/copy. Refer to CUpti_ActivityMemoryKind
127
  char src_kind[kMemKindMaxLen];
128
  // The destination memory kind read by the memory copy.
129
  char dst_kind[kMemKindMaxLen];
130 131 132 133 134 135
};

struct MemsetEventInfo {
  // The number of bytes being set by the memory set.
  uint64_t num_bytes;
  // The memory kind of the memory set. Refer to CUpti_ActivityMemoryKind
136
  char memory_kind[kMemKindMaxLen];
137 138 139 140
  // the value being assigned to memory by the memory set.
  uint32_t value;
};

C
chenjian 已提交
141 142 143
struct OperatorSupplementEvent {
  OperatorSupplementEvent() = default;
  OperatorSupplementEvent(
144 145
      uint64_t timestamp_ns,
      const std::string& op_type,
C
chenjian 已提交
146 147 148
      const std::map<std::string, std::vector<std::vector<int64_t>>>&
          input_shapes,
      const std::map<std::string, std::vector<std::string>>& dtypes,
149 150 151
      const std::string& callstack,
      uint64_t process_id,
      uint64_t thread_id)
C
chenjian 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
      : timestamp_ns(timestamp_ns),
        op_type(op_type),
        input_shapes(input_shapes),
        dtypes(dtypes),
        callstack(callstack),
        process_id(process_id),
        thread_id(thread_id) {}
  // timestamp of the record
  uint64_t timestamp_ns;
  // op type name
  std::string op_type;
  // input shapes
  std::map<std::string, std::vector<std::vector<int64_t>>> input_shapes;
  std::map<std::string, std::vector<std::string>> dtypes;
  // call stack
  std::string callstack;
  // process id of the record
  uint64_t process_id;
  // thread id of the record
  uint64_t thread_id;
};

174 175
struct HostTraceEvent {
  HostTraceEvent() = default;
176 177 178 179 180
  HostTraceEvent(const std::string& name,
                 TracerEventType type,
                 uint64_t start_ns,
                 uint64_t end_ns,
                 uint64_t process_id,
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
                 uint64_t thread_id)
      : name(name),
        type(type),
        start_ns(start_ns),
        end_ns(end_ns),
        process_id(process_id),
        thread_id(thread_id) {}
  // record name
  std::string name;
  // record type, one of TracerEventType
  TracerEventType type;
  // start timestamp of the record
  uint64_t start_ns;
  // end timestamp of the record
  uint64_t end_ns;
  // process id of the record
  uint64_t process_id;
  // thread id of the record
  uint64_t thread_id;
};

struct RuntimeTraceEvent {
  RuntimeTraceEvent() = default;
204 205 206 207 208 209 210
  RuntimeTraceEvent(const std::string& name,
                    uint64_t start_ns,
                    uint64_t end_ns,
                    uint64_t process_id,
                    uint64_t thread_id,
                    uint32_t correlation_id,
                    uint32_t callback_id)
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
      : name(name),
        start_ns(start_ns),
        end_ns(end_ns),
        process_id(process_id),
        thread_id(thread_id),
        correlation_id(correlation_id),
        callback_id(callback_id) {}

  // record name
  std::string name;
  // record type, one of TracerEventType
  TracerEventType type{TracerEventType::CudaRuntime};
  // start timestamp of the record
  uint64_t start_ns;
  // end timestamp of the record
  uint64_t end_ns;
  // process id of the record
  uint64_t process_id;
  // thread id of the record
  uint64_t thread_id;
  // correlation id, used for correlating async activities happened on device
  uint32_t correlation_id;
  // callback id, used to identify which cuda runtime api is called
  uint32_t callback_id;
};

struct DeviceTraceEvent {
  DeviceTraceEvent() = default;
239 240 241 242 243 244 245 246 247
  DeviceTraceEvent(const std::string& name,
                   TracerEventType type,
                   uint64_t start_ns,
                   uint64_t end_ns,
                   uint64_t device_id,
                   uint64_t context_id,
                   uint64_t stream_id,
                   uint32_t correlation_id,
                   const KernelEventInfo& kernel_info)
248 249 250 251 252 253 254 255 256
      : name(name),
        type(type),
        start_ns(start_ns),
        end_ns(end_ns),
        device_id(device_id),
        context_id(context_id),
        stream_id(stream_id),
        correlation_id(correlation_id),
        kernel_info(kernel_info) {}
257 258 259 260 261 262 263 264 265
  DeviceTraceEvent(const std::string& name,
                   TracerEventType type,
                   uint64_t start_ns,
                   uint64_t end_ns,
                   uint64_t device_id,
                   uint64_t context_id,
                   uint64_t stream_id,
                   uint32_t correlation_id,
                   const MemcpyEventInfo& memcpy_info)
266 267 268 269 270 271 272 273 274
      : name(name),
        type(type),
        start_ns(start_ns),
        end_ns(end_ns),
        device_id(device_id),
        context_id(context_id),
        stream_id(stream_id),
        correlation_id(correlation_id),
        memcpy_info(memcpy_info) {}
275 276 277 278 279 280 281 282 283
  DeviceTraceEvent(const std::string& name,
                   TracerEventType type,
                   uint64_t start_ns,
                   uint64_t end_ns,
                   uint64_t device_id,
                   uint64_t context_id,
                   uint64_t stream_id,
                   uint32_t correlation_id,
                   const MemsetEventInfo& memset_info)
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
      : name(name),
        type(type),
        start_ns(start_ns),
        end_ns(end_ns),
        device_id(device_id),
        context_id(context_id),
        stream_id(stream_id),
        correlation_id(correlation_id),
        memset_info(memset_info) {}
  // record name
  std::string name;
  // record type, one of TracerEventType
  TracerEventType type;
  // start timestamp of the record
  uint64_t start_ns;
  // end timestamp of the record
  uint64_t end_ns;
  // device id
  uint64_t device_id;
  // context id
  uint64_t context_id;
  // stream id
  uint64_t stream_id;
  // correlation id, used for correlating async activities happened on device
  uint32_t correlation_id;
  // union, specific device record type has different detail information
  union {
    // used for TracerEventType::Kernel
    KernelEventInfo kernel_info;
    // used for TracerEventType::Memcpy
    MemcpyEventInfo memcpy_info;
    // used for TracerEventType::Memset
    MemsetEventInfo memset_info;
  };
};

C
chenjian 已提交
320 321
struct MemTraceEvent {
  MemTraceEvent() = default;
322 323 324 325 326 327 328 329 330 331
  MemTraceEvent(uint64_t timestamp_ns,
                uint64_t addr,
                TracerMemEventType type,
                uint64_t process_id,
                uint64_t thread_id,
                int64_t increase_bytes,
                const std::string& place,
                uint64_t current_allocated,
                uint64_t current_reserved,
                uint64_t peak_allocated,
332
                uint64_t peak_reserved)
C
chenjian 已提交
333 334 335 336 337 338 339 340
      : timestamp_ns(timestamp_ns),
        addr(addr),
        type(type),
        process_id(process_id),
        thread_id(thread_id),
        increase_bytes(increase_bytes),
        place(place),
        current_allocated(current_allocated),
341 342 343
        current_reserved(current_reserved),
        peak_allocated(peak_allocated),
        peak_reserved(peak_reserved) {}
C
chenjian 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

  // timestamp of the record
  uint64_t timestamp_ns;
  // memory addr of allocation or free
  uint64_t addr;
  // memory manipulation type
  TracerMemEventType type;
  // process id of the record
  uint64_t process_id;
  // thread id of the record
  uint64_t thread_id;
  // increase bytes after this manipulation, allocation for sign +, free for
  // sign -
  int64_t increase_bytes;
  // place
  std::string place;
  // current total allocated memory
  uint64_t current_allocated;
  // current total reserved memory
  uint64_t current_reserved;
364 365 366 367
  // current peak allocated memory
  uint64_t peak_allocated;
  // current peak reserved memory
  uint64_t peak_reserved;
C
chenjian 已提交
368 369
};

370 371
}  // namespace platform
}  // namespace paddle