profiler.h 3.3 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>
18
#include <string>
D
dangqingqing 已提交
19
#include <vector>
Y
Yi Wang 已提交
20
#include "paddle/fluid/platform/device_context.h"
21
#include "paddle/fluid/platform/profiler.pb.h"
D
dangqingqing 已提交
22 23 24 25

namespace paddle {
namespace platform {

26
enum EventType { kMark, kPushRange, kPopRange };
D
dangqingqing 已提交
27 28 29

class Event {
 public:
D
dangqingqing 已提交
30 31
  // The DeviceContext is used to get the cuda stream.
  // If CPU profiling mode, can pass nullptr.
32
  Event(EventType type, std::string name, uint32_t thread_id,
D
dangqingqing 已提交
33
        const DeviceContext* dev_ctx);
D
dangqingqing 已提交
34

35
  const EventType& type() const;
D
dangqingqing 已提交
36
  std::string name() const { return name_; }
37
  uint32_t thread_id() const { return thread_id_; }
D
dangqingqing 已提交
38 39 40 41 42 43 44
  bool has_cuda() const { return has_cuda_; }

#ifdef PADDLE_WITH_CUDA
  cudaEvent_t event() const { return event_; }
  int device() const { return device_; }
#endif

45 46
  double CpuElapsedMs(const Event& e) const;
  double CudaElapsedMs(const Event& e) const;
D
dangqingqing 已提交
47 48

 private:
49
  EventType type_;
D
dangqingqing 已提交
50 51 52 53 54 55 56 57 58 59 60
  std::string name_;
  uint32_t thread_id_;
  int64_t cpu_ns_;
  bool has_cuda_;
#ifdef PADDLE_WITH_CUDA
  cudaEvent_t event_ = nullptr;
  int device_ = -1;
#endif
};

enum ProfilerState {
D
dangqingqing 已提交
61 62 63
  kDisabled,  // disabled state
  kCPU,       // CPU profiling state
  kCUDA,      // GPU profiling state
64
  kAll,       // Profile both CPU and GPU. (Currently experimental).
D
dangqingqing 已提交
65 66
};

D
dangqingqing 已提交
67
void Mark(const std::string& name, const DeviceContext* dev_ctx);
D
dangqingqing 已提交
68

D
dangqingqing 已提交
69
void PushEvent(const std::string& name, const DeviceContext* dev_ctx);
70

D
dangqingqing 已提交
71
void PopEvent(const std::string& name, const DeviceContext* dev_ctx);
72

D
dangqingqing 已提交
73
struct RecordEvent {
Y
Yibing Liu 已提交
74
  RecordEvent(const std::string& name, const DeviceContext* dev_ctx);
D
dangqingqing 已提交
75

D
dangqingqing 已提交
76 77
  ~RecordEvent();

X
Xin Pan 已提交
78
  uint64_t start_ns_;
D
dangqingqing 已提交
79
  // The device context is used by Event to get the current cuda stream.
D
dangqingqing 已提交
80
  const DeviceContext* dev_ctx_;
Y
Yibing Liu 已提交
81
  // Event name
82
  std::string name_;
83 84 85
  // 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 已提交
86 87
};

X
Xin Pan 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101
struct RecordBlock {
  explicit RecordBlock(int block_id);
  ~RecordBlock();

 private:
  std::string name_;
  uint64_t start_ns_;
};

struct RecordThread {
  explicit RecordThread(int thread_id);
  ~RecordThread();
};

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

106 107 108
// Candidate keys to sort the profiling report
enum EventSortingKey { kDefault, kCalls, kTotal, kMin, kMax, kAve };

109 110 111 112 113 114
// 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 已提交
115 116
void DisableProfiler(EventSortingKey sorted_key,
                     const std::string& profile_path);
117

D
dangqingqing 已提交
118 119
}  // namespace platform
}  // namespace paddle