forked from score-p/scorep_binding_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.cpp
More file actions
268 lines (217 loc) · 7.86 KB
/
Copy pathmethods.cpp
File metadata and controls
268 lines (217 loc) · 7.86 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "methods.hpp"
#include "scorepy/events.hpp"
#include "scorepy/pathUtils.hpp"
#include <Python.h>
#include <cstdint>
#include <scorep/SCOREP_User_Functions.h>
#include <iostream>
extern "C"
{
extern const char* SCOREP_GetExperimentDirName(void);
extern void SCOREP_RegisterExitHandler(void);
extern void SCOREP_FinalizeMeasurement(void);
static PyObject* enable_recording(PyObject* self, PyObject* args)
{
SCOREP_User_EnableRecording();
Py_RETURN_NONE;
}
static PyObject* disable_recording(PyObject* self, PyObject* args)
{
SCOREP_User_DisableRecording();
Py_RETURN_NONE;
}
static PyObject* try_region_begin(PyObject* self, PyObject* args)
{
PyObject* identifier = nullptr;
if (!PyArg_ParseTuple(args, "O", &identifier))
{
return NULL;
}
bool success = scorepy::try_region_begin(reinterpret_cast<PyCodeObject*>(identifier));
if (success)
{
Py_RETURN_TRUE;
}
else
{
Py_RETURN_FALSE;
}
}
/** This code is not thread save. However, this does not matter as the python GIL is not
* released.
*/
static PyObject* region_begin(PyObject* self, PyObject* args)
{
const char* module_cstr;
const char* function_name_cstr;
const char* file_name_cstr;
Py_ssize_t module_len;
Py_ssize_t function_name_len;
Py_ssize_t file_name_len;
PyObject* identifier = nullptr;
std::uint64_t line_number = 0;
if (!PyArg_ParseTuple(args, "s#s#s#KO", &module_cstr, &module_len, &function_name_cstr,
&function_name_len, &file_name_cstr, &file_name_len, &line_number,
&identifier))
{
return NULL;
}
std::string module(module_cstr, module_len);
std::string_view function_name(function_name_cstr, function_name_len);
auto const file_name_abs =
scorepy::abspath(std::string_view(file_name_cstr, file_name_len));
if (identifier == nullptr || identifier == Py_None)
{
scorepy::region_begin(function_name, std::move(module), std::move(file_name_abs),
line_number);
}
else
{
scorepy::region_begin(function_name, std::move(module), std::move(file_name_abs),
line_number, reinterpret_cast<PyCodeObject*>(identifier));
}
Py_RETURN_NONE;
}
static PyObject* try_region_end(PyObject* self, PyObject* args)
{
PyObject* identifier = nullptr;
if (!PyArg_ParseTuple(args, "O", &identifier))
{
return NULL;
}
bool success = scorepy::try_region_end(reinterpret_cast<PyCodeObject*>(identifier));
if (success)
{
Py_RETURN_TRUE;
}
else
{
Py_RETURN_FALSE;
}
}
/** This code is not thread save. However, this does not matter as the python GIL is not
* released.
*/
static PyObject* region_end(PyObject* self, PyObject* args)
{
const char* module_cstr;
const char* function_name_cstr;
Py_ssize_t module_len;
Py_ssize_t function_name_len;
PyObject* identifier = nullptr;
if (!PyArg_ParseTuple(args, "s#s#O", &module_cstr, &module_len, &function_name_cstr,
&function_name_len, &identifier))
{
return NULL;
}
std::string module(module_cstr, module_len);
std::string_view function_name(function_name_cstr, function_name_len);
if (identifier == nullptr || identifier == Py_None)
{
scorepy::region_end(function_name, std::move(module));
}
else
{
scorepy::region_end(function_name, std::move(module),
reinterpret_cast<PyCodeObject*>(identifier));
}
Py_RETURN_NONE;
}
static PyObject* rewind_begin(PyObject* self, PyObject* args)
{
const char* region_name;
const char* file_name;
std::uint64_t line_number = 0;
if (!PyArg_ParseTuple(args, "ssK", ®ion_name, &file_name, &line_number))
return NULL;
scorepy::rewind_begin(region_name, file_name, line_number);
Py_RETURN_NONE;
}
static PyObject* rewind_end(PyObject* self, PyObject* args)
{
const char* region_name;
PyObject* value; // false C-Style
if (!PyArg_ParseTuple(args, "sO", ®ion_name, &value))
return NULL;
// TODO cover PyObject_IsTrue(value) == -1 (error case)
scorepy::rewind_end(region_name, PyObject_IsTrue(value) == 1);
Py_RETURN_NONE;
}
static PyObject* parameter_string(PyObject* self, PyObject* args)
{
const char* name;
const char* value;
if (!PyArg_ParseTuple(args, "ss", &name, &value))
return NULL;
scorepy::parameter_string(name, value);
Py_RETURN_NONE;
}
static PyObject* parameter_int(PyObject* self, PyObject* args)
{
const char* name;
long long value;
if (!PyArg_ParseTuple(args, "sL", &name, &value))
return NULL;
scorepy::parameter_int(name, value);
Py_RETURN_NONE;
}
static PyObject* parameter_uint(PyObject* self, PyObject* args)
{
const char* name;
unsigned long long value;
if (!PyArg_ParseTuple(args, "sK", &name, &value))
return NULL;
scorepy::parameter_uint(name, value);
Py_RETURN_NONE;
}
static PyObject* get_experiment_dir_name(PyObject* self, PyObject* args)
{
return PyUnicode_FromString(SCOREP_GetExperimentDirName());
}
static PyObject* abspath(PyObject* self, PyObject* args)
{
const char* path;
if (!PyArg_ParseTuple(args, "s", &path))
return NULL;
return PyUnicode_FromString(scorepy::abspath(path).c_str());
}
static PyObject* force_finalize(PyObject* self, PyObject* args)
{
SCOREP_FinalizeMeasurement();
Py_RETURN_NONE;
}
static PyObject* reregister_exit_handler(PyObject* self, PyObject* args)
{
SCOREP_RegisterExitHandler();
Py_RETURN_NONE;
}
static PyMethodDef ScorePMethods[] = {
{ "region_begin", region_begin, METH_VARARGS, "enter a region." },
{ "try_region_begin", try_region_begin, METH_VARARGS,
"Tries to begin a region, returns True on Sucess." },
{ "region_end", region_end, METH_VARARGS, "exit a region." },
{ "try_region_end", try_region_end, METH_VARARGS,
"Tries to end a region, returns True on Sucess." },
{ "rewind_begin", rewind_begin, METH_VARARGS, "rewind begin." },
{ "rewind_end", rewind_end, METH_VARARGS, "rewind end." },
{ "enable_recording", enable_recording, METH_VARARGS, "disable scorep recording." },
{ "disable_recording", disable_recording, METH_VARARGS, "disable scorep recording." },
{ "parameter_int", parameter_int, METH_VARARGS, "User parameter int." },
{ "parameter_uint", parameter_uint, METH_VARARGS, "User parameter uint." },
{ "parameter_string", parameter_string, METH_VARARGS, "User parameter string." },
{ "get_experiment_dir_name", get_experiment_dir_name, METH_VARARGS,
"Get the Score-P experiment dir." },
{ "abspath", abspath, METH_VARARGS, "Estimates the absolute Path." },
{ "force_finalize", force_finalize, METH_VARARGS, "triggers a finalize" },
{ "reregister_exit_handler", reregister_exit_handler, METH_VARARGS,
"register a new atexit handler" },
{ NULL, NULL, 0, NULL } /* Sentinel */
};
}
namespace scorepy
{
PyMethodDef* getMethodTable()
{
return ScorePMethods;
}
} // namespace scorepy