Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ModelZoo
ResNet50_tensorflow
Commits
830a17ec
"src/include/blockwise_3d_tensor_op.hpp" did not exist on "b2439ec9dd8acc7a6788c3225fda80eb7f416ce6"
Commit
830a17ec
authored
Jul 19, 2019
by
Zongwei Zhou
Committed by
zongweiz
Jul 21, 2019
Browse files
Add a simple signal-based Python callstack sampler for debugging
parent
448c31b6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
0 deletions
+62
-0
official/utils/misc/callstack_sampler.py
official/utils/misc/callstack_sampler.py
+62
-0
No files found.
official/utils/misc/callstack_sampler.py
0 → 100644
View file @
830a17ec
"""A simple Python callstack sampler."""
import
contextlib
import
datetime
import
signal
import
traceback
class
CallstackSampler
(
object
):
"""A simple signal-based Python callstack sampler.
"""
def
__init__
(
self
,
interval
=
None
):
self
.
stacks
=
[]
self
.
interval
=
0.001
if
interval
is
None
else
interval
def
_sample
(
self
,
signum
,
frame
):
"""Samples the current stack."""
del
signum
stack
=
traceback
.
extract_stack
(
frame
)
formatted_stack
=
[]
formatted_stack
.
append
(
datetime
.
datetime
.
utcnow
())
for
filename
,
lineno
,
function_name
,
text
in
stack
:
formatted_frame
=
'{}:{}({})({})'
.
format
(
filename
,
lineno
,
function_name
,
text
)
formatted_stack
.
append
(
formatted_frame
)
self
.
stacks
.
append
(
formatted_stack
)
signal
.
setitimer
(
signal
.
ITIMER_VIRTUAL
,
self
.
interval
,
0
)
@
contextlib
.
contextmanager
def
profile
(
self
):
signal
.
signal
(
signal
.
SIGVTALRM
,
self
.
_sample
)
signal
.
setitimer
(
signal
.
ITIMER_VIRTUAL
,
self
.
interval
,
0
)
try
:
yield
finally
:
signal
.
setitimer
(
signal
.
ITIMER_VIRTUAL
,
0
)
def
save
(
self
,
fname
):
with
open
(
fname
,
'w'
)
as
f
:
for
s
in
self
.
stacks
:
for
l
in
s
:
f
.
write
(
'%s
\n
'
%
l
)
f
.
write
(
'
\n
'
)
@
contextlib
.
contextmanager
def
callstack_sampling
(
filename
,
interval
=
None
):
"""Periodically samples the Python callstack.
Args:
filename: the filename
interval: the sampling interval, in seconds. Defaults to 0.001.
Yields:
nothing
"""
sampler
=
CallstackSampler
(
interval
=
interval
)
with
sampler
.
profile
():
yield
sampler
.
save
(
filename
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment