11use crate :: prelude:: * ;
2- use lazy_static:: lazy_static;
3- use regex:: Regex ;
42use std:: collections:: HashSet ;
53use std:: fs;
6- use std:: path:: Path ;
4+ use std:: path:: { Path , PathBuf } ;
75
8- lazy_static ! {
9- static ref PERF_MAP_REGEX : Regex = Regex :: new( r"perf-(\d+)\.map" ) . unwrap( ) ;
10- }
11-
12- pub fn harvest_perf_maps ( profile_folder : & Path ) -> Result < ( ) > {
6+ pub async fn harvest_perf_maps ( profile_folder : & Path ) -> Result < ( ) > {
137 // Get profile files (files with .out extension)
148 let profile_files = fs:: read_dir ( profile_folder) ?
159 . filter_map ( |entry| entry. ok ( ) )
@@ -21,27 +15,31 @@ pub fn harvest_perf_maps(profile_folder: &Path) -> Result<()> {
2115 . iter ( )
2216 . filter_map ( |path| path. file_stem ( ) )
2317 . map ( |pid| pid. to_str ( ) . unwrap ( ) )
18+ . filter_map ( |pid| pid. parse ( ) . ok ( ) )
2419 . collect :: < HashSet < _ > > ( ) ;
2520
26- let perf_map_files = fs:: read_dir ( "/tmp" ) ?
27- . filter_map ( |entry| entry. ok ( ) )
28- . map ( |entry| entry. path ( ) )
29- . filter ( |path| {
30- path. file_name ( )
31- . and_then ( |name| name. to_str ( ) )
32- . and_then ( |name| PERF_MAP_REGEX . captures ( name) )
33- . and_then ( |captures| captures. get ( 1 ) )
34- . map ( |pid| pids. contains ( pid. as_str ( ) ) )
35- . unwrap_or ( false )
36- } ) ;
21+ harvest_perf_maps_for_pids ( profile_folder, & pids) . await
22+ }
23+
24+ pub async fn harvest_perf_maps_for_pids ( profile_folder : & Path , pids : & HashSet < i32 > ) -> Result < ( ) > {
25+ let perf_maps = pids
26+ . iter ( )
27+ . map ( |pid| format ! ( "perf-{}.map" , pid) )
28+ . map ( |file_name| {
29+ (
30+ PathBuf :: from ( "/tmp" ) . join ( & file_name) ,
31+ profile_folder. join ( & file_name) ,
32+ )
33+ } )
34+ . filter ( |( src_path, _) | src_path. exists ( ) )
35+ . collect :: < Vec < _ > > ( ) ;
36+ debug ! ( "Found {} perf maps" , perf_maps. len( ) ) ;
3737
38- for perf_map_file in perf_map_files {
39- let source_path = perf_map_file. clone ( ) ;
40- let dest_path = profile_folder. join ( perf_map_file. file_name ( ) . unwrap ( ) ) ;
41- fs:: copy ( source_path, dest_path) . map_err ( |e| {
38+ for ( src_path, dst_path) in perf_maps {
39+ fs:: copy ( & src_path, & dst_path) . map_err ( |e| {
4240 anyhow ! (
43- "Failed to copy perf map file: {} to {}: {}" ,
44- perf_map_file . display ( ) ,
41+ "Failed to copy perf map file: {:? } to {}: {}" ,
42+ src_path . file_name ( ) ,
4543 profile_folder. display( ) ,
4644 e
4745 )
0 commit comments