Skip to content

Commit

Permalink
Allow enable and set profiler settings from cli (#235)
Browse files Browse the repository at this point in the history
* Allow enable and set profiler settings from cli

* Apply suggestions from code review

Co-authored-by: Ngo Iok Ui (Wu Yu Wei) <[email protected]>
Signed-off-by: Tony <[email protected]>

---------

Signed-off-by: Tony <[email protected]>
Co-authored-by: Ngo Iok Ui (Wu Yu Wei) <[email protected]>
  • Loading branch information
Legend-Master and wusyong authored Nov 14, 2024
1 parent 348f8bc commit 2650e80
Showing 1 changed file with 58 additions and 7 deletions.
65 changes: 58 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,19 @@ use net_traits::{
response::{Response, ResponseBody},
ResourceFetchTiming,
};
use servo_config::opts::{default_opts, set_options, Opts};
use servo_config::opts::{default_opts, set_options, Opts, OutputOptions};
use winit::{dpi, window::WindowAttributes};

/// Servo time profile settings
#[derive(Clone, Debug)]
pub struct ProfilerSettings {
/// Servo time profile settings
output_options: OutputOptions,
/// When servo profiler is enabled, this is an optional path to dump a self-contained HTML file
/// visualizing the traces as a timeline.
trace_path: Option<String>,
}

/// Command line arguments.
#[derive(Clone, Debug, Default)]
pub struct CliArgs {
Expand All @@ -24,6 +34,8 @@ pub struct CliArgs {
pub window_attributes: WindowAttributes,
/// Port number to start a server to listen to remote Firefox devtools connections. 0 for random port.
pub devtools_port: Option<u16>,
/// Servo time profile settings
pub profiler_settings: Option<ProfilerSettings>,
/// Path to resource directory. If None, Verso will try to get default directory. And if that
/// still doesn't exist, all resource configuration will set to default values.
pub resource_dir: Option<PathBuf>,
Expand All @@ -45,7 +57,7 @@ fn parse_cli_args() -> Result<CliArgs, getopts::Fail> {
let args: Vec<String> = std::env::args().collect();

let mut opts = getopts::Options::new();
opts.optopt("", "url", "URL to load on start", "URL");
opts.optopt("", "url", "URL to load on start", "docs.rs");
opts.optopt("", "resources", "Path to resource directory", "PATH");
opts.optopt(
"",
Expand All @@ -58,32 +70,50 @@ fn parse_cli_args() -> Result<CliArgs, getopts::Fail> {
"",
"devtools-port",
"Launch Verso with devtools server enabled and listen to port",
"port",
"1234",
);
opts.optopt(
"p",
"profiler",
"Launch Verso with servo time profiler enabled and output to stdout with an interval",
"5",
);
opts.optopt(
"",
"profiler-output-file",
"Make servo profiler output to this file instead of stdout",
"out.tsv",
);
opts.optopt(
"",
"profiler-trace-path",
"Path to dump a self-contained HTML timeline of profiler traces",
"out.html",
);

opts.optopt(
"w",
"width",
"Initial window's width in physical unit, the height command line arg must also be set",
"",
"1280",
);
opts.optopt(
"h",
"height",
"Initial window's height in physical unit, the width command line arg must also be set",
"",
"720",
);
opts.optopt(
"x",
"",
"Initial window's top left x position in physical unit, the y command line arg must also be set. Wayland isn't supported.",
"",
"200",
);
opts.optopt(
"y",
"",
"Initial window's top left y position in physical unit, the x command line arg must also be set. Wayland isn't supported.",
"",
"200",
);

let matches: getopts::Matches = opts.parse(&args[1..])?;
Expand All @@ -109,6 +139,21 @@ fn parse_cli_args() -> Result<CliArgs, getopts::Fail> {
None
});

let profiler_settings = if let Ok(Some(profiler_interval)) = matches.opt_get("profiler") {
let profile_output = matches.opt_str("profiler-output-file");
let trace_output = matches.opt_str("profiler-trace-path");
Some(ProfilerSettings {
output_options: if let Some(output_file) = profile_output {
OutputOptions::FileName(output_file)
} else {
OutputOptions::Stdout(profiler_interval)
},
trace_path: trace_output,
})
} else {
None
};

let mut window_attributes = winit::window::Window::default_attributes();

let width = matches.opt_get::<u32>("width").unwrap_or_else(|e| {
Expand Down Expand Up @@ -161,6 +206,7 @@ fn parse_cli_args() -> Result<CliArgs, getopts::Fail> {
no_panel,
window_attributes,
devtools_port,
profiler_settings,
})
}

Expand All @@ -176,6 +222,11 @@ impl Config {
opts.devtools_port = devtools_port;
}

if let Some(ref profiler_settings) = args.profiler_settings {
opts.time_profiling = Some(profiler_settings.output_options.clone());
opts.time_profiler_trace_path = profiler_settings.trace_path.clone();
}

let resource_dir = args.resource_dir.clone().unwrap_or(resources_dir_path());

Self {
Expand Down

0 comments on commit 2650e80

Please sign in to comment.