-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Starting with C++ binaries. This work leverages the nix build system to build the test programs [0] and executes them while running the profiler to ensure the traces are what we expect. There are some future improvements such as testing other languages and programs, as well as asserting metadata, and improving the assertion helper to ensure that the frames are correct by changing it to be a macro and not converting it to a string Test Plan ========= Added tests, CI [0]: https://github.com/javierhonduco/lightswitch-testprogs
- Loading branch information
1 parent
fc8d6b7
commit 17978cc
Showing
9 changed files
with
423 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
use std::io; | ||
use std::io::Write; | ||
use std::process::{Child, Command, Stdio}; | ||
use std::time::Duration; | ||
|
||
use lightswitch::collector::Collector; | ||
use lightswitch::profile::symbolize_profile; | ||
use lightswitch::profiler::Profiler; | ||
use lightswitch::profiler::SymbolizedAggregatedProfile; | ||
|
||
/// Find the `nix` binary either in the $PATH or in the below hardcoded location. | ||
fn nix_bin() -> String { | ||
for path in ["nix", "/nix/var/nix/profiles/default/bin/nix"] { | ||
if Command::new(path).arg("--help").output().is_ok() { | ||
return path.into(); | ||
} | ||
} | ||
|
||
panic!("`nix` could not be found in $PATH or /nix/var/nix/profiles/default/bin/nix"); | ||
} | ||
|
||
/// Builds the given test program and writes the resulting binaries under `target/nix` to prevent | ||
/// clobbering artifacts from manual builds. | ||
fn build_test_binary(target: &str) { | ||
let output = Command::new(nix_bin()) | ||
.args([ | ||
"build", | ||
&format!("./tests/testprogs#{}", target), | ||
"--out-link", | ||
"target/nix", | ||
]) | ||
.output() | ||
.expect("failed to execute process"); | ||
|
||
if !output.status.success() { | ||
io::stdout().write_all(&output.stdout).unwrap(); | ||
io::stderr().write_all(&output.stderr).unwrap(); | ||
panic!("process exited with an error"); | ||
} | ||
} | ||
|
||
struct TestProcess { | ||
child: Child, | ||
} | ||
|
||
/// Runs a test program and terminates it when the scope exits. | ||
impl TestProcess { | ||
fn new(target: &str) -> Self { | ||
Self { | ||
child: Command::new(format!("./target/nix/bin/{}", target)) | ||
.stdout(Stdio::null()) | ||
.stderr(Stdio::null()) | ||
.spawn() | ||
.unwrap(), | ||
} | ||
} | ||
|
||
fn pid(&self) -> i32 { | ||
self.child.id() as i32 | ||
} | ||
} | ||
|
||
impl Drop for TestProcess { | ||
fn drop(&mut self) { | ||
self.child.kill().unwrap(); | ||
} | ||
} | ||
|
||
fn assert_any_stack_contains( | ||
symbolized_profile: &SymbolizedAggregatedProfile, | ||
expected_stack: &[&str], | ||
) -> bool { | ||
for sample in symbolized_profile { | ||
let stack_string = sample | ||
.ustack | ||
.iter() | ||
.map(|e| e.name.clone()) | ||
.collect::<Vec<_>>() | ||
.join("::"); | ||
|
||
if stack_string.contains(&expected_stack.join("::")) { | ||
return true; | ||
} | ||
} | ||
|
||
false | ||
} | ||
|
||
#[test] | ||
fn test_integration() { | ||
let bpf_test_debug = std::env::var("TEST_DEBUG_BPF").is_ok(); | ||
|
||
build_test_binary("cpp-progs"); | ||
let cpp_proc = TestProcess::new("main_cpp_clang_O1"); | ||
|
||
let collector = Collector::new(); | ||
let mut p = Profiler::new(bpf_test_debug, bpf_test_debug, Duration::from_secs(5), 999); | ||
p.profile_pids(vec![cpp_proc.pid()]); | ||
p.run(collector.clone()); | ||
let collector = collector.lock().unwrap(); | ||
let (raw_profile, procs, objs) = collector.finish(); | ||
let symbolized_profile = symbolize_profile(&raw_profile, procs, objs); | ||
|
||
assert!(assert_any_stack_contains( | ||
&symbolized_profile, | ||
&[ | ||
"top2()", | ||
"c2()", | ||
"b2()", | ||
"a2()", | ||
"main", | ||
"__libc_start_call_main", | ||
], | ||
)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2024 Francisco Javier Honduvilla Coto | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Testing wrapper to build all the x86_64 derivations and ensure they works. | ||
nix flake show --json | jq '.packages."x86_64-linux" | keys[]' | xargs -I {} nix build .#{} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.