NAME

Lib::CPUInfo - Perl interface to PyTorch's libcpuinfo C library

VERSION

version 0.001

SYNOPSIS

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();

DESCRIPTION

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.

FUNCTIONS

The following functions are available:

initialize

my $success = initialize();
if ( !$success ) {...}

# or better yet
initialize()
    or die "Cannot initialize libcpuinfo";

Initialize the library.

deinitialize

deinitialize();

De-initialize the library.

get_processors_count

my $count = get_processors_count();

Return how many processors there are.

get_cores_count

my $count = get_cores_count();

Return how many cores there are.

get_clusters_count

my $count = get_clusters_count();

Return how many clusters there are.

get_packages_count

my $count = get_packages_count();

Return how many packages there are.

get_uarchs_count

my $count = get_uarchs_count();

Return how many uarchs there are.

get_l1i_caches_count

my $count = get_l1i_caches_count();

Return how many L1i caches there are.

get_l1d_caches_count

my $count = get_l1d_caches_count();

Return how many L1d caches there are.

get_l2_caches_count

my $count = get_l2_caches_count();

Return how many L2 caches there are.

get_l3_caches_count

my $count = get_l3_caches_count();

Return how many L3 caches there are.

get_l4_caches_count

my $count = get_l4_caches_count();

Return how many L4 caches there are.

get_processors

foreach my $processor ( get_processors()->@* ) {
    # do something with processor object
}

Return an arrayref of all the processor objects.

See Lib::CPUInfo::Processor.

get_cores

foreach my $core ( get_cores()->@* ) {
    # do something with core object
}

Return an arrayref of all the core objects.

See Lib::CPUInfo::Core.

get_clusters

foreach my $cluster ( get_clusters()->@* ) {
    # do something with cluster object
}

Return an arrayref of all the cluster objects.

See Lib::CPUInfo::Cluster.

get_packages

foreach my $package ( get_packages()->@* ) {
    # do something with package object
}

Return an arrayref of all the package objects.

See Lib::CPUInfo::Package.

get_uarchs

foreach my $uarch ( get_uarchs()->@* ) {
    # do something with uarch object
}

Return an arrayref of all the uarch objects.

See Lib::CPUInfo::UArchInfo.

get_l1i_caches

foreach my $cache ( get_l1i_caches()->@* ) {
    # do something with cache object
}

Return an arrayref of all the L1i cache objects.

See Lib::CPUInfo::Cache.

get_l1d_caches

foreach my $cache ( get_l1d_caches()->@* ) {
    # do something with cache object
}

Return an arrayref of all the L1d cache objects.

See Lib::CPUInfo::Cache.

get_l2_caches

foreach my $cache ( get_l2_caches()->@* ) {
    # do something with cache object
}

Return an arrayref of all the L2 cache objects.

See Lib::CPUInfo::Cache.

get_l3_caches

foreach my $cache ( get_l3_caches()->@* ) {
    # do something with cache object
}

Return an arrayref of all the L3 cache objects.

See Lib::CPUInfo::Cache.

get_l4_caches

foreach my $cache ( get_l4_caches()->@* ) {
    # do something with cache object
}

Return an arrayref of all the L4 cache objects.

See Lib::CPUInfo::Cache.

get_processor($index)

my $index     = 0;
my $processor = get_processor($index);

Return the Lib::CPUInfo::Processor processor object at index $index.

get_core($index)

my $index = 0;
my $core  = get_core($index);

Return the <Lib::CPUInfo::Core> core object at index $index.

get_cluster($index)

my $index   = 0;
my $cluster = get_cluster($index);

Return the Lib::CPUInfo::Cluster cluster object at index $index.

get_package($index)

my $index   = 0;
my $package = get_package($index);

Return the Lib::CPUInfo::Package package object at index $index.

get_uarch($index)

my $index     = 0;
my $uarchinfo = get_uarch($index);

Return the Lib::CPUInfo::UArchInfo uarch object at index $index.

get_l1i_cache($index)

my $index = 0;
my $cache = get_l1i_cache($index);

Return the Lib::CPUInfo::Cache L1i cache object at index $index.

get_l1d_cache($index)

my $index = 0;
my $cache = get_l1d_cache($index);

Return the Lib::CPUInfo::Cache L1d cache object at index $index.

get_l2_cache($index)

my $index = 0;
my $cache = get_l2_cache($index);

Return the Lib::CPUInfo::Cache L2 cache object at index $index.

get_l3_cache($index)

my $index = 0;
my $cache = get_l3_cache($index);

Return the Lib::CPUInfo::Cache L3 cache object at index $index.

get_l4_cache($index)

my $index = 0;
my $cache = get_l4_cache($index);

Return the Lib::CPUInfo::Cache L4 cache object at index $index.

get_max_cache_size

my $size = get_max_cache_size();

Get the max cache size.

get_current_uarch_index

my $index = get_current_uarch_index();

Get the current UArch index, I guess?

get_current_core

my $core = get_current_core();

Get the current Lib::CPUInfo::Core core object.

get_current_processor

my $processor = get_current_processor();

Get the current Lib::CPUInfo::Processor processor object.

BENCHMARKS

  • 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%)

COVERAGE

-------------- ------ ------ ------ ------ ------ ------ ------
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
-------------- ------ ------ ------ ------ ------ ------ ------

SEE ALSO

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:

AUTHOR

Sawyer X

COPYRIGHT AND LICENSE

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.