diff --git a/bin/bolt.rs b/bin/bolt.rs index 355ae7a020d4947259d5dc91bf95f4b35b487a46..b40dfe7df75c1c52f5ef23622a6d08443fd83944 100644 --- a/bin/bolt.rs +++ b/bin/bolt.rs @@ -9,15 +9,32 @@ // See the Mulan PSL v2 for more details. // Create: 2023-8-28 -use crate::lib::fs_ext; use crate::lib::process_ext::run_child; +use crate::common::ARCH; use crate::config::RtoConfig; use std::fs; use std::path::Path; +// 因为sysboost限制最多只有10个APP可以做优化, 因此假设app名称不会冲突 +// 为了避免不同架构profile误用, 因此名称中带arch信息 +// 例子: mysqld的profile路径是 /usr/lib/sysboost.d/profile/mysqld.profile.aarch64 const SYSBOOST_BOLT_PROFILE: &str = "/usr/lib/sysboost.d/profile/"; +fn get_profile_path(conf: &RtoConfig) -> String { + if let Some(profile_path_str) = conf.profile_path.clone() { + return profile_path_str; + } else { + match Path::new(&conf.elf_path).file_name() { + Some(app_file_name) => { + return format!("{}{}.profile.{}", SYSBOOST_BOLT_PROFILE, app_file_name.to_string_lossy(), ARCH); + }, + None => {}, + } + } + return "".to_string(); +} + // support profile fn bolt_optimize_bin(conf: &RtoConfig) -> i32 { let mut args: Vec = Vec::new(); @@ -50,15 +67,13 @@ fn bolt_optimize_bin(conf: &RtoConfig) -> i32 { args.push(elf_bak_path.to_str().unwrap().to_string()); args.push("-o".to_string()); args.push(elf.split_whitespace().collect()); - let profile_str = format!("{}{}", elf, ".profile"); - if let Some(profile_path_str) = conf.profile_path.clone() { - args.push(format!("{}{}", "-data=".to_string(), profile_path_str)); - } else { - if let Some(find_file_str) = fs_ext::find_file_in_dirs(&profile_str, &SYSBOOST_BOLT_PROFILE) { - args.push(format!("{}{}", "-data=".to_string(), find_file_str)); - } + let real_profile_path = get_profile_path(conf); + if real_profile_path != "" { + args.push(format!("-data={}", real_profile_path)); + } + let ret = run_child("/usr/bin/llvm-bolt", &args); return ret; @@ -115,3 +130,29 @@ pub fn bolt_optimize(conf: &RtoConfig) -> i32 { } } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::common::ARCH; + + // cargo test -- tests::test_get_profile_path --nocapture + // 测试profile路径是否正确 + #[test] + fn test_get_profile_path() { + let conf = RtoConfig { + elf_path: "/usr/bin/mysqld".to_string(), + mode: "static".to_string(), + libs: Vec::new(), + path: None, + profile_path: None, + watch_paths: Vec::new(), + }; + let profile_str = get_profile_path(&conf); + println!("---{}", profile_str); + + // /usr/lib/sysboost.d/profile/mysqld.profile.aarch64 + let expect = format!("/usr/lib/sysboost.d/profile/mysqld.profile.{}", ARCH); + assert!(profile_str == expect, "result: {} expect: {}", profile_str, expect); + } +} diff --git a/bin/common.rs b/bin/common.rs index 1ce5c980c7d628e02998abe4de4c636c60eb65df..65ebc38e5e53c86ba91694ee41d246f3c3248b75 100644 --- a/bin/common.rs +++ b/bin/common.rs @@ -10,3 +10,9 @@ // Create: 2023-8-26 pub const SYSBOOST_PATH: &str = "/usr/bin/sysboost"; + +#[cfg(target_arch = "x86_64")] +pub const ARCH: &str = "x86_64"; + +#[cfg(target_arch = "aarch64")] +pub const ARCH: &str = "aarch64"; diff --git a/bin/daemon.rs b/bin/daemon.rs index 8f552f1cc9666f8dbf239f5165a41d9993fb1ff8..121b55d3281e1b13cd28551bd0909713bf81ab9d 100644 --- a/bin/daemon.rs +++ b/bin/daemon.rs @@ -320,6 +320,8 @@ pub fn daemon_loop() { mod tests { use super::*; use basic::logger::{self}; + use std::process::Command; + use crate::lib::process_ext::run_child; #[test] fn test_check_elf_files_modify_1() { diff --git a/profile/mysql/mysql.profile.aarch64.xz b/profile/mysql.profile.aarch64.xz similarity index 100% rename from profile/mysql/mysql.profile.aarch64.xz rename to profile/mysql.profile.aarch64.xz diff --git a/profile/mysql/mysql.profile.x86_64.xz b/profile/mysql.profile.x86_64.xz similarity index 100% rename from profile/mysql/mysql.profile.x86_64.xz rename to profile/mysql.profile.x86_64.xz diff --git a/profile/mysql.profile.xz b/profile/mysql.profile.xz deleted file mode 100644 index ecfe5874386964ac63080ee3245d0717e6c29c49..0000000000000000000000000000000000000000 Binary files a/profile/mysql.profile.xz and /dev/null differ