-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
104 additions
and
41 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use core::arch::asm; | ||
|
||
#[derive(Clone, Debug, Default)] | ||
pub struct ArmCounter; | ||
|
||
impl ArmCounter { | ||
pub fn now(&self) -> u64 { | ||
let count: u64; | ||
|
||
unsafe { asm!("mrs {}, cntvct_el0", out(reg) count); } | ||
|
||
count | ||
} | ||
|
||
pub fn freq_hz(&self) -> Option<u64> { | ||
let freq_hz: u64; | ||
|
||
unsafe { asm!("mrs {}, cntfrq_el0", out(reg) freq_hz); } | ||
|
||
Some(freq_hz) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#[cfg(target_arch = "aarch64")] | ||
mod aarch64; | ||
|
||
#[cfg(target_arch = "aarch64")] | ||
pub use self::aarch64::Counter; | ||
|
||
#[cfg(all(target_arch = "x86_64", target_feature = "sse2"))] | ||
mod x86_64; | ||
|
||
#[cfg(all(target_arch = "x86_64", target_feature = "sse2"))] | ||
pub use self::x86_64::Counter; | ||
|
||
#[cfg(not(any( | ||
all(target_arch = "x86_64", target_feature = "sse2"), | ||
target_arch = "aarch64", | ||
)))] | ||
impl Counter { | ||
pub fn now(&self) -> u64 { | ||
panic!("can't use counter without TSC (x86_64) or system counter (ARM) support"); | ||
} | ||
|
||
pub fn freq_hz(&self) -> Option<u64> { | ||
None | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use core::arch::x86_64::_rdtsc; | ||
|
||
#[derive(Clone, Debug, Default)] | ||
pub struct Counter; | ||
|
||
impl Counter { | ||
pub fn now(&self) -> u64 { | ||
unsafe { _rdtsc() } | ||
} | ||
|
||
pub fn freq_hz(&self) -> Option<u64> { | ||
// TODO: This is where we could potentially query the CPU or add model/arch-specific | ||
// overrides where the TSC frequency is known. From what I remember reading through the | ||
// Intel SDM, some processors _do_ have a fixed TSC frequency? Needs more investigation, | ||
// especially to also consider AMD, and so on. | ||
None | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#[allow(dead_code)] | ||
#[cfg(all(target_arch = "x86_64", target_feature = "sse2"))] | ||
pub fn has_counter_support() -> bool { | ||
let cpuid = raw_cpuid::CpuId::new(); | ||
let has_invariant_tsc = cpuid | ||
.get_advanced_power_mgmt_info() | ||
.map_or(false, |apm| apm.has_invariant_tsc()); | ||
let has_rdtscp = cpuid | ||
.get_extended_processor_and_feature_identifiers() | ||
.map_or(false, |epf| epf.has_rdtscp()); | ||
|
||
has_invariant_tsc && has_rdtscp | ||
} | ||
|
||
#[cfg(target_arch = "aarch64")] | ||
pub fn has_counter_support() -> bool { | ||
// AArch64 implies ARMv8 or above, where the system counter is always present. | ||
true | ||
} | ||
|
||
#[allow(dead_code)] | ||
#[cfg(not(any( | ||
all(target_arch = "x86_64", target_feature = "sse2"), | ||
target_arch = "aarch64", | ||
)))] | ||
pub fn has_counter_support() -> bool { | ||
false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters