Created
January 2, 2013 12:14
-
-
Save anonymous/4434156 to your computer and use it in GitHub Desktop.
Sampling profiler for Julia (test).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ProfNew | |
# Initialize the profile data structures | |
# Have the timer fire every 1ms | |
# Delete the last 3 items on the bt | |
ccall(:profile_init, Void, (Uint64, Int32), 1_000_000, 0) | |
profile_start_timer() = ccall(:profile_start_timer, Void, ()) | |
profile_stop_timer() = ccall(:profile_stop_timer, Void, ()) | |
get_profile_data() = pointer_to_array( | |
convert(Ptr{Uint}, ccall(:get_profile_data, Ptr{Uint8}, ())), (convert(Int, ccall(:get_profile_data_len, Int32, ())),)) | |
clear_profile_data() = ccall(:clear_profile_data, Void, ()) | |
# This is a simple linecount parser, the more interesting case is following the backtrace. Not hard, but currently premature. | |
function parse_backtrace() | |
linecount = (Uint=>Int)[] | |
data = get_profile_data() | |
for i = 1:length(data) | |
if data[i] == 0 | |
continue | |
end | |
linecount[data[i]] = get(linecount, data[i], 0)+1 | |
end | |
buf = Array(Uint, 0) | |
n = Array(Int, 0) | |
for (k,v) in linecount | |
push(buf, k) | |
push(n, v) | |
end | |
@show buf | |
parsed = ccall(:jl_parse_backtrace, Array{Any, 1}, (Uint, Ptr{Uint}), length(buf), buf) | |
return parsed, n | |
end | |
macro sprofile(ex) | |
quote | |
try | |
profile_start_timer() | |
$(esc(ex)) | |
finally | |
profile_stop_timer() | |
end | |
end | |
end | |
function testlen() | |
x = randn(2000000) | |
y = sum(x) | |
println("length = ", ccall(:get_profile_data_len, Int32, ())) | |
y = sum(x) | |
println("length = ", ccall(:get_profile_data_len, Int32, ())) | |
y = sum(x) | |
println("length = ", ccall(:get_profile_data_len, Int32, ())) | |
y = sum(x) | |
println("length = ", ccall(:get_profile_data_len, Int32, ())) | |
y = sum(x) | |
println("length = ", ccall(:get_profile_data_len, Int32, ())) | |
y = sum(x) | |
end | |
export @sprofile, testlen, parse_backtrace | |
end # module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment