Make tracing available on all platforms, with or without perfetto (#33301)

* Make tracing available on all platforms

Signed-off-by: Delan Azabani <dazabani@igalia.com>

* Gate perfetto support behind its own feature

Signed-off-by: Delan Azabani <dazabani@igalia.com>

---------

Signed-off-by: Delan Azabani <dazabani@igalia.com>
This commit is contained in:
Delan Azabani 2024-09-04 20:56:29 +08:00 committed by GitHub
parent 891562be8e
commit c0ced7a524
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 33 additions and 79 deletions

View file

@ -48,7 +48,8 @@ multiview = ["libservo/multiview"]
native-bluetooth = ["libservo/native-bluetooth"]
profilemozjs = ["libservo/profilemozjs"]
refcell_backtrace = ["libservo/refcell_backtrace"]
tracing = ["dep:tracing", "dep:tracing-subscriber", "dep:tracing-perfetto"]
tracing = ["dep:tracing", "dep:tracing-subscriber"]
tracing-perfetto = ["tracing", "dep:tracing-perfetto"]
webdriver = ["libservo/webdriver"]
webgl_backtrace = ["libservo/webgl_backtrace"]
@ -62,6 +63,9 @@ mime_guess = { workspace = true }
url = { workspace = true }
servo-media = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true, optional = true }
tracing-subscriber = { workspace = true, optional = true }
tracing-perfetto = { workspace = true, optional = true }
[target.'cfg(target_os = "android")'.dependencies]
android_logger = "0.14"
@ -111,9 +115,6 @@ raw-window-handle = "0.6"
shellwords = "1.0.0"
surfman = { workspace = true, features = ["sm-x11", "sm-raw-window-handle-06"] }
tinyfiledialogs = "3.0"
tracing = { workspace = true, optional = true }
tracing-subscriber = { workspace = true, features = ["env-filter"], optional = true }
tracing-perfetto = { workspace = true, optional = true }
webxr = { git = "https://github.com/servo/webxr", features = ["ipc", "glwindow", "headless"] }
winit = "0.29.10"

View file

@ -8,42 +8,13 @@ use getopts::Options;
use log::error;
use servo::config::opts::{self, ArgumentParsingResult};
use servo::servo_config::pref;
#[cfg(feature = "tracing")]
use tracing::subscriber::set_global_default;
#[cfg(feature = "tracing")]
use tracing_perfetto::PerfettoLayer;
#[cfg(feature = "tracing")]
use tracing_subscriber::layer::SubscriberExt;
#[cfg(feature = "tracing")]
use tracing_subscriber::util::SubscriberInitExt;
#[cfg(feature = "tracing")]
use tracing_subscriber::EnvFilter;
use crate::desktop::app::App;
use crate::panic_hook;
pub fn main() {
crate::crash_handler::install();
#[cfg(feature = "tracing")]
{
let file = std::fs::File::create("servo.pftrace").unwrap();
let perfetto_layer = PerfettoLayer::new(std::sync::Mutex::new(file));
// Set up a custom tracing subscriber and PerfettoLayer for performance tracing.
// The servo.pftrace file can be uploaded to https://ui.perfetto.dev for analysis.
let env_filter_layer = EnvFilter::from_default_env();
let subscriber = tracing_subscriber::registry()
.with(env_filter_layer)
.with(perfetto_layer);
// Set the subscriber as the global default for tracing. This means it will be used
// throughout the entire program unless a thread-local subscriber is set.
// Can only be set once; subsequent attempts will fail.
// <https://docs.rs/tracing/0.1.40/src/tracing/subscriber.rs.html#30>
set_global_default(subscriber).expect("Failed to set global default subscriber");
}
crate::init_tracing();
crate::resources::init();
// Parse the command line options and store them globally

View file

@ -107,6 +107,7 @@ pub fn init(
waker: Box<dyn EventLoopWaker>,
callbacks: Box<dyn HostTrait>,
) -> Result<(), &'static str> {
crate::init_tracing();
resources::set(Box::new(ResourceReaderInstance::new()));
if let Some(prefs) = init_opts.prefs {

View file

@ -40,6 +40,7 @@ pub fn init(
callbacks: Box<dyn HostTrait>,
) -> Result<ServoGlue, &'static str> {
info!("Entered simpleservo init function");
crate::init_tracing();
resources::set(Box::new(ResourceReaderInstance::new()));
gl.clear_color(1.0, 1.0, 1.0, 1.0);

View file

@ -39,17 +39,28 @@ pub fn main() {
desktop::cli::main()
}
#[cfg(target_os = "android")]
pub fn main() {
println!(
"Cannot start /ports/servoshell/ on Android. \
Use /support/android/apk/ + `libservoshell.so` instead"
);
}
pub fn init_tracing() {
#[cfg(feature = "tracing")]
{
let subscriber = tracing_subscriber::registry();
#[cfg(target_env = "ohos")]
pub fn main() {
println!("You shouldn't start /ports/servoshell/ on OpenHarmony.");
#[cfg(feature = "tracing-perfetto")]
let subscriber = {
use tracing_subscriber::layer::SubscriberExt;
// Set up a PerfettoLayer for performance tracing.
// The servo.pftrace file can be uploaded to https://ui.perfetto.dev for analysis.
let file = std::fs::File::create("servo.pftrace").unwrap();
let perfetto_layer = tracing_perfetto::PerfettoLayer::new(std::sync::Mutex::new(file));
subscriber.with(perfetto_layer)
};
// Same as SubscriberInitExt::init, but avoids initialising the tracing-log compat layer,
// since it would break Servos FromScriptLogger and FromCompositorLogger.
// <https://docs.rs/tracing-subscriber/0.3.18/tracing_subscriber/util/trait.SubscriberInitExt.html#method.init>
// <https://docs.rs/tracing/0.1.40/tracing/#consuming-log-records>
tracing::subscriber::set_global_default(subscriber)
.expect("Failed to set tracing subscriber");
}
}
pub fn servo_version() -> String {

View file

@ -25,6 +25,8 @@ fn main() {
if #[cfg(not(any(target_os = "android", target_env = "ohos")))] {
servoshell::main()
} else {
// Android: see ports/servoshell/egl/android/simpleservo.rs.
// OpenHarmony: see ports/servoshell/egl/ohos/simpleservo.rs.
println!(
"Cannot run the servoshell `bin` executable on platforms such as \
Android or OpenHarmony. On these platforms you need to compile \