-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu.pl
More file actions
74 lines (58 loc) · 2.01 KB
/
cpu.pl
File metadata and controls
74 lines (58 loc) · 2.01 KB
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
#!/usr/bin/perl
use strict;
use warnings;
print "CPU INFORMATION\n";
print "=" x 50, "\n";
if (-e "/proc/cpuinfo") {
open(my $cpu, '<', "/proc/cpuinfo") or die "Cannot open /proc/cpuinfo: $!";
my $core_count = 0;
my %cpu_info;
while (my $line = <$cpu>) {
chomp $line;
if ($line =~ /^processor\s*:\s*(\d+)/) {
$core_count++;
$cpu_info{processor}{$1} = {};
}
elsif ($line =~ /^([^:]+):\s*(.+)/) {
my $key = $1;
my $val = $2;
# Store first occurrence for summary
if (!exists $cpu_info{summary}{$key}) {
$cpu_info{summary}{$key} = $val;
}
}
}
close $cpu;
print "Cores: $core_count\n";
if (exists $cpu_info{summary}{'model name'}) {
print "Model: $cpu_info{summary}{'model name'}\n";
}
if (exists $cpu_info{summary}{'cpu MHz'}) {
printf "Frequency: %.2f MHz\n", $cpu_info{summary}{'cpu MHz'};
}
if (exists $cpu_info{summary}{'cache size'}) {
print "Cache: $cpu_info{summary}{'cache size'}\n";
}
}
# Check CPU scaling
if (-d "/sys/devices/system/cpu") {
print "\nCPU FREQUENCY SCALING:\n";
opendir(my $dh, "/sys/devices/system/cpu") or die $!;
my @cpus = grep { /^cpu[0-9]+$/ } readdir($dh);
closedir $dh;
foreach my $cpu (sort @cpus) {
my $gov_file = "/sys/devices/system/cpu/$cpu/cpufreq/scaling_governor";
my $freq_file = "/sys/devices/system/cpu/$cpu/cpufreq/scaling_cur_freq";
if (-e $gov_file && -e $freq_file) {
open(my $gov, '<', $gov_file) or next;
open(my $freq, '<', $freq_file) or next;
my $governor = <$gov>;
my $frequency = <$freq>;
chomp $governor;
chomp $frequency;
printf "$cpu: %s @ %.2f GHz\n", $governor, $frequency / 1000000;
close $gov;
close $freq;
}
}
}