forked from score-p/scorep_binding_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscorep.cpp
More file actions
335 lines (276 loc) · 10.7 KB
/
Copy pathscorep.cpp
File metadata and controls
335 lines (276 loc) · 10.7 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <Python.h>
#include <iostream>
#include <scorep/SCOREP_User_Functions.h>
#include <scorep/SCOREP_User_Variables.h>
#include <set>
#include <string>
#include <unordered_map>
namespace scorep
{
// SCOREP_User_RegionHandle handle = SCOREP_USER_INVALID_REGION
struct region_handle
{
region_handle() = default;
~region_handle() = default;
SCOREP_User_RegionHandle value = SCOREP_USER_INVALID_REGION;
};
static std::unordered_map<std::string, region_handle> regions;
static std::unordered_map<std::string, region_handle> rewind_regions;
void region_begin(const std::string& region_name, std::string module, std::string file_name,
std::uint64_t line_number)
{
auto pair = regions.emplace(make_pair(region_name, region_handle()));
bool inserted_new = pair.second;
auto& handle = pair.first->second;
if (inserted_new)
{
SCOREP_User_RegionInit(&handle.value, NULL, &SCOREP_User_LastFileHandle,
region_name.c_str(), SCOREP_USER_REGION_TYPE_FUNCTION,
file_name.c_str(), line_number);
SCOREP_User_RegionSetGroup(handle.value, std::string(module, 0, module.find('.')).c_str());
}
SCOREP_User_RegionEnter(handle.value);
}
void region_end(const std::string& region_name)
{
try
{
auto& handle = regions.at(region_name);
SCOREP_User_RegionEnd(handle.value);
}
catch (std::out_of_range& e)
{
static region_handle error_region;
static SCOREP_User_ParameterHandle scorep_param = SCOREP_USER_INVALID_PARAMETER;
static bool error_printed = false;
if (error_region.value == SCOREP_USER_INVALID_REGION)
{
SCOREP_User_RegionInit(&error_region.value, NULL, &SCOREP_User_LastFileHandle,
"error_region", SCOREP_USER_REGION_TYPE_FUNCTION, "scorep.cpp",
0);
SCOREP_User_RegionSetGroup(error_region.value, "error");
}
SCOREP_User_RegionEnter(error_region.value);
SCOREP_User_ParameterString(&scorep_param, "leave-region", region_name.c_str());
SCOREP_User_RegionEnd(error_region.value);
if (!error_printed)
{
std::cerr << "SCOREP_BINDING_PYTHON ERROR: There was a region exit without an enter!\n"
<< "SCOREP_BINDING_PYTHON ERROR: For details look for \"error_region\" in "
"the trace or profile."
<< std::endl;
error_printed = true;
}
}
}
void rewind_begin(std::string region_name, std::string file_name, std::uint64_t line_number)
{
auto pair = rewind_regions.emplace(make_pair(region_name, region_handle()));
bool inserted_new = pair.second;
auto& handle = pair.first->second;
if (inserted_new)
{
SCOREP_User_RegionInit(&handle.value, NULL, &SCOREP_User_LastFileHandle,
region_name.c_str(), SCOREP_USER_REGION_TYPE_FUNCTION,
file_name.c_str(), line_number);
}
SCOREP_User_RewindRegionEnter(handle.value);
}
void rewind_end(std::string region_name, bool value)
{
auto& handle = rewind_regions.at(region_name);
/* don't call SCOREP_ExitRewindRegion, as
* SCOREP_User_RewindRegionEnd does some additional magic
* */
SCOREP_User_RewindRegionEnd(handle.value, value);
}
void parameter_int(std::string name, int64_t value)
{
static SCOREP_User_ParameterHandle scorep_param = SCOREP_USER_INVALID_PARAMETER;
SCOREP_User_ParameterInt64(&scorep_param, name.c_str(), value);
}
void parameter_uint(std::string name, uint64_t value)
{
static SCOREP_User_ParameterHandle scorep_param = SCOREP_USER_INVALID_PARAMETER;
SCOREP_User_ParameterUint64(&scorep_param, name.c_str(), value);
}
void parameter_string(std::string name, std::string value)
{
static SCOREP_User_ParameterHandle scorep_param = SCOREP_USER_INVALID_PARAMETER;
SCOREP_User_ParameterString(&scorep_param, name.c_str(), value.c_str());
}
void oa_region_begin(std::string region_name, std::string file_name, std::uint64_t line_number)
{
auto& handle = regions[region_name];
SCOREP_User_OaPhaseBegin(&handle.value, &SCOREP_User_LastFileName, &SCOREP_User_LastFileHandle,
region_name.c_str(), SCOREP_USER_REGION_TYPE_FUNCTION,
file_name.c_str(), line_number);
}
void oa_region_end(std::string region_name)
{
auto& handle = regions[region_name];
SCOREP_User_OaPhaseEnd(handle.value);
}
} // namespace scorep
extern "C"
{
extern const char* SCOREP_GetExperimentDirName(void);
static PyObject* enable_recording(PyObject* self, PyObject* args)
{
SCOREP_User_EnableRecording();
Py_INCREF(Py_None);
return Py_None;
}
static PyObject* disable_recording(PyObject* self, PyObject* args)
{
SCOREP_User_DisableRecording();
Py_INCREF(Py_None);
return Py_None;
}
/** 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;
const char* region_name;
const char* file_name;
std::uint64_t line_number = 0;
if (!PyArg_ParseTuple(args, "sssK", &module, ®ion_name, &file_name, &line_number))
return NULL;
static std::string region = "";
region = module;
region += ":";
region += region_name;
scorep::region_begin(region, module, file_name, line_number);
Py_INCREF(Py_None);
return Py_None;
}
/** 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;
const char* region_name;
if (!PyArg_ParseTuple(args, "ss", &module, ®ion_name))
return NULL;
static std::string region = "";
region = module;
region += ":";
region += region_name;
scorep::region_end(region);
Py_INCREF(Py_None);
return Py_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;
scorep::rewind_begin(region_name, file_name, line_number);
Py_INCREF(Py_None);
return Py_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)
scorep::rewind_end(region_name, PyObject_IsTrue(value) == 1);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject* oa_region_begin(PyObject* self, PyObject* args)
{
const char* region;
const char* file_name;
std::uint64_t line_number = 0;
if (!PyArg_ParseTuple(args, "ssK", ®ion, &file_name, &line_number))
return NULL;
scorep::oa_region_begin(region, file_name, line_number);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject* oa_region_end(PyObject* self, PyObject* args)
{
const char* region;
if (!PyArg_ParseTuple(args, "s", ®ion))
return NULL;
scorep::oa_region_end(region);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject* parameter_string(PyObject* self, PyObject* args)
{
const char* name;
const char* value;
if (!PyArg_ParseTuple(args, "ss", &name, &value))
return NULL;
scorep::parameter_string(name, value);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject* parameter_int(PyObject* self, PyObject* args)
{
const char* name;
long long value;
if (!PyArg_ParseTuple(args, "sL", &name, &value))
return NULL;
scorep::parameter_int(name, value);
Py_INCREF(Py_None);
return Py_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;
scorep::parameter_uint(name, value);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject* get_expiriment_dir_name(PyObject* self, PyObject* args)
{
return PyUnicode_FromString(SCOREP_GetExperimentDirName());
}
static PyMethodDef ScorePMethods[] = {
{ "region_begin", region_begin, METH_VARARGS, "enter a region." },
{ "region_end", region_end, METH_VARARGS, "exit a region." },
{ "rewind_begin", rewind_begin, METH_VARARGS, "rewind begin." },
{ "rewind_end", rewind_end, METH_VARARGS, "rewind end." },
{ "oa_region_begin", oa_region_begin, METH_VARARGS, "enter an online access region." },
{ "oa_region_end", oa_region_end, METH_VARARGS, "exit an online access region." },
{ "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_expiriment_dir_name", get_expiriment_dir_name, METH_VARARGS,
"Get the Score-P experiment dir." },
{ NULL, NULL, 0, NULL } /* Sentinel */
};
#if PY_VERSION_HEX < 0x03000000
PyMODINIT_FUNC initscorep_bindings(void)
{
(void)Py_InitModule("scorep_bindings", ScorePMethods);
}
#else /*python 3*/
static struct PyModuleDef scorepmodule = { PyModuleDef_HEAD_INIT,
"scorep_bindings", /* name of module */
NULL, /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module,
or -1 if the module keeps state in global
variables. */
ScorePMethods };
PyMODINIT_FUNC PyInit_scorep_bindings(void)
{
return PyModule_Create(&scorepmodule);
}
#endif /*python 3*/
}