Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
Pyston
Commits
ee277f56
Commit
ee277f56
authored
Dec 06, 2014
by
Kevin Modzelewski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ressurect the C-extension tracing measure_loc
parent
860be3a2
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
135 additions
and
4 deletions
+135
-4
tools/measure_loc.py
tools/measure_loc.py
+10
-4
tools/measure_loc_ext/measure_loc.cpp
tools/measure_loc_ext/measure_loc.cpp
+116
-0
tools/measure_loc_ext/setup.py
tools/measure_loc_ext/setup.py
+9
-0
No files found.
tools/measure_loc.py
View file @
ee277f56
...
...
@@ -41,13 +41,12 @@ use the python_trace_counter instead of python_sampler (you have to modify the s
"""
import
os
import
os
import
sys
import
cPickle
import
os
import
runpy
import
signal
import
sys
import
traceback
class
SamplingProfiler
(
object
):
# Copied + modified from https://github.com/bdarnell/plop/blob/master/plop/collector.py
...
...
@@ -124,6 +123,7 @@ def run(sampler, kind):
runpy
.
run_path
(
fn
,
run_name
=
"__main__"
)
except
KeyboardInterrupt
:
print
"Interrupted!"
traceback
.
print_exc
()
times
=
sampler
.
stop
()
...
...
@@ -175,7 +175,13 @@ def run(sampler, kind):
python_sampler
=
SamplingProfiler
(
signal_handler
,
get_times
,
"real"
,
interval
=
0.00001
)
python_trace_counter
=
TracingProfiler
(
trace_count
,
get_times
)
try
:
import
measure_loc_ext
cext_trace_timer
=
TracingProfiler
(
measure_loc_ext
.
trace
,
lambda
:
measure_loc_ext
.
get_times
().
items
())
except
ImportError
:
print
"(extension module not available)"
if
__name__
==
"__main__"
:
run
(
python_sampler
,
"count"
)
# run(python_trace_counter, "count")
# run(cext_trace_timer, "time")
tools/measure_loc_ext/measure_loc.cpp
0 → 100644
View file @
ee277f56
#include <unordered_map>
#include <vector>
#include <ctime>
#include <Python.h>
#include <structmember.h>
#include <frameobject.h>
static
PyObject
*
trace_func
;
/*
CC=/home/kmod/pyston_deps/llvm-trunk-build/Release/bin/clang++ CFLAGS='-Wno-write-strings -std=c++11' python setup.py build && time LD_PRELOAD='/home/kmod/pyston_deps/gcc-4.8.2-install/lib64/libstdc++.so' python test.py ../../minibenchmarks/raytrace.py
*/
namespace
std
{
template
<
typename
T1
,
typename
T2
>
struct
hash
<
pair
<
T1
,
T2
>
>
{
size_t
operator
()(
const
pair
<
T1
,
T2
>
p
)
const
{
return
hash
<
T1
>
()(
p
.
first
)
^
(
hash
<
T2
>
()(
p
.
second
)
<<
1
);
}
};
}
//struct Position : public std::pair<const char*, int> {
//constexpr Position (const char* c, const char*
//};
typedef
std
::
pair
<
const
char
*
,
int
>
Position
;
static
std
::
unordered_map
<
Position
,
double
>
times
;
static
double
*
next_time
=
NULL
;
static
double
prev_time
;
static
std
::
vector
<
Position
>
call_stack
;
double
floattime
()
{
struct
timeval
t
;
gettimeofday
(
&
t
,
NULL
);
return
(
double
)
t
.
tv_sec
+
t
.
tv_usec
*
0.000001
;
}
static
PyObject
*
trace
(
PyObject
*
self
,
PyObject
*
args
)
{
if
(
next_time
)
*
next_time
+=
(
floattime
()
-
prev_time
);
PyObject
*
_frame
;
char
*
event
;
PyObject
*
arg
;
if
(
!
PyArg_ParseTuple
(
args
,
"OsO"
,
&
_frame
,
&
event
,
&
arg
))
return
NULL
;
assert
(
PyFrame_Check
(
_frame
));
PyFrameObject
*
frame
=
(
PyFrameObject
*
)
_frame
;
PyObject
*
fn
=
frame
->
f_code
->
co_filename
;
char
*
fn_str
=
PyString_AsString
(
fn
);
//printf("'%s': %s:%d (%p)\n", event, fn_str, frame->f_lineno, frame->f_back);
if
(
strcmp
(
event
,
"call"
)
==
0
)
{
Position
p
(
fn_str
,
frame
->
f_lineno
);
call_stack
.
push_back
(
p
);
next_time
=
&
times
[
p
];
}
else
if
(
strcmp
(
event
,
"line"
)
==
0
||
strcmp
(
event
,
"exception"
)
==
0
)
{
Position
p
(
fn_str
,
frame
->
f_lineno
);
next_time
=
&
times
[
p
];
}
else
if
(
strcmp
(
event
,
"return"
)
==
0
)
{
Position
p
=
call_stack
.
back
();
call_stack
.
pop_back
();
next_time
=
&
times
[
p
];
}
else
{
printf
(
"unknown event: %s
\n
"
,
event
);
PyErr_SetString
(
PyExc_RuntimeError
,
"unknown event!"
);
Py_FatalError
(
"Unknown event!"
);
return
NULL
;
}
prev_time
=
floattime
();
Py_INCREF
(
trace_func
);
return
trace_func
;
}
static
PyObject
*
get_times
(
PyObject
*
self
,
PyObject
*
args
)
{
if
(
!
PyArg_ParseTuple
(
args
,
""
))
return
NULL
;
PyObject
*
d
=
PyDict_New
();
for
(
auto
&
p
:
times
)
{
// TODO reference handling
PyObject
*
fn
=
PyString_FromString
(
p
.
first
.
first
);
PyObject
*
lineno
=
PyInt_FromLong
(
p
.
first
.
second
);
PyObject
*
tuple
=
PyTuple_Pack
(
2
,
fn
,
lineno
);
PyObject
*
val
=
PyFloat_FromDouble
(
p
.
second
);
PyDict_SetItem
(
d
,
tuple
,
val
);
}
return
d
;
}
static
PyMethodDef
MeasureLocMethods
[]
=
{
{
"trace"
,
trace
,
METH_VARARGS
,
"Tracer."
},
{
"get_times"
,
get_times
,
METH_VARARGS
,
"Get logged times."
},
{
NULL
,
NULL
,
0
,
NULL
}
/* Sentinel */
};
PyMODINIT_FUNC
initmeasure_loc_ext
(
void
)
{
PyObject
*
m
;
m
=
Py_InitModule
(
"measure_loc_ext"
,
MeasureLocMethods
);
if
(
m
==
NULL
)
return
;
trace_func
=
PyObject_GetAttrString
(
m
,
"trace"
);
if
(
trace_func
==
NULL
)
return
;
}
tools/measure_loc_ext/setup.py
0 → 100644
View file @
ee277f56
from
distutils.core
import
setup
,
Extension
setup
(
name
=
"measure_loc"
,
version
=
"1.0"
,
description
=
"measures loc"
,
ext_modules
=
[
Extension
(
"measure_loc_ext"
,
sources
=
[
"measure_loc.cpp"
]),
],
)
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