Skip to content

Commit 7922f33

Browse files
committed
better api
1 parent a6daf66 commit 7922f33

File tree

3 files changed

+166
-141
lines changed

3 files changed

+166
-141
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ docker-sync.yml
2525
libtest.so
2626
libtest_cpp.so
2727
examples/cxx/profiling
28+
profile.pprof

examples/cxx/profiling.cpp

Lines changed: 62 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <iostream>
55
#include <memory>
6+
#include <fstream>
67
#include "libdd-profiling/src/cxx.rs.h"
78

89
using namespace datadog::profiling;
@@ -11,62 +12,79 @@ int main() {
1112
try {
1213
std::cout << "Creating Profile using CXX bindings..." << std::endl;
1314

14-
auto builder = ProfileBuilder::create();
15+
// Define sample types using initializer list (converts to rust::Vec)
16+
CxxValueType wall_time{"wall-time", "nanoseconds"};
1517

16-
// Add sample types
17-
CxxValueType cpu_time;
18-
cpu_time.type_ = "cpu";
19-
cpu_time.unit = "nanoseconds";
20-
builder->add_sample_type(cpu_time);
18+
// Define period
19+
CxxPeriod period{wall_time, 60}; // 60ns sample period
2120

22-
CxxValueType sample_count;
23-
sample_count.type_ = "samples";
24-
sample_count.unit = "count";
25-
builder->add_sample_type(sample_count);
21+
// Create the profile with initializer list
22+
auto profile = Profile::create({wall_time}, period);
23+
std::cout << "✅ Profile created" << std::endl;
2624

27-
// Set duration
28-
builder->set_duration_nanos(1000000000); // 1 second
25+
// Add samples
26+
std::cout << "Adding samples..." << std::endl;
27+
for (int i = 0; i < 100; i++) {
28+
// Create mapping
29+
CxxMapping mapping{
30+
0x10000000, // memory_start
31+
0x20000000, // memory_limit
32+
0, // file_offset
33+
"/usr/lib/libexample.so", // filename
34+
"abc123def456" // build_id
35+
};
36+
37+
// Create function
38+
auto func_name = "my_function_" + std::to_string(i % 10);
39+
auto sys_name = "_Z11my_function" + std::to_string(i % 10) + "v";
40+
CxxFunction function{func_name.c_str(), sys_name.c_str(), "/path/to/source.cpp"};
41+
42+
// Create location
43+
CxxLocation location{
44+
mapping,
45+
function,
46+
static_cast<uint64_t>(0x12345678 + i * 0x100), // address
47+
42 + i // line
48+
};
49+
50+
// Create labels
51+
CxxLabel thread_label{"thread_id", "", static_cast<int64_t>(i % 4), ""};
52+
CxxLabel counter_label{"unique_counter", "", static_cast<int64_t>(i), ""};
53+
54+
// Create sample using initializer lists
55+
int64_t wall_time_value = 1000000; // 1ms
56+
CxxSample sample{
57+
{location}, // locations
58+
{wall_time_value}, // values
59+
{thread_label, counter_label} // labels
60+
};
61+
62+
profile->add_sample(sample);
63+
}
2964

30-
// Create a sample
31-
CxxSample sample;
65+
std::cout << "✅ Added 100 samples" << std::endl;
3266

33-
// Add a location
34-
CxxLocation location;
35-
location.address = 0x12345678;
36-
location.line = 42;
67+
// Serialize the profile
68+
std::cout << "Serializing profile..." << std::endl;
69+
auto serialized = profile->serialize_to_vec();
70+
std::cout << "✅ Profile serialized to " << serialized.size() << " bytes" << std::endl;
3771

38-
location.mapping.memory_start = 0x10000000;
39-
location.mapping.memory_limit = 0x20000000;
40-
location.mapping.file_offset = 0;
41-
location.mapping.filename = "/usr/lib/libexample.so";
42-
location.mapping.build_id = "abc123def456";
72+
// Optionally write to file
73+
std::ofstream out("profile.pprof", std::ios::binary);
74+
out.write(reinterpret_cast<const char*>(serialized.data()), serialized.size());
75+
out.close();
76+
std::cout << "✅ Profile written to profile.pprof" << std::endl;
4377

44-
location.function.name = "my_function";
45-
location.function.system_name = "_Z11my_functionv";
46-
location.function.filename = "/path/to/source.cpp";
78+
// Reset the profile
79+
std::cout << "Resetting profile..." << std::endl;
80+
profile->reset();
81+
std::cout << "✅ Profile reset" << std::endl;
4782

48-
sample.locations.push_back(location);
49-
50-
// Add values
51-
sample.values.push_back(5000000); // 5ms CPU time
52-
sample.values.push_back(1); // 1 sample
53-
54-
// Add labels
55-
CxxLabel thread_label;
56-
thread_label.key = "thread";
57-
thread_label.str = "main";
58-
thread_label.num = 0;
59-
thread_label.num_unit = "";
60-
sample.labels.push_back(thread_label);
61-
62-
builder->add_sample(sample);
63-
64-
std::cout << "\n✅ Profile created successfully!" << std::endl;
83+
std::cout << "\n✅ Success!" << std::endl;
6584
return 0;
6685

6786
} catch (const std::exception& e) {
6887
std::cerr << "❌ Exception: " << e.what() << std::endl;
6988
return 1;
7089
}
7190
}
72-

0 commit comments

Comments
 (0)