Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vscode/cspell.dictionaries/jargon.wordlist.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
AFAICT
asimd
ASIMD
alloc
arity
autogenerate
Expand Down Expand Up @@ -70,6 +72,7 @@ hardlink
hardlinks
hasher
hashsums
hwcaps
infile
iflag
iflags
Expand Down Expand Up @@ -145,6 +148,8 @@ tokenize
toolchain
totalram
truthy
tunables
TUNABLES
ucase
unbuffered
udeps
Expand Down
15 changes: 12 additions & 3 deletions src/uu/wc/src/count_fast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// file that was distributed with this source code.

// cSpell:ignore sysconf
use crate::word_count::WordCount;
use crate::{cpu_features, word_count::WordCount};

use super::WordCountable;

Expand Down Expand Up @@ -232,6 +232,7 @@ pub(crate) fn count_bytes_chars_and_lines_fast<
) -> (WordCount, Option<io::Error>) {
let mut total = WordCount::default();
let buf: &mut [u8] = &mut AlignedBuffer::default().data;
let simd_allowed = cpu_features::simd_policy().env_allows_simd();
loop {
match handle.read(buf) {
Ok(0) => return (total, None),
Expand All @@ -240,10 +241,18 @@ pub(crate) fn count_bytes_chars_and_lines_fast<
total.bytes += n;
}
if COUNT_CHARS {
total.chars += bytecount::num_chars(&buf[..n]);
total.chars += if simd_allowed {
bytecount::num_chars(&buf[..n])
} else {
bytecount::naive_num_chars(&buf[..n])
};
}
if COUNT_LINES {
total.lines += bytecount::count(&buf[..n], b'\n');
total.lines += if simd_allowed {
bytecount::count(&buf[..n], b'\n')
} else {
bytecount::naive_count(&buf[..n], b'\n')
};
}
}
Err(ref e) if e.kind() == ErrorKind::Interrupted => (),
Expand Down
99 changes: 99 additions & 0 deletions src/uu/wc/src/cpu_features.rs
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should agree on a common implementation to put in uucore for it to be reused for the --debug flag of cksum (see #9088)

Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

use std::env;
use std::sync::OnceLock;

#[derive(Debug)]
pub(crate) struct SimdPolicy {
disabled_by_env: Vec<String>,
available: Vec<&'static str>,
}

impl SimdPolicy {
fn detect() -> Self {
let tunables = env::var_os("GLIBC_TUNABLES")
.and_then(|value| value.into_string().ok())
.unwrap_or_default();

let disabled_by_env = parse_disabled_features(&tunables);
let available = detect_available_features();

Self {
disabled_by_env,
available,
}
}

pub(crate) fn env_allows_simd(&self) -> bool {
self.disabled_by_env.is_empty()
}

pub(crate) fn disabled_features(&self) -> &[String] {
&self.disabled_by_env
}

pub(crate) fn available_features(&self) -> &[&'static str] {
&self.available
}
}

static SIMD_POLICY: OnceLock<SimdPolicy> = OnceLock::new();

pub(crate) fn simd_policy() -> &'static SimdPolicy {
SIMD_POLICY.get_or_init(SimdPolicy::detect)
}

fn parse_disabled_features(tunables: &str) -> Vec<String> {
if tunables.is_empty() {
return Vec::new();
}

let mut disabled = Vec::new();

for entry in tunables.split(':') {
let entry = entry.trim();
let Some((name, raw_value)) = entry.split_once('=') else {
continue;
};

if name.trim() != "glibc.cpu.hwcaps" {
continue;
}

for token in raw_value.split(',') {
let token = token.trim();
if !token.starts_with('-') {
continue;
}
let feature = token.trim_start_matches('-').to_ascii_uppercase();
if !feature.is_empty() {
disabled.push(feature);
}
}
}

disabled
}

fn detect_available_features() -> Vec<&'static str> {
let mut features = Vec::new();
#[cfg(any(target_arch = "x86", target_arch = "x86_64")]
{
if std::arch::is_x86_feature_detected!("avx2") {
features.push("AVX2");
}
if std::arch::is_x86_feature_detected!("sse2") {
features.push("SSE2");
}
}
#[cfg(all(target_arch = "aarch64", target_endian = "little"))]
{
if std::arch::is_aarch64_feature_detected!("asimd") {
features.push("ASIMD");
}
}
features
}
37 changes: 37 additions & 0 deletions src/uu/wc/src/wc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

mod count_fast;
mod countable;
mod cpu_features;
mod utf8;
mod word_count;

Expand Down Expand Up @@ -49,6 +50,7 @@ struct Settings<'a> {
show_lines: bool,
show_words: bool,
show_max_line_length: bool,
debug: bool,
files0_from: Option<Input<'a>>,
total_when: TotalWhen,
}
Expand All @@ -62,6 +64,7 @@ impl Default for Settings<'_> {
show_lines: true,
show_words: true,
show_max_line_length: false,
debug: false,
files0_from: None,
total_when: TotalWhen::default(),
}
Expand All @@ -85,6 +88,7 @@ impl<'a> Settings<'a> {
show_lines: matches.get_flag(options::LINES),
show_words: matches.get_flag(options::WORDS),
show_max_line_length: matches.get_flag(options::MAX_LINE_LENGTH),
debug: matches.get_flag(options::DEBUG),
files0_from,
total_when,
};
Expand All @@ -95,6 +99,7 @@ impl<'a> Settings<'a> {
Self {
files0_from: settings.files0_from,
total_when,
debug: settings.debug,
..Default::default()
}
}
Expand Down Expand Up @@ -122,6 +127,7 @@ mod options {
pub static MAX_LINE_LENGTH: &str = "max-line-length";
pub static TOTAL: &str = "total";
pub static WORDS: &str = "words";
pub static DEBUG: &str = "debug";
}
static ARG_FILES: &str = "files";
static STDIN_REPR: &str = "-";
Expand Down Expand Up @@ -445,6 +451,12 @@ pub fn uu_app() -> Command {
.help(translate!("wc-help-words"))
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(options::DEBUG)
.long(options::DEBUG)
.action(ArgAction::SetTrue)
.hide(true),
)
.arg(
Arg::new(ARG_FILES)
.action(ArgAction::Append)
Expand Down Expand Up @@ -814,6 +826,31 @@ fn wc(inputs: &Inputs, settings: &Settings) -> UResult<()> {
_ => (compute_number_width(inputs, settings), true),
};

if settings.debug {
let policy = cpu_features::simd_policy();
if policy.env_allows_simd() {
let available = policy.available_features();
if available.is_empty() {
eprintln!("wc: debug: hardware support unavailable on this CPU");
} else {
eprintln!(
"wc: debug: using hardware support (features: {})",
available.join(", ")
);
}
} else {
let disabled = policy.disabled_features();
if disabled.is_empty() {
eprintln!("wc: debug: hardware support disabled by environment");
} else {
eprintln!(
"wc: debug: hardware support disabled by GLIBC_TUNABLES ({})",
disabled.join(", ")
);
}
}
}

for maybe_input in inputs.try_iter(settings)? {
num_inputs += 1;

Expand Down
Loading