Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Profiler] Implement interning API #917

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions examples/ffi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ add_executable(crashinfo crashinfo.cpp)
target_compile_features(crashinfo PRIVATE cxx_std_20)
target_link_libraries(crashinfo PRIVATE Datadog::Profiling)

add_executable(profile_intern profile_intern.cpp)
# needed for designated initializers
target_compile_features(profile_intern PRIVATE cxx_std_20)
target_link_libraries(profile_intern PRIVATE Datadog::Profiling)

if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
target_compile_definitions(exporter PUBLIC _CRT_SECURE_NO_WARNINGS)
Expand Down
146 changes: 146 additions & 0 deletions examples/ffi/profile_intern.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0

extern "C" {
#include <datadog/common.h>
#include <datadog/crashtracker.h>
#include <datadog/profiling.h>
}
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <memory>
#include <optional>
#include <string>
#include <thread>
#include <vector>

static ddog_CharSlice to_slice_c_char(const char *s) { return {.ptr = s, .len = strlen(s)}; }
static ddog_CharSlice to_slice_c_char(const char *s, std::size_t size) {
return {.ptr = s, .len = size};
}
static ddog_CharSlice to_slice_string(std::string const &s) {
return {.ptr = s.data(), .len = s.length()};
}

static std::string to_string(ddog_CharSlice s) { return std::string(s.ptr, s.len); }

void print_error(const ddog_Error &err) {
auto charslice = ddog_Error_message(&err);
printf("%.*s\n", static_cast<int>(charslice.len), charslice.ptr);
}

#define CHECK_RESULT(typ, ok_tag) \
void check_result(typ result) { \
if (result.tag != ok_tag) { \
print_error(result.err); \
ddog_Error_drop(&result.err); \
exit(EXIT_FAILURE); \
} \
}

CHECK_RESULT(ddog_VoidResult, DDOG_VOID_RESULT_OK)

#define EXTRACT_RESULT(typ, uppercase) \
ddog_prof_##typ##Id extract_result(ddog_prof_##typ##Id_Result result) { \
if (result.tag != DDOG_PROF_##uppercase##_ID_RESULT_OK_GENERATIONAL_ID_##uppercase##_ID) { \
print_error(result.err); \
ddog_Error_drop(&result.err); \
exit(EXIT_FAILURE); \
} else { \
return result.ok; \
} \
}

EXTRACT_RESULT(Function, FUNCTION)
EXTRACT_RESULT(Label, LABEL)
EXTRACT_RESULT(LabelSet, LABEL_SET)
EXTRACT_RESULT(Location, LOCATION)
EXTRACT_RESULT(Mapping, MAPPING)
EXTRACT_RESULT(StackTrace, STACK_TRACE)
EXTRACT_RESULT(String, STRING)

void wait_for_user(std::string s) {
std::cout << s << std::endl;
getchar();
}

int main(void) {
const ddog_prof_ValueType wall_time = {
.type_ = DDOG_CHARSLICE_C("wall-time"),
.unit = DDOG_CHARSLICE_C("nanoseconds"),
};
const ddog_prof_Slice_ValueType sample_types = {&wall_time, 1};
const ddog_prof_Period period = {wall_time, 60};

ddog_prof_Profile_NewResult new_result = ddog_prof_Profile_new(sample_types, &period, NULL);
if (new_result.tag != DDOG_PROF_PROFILE_NEW_RESULT_OK) {
ddog_CharSlice message = ddog_Error_message(&new_result.err);
fprintf(stderr, "%.*s", (int)message.len, message.ptr);
ddog_Error_drop(&new_result.err);
exit(EXIT_FAILURE);
}

ddog_prof_Profile *profile = &new_result.ok;
auto empty_string_id =
extract_result(ddog_prof_Profile_intern_string(profile, DDOG_CHARSLICE_C("")));

auto root_function_name =
extract_result(ddog_prof_Profile_intern_string(profile, DDOG_CHARSLICE_C("{main}")));
auto root_file_name = extract_result(
ddog_prof_Profile_intern_string(profile, DDOG_CHARSLICE_C("/srv/example/index.php")));
auto root_mapping = extract_result(
ddog_prof_Profile_intern_mapping(profile, 0, 0, 0, root_file_name, empty_string_id));
auto root_function = extract_result(ddog_prof_Profile_intern_function(
profile, root_function_name, empty_string_id, root_file_name, 0));
auto root_location =
extract_result(ddog_prof_Profile_intern_location(profile, root_mapping, root_function, 0, 0));
ddog_prof_Slice_GenerationalIdLocationId locations = {.ptr = &root_location, .len = 1};
auto stacktrace = extract_result(ddog_prof_Profile_intern_stacktrace(profile, locations));

auto magic_label_key =
extract_result(ddog_prof_Profile_intern_string(profile, DDOG_CHARSLICE_C("magic_word")));
auto magic_label_val =
extract_result(ddog_prof_Profile_intern_string(profile, DDOG_CHARSLICE_C("abracadabra")));
auto magic_label =
extract_result(ddog_prof_Profile_intern_label_str(profile, magic_label_key, magic_label_val));

// Keep this id around, no need to reintern the same string over and over again.
auto counter_id =
extract_result(ddog_prof_Profile_intern_string(profile, DDOG_CHARSLICE_C("unique_counter")));

// wait_for_user("Press any key to start adding values ...");

std::chrono::time_point<std::chrono::system_clock> start = std::chrono::system_clock::now();
for (auto i = 0; i < 10000000; i++) {
auto counter_label = extract_result(ddog_prof_Profile_intern_label_num(profile, counter_id, i));
ddog_prof_LabelId label_array[2] = {magic_label, counter_label};
ddog_prof_Slice_LabelId label_slice = {.ptr = label_array, .len = 2};
auto labels = extract_result(ddog_prof_Profile_intern_labelset(profile, label_slice));

int64_t value = i * 10;
ddog_Slice_I64 values = {.ptr = &value, .len = 1};
int64_t timestamp = 3 + 800 * i;
check_result(ddog_prof_Profile_intern_sample(profile, stacktrace, values, labels, timestamp));
}
std::chrono::time_point<std::chrono::system_clock> end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::cout << "elapsed time: " << elapsed_seconds.count() << "s" << std::endl;

// wait_for_user("Press any key to reset and drop...");

ddog_prof_Profile_Result reset_result = ddog_prof_Profile_reset(profile, NULL);
if (reset_result.tag != DDOG_PROF_PROFILE_RESULT_OK) {
ddog_CharSlice message = ddog_Error_message(&reset_result.err);
fprintf(stderr, "%.*s", (int)message.len, message.ptr);
ddog_Error_drop(&reset_result.err);
}
ddog_prof_Profile_drop(profile);

// wait_for_user("Press any key to exit...");

return EXIT_SUCCESS;
}
4 changes: 2 additions & 2 deletions examples/ffi/profiles.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ int main(void) {
}
ddog_prof_Profile_drop(profile);

printf("Press any key to exit...");
getchar();
// printf("Press any key to exit...");
// getchar();

return 0;
}
15 changes: 8 additions & 7 deletions profiling-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,19 @@ build_common = { path = "../build-common" }

[dependencies]
anyhow = "1.0"
data-pipeline-ffi = { path = "../data-pipeline-ffi", default-features = false, optional = true }
datadog-crashtracker-ffi = { path = "../crashtracker-ffi", default-features = false, optional = true}
datadog-library-config-ffi = { path = "../library-config-ffi", default-features = false, optional = true }
datadog-profiling = { path = "../profiling" }
hyper = { version = "0.14", features = ["backports", "deprecated"], default-features = false }
ddcommon = { path = "../ddcommon"}
ddcommon-ffi = { path = "../ddcommon-ffi", default-features = false }
ddtelemetry-ffi = { path = "../ddtelemetry-ffi", default-features = false, optional = true, features = ["expanded_builder_macros"] }
function_name = "0.3.0"
futures = { version = "0.3", default-features = false }
hyper = { version = "0.14", features = ["backports", "deprecated"], default-features = false }
libc = "0.2"
tokio-util = "0.7.1"
serde_json = { version = "1.0" }
futures = { version = "0.3", default-features = false }
symbolizer-ffi = { path = "../symbolizer-ffi", optional = true, default-features = false }
symbolic-demangle = { version = "12.8.0", default-features = false, features = ["rust", "cpp", "msvc"] }
symbolic-common = "12.8.0"
data-pipeline-ffi = { path = "../data-pipeline-ffi", default-features = false, optional = true }
datadog-library-config-ffi = { path = "../library-config-ffi", default-features = false, optional = true }
symbolic-demangle = { version = "12.8.0", default-features = false, features = ["rust", "cpp", "msvc"] }
symbolizer-ffi = { path = "../symbolizer-ffi", optional = true, default-features = false }
tokio-util = "0.7.1"
31 changes: 31 additions & 0 deletions profiling-ffi/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,37 @@ renaming_overrides_prefixing = true
"ManagedStringId" = "ddog_prof_ManagedStringId"
"StringWrapper" = "ddog_StringWrapper"
"StringWrapperResult" = "ddog_StringWrapperResult"
"VoidResult" = "ddog_VoidResult"

"Slice_GenerationalIdLabelId" = "ddog_prof_Slice_LabelId"

"GenerationalId_FunctionId" = "ddog_prof_FunctionId"
"Result_GenerationalIdFunctionId" = "ddog_prof_FunctionId_Result"
"FunctionId" = "OpaqueFunctionId"

"GenerationalId_LabelId" = "ddog_prof_LabelId"
"Result_GenerationalIdLabelId" = "ddog_prof_LabelId_Result"
"LabelId" = "OpaqueLabelId"

"GenerationalId_LabelSetId" = "ddog_prof_LabelSetId"
"Result_GenerationalIdLabelSetId" = "ddog_prof_LabelSetId_Result"
"LabelSetId" = "OpaqueLabelSetId"

"GenerationalId_LocationId" = "ddog_prof_LocationId"
"Result_GenerationalIdLocationId" = "ddog_prof_LocationId_Result"
"LocationId" = "OpaqueLocationId"

"GenerationalId_MappingId" = "ddog_prof_MappingId"
"Result_GenerationalIdMappingId" = "ddog_prof_MappingId_Result"
"MappingId" = "OpaqueMappingId"

"GenerationalId_StackTraceId" = "ddog_prof_StackTraceId"
"Result_GenerationalIdStackTraceId" = "ddog_prof_StackTraceId_Result"
"StackTraceId" = "OpaqueStackTraceId"

"GenerationalId_StringId" = "ddog_prof_StringId"
"Result_GenerationalIdStringId" = "ddog_prof_StringId_Result"
"StringId" = "OpaqueStringId"

[export.mangle]
rename_types = "PascalCase"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ pub unsafe extern "C" fn ddog_prof_Profile_add(
.into()
}

unsafe fn profile_ptr_to_inner<'a>(
pub(crate) unsafe fn profile_ptr_to_inner<'a>(
profile_ptr: *mut Profile,
) -> anyhow::Result<&'a mut internal::Profile> {
match profile_ptr.as_mut() {
Expand Down
Loading
Loading