Instrument index duration

This commit is contained in:
Antoine Gersant 2019-09-02 14:28:25 -07:00
parent d2cc868a0a
commit 82ea7983aa
6 changed files with 69 additions and 1 deletions

43
Cargo.lock generated
View file

@ -576,6 +576,28 @@ dependencies = [
"redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)", "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]] [[package]]
name = "flate2" name = "flate2"
version = "1.0.9" version = "1.0.9"
@ -924,6 +946,11 @@ name = "language-tags"
version = "0.2.2" version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" 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]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.3.0" version = "1.3.0"
@ -1458,6 +1485,8 @@ dependencies = [
"diesel 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "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)", "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)", "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)", "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]] [[package]]
name = "thread_local" name = "thread_local"
version = "0.3.6" 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 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 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 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 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 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" "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 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 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 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 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 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" "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 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 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 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 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 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" "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f"

View file

@ -6,6 +6,7 @@ edition = "2018"
[features] [features]
ui = [] ui = []
profile-index = ["flame", "flamer"]
[dependencies] [dependencies]
ape = "0.2.0" ape = "0.2.0"
@ -14,6 +15,8 @@ base64 = "0.10.0"
diesel = { version = "1.4", features = ["sqlite"] } diesel = { version = "1.4", features = ["sqlite"] }
diesel_migrations = { version = "1.4", features = ["sqlite"] } diesel_migrations = { version = "1.4", features = ["sqlite"] }
error-chain = "0.12.0" error-chain = "0.12.0"
flame = { version = "0.2.2", optional = true }
flamer = { version = "0.3", optional = true }
getopts = "0.2.15" getopts = "0.2.15"
hex = "0.3" hex = "0.3"
id3 = "0.3" id3 = "0.3"

View file

@ -5,6 +5,7 @@ use diesel::prelude::*;
use diesel::sql_types; use diesel::sql_types;
use diesel::sqlite::SqliteConnection; use diesel::sqlite::SqliteConnection;
use error_chain::bail; use error_chain::bail;
use flame;
use log::{error, info}; use log::{error, info};
use regex::Regex; use regex::Regex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -169,6 +170,7 @@ struct IndexBuilder<'conn> {
} }
impl<'conn> IndexBuilder<'conn> { impl<'conn> IndexBuilder<'conn> {
#[cfg_attr(feature = "profile-index", flame)]
fn new( fn new(
connection: &Mutex<SqliteConnection>, connection: &Mutex<SqliteConnection>,
album_art_pattern: Regex, 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> { fn flush_songs(&mut self) -> Result<(), errors::Error> {
let connection = self.connection.lock().unwrap(); let connection = self.connection.lock().unwrap();
let connection = connection.deref(); let connection = connection.deref();
@ -198,6 +201,7 @@ impl<'conn> IndexBuilder<'conn> {
Ok(()) Ok(())
} }
#[cfg_attr(feature = "profile-index", flame)]
fn flush_directories(&mut self) -> Result<(), errors::Error> { fn flush_directories(&mut self) -> Result<(), errors::Error> {
let connection = self.connection.lock().unwrap(); let connection = self.connection.lock().unwrap();
let connection = connection.deref(); let connection = connection.deref();
@ -211,6 +215,7 @@ impl<'conn> IndexBuilder<'conn> {
Ok(()) Ok(())
} }
#[cfg_attr(feature = "profile-index", flame)]
fn push_song(&mut self, song: NewSong) -> Result<(), errors::Error> { fn push_song(&mut self, song: NewSong) -> Result<(), errors::Error> {
if self.new_songs.len() >= self.new_songs.capacity() { if self.new_songs.len() >= self.new_songs.capacity() {
self.flush_songs()?; self.flush_songs()?;
@ -219,6 +224,7 @@ impl<'conn> IndexBuilder<'conn> {
Ok(()) Ok(())
} }
#[cfg_attr(feature = "profile-index", flame)]
fn push_directory(&mut self, directory: NewDirectory) -> Result<(), errors::Error> { fn push_directory(&mut self, directory: NewDirectory) -> Result<(), errors::Error> {
if self.new_directories.len() >= self.new_directories.capacity() { if self.new_directories.len() >= self.new_directories.capacity() {
self.flush_directories()?; self.flush_directories()?;
@ -239,6 +245,7 @@ impl<'conn> IndexBuilder<'conn> {
Ok(None) Ok(None)
} }
#[cfg_attr(feature = "profile-index", flame)]
fn populate_directory( fn populate_directory(
&mut self, &mut self,
parent: Option<&Path>, parent: Option<&Path>,
@ -271,8 +278,9 @@ impl<'conn> IndexBuilder<'conn> {
// Insert content // Insert content
for file in fs::read_dir(path)? { for file in fs::read_dir(path)? {
let _guard = flame::start_guard("directory-entry");
let file_path = match file { let file_path = match file {
Ok(f) => f.path(), Ok(ref f) => f.path(),
_ => { _ => {
error!("File read error within {}", path_string); error!("File read error within {}", path_string);
break; break;
@ -358,6 +366,7 @@ impl<'conn> IndexBuilder<'conn> {
} }
} }
#[cfg_attr(feature = "profile-index", flame)]
fn clean<T>(db: &T) -> Result<(), errors::Error> fn clean<T>(db: &T) -> Result<(), errors::Error>
where where
T: ConnectionSource + VFSSource, T: ConnectionSource + VFSSource,
@ -417,6 +426,7 @@ where
Ok(()) Ok(())
} }
#[cfg_attr(feature = "profile-index", flame)]
fn populate<T>(db: &T) -> Result<(), errors::Error> fn populate<T>(db: &T) -> Result<(), errors::Error>
where where
T: ConnectionSource + VFSSource, T: ConnectionSource + VFSSource,
@ -453,6 +463,8 @@ where
"Library index update took {} seconds", "Library index update took {} seconds",
start.elapsed().as_secs() start.elapsed().as_secs()
); );
#[cfg(feature = "profile-index")]
flame::dump_html(&mut fs::File::create("index-flame-graph.html").unwrap()).unwrap();
Ok(()) Ok(())
} }
@ -512,6 +524,7 @@ where
} }
} }
#[cfg_attr(feature = "profile-index", flame)]
pub fn virtualize_song(vfs: &VFS, mut song: Song) -> Option<Song> { pub fn virtualize_song(vfs: &VFS, mut song: Song) -> Option<Song> {
song.path = match vfs.real_to_virtual(Path::new(&song.path)) { song.path = match vfs.real_to_virtual(Path::new(&song.path)) {
Ok(p) => p.to_string_lossy().into_owned(), Ok(p) => p.to_string_lossy().into_owned(),
@ -526,6 +539,7 @@ pub fn virtualize_song(vfs: &VFS, mut song: Song) -> Option<Song> {
Some(song) Some(song)
} }
#[cfg_attr(feature = "profile-index", flame)]
fn virtualize_directory(vfs: &VFS, mut directory: Directory) -> Option<Directory> { fn virtualize_directory(vfs: &VFS, mut directory: Directory) -> Option<Directory> {
directory.path = match vfs.real_to_virtual(Path::new(&directory.path)) { directory.path = match vfs.real_to_virtual(Path::new(&directory.path)) {
Ok(p) => p.to_string_lossy().into_owned(), Ok(p) => p.to_string_lossy().into_owned(),

View file

@ -6,6 +6,8 @@
extern crate diesel; extern crate diesel;
#[macro_use] #[macro_use]
extern crate diesel_migrations; extern crate diesel_migrations;
#[cfg(feature = "profile-index")]
#[macro_use] extern crate flamer;
#[cfg(unix)] #[cfg(unix)]
use libsystemd::daemon::{self, NotifyState}; use libsystemd::daemon::{self, NotifyState};

View file

@ -24,6 +24,7 @@ pub struct SongTags {
pub year: Option<i32>, pub year: Option<i32>,
} }
#[cfg_attr(feature = "profile-index", flame)]
pub fn read(path: &Path) -> Result<SongTags> { pub fn read(path: &Path) -> Result<SongTags> {
match utils::get_audio_format(path) { match utils::get_audio_format(path) {
Some(AudioFormat::FLAC) => read_flac(path), Some(AudioFormat::FLAC) => read_flac(path),
@ -34,6 +35,7 @@ pub fn read(path: &Path) -> Result<SongTags> {
} }
} }
#[cfg_attr(feature = "profile-index", flame)]
fn read_id3(path: &Path) -> Result<SongTags> { fn read_id3(path: &Path) -> Result<SongTags> {
let tag = id3::Tag::read_from_path(&path)?; let tag = id3::Tag::read_from_path(&path)?;
let duration = mp3_duration::from_path(&path) let duration = mp3_duration::from_path(&path)
@ -92,6 +94,7 @@ fn read_ape_x_of_y(item: &ape::Item) -> Option<u32> {
} }
} }
#[cfg_attr(feature = "profile-index", flame)]
fn read_ape(path: &Path) -> Result<SongTags> { fn read_ape(path: &Path) -> Result<SongTags> {
let tag = ape::read(path)?; let tag = ape::read(path)?;
let artist = tag.item("Artist").and_then(read_ape_string); let artist = tag.item("Artist").and_then(read_ape_string);
@ -113,6 +116,7 @@ fn read_ape(path: &Path) -> Result<SongTags> {
}) })
} }
#[cfg_attr(feature = "profile-index", flame)]
fn read_vorbis(path: &Path) -> Result<SongTags> { fn read_vorbis(path: &Path) -> Result<SongTags> {
let file = fs::File::open(path)?; let file = fs::File::open(path)?;
let source = OggStreamReader::new(file)?; let source = OggStreamReader::new(file)?;
@ -144,6 +148,7 @@ fn read_vorbis(path: &Path) -> Result<SongTags> {
Ok(tags) Ok(tags)
} }
#[cfg_attr(feature = "profile-index", flame)]
fn read_flac(path: &Path) -> Result<SongTags> { fn read_flac(path: &Path) -> Result<SongTags> {
let tag = metaflac::Tag::read_from_path(path)?; let tag = metaflac::Tag::read_from_path(path)?;
let vorbis = tag.vorbis_comments().ok_or("Missing Vorbis comments")?; let vorbis = tag.vorbis_comments().ok_or("Missing Vorbis comments")?;

View file

@ -34,6 +34,7 @@ pub enum AudioFormat {
OGG, OGG,
} }
#[cfg_attr(feature = "profile-index", flame)]
pub fn get_audio_format(path: &Path) -> Option<AudioFormat> { pub fn get_audio_format(path: &Path) -> Option<AudioFormat> {
let extension = match path.extension() { let extension = match path.extension() {
Some(e) => e, Some(e) => e,