Skip to content

Commit 6c51598

Browse files
committed
Move profiling ffi crate back out of profiling
Due to a [rust bug][1], LTO was not being applied, and we need the lib crate type in order to run tests and clippy lints. For now, keep these as separate crates. [1]: rust-lang/rust#51009
1 parent d61002f commit 6c51598

16 files changed

+94
-50
lines changed

Cargo.lock

+14-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
[workspace]
55
members = [
66
"profiling",
7+
"profiling-ffi",
78
"ddcommon",
89
"ddcommon-ffi",
910
"ddtelemetry",

LICENSE-3rdparty.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
root_name: "datadog-profiling, ddcommon, ddcommon-ffi, ddtelemetry"
2+
root_name: "datadog-profiling, ddcommon, datadog-profiling-ffi, ddcommon-ffi, ddtelemetry"
33
third_party_libraries:
44
- package_name: aho-corasick
55
package_version: 0.7.18

build-profiling-ffi.sh

+23-10
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ case "$target" in
6363
esac
6464

6565
echo "Recognized platform '${target}'. Adding libs: ${native_static_libs}"
66-
cd profiling
66+
cd profiling-ffi
6767
sed < datadog_profiling.pc.in "s/@Datadog_VERSION@/${version}/g" \
6868
> "$destdir/lib/pkgconfig/datadog_profiling.pc"
6969

@@ -86,12 +86,21 @@ cp -v LICENSE LICENSE-3rdparty.yml NOTICE "$destdir/"
8686

8787
export RUSTFLAGS="${RUSTFLAGS:- -C relocation-model=pic}"
8888

89-
echo "Building the datadog profiling library (may take some time)..."
90-
cargo build --package=datadog-profiling --features=datadog_profiling_ffi --release --target "${target}"
89+
datadog_profiling_ffi="datadog-profiling-ffi"
90+
echo "Building the ${datadog_profiling_ffi} crate (may take some time)..."
91+
cargo build --package="${datadog_profiling_ffi}" --release --target "${target}"
9192

92-
shared_library_name="${library_prefix}datadog_profiling${shared_library_suffix}"
93-
static_library_name="${library_prefix}datadog_profiling${static_library_suffix}"
94-
cp -v "target/${target}/release/$static_library_name" "target/${target}/release/$shared_library_name" "$destdir/lib/"
93+
# Remove _ffi suffix when copying
94+
shared_library_name="${library_prefix}datadog_profiling_ffi${shared_library_suffix}"
95+
static_library_name="${library_prefix}datadog_profiling_ffi${static_library_suffix}"
96+
shared_library_rename="${library_prefix}datadog_profiling${shared_library_suffix}"
97+
static_library_rename="${library_prefix}datadog_profiling${static_library_suffix}"
98+
99+
cp -v "target/${target}/release/${shared_library_name}" "$destdir/lib/${shared_library_rename}"
100+
cp -v "target/${target}/release/${static_library_rename}" "$destdir/lib/${static_library_rename}"
101+
102+
shared_library_name="${shared_library_rename}"
103+
static_library_name="${static_library_rename}"
95104

96105
if [[ "$remove_rpath" -eq 1 ]]; then
97106
patchelf --remove-rpath "$destdir/lib/${shared_library_name}"
@@ -114,8 +123,8 @@ if command -v objcopy > /dev/null && [[ "$target" != "x86_64-pc-windows-msvc" ]]
114123
fi
115124

116125
echo "Checking that native-static-libs are as expected for this platform..."
117-
cd profiling
118-
actual_native_static_libs="$(cargo rustc --features=datadog_profiling_ffi --release --target "${target}" -- --print=native-static-libs 2>&1 | awk -F ':' '/note: native-static-libs:/ { print $3 }')"
126+
cd profiling-ffi
127+
actual_native_static_libs="$(cargo rustc --release --target "${target}" -- --print=native-static-libs 2>&1 | awk -F ':' '/note: native-static-libs:/ { print $3 }')"
119128
echo "Actual native-static-libs:${actual_native_static_libs}"
120129
echo "Expected native-static-libs:${expected_native_static_libs}"
121130

@@ -140,8 +149,12 @@ echo "Building tools"
140149
cargo build --package tools --bins
141150

142151
echo "Generating $destdir/include/libdatadog headers..."
143-
cbindgen --crate ddcommon-ffi --config ddcommon-ffi/cbindgen.toml --output "$destdir/include/datadog/common.h"
144-
cbindgen --crate datadog-profiling --config profiling/cbindgen.toml --output "$destdir/include/datadog/profiling.h"
152+
cbindgen --crate ddcommon-ffi \
153+
--config ddcommon-ffi/cbindgen.toml \
154+
--output "$destdir/include/datadog/common.h"
155+
cbindgen --crate "${datadog_profiling_ffi}" \
156+
--config profiling-ffi/cbindgen.toml \
157+
--output "$destdir/include/datadog/profiling.h"
145158
./target/debug/dedup_headers "$destdir/include/datadog/common.h" "$destdir/include/datadog/profiling.h"
146159

147160
echo "Done."

cmake/DatadogConfig.cmake.in

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ if(Datadog_FOUND)
4242
wsock32
4343
ws2_32
4444
shlwapi
45-
Secur32)
45+
Secur32
46+
Ncrypt)
4647
endif()
4748

4849
add_library(Datadog::Profiling ALIAS datadog_profiling)

profiling-ffi/Cargo.toml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
2+
# This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021-Present Datadog, Inc.
3+
4+
[package]
5+
name = "datadog-profiling-ffi"
6+
version = "0.8.0-alpha.1"
7+
edition = "2021"
8+
license = "Apache-2.0"
9+
10+
[lib]
11+
# LTO is ignored if "lib" is added as crate type
12+
# cf. https://github.com/rust-lang/rust/issues/51009
13+
crate-type = ["staticlib", "cdylib"]
14+
15+
[dependencies]
16+
anyhow = "1.0"
17+
chrono = "0.4"
18+
datadog-profiling = { path = "../profiling"}
19+
hyper = {version = "0.14", default-features = false}
20+
ddcommon = { path = "../ddcommon"}
21+
ddcommon-ffi = { path = "../ddcommon-ffi"}
22+
libc = "0.2"
23+
tokio-util = "0.7.1"

profiling-ffi/NOTICE

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../NOTICE

profiling/cbindgen.toml profiling-ffi/cbindgen.toml

+1-4
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ no_includes = true
1313
sys_includes = ["stdbool.h", "stddef.h", "stdint.h"]
1414
includes = ["datadog/common.h"]
1515

16-
[defines]
17-
"feature = datadog_profiling_ffi" = "DATADOG_PROFILING_FFI"
18-
1916
[export]
2017
prefix = "ddog_"
2118

@@ -31,4 +28,4 @@ must_use = "DDOG_CHECK_RETURN"
3128

3229
[parse]
3330
parse_deps = true
34-
include = ["ddcommon", "ddcommon-ffi", "ux"]
31+
include = ["ddcommon", "ddcommon-ffi", "datadog-profiling", "ux"]
File renamed without changes.

profiling/src/ffi/exporter.rs profiling-ffi/src/exporter.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
#![allow(renamed_and_removed_lints)]
55
#![allow(clippy::box_vec)]
66

7-
use super::Timespec;
8-
use crate::exporter;
9-
use crate::exporter::ProfileExporter;
7+
use crate::Timespec;
8+
use datadog_profiling::exporter;
109
use ddcommon::tag::Tag;
1110
use ddcommon_ffi::slice::{AsBytes, ByteSlice, CharSlice, Slice};
11+
use exporter::ProfileExporter;
1212
use std::borrow::Cow;
1313
use std::ptr::NonNull;
1414
use std::str::FromStr;
@@ -304,12 +304,12 @@ pub extern "C" fn ddog_CancellationToken_drop(_cancel: Option<Box<CancellationTo
304304

305305
#[export_name = "ddog_SendResult_drop"]
306306
pub unsafe extern "C" fn send_result_drop(result: SendResult) {
307-
drop(result)
307+
std::mem::drop(result)
308308
}
309309

310310
#[cfg(test)]
311311
mod test {
312-
use super::*;
312+
use crate::exporter::*;
313313
use ddcommon_ffi::Slice;
314314

315315
fn family() -> CharSlice<'static> {
@@ -336,7 +336,8 @@ mod test {
336336
NewProfileExporterResult::Ok(exporter) => unsafe {
337337
profile_exporter_delete(Some(Box::from_raw(exporter)))
338338
},
339-
NewProfileExporterResult::Err(_) => {
339+
NewProfileExporterResult::Err(message) => {
340+
std::mem::drop(message);
340341
panic!("Should not occur!")
341342
}
342343
}
@@ -350,7 +351,8 @@ mod test {
350351
NewProfileExporterResult::Ok(exporter) => unsafe {
351352
Some(NonNull::new_unchecked(exporter))
352353
},
353-
NewProfileExporterResult::Err(_) => {
354+
NewProfileExporterResult::Err(message) => {
355+
std::mem::drop(message);
354356
panic!("Should not occur!")
355357
}
356358
};
File renamed without changes.

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

+18-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
22
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021-Present Datadog, Inc.
33

4-
use super::Timespec;
5-
use crate::profile as profiles;
4+
use crate::Timespec;
5+
use datadog_profiling::profile as profiles;
66
use ddcommon_ffi::slice::{AsBytes, CharSlice, Slice};
77
use std::convert::{TryFrom, TryInto};
88
use std::str::Utf8Error;
@@ -303,11 +303,11 @@ pub unsafe extern "C" fn ddog_Profile_new(
303303
sample_types: Slice<ValueType>,
304304
period: Option<&Period>,
305305
start_time: Option<&Timespec>,
306-
) -> Box<crate::profile::Profile> {
307-
let types: Vec<crate::profile::api::ValueType> =
306+
) -> Box<datadog_profiling::profile::Profile> {
307+
let types: Vec<datadog_profiling::profile::api::ValueType> =
308308
sample_types.into_slice().iter().map(Into::into).collect();
309309

310-
let builder = crate::profile::Profile::builder()
310+
let builder = datadog_profiling::profile::Profile::builder()
311311
.period(period.map(Into::into))
312312
.sample_types(types)
313313
.start_time(start_time.map(SystemTime::from));
@@ -319,15 +319,18 @@ pub unsafe extern "C" fn ddog_Profile_new(
319319
/// # Safety
320320
/// The `profile` must point to an object created by another FFI routine in this
321321
/// module, such as `ddog_Profile_with_sample_types`.
322-
pub unsafe extern "C" fn ddog_Profile_free(_profile: Box<crate::profile::Profile>) {}
322+
pub unsafe extern "C" fn ddog_Profile_free(_profile: Box<datadog_profiling::profile::Profile>) {}
323323

324324
#[no_mangle]
325325
/// # Safety
326326
/// The `profile` ptr must point to a valid Profile object created by this
327327
/// module. All pointers inside the `sample` need to be valid for the duration
328328
/// of this call.
329329
/// This call is _NOT_ thread-safe.
330-
pub extern "C" fn ddog_Profile_add(profile: &mut crate::profile::Profile, sample: Sample) -> u64 {
330+
pub extern "C" fn ddog_Profile_add(
331+
profile: &mut datadog_profiling::profile::Profile,
332+
sample: Sample,
333+
) -> u64 {
331334
match sample.try_into().map(|s| profile.add(s)) {
332335
Ok(r) => match r {
333336
Ok(id) => id.into(),
@@ -355,7 +358,7 @@ pub extern "C" fn ddog_Profile_add(profile: &mut crate::profile::Profile, sample
355358
/// This call is _NOT_ thread-safe.
356359
#[no_mangle]
357360
pub unsafe extern "C" fn ddog_Profile_set_endpoint<'a>(
358-
profile: &mut crate::profile::Profile,
361+
profile: &mut datadog_profiling::profile::Profile,
359362
local_root_span_id: CharSlice<'a>,
360363
endpoint: CharSlice<'a>,
361364
) {
@@ -372,8 +375,8 @@ pub struct EncodedProfile {
372375
buffer: ddcommon_ffi::Vec<u8>,
373376
}
374377

375-
impl From<crate::profile::EncodedProfile> for EncodedProfile {
376-
fn from(value: crate::profile::EncodedProfile) -> Self {
378+
impl From<datadog_profiling::profile::EncodedProfile> for EncodedProfile {
379+
fn from(value: datadog_profiling::profile::EncodedProfile) -> Self {
377380
let start = value.start.into();
378381
let end = value.end.into();
379382
let buffer = value.buffer.into();
@@ -406,7 +409,7 @@ pub enum SerializeResult {
406409
/// The `duration_nanos` must be null or otherwise point to a valid i64.
407410
#[no_mangle]
408411
pub unsafe extern "C" fn ddog_Profile_serialize(
409-
profile: &crate::profile::Profile,
412+
profile: &datadog_profiling::profile::Profile,
410413
end_time: Option<&Timespec>,
411414
duration_nanos: Option<&i64>,
412415
) -> SerializeResult {
@@ -416,7 +419,7 @@ pub unsafe extern "C" fn ddog_Profile_serialize(
416419
Some(x) if *x < 0 => None,
417420
Some(x) => Some(Duration::from_nanos((*x) as u64)),
418421
};
419-
match || -> anyhow::Result<crate::profile::EncodedProfile> {
422+
match || -> anyhow::Result<datadog_profiling::profile::EncodedProfile> {
420423
Ok(profile.serialize(end_time, duration)?)
421424
}() {
422425
Ok(ok) => SerializeResult::Ok(ok.into()),
@@ -447,15 +450,15 @@ pub unsafe extern "C" fn ddog_Vec_u8_as_slice(vec: &ddcommon_ffi::Vec<u8>) -> Sl
447450
/// If `time` is not null, it must point to a valid Timespec object.
448451
#[no_mangle]
449452
pub unsafe extern "C" fn ddog_Profile_reset(
450-
profile: &mut crate::profile::Profile,
453+
profile: &mut datadog_profiling::profile::Profile,
451454
start_time: Option<&Timespec>,
452455
) -> bool {
453456
profile.reset(start_time.map(SystemTime::from)).is_some()
454457
}
455458

456459
#[cfg(test)]
457460
mod test {
458-
use super::*;
461+
use crate::profiles::*;
459462
use ddcommon_ffi::Slice;
460463

461464
#[test]
@@ -518,7 +521,7 @@ mod test {
518521
}
519522
}
520523

521-
unsafe fn provide_distinct_locations_ffi() -> crate::profile::Profile {
524+
unsafe fn provide_distinct_locations_ffi() -> datadog_profiling::profile::Profile {
522525
let sample_type: *const ValueType = &ValueType::new("samples", "count");
523526
let mut profile = ddog_Profile_new(Slice::new(sample_type, 1), None, None);
524527

profiling/Cargo.toml

+1-8
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ build = "build.rs"
99
license = "Apache-2.0"
1010

1111
[lib]
12-
# LTO is ignored if "lib" is added as crate type
13-
# cf. https://github.com/rust-lang/rust/issues/51009
14-
crate-type = ["cdylib", "staticlib", "lib"]
15-
16-
[features]
17-
default = []
18-
datadog_profiling_ffi = ["dep:ddcommon-ffi"]
12+
crate-type = ["lib"]
1913

2014
[build-dependencies]
2115
prost-build = "0.10"
@@ -25,7 +19,6 @@ anyhow = "1.0"
2519
bytes = "1.1"
2620
chrono = "0.4"
2721
ddcommon = {path = "../ddcommon"}
28-
ddcommon-ffi = { path = "../ddcommon-ffi", optional = true }
2922
futures = "0.3"
3023
futures-core = {version = "0.3.0", default-features = false}
3124
futures-util = {version = "0.3.0", default-features = false}

profiling/src/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,3 @@
33

44
pub mod exporter;
55
pub mod profile;
6-
7-
#[cfg(feature = "datadog_profiling_ffi")]
8-
pub mod ffi;

0 commit comments

Comments
 (0)