15

如何在 Perl 中获取 CPU 或核心数。我希望这个来决定动态创建线程数。下面我创建了 3 个线程。但我想根据该机器中的内核数创建线程。

#!/usr/bin/perl -w
use threads;
use Thread::Semaphore;

my $semaphore = Thread::Semaphore->new();`enter code here`
my $cur_dir   = "D:\\qout";
opendir( CURDIR, "$cur_dir" );
my @file_list : shared = readdir(CURDIR);
closedir(CURDIR);


$thr1 = threads->create( \&changemode, \@file_list, "th1" );
$thr2 = threads->create( \&changemode, \@file_list, "th2" );
$thr3 = threads->create( \&changemode, \@file_list, "th3" );

sub &changemode {

    my ($file_list) = shift;
    my ($message)   = shift;
    my ($i)         = shift;
    while (@{$file_list}) {
        my $fname;
        $semaphore->down();
        if (@{$file_list}) {
            $fname = shift(@{$file_list});
        }
        $semaphore->up();
        print("$message got access of $fname\n");
        system ("csh -fc \"chmod +w $fname\"");
        #sleep (2);
    }
}


$thr1->join();

$thr2->join();

$thr3->join();
4

7 回答 7

15

查看 CPAN 模块,例如Sys::Info::Device::CPU

   use Sys::Info;
   use Sys::Info::Constants qw( :device_cpu );
   my $info = Sys::Info->new;
   my $cpu  = $info->device( CPU => %options );

   printf "CPU: %s\n", scalar($cpu->identify)  || 'N/A';
   printf "CPU speed is %s MHz\n", $cpu->speed || 'N/A';
   printf "There are %d CPUs\n"  , $cpu->count || 1;
   printf "CPU load: %s\n"       , $cpu->load  || 0;
于 2013-08-21T14:38:13.107 回答
7

老问题,但这里是我如何告诉我的 linux 服务器上的 CPU 数量:

#!/usr/bin/perl
chomp(my $cpu_count = `grep -c -P '^processor\\s+:' /proc/cpuinfo`);
print("CPUs: $cpu_count\n");

这仅适用于 linux/cygwin。从好的方面来说,这个解决方案不需要安装任何额外的 perl 模块。

编辑:
Barak Dagan 提出了一个“仅限 perl”的解决方案(我还没有测试过):

open my $handle, "/proc/cpuinfo" or die "Can't open cpuinfo: $!\n";
printf "CPUs: %d\n", scalar (map /^processor/, <$handle>) ; 
close $handle;
于 2015-01-13T04:36:49.343 回答
4

getNumCpus方法Sys::CpuAffinity适用于许多不同的操作系统和配置。

于 2013-08-21T16:08:01.027 回答
2

无法使用Sys::InfoSys::CpuAffinity的基于 Windows 的用户的替代方法:

my $numberofcores = $ENV{"NUMBER_OF_PROCESSORS"};
于 2016-03-15T12:08:57.810 回答
0

这是我正在使用的紧凑版本:

use Path::Tiny;
sub getProcessors {
    my @cpuinfo = split "\n", path("/proc/cpuinfo")->slurp_utf8();
    return scalar (map /^processor/, @cpuinfo) ;
}
于 2017-10-23T18:30:37.900 回答
0

似乎 $ENV{NUMBER_OF_PROCESSORS} 在 Windows 上工作得很好,但我一直在寻找一种在 Linux 和 Cygwin 上也能工作的 Perl 单线器,它不调用外部可执行文件。不幸的是,我没有 Sys::* 包,我无法安装它们。

我从getconf(1)开始。它显示了配置变量:_NPROCESSORS_CONF_NPROCESSORS_ONLN。在 getconf(1) ( ) 上使用strace(1)strace -o s.log getconf -a结果证明这些信息是使用/sys/devices/system/cpu路径收集的。这个目录有cpu[0-9]+类似的子目录,这使得生活有点复杂。所以我回到了众所周知的/proc/cpuinfo,这是单线:

my $cpus = do { local @ARGV='/proc/cpuinfo'; grep /^processor\s+:/, <>;};

也许这可以扩展以获得在线核心的数量,但现在已经足够了。

于 2018-03-01T10:25:20.997 回答
0

旧线程,但我最近写了这个,它似乎运作良好......

sub getCoreCount {
  my $cpucount = 0;
  open(my $CPUINFO, "<", "/proc/cpuinfo") or die $!;


  while (my $line = <$CPUINFO>) {
    if ($line =~ /^processor/) {
      $cpucount++;
    }
  }

  close $CPUINFO;
  return $cpucount;
}

print "CPUs: ".getCoreCount()."\n";

对于 OS X/macOS 我有这个https://gist.github.com/ablakely/4328790d106ae448885fe8949f5f4301

于 2020-03-02T14:52:56.627 回答