Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scx_bpfland: use scx_utils::Cpumask #522

Merged
merged 2 commits into from
Aug 21, 2024
Merged

scx_bpfland: use scx_utils::Cpumask #522

merged 2 commits into from
Aug 21, 2024

Conversation

arighi
Copy link
Contributor

@arighi arighi commented Aug 20, 2024

Rely on scx_utils::Cpumask instead of re-implementing a custom CpuMask struct.

This is just some code refactoring, no functional change.

@arighi arighi requested review from htejun and Byte-Lab August 20, 2024 13:40
@arighi arighi changed the title scx_bpfland: use scx_utils:::Cpumask scx_bpfland: use scx_utils::Cpumask Aug 20, 2024

// Treat the last chunk separately, because it may not require the 16 hex digits padding.
if let Some(last) = slice.last() {
write!(f, "{:x}", last)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One problem with this is that if the leading hexes are zero, they'll be omitted even for possible CPUs leading to unaligned outputs. How about something like the following?

    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut masks: Vec<u32> = self
            .as_raw_slice()
            .iter()                                
            .map(|x| [*x as u32, (x >> 32) as u32]) 
            .flatten()                             
            .collect();                            

        // Throw out possible stray from u64 -> u32.
        masks.truncate((*NR_CPU_IDS + 31) / 32);

        // Print the highest 32bit. Trim digits beyond NR_CPU_IDS.
        let width = match (*NR_CPU_IDS + 3) / 4 % 8 {
            0 => 8,                                
            v => v,                                
        };                                         
        write!(f, "{:0width$x}", masks.pop().unwrap(), width = width)?;

        // The rest in descending order.
        for submask in masks.iter().rev() {
            write!(f, " {:08x}", submask)?;
        }                                          
        Ok(())                                     
    }                                              

This also cuts it on 32bit boundaries which is close to how kernel prints out cpumasks and easier to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One problem with this is that if the leading hexes are zero, they'll be omitted even for possible CPUs leading to unaligned outputs. How about something like the following?

Oh I see, even if it's equivalent, it'd be nicer to print all the bits for all the possible CPUs.

    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut masks: Vec<u32> = self
            .as_raw_slice()
            .iter()                                
            .map(|x| [*x as u32, (x >> 32) as u32]) 
            .flatten()                             
            .collect();                            

        // Throw out possible stray from u64 -> u32.
        masks.truncate((*NR_CPU_IDS + 31) / 32);

        // Print the highest 32bit. Trim digits beyond NR_CPU_IDS.
        let width = match (*NR_CPU_IDS + 3) / 4 % 8 {
            0 => 8,                                
            v => v,                                
        };                                         
        write!(f, "{:0width$x}", masks.pop().unwrap(), width = width)?;

        // The rest in descending order.
        for submask in masks.iter().rev() {
            write!(f, " {:08x}", submask)?;
        }                                          
        Ok(())                                     
    }                                              

This also cuts it on 32bit boundaries which is close to how kernel prints out cpumasks and easier to read.

I like this, also with the extra space between each 32-bit chunk to make the cpumask more readable. I'll apply this change and do some tests, thanks!

Allow to format a Cpumask as an hex string, implementing the proper
formatter LowerHex / UpperHex traits.

Signed-off-by: Tejun Heo <[email protected]>
Signed-off-by: Andrea Righi <[email protected]>
Rely on scx_utils::Cpumask instead of re-implementing a custom struct to
parse and manage CPU masks.

Signed-off-by: Andrea Righi <[email protected]>
@arighi
Copy link
Contributor Author

arighi commented Aug 21, 2024

Added @htejun's fmt() and refactored the code a bit to avoid code duplication.

@arighi arighi merged commit 014dc7b into main Aug 21, 2024
2 checks passed
@arighi arighi deleted the bpfland-cpumask branch August 21, 2024 06:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants