Skip to content

Commit 9adf42e

Browse files
wip
1 parent 24ad01a commit 9adf42e

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

ddtrace/internal/datadog/profiling/dd_wrapper/include/profile.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ class Profile
4646
// The profile object is initialized here as a skeleton object, but it
4747
// cannot be used until it's initialized by libdatadog
4848
ddog_prof_Profile cur_profile{};
49+
ddog_prof_Profile last_profile{};
4950

5051
Datadog::ProfilerStats cur_profiler_stats{};
52+
Datadog::ProfilerStats last_profiler_stats{};
5153

5254
// Internal access methods - not for direct use
5355
ddog_prof_Profile& profile_borrow_internal();

ddtrace/internal/datadog/profiling/dd_wrapper/src/profile.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,20 @@ Datadog::Profile::reset_profile()
5858
return false;
5959
}
6060

61+
res = ddog_prof_Profile_reset(&last_profile);
62+
if (!res.ok) { // NOLINT (cppcoreguidelines-pro-type-union-access)
63+
auto err = res.err; // NOLINT (cppcoreguidelines-pro-type-union-access)
64+
if (!already_warned) {
65+
already_warned = true;
66+
const std::string errmsg = err_to_msg(&err, "Error resetting last_profile");
67+
std::cerr << "Could not drop last_profile:" << errmsg << std::endl;
68+
}
69+
ddog_Error_drop(&err);
70+
return false;
71+
}
72+
6173
cur_profiler_stats.reset_state();
74+
last_profiler_stats.reset_state();
6275
return true;
6376
}
6477

@@ -189,7 +202,15 @@ Datadog::Profile::one_time_init(SampleType type, unsigned int _max_nframes)
189202
if (!make_profile(sample_types, &default_period, cur_profile)) {
190203
if (!already_warned) {
191204
already_warned = true;
192-
std::cerr << "Error initializing profile" << std::endl;
205+
std::cerr << "Error initializing cur_profile" << std::endl;
206+
}
207+
return;
208+
}
209+
210+
if (!make_profile(sample_types, &default_period, last_profile)) {
211+
if (!already_warned) {
212+
already_warned = true;
213+
std::cerr << "Error initializing last_profile" << std::endl;
193214
}
194215
return;
195216
}
@@ -229,5 +250,6 @@ Datadog::Profile::postfork_child()
229250
new (&profile_mtx) std::mutex();
230251
// Reset the profile to clear any samples collected in the parent process
231252
cur_profiler_stats.reset_state();
253+
last_profiler_stats.reset_state();
232254
reset_profile();
233255
}

ddtrace/internal/datadog/profiling/dd_wrapper/src/profile_borrow.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Datadog::ProfileBorrow::ProfileBorrow(Profile& profile)
66
{
77
// Lock the mutex on construction
88
profile_ptr->profile_borrow_internal();
9+
10+
std::swap(profile_ptr->cur_profile, profile_ptr->last_profile);
11+
std::swap(profile_ptr->cur_profiler_stats, profile_ptr->last_profiler_stats);
912
}
1013

1114
Datadog::ProfileBorrow::~ProfileBorrow()
@@ -40,11 +43,11 @@ Datadog::ProfileBorrow::operator=(ProfileBorrow&& other) noexcept
4043
ddog_prof_Profile&
4144
Datadog::ProfileBorrow::profile()
4245
{
43-
return profile_ptr->cur_profile;
46+
return profile_ptr->last_profile;
4447
}
4548

4649
Datadog::ProfilerStats&
4750
Datadog::ProfileBorrow::stats()
4851
{
49-
return profile_ptr->cur_profiler_stats;
52+
return profile_ptr->last_profiler_stats;
5053
}

0 commit comments

Comments
 (0)