stats.h 9.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* 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

#include <atomic>
#include <map>
#include <string>
20

21 22 23
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/errors.h"
#include "paddle/fluid/platform/macros.h"
24
#include "paddle/phi/common/thread_data_registry.h"
L
Leo Chen 已提交
25
#include "paddle/utils/string/string_helper.h"
26 27 28 29

namespace paddle {
namespace memory {

30
using phi::ThreadDataRegistry;
31 32 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

struct ThreadLocalStatBase {
  int64_t current{0};
  int64_t peak{0};
};

class StatBase {
 public:
  StatBase() = default;
  virtual ~StatBase() = default;

  virtual int64_t GetCurrentValue() = 0;
  virtual int64_t GetPeakValue() = 0;
  virtual void Update(int64_t) = 0;

 private:
  DISABLE_COPY_AND_ASSIGN(StatBase);
};

template <typename ThreadLocalStatType>
class Stat : public StatBase {
 public:
  static Stat* GetInstance() {
    static Stat instance;
    return &instance;
  }

  int64_t GetCurrentValue() override {
    std::unordered_map<uint64_t, ThreadLocalStatType> thread_local_stats =
        ThreadDataRegistry<ThreadLocalStatType>::GetInstance()
            .GetAllThreadDataByValue();
    int64_t current_value = 0;
    for (auto pair : thread_local_stats) {
      current_value += pair.second.current;
    }
    return current_value;
  }

  int64_t GetPeakValue() override { return peak_value_; }

  void Update(int64_t increment) override {
    auto& thread_data_registry =
        ThreadDataRegistry<ThreadLocalStatType>::GetInstance();
    ThreadLocalStatType* thread_local_stat =
        thread_data_registry.GetMutableCurrentThreadData();
    thread_local_stat->current += increment;

L
Leo Chen 已提交
78 79 80 81 82 83 84
    VLOG(8) << string::split_string(
                   phi::enforce::demangle(typeid(*thread_local_stat).name()),
                   "::")
                   .back()
            << ": Update current_value with " << increment
            << ", after update, current value = " << GetCurrentValue();

85 86 87 88 89 90 91
    if (thread_local_stat->current > thread_local_stat->peak) {
      thread_local_stat->peak = thread_local_stat->current;
      int64_t current_value = GetCurrentValue();
      int64_t prev_value = peak_value_;
      while (prev_value < current_value &&
             !peak_value_.compare_exchange_weak(prev_value, current_value)) {
      }
L
Leo Chen 已提交
92 93 94 95 96 97 98
      VLOG(8) << string::split_string(
                     phi::enforce::demangle(typeid(*thread_local_stat).name()),
                     "::")
                     .back()
              << ": Update current_value with " << increment
              << ", after update, peak_value = " << peak_value_.load()
              << " , current value = " << current_value;
99 100 101 102 103 104 105 106 107
    }
  }

 private:
  Stat() {}
  ~Stat() {}
  std::atomic<int64_t> peak_value_{0};
};

108 109 110 111 112 113 114
// xxxMemoryStatCurrentValue, xxxMemoryStatPeakValue and xxxMemoryStatUpdate
// support to operate STAT values by a string, however, they has worse
// performance than the macro function xxx_MEMORY_STAT_CURRENT_VALUE,
// xxx_MEMORY_STAT_PEAK_VALUE, and xxx_MEMORY_STAT_UPDATE. Try to use the macro
// functions where ultra-low performance overhead is required.
int64_t DeviceMemoryStatCurrentValue(const std::string& stat_type, int dev_id);
int64_t DeviceMemoryStatPeakValue(const std::string& stat_type, int dev_id);
115 116
void DeviceMemoryStatUpdate(const std::string& stat_type,
                            int dev_id,
117 118 119 120
                            int64_t increment);

int64_t HostMemoryStatCurrentValue(const std::string& stat_type, int dev_id);
int64_t HostMemoryStatPeakValue(const std::string& stat_type, int dev_id);
121 122
void HostMemoryStatUpdate(const std::string& stat_type,
                          int dev_id,
123 124 125 126 127 128
                          int64_t increment);

#define DEVICE_MEMORY_STAT_FUNC_SWITHCH_CASE(item, id)              \
  case id:                                                          \
    stat = paddle::memory::Stat<                                    \
        paddle::memory::DeviceMemoryStat##item##id>::GetInstance(); \
129 130
    break

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
#define DEVICE_MEMORY_STAT_FUNC(item, id, func, ...)                          \
  [&] {                                                                       \
    paddle::memory::StatBase* stat = nullptr;                                 \
    switch (id) {                                                             \
      DEVICE_MEMORY_STAT_FUNC_SWITHCH_CASE(item, 0);                          \
      DEVICE_MEMORY_STAT_FUNC_SWITHCH_CASE(item, 1);                          \
      DEVICE_MEMORY_STAT_FUNC_SWITHCH_CASE(item, 2);                          \
      DEVICE_MEMORY_STAT_FUNC_SWITHCH_CASE(item, 3);                          \
      DEVICE_MEMORY_STAT_FUNC_SWITHCH_CASE(item, 4);                          \
      DEVICE_MEMORY_STAT_FUNC_SWITHCH_CASE(item, 5);                          \
      DEVICE_MEMORY_STAT_FUNC_SWITHCH_CASE(item, 6);                          \
      DEVICE_MEMORY_STAT_FUNC_SWITHCH_CASE(item, 7);                          \
      DEVICE_MEMORY_STAT_FUNC_SWITHCH_CASE(item, 8);                          \
      DEVICE_MEMORY_STAT_FUNC_SWITHCH_CASE(item, 9);                          \
      DEVICE_MEMORY_STAT_FUNC_SWITHCH_CASE(item, 10);                         \
      DEVICE_MEMORY_STAT_FUNC_SWITHCH_CASE(item, 11);                         \
      DEVICE_MEMORY_STAT_FUNC_SWITHCH_CASE(item, 12);                         \
      DEVICE_MEMORY_STAT_FUNC_SWITHCH_CASE(item, 13);                         \
      DEVICE_MEMORY_STAT_FUNC_SWITHCH_CASE(item, 14);                         \
      DEVICE_MEMORY_STAT_FUNC_SWITHCH_CASE(item, 15);                         \
      default:                                                                \
        PADDLE_THROW(paddle::platform::errors::OutOfRange(                    \
            "Only support device id between [0, 15] for device memory stats," \
            "not support device id: %d",                                      \
            id));                                                             \
        break;                                                                \
    }                                                                         \
    return stat->func(__VA_ARGS__);                                           \
159
  }()
160

161 162 163 164 165 166 167
#define DEVICE_MEMORY_STAT_CURRENT_VALUE(item, id) \
  DEVICE_MEMORY_STAT_FUNC(item, id, GetCurrentValue)
#define DEVICE_MEMORY_STAT_PEAK_VALUE(item, id) \
  DEVICE_MEMORY_STAT_FUNC(item, id, GetPeakValue)
#define DEVICE_MEMORY_STAT_UPDATE(item, id, increment) \
  DEVICE_MEMORY_STAT_FUNC(item, id, Update, increment)

168 169
#define HOST_MEMORY_STAT_FUNC(item, id, func, ...)                     \
  [&] {                                                                \
170 171
    PADDLE_ENFORCE_EQ(id,                                              \
                      0,                                               \
172 173 174 175 176 177 178
                      paddle::platform::errors::OutOfRange(            \
                          "Only support device id 0 for host memory "  \
                          "stats, not support device id: %d",          \
                          id));                                        \
    return paddle::memory::Stat<                                       \
               paddle::memory::HostMemoryStat##item##0>::GetInstance() \
        ->func(__VA_ARGS__);                                           \
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
  }()

#define HOST_MEMORY_STAT_CURRENT_VALUE(item, id) \
  HOST_MEMORY_STAT_FUNC(item, id, GetCurrentValue)
#define HOST_MEMORY_STAT_PEAK_VALUE(item, id) \
  HOST_MEMORY_STAT_FUNC(item, id, GetPeakValue)
#define HOST_MEMORY_STAT_UPDATE(item, id, increment) \
  HOST_MEMORY_STAT_FUNC(item, id, Update, increment)

#define DEVICE_MEMORY_STAT_DECLARE_WITH_ID(item, id) \
  struct DeviceMemoryStat##item##id : public ThreadLocalStatBase {}

#define DEVICE_MEMORY_STAT_DECLARE(item)        \
  DEVICE_MEMORY_STAT_DECLARE_WITH_ID(item, 0);  \
  DEVICE_MEMORY_STAT_DECLARE_WITH_ID(item, 1);  \
  DEVICE_MEMORY_STAT_DECLARE_WITH_ID(item, 2);  \
  DEVICE_MEMORY_STAT_DECLARE_WITH_ID(item, 3);  \
  DEVICE_MEMORY_STAT_DECLARE_WITH_ID(item, 4);  \
  DEVICE_MEMORY_STAT_DECLARE_WITH_ID(item, 5);  \
  DEVICE_MEMORY_STAT_DECLARE_WITH_ID(item, 6);  \
  DEVICE_MEMORY_STAT_DECLARE_WITH_ID(item, 7);  \
  DEVICE_MEMORY_STAT_DECLARE_WITH_ID(item, 8);  \
  DEVICE_MEMORY_STAT_DECLARE_WITH_ID(item, 9);  \
  DEVICE_MEMORY_STAT_DECLARE_WITH_ID(item, 10); \
  DEVICE_MEMORY_STAT_DECLARE_WITH_ID(item, 11); \
  DEVICE_MEMORY_STAT_DECLARE_WITH_ID(item, 12); \
  DEVICE_MEMORY_STAT_DECLARE_WITH_ID(item, 13); \
  DEVICE_MEMORY_STAT_DECLARE_WITH_ID(item, 14); \
  DEVICE_MEMORY_STAT_DECLARE_WITH_ID(item, 15)

// Only support id 0 for host memory stat
#define HOST_MEMORY_STAT_DECLARE(item) \
  struct HostMemoryStat##item##0 : public ThreadLocalStatBase{};
212 213

// To add a new STAT type, declare here and register in stats.cc
214 215 216 217 218
DEVICE_MEMORY_STAT_DECLARE(Allocated);
DEVICE_MEMORY_STAT_DECLARE(Reserved);

HOST_MEMORY_STAT_DECLARE(Allocated);
HOST_MEMORY_STAT_DECLARE(Reserved);
219 220 221

}  // namespace memory
}  // namespace paddle