perf: replace Arc<String> with Arc<str>

Signed-off-by: Changyuan Lyu <changyuanl@google.com>
This commit is contained in:
Changyuan Lyu 2024-08-31 22:51:07 -07:00 committed by Lencerf
parent ed2a72ccd0
commit 5304e37ab5
16 changed files with 62 additions and 51 deletions

View file

@ -354,7 +354,7 @@ fn main_run(args: RunArgs) -> Result<(), Error> {
}; };
if args.entropy { if args.entropy {
vm.add_virtio_dev("virtio-entropy".to_owned(), EntropyParam) vm.add_virtio_dev("virtio-entropy", EntropyParam)
.context(error::CreateDevice)?; .context(error::CreateDevice)?;
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
@ -396,7 +396,7 @@ fn main_run(args: RunArgs) -> Result<(), Error> {
serde_aco::from_args(&vsock, &objects).context(error::ParseArg { arg: vsock })?; serde_aco::from_args(&vsock, &objects).context(error::ParseArg { arg: vsock })?;
match param { match param {
VsockParam::Vhost(p) => vm VsockParam::Vhost(p) => vm
.add_virtio_dev("vhost-vsock".to_owned(), p) .add_virtio_dev("vhost-vsock", p)
.context(error::CreateDevice)?, .context(error::CreateDevice)?,
}; };
} }

View file

@ -71,7 +71,7 @@ pub trait UartRecv: Send + 'static {
} }
struct ConsoleWorker<U: UartRecv> { struct ConsoleWorker<U: UartRecv> {
name: Arc<String>, name: Arc<str>,
uart: U, uart: U,
poll: Poll, poll: Poll,
} }
@ -139,7 +139,7 @@ impl<U: UartRecv> ConsoleWorker<U> {
#[derive(Debug)] #[derive(Debug)]
pub struct Console { pub struct Console {
pub name: Arc<String>, pub name: Arc<str>,
worker_thread: Option<JoinHandle<()>>, worker_thread: Option<JoinHandle<()>>,
exit_waker: Waker, exit_waker: Waker,
} }
@ -148,7 +148,8 @@ const TOKEN_SHUTDOWN: Token = Token(1);
const TOKEN_STDIN: Token = Token(0); const TOKEN_STDIN: Token = Token(0);
impl Console { impl Console {
pub fn new(name: Arc<String>, uart: impl UartRecv) -> Result<Self> { pub fn new(name: impl Into<Arc<str>>, uart: impl UartRecv) -> Result<Self> {
let name = name.into();
let poll = Poll::new()?; let poll = Poll::new()?;
let waker = Waker::new(poll.registry(), TOKEN_SHUTDOWN)?; let waker = Waker::new(poll.registry(), TOKEN_SHUTDOWN)?;
let mut worker = ConsoleWorker { let mut worker = ConsoleWorker {

View file

@ -143,7 +143,7 @@ struct Pl011Reg {
/// https://developer.arm.com/documentation/ddi0183/g /// https://developer.arm.com/documentation/ddi0183/g
#[derive(Debug)] #[derive(Debug)]
pub struct Pl011<I> { pub struct Pl011<I> {
name: Arc<String>, name: Arc<str>,
irq_line: Arc<I>, irq_line: Arc<I>,
reg: Arc<Mutex<Pl011Reg>>, reg: Arc<Mutex<Pl011Reg>>,
console: Console, console: Console,
@ -156,7 +156,7 @@ where
pub fn new(base_addr: u64, irq_line: I) -> io::Result<Self> { pub fn new(base_addr: u64, irq_line: I) -> io::Result<Self> {
let irq_line = Arc::new(irq_line); let irq_line = Arc::new(irq_line);
let reg = Arc::new(Mutex::new(Pl011Reg::default())); let reg = Arc::new(Mutex::new(Pl011Reg::default()));
let name = Arc::new(format!("pl011@{base_addr:#x}")); let name: Arc<str> = Arc::from(format!("pl011@{base_addr:#x}"));
let pl011_recv = Pl011Recv { let pl011_recv = Pl011Recv {
irq_line: irq_line.clone(), irq_line: irq_line.clone(),
reg: reg.clone(), reg: reg.clone(),

View file

@ -183,7 +183,7 @@ struct SerialReg {
#[derive(Debug)] #[derive(Debug)]
pub struct Serial<I> { pub struct Serial<I> {
name: Arc<String>, name: Arc<str>,
irq_sender: Arc<I>, irq_sender: Arc<I>,
reg: Arc<Mutex<SerialReg>>, reg: Arc<Mutex<SerialReg>>,
console: Console, console: Console,
@ -283,7 +283,7 @@ where
} }
struct SerialRecv<I: IrqSender> { struct SerialRecv<I: IrqSender> {
pub name: Arc<String>, pub name: Arc<str>,
pub irq_sender: Arc<I>, pub irq_sender: Arc<I>,
pub reg: Arc<Mutex<SerialReg>>, pub reg: Arc<Mutex<SerialReg>>,
} }
@ -312,7 +312,7 @@ where
pub fn new(base_port: u16, irq_sender: I) -> io::Result<Self> { pub fn new(base_port: u16, irq_sender: I) -> io::Result<Self> {
let irq_sender = Arc::new(irq_sender); let irq_sender = Arc::new(irq_sender);
let reg = Arc::new(Mutex::new(SerialReg::default())); let reg = Arc::new(Mutex::new(SerialReg::default()));
let name = Arc::new(format!("serial_{:#x}", base_port)); let name: Arc<str> = Arc::from(format!("serial_{:#x}", base_port));
let uart_recv = SerialRecv { let uart_recv = SerialRecv {
irq_sender: irq_sender.clone(), irq_sender: irq_sender.clone(),
name: name.clone(), name: name.clone(),

View file

@ -97,10 +97,7 @@ pub struct PciBus {
impl PciBus { impl PciBus {
pub fn new() -> Self { pub fn new() -> Self {
let devices = if cfg!(target_arch = "x86_64") { let devices = if cfg!(target_arch = "x86_64") {
let bridge = PciDevice::new( let bridge = PciDevice::new("host_bridge", Arc::new(HostBridge::new()));
Arc::new("host_bridge".to_owned()),
Arc::new(HostBridge::new()),
);
HashMap::from([(Bdf(0), bridge)]) HashMap::from([(Bdf(0), bridge)])
} else { } else {
HashMap::new() HashMap::new()
@ -122,7 +119,7 @@ impl PciBus {
} }
} }
pub fn reserve(&self, bdf: Option<Bdf>, name: Arc<String>) -> Option<Bdf> { pub fn reserve(&self, bdf: Option<Bdf>, name: Arc<str>) -> Option<Bdf> {
self.segment.reserve(bdf, name) self.segment.reserve(bdf, name)
} }

View file

@ -92,12 +92,12 @@ impl MemRegionCallback for BarCallback {
#[derive(Debug)] #[derive(Debug)]
pub struct PciDevice { pub struct PciDevice {
pub name: Arc<String>, pub name: Arc<str>,
pub dev: Arc<dyn Pci>, pub dev: Arc<dyn Pci>,
} }
impl PciDevice { impl PciDevice {
pub fn new(name: Arc<String>, dev: Arc<dyn Pci>) -> PciDevice { pub fn new(name: impl Into<Arc<str>>, dev: Arc<dyn Pci>) -> PciDevice {
let config = dev.config(); let config = dev.config();
let dev_bars = &config.get_header().bars; let dev_bars = &config.get_header().bars;
for (index, dev_bar) in dev_bars.iter().enumerate() { for (index, dev_bar) in dev_bars.iter().enumerate() {
@ -114,6 +114,9 @@ impl PciDevice {
})), })),
} }
} }
PciDevice { name, dev } PciDevice {
name: name.into(),
dev,
}
} }
} }

View file

@ -59,7 +59,7 @@ impl PciSegment {
} }
} }
pub fn reserve(&self, bdf: Option<Bdf>, name: Arc<String>) -> Option<Bdf> { pub fn reserve(&self, bdf: Option<Bdf>, name: Arc<str>) -> Option<Bdf> {
let mut empty_dev = PciDevice { let mut empty_dev = PciDevice {
name: name.clone(), name: name.clone(),
dev: Arc::new(EmptyDevice), dev: Arc::new(EmptyDevice),

View file

@ -210,7 +210,7 @@ impl SlotBackend for MaskedCap {
#[derive(Debug)] #[derive(Debug)]
struct VfioCdev { struct VfioCdev {
name: Arc<String>, name: Arc<str>,
dev: Cdev, dev: Cdev,
} }
@ -397,14 +397,17 @@ where
M: MsiSender, M: MsiSender,
{ {
pub fn new( pub fn new(
name: Arc<String>, name: impl Into<Arc<str>>,
param: &VfioParam, param: &VfioParam,
ioas: Arc<Ioas>, ioas: Arc<Ioas>,
msi_sender: M, msi_sender: M,
) -> Result<VfioPciDev<M>> { ) -> Result<VfioPciDev<M>> {
let mut dev = Cdev::new(&param.cdev)?; let mut dev = Cdev::new(&param.cdev)?;
dev.attach_iommu_ioas(ioas)?; dev.attach_iommu_ioas(ioas)?;
let cdev = Arc::new(VfioCdev { dev, name }); let cdev = Arc::new(VfioCdev {
dev,
name: name.into(),
});
let region_config = cdev.dev.get_region_info(VfioPciRegion::CONFIG.raw())?; let region_config = cdev.dev.get_region_info(VfioPciRegion::CONFIG.raw())?;

View file

@ -136,21 +136,21 @@ pub struct BlockParam {
impl DevParam for BlockParam { impl DevParam for BlockParam {
type Device = Block; type Device = Block;
fn build(self, name: Arc<String>) -> Result<Block> { fn build(self, name: impl Into<Arc<str>>) -> Result<Block> {
Block::new(self, name) Block::new(self, name)
} }
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Block { pub struct Block {
name: Arc<String>, name: Arc<str>,
config: Arc<BlockConfig>, config: Arc<BlockConfig>,
disk: File, disk: File,
feature: BlockFeature, feature: BlockFeature,
} }
impl Block { impl Block {
pub fn new(param: BlockParam, name: Arc<String>) -> Result<Self> { pub fn new(param: BlockParam, name: impl Into<Arc<str>>) -> Result<Self> {
let access_disk = error::AccessFile { let access_disk = error::AccessFile {
path: param.path.as_path(), path: param.path.as_path(),
}; };
@ -171,7 +171,7 @@ impl Block {
feature |= BlockFeature::RO; feature |= BlockFeature::RO;
} }
Ok(Block { Ok(Block {
name, name: name.into(),
disk, disk,
config, config,
feature, feature,

View file

@ -146,7 +146,7 @@ struct DeviceWorker<D, S>
where where
S: IrqSender, S: IrqSender,
{ {
name: Arc<String>, name: Arc<str>,
dev: D, dev: D,
poll: Poll, poll: Poll,
memory: Arc<RamBus>, memory: Arc<RamBus>,
@ -162,7 +162,7 @@ where
S: IrqSender, S: IrqSender,
E: IoeventFd, E: IoeventFd,
{ {
pub name: Arc<String>, pub name: Arc<str>,
pub device_config: Arc<D::Config>, pub device_config: Arc<D::Config>,
pub reg: Arc<Register>, pub reg: Arc<Register>,
pub queue_regs: Arc<Vec<Queue>>, pub queue_regs: Arc<Vec<Queue>>,
@ -192,7 +192,7 @@ where
} }
pub fn new<R>( pub fn new<R>(
name: Arc<String>, name: impl Into<Arc<str>>,
dev: D, dev: D,
memory: Arc<RamBus>, memory: Arc<RamBus>,
registry: &R, registry: &R,
@ -201,6 +201,7 @@ where
where where
R: IoeventFdRegistry<IoeventFd = E>, R: IoeventFdRegistry<IoeventFd = E>,
{ {
let name = name.into();
let poll = Poll::new().context(error::CreatePoll)?; let poll = Poll::new().context(error::CreatePoll)?;
let device_config = dev.config(); let device_config = dev.config();
let mut dev_feat = dev.feature(); let mut dev_feat = dev.feature();
@ -250,7 +251,7 @@ where
state: WorkerState::Pending, state: WorkerState::Pending,
}; };
let handle = std::thread::Builder::new() let handle = std::thread::Builder::new()
.name(name.as_ref().to_owned()) .name(name.to_string())
.spawn(move || { .spawn(move || {
let r = device_worker.do_work(); let r = device_worker.do_work();
if let Err(e) = r { if let Err(e) = r {
@ -419,7 +420,7 @@ where
pub trait DevParam { pub trait DevParam {
type Device; type Device;
fn build(self, name: Arc<String>) -> Result<Self::Device>; fn build(self, name: impl Into<Arc<str>>) -> Result<Self::Device>;
fn needs_mem_shared_fd(&self) -> bool { fn needs_mem_shared_fd(&self) -> bool {
false false
} }

View file

@ -55,19 +55,19 @@ bitflags! {
#[derive(Debug)] #[derive(Debug)]
pub struct Entropy { pub struct Entropy {
name: Arc<String>, name: Arc<str>,
source: File, source: File,
config: Arc<EntropyConfig>, config: Arc<EntropyConfig>,
} }
impl Entropy { impl Entropy {
pub fn new(name: Arc<String>) -> Result<Self> { pub fn new(name: impl Into<Arc<str>>) -> Result<Self> {
let mut options = OpenOptions::new(); let mut options = OpenOptions::new();
options.custom_flags(O_NONBLOCK).read(true); options.custom_flags(O_NONBLOCK).read(true);
let path = "/dev/urandom"; let path = "/dev/urandom";
let file = options.open(path).context(error::AccessFile { path })?; let file = options.open(path).context(error::AccessFile { path })?;
Ok(Entropy { Ok(Entropy {
name, name: name.into(),
source: file, source: file,
config: Arc::new(EntropyConfig), config: Arc::new(EntropyConfig),
}) })
@ -140,7 +140,7 @@ pub struct EntropyParam;
impl DevParam for EntropyParam { impl DevParam for EntropyParam {
type Device = Entropy; type Device = Entropy;
fn build(self, name: Arc<String>) -> Result<Self::Device> { fn build(self, name: impl Into<Arc<str>>) -> Result<Self::Device> {
Entropy::new(name) Entropy::new(name)
} }
} }

View file

@ -74,7 +74,7 @@ const VHOST_USER_BACKEND_FS_UNMAP: u32 = 7;
#[derive(Debug)] #[derive(Debug)]
pub struct VuFs { pub struct VuFs {
name: Arc<String>, name: Arc<str>,
vu_dev: Arc<VuDev>, vu_dev: Arc<VuDev>,
config: Arc<FsConfig>, config: Arc<FsConfig>,
feature: u64, feature: u64,
@ -84,7 +84,8 @@ pub struct VuFs {
} }
impl VuFs { impl VuFs {
pub fn new(param: VuFsParam, name: Arc<String>) -> Result<Self> { pub fn new(param: VuFsParam, name: impl Into<Arc<str>>) -> Result<Self> {
let name = name.into();
let mut vu_dev = VuDev::new(param.socket)?; let mut vu_dev = VuDev::new(param.socket)?;
let dev_feat = vu_dev.get_features()?; let dev_feat = vu_dev.get_features()?;
let virtio_feat = VirtioFeature::from_bits_retain(dev_feat); let virtio_feat = VirtioFeature::from_bits_retain(dev_feat);
@ -167,7 +168,7 @@ pub struct VuFsParam {
impl DevParam for VuFsParam { impl DevParam for VuFsParam {
type Device = VuFs; type Device = VuFs;
fn build(self, name: Arc<String>) -> Result<Self::Device> { fn build(self, name: impl Into<Arc<str>>) -> Result<Self::Device> {
VuFs::new(self, name) VuFs::new(self, name)
} }

View file

@ -135,7 +135,7 @@ bitflags! {
#[derive(Debug)] #[derive(Debug)]
pub struct Net { pub struct Net {
name: Arc<String>, name: Arc<str>,
config: Arc<NetConfig>, config: Arc<NetConfig>,
tap_sockets: Vec<File>, tap_sockets: Vec<File>,
feature: NetFeature, feature: NetFeature,
@ -168,7 +168,7 @@ pub struct NetParam {
impl DevParam for NetParam { impl DevParam for NetParam {
type Device = Net; type Device = Net;
fn build(self, name: Arc<String>) -> Result<Net> { fn build(self, name: impl Into<Arc<str>>) -> Result<Net> {
Net::new(self, name) Net::new(self, name)
} }
} }
@ -184,7 +184,7 @@ fn new_socket(dev_tap: Option<&Path>) -> Result<File> {
} }
impl Net { impl Net {
pub fn new(param: NetParam, name: Arc<String>) -> Result<Self> { pub fn new(param: NetParam, name: impl Into<Arc<str>>) -> Result<Self> {
let mut socket = new_socket(param.tap.as_deref())?; let mut socket = new_socket(param.tap.as_deref())?;
let max_queue_pairs = param.queue_pairs.map(From::from).unwrap_or(1); let max_queue_pairs = param.queue_pairs.map(From::from).unwrap_or(1);
setup_socket(&mut socket, param.if_name.as_deref(), max_queue_pairs > 1)?; setup_socket(&mut socket, param.if_name.as_deref(), max_queue_pairs > 1)?;
@ -202,7 +202,7 @@ impl Net {
dev_feat |= NetFeature::MQ; dev_feat |= NetFeature::MQ;
} }
let net = Net { let net = Net {
name, name: name.into(),
config: Arc::new(NetConfig { config: Arc::new(NetConfig {
mac: param.mac, mac: param.mac,
max_queue_pairs, max_queue_pairs,

View file

@ -45,14 +45,14 @@ pub struct VhostVsockParam {
impl DevParam for VhostVsockParam { impl DevParam for VhostVsockParam {
type Device = VhostVsock; type Device = VhostVsock;
fn build(self, name: Arc<String>) -> Result<Self::Device> { fn build(self, name: impl Into<Arc<str>>) -> Result<Self::Device> {
VhostVsock::new(self, name) VhostVsock::new(self, name)
} }
} }
#[derive(Debug)] #[derive(Debug)]
pub struct VhostVsock { pub struct VhostVsock {
name: Arc<String>, name: Arc<str>,
vhost_dev: Arc<VhostDev>, vhost_dev: Arc<VhostDev>,
config: VsockConfig, config: VsockConfig,
features: u64, features: u64,
@ -60,7 +60,8 @@ pub struct VhostVsock {
} }
impl VhostVsock { impl VhostVsock {
pub fn new(param: VhostVsockParam, name: Arc<String>) -> Result<VhostVsock> { pub fn new(param: VhostVsockParam, name: impl Into<Arc<str>>) -> Result<VhostVsock> {
let name = name.into();
let vhost_dev = match param.dev { let vhost_dev = match param.dev {
Some(dev) => VhostDev::new(dev), Some(dev) => VhostDev::new(dev),
None => VhostDev::new("/dev/vhost-vsock"), None => VhostDev::new("/dev/vhost-vsock"),

View file

@ -184,7 +184,7 @@ pub struct VirtioPciRegisterMmio<M>
where where
M: MsiSender, M: MsiSender,
{ {
name: Arc<String>, name: Arc<str>,
reg: Arc<Register>, reg: Arc<Register>,
queues: Arc<Vec<Queue>>, queues: Arc<Vec<Queue>>,
irq_sender: Arc<PciIrqSender<M>>, irq_sender: Arc<PciIrqSender<M>>,

View file

@ -202,7 +202,7 @@ where
pub fn add_pvpanic(&mut self) -> Result<(), Error> { pub fn add_pvpanic(&mut self) -> Result<(), Error> {
let dev = PvPanic::new(); let dev = PvPanic::new();
let pci_dev = PciDevice::new("pvpanic".to_owned().into(), Arc::new(dev)); let pci_dev = PciDevice::new("pvpanic", Arc::new(dev));
self.add_pci_dev(None, pci_dev) self.add_pci_dev(None, pci_dev)
} }
@ -226,7 +226,7 @@ where
pub fn add_virtio_dev<D, P>( pub fn add_virtio_dev<D, P>(
&mut self, &mut self,
name: String, name: impl Into<Arc<str>>,
param: P, param: P,
) -> Result<Arc<VirtioPciDev<D, H>>, Error> ) -> Result<Arc<VirtioPciDev<D, H>>, Error>
where where
@ -236,7 +236,7 @@ where
if param.needs_mem_shared_fd() && !self.board.config.mem.has_shared_fd() { if param.needs_mem_shared_fd() && !self.board.config.mem.has_shared_fd() {
return error::MemNotSharedFd.fail(); return error::MemNotSharedFd.fail();
} }
let name = Arc::new(name); let name = name.into();
let bdf = self.board.pci_bus.reserve(None, name.clone()).unwrap(); let bdf = self.board.pci_bus.reserve(None, name.clone()).unwrap();
let dev = param.build(name.clone())?; let dev = param.build(name.clone())?;
if let Some(callback) = dev.mem_update_callback() { if let Some(callback) = dev.mem_update_callback() {
@ -301,8 +301,12 @@ where
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
impl Machine<Kvm> { impl Machine<Kvm> {
pub fn add_vfio_dev(&mut self, name: String, param: VfioParam) -> Result<(), Error> { pub fn add_vfio_dev(
let name = Arc::new(name); &mut self,
name: impl Into<Arc<str>>,
param: VfioParam,
) -> Result<(), Error> {
let name = name.into();
let iommu = if let Some(iommu) = &self.iommu { let iommu = if let Some(iommu) = &self.iommu {
iommu.clone() iommu.clone()
} else { } else {