-
Notifications
You must be signed in to change notification settings - Fork 2
/
CpuTopology.pm
executable file
·144 lines (122 loc) · 3.05 KB
/
CpuTopology.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# CpuTopology.pm
# query sysfs to read the system's cpu topology.
#
# Assumptions:
# - The system is a regular hierarchy of sockets->cores->threads. That is:
# + All sockets have the same amount of cores
# + All cores have the same amount of threads
# - sysfs exists
# + sysfs is mounted on /sys
package CpuTopology;
use strict;
use warnings;
use Exporter qw(import);
our @EXPORT_OK = qw(
cpus
init
n_cores_per_socket
n_sockets
n_threads_per_core
threads_in_socket
sockets
);
my $sysfs_path = '/sys/devices/system/cpu';
my %sockets;
my @cpus;
sub read_param {
my ($id, $param) = @_;
my $val;
my $filename = "$sysfs_path/cpu$id/topology/$param";
open(INPUT, "<$filename") or die("Cannot open $filename: $!\n");
while (<INPUT>) {
chomp;
if ($_ =~ /^([0-9]+)$/) {
$val = $1;
} else {
die ("Cannot interpret '$_' in $filename\n");
}
}
close(INPUT) or die("Cannot close $filename: $!\n");
return $val;
}
sub get_cpus {
opendir(my $dh, $sysfs_path) || die "can't opendir $sysfs_path: $!";
my @array = grep { /^cpu[0-9]+$/ } readdir($dh);
closedir $dh;
foreach my $cpu (@array) {
$cpu =~ s/cpu//;
}
@cpus = sort { $a <=> $b } @array;
if (@cpus == 0) {
die ("0 cpus seen -- is sysfs mounted on /sys? Are you on Linux?");
}
}
# No need to call this function unless you have CPU hotplug, in which
# case by calling it you'll force a read of the topology.
sub init {
get_cpus();
my $sock;
foreach my $id (@cpus) {
my $socket = read_param($id, 'physical_package_id');
my $core = read_param($id, 'core_id');
push @{ $sock->{$socket}->{$core} }, $id;
}
# clean up $sock, since socket/core id's might not be contiguous
my $i = 0;
foreach my $s (sort { $a <=> $b } keys %$sock) {
my $j = 0;
foreach my $c (sort { $a <=> $b } keys %{ $sock->{$s} }) {
push @{ $sockets{$i}{$j} }, sort { $a <=> $b } @{ $sock->{$s}{$c} };
$j++;
}
$i++;
}
}
sub _init {
if (@cpus and %sockets) {
return;
}
init();
}
sub cpus {
_init();
return @cpus;
}
sub n_cores_per_socket {
_init();
return scalar(keys %{ $sockets{0} });
}
sub n_sockets {
_init();
return scalar(keys %sockets);
}
sub n_threads_per_core {
_init();
return scalar(@{ $sockets{0}->{0} });
}
# Returns a list of threads in the same socket.
# $max_threads_per_core threads from each core in the socket are returned.
sub threads_in_socket {
my ($socket, $max_threads_per_core) = @_;
_init();
die ("Socket $socket does not exist\n") if !$sockets{$socket};
my $n = $max_threads_per_core;
my $max_n = n_threads_per_core();
if ($n > $max_n) {
warn "requested max_threads_per_core=$max_threads_per_core > n_threads_per_core=$max_n: using $max_n\n";
$n = $max_n;
}
my @ret = ();
foreach my $core (sort { $a <=> $b } keys %{ $sockets{$socket} }) {
for (my $i = 0; $i < $n; $i++) {
my $thread_list = $sockets{$socket}->{$core};
push @ret, $thread_list->[$i];
}
}
return @ret;
}
sub sockets {
_init();
return %sockets;
}
return 1;