-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
cpuid crate refactoring - part 2 #956
cpuid crate refactoring - part 2 #956
Conversation
82d2d56
to
0aafc1d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it is a good idea to have one module for each leaf. The code it is too sparse and hard to follow. What is the benefit of that? I think it is good practice to explain the why in PRs for cases where the need for the change is not obvious.
If you create a template module, there is no need to name the individual files c3_template and t2_template. You can name them just t2 and c3 as they are going to be imported as use cpuid::template::{c3, t2}.
Ok(max_addressable_lcpu as u8) | ||
} | ||
|
||
pub struct Engine { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is Engine suppose to be?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LeafEngine.process is supposed to be the handler that configures each leaf. There was a switch-case in filter_cpuid that used to do that.
Engine is just an implementation of LeafEngine
I agree with naming the template files By splitting the cpu_leaf logic I wanted to avoid having a file that is bigger then 1000 lines and that contains multiple test modules (one per LeafEngine) and uses multiple imbrication layers. I tried to do it like that and I couldn't follow the code. Also in the future I would like to maybe be able to split the leafs into multiple folders by cpu vendor if needed. For example:
In general I think that modularity is an asset, not a liability. Anyway, let's discuss this offline. |
20f9be1
to
9851ee9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At a first glance, LGTM. I will do a in-depth review tomorrow to check all the leafs.
cpuid/src/transformer/common.rs
Outdated
assert!(get_max_cpus_per_package(u8::max_value()).is_err()); | ||
} | ||
|
||
fn test_update_feature_info_entry(cpu_count: u8, expected_htt: bool) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: you can name this check_update.... because we are prefixing with test on the test functions.
Same for the other test helper functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
9851ee9
to
23affd3
Compare
@serban300 It would be a lot easier to review if the changes got split into multiple commits. I see that in the Desctiption of changes from the conversation section you are enumerating three directions this PR heads towards. Would it be feasible to do a commit for each of them? |
The first 2 points are connected so I don't think it's feasible to split them into separate commits. |
cpuid/src/cpu_leaf.rs
Outdated
@@ -81,11 +81,11 @@ pub mod leaf_0x4 { | |||
msb_index: 7, | |||
lsb_index: 5, | |||
}; | |||
pub const MAX_ADDR_IDS_SHARING_CACHE_BITRANGE: BitRange = BitRange { | |||
pub const MAX_CPUS_PER_CORE: BitRange = BitRange { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit-ish: this looks a bit cumbersome to me. I mean, it's four lines of code, just to specify a region. What do you think about having a constructor there for BitRange, such that you could slim these definitions down to something like:
pub const MAX_CPUS_PER_CORE: BitRange = BitRange::new(25, 14);
Alternatively, a macro could be used for keeping the definitions short. So that you could do something like:
bit_range!(MAX_CPUS_PER_CORE, 25, 14)
Also nit-ish: the _index
suffix seems superfluous to me. The bit part in MSB or LSB has always implied an index, not a value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BitRange::new(25, 14);
Wouldn't work because you can't call functions when initializing a const
I like the macro idea but I would prefer declaring the variable outside of the macro:
pub const MAX_CPUS_PER_CORE_BITRANGE: BitRange = bit_range!(25, 14);
I don't know, I think MSB or LSB is opened to interpretation. It could refer either to the value of the bit or to the index. I would prefer to keep the _index
in the name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, right - the const / function thingy flew right past me. Anyway, what I was getting at was a clear / easy / quick way to scan the source and see what those constants meant.
cpuid/src/transformer/mod.rs
Outdated
if let Ok(host_bstr) = BrandString::from_host_cpuid() { | ||
if host_bstr.starts_with(b"Intel") { | ||
if let Some(freq) = host_bstr.find_freq() { | ||
let mut v3 = vec![]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't need an extra Vec
here (heap allocated). How about just:
bstr.push_bytes(b" @ ").unwrap();
bstr.push_bytes(freq)
.expect("Unexpected frequency information in host CPUID");
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. Done.
cpuid/src/transformer/mod.rs
Outdated
/// | ||
/// This is safe because we know DEFAULT_BRAND_STRING to hold valid data | ||
/// (allowed length and holding only valid ASCII chars). | ||
pub fn get_brand_string() -> BrandString { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems more like a create_ / build_ / make_, rather than a get_.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
@@ -0,0 +1,112 @@ | |||
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you use both a common.rs
and a mod.rs
? The latter seems empty.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
common.rs contains the common cpuid transformer functions like update_feature_info_entry
mod.rs contains the basics for the module like the VmSpec
struct, the Error
enum, etc.
Neither one is empty.
23affd3
to
e1707c0
Compare
- move leaf handlers into separate functions - test leaf handlers individually - move all the templates into a folder Signed-off-by: Serban Iorga <seriorga@amazon.com>
e1707c0
to
d3d13ef
Compare
@dhrgit I addressed all the comments. Please take another look. |
Signed-off-by: Serban Iorga seriorga@amazon.com
Issue #, if available: #851
Description of changes:
- move leaf handlers into separate functions
- test leaf handlers individually
- move all the templates into a folder
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.