|
3 | 3 |
|
4 | 4 | mod upscaling;
|
5 | 5 |
|
6 |
| -use function_name::named; |
7 |
| -pub use upscaling::UpscalingRule; |
| 6 | +pub use upscaling::ProportionalUpscalingRule; |
8 | 7 |
|
9 | 8 | use crate::profile_handle::ProfileHandle;
|
10 |
| -use crate::profiles::{ensure_non_null_out_parameter, Utf8Option}; |
| 9 | +use crate::profiles::pprof_builder::upscaling::PoissonUpscalingRule; |
| 10 | +use crate::profiles::{ |
| 11 | + ensure_non_null_out_parameter, Utf8ConversionError, Utf8Option, |
| 12 | +}; |
11 | 13 | use crate::{ArcHandle, ProfileStatus};
|
12 | 14 | use datadog_profiling::exporter::EncodedProfile;
|
13 |
| -use datadog_profiling::profiles; |
14 | 15 | use datadog_profiling::profiles::datatypes::{
|
15 | 16 | Profile, ProfilesDictionary, ScratchPad,
|
16 | 17 | };
|
@@ -48,49 +49,127 @@ pub unsafe extern "C" fn ddog_prof_PprofBuilder_new(
|
48 | 49 | }())
|
49 | 50 | }
|
50 | 51 |
|
51 |
| -/// Adds a profile to the builder with the attached upscaling rules. |
| 52 | +/// Adds a profile to the builder without upscaling rules. |
| 53 | +/// |
| 54 | +/// # Safety |
| 55 | +/// |
| 56 | +/// - `handle` must refer to a live builder, and no other mutable |
| 57 | +/// references to that builder may be active for the duration of the call. |
| 58 | +/// - `profile` must be non-null and point to a valid `Profile` that |
| 59 | +/// remains alive until the pprof builder is done. |
| 60 | +/// TODO: finish safety |
| 61 | +#[must_use] |
| 62 | +#[no_mangle] |
| 63 | +pub unsafe extern "C" fn ddog_prof_PprofBuilder_add_profile( |
| 64 | + mut handle: ProfileHandle<PprofBuilder>, |
| 65 | + profile: *const Profile, |
| 66 | +) -> ProfileStatus { |
| 67 | + crate::profiles::ensure_non_null_insert!(profile); |
| 68 | + let result = || -> Result<(), ProfileStatus> { |
| 69 | + let builder = unsafe { |
| 70 | + handle |
| 71 | + .as_inner_mut() |
| 72 | + .map_err(|e| ProfileStatus::from_ffi_safe_error_message(e))? |
| 73 | + }; |
| 74 | + let prof_ref = unsafe { &*profile }; |
| 75 | + builder |
| 76 | + .try_add_profile(prof_ref) |
| 77 | + .map_err(ProfileStatus::from_ffi_safe_error_message) |
| 78 | + }(); |
| 79 | + match result { |
| 80 | + Ok(_) => ProfileStatus::OK, |
| 81 | + Err(err) => err, |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/// Adds a profile to the builder with the attached poisson upscaling rule. |
| 86 | +/// |
| 87 | +/// # Safety |
| 88 | +/// |
| 89 | +/// - `handle` must refer to a live builder, and no other mutable |
| 90 | +/// references to that builder may be active for the duration of the call. |
| 91 | +/// - `profile` must be non-null and point to a valid `Profile` that |
| 92 | +/// remains alive until the pprof builder is done. |
| 93 | +/// TODO: finish safety |
| 94 | +#[must_use] |
| 95 | +#[no_mangle] |
| 96 | +pub unsafe extern "C" fn ddog_prof_PprofBuilder_add_profile_with_poisson_upscaling( |
| 97 | + mut handle: ProfileHandle<PprofBuilder>, |
| 98 | + profile: *const Profile, |
| 99 | + upscaling_rule: PoissonUpscalingRule, |
| 100 | +) -> ProfileStatus { |
| 101 | + crate::profiles::ensure_non_null_insert!(profile); |
| 102 | + let result = || -> Result<(), ProfileStatus> { |
| 103 | + let builder = unsafe { |
| 104 | + handle |
| 105 | + .as_inner_mut() |
| 106 | + .map_err(|e| ProfileStatus::from_ffi_safe_error_message(e))? |
| 107 | + }; |
| 108 | + let prof_ref = unsafe { &*profile }; |
| 109 | + |
| 110 | + let upscaling_rule = upscaling_rule |
| 111 | + .try_into() |
| 112 | + .map_err(ProfileStatus::from_ffi_safe_error_message)?; |
| 113 | + builder |
| 114 | + .try_add_profile_with_poisson_upscaling(prof_ref, upscaling_rule) |
| 115 | + .map_err(ProfileStatus::from_ffi_safe_error_message) |
| 116 | + }(); |
| 117 | + match result { |
| 118 | + Ok(_) => ProfileStatus::OK, |
| 119 | + Err(status) => status, |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +/// Adds a profile to the builder with the attached proportional rule. |
52 | 124 | ///
|
53 | 125 | /// # Safety
|
54 | 126 | ///
|
55 | 127 | /// - `handle` must refer to a live builder, and no other mutable
|
56 | 128 | /// references to that builder may be active for the duration of the call.
|
57 | 129 | /// - `profile` must be non-null and point to a valid `Profile` that
|
58 |
| -/// remains alive for the duration of the call. |
| 130 | +/// remains alive until the pprof builder is done. |
| 131 | +/// TODO: finish safety |
59 | 132 | #[must_use]
|
60 |
| -#[named] |
61 | 133 | #[no_mangle]
|
62 |
| -pub unsafe extern "C" fn ddog_prof_PprofBuilder_add_profile<'a>( |
| 134 | +pub unsafe extern "C" fn ddog_prof_PprofBuilder_add_profile_with_proportional_upscaling< |
| 135 | + 'a, |
| 136 | +>( |
63 | 137 | mut handle: ProfileHandle<PprofBuilder<'a>>,
|
64 | 138 | profile: *const Profile,
|
65 |
| - upscaling_rules: Slice<UpscalingRule<'a>>, |
| 139 | + upscaling_rules: Slice<ProportionalUpscalingRule<'a>>, |
66 | 140 | utf8_option: Utf8Option,
|
67 | 141 | ) -> ProfileStatus {
|
68 | 142 | crate::profiles::ensure_non_null_insert!(profile);
|
69 |
| - ProfileStatus::from(|| -> Result<(), ProfileError> { |
70 |
| - let builder = unsafe { handle.as_inner_mut()? }; |
| 143 | + let result = || -> Result<(), ProfileStatus> { |
| 144 | + let builder = unsafe { handle.as_inner_mut() } |
| 145 | + .map_err(ProfileStatus::from_ffi_safe_error_message)?; |
71 | 146 | let prof_ref = unsafe { &*profile };
|
72 | 147 |
|
73 |
| - let upscaling_rules = upscaling_rules.try_as_slice().map_err(|e| { |
74 |
| - ProfileError::fmt(format_args!( |
75 |
| - "{} failed: upscaling rules couldn't be converted to a Rust slice: {e}", |
76 |
| - function_name!(), |
77 |
| - )) |
78 |
| - })?; |
| 148 | + let upscaling_rules = upscaling_rules |
| 149 | + .try_as_slice() |
| 150 | + .map_err(ProfileStatus::from_ffi_safe_error_message)?; |
79 | 151 |
|
80 |
| - builder.try_add_profile( |
81 |
| - prof_ref, |
82 |
| - upscaling_rules.iter().map(|rule| { |
83 |
| - let key = utf8_option |
84 |
| - .try_as_bytes_convert(rule.group_by_label_key)?; |
85 |
| - let value = utf8_option |
86 |
| - .try_as_bytes_convert(rule.group_by_label_value)?; |
87 |
| - let group_by_label = (key, value); |
88 |
| - let upscaling_info = |
89 |
| - profiles::UpscalingInfo::try_from(rule.upscaling_info)?; |
90 |
| - Ok(profiles::UpscalingRule { group_by_label, upscaling_info }) |
91 |
| - }), |
92 |
| - ) |
93 |
| - }()) |
| 152 | + builder |
| 153 | + .try_add_profile_with_proportional_upscaling( |
| 154 | + prof_ref, |
| 155 | + upscaling_rules.iter().map( |
| 156 | + |rule| -> Result<_, Utf8ConversionError> { |
| 157 | + let key = rule.group_by_label.key; |
| 158 | + let value = utf8_option |
| 159 | + .try_as_bytes_convert(rule.group_by_label.value)?; |
| 160 | + Ok(( |
| 161 | + (key, value), |
| 162 | + rule.sampled as f64 / rule.real as f64, |
| 163 | + )) |
| 164 | + }, |
| 165 | + ), |
| 166 | + ) |
| 167 | + .map_err(ProfileStatus::from_ffi_safe_error_message) |
| 168 | + }(); |
| 169 | + match result { |
| 170 | + Ok(_) => ProfileStatus::OK, |
| 171 | + Err(status) => status, |
| 172 | + } |
94 | 173 | }
|
95 | 174 |
|
96 | 175 | /// Builds and returns a compressed `EncodedProfile` via `out_profile`.
|
|
0 commit comments