forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_hooks.h
More file actions
194 lines (159 loc) · 5.95 KB
/
python_hooks.h
File metadata and controls
194 lines (159 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* Copyright 2020 The TensorFlow 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.
==============================================================================*/
#ifndef TENSORFLOW_PYTHON_PROFILER_INTERNAL_PYTHON_HOOKS_H_
#define TENSORFLOW_PYTHON_PROFILER_INTERNAL_PYTHON_HOOKS_H_
#include <memory>
#include <stack>
#include <vector>
#include "absl/container/flat_hash_map.h"
#include "absl/memory/memory.h"
#include "pybind11/cast.h"
#include "pybind11/pybind11.h"
#include "pybind11/pytypes.h"
#include "tensorflow/core/platform/macros.h"
#include "tensorflow/core/platform/types.h"
#include "tensorflow/core/profiler/protobuf/xplane.pb.h"
namespace tensorflow {
namespace profiler {
namespace py = ::pybind11;
struct PythonHooksOptions {
bool enable_trace_python_function = false;
bool enable_python_traceme = true;
bool end_to_end_mode = false;
// Incomplete events are defined as those python calls which we only see
// either start or end, but not both. If we want to include them in the final
// result, profiler start, end time are used respectively to the absent
// timestamps.
bool include_incomplete_events = true;
};
struct PythonTraceEntry {
// Capture the source/line information for a PyCodeObject object.
// In eager mode, keeping a reference to PyCodeObject leaks device memory.
PythonTraceEntry(uint64 start, uint64 end, PyCodeObject* py_code_object)
: start_time_ns(start),
end_time_ns(end),
co_filename(py_code_object->co_filename),
co_name(py_code_object->co_name),
co_firstlineno(py_code_object->co_firstlineno) {
Py_XINCREF(co_filename);
Py_XINCREF(co_name);
}
// Capture the source/line information for a PyCFunctionObject object.
// In eager mode, keeping a reference to PyCFunctionObject leaks device
// memory.
PythonTraceEntry(uint64 start, uint64 end, PyCFunctionObject* py_c_function)
: start_time_ns(start),
end_time_ns(end),
method_def(py_c_function->m_ml),
m_module(py_c_function->m_module) {
Py_XINCREF(m_module);
}
~PythonTraceEntry() {
Py_XDECREF(co_filename);
Py_XDECREF(co_name);
Py_XDECREF(m_module);
}
PythonTraceEntry(PythonTraceEntry&& other) {
start_time_ns = other.start_time_ns;
end_time_ns = other.end_time_ns;
co_firstlineno = other.co_firstlineno;
co_filename = other.co_filename;
co_name = other.co_name;
method_def = other.method_def;
m_module = other.m_module;
other.co_filename = nullptr;
other.co_name = nullptr;
other.method_def = nullptr;
other.m_module = nullptr;
}
std::string Name() const;
uint64 start_time_ns;
uint64 end_time_ns;
PyObject* co_filename = nullptr;
PyObject* co_name = nullptr;
int co_firstlineno = 0;
PyMethodDef* method_def = nullptr;
PyObject* m_module = nullptr;
PythonTraceEntry(const PythonTraceEntry& other) = delete;
void operator=(const PythonTraceEntry&) = delete;
void operator=(PythonTraceEntry&&) = delete;
};
struct PerThreadEvents {
std::deque<PythonTraceEntry> completed;
std::stack<PythonTraceEntry> active;
};
class PythonHooks;
class PythonHookContext {
public:
void Finalize(XSpace* space);
friend class ::tensorflow::profiler::PythonHooks;
private:
void Start(const PythonHooksOptions& option);
void Stop();
void ProfileFast(PyFrameObject* frame, int what, PyObject* arg);
void CollectData(XPlane* raw_plane);
static void EnableTraceMe(bool enable);
static void SetProfilerInAllThreads();
static void ClearProfilerInAllThreads();
void operator=(const PythonHookContext&) = delete;
void operator=(PythonHookContext&&) = delete;
absl::flat_hash_map<int64_t, PerThreadEvents> entries_;
uint64 start_timestamp_ns_;
PythonHooksOptions options_;
// In end to end mode, Python get uninitialized before Stop()/Finalize(), we
// need to buffer the result.
absl::optional<XPlane> end_to_end_xplane_;
};
// Singleton for tracing python function calls.
class PythonHooks {
public:
static PythonHooks* GetSingleton();
void Start(const PythonHooksOptions& option) {
if (active_context_) return;
active_context_ = std::make_unique<PythonHookContext>();
active_context_->Start(option);
}
std::unique_ptr<PythonHookContext> Stop() {
if (e2e_context_) {
auto* e2e_context = e2e_context_;
e2e_context_ = nullptr;
return absl::WrapUnique(e2e_context);
}
if (!active_context_) return nullptr;
active_context_->Stop();
std::unique_ptr<PythonHookContext> output = std::move(active_context_);
active_context_.reset();
return output;
}
friend class ::tensorflow::profiler::PythonHookContext;
private:
void ProfileSlow(const py::object& frame, const string& event,
const py::object& arg);
void ProfileFast(PyFrameObject* frame, int what, PyObject* arg) {
if (TF_PREDICT_TRUE(active_context_)) {
active_context_->ProfileFast(frame, what, arg);
}
}
static void set_e2e_context(PythonHookContext* e2e_context) {
e2e_context_ = e2e_context;
}
static PythonHookContext* e2e_context() { return e2e_context_; }
static int ProfileFunction(PyObject* obj, PyFrameObject* frame, int what,
PyObject* arg);
// active_context_ are accessed when GIL is held, therefore no race
// conditions.
std::unique_ptr<PythonHookContext> active_context_;
static PythonHookContext* e2e_context_;
};
} // namespace profiler
} // namespace tensorflow
#endif // TENSORFLOW_PYTHON_PROFILER_INTERNAL_PYTHON_HOOKS_H_