Lib::CPUInfo - Perl interface to PyTorch's libcpuinfo C library
version 0.001
use Lib::CPUInfo qw<
initialize
get_cores_count
get_current_core
get_clusters
deinitialize
>;
# First, initialize
initialize()
or die "Cannot initialize cpuinfo";
# Get all the data you want through the functions
my $count = get_cores_count();
# Some functions return struct objects
my $core = get_current_core();
printf "Vendor: %s\n", $core->vendor();
foreach my $cluster ( get_clusters()->@* ) {
printf "Cluster (%d): %s\n", $cluster->id(), $cluster->vendor();
}
# Wrap up by de-initializing
deinitialize();
This module implements an interface to PyTorch's libcpuinfo
available
here.
Installing it on Debian and Debian-based distros:
apt install libcpuinfo0
I had written it against Debian version 0.0~git20200422.a1e0b95-2. If you find differences, please report via GitHub and I'll do my best to handle it.
If you have use for this and need an Alien module to install the library for you as a dependency, let me know.
The following functions are available:
my $success = initialize();
if ( !$success ) {...}
# or better yet
initialize()
or die "Cannot initialize libcpuinfo";
Initialize the library.
deinitialize();
De-initialize the library.
my $count = get_processors_count();
Return how many processors there are.
my $count = get_cores_count();
Return how many cores there are.
my $count = get_clusters_count();
Return how many clusters there are.
my $count = get_packages_count();
Return how many packages there are.
my $count = get_uarchs_count();
Return how many uarchs there are.
my $count = get_l1i_caches_count();
Return how many L1i caches there are.
my $count = get_l1d_caches_count();
Return how many L1d caches there are.
my $count = get_l2_caches_count();
Return how many L2 caches there are.
my $count = get_l3_caches_count();
Return how many L3 caches there are.
my $count = get_l4_caches_count();
Return how many L4 caches there are.
foreach my $processor ( get_processors()->@* ) {
# do something with processor object
}
Return an arrayref of all the processor objects.
foreach my $core ( get_cores()->@* ) {
# do something with core object
}
Return an arrayref of all the core objects.
See Lib::CPUInfo::Core.
foreach my $cluster ( get_clusters()->@* ) {
# do something with cluster object
}
Return an arrayref of all the cluster objects.
foreach my $package ( get_packages()->@* ) {
# do something with package object
}
Return an arrayref of all the package objects.
foreach my $uarch ( get_uarchs()->@* ) {
# do something with uarch object
}
Return an arrayref of all the uarch objects.
foreach my $cache ( get_l1i_caches()->@* ) {
# do something with cache object
}
Return an arrayref of all the L1i cache objects.
See Lib::CPUInfo::Cache.
foreach my $cache ( get_l1d_caches()->@* ) {
# do something with cache object
}
Return an arrayref of all the L1d cache objects.
See Lib::CPUInfo::Cache.
foreach my $cache ( get_l2_caches()->@* ) {
# do something with cache object
}
Return an arrayref of all the L2 cache objects.
See Lib::CPUInfo::Cache.
foreach my $cache ( get_l3_caches()->@* ) {
# do something with cache object
}
Return an arrayref of all the L3 cache objects.
See Lib::CPUInfo::Cache.
foreach my $cache ( get_l4_caches()->@* ) {
# do something with cache object
}
Return an arrayref of all the L4 cache objects.
See Lib::CPUInfo::Cache.
my $index = 0;
my $processor = get_processor($index);
Return the Lib::CPUInfo::Processor processor object at index $index
.
my $index = 0;
my $core = get_core($index);
Return the Lib::CPUInfo::Core core object at index $index
.
my $index = 0;
my $cluster = get_cluster($index);
Return the Lib::CPUInfo::Cluster cluster object at index $index
.
my $index = 0;
my $package = get_package($index);
Return the Lib::CPUInfo::Package package object at index $index
.
my $index = 0;
my $uarchinfo = get_uarch($index);
Return the Lib::CPUInfo::UArchInfo uarch object at index $index
.
my $index = 0;
my $cache = get_l1i_cache($index);
Return the Lib::CPUInfo::Cache L1i cache object at index $index
.
my $index = 0;
my $cache = get_l1d_cache($index);
Return the Lib::CPUInfo::Cache L1d cache object at index $index
.
my $index = 0;
my $cache = get_l2_cache($index);
Return the Lib::CPUInfo::Cache L2 cache object at index $index
.
my $index = 0;
my $cache = get_l3_cache($index);
Return the Lib::CPUInfo::Cache L3 cache object at index $index
.
my $index = 0;
my $cache = get_l4_cache($index);
Return the Lib::CPUInfo::Cache L4 cache object at index $index
.
my $size = get_max_cache_size();
Get the max cache size.
my $index = get_current_uarch_index();
Get the current UArch index, I guess?
my $core = get_current_core();
Get the current Lib::CPUInfo::Core core object.
my $processor = get_current_processor();
Get the current Lib::CPUInfo::Processor processor object.
-
Counting number of CPUs
Loops: 1,000.
Lib::CPUInfo: Ran 21 iterations (1 outliers). Lib::CPUInfo: Rounded run time per iteration: 4.163e-04 +/- 1.5e-06 (0.4%) Sys::Info::Device::CPU: Ran 25 iterations (5 outliers). Sys::Info::Device::CPU: Rounded run time per iteration: 9.4582e-01 +/- 2.9e-04 (0.0%) Rex::Inventory::Proc: Ran 21 iterations (0 outliers). Rex::Inventory::Proc: Rounded run time per iteration: 5.790e-01 +/- 1.1e-03 (0.2%)
-
Getting the CPU package name
Loops: 1,000.
Lib::CPUInfo: Ran 23 iterations (3 outliers). Lib::CPUInfo: Rounded run time per iteration: 1.2206e-02 +/- 1.3e-05 (0.1%) Sys::Info::Device::CPU: Ran 23 iterations (3 outliers). Sys::Info::Device::CPU: Rounded run time per iteration: 9.6313e-01 +/- 1.0e-03 (0.1%)
-------------- ------ ------ ------ ------ ------ ------ ------
File stmt bran cond sub pod time total
-------------- ------ ------ ------ ------ ------ ------ ------
Lib/CPUInfo.pm 100.0 n/a 63.6 100.0 100.0 100.0 93.5
Total 100.0 n/a 63.6 100.0 100.0 100.0 93.5
-------------- ------ ------ ------ ------ ------ ------ ------
This module uses FFI::Platypus to connect to the C library and FFI::C to define the object structs.
These modules also retrieve CPU information:
- Sys::Info
- Proc::CPUUsage
- Rex::Inventory::Proc
- Linux::Cpuinfo
- Linux::Proc::Cpuinfo
- Linux::Info::CpuStats
Sawyer X
This software is copyright (c) 2021 by Sawyer X.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.