profiler.h 5.0 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
D
dangqingqing 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

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 <forward_list>
#include <list>
C
chengduo 已提交
18 19 20
#include <map>
#include <memory>
#include <mutex>  // NOLINT
21
#include <string>
C
chengduo 已提交
22 23 24
#include <unordered_map>
#include <unordered_set>
#include <utility>
D
dangqingqing 已提交
25
#include <vector>
26 27
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/event.h"
C
chengduo 已提交
28
#include "paddle/fluid/platform/place.h"
D
dangqingqing 已提交
29
#ifdef PADDLE_WITH_CUDA
30
#include "paddle/fluid/platform/gpu_info.h"
D
dangqingqing 已提交
31
#endif
32 33
namespace paddle {
namespace platform {
D
dangqingqing 已提交
34 35

enum ProfilerState {
D
dangqingqing 已提交
36 37 38
  kDisabled,  // disabled state
  kCPU,       // CPU profiling state
  kCUDA,      // GPU profiling state
39
  kAll,       // Profile both CPU and GPU. (Currently experimental).
D
dangqingqing 已提交
40 41
};

42
void Mark(const std::string& name);
D
dangqingqing 已提交
43

C
chengduo 已提交
44 45 46 47 48 49 50 51 52 53 54
void PushMemEvent(uint64_t start_ns, uint64_t end_ns, size_t bytes,
                  const Place& place);
void PopMemEvent(uint64_t start_ns, uint64_t end_ns, size_t bytes,
                 const Place& place);

struct MemEvenRecorder {
 public:
  void PushMemRecord(const void* ptr, const Place& place, size_t size);
  void PopMemRecord(const void* ptr, const Place& place);
  void Flush();
  static MemEvenRecorder& Instance() { return recorder; }
55

C
chengduo 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
 private:
  struct RecordMemEvent {
    RecordMemEvent(const Place& place, size_t bytes);
    ~RecordMemEvent();

    Place place_;
    size_t bytes_;
    uint64_t start_ns_;
    uint64_t end_ns_;
    std::string alloc_in_;
    std::string free_in_;
  };

  static MemEvenRecorder recorder;
  std::map<Place,
           std::unordered_map<const void*, std::unique_ptr<RecordMemEvent>>>
      address_memevent_;
  std::mutex mtx_;
  MemEvenRecorder() {}
  DISABLE_COPY_AND_ASSIGN(MemEvenRecorder);
};

Event* PushEvent(const std::string& name);
79
void PopEvent(const std::string& name);
80

D
dangqingqing 已提交
81
struct RecordEvent {
82
  explicit RecordEvent(const std::string& name);
D
dangqingqing 已提交
83

D
dangqingqing 已提交
84 85
  ~RecordEvent();

X
Xin Pan 已提交
86
  bool is_enabled_;
X
Xin Pan 已提交
87
  uint64_t start_ns_;
Y
Yibing Liu 已提交
88
  // Event name
89
  std::string name_;
90 91 92
  // Need to distinguish name by op type, block_id, program_id and perhaps
  // different kernel invocations within an op.
  std::string full_name_;
D
dangqingqing 已提交
93 94
};

G
gongweibao 已提交
95 96
class RecordRPCEvent {
 public:
97
  explicit RecordRPCEvent(const std::string& name);
G
gongweibao 已提交
98 99 100 101 102 103
  ~RecordRPCEvent() {}

 private:
  std::unique_ptr<RecordEvent> event_;
};

X
Xin Pan 已提交
104 105 106 107 108
struct RecordBlock {
  explicit RecordBlock(int block_id);
  ~RecordBlock();

 private:
X
Xin Pan 已提交
109
  bool is_enabled_;
X
Xin Pan 已提交
110 111 112 113
  std::string name_;
  uint64_t start_ns_;
};

114
// Return the event list of all threads. Assumed the returned value calls
D
dangqingqing 已提交
115
// event_lists, event_lists[i][j] represents the j-th Event of i-th thread.
116
std::vector<std::vector<Event>> GetAllEvents();
D
dangqingqing 已提交
117

118
// Candidate keys to sort the profiling report
C
chengduo 已提交
119 120 121 122 123 124 125 126 127 128
enum EventSortingKey {
  kDefault,
  kCalls,
  kTotal,
  kMin,
  kMax,
  kAve,
  kCPUTime,
  kGPUTime
};
129

C
chengduo 已提交
130 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 159 160 161 162 163 164
template <typename T>
struct EventList {
  constexpr static size_t kMB = 1024 * 1024;
  constexpr static size_t kEventBlockSize = 16 * kMB;
  constexpr static size_t kEventSize = sizeof(T);
  constexpr static size_t kEventAlign = alignof(T);
  constexpr static size_t kNumBlock =
      kEventBlockSize /
      ((kEventSize + kEventAlign - 1) / kEventAlign * kEventAlign);

  template <typename... Args>
  T* Record(Args&&... args) {
    if (event_blocks.empty() || event_blocks.front().size() == kNumBlock) {
      event_blocks.emplace_front();
      event_blocks.front().reserve(kNumBlock);
    }
    event_blocks.front().emplace_back(std::forward<Args>(args)...);
    return &event_blocks.front().back();
  }

  std::vector<T> Reduce() {
    std::vector<T> result;
    for (auto& block : event_blocks) {
      result.insert(result.begin(), std::make_move_iterator(block.begin()),
                    std::make_move_iterator(block.end()));
    }
    event_blocks.clear();
    return result;
  }

  void Clear() { event_blocks.clear(); }

  std::forward_list<std::vector<T>> event_blocks;
};

165 166 167 168 169 170
// Enable the profiling function.
void EnableProfiler(ProfilerState state);

// Clear the g_all_event_lists, which is total event lists of all threads.
void ResetProfiler();

X
Xin Pan 已提交
171 172
void DisableProfiler(EventSortingKey sorted_key,
                     const std::string& profile_path);
173

X
Xin Pan 已提交
174 175
const int kEnableProfiler = 1;
const int kDisableProfiler = 2;
176 177 178 179 180
// Test if the profiler is currently enabled.
bool IsProfileEnabled();
// Whether the trainer should send profiling state to PS.
bool ShouldSendProfileState();
// Mark current process as PS by assigning a lister id.
X
Xin Pan 已提交
181
void SetProfileListener();
182 183
int64_t ListenerId();

184 185 186 187
#ifdef PADDLE_WITH_CUDA
void DummyKernelAndEvent();
#endif

D
dangqingqing 已提交
188 189
}  // namespace platform
}  // namespace paddle