From 82ea7983aa652211c9839b67ef60835be735c704 Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Mon, 2 Sep 2019 14:28:25 -0700 Subject: [PATCH] Instrument index duration --- Cargo.lock | 43 +++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 +++ src/index.rs | 16 +++++++++++++++- src/main.rs | 2 ++ src/metadata.rs | 5 +++++ src/utils.rs | 1 + 6 files changed, 69 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index a7d212d..35ce85a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -576,6 +576,28 @@ dependencies = [ "redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "flame" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.98 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.98 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)", + "thread-id 3.3.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "flamer" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "flame 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.42 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "flate2" version = "1.0.9" @@ -924,6 +946,11 @@ name = "language-tags" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "lazy_static" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "lazy_static" version = "1.3.0" @@ -1458,6 +1485,8 @@ dependencies = [ "diesel 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "diesel_migrations 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "error-chain 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)", + "flame 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "flamer 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "getopts 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)", "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "id3 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2150,6 +2179,16 @@ dependencies = [ "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "thread-id" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "thread_local" version = "0.3.6" @@ -2616,6 +2655,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1" "checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" "checksum filetime 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "2f8c63033fcba1f51ef744505b3cad42510432b904c062afa67ad7ece008429d" +"checksum flame 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1fc2706461e1ee94f55cab2ed2e3d34ae9536cfa830358ef80acff1a3dacab30" +"checksum flamer 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f2add1a5e84b1ed7b5d00cdc21789a28e0a8f4e427b677313c773880ba3c4dac" "checksum flate2 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)" = "550934ad4808d5d39365e5d61727309bf18b3b02c6c56b729cb92e7dd84bc3d8" "checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3" "checksum foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" @@ -2654,6 +2695,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum jpeg-decoder 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = "c8b7d43206b34b3f94ea9445174bda196e772049b9bddbc620c9d29b2d20110d" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a" +"checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73" "checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" "checksum lazycell 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f" "checksum lewton 0.9.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8d542c1a317036c45c2aa1cf10cc9d403ca91eb2d333ef1a4917e5cb10628bd0" @@ -2783,6 +2825,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum synstructure 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "02353edf96d6e4dc81aea2d8490a7e9db177bf8acb0e951c24940bf866cb313f" "checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" "checksum term 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "edd106a334b7657c10b7c540a0106114feadeb4dc314513e97df481d5d966f42" +"checksum thread-id 3.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c7fbf4c9d56b320106cd64fd024dadfa0be7cb4706725fc44a7d7ce952d820c1" "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum tiff 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d7b7c2cfc4742bd8a32f2e614339dd8ce30dbcf676bb262bd63a2327bc5df57d" "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" diff --git a/Cargo.toml b/Cargo.toml index 0008090..066cc27 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2018" [features] ui = [] +profile-index = ["flame", "flamer"] [dependencies] ape = "0.2.0" @@ -14,6 +15,8 @@ base64 = "0.10.0" diesel = { version = "1.4", features = ["sqlite"] } diesel_migrations = { version = "1.4", features = ["sqlite"] } error-chain = "0.12.0" +flame = { version = "0.2.2", optional = true } +flamer = { version = "0.3", optional = true } getopts = "0.2.15" hex = "0.3" id3 = "0.3" diff --git a/src/index.rs b/src/index.rs index 3ceb275..3343f87 100644 --- a/src/index.rs +++ b/src/index.rs @@ -5,6 +5,7 @@ use diesel::prelude::*; use diesel::sql_types; use diesel::sqlite::SqliteConnection; use error_chain::bail; +use flame; use log::{error, info}; use regex::Regex; use serde::{Deserialize, Serialize}; @@ -169,6 +170,7 @@ struct IndexBuilder<'conn> { } impl<'conn> IndexBuilder<'conn> { + #[cfg_attr(feature = "profile-index", flame)] fn new( connection: &Mutex, album_art_pattern: Regex, @@ -185,6 +187,7 @@ impl<'conn> IndexBuilder<'conn> { }) } + #[cfg_attr(feature = "profile-index", flame)] fn flush_songs(&mut self) -> Result<(), errors::Error> { let connection = self.connection.lock().unwrap(); let connection = connection.deref(); @@ -198,6 +201,7 @@ impl<'conn> IndexBuilder<'conn> { Ok(()) } + #[cfg_attr(feature = "profile-index", flame)] fn flush_directories(&mut self) -> Result<(), errors::Error> { let connection = self.connection.lock().unwrap(); let connection = connection.deref(); @@ -211,6 +215,7 @@ impl<'conn> IndexBuilder<'conn> { Ok(()) } + #[cfg_attr(feature = "profile-index", flame)] fn push_song(&mut self, song: NewSong) -> Result<(), errors::Error> { if self.new_songs.len() >= self.new_songs.capacity() { self.flush_songs()?; @@ -219,6 +224,7 @@ impl<'conn> IndexBuilder<'conn> { Ok(()) } + #[cfg_attr(feature = "profile-index", flame)] fn push_directory(&mut self, directory: NewDirectory) -> Result<(), errors::Error> { if self.new_directories.len() >= self.new_directories.capacity() { self.flush_directories()?; @@ -239,6 +245,7 @@ impl<'conn> IndexBuilder<'conn> { Ok(None) } + #[cfg_attr(feature = "profile-index", flame)] fn populate_directory( &mut self, parent: Option<&Path>, @@ -271,8 +278,9 @@ impl<'conn> IndexBuilder<'conn> { // Insert content for file in fs::read_dir(path)? { + let _guard = flame::start_guard("directory-entry"); let file_path = match file { - Ok(f) => f.path(), + Ok(ref f) => f.path(), _ => { error!("File read error within {}", path_string); break; @@ -358,6 +366,7 @@ impl<'conn> IndexBuilder<'conn> { } } +#[cfg_attr(feature = "profile-index", flame)] fn clean(db: &T) -> Result<(), errors::Error> where T: ConnectionSource + VFSSource, @@ -417,6 +426,7 @@ where Ok(()) } +#[cfg_attr(feature = "profile-index", flame)] fn populate(db: &T) -> Result<(), errors::Error> where T: ConnectionSource + VFSSource, @@ -453,6 +463,8 @@ where "Library index update took {} seconds", start.elapsed().as_secs() ); + #[cfg(feature = "profile-index")] + flame::dump_html(&mut fs::File::create("index-flame-graph.html").unwrap()).unwrap(); Ok(()) } @@ -512,6 +524,7 @@ where } } +#[cfg_attr(feature = "profile-index", flame)] pub fn virtualize_song(vfs: &VFS, mut song: Song) -> Option { song.path = match vfs.real_to_virtual(Path::new(&song.path)) { Ok(p) => p.to_string_lossy().into_owned(), @@ -526,6 +539,7 @@ pub fn virtualize_song(vfs: &VFS, mut song: Song) -> Option { Some(song) } +#[cfg_attr(feature = "profile-index", flame)] fn virtualize_directory(vfs: &VFS, mut directory: Directory) -> Option { directory.path = match vfs.real_to_virtual(Path::new(&directory.path)) { Ok(p) => p.to_string_lossy().into_owned(), diff --git a/src/main.rs b/src/main.rs index 404b618..6ba83f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,8 @@ extern crate diesel; #[macro_use] extern crate diesel_migrations; +#[cfg(feature = "profile-index")] +#[macro_use] extern crate flamer; #[cfg(unix)] use libsystemd::daemon::{self, NotifyState}; diff --git a/src/metadata.rs b/src/metadata.rs index f52de06..8c83ca6 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -24,6 +24,7 @@ pub struct SongTags { pub year: Option, } +#[cfg_attr(feature = "profile-index", flame)] pub fn read(path: &Path) -> Result { match utils::get_audio_format(path) { Some(AudioFormat::FLAC) => read_flac(path), @@ -34,6 +35,7 @@ pub fn read(path: &Path) -> Result { } } +#[cfg_attr(feature = "profile-index", flame)] fn read_id3(path: &Path) -> Result { let tag = id3::Tag::read_from_path(&path)?; let duration = mp3_duration::from_path(&path) @@ -92,6 +94,7 @@ fn read_ape_x_of_y(item: &ape::Item) -> Option { } } +#[cfg_attr(feature = "profile-index", flame)] fn read_ape(path: &Path) -> Result { let tag = ape::read(path)?; let artist = tag.item("Artist").and_then(read_ape_string); @@ -113,6 +116,7 @@ fn read_ape(path: &Path) -> Result { }) } +#[cfg_attr(feature = "profile-index", flame)] fn read_vorbis(path: &Path) -> Result { let file = fs::File::open(path)?; let source = OggStreamReader::new(file)?; @@ -144,6 +148,7 @@ fn read_vorbis(path: &Path) -> Result { Ok(tags) } +#[cfg_attr(feature = "profile-index", flame)] fn read_flac(path: &Path) -> Result { let tag = metaflac::Tag::read_from_path(path)?; let vorbis = tag.vorbis_comments().ok_or("Missing Vorbis comments")?; diff --git a/src/utils.rs b/src/utils.rs index fa76830..f6abe49 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -34,6 +34,7 @@ pub enum AudioFormat { OGG, } +#[cfg_attr(feature = "profile-index", flame)] pub fn get_audio_format(path: &Path) -> Option { let extension = match path.extension() { Some(e) => e,