Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alloc profiler: rename structs to match Julia C style #16

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions src/gc-alloc-profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,42 @@
using std::string;
using std::vector;

struct RawBacktrace {
struct jl_raw_backtrace_t {
jl_bt_element_t *data;
size_t size;
};

struct RawAlloc {
struct jl_raw_alloc_t {
jl_datatype_t *type_address;
RawBacktrace backtrace;
jl_raw_backtrace_t backtrace;
size_t size;
};

// == These structs define the global singleton profile buffer that will be used by
// callbacks to store profile results. ==
struct PerThreadAllocProfile {
vector<RawAlloc> allocs;
struct jl_per_thread_alloc_profile_t {
vector<jl_raw_alloc_t> allocs;
};

struct AllocProfile {
struct jl_alloc_profile_t {
double sample_rate;

vector<PerThreadAllocProfile> per_thread_profiles;
vector<jl_per_thread_alloc_profile_t> per_thread_profiles;
};

struct CombinedResults {
vector<RawAlloc> combined_allocs;
struct jl_combined_results {
vector<jl_raw_alloc_t> combined_allocs;
};

// == Global variables manipulated by callbacks ==

AllocProfile g_alloc_profile;
jl_alloc_profile_t g_alloc_profile;
int g_alloc_profile_enabled = false;
CombinedResults g_combined_results; // Will live forever.
jl_combined_results g_combined_results; // Will live forever.

// === stack stuff ===

RawBacktrace get_raw_backtrace() {
jl_raw_backtrace_t get_raw_backtrace() {
// A single large buffer to record backtraces onto
static jl_bt_element_t static_bt_data[JL_MAX_BT_SIZE];

Expand All @@ -57,7 +57,7 @@ RawBacktrace get_raw_backtrace() {
jl_bt_element_t *bt_data = (jl_bt_element_t*) malloc(bt_bytes);
memcpy(bt_data, static_bt_data, bt_bytes);

return RawBacktrace{
return jl_raw_backtrace_t{
bt_data,
bt_size
};
Expand All @@ -70,14 +70,14 @@ extern "C" { // Needed since these functions doesn't take any arguments.
JL_DLLEXPORT void jl_start_alloc_profile(double sample_rate) {
// We only need to do this once, the first time this is called.
while (g_alloc_profile.per_thread_profiles.size() < jl_n_threads) {
g_alloc_profile.per_thread_profiles.push_back(PerThreadAllocProfile{});
g_alloc_profile.per_thread_profiles.push_back(jl_per_thread_alloc_profile_t{});
}

g_alloc_profile.sample_rate = sample_rate;
g_alloc_profile_enabled = true;
}

JL_DLLEXPORT struct RawAllocResults jl_fetch_alloc_profile() {
JL_DLLEXPORT jl_raw_alloc_results_t jl_fetch_alloc_profile() {
// combine allocs
// TODO: interleave to preserve ordering
for (auto& profile : g_alloc_profile.per_thread_profiles) {
Expand All @@ -88,7 +88,7 @@ JL_DLLEXPORT struct RawAllocResults jl_fetch_alloc_profile() {
profile.allocs.clear();
}

return RawAllocResults{
return jl_raw_alloc_results_t{
g_combined_results.combined_allocs.data(),
g_combined_results.combined_allocs.size(),
};
Expand Down Expand Up @@ -129,7 +129,7 @@ void _maybe_record_alloc_to_profile(jl_value_t *val, size_t size) JL_NOTSAFEPOIN
}

auto type = (jl_datatype_t*)jl_typeof(val);
profile.allocs.emplace_back(RawAlloc{
profile.allocs.emplace_back(jl_raw_alloc_t{
type,
get_raw_backtrace(),
size
Expand Down
10 changes: 5 additions & 5 deletions src/gc-alloc-profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ extern "C" {
// ---------------------------------------------------------------------

// Forward-declaration to avoid depenency in header file.
struct RawAlloc; // Defined in gc-alloc-profiler.cpp
struct jl_raw_alloc_t; // Defined in gc-alloc-profiler.cpp

struct RawAllocResults {
struct RawAlloc *allocs;
typedef struct {
struct jl_raw_alloc_t *allocs;
size_t num_allocs;
};
} jl_raw_alloc_results_t;

JL_DLLEXPORT void jl_start_alloc_profile(double sample_rate);
JL_DLLEXPORT struct RawAllocResults jl_fetch_alloc_profile(void);
JL_DLLEXPORT jl_raw_alloc_results_t jl_fetch_alloc_profile(void);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd still probably push for one more rename to call this jl_profile_allocs_raw_results_t, since the current thing doesn't indicate it's something for profiling

JL_DLLEXPORT void jl_stop_alloc_profile(void);
JL_DLLEXPORT void jl_free_alloc_profile(void);

Expand Down
8 changes: 4 additions & 4 deletions stdlib/Profile/src/Allocs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ using Base: InterpreterIP
# The C jl_bt_element_t object contains either an IP pointer (size_t) or a void*.
const BTElement = Csize_t;

# matches RawBacktrace on the C side
# matches jl_raw_backtrace_t on the C side
struct RawBacktrace
data::Ptr{BTElement} # in C: *jl_bt_element_t
size::Csize_t
end

# matches RawAlloc on the C side
# matches jl_raw_alloc_t on the C side
struct RawAlloc
type::Ptr{Type}
backtrace::RawBacktrace
size::Csize_t
end

# matches RawAllocResults on the C side
# matches jl_raw_alloc_results_t on the C side
struct RawAllocResults
allocs::Ptr{RawAlloc}
num_allocs::Csize_t
Expand Down Expand Up @@ -115,7 +115,7 @@ struct AllocResults
allocs::Vector{Alloc}
end

# Without this, the Alloc's stacktrace prints for lines and lines and lines..
# Without this, the Alloc's stacktrace prints for lines and lines and lines...
function Base.show(io::IO, a::Alloc)
stacktrace_sample = length(a.stacktrace) >= 1 ? "$(a.stacktrace[1]), ..." : ""
print(io, "$Alloc($(a.type), $StackFrame[$stacktrace_sample], $(a.size))")
Expand Down