Skip to content

Instantly share code, notes, and snippets.

@oconnor663
Created March 24, 2022 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oconnor663/aed7016c9dbe5507510fc50faceaaa07 to your computer and use it in GitHub Desktop.
Save oconnor663/aed7016c9dbe5507510fc50faceaaa07 to your computer and use it in GitHub Desktop.
BLAKE3 and SHA-256 power usage
[package]
name = "blake3_loop"
version = "0.1.0"
edition = "2021"
[dependencies]
blake3 = "1.3.1"
ctrlc = "3.2.1"
rand = "0.8.5"
use rand::prelude::*;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Instant;
static BYTES_SO_FAR: AtomicU64 = AtomicU64::new(0);
fn hash(input: &[u8]) -> [u8; 32] {
blake3::hash(input).into()
}
fn main() {
let mut input = vec![0u8; 65536];
let mut rng = rand::thread_rng();
rng.fill(&mut input[..]);
let first = hash(&input);
let start_time = Instant::now();
ctrlc::set_handler(move || {
let duration = Instant::now() - start_time;
let nanoseconds = duration.as_nanos() as f64;
let bytes = BYTES_SO_FAR.load(Ordering::Relaxed) as f64;
println!();
println!(" Runtime: {:.3} seconds", nanoseconds / 1e9);
println!("Total hashed: {:.3} GB", bytes / 1e9);
println!(" Hash rate: {:.3} GB/s", bytes / nanoseconds);
std::process::exit(0);
})
.expect("Error setting Ctrl-C handler");
loop {
let again = hash(&input);
assert_eq!(first, again);
BYTES_SO_FAR.fetch_add(input.len() as u64, Ordering::Relaxed);
}
}
Runtime: 162.852 seconds
Total hashed: 1385.480 GB
Hash rate: 8.508 GB/s
$ sudo powerstat -R -c -z
Running for 60.0 seconds (60 samples at 1.0 second intervals).
Power measurements will start in 0 seconds time.
Time User Nice Sys Idle IO Run Ctxt/s IRQ/s Fork Exec Exit Watts
14:26:04 12.6 0.0 0.1 87.1 0.1 2 693 708 1 0 1 30.70
14:26:05 12.5 0.0 0.1 87.4 0.0 2 271 617 0 0 0 30.73
14:26:06 12.5 0.0 0.1 87.2 0.1 2 342 648 0 0 0 30.69
14:26:07 12.6 0.0 0.1 87.2 0.0 2 284 552 0 0 0 30.53
14:26:08 12.8 0.0 0.1 87.1 0.0 2 360 589 0 0 0 30.39
14:26:09 12.6 0.0 0.3 87.1 0.0 2 829 730 1 0 1 30.44
14:26:10 12.5 0.0 0.0 87.5 0.0 2 241 632 0 0 0 30.10
14:26:11 12.6 0.0 0.1 87.2 0.0 2 281 654 0 0 0 30.10
14:26:12 12.5 0.0 0.1 87.4 0.0 2 308 589 0 0 0 30.11
14:26:13 12.5 0.0 0.0 87.5 0.0 2 252 637 0 0 0 29.83
14:26:14 12.6 0.0 0.2 87.1 0.0 2 353 666 1 0 1 29.77
14:26:15 12.6 0.0 0.1 87.1 0.1 2 348 622 0 0 0 29.56
14:26:16 12.5 0.0 0.0 87.5 0.0 2 230 573 0 0 0 29.41
14:26:17 12.6 0.0 0.2 87.1 0.0 2 309 626 0 0 0 29.29
14:26:18 12.8 0.0 0.6 86.5 0.1 2 1382 929 0 0 0 29.71
14:26:19 12.5 0.0 0.3 87.2 0.0 2 434 686 1 0 1 29.16
14:26:20 12.5 0.0 0.1 87.4 0.0 2 274 627 0 0 0 28.95
Time User Nice Sys Idle IO Run Ctxt/s IRQ/s Fork Exec Exit Watts
14:26:21 12.5 0.0 0.1 87.4 0.0 2 289 629 0 0 0 28.89
14:26:22 12.5 0.1 0.1 87.2 0.0 2 278 583 0 0 0 28.81
14:26:23 12.5 0.0 0.0 87.5 0.0 2 288 606 0 0 0 28.91
14:26:24 12.5 0.0 0.3 87.2 0.0 2 396 637 1 0 1 29.03
14:26:25 12.5 0.0 0.0 87.5 0.0 2 259 598 0 0 0 28.99
14:26:26 12.5 0.0 0.1 87.4 0.0 2 292 639 0 0 0 29.06
14:26:27 12.5 0.0 0.1 87.3 0.0 2 327 601 0 0 0 29.10
14:26:28 12.6 0.0 0.1 87.1 0.1 2 378 587 0 0 0 29.30
14:26:29 12.7 0.0 0.0 87.3 0.0 2 423 615 1 0 1 29.34
14:26:30 12.6 0.0 0.1 87.2 0.0 2 309 618 0 0 0 29.21
14:26:31 12.5 0.0 0.1 87.4 0.0 2 236 565 0 0 0 29.15
14:26:32 12.5 0.0 0.0 87.5 0.0 2 265 598 0 0 0 29.27
14:26:33 12.5 0.0 0.1 87.3 0.0 2 292 605 0 0 0 29.39
14:26:34 12.6 0.0 0.1 87.2 0.0 2 335 575 1 0 1 29.35
14:26:35 12.6 0.0 0.1 87.2 0.0 2 278 625 0 0 0 29.26
14:26:36 12.5 0.0 0.1 87.4 0.0 2 336 621 0 0 0 29.39
14:26:37 12.6 0.0 0.1 87.2 0.0 2 274 591 0 0 0 29.33
Time User Nice Sys Idle IO Run Ctxt/s IRQ/s Fork Exec Exit Watts
14:26:38 12.5 0.0 0.1 87.4 0.0 2 297 619 0 0 0 29.44
14:26:39 12.5 0.0 0.3 87.2 0.0 2 406 665 1 0 1 29.50
14:26:40 12.5 0.0 0.0 87.5 0.0 2 233 600 0 0 0 29.32
14:26:41 12.5 0.0 0.2 87.2 0.0 2 337 661 0 0 0 29.46
14:26:42 12.6 0.0 0.1 87.2 0.0 2 321 650 0 0 0 29.50
14:26:43 12.5 0.0 0.0 87.5 0.0 2 235 602 0 0 0 29.41
14:26:44 12.5 0.0 0.1 87.4 0.0 2 357 646 1 0 1 29.61
14:26:45 12.6 0.0 0.1 87.2 0.0 2 301 666 0 0 0 29.58
14:26:46 12.5 0.0 0.1 87.4 0.0 2 274 602 2 0 2 29.31
14:26:47 12.6 0.0 0.1 87.2 0.0 2 311 625 0 0 0 29.47
14:26:48 12.5 0.0 0.1 87.4 0.0 2 267 641 0 0 0 29.55
14:26:49 12.6 0.0 0.0 87.4 0.0 2 369 608 1 0 1 29.51
14:26:50 12.5 0.0 0.1 87.3 0.0 2 281 626 0 0 0 29.47
14:26:51 12.5 0.0 0.1 87.4 0.0 2 274 628 0 0 0 29.56
14:26:52 12.6 0.0 0.1 87.2 0.0 2 224 557 0 0 0 29.40
14:26:53 12.5 0.0 0.1 87.4 0.0 2 282 625 0 0 0 29.50
14:26:54 12.5 0.0 0.1 87.3 0.0 2 368 648 1 0 1 29.64
Time User Nice Sys Idle IO Run Ctxt/s IRQ/s Fork Exec Exit Watts
14:26:55 12.5 0.0 0.1 87.4 0.0 2 245 595 0 0 0 29.54
14:26:56 12.6 0.0 0.1 87.2 0.0 2 272 611 0 0 0 29.57
14:26:57 12.5 0.0 0.3 87.2 0.0 2 359 614 0 0 0 29.59
14:26:58 12.5 0.0 0.1 87.4 0.0 2 240 613 0 0 0 29.32
14:26:59 12.5 0.0 0.4 87.0 0.1 2 865 723 1 0 1 29.63
14:27:00 12.7 0.0 0.0 87.3 0.0 2 300 631 0 0 0 29.54
14:27:01 12.5 0.0 0.1 87.4 0.0 2 268 614 0 0 0 29.47
14:27:02 12.5 0.0 0.1 87.4 0.0 2 286 610 0 0 0 29.47
14:27:03 12.6 0.0 0.1 87.2 0.0 2 271 633 0 0 0 29.48
-------- ----- ----- ----- ----- ----- ---- ------ ------ ---- ---- ---- ------
Average 12.6 0.0 0.1 87.3 0.0 2.0 344.8 628.2 0.2 0.0 0.2 29.53
GeoMean 12.6 0.0 0.0 87.3 0.0 2.0 321.2 626.3 0.0 0.0 0.0 29.53
StdDev 0.1 0.0 0.1 0.1 0.0 0.0 180.0 52.5 0.5 0.0 0.5 0.44
-------- ----- ----- ----- ----- ----- ---- ------ ------ ---- ---- ---- ------
Minimum 12.5 0.0 0.0 86.5 0.0 2.0 224.0 552.0 0.0 0.0 0.0 28.81
Maximum 12.8 0.1 0.6 87.5 0.1 2.0 1382.0 929.0 2.0 0.0 2.0 30.73
-------- ----- ----- ----- ----- ----- ---- ------ ------ ---- ---- ---- ------
Summary:
CPU: 29.53 Watts on average with standard deviation 0.44
Note: power read from RAPL domains: package-0, uncore, package-0, core, psys.
These readings do not cover all the hardware in this device.
C-State Resident Count Latency
C3_ACPI 78.745% 13734 1048
C2_ACPI 8.094% 12769 253
C1_ACPI 0.133% 1854 1
POLL 0.001% 51 0
C0 13.026%
[package]
name = "sha256_loop"
version = "0.1.0"
edition = "2021"
[dependencies]
ctrlc = "3.2.1"
openssl = "0.10.38"
rand = "0.8.5"
use rand::prelude::*;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Instant;
static BYTES_SO_FAR: AtomicU64 = AtomicU64::new(0);
fn hash(input: &[u8]) -> [u8; 32] {
openssl::hash::hash(openssl::hash::MessageDigest::sha256(), input)
.unwrap()
.as_ref()
.try_into()
.unwrap()
}
fn main() {
let mut input = vec![0u8; 65536];
let mut rng = rand::thread_rng();
rng.fill(&mut input[..]);
let first = hash(&input);
let start_time = Instant::now();
ctrlc::set_handler(move || {
let duration = Instant::now() - start_time;
let nanoseconds = duration.as_nanos() as f64;
let bytes = BYTES_SO_FAR.load(Ordering::Relaxed) as f64;
println!();
println!(" Runtime: {:.3} seconds", nanoseconds / 1e9);
println!("Total hashed: {:.3} GB", bytes / 1e9);
println!(" Hash rate: {:.3} GB/s", bytes / nanoseconds);
std::process::exit(0);
})
.expect("Error setting Ctrl-C handler");
loop {
let again = hash(&input);
assert_eq!(first, again);
BYTES_SO_FAR.fetch_add(input.len() as u64, Ordering::Relaxed);
}
}
Runtime: 74.444 seconds
Total hashed: 134.945 GB
Hash rate: 1.813 GB/s
$ sudo powerstat -R -c -z
Running for 60.0 seconds (60 samples at 1.0 second intervals).
Power measurements will start in 0 seconds time.
Time User Nice Sys Idle IO Run Ctxt/s IRQ/s Fork Exec Exit Watts
14:28:53 12.6 0.0 0.1 87.1 0.1 2 1121 716 0 0 0 26.77
14:28:54 12.6 0.0 0.3 87.1 0.0 2 491 538 1 0 1 26.89
14:28:55 12.6 0.0 0.1 87.2 0.0 2 313 464 0 0 0 26.67
14:28:56 12.6 0.0 0.0 87.4 0.0 2 272 616 0 0 0 26.83
14:28:57 12.6 0.0 0.0 87.4 0.0 2 308 538 0 0 0 26.64
14:28:58 12.6 0.0 0.0 87.4 0.0 2 287 470 0 0 0 26.64
14:28:59 12.6 0.0 0.1 87.2 0.0 2 424 532 1 0 0 26.84
14:29:00 12.6 0.0 0.0 87.4 0.0 2 308 508 0 0 1 26.72
14:29:01 12.5 0.0 0.0 87.5 0.0 2 294 592 0 0 0 26.84
14:29:02 12.6 0.0 0.1 87.2 0.0 2 278 612 0 0 0 26.87
14:29:03 12.5 0.0 0.0 87.5 0.0 2 236 511 0 0 0 26.66
14:29:04 12.6 0.0 0.1 87.2 0.0 2 341 500 1 0 1 26.80
14:29:05 12.6 0.0 0.1 87.2 0.0 2 297 498 0 0 0 26.75
14:29:06 12.5 0.0 0.0 87.5 0.0 2 239 615 0 0 0 26.70
14:29:07 12.6 0.0 0.0 87.4 0.0 2 352 623 0 0 0 26.84
14:29:08 12.7 0.0 0.0 87.3 0.0 2 299 614 0 0 0 26.85
14:29:09 12.8 0.0 0.2 87.0 0.0 2 452 607 1 0 0 27.15
Time User Nice Sys Idle IO Run Ctxt/s IRQ/s Fork Exec Exit Watts
14:29:10 12.7 0.0 0.0 87.3 0.0 2 337 621 0 0 1 26.85
14:29:11 12.5 0.0 0.1 87.4 0.0 2 291 603 0 0 0 26.86
14:29:12 12.6 0.0 0.0 87.4 0.0 2 269 617 0 0 0 26.78
14:29:13 12.6 0.0 0.2 87.1 0.0 2 311 599 0 0 0 26.81
14:29:14 12.7 0.0 0.1 87.2 0.0 2 402 651 1 0 1 26.95
14:29:15 12.6 0.0 0.0 87.4 0.0 2 260 553 0 0 0 26.79
14:29:16 12.5 0.0 0.1 87.4 0.0 2 256 636 0 0 0 26.81
14:29:17 12.6 0.0 0.1 87.2 0.0 2 331 579 0 0 0 26.85
14:29:18 12.5 0.0 0.0 87.5 0.0 2 247 598 0 0 0 26.75
14:29:19 12.8 0.0 0.0 87.2 0.0 2 380 549 1 0 0 26.95
14:29:20 12.5 0.0 0.3 87.2 0.0 2 789 640 0 0 2 27.01
14:29:21 12.6 0.0 0.0 87.4 0.0 2 261 432 0 0 0 26.78
14:29:22 12.6 0.0 0.1 87.3 0.0 2 279 456 0 0 0 26.82
14:29:23 12.7 0.0 0.1 87.1 0.0 2 333 488 0 0 0 26.97
14:29:24 12.7 0.0 0.0 87.3 0.0 2 321 457 1 0 1 26.93
14:29:25 12.6 0.0 0.0 87.4 0.0 2 317 552 0 0 0 26.86
14:29:26 12.5 0.0 0.0 87.5 0.0 2 285 643 0 0 0 26.87
Time User Nice Sys Idle IO Run Ctxt/s IRQ/s Fork Exec Exit Watts
14:29:27 12.6 0.0 0.0 87.4 0.0 2 278 595 0 0 0 26.81
14:29:28 12.5 0.0 0.0 87.5 0.0 2 259 626 0 0 0 26.87
14:29:29 12.7 0.0 0.1 87.2 0.0 2 397 612 1 0 0 27.03
14:29:30 12.6 0.0 0.0 87.4 0.0 2 297 617 0 0 1 26.77
14:29:31 12.5 0.0 0.0 87.5 0.0 2 316 640 0 0 0 26.89
14:29:32 12.6 0.0 0.1 87.2 0.0 2 284 665 0 0 0 26.90
14:29:33 12.5 0.0 0.0 87.5 0.0 2 252 543 0 0 0 26.77
14:29:34 12.6 0.0 0.1 87.2 0.0 2 394 522 1 0 1 27.02
14:29:35 12.6 0.0 0.0 87.4 0.0 2 333 477 0 0 0 26.85
14:29:36 12.6 0.0 0.0 87.4 0.0 2 265 449 1 0 0 26.79
14:29:37 12.8 0.0 0.1 87.1 0.0 2 459 708 0 0 1 27.02
14:29:38 12.5 0.0 0.0 87.5 0.0 2 287 660 0 0 0 26.87
14:29:39 12.9 0.0 0.0 87.1 0.0 2 459 618 1 0 0 27.11
14:29:40 12.5 0.0 0.1 87.4 0.0 2 332 662 0 0 1 26.91
14:29:41 12.7 0.0 0.0 87.3 0.0 2 332 645 0 0 0 26.88
14:29:42 12.6 0.0 0.0 87.4 0.0 2 298 645 0 0 0 26.78
14:29:43 12.8 0.0 0.0 87.2 0.0 2 338 524 0 0 0 26.88
Time User Nice Sys Idle IO Run Ctxt/s IRQ/s Fork Exec Exit Watts
14:29:44 12.6 0.0 0.1 87.2 0.0 2 358 522 1 0 1 27.08
14:29:45 12.5 0.0 0.1 87.4 0.0 2 279 445 0 0 0 26.90
14:29:46 12.6 0.0 0.0 87.4 0.0 2 304 455 0 0 0 26.97
14:29:47 12.5 0.0 0.1 87.3 0.0 2 398 539 2 0 4 27.10
14:29:48 12.5 0.0 0.0 87.5 0.0 2 223 423 0 0 0 26.94
14:29:49 12.8 0.0 0.0 87.2 0.0 2 469 646 1 0 3 27.08
14:29:50 12.6 0.0 0.1 87.2 0.0 2 301 634 0 0 1 26.88
14:29:51 12.6 0.0 0.0 87.4 0.0 2 275 464 0 0 0 26.81
14:29:52 12.5 0.0 0.0 87.5 0.0 2 273 659 0 0 0 26.87
-------- ----- ----- ----- ----- ----- ---- ------ ------ ---- ---- ---- ------
Average 12.6 0.0 0.1 87.3 0.0 2.0 340.7 570.4 0.2 0.0 0.3 26.86
GeoMean 12.6 0.0 0.0 87.3 0.0 2.0 325.6 565.2 0.0 0.0 0.0 26.86
StdDev 0.1 0.0 0.1 0.1 0.0 0.0 133.1 75.2 0.5 0.0 0.7 0.11
-------- ----- ----- ----- ----- ----- ---- ------ ------ ---- ---- ---- ------
Minimum 12.5 0.0 0.0 87.0 0.0 2.0 223.0 423.0 0.0 0.0 0.0 26.64
Maximum 12.9 0.0 0.3 87.5 0.1 2.0 1121.0 716.0 2.0 0.0 4.0 27.15
-------- ----- ----- ----- ----- ----- ---- ------ ------ ---- ---- ---- ------
Summary:
CPU: 26.86 Watts on average with standard deviation 0.11
Note: power read from RAPL domains: package-0, uncore, package-0, core, psys.
These readings do not cover all the hardware in this device.
C-State Resident Count Latency
C3_ACPI 81.419% 14583 1048
C2_ACPI 5.533% 8963 253
C1_ACPI 0.109% 988 1
POLL 0.000% 37 0
C0 12.939%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment