|
| 1 | +// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use criterion::{black_box, criterion_group, BenchmarkId, Criterion}; |
| 5 | +use datadog_crashtracker::benchmark::receiver_entry_point; |
| 6 | +use datadog_crashtracker::shared::constants::*; |
| 7 | +use datadog_crashtracker::{ |
| 8 | + default_signals, get_data_folder_path, CrashtrackerConfiguration, SharedLibrary, |
| 9 | + StacktraceCollection, |
| 10 | +}; |
| 11 | +use std::fmt::Write; |
| 12 | +use std::time::Duration; |
| 13 | +use tokio::io::BufReader; |
| 14 | + |
| 15 | +macro_rules! add_frame { |
| 16 | + ($report:expr, $fn:expr, $lib:expr) => { |
| 17 | + add_frame!($report, $lib.get_symbol_address($fn).unwrap().as_str()); |
| 18 | + }; |
| 19 | + ($report:expr, $address:expr) => { |
| 20 | + writeln!( |
| 21 | + $report, |
| 22 | + "{{ \"ip\": \"{}\", \"module_address\": \"0x21\", \"sp\": \"0x11\" }}", |
| 23 | + $address |
| 24 | + ) |
| 25 | + .expect("Failed to write frame"); |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +fn add_proc_info(report: &mut String) { |
| 30 | + writeln!(report, "{DD_CRASHTRACK_BEGIN_PROCINFO}") |
| 31 | + .expect("Failed to write DD_CRASHTRACK_BEGIN_PROCINFO"); |
| 32 | + writeln!(report, "{{ \"pid\": {} }}", std::process::id()).expect("Failed to write PID"); |
| 33 | + writeln!(report, "{DD_CRASHTRACK_END_PROCINFO}") |
| 34 | + .expect("Failed to write DD_CRASHTRACK_END_PROCINFO"); |
| 35 | +} |
| 36 | + |
| 37 | +fn add_config(report: &mut String) { |
| 38 | + writeln!(report, "{DD_CRASHTRACK_BEGIN_CONFIG}") |
| 39 | + .expect("Failed to write DD_CRASHTRACK_BEGIN_CONFIG"); |
| 40 | + let config = CrashtrackerConfiguration::new( |
| 41 | + vec![], // additional_files |
| 42 | + true, // create_alt_stack |
| 43 | + true, // use_alt_stack |
| 44 | + None, |
| 45 | + StacktraceCollection::EnabledWithSymbolsInReceiver, |
| 46 | + default_signals(), |
| 47 | + Some(Duration::from_secs(10)), |
| 48 | + Some("".to_string()), // unix_socket_path |
| 49 | + true, // demangle_names |
| 50 | + ) |
| 51 | + .expect("Failed to create crashtracker configuration"); |
| 52 | + let config_str = |
| 53 | + serde_json::to_string(&config).expect("Failed to serialize crashtracker configuration"); |
| 54 | + writeln!(report, "{}", config_str).expect("Failed to write crashtracker configuration"); |
| 55 | + writeln!(report, "{DD_CRASHTRACK_END_CONFIG}") |
| 56 | + .expect("Failed to write DD_CRASHTRACK_END_CONFIG"); |
| 57 | +} |
| 58 | + |
| 59 | +fn add_stacktrace(report: &mut String, test_cpp_so: &SharedLibrary, test_c_so: &SharedLibrary) { |
| 60 | + writeln!(report, "{DD_CRASHTRACK_BEGIN_STACKTRACE}") |
| 61 | + .expect("Failed to write DD_CRASHTRACK_BEGIN_STACKTRACE"); |
| 62 | + |
| 63 | + add_frame!(report, "my_function", test_c_so); |
| 64 | + add_frame!(report, "func1", test_c_so); |
| 65 | + add_frame!(report, "func2", test_c_so); |
| 66 | + add_frame!(report, "0x01"); |
| 67 | + add_frame!(report, "0x02"); |
| 68 | + add_frame!(report, "_Z12cpp_functionv", test_cpp_so); |
| 69 | + add_frame!( |
| 70 | + report, |
| 71 | + "_ZN10FirstClass10InnerClass12InnerMethod1Ev", |
| 72 | + test_cpp_so |
| 73 | + ); |
| 74 | + add_frame!( |
| 75 | + report, |
| 76 | + "_ZN10FirstClass10InnerClass12InnerMethod2Ev", |
| 77 | + test_cpp_so |
| 78 | + ); |
| 79 | + add_frame!(report, "0x03"); |
| 80 | + add_frame!(report, "0x03"); |
| 81 | + add_frame!(report, "0x05"); |
| 82 | + add_frame!(report, "func3", test_c_so); |
| 83 | + add_frame!(report, "_ZN10FirstClass7Method1Ev", test_cpp_so); |
| 84 | + add_frame!(report, "func4", test_c_so); |
| 85 | + add_frame!( |
| 86 | + report, |
| 87 | + "_ZN10FirstClass7Method2EibNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE", |
| 88 | + test_cpp_so |
| 89 | + ); |
| 90 | + add_frame!(report, "0x06"); |
| 91 | + add_frame!(report, "func5", test_c_so); |
| 92 | + add_frame!(report, "func6", test_c_so); |
| 93 | + add_frame!( |
| 94 | + report, |
| 95 | + "_ZN11MyNamespace16ClassInNamespace18MethodInNamespace1Efx", |
| 96 | + test_cpp_so |
| 97 | + ); |
| 98 | + add_frame!(report, "0x07"); |
| 99 | + add_frame!( |
| 100 | + report, |
| 101 | + "_ZN11MyNamespace16ClassInNamespace18MethodInNamespace2Edc", |
| 102 | + test_cpp_so |
| 103 | + ); |
| 104 | + add_frame!(report, "0x08"); |
| 105 | + add_frame!(report, "func6", test_c_so); |
| 106 | + add_frame!(report, "0x09"); |
| 107 | + add_frame!( |
| 108 | + report, |
| 109 | + "_ZN11MyNamespace16ClassInNamespace21InnerClassInNamespace12InnerMethod1Ev", |
| 110 | + test_cpp_so |
| 111 | + ); |
| 112 | + add_frame!(report, "0x0A"); |
| 113 | + add_frame!( |
| 114 | + report, |
| 115 | + "_ZN11MyNamespace16ClassInNamespace21InnerClassInNamespace12InnerMethod2Eiix", |
| 116 | + test_cpp_so |
| 117 | + ); |
| 118 | + add_frame!(report, "0x0B"); |
| 119 | + add_frame!(report, "func7", test_c_so); |
| 120 | + add_frame!(report, "func8", test_c_so); |
| 121 | + add_frame!( |
| 122 | + report, |
| 123 | + "_ZN11MyNamespace16ClassInNamespace21InnerClassInNamespace12InnerMethod2Eiix", |
| 124 | + test_cpp_so |
| 125 | + ); |
| 126 | + add_frame!(report, "func9", test_c_so); |
| 127 | + add_frame!(report, "func10", test_c_so); |
| 128 | + add_frame!(report, "0x00"); |
| 129 | + |
| 130 | + writeln!(report, "{DD_CRASHTRACK_END_STACKTRACE}") |
| 131 | + .expect("Failed to write DD_CRASHTRACK_END_STACKTRACE"); |
| 132 | +} |
| 133 | + |
| 134 | +fn create_crash_report(test_cpp_so: &SharedLibrary, test_c_so: &SharedLibrary) -> String { |
| 135 | + // Manual test revealed that the report size was arount 3000 bytes |
| 136 | + let mut report = String::with_capacity(3000); |
| 137 | + add_proc_info(&mut report); |
| 138 | + add_config(&mut report); |
| 139 | + add_stacktrace(&mut report, test_cpp_so, test_c_so); |
| 140 | + writeln!(report, "{DD_CRASHTRACK_DONE}").expect("Failed to write DD_CRASHTRACK_DONE"); |
| 141 | + report |
| 142 | +} |
| 143 | + |
| 144 | +async fn bench_receiver_entry_point_from_str(data: &str) { |
| 145 | + let cursor = std::io::Cursor::new(data.as_bytes()); |
| 146 | + let reader = BufReader::new(cursor); |
| 147 | + let timeout = Duration::from_millis(5000); |
| 148 | + |
| 149 | + let _ = receiver_entry_point(timeout, reader).await; |
| 150 | +} |
| 151 | + |
| 152 | +fn load_test_libraries() -> (SharedLibrary, SharedLibrary) { |
| 153 | + let sofile_c_path = get_data_folder_path() |
| 154 | + .expect("Failed to get the data folder path") |
| 155 | + .join("libtest.so") |
| 156 | + .canonicalize() |
| 157 | + .unwrap(); |
| 158 | + |
| 159 | + let sofile_cpp_path = get_data_folder_path() |
| 160 | + .expect("Failed to get the data folder path") |
| 161 | + .join("libtest_cpp.so") |
| 162 | + .canonicalize() |
| 163 | + .unwrap(); |
| 164 | + |
| 165 | + let test_c_so = SharedLibrary::open(sofile_c_path.to_str().unwrap()).unwrap(); |
| 166 | + let test_cpp_so = SharedLibrary::open(sofile_cpp_path.to_str().unwrap()).unwrap(); |
| 167 | + (test_cpp_so, test_c_so) |
| 168 | +} |
| 169 | + |
| 170 | +pub fn receiver_entry_point_benchmarks(c: &mut Criterion) { |
| 171 | + let mut group = c.benchmark_group("receiver_entry_point"); |
| 172 | + |
| 173 | + // the libraries must be opened as long as the benchmark is running |
| 174 | + // That why we pass them as references |
| 175 | + let (sofile_cpp, sofile_c) = load_test_libraries(); |
| 176 | + let report = create_crash_report(&sofile_cpp, &sofile_c); |
| 177 | + group.bench_with_input( |
| 178 | + BenchmarkId::new("report", report.len()), |
| 179 | + &report, |
| 180 | + |b, data| { |
| 181 | + b.iter(|| { |
| 182 | + let rt = tokio::runtime::Builder::new_current_thread() |
| 183 | + .enable_all() |
| 184 | + .build() |
| 185 | + .unwrap(); |
| 186 | + rt.block_on(bench_receiver_entry_point_from_str(black_box(data))) |
| 187 | + }); |
| 188 | + }, |
| 189 | + ); |
| 190 | +} |
| 191 | + |
| 192 | +criterion_group!(benches, receiver_entry_point_benchmarks); |
0 commit comments