Skip to content

Commit 4b986a3

Browse files
committed
fix: workaround Option<*Id> not lowering to *mut void
1 parent 0ae34c7 commit 4b986a3

File tree

9 files changed

+31
-68
lines changed

9 files changed

+31
-68
lines changed

datadog-profiling-ffi/cbindgen.toml

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -80,33 +80,6 @@ renaming_overrides_prefixing = true
8080
"StringTableLookupResult" = "ddog_prof_StringTable_LookupResult"
8181
"StringTableNewResult" = "ddog_prof_StringTable_NewResult"
8282

83-
"CbindgenIsDumbStringId" = "ddog_prof_StringId"
84-
85-
"Slice_GenerationalIdLabelId" = "ddog_prof_Slice_LabelId"
86-
"Slice_GenerationalIdLocationId" = "ddog_prof_Slice_LocationId"
87-
88-
"GenerationalId_FunctionId" = "ddog_prof_FunctionId"
89-
"Result_GenerationalIdFunctionId" = "ddog_prof_FunctionId_Result"
90-
91-
"GenerationalId_LabelId" = "ddog_prof_LabelId"
92-
"Result_GenerationalIdLabelId" = "ddog_prof_LabelId_Result"
93-
94-
"GenerationalId_LabelSetId" = "ddog_prof_LabelSetId"
95-
"Result_GenerationalIdLabelSetId" = "ddog_prof_LabelSetId_Result"
96-
"LabelSetId" = "OpaqueLabelSetId"
97-
98-
"GenerationalId_LocationId" = "ddog_prof_LocationId"
99-
"Result_GenerationalIdLocationId" = "ddog_prof_LocationId_Result"
100-
101-
"GenerationalId_MappingId" = "ddog_prof_MappingId"
102-
"Result_GenerationalIdMappingId" = "ddog_prof_MappingId_Result"
103-
104-
"GenerationalId_StackTraceId" = "ddog_prof_StackTraceId"
105-
"Result_GenerationalIdStackTraceId" = "ddog_prof_StackTraceId_Result"
106-
107-
"GenerationalId_StringId" = "ddog_prof_StringId"
108-
"Result_GenerationalIdStringId" = "ddog_prof_StringId_Result"
109-
11083
"HandleProfileExporter" = "ddog_prof_ProfileExporter"
11184
"Handle_ProfileExporter" = "ddog_prof_ProfileExporter"
11285
"Result_HandleProfileExporter" = "ddog_prof_ProfileExporter_Result"

datadog-profiling-ffi/src/profiles/profiling_dictionary.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ use datadog_profiling::profiles::datatypes::{
1212
use datadog_profiling::profiles::ProfileError;
1313
use ddcommon_ffi::CharSlice;
1414

15+
/// A StringId that represents the empty string. This is always available in
16+
/// every string set and can be used without needing to insert an empty string.
17+
#[no_mangle]
18+
pub static DDOG_PROF_STRINGID_EMPTY: StringId = StringId::EMPTY;
19+
1520
/// Allocates a new `ProfilesDictionary` and writes a handle to it in `handle`.
1621
///
1722
/// # Safety

datadog-profiling/examples/profiles.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use datadog_profiling::profiles::datatypes::{
77
};
88
use datadog_profiling::profiles::{Compressor, FallibleStringWriter, PprofBuilder};
99
use std::io;
10+
use std::ptr::null_mut;
1011

1112
// Keep this roughly in-sync with profiles.c
1213
fn main() {
@@ -58,20 +59,20 @@ fn main() {
5859
let location_1 = locations
5960
.try_insert(Location {
6061
address: 0,
61-
mapping_id: None,
62+
mapping_id: null_mut(),
6263
line: Line {
6364
line_number: 3,
64-
function_id: Some(phpinfo_id),
65+
function_id: phpinfo_id.as_ptr(),
6566
},
6667
})
6768
.unwrap();
6869
let location_2 = locations
6970
.try_insert(Location {
7071
address: 0,
71-
mapping_id: None,
72+
mapping_id: null_mut(),
7273
line: Line {
7374
line_number: 10,
74-
function_id: Some(main_id),
75+
function_id: main_id.as_ptr(),
7576
},
7677
})
7778
.unwrap();

datadog-profiling/src/profiles/datatypes/function.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ pub struct Function {
1818
// Avoid NonNull<()> in FFI; see PR:
1919
// https://github.com/mozilla/cbindgen/pull/1098
2020
pub type FunctionId = std::ptr::NonNull<c_void>;
21+
pub type OptionalFunctionId = *mut c_void;
2122

2223
pub type FunctionSet = ParallelSet<Function, 4>;

datadog-profiling/src/profiles/datatypes/location.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use crate::profiles::collections::ParallelSet;
5-
use crate::profiles::datatypes::{FunctionId, MappingId};
5+
use crate::profiles::datatypes::{OptionalFunctionId, OptionalMappingId};
66
use std::ffi::c_void;
77

88
/// A representation of a location that is an intersection of the Otel and
@@ -13,7 +13,7 @@ use std::ffi::c_void;
1313
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash)]
1414
pub struct Location {
1515
pub address: u64,
16-
pub mapping_id: Option<MappingId>,
16+
pub mapping_id: OptionalMappingId,
1717
pub line: Line,
1818
}
1919

@@ -27,7 +27,7 @@ pub type LocationId = std::ptr::NonNull<c_void>;
2727
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash)]
2828
pub struct Line {
2929
pub line_number: i64,
30-
pub function_id: Option<FunctionId>,
30+
pub function_id: OptionalFunctionId,
3131
}
3232

3333
pub type LocationSet = ParallelSet<Location, 4>;

datadog-profiling/src/profiles/datatypes/mapping.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ pub struct Mapping {
2121
// https://github.com/mozilla/cbindgen/pull/1098
2222
pub type MappingId = std::ptr::NonNull<c_void>;
2323

24+
// cbindgen didn't understand Option<MappingId> :'(
25+
pub type OptionalMappingId = *mut c_void;
26+
2427
pub type MappingSet = ParallelSet<Mapping, 2>;

datadog-profiling/src/profiles/pprof_builder/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use ddcommon::vec::VecExt;
2929
use hashbrown::HashSet;
3030
use std::collections::{hash_map, HashMap};
3131
use std::io::Write;
32+
use std::ptr::NonNull;
3233
use std::time::{SystemTime, UNIX_EPOCH};
3334

3435
/// Compacts ids into "offsets" that begin at 1 since id=0 is reserved in
@@ -714,14 +715,14 @@ impl<'a> PprofBuilder<'a> {
714715
) -> Result<u64, ProfileError> {
715716
loc_ids.ensure_with(sid, |id| {
716717
let loc = unsafe { scratch.locations().get(sid) };
717-
let mapping_id = match loc.mapping_id {
718+
let mapping_id = match NonNull::new(loc.mapping_id) {
718719
Some(mid) => {
719720
let set_id = unsafe { SetId::from_raw(mid.cast()) };
720721
Self::ensure_mapping(w, set_id, dict, strings, map_ids)?
721722
}
722723
None => 0,
723724
};
724-
let line = if let Some(fid) = loc.line.function_id {
725+
let line = if let Some(fid) = NonNull::new(loc.line.function_id) {
725726
let function_id = unsafe { SetId::from_raw(fid) };
726727
let fid64 = Self::ensure_function(w, function_id, dict, strings, func_ids)?;
727728
pprof::Line {

examples/ffi/exporter.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,8 @@ int main(int argc, char **argv) {
7979
check_ok(ddog_prof_ProfilesDictionary_insert_mapping(&map_id, dict, &mapping), "insert_mapping");
8080

8181
// Insert a location and stack in the scratchpad
82-
ddog_prof_Option_FunctionId opt_fn = {.tag = DDOG_PROF_OPTION_FUNCTION_ID_SOME_FUNCTION_ID,
83-
.some = func_id};
84-
ddog_prof_Option_MappingId opt_map = {.tag = DDOG_PROF_OPTION_MAPPING_ID_SOME_MAPPING_ID,
85-
.some = map_id};
86-
ddog_prof_Line line = {.line_number = 42, .function_id = opt_fn};
87-
ddog_prof_Location loc = {.address = 0, .mapping_id = opt_map, .line = line};
82+
ddog_prof_Line line = {.line_number = 42, .function_id = func_id};
83+
ddog_prof_Location loc = {.address = 0, .mapping_id = map_id, .line = line};
8884
ddog_prof_LocationId loc_id = NULL;
8985
check_ok(ddog_prof_ScratchPad_insert_location(&loc_id, scratch, &loc),
9086
"ScratchPad_insert_location");

examples/ffi/profiles.c

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,46 +37,29 @@ int main(void) {
3737
ddog_prof_ValueType vt = {.type_id = vt_type, .unit_id = vt_unit};
3838

3939
// Insert function/mapping strings and create ids
40-
ddog_prof_StringId fn_name = {0}, fn_sys = {0}, fn_file = {0};
41-
check_ok(ddog_prof_ProfilesDictionary_insert_str(&fn_name, dict, DDOG_CHARSLICE_C("{main}"),
40+
ddog_prof_Function func = {.system_name = DDOG_PROF_STRINGID_EMPTY};
41+
check_ok(ddog_prof_ProfilesDictionary_insert_str(&func.name, dict, DDOG_CHARSLICE_C("{main}"),
4242
DDOG_PROF_UTF8_OPTION_VALIDATE),
4343
"insert_str(fn name)");
44-
check_ok(ddog_prof_ProfilesDictionary_insert_str(&fn_sys, dict, DDOG_CHARSLICE_C("{main}"),
45-
DDOG_PROF_UTF8_OPTION_VALIDATE),
46-
"insert_str(fn system)");
47-
check_ok(ddog_prof_ProfilesDictionary_insert_str(&fn_file, dict,
44+
check_ok(ddog_prof_ProfilesDictionary_insert_str(&func.file_name, dict,
4845
DDOG_CHARSLICE_C("/srv/example/index.php"),
4946
DDOG_PROF_UTF8_OPTION_VALIDATE),
5047
"insert_str(fn file)");
5148

52-
ddog_prof_Function func = {.name = fn_name, .system_name = fn_sys, .file_name = fn_file};
5349
ddog_prof_FunctionId func_id = NULL;
5450
check_ok(ddog_prof_ProfilesDictionary_insert_function(&func_id, dict, &func), "insert_function");
5551

56-
ddog_prof_StringId map_file = {0}, map_build = {0};
57-
check_ok(ddog_prof_ProfilesDictionary_insert_str(
58-
&map_file, dict, DDOG_CHARSLICE_C("/bin/example"), DDOG_PROF_UTF8_OPTION_VALIDATE),
59-
"insert_str(map filename)");
60-
check_ok(ddog_prof_ProfilesDictionary_insert_str(&map_build, dict, DDOG_CHARSLICE_C("deadbeef"),
52+
ddog_prof_Mapping mapping = {.build_id = DDOG_PROF_STRINGID_EMPTY};
53+
check_ok(ddog_prof_ProfilesDictionary_insert_str(&mapping.filename, dict,
54+
DDOG_CHARSLICE_C("/bin/example"),
6155
DDOG_PROF_UTF8_OPTION_VALIDATE),
62-
"insert_str(map build)");
63-
ddog_prof_Mapping mapping = {
64-
.memory_start = 0,
65-
.memory_limit = 0,
66-
.file_offset = 0,
67-
.filename = map_file,
68-
.build_id = map_build,
69-
};
56+
"insert_str(map filename)");
7057
ddog_prof_MappingId map_id = NULL;
7158
check_ok(ddog_prof_ProfilesDictionary_insert_mapping(&map_id, dict, &mapping), "insert_mapping");
7259

7360
// Create a location in the scratchpad
74-
ddog_prof_Option_FunctionId opt_fn = {.tag = DDOG_PROF_OPTION_FUNCTION_ID_SOME_FUNCTION_ID,
75-
.some = func_id};
76-
ddog_prof_Option_MappingId opt_map = {.tag = DDOG_PROF_OPTION_MAPPING_ID_SOME_MAPPING_ID,
77-
.some = map_id};
78-
ddog_prof_Line line = {.line_number = 0, .function_id = opt_fn};
79-
ddog_prof_Location loc = {.address = 0, .mapping_id = opt_map, .line = line};
61+
ddog_prof_Line line = {.line_number = 0, .function_id = func_id};
62+
ddog_prof_Location loc = {.address = 0, .mapping_id = map_id, .line = line};
8063
ddog_prof_LocationId loc_id = NULL;
8164
check_ok(ddog_prof_ScratchPad_insert_location(&loc_id, scratch, &loc),
8265
"ScratchPad_insert_location");

0 commit comments

Comments
 (0)