forked from harryherold/scorep_binding_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscorep.cpp
More file actions
246 lines (198 loc) · 7.7 KB
/
Copy pathscorep.cpp
File metadata and controls
246 lines (198 loc) · 7.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
#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
{
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;
void region_begin(std::string region_name, std::string file_name, std::uint64_t line_number)
{
auto& handle = regions[region_name];
SCOREP_User_RegionBegin(&handle.value, NULL, &SCOREP_User_LastFileHandle, region_name.c_str(),
SCOREP_USER_REGION_TYPE_FUNCTION, file_name.c_str(), line_number);
}
void region_end(std::string region_name)
{
auto& handle = regions[region_name];
SCOREP_User_RegionEnd(handle.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
namespace scorep_python
{
std::set<std::string> filter_modules = { "scorep.user", "scorep.strace" };
}
extern "C"
{
extern const char* SCOREP_GetExperimentDirName(void);
constexpr unsigned long region_buffer_max = 4096;
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;
}
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;
if (scorep_python::filter_modules.find(module) == scorep_python::filter_modules.end())
{
char region_buffer[region_buffer_max];
assert((strlen(module) + strlen(region_name)) < region_buffer_max &&
"Resulting region is too long\n");
sprintf(region_buffer, "%s:%s", module, region_name);
scorep::region_begin(region_buffer, file_name, line_number);
}
Py_INCREF(Py_None);
return Py_None;
}
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;
if (scorep_python::filter_modules.find(module) == scorep_python::filter_modules.end())
{
char region_buffer[region_buffer_max];
assert((strlen(module) + strlen(region_name)) < region_buffer_max &&
"Resulting region is too long\n");
sprintf(region_buffer, "%s:%s", module, region_name);
scorep::region_end(region_buffer);
}
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." },
{ "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 /* python3 */
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*/
} /* namespace scorep */