-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add flamegraph generation to flamegraph example (#968)
- Loading branch information
Showing
8 changed files
with
214 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
shotover-proxy/example-configs/cassandra-cluster-v4/topology-encode.yaml
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,22 @@ | ||
--- | ||
sources: | ||
cassandra_prod: | ||
Cassandra: | ||
listen_addr: "127.0.0.1:9042" | ||
chain_config: | ||
main_chain: | ||
- DebugForceEncode: | ||
encode_requests: true | ||
encode_responses: true | ||
- CassandraSinkCluster: | ||
first_contact_points: ["172.16.1.2:9044", "172.16.1.3:9044"] | ||
local_shotover_host_id: "2dd022d6-2937-4754-89d6-02d2933a8f7a" | ||
shotover_nodes: | ||
- address: "127.0.0.1:9042" | ||
data_center: "dc1" | ||
rack: "rack1" | ||
host_id: "2dd022d6-2937-4754-89d6-02d2933a8f7a" | ||
connect_timeout_ms: 3000 | ||
|
||
source_to_chain_mapping: | ||
cassandra_prod: main_chain |
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 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 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 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 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,79 @@ | ||
use inferno::collapse::perf::{Folder, Options as CollapseOptions}; | ||
use inferno::collapse::Collapse; | ||
use inferno::flamegraph::{from_reader, Options}; | ||
use std::fs::File; | ||
use std::io::{BufReader, BufWriter}; | ||
use std::process::{Child, Command}; | ||
|
||
use crate::docker_compose::run_command; | ||
|
||
pub struct Perf(Child); | ||
|
||
impl Perf { | ||
/// Begin recording perf data to perf.data | ||
pub fn new(pid: u32) -> Perf { | ||
// remove perf.data if it exists to avoid generating perf.data.old clutter | ||
std::fs::remove_file("perf.data").ok(); | ||
|
||
// Run `sudo ls` so that we can get sudo to stop asking for password for a while. | ||
// It also lets us block until the user has entered the password, otherwise the rest of the test would continue before `perf` has started. | ||
// TODO: It would be more robust to get a single root prompt and then keep feeding commands into it. | ||
// But that would require a bunch of work so its out of scope for now. | ||
run_command("sudo", &["ls"]).unwrap(); | ||
|
||
let mut command = Command::new("sudo"); | ||
command.args([ | ||
"perf", | ||
"record", | ||
"-F", | ||
"997", | ||
"--call-graph", | ||
"dwarf,16384", | ||
"-g", | ||
"-o", | ||
"perf.data", | ||
"-p", | ||
&pid.to_string(), | ||
]); | ||
|
||
Perf(command.spawn().unwrap()) | ||
} | ||
|
||
/// Blocks until the recorded process has terminated | ||
/// Generates a flamegraph at flamegraph.svg | ||
pub fn flamegraph(mut self) { | ||
self.0.wait().unwrap(); | ||
|
||
run_command( | ||
"sudo", | ||
&["chown", &std::env::var("USER").unwrap(), "perf.data"], | ||
) | ||
.unwrap(); | ||
|
||
let output = Command::new("perf").args(["script"]).output().unwrap(); | ||
if !output.status.success() { | ||
panic!( | ||
"unable to run 'perf script': ({}) {}", | ||
output.status, | ||
std::str::from_utf8(&output.stderr).unwrap() | ||
); | ||
} | ||
let output = output.stdout; | ||
|
||
let perf_reader = BufReader::new(&*output); | ||
let mut collapsed = vec![]; | ||
let collapsed_writer = BufWriter::new(&mut collapsed); | ||
|
||
Folder::from(CollapseOptions::default()) | ||
.collapse(perf_reader, collapsed_writer) | ||
.unwrap(); | ||
|
||
let collapsed_reader = BufReader::new(&*collapsed); | ||
|
||
let filename = "flamegraph.svg"; | ||
println!("writing flamegraph to {filename:?}"); | ||
let flamegraph_writer = BufWriter::new(File::create(filename).unwrap()); | ||
|
||
from_reader(&mut Options::default(), collapsed_reader, flamegraph_writer).unwrap(); | ||
} | ||
} |
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