Commit 441fc1c
authored
feat(profiling): make Profile upload lock-free (#15302)
## Description
https://datadoghq.atlassian.net/browse/PROF-13044
This PR reduces locking around the libdatadog Profile object to make
uploading non-blocking for sampling. Previously, the Scheduler thread
(which uploads every once in a while) would take the Lock on the Profile
object, preventing any new Samples from being pushed to it until the
upload finishes. Although we don't have any numbers on that, it is
possible that this would trigger adaptive sampling or make us lose some
data.
The new implementation doesn't make uploading completely lock-free, but
already improves it by eliminating the need for locking _during the
upload_. We still need to lock the `Profile` object when we encode it.
It may be possible to optimise this further by having two `Profile`
objects (one we would be writing to, and one that would be _being
uploaded_) but this requires more complicated lock handling to actually
be safe. (Reason is, having two Profiles is still a problem if the
previous encoding/uploading hasn't finished when the next needs to be
encoded/uploaded.)
In the new implementation, the Profile Lock is acquired just before
encoding is done, and released right after. Profiler Stats are also
copied and reset while the Lock is held.
In order to make that work, I had to slightly adapt the interface of
`Uploader` for it to work an `EncodedProfile` instead of a `Profile`,
which allowed me to hoist the encoding outside of the `Uploader` (to the
`UploaderBuilder`), and thus only hold the Profile Lock during
`UploaderBuilder::Build` (as opposed to during the whole of
`Uploader::upload`).
`ProfilerStats` are captured and reset by `std::swap`-ping them with an
empty `ProfilerStats` object, eliminating the previous "singleton-like"
`ProfilerStats` we had. (This works and is trivial because
`ProfilerStats` is a C++ object; we cannot do the same for the `Profile`
itself, as it is managed by the Rust library.)
`Uploader`'s interface has also slightly changed – it is now constructed
with the `ProfilerStats` and `EncodedProfile` directly (not taken as
argument for `upload`).1 parent 274a440 commit 441fc1c
File tree
9 files changed
+75
-54
lines changed- ddtrace/internal/datadog/profiling/dd_wrapper
- include
- src
9 files changed
+75
-54
lines changedLines changed: 1 addition & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
50 | | - | |
| 49 | + | |
51 | 50 | | |
52 | 51 | | |
53 | 52 | | |
| |||
Lines changed: 1 addition & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
| |||
17 | 16 | | |
18 | 17 | | |
19 | 18 | | |
20 | | - | |
21 | | - | |
22 | 19 | | |
23 | 20 | | |
24 | 21 | | |
| |||
37 | 34 | | |
38 | 35 | | |
39 | 36 | | |
40 | | - | |
41 | | - | |
42 | | - | |
| 37 | + | |
43 | 38 | | |
44 | 39 | | |
45 | 40 | | |
| |||
Lines changed: 16 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
| 26 | + | |
25 | 27 | | |
26 | | - | |
| 28 | + | |
27 | 29 | | |
28 | 30 | | |
29 | | - | |
| 31 | + | |
30 | 32 | | |
31 | 33 | | |
32 | 34 | | |
33 | 35 | | |
34 | 36 | | |
35 | 37 | | |
36 | 38 | | |
37 | | - | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
38 | 43 | | |
39 | 44 | | |
40 | 45 | | |
| |||
44 | 49 | | |
45 | 50 | | |
46 | 51 | | |
| 52 | + | |
47 | 53 | | |
48 | 54 | | |
49 | 55 | | |
| |||
67 | 73 | | |
68 | 74 | | |
69 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
70 | 79 | | |
71 | 80 | | |
72 | 81 | | |
| |||
75 | 84 | | |
76 | 85 | | |
77 | 86 | | |
| 87 | + | |
78 | 88 | | |
79 | 89 | | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
80 | 93 | | |
81 | 94 | | |
82 | 95 | | |
| |||
Lines changed: 8 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
347 | 347 | | |
348 | 348 | | |
349 | 349 | | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
350 | 353 | | |
351 | 354 | | |
352 | 355 | | |
| |||
359 | 362 | | |
360 | 363 | | |
361 | 364 | | |
362 | | - | |
363 | | - | |
364 | | - | |
365 | | - | |
366 | | - | |
367 | | - | |
368 | | - | |
369 | | - | |
370 | | - | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
371 | 370 | | |
372 | 371 | | |
373 | 372 | | |
| |||
Lines changed: 3 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
8 | 7 | | |
9 | 8 | | |
10 | 9 | | |
| |||
58 | 57 | | |
59 | 58 | | |
60 | 59 | | |
61 | | - | |
| 60 | + | |
62 | 61 | | |
63 | 62 | | |
64 | 63 | | |
| |||
189 | 188 | | |
190 | 189 | | |
191 | 190 | | |
192 | | - | |
| 191 | + | |
193 | 192 | | |
194 | 193 | | |
195 | 194 | | |
| |||
228 | 227 | | |
229 | 228 | | |
230 | 229 | | |
231 | | - | |
| 230 | + | |
232 | 231 | | |
233 | 232 | | |
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
| 49 | + | |
50 | 50 | | |
Lines changed: 3 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
| |||
46 | 45 | | |
47 | 46 | | |
48 | 47 | | |
49 | | - | |
| 48 | + | |
50 | 49 | | |
51 | 50 | | |
| 51 | + | |
52 | 52 | | |
53 | 53 | | |
54 | | - | |
| 54 | + | |
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
| |||
Lines changed: 14 additions & 22 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
17 | 20 | | |
18 | 21 | | |
| 22 | + | |
| 23 | + | |
19 | 24 | | |
20 | 25 | | |
21 | 26 | | |
22 | 27 | | |
23 | 28 | | |
24 | 29 | | |
25 | 30 | | |
26 | | - | |
| 31 | + | |
27 | 32 | | |
28 | 33 | | |
29 | 34 | | |
| |||
37 | 42 | | |
38 | 43 | | |
39 | 44 | | |
40 | | - | |
| 45 | + | |
| 46 | + | |
41 | 47 | | |
42 | 48 | | |
43 | 49 | | |
| |||
63 | 69 | | |
64 | 70 | | |
65 | 71 | | |
66 | | - | |
| 72 | + | |
67 | 73 | | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | 74 | | |
81 | | - | |
82 | | - | |
83 | | - | |
| 75 | + | |
84 | 76 | | |
85 | 77 | | |
86 | 78 | | |
| |||
95 | 87 | | |
96 | 88 | | |
97 | 89 | | |
98 | | - | |
| 90 | + | |
| 91 | + | |
99 | 92 | | |
100 | 93 | | |
101 | 94 | | |
102 | | - | |
| 95 | + | |
103 | 96 | | |
104 | 97 | | |
105 | 98 | | |
| |||
110 | 103 | | |
111 | 104 | | |
112 | 105 | | |
113 | | - | |
114 | 106 | | |
115 | 107 | | |
116 | 108 | | |
| |||
Lines changed: 28 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
186 | 187 | | |
187 | 188 | | |
188 | 189 | | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
189 | 213 | | |
190 | | - | |
| 214 | + | |
191 | 215 | | |
192 | 216 | | |
193 | 217 | | |
194 | 218 | | |
195 | 219 | | |
196 | | - | |
197 | | - | |
198 | | - | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
199 | 223 | | |
0 commit comments