Skip to content

Commit 1db7374

Browse files
committed
Add class name to module name, if available
1 parent 7ec85af commit 1db7374

4 files changed

Lines changed: 35 additions & 23 deletions

File tree

scorep/_instrumenters/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from scorep._bindings import abspath
2+
from scorep.instrumenter import has_c_instrumenter
23

34

45
def get_module_name(frame):
@@ -11,6 +12,12 @@ def get_module_name(frame):
1112
modulename = "numpy.__array_function__"
1213
else:
1314
modulename = "unkown"
15+
typeobject = frame.f_locals.get("self", None)
16+
if typeobject is not None:
17+
if has_c_instrumenter():
18+
return ".".join([modulename, type(typeobject).__name__])
19+
else:
20+
return ".".join([modulename, typeobject.__class__.__name__])
1421
return modulename
1522

1623

src/scorepy/compat.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ namespace compat
1414
{
1515
/// Region names that are known to have no region enter event and should not report an error
1616
/// on region exit
17-
static const std::array<std::string, 2> exit_region_whitelist = {
17+
static const std::array<std::string, 4> exit_region_whitelist = {
1818
#if PY_MAJOR_VERSION >= 3
19-
"threading:_bootstrap_inner", "threading:_bootstrap"
19+
"threading.Thread:_bootstrap_inner", "threading.Thread:_bootstrap",
20+
"threading.TMonitor:_bootstrap_inner", "threading.TMonitor:_bootstrap"
2021
#else
21-
"threading:__bootstrap_inner", "threading:__bootstrap"
22+
"threading.Thread:__bootstrap_inner", "threading.Thread:__bootstrap",
23+
"threading.TMonitor:__bootstrap_inner", "threading.TMonitor:__bootstrap"
2224
#endif
2325
};
2426

src/scorepy/pythonHelpers.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,36 @@
33
#include "pathUtils.hpp"
44
#include "pythoncapi_compat.h"
55

6+
#include <sstream>
67
#include <stdlib.h>
78

89
namespace scorepy
910
{
1011
std::string get_module_name(PyFrameObject& frame)
1112
{
13+
const char* self_name = nullptr;
14+
PyObject* locals = PyFrame_GetLocals(&frame);
15+
PyObject* self = PyDict_GetItemString(locals, "self");
16+
if (self)
17+
{
18+
Py_INCREF(self);
19+
PyTypeObject* type = Py_TYPE(self);
20+
self_name = _PyType_Name(type);
21+
Py_DECREF(self);
22+
}
23+
Py_DECREF(locals);
24+
1225
PyObject* globals = PyFrame_GetGlobals(&frame);
1326
PyObject* module_name = PyDict_GetItemString(globals, "__name__");
1427
Py_DECREF(globals);
1528
if (module_name)
16-
return std::move(std::string(compat::get_string_as_utf_8(module_name)));
29+
{
30+
std::stringstream result;
31+
result << compat::get_string_as_utf_8(module_name);
32+
if (self_name)
33+
result << '.' << self_name;
34+
return std::move(result).str();
35+
}
1736

1837
// this is a NUMPY special situation, see NEP-18, and Score-P issue #63
1938
// TODO: Use string_view/C-String to avoid creating 2 std::strings

test/test_scorep.py

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -309,25 +309,9 @@ def test_classes(scorep_env, instrumenter):
309309

310310
trace = OTF2_Trace(trace_path)
311311

312-
region_ids = []
313-
foo_count = 0
314-
for line in str(trace).split("\n"):
315-
m = re.search('ENTER[ ]*[0-9 ]*[0-9 ]*Region: "__main__:foo" <([0-9]*)>', line)
316-
if m is not None:
317-
foo_count += 1
318-
r_id = m.group(1)
319-
320-
if foo_count == 1:
321-
region_ids.append(r_id)
322-
continue
323-
324-
if foo_count < 4:
325-
assert region_ids[-1] < r_id # check if foo regions are different
326-
else:
327-
assert r_id == region_ids[0] # check if last foo is fist foo
328-
region_ids.append(r_id)
329-
330-
assert len(region_ids) == 4
312+
assert OTF2_Region("__main__:foo") in trace
313+
assert OTF2_Region("__main__.TestClass:foo") in trace
314+
assert OTF2_Region("__main__.TestClass2:foo") in trace
331315

332316

333317
def test_dummy(scorep_env):

0 commit comments

Comments
 (0)