mirror of https://github.com/boa-dev/boa.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
5.0 KiB
153 lines
5.0 KiB
2 years ago
|
//! The **`boa_profiler`** crate is a code profiler for Boa.
|
||
|
//!
|
||
|
//! # Crate Overview
|
||
|
//!
|
||
2 years ago
|
//! This crate provides a code profiler for Boa. For more information, please
|
||
|
//! see Boa's page on [profiling][profiler-md].
|
||
2 years ago
|
//!
|
||
|
//! [profiler-md]: https://github.com/boa-dev/boa/blob/main/docs/profiling.md
|
||
1 year ago
|
#![doc = include_str!("../ABOUT.md")]
|
||
2 years ago
|
#![doc(
|
||
|
html_logo_url = "https://raw.githubusercontent.com/boa-dev/boa/main/assets/logo.svg",
|
||
|
html_favicon_url = "https://raw.githubusercontent.com/boa-dev/boa/main/assets/logo.svg"
|
||
|
)]
|
||
2 years ago
|
#![cfg_attr(not(test), forbid(clippy::unwrap_used))]
|
||
2 years ago
|
#![cfg_attr(not(feature = "profiler"), no_std)]
|
||
2 years ago
|
|
||
2 years ago
|
use core::fmt::{self, Debug};
|
||
5 years ago
|
|
||
|
#[cfg(feature = "profiler")]
|
||
2 years ago
|
use measureme::{EventId, Profiler as MeasuremeProfiler, StringId, TimingGuard};
|
||
5 years ago
|
#[cfg(feature = "profiler")]
|
||
|
use once_cell::sync::OnceCell;
|
||
|
#[cfg(feature = "profiler")]
|
||
2 years ago
|
use rustc_hash::FxHashMap;
|
||
|
#[cfg(feature = "profiler")]
|
||
|
use std::collections::hash_map::Entry;
|
||
|
#[cfg(feature = "profiler")]
|
||
|
use std::sync::RwLock;
|
||
|
#[cfg(feature = "profiler")]
|
||
5 years ago
|
use std::{
|
||
|
path::Path,
|
||
|
thread::{current, ThreadId},
|
||
|
};
|
||
|
|
||
2 years ago
|
/// Profiler for the Boa JavaScript engine.
|
||
5 years ago
|
#[cfg(feature = "profiler")]
|
||
3 years ago
|
pub struct Profiler {
|
||
|
profiler: MeasuremeProfiler,
|
||
2 years ago
|
string_cache: RwLock<FxHashMap<String, StringId>>,
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
/// This static instance must never be public, and its only access must be done through the
|
||
|
/// `global()` and `drop()` methods. This is because `get_or_init` manages synchronization and the
|
||
|
/// case of an empty value.
|
||
5 years ago
|
#[cfg(feature = "profiler")]
|
||
3 years ago
|
static mut INSTANCE: OnceCell<Profiler> = OnceCell::new();
|
||
5 years ago
|
|
||
|
#[cfg(feature = "profiler")]
|
||
3 years ago
|
impl Profiler {
|
||
2 years ago
|
/// Start a new profiled event.
|
||
4 years ago
|
pub fn start_event(&self, label: &str, category: &str) -> TimingGuard<'_> {
|
||
2 years ago
|
let kind = self.get_or_alloc_string(category);
|
||
|
let id = EventId::from_label(self.get_or_alloc_string(label));
|
||
5 years ago
|
let thread_id = Self::thread_id_to_u32(current().id());
|
||
|
self.profiler
|
||
|
.start_recording_interval_event(kind, id, thread_id)
|
||
|
}
|
||
|
|
||
2 years ago
|
#[allow(clippy::significant_drop_tightening)]
|
||
2 years ago
|
fn get_or_alloc_string(&self, s: &str) -> StringId {
|
||
|
{
|
||
|
// Check the cache only with the read lock first.
|
||
|
let cache = self
|
||
|
.string_cache
|
||
|
.read()
|
||
|
.expect("Some writer panicked while holding an exclusive lock.");
|
||
|
if let Some(id) = cache.get(s) {
|
||
|
return *id;
|
||
|
}
|
||
|
}
|
||
|
let mut cache = self
|
||
|
.string_cache
|
||
|
.write()
|
||
|
.expect("Some writer panicked while holding an exclusive lock.");
|
||
|
let entry = cache.entry(s.into());
|
||
|
match entry {
|
||
|
Entry::Occupied(entry) => *entry.get(),
|
||
|
Entry::Vacant(entry) => {
|
||
|
let id = self.profiler.alloc_string(s);
|
||
|
*entry.insert(id)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
fn default() -> Self {
|
||
3 years ago
|
let profiler =
|
||
|
MeasuremeProfiler::new(Path::new("./my_trace")).expect("must be able to create file");
|
||
2 years ago
|
Self {
|
||
|
profiler,
|
||
|
string_cache: RwLock::new(FxHashMap::default()),
|
||
|
}
|
||
5 years ago
|
}
|
||
|
|
||
2 years ago
|
/// Return the global instance of the profiler.
|
||
|
#[must_use]
|
||
3 years ago
|
pub fn global() -> &'static Self {
|
||
5 years ago
|
unsafe { INSTANCE.get_or_init(Self::default) }
|
||
|
}
|
||
|
|
||
2 years ago
|
/// Drop the global instance of the profiler.
|
||
1 year ago
|
///
|
||
|
/// # Panics
|
||
|
///
|
||
|
/// Calling `drop` will panic if `INSTANCE` cannot be taken back.
|
||
5 years ago
|
pub fn drop(&self) {
|
||
|
// In order to drop the INSTANCE we need to get ownership of it, which isn't possible on a static unless you make it a mutable static
|
||
|
// mutating statics is unsafe, so we need to wrap it as so.
|
||
|
// This is actually safe though because init and drop are only called at the beginning and end of the application
|
||
|
unsafe {
|
||
|
INSTANCE
|
||
|
.take()
|
||
|
.expect("Could not take back profiler instance");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Sadly we need to use the unsafe method until this is resolved:
|
||
|
// https://github.com/rust-lang/rust/issues/67939
|
||
|
// Once `as_64()` is in stable we can do this:
|
||
|
// https://github.com/rust-lang/rust/pull/68531/commits/ea42b1c5b85f649728e3a3b334489bac6dce890a
|
||
|
// Until then our options are: use rust-nightly or use unsafe {}
|
||
2 years ago
|
#[allow(clippy::cast_possible_truncation)]
|
||
5 years ago
|
fn thread_id_to_u32(tid: ThreadId) -> u32 {
|
||
|
unsafe { std::mem::transmute::<ThreadId, u64>(tid) as u32 }
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
impl Debug for Profiler {
|
||
5 years ago
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||
|
Debug::fmt("no debug implemented", f)
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
/// An empty profiler that does nothing.
|
||
5 years ago
|
#[cfg(not(feature = "profiler"))]
|
||
2 years ago
|
#[derive(Copy, Clone)]
|
||
3 years ago
|
pub struct Profiler;
|
||
5 years ago
|
|
||
|
#[cfg(not(feature = "profiler"))]
|
||
3 years ago
|
impl Profiler {
|
||
2 years ago
|
/// Does nothing.
|
||
|
#[allow(clippy::unused_unit)]
|
||
|
pub const fn start_event(&self, _label: &str, _category: &str) -> () {}
|
||
5 years ago
|
|
||
2 years ago
|
/// Does nothing.
|
||
|
pub const fn drop(&self) {}
|
||
5 years ago
|
|
||
2 years ago
|
/// Does nothing.
|
||
|
#[must_use]
|
||
|
pub const fn global() -> Self {
|
||
3 years ago
|
Self
|
||
5 years ago
|
}
|
||
|
}
|