libdd-profiling 1.0.0

Continuous profiling library supporting pprof format collection and export to Datadog
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// Copyright 2021-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0

use libdd_profiling_protobuf::prost_impls;
use std::ops::{Add, Sub};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct ValueType<'a> {
    pub r#type: &'a str,
    pub unit: &'a str,
}

impl<'a> ValueType<'a> {
    #[inline(always)]
    pub const fn new(r#type: &'a str, unit: &'a str) -> Self {
        Self { r#type, unit }
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Period<'a> {
    pub r#type: ValueType<'a>,
    pub value: i64,
}

#[derive(Copy, Clone, Default, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct ManagedStringId {
    pub value: u32,
}

impl ManagedStringId {
    pub const fn empty() -> Self {
        Self::new(0)
    }

    pub const fn new(value: u32) -> Self {
        ManagedStringId { value }
    }
}

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct Mapping<'a> {
    /// Address at which the binary (or DLL) is loaded into memory.
    pub memory_start: u64,

    /// The limit of the address range occupied by this mapping.
    pub memory_limit: u64,

    /// Offset in the binary that corresponds to the first mapped address.
    pub file_offset: u64,

    /// The object this entry is loaded from.  This can be a filename on
    /// disk for the main binary and shared libraries, or virtual
    /// abstractions like "[vdso]".
    pub filename: &'a str,

    /// A string that uniquely identifies a particular program version
    /// with high probability. E.g., for binaries generated by GNU tools,
    /// it could be the contents of the .note.gnu.build-id field.
    pub build_id: &'a str,
}

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
// Same as Mapping, but using StringIds
pub struct StringIdMapping {
    pub memory_start: u64,
    pub memory_limit: u64,
    pub file_offset: u64,
    pub filename: ManagedStringId,
    pub build_id: ManagedStringId,
}

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct Function<'a> {
    /// Name of the function, in human-readable form if available.
    pub name: &'a str,

    /// Name of the function, as identified by the system.
    /// For instance, it can be a C++ mangled name.
    pub system_name: &'a str,

    /// Source file containing the function.
    pub filename: &'a str,
}

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
// Same as Function, but using StringIds
pub struct StringIdFunction {
    pub name: ManagedStringId,
    pub system_name: ManagedStringId,
    pub filename: ManagedStringId,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Line<'a> {
    /// The corresponding profile.Function for this line.
    pub function: Function<'a>,

    /// Line number in source code.
    pub line: i64,
}

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct Location<'a> {
    pub mapping: Mapping<'a>,
    pub function: Function<'a>,

    /// The instruction address for this location, if available.  It
    /// should be within [Mapping.memory_start...Mapping.memory_limit]
    /// for the corresponding mapping. A non-leaf address may be in the
    /// middle of a call instruction. It is up to display tools to find
    /// the beginning of the instruction if necessary.
    pub address: u64,
    pub line: i64,
}

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
// Same as Location, but using StringIds
pub struct StringIdLocation {
    pub mapping: StringIdMapping,
    pub function: StringIdFunction,
    pub address: u64,
    pub line: i64,
}

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct Label<'a> {
    pub key: &'a str,

    /// At most one of the following must be present
    pub str: &'a str,
    pub num: i64,

    /// Should only be present when num is present.
    /// Specifies the units of num.
    /// Use arbitrary string (for example, "requests") as a custom count unit.
    /// If no unit is specified, consumer may apply heuristic to deduce the unit.
    /// Consumers may also  interpret units like "bytes" and "kilobytes" as memory
    /// units and units like "seconds" and "nanoseconds" as time units,
    /// and apply appropriate unit conversions to these.
    pub num_unit: &'a str,
}

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
// Same as Label, but using StringIds
pub struct StringIdLabel {
    pub key: ManagedStringId,

    /// At most one of the following must be present
    pub str: ManagedStringId,
    pub num: i64,

    /// Should only be present when num is present.
    pub num_unit: ManagedStringId,
}

impl Label<'_> {
    pub fn uses_at_most_one_of_str_and_num(&self) -> bool {
        self.str.is_empty() || (self.num == 0 && self.num_unit.is_empty())
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Sample<'a> {
    /// The leaf is at locations[0].
    pub locations: Vec<Location<'a>>,

    /// The type and unit of each value is defined by the corresponding
    /// entry in Profile.sample_type. All samples must have the same
    /// number of values, the same as the length of Profile.sample_type.
    /// When aggregating multiple samples into a single sample, the
    /// result has a list of values that is the element-wise sum of the
    /// lists of the originals.
    pub values: &'a [i64],

    /// label includes additional context for this sample. It can include
    /// things like a thread id, allocation size, etc
    pub labels: Vec<Label<'a>>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
// Same as Sample, but using StringIds
pub struct StringIdSample<'a> {
    pub locations: Vec<StringIdLocation>,
    pub values: &'a [i64],
    pub labels: Vec<StringIdLabel>,
}

#[derive(Debug)]
#[cfg_attr(test, derive(bolero::generator::TypeGenerator))]
pub enum UpscalingInfo {
    Poisson {
        // sum_value_offset and count_value_offset are offsets in the profile values type array
        sum_value_offset: usize,
        count_value_offset: usize,
        sampling_distance: u64,
    },
    PoissonNonSampleTypeCount {
        // sum_value_offset is an offset in the profile values type array
        sum_value_offset: usize,
        count_value: u64,
        sampling_distance: u64,
    },
    Proportional {
        scale: f64,
    },
}

impl std::fmt::Display for UpscalingInfo {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            UpscalingInfo::Poisson {
                sum_value_offset,
                count_value_offset,
                sampling_distance,
            } => write!(
                f,
                "Poisson = sum_value_offset: {sum_value_offset}, count_value_offset: {count_value_offset}, sampling_distance: {sampling_distance}"
            ),
            UpscalingInfo::PoissonNonSampleTypeCount {
                sum_value_offset,
                count_value,
                sampling_distance,
            } => write!(
                f,
                "Poisson = sum_value_offset: {sum_value_offset}, count_value: {count_value}, sampling_distance: {sampling_distance}",
            ),
            UpscalingInfo::Proportional { scale } => {
                write!(f, "Proportional = scale: {scale}")
            }
        }
    }
}

impl UpscalingInfo {
    pub fn check_validity(&self, number_of_values: usize) -> anyhow::Result<()> {
        match self {
            UpscalingInfo::Poisson {
                sum_value_offset,
                count_value_offset,
                sampling_distance,
            } => {
                anyhow::ensure!(
                    sum_value_offset < &number_of_values && count_value_offset < &number_of_values,
                    "sum_value_offset {sum_value_offset} and count_value_offset {count_value_offset} must be strictly less than {number_of_values}"
                );
                anyhow::ensure!(
                    sampling_distance != &0,
                    "sampling_distance {sampling_distance} must be greater than 0"
                )
            }
            UpscalingInfo::PoissonNonSampleTypeCount {
                sum_value_offset,
                count_value,
                sampling_distance,
            } => {
                anyhow::ensure!(
                    sum_value_offset < &number_of_values,
                    "sum_value_offset {sum_value_offset} must be strictly less than {number_of_values}"
                );
                anyhow::ensure!(
                    count_value != &0,
                    "count_value {count_value} must be greater than 0"
                );
                anyhow::ensure!(
                    sampling_distance != &0,
                    "sampling_distance {sampling_distance} must be greater than 0"
                )
            }
            UpscalingInfo::Proportional { scale: _ } => (),
        }
        anyhow::Ok(())
    }
}

pub struct Profile<'a> {
    pub duration: Duration,
    pub period: Option<(i64, ValueType<'a>)>,
    pub sample_types: Vec<ValueType<'a>>,
    pub samples: Vec<Sample<'a>>,
    pub start_time: SystemTime,
}

fn string_table_fetch(pprof: &prost_impls::Profile, id: i64) -> anyhow::Result<&String> {
    pprof
        .string_table
        .get(id as u64 as usize)
        .ok_or_else(|| anyhow::anyhow!("String {id} was not found."))
}

fn mapping_fetch(pprof: &prost_impls::Profile, id: u64) -> anyhow::Result<Mapping<'_>> {
    if id == 0 {
        return Ok(Mapping::default());
    }

    match pprof.mappings.iter().find(|item| item.id == id) {
        Some(mapping) => Ok(Mapping {
            memory_start: mapping.memory_start,
            memory_limit: mapping.memory_limit,
            file_offset: mapping.file_offset,
            filename: string_table_fetch(pprof, mapping.filename)?,
            build_id: string_table_fetch(pprof, mapping.build_id)?,
        }),
        None => anyhow::bail!("Mapping {id} was not found."),
    }
}

fn function_fetch(pprof: &prost_impls::Profile, id: u64) -> anyhow::Result<Function<'_>> {
    if id == 0 {
        return Ok(Function::default());
    }

    match pprof.functions.iter().find(|item| item.id == id) {
        Some(function) => Ok(Function {
            name: string_table_fetch(pprof, function.name)?,
            system_name: string_table_fetch(pprof, function.system_name)?,
            filename: string_table_fetch(pprof, function.filename)?,
        }),
        None => anyhow::bail!("Function {id} was not found."),
    }
}

fn location_fetch(pprof: &prost_impls::Profile, id: u64) -> anyhow::Result<Location<'_>> {
    if id == 0 {
        return Ok(Location::default());
    }

    match pprof.locations.iter().find(|item| item.id == id) {
        Some(location) => {
            anyhow::ensure!(!location.is_folded, "expected Location to not be folded");
            anyhow::ensure!(
                location.lines.len() == 1,
                "expected Location to have exactly 1 Line"
            );
            // Safety: guarded by len check above.
            let line = unsafe { location.lines.get_unchecked(0) };
            let function = function_fetch(pprof, line.function_id)?;

            Ok(Location {
                mapping: mapping_fetch(pprof, location.mapping_id)?,
                function,
                address: location.address,
                line: line.line,
            })
        }
        None => anyhow::bail!("Location {id} was not found."),
    }
}

fn locations_fetch<'a>(
    pprof: &'a prost_impls::Profile,
    ids: &'a [u64],
) -> anyhow::Result<Vec<Location<'a>>> {
    let mut locations = Vec::with_capacity(ids.len());
    for id in ids {
        let location = location_fetch(pprof, *id)?;
        locations.push(location);
    }
    Ok(locations)
}

impl<'a> TryFrom<&'a prost_impls::Profile> for Profile<'a> {
    type Error = anyhow::Error;

    fn try_from(pprof: &'a prost_impls::Profile) -> Result<Self, Self::Error> {
        assert!(pprof.duration_nanos >= 0);
        let duration = Duration::from_nanos(pprof.duration_nanos as u64);
        let start_time = if pprof.time_nanos.is_negative() {
            UNIX_EPOCH.sub(Duration::from_nanos(pprof.time_nanos.unsigned_abs()))
        } else {
            UNIX_EPOCH.add(Duration::from_nanos(pprof.time_nanos as u64))
        };

        let period = match pprof.period_type {
            Some(t) => {
                let r#type = ValueType::new(
                    string_table_fetch(pprof, t.r#type)?,
                    string_table_fetch(pprof, t.unit)?,
                );
                Some((pprof.period, r#type))
            }
            None => None,
        };

        let mut sample_types = Vec::with_capacity(pprof.samples.len());
        for t in pprof.sample_types.iter() {
            sample_types.push(ValueType::new(
                string_table_fetch(pprof, t.r#type)?,
                string_table_fetch(pprof, t.unit)?,
            ));
        }

        let mut samples = Vec::with_capacity(pprof.samples.len());
        for sample in pprof.samples.iter() {
            let locations = locations_fetch(pprof, &sample.location_ids)?;

            let mut labels = Vec::with_capacity(sample.labels.len());
            for label in sample.labels.iter() {
                labels.push(Label {
                    key: string_table_fetch(pprof, label.key)?,
                    str: string_table_fetch(pprof, label.str)?,
                    num: label.num,
                    num_unit: string_table_fetch(pprof, label.num_unit)?,
                })
            }
            let sample = Sample {
                locations,
                values: &sample.values,
                labels,
            };
            samples.push(sample);
        }

        Ok(Profile {
            duration,
            period,
            sample_types,
            samples,
            start_time,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn label_uses_at_most_one_of_str_and_num() {
        let label = Label {
            key: "name",
            str: "levi",
            num: 0,
            num_unit: "name", // can't use num_unit with str
        };
        assert!(!label.uses_at_most_one_of_str_and_num());

        let label = Label {
            key: "name",
            str: "levi",
            num: 10, // can't use num with str
            num_unit: "",
        };
        assert!(!label.uses_at_most_one_of_str_and_num());

        let label = Label {
            key: "name",
            str: "levi",
            num: 0,
            num_unit: "",
        };
        assert!(label.uses_at_most_one_of_str_and_num());

        let label = Label {
            key: "process_id",
            str: "",
            num: 0,
            num_unit: "",
        };
        assert!(label.uses_at_most_one_of_str_and_num());

        let label = Label {
            key: "local root span id",
            str: "",
            num: 10901,
            num_unit: "",
        };
        assert!(label.uses_at_most_one_of_str_and_num());

        let label = Label {
            key: "duration",
            str: "",
            num: 12345,
            num_unit: "nanoseconds",
        };
        assert!(label.uses_at_most_one_of_str_and_num());
    }
}