profiler.h 2.8 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>
20 21
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/event.h"
D
dangqingqing 已提交
22
#ifdef PADDLE_WITH_CUDA
23
#include "paddle/fluid/platform/gpu_info.h"
D
dangqingqing 已提交
24
#endif
25 26
namespace paddle {
namespace platform {
D
dangqingqing 已提交
27 28

enum ProfilerState {
D
dangqingqing 已提交
29 30 31
  kDisabled,  // disabled state
  kCPU,       // CPU profiling state
  kCUDA,      // GPU profiling state
32
  kAll,       // Profile both CPU and GPU. (Currently experimental).
D
dangqingqing 已提交
33 34
};

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

37
Event* PushEvent(const std::string& name);
38

39
void PopEvent(const std::string& name);
40

D
dangqingqing 已提交
41
struct RecordEvent {
42
  explicit RecordEvent(const std::string& name);
D
dangqingqing 已提交
43

D
dangqingqing 已提交
44 45
  ~RecordEvent();

X
Xin Pan 已提交
46
  bool is_enabled_;
X
Xin Pan 已提交
47
  uint64_t start_ns_;
Y
Yibing Liu 已提交
48
  // Event name
49
  std::string name_;
50 51 52
  // 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 已提交
53 54
};

G
gongweibao 已提交
55 56
class RecordRPCEvent {
 public:
57
  explicit RecordRPCEvent(const std::string& name);
G
gongweibao 已提交
58 59 60 61 62 63
  ~RecordRPCEvent() {}

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

X
Xin Pan 已提交
64 65 66 67 68
struct RecordBlock {
  explicit RecordBlock(int block_id);
  ~RecordBlock();

 private:
X
Xin Pan 已提交
69
  bool is_enabled_;
X
Xin Pan 已提交
70 71 72 73
  std::string name_;
  uint64_t start_ns_;
};

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

78
// Candidate keys to sort the profiling report
C
chengduo 已提交
79 80 81 82 83 84 85 86 87 88
enum EventSortingKey {
  kDefault,
  kCalls,
  kTotal,
  kMin,
  kMax,
  kAve,
  kCPUTime,
  kGPUTime
};
89

90 91 92 93 94 95
// 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 已提交
96 97
void DisableProfiler(EventSortingKey sorted_key,
                     const std::string& profile_path);
98

X
Xin Pan 已提交
99 100
const int kEnableProfiler = 1;
const int kDisableProfiler = 2;
101 102 103 104 105
// 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 已提交
106
void SetProfileListener();
107 108
int64_t ListenerId();

109 110 111 112
#ifdef PADDLE_WITH_CUDA
void DummyKernelAndEvent();
#endif

D
dangqingqing 已提交
113 114
}  // namespace platform
}  // namespace paddle