Skip to content

Commit 48ebf95

Browse files
refactor
1 parent 89ffc47 commit 48ebf95

File tree

11 files changed

+142
-45
lines changed

11 files changed

+142
-45
lines changed

ddtrace/internal/datadog/profiling/dd_wrapper/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ add_library(
4747
src/ddup_interface.cpp
4848
src/profiler_stats.cpp
4949
src/profile.cpp
50+
src/profile_borrow.cpp
5051
src/sample.cpp
5152
src/sample_manager.cpp
5253
src/static_sample_pool.cpp

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
#pragma once
22

3-
#include <stddef.h>
4-
#include <stdint.h>
3+
#include <cstdint>
54
#include <string_view>
65
#include <unordered_map>
76

87
// Forward decl of the return pointer
98
namespace Datadog {
109
class Sample;
11-
}
10+
} // namespace Datadog
1211

1312
#ifdef __cplusplus
1413
extern "C"
@@ -68,6 +67,10 @@ extern "C"
6867
int64_t line);
6968
void ddup_push_absolute_ns(Datadog::Sample* sample, int64_t timestamp_ns);
7069
void ddup_push_monotonic_ns(Datadog::Sample* sample, int64_t monotonic_ns);
70+
71+
void ddup_increment_sampling_event_count();
72+
void ddup_increment_sample_count();
73+
7174
void ddup_flush_sample(Datadog::Sample* sample);
7275
// Stack v2 specific flush, which reverses the locations
7376
void ddup_flush_sample_v2(Datadog::Sample* sample);

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ extern "C"
1515

1616
namespace Datadog {
1717

18+
class ProfileBorrow;
19+
1820
// Serves to collect individual samples, as well as lengthen the scope of string data
1921
class Profile
2022
{
23+
friend class ProfileBorrow;
24+
2125
private:
2226
// Serialization for static state
2327
// - string table
@@ -45,6 +49,10 @@ class Profile
4549

4650
Datadog::ProfilerStats profiler_stats{};
4751

52+
// Internal access methods - not for direct use
53+
ddog_prof_Profile& profile_borrow_internal();
54+
void profile_release();
55+
4856
public:
4957
// State management
5058
void one_time_init(SampleType type, unsigned int _max_nframes);
@@ -53,11 +61,8 @@ class Profile
5361

5462
// Getters
5563
size_t get_sample_type_length();
56-
ddog_prof_Profile& profile_borrow();
57-
ProfilerStats& profiler_stats_borrow();
5864

59-
void profiler_stats_release();
60-
void profile_release();
65+
ProfileBorrow borrow();
6166

6267
// constref getters
6368
const ValueIndex& val();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
3+
#include "profile.hpp"
4+
5+
namespace Datadog {
6+
7+
// Forward declaration
8+
class Profile;
9+
10+
// RAII wrapper for borrowing both profile and stats under a single lock
11+
class ProfileBorrow
12+
{
13+
private:
14+
Profile* profile_ptr;
15+
16+
public:
17+
explicit ProfileBorrow(Profile& profile);
18+
~ProfileBorrow();
19+
20+
// Disable copy
21+
ProfileBorrow(const ProfileBorrow&) = delete;
22+
ProfileBorrow& operator=(const ProfileBorrow&) = delete;
23+
24+
// Enable move
25+
ProfileBorrow(ProfileBorrow&& other) noexcept;
26+
ProfileBorrow& operator=(ProfileBorrow&& other) noexcept;
27+
28+
// Accessors
29+
ddog_prof_Profile& profile();
30+
ProfilerStats& stats();
31+
};
32+
33+
} // namespace Datadog

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "libdatadog_helpers.hpp"
44
#include "profile.hpp"
5-
#include "profiler_stats.hpp"
5+
#include "profile_borrow.hpp"
66
#include "types.hpp"
77

88
#include <string>
@@ -135,9 +135,7 @@ class Sample
135135
// Flushes the current buffer, clearing it
136136
bool flush_sample(bool reverse_locations = false);
137137

138-
static ddog_prof_Profile& profile_borrow();
139-
static ProfilerStats& profiler_stats_borrow();
140-
static void profile_release();
138+
static ProfileBorrow profile_borrow();
141139
static void postfork_child();
142140
Sample(SampleType _type_mask, unsigned int _max_nframes);
143141

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,20 @@ ddup_push_monotonic_ns(Datadog::Sample* sample, int64_t monotonic_ns) // cppchec
303303
sample->push_monotonic_ns(monotonic_ns);
304304
}
305305

306+
void
307+
ddup_increment_sampling_event_count() // cppcheck-suppress unusedFunction
308+
{
309+
auto borrowed = Datadog::Sample::profile_borrow();
310+
borrowed.stats().increment_sampling_event_count();
311+
}
312+
313+
void
314+
ddup_increment_sample_count() // cppcheck-suppress unusedFunction
315+
{
316+
auto borrowed = Datadog::Sample::profile_borrow();
317+
borrowed.stats().increment_sample_count();
318+
}
319+
306320
void
307321
ddup_flush_sample(Datadog::Sample* sample) // cppcheck-suppress unusedFunction
308322
{
@@ -350,18 +364,19 @@ ddup_upload() // cppcheck-suppress unusedFunction
350364
// be modified. It gets released and cleared after uploading.
351365
// * Uploading cancels inflight uploads. There are better ways to do this, but this is what
352366
// we have for now.
353-
uploader.upload(Datadog::Sample::profile_borrow(), Datadog::Sample::profiler_stats_borrow());
354-
Datadog::Sample::profiler_stats_borrow().reset_state();
355-
Datadog::Sample::profile_release();
356-
return true;
367+
auto borrowed = Datadog::Sample::profile_borrow();
368+
bool success = uploader.upload(borrowed.profile(), borrowed.stats());
369+
borrowed.stats().reset_state();
370+
return success;
357371
}
358372

359373
void
360374
ddup_profile_set_endpoints(
361375
std::unordered_map<int64_t, std::string_view> span_ids_to_endpoints) // cppcheck-suppress unusedFunction
362376
{
363377
static bool already_warned = false; // cppcheck-suppress threadsafety-threadsafety
364-
ddog_prof_Profile& profile = Datadog::Sample::profile_borrow();
378+
auto borrowed = Datadog::Sample::profile_borrow();
379+
ddog_prof_Profile& profile = borrowed.profile();
365380
for (const auto& [span_id, trace_endpoint] : span_ids_to_endpoints) {
366381
ddog_CharSlice trace_endpoint_slice = Datadog::to_slice(trace_endpoint);
367382
auto res = ddog_prof_Profile_set_endpoint(&profile, span_id, trace_endpoint_slice);
@@ -375,14 +390,14 @@ ddup_profile_set_endpoints(
375390
ddog_Error_drop(&err);
376391
}
377392
}
378-
Datadog::Sample::profile_release();
379393
}
380394

381395
void
382396
ddup_profile_add_endpoint_counts(std::unordered_map<std::string_view, int64_t> trace_endpoints_to_counts)
383397
{
384398
static bool already_warned = false; // cppcheck-suppress threadsafety-threadsafety
385-
ddog_prof_Profile& profile = Datadog::Sample::profile_borrow();
399+
auto borrowed = Datadog::Sample::profile_borrow();
400+
ddog_prof_Profile& profile = borrowed.profile();
386401
for (const auto& [trace_endpoint, count] : trace_endpoints_to_counts) {
387402
ddog_CharSlice trace_endpoint_slice = Datadog::to_slice(trace_endpoint);
388403
auto res = ddog_prof_Profile_add_endpoint_count(&profile, trace_endpoint_slice, count);
@@ -396,5 +411,4 @@ ddup_profile_add_endpoint_counts(std::unordered_map<std::string_view, int64_t> t
396411
ddog_Error_drop(&err);
397412
}
398413
}
399-
Datadog::Sample::profile_release();
400414
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "profile.hpp"
22

33
#include "libdatadog_helpers.hpp"
4+
#include "profile_borrow.hpp"
5+
#include "profiler_stats.hpp"
46

57
#include <functional>
68
#include <iostream>
@@ -129,11 +131,16 @@ Datadog::Profile::get_sample_type_length()
129131
return samplers.size();
130132
}
131133

134+
Datadog::ProfileBorrow
135+
Datadog::Profile::borrow()
136+
{
137+
return ProfileBorrow(*this);
138+
}
139+
132140
ddog_prof_Profile&
133-
Datadog::Profile::profile_borrow()
141+
Datadog::Profile::profile_borrow_internal()
134142
{
135-
// We could wrap this in an object for better RAII, but since this
136-
// sequence is only used in a single place, we'll hold off on that sidequest.
143+
// Note: Caller is responsible for ensuring profile_release() is called
137144
profile_mtx.lock();
138145
return cur_profile;
139146
}
@@ -221,5 +228,6 @@ Datadog::Profile::postfork_child()
221228
{
222229
new (&profile_mtx) std::mutex();
223230
// Reset the profile to clear any samples collected in the parent process
231+
profiler_stats.reset_state();
224232
reset_profile();
225233
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "profile_borrow.hpp"
2+
#include "profile.hpp"
3+
4+
Datadog::ProfileBorrow::ProfileBorrow(Profile& profile)
5+
: profile_ptr(&profile)
6+
{
7+
// Lock the mutex on construction
8+
profile_ptr->profile_borrow_internal();
9+
}
10+
11+
Datadog::ProfileBorrow::~ProfileBorrow()
12+
{
13+
if (profile_ptr) {
14+
profile_ptr->profile_release();
15+
}
16+
}
17+
18+
Datadog::ProfileBorrow::ProfileBorrow(ProfileBorrow&& other) noexcept
19+
: profile_ptr(other.profile_ptr)
20+
{
21+
other.profile_ptr = nullptr;
22+
}
23+
24+
Datadog::ProfileBorrow&
25+
Datadog::ProfileBorrow::operator=(ProfileBorrow&& other) noexcept
26+
{
27+
if (this != &other) {
28+
// Release current lock if any
29+
if (profile_ptr) {
30+
profile_ptr->profile_release();
31+
}
32+
33+
// Take ownership from other
34+
profile_ptr = other.profile_ptr;
35+
other.profile_ptr = nullptr;
36+
}
37+
return *this;
38+
}
39+
40+
ddog_prof_Profile&
41+
Datadog::ProfileBorrow::profile()
42+
{
43+
return profile_ptr->cur_profile;
44+
}
45+
46+
Datadog::ProfilerStats&
47+
Datadog::ProfileBorrow::stats()
48+
{
49+
return profile_ptr->profiler_stats;
50+
}

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -523,22 +523,10 @@ Datadog::Sample::is_timeline_enabled() const
523523
return timeline_enabled;
524524
}
525525

526-
ddog_prof_Profile&
526+
Datadog::ProfileBorrow
527527
Datadog::Sample::profile_borrow()
528528
{
529-
return profile_state.profile_borrow();
530-
}
531-
532-
Datadog::ProfilerStats&
533-
Datadog::Sample::profiler_stats_borrow()
534-
{
535-
return profile_state.profiler_stats_borrow();
536-
}
537-
538-
void
539-
Datadog::Sample::profile_release()
540-
{
541-
profile_state.profile_release();
529+
return profile_state.borrow();
542530
}
543531

544532
void

ddtrace/internal/datadog/profiling/stack_v2/include/stack_renderer.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
#pragma once
22

3-
#include <chrono>
4-
#include <fstream>
5-
#include <memory>
6-
#include <mutex>
73
#include <string>
84
#include <string_view>
9-
#include <thread>
10-
#include <vector>
115

126
#include "python_headers.hpp"
137

0 commit comments

Comments
 (0)