forked from harryherold/scorep_binding_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrace.py
More file actions
165 lines (141 loc) · 5.24 KB
/
Copy pathtrace.py
File metadata and controls
165 lines (141 loc) · 5.24 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
__all__ = ['ScorepTrace']
import sys
import inspect
import os.path
try:
import threading
except ImportError:
_settrace = sys.settrace
def _unsettrace():
sys.settrace(None)
else:
def _settrace(func):
threading.settrace(func)
sys.settrace(func)
def _unsettrace():
sys.settrace(None)
threading.settrace(None)
class ScorepTrace:
def __init__(self, scorep_bindings, trace=True):
"""
@param trace true if there shall be any tracing at all
"""
self.pathtobasename = {} # for memoizing os.path.basename
self.donothing = False
self.trace = trace
self.scorep_bindings = scorep_bindings
if trace:
self.globaltrace = self.globaltrace_lt
self.localtrace = self.localtrace_trace
else:
self.donothing = True
def register(self):
if not self.donothing:
_settrace(self.globaltrace)
def run(self, cmd):
#import __main__
#dict = __main__.__dict__
#self.runctx(cmd, dict, dict)
self.runctx(cmd)
def runctx(self, cmd, globals=None, locals=None):
if globals is None:
globals = {}
if locals is None:
locals = {}
if not self.donothing:
_settrace(self.globaltrace)
try:
exec(cmd, globals, locals)
finally:
if not self.donothing:
_unsettrace()
def runfunc(self, func, *args, **kw):
result = None
if not self.donothing:
sys.settrace(self.globaltrace)
try:
result = func(*args, **kw)
finally:
if not self.donothing:
sys.settrace(None)
return result
def globaltrace_lt(self, frame, why, arg):
"""Handler for call events.
If the code block being entered is to be ignored, returns `None',
else returns self.localtrace.
"""
if why == 'call':
code = frame.f_code
modulename = frame.f_globals.get('__name__', None)
if modulename is None:
modulename = "None"
file_name = frame.f_globals.get('__file__', None)
if file_name is not None:
full_file_name = os.path.abspath(file_name)
else:
full_file_name = "None"
line_number = frame.f_lineno
if self.trace and code.co_name is not "_unsettrace":
self.scorep_bindings.region_begin(
modulename, code.co_name, full_file_name, line_number)
return self.localtrace
else:
return None
def localtrace_trace(self, frame, why, arg):
if why == "return":
code = frame.f_code
modulename = frame.f_globals.get('__name__', None)
if modulename is None:
modulename = "None"
if self.trace:
self.scorep_bindings.region_end(modulename, code.co_name)
return self.localtrace
def user_region_begin(self, name, file_name=None, line_number=None):
"""
Begin of an User region. If file_name or line_number is None, both will
bet determined automatically
@param name name of the user region
@param file_name file name of the user region
@param line_number line number of the user region
"""
if file_name is None or line_number is None:
frame = inspect.currentframe().f_back
file_name = frame.f_globals.get('__file__', None)
line_number = frame.f_lineno
if file_name is not None:
full_file_name = os.path.abspath(file_name)
else:
full_file_name = "None"
self.scorep_bindings.region_begin(
"user", name, full_file_name, line_number)
def user_region_end(self, name):
self.scorep_bindings.region_end("user", name)
def oa_region_begin(self, name, file_name=None, line_number=None):
"""
Begin of an Online Access region. If file_name or line_number is None, both will
bet determined automatically
@param name name of the user region
@param file_name file name of the user region
@param line_number line number of the user region
"""
if file_name is None or line_number is None:
frame = inspect.currentframe().f_back
file_name = frame.f_globals.get('__file__', None)
line_number = frame.f_lineno
if file_name is not None:
full_file_name = os.path.abspath(file_name)
else:
full_file_name = "None"
self.scorep_bindings.oa_region_begin(name, full_file_name, line_number)
def oa_region_end(self, name):
self.scorep_bindings.oa_region_end(name)
def user_enable_recording(self):
self.scorep_bindings.enable_recording()
def user_disable_recording(self):
self.scorep_bindings.disable_recording()
def user_parameter_int(self, name, val):
self.scorep_bindings.parameter_int(name, val)
def user_parameter_uint(self, name, val):
self.scorep_bindings.parameter_string(name, val)
def user_parameter_string(self, name, string):
self.scorep_bindings.parameter_string(name, string)