[pve-devel] r5094 - pve-common/trunk
svn-commits at proxmox.com
svn-commits at proxmox.com
Tue Sep 7 10:33:22 CEST 2010
Author: dietmar
Date: 2010-09-07 08:33:21 +0000 (Tue, 07 Sep 2010)
New Revision: 5094
Added:
pve-common/trunk/ProcFSTools.pm
Modified:
pve-common/trunk/ChangeLog
pve-common/trunk/Makefile
Log:
* ProcFSTools.pm: new file - utilities to read /proc/
* ProcFSTools.pm (get_cpu_info): read cpu info from /proc
Modified: pve-common/trunk/ChangeLog
===================================================================
--- pve-common/trunk/ChangeLog 2010-09-06 07:49:53 UTC (rev 5093)
+++ pve-common/trunk/ChangeLog 2010-09-07 08:33:21 UTC (rev 5094)
@@ -1,3 +1,9 @@
+2010-09-07 Proxmox Support Team <support at proxmox.com>
+
+ * ProcFSTools.pm: new file - utilities to read /proc/
+
+ * ProcFSTools.pm (get_cpu_info): read cpu info from /proc
+
2010-08-27 Proxmox Support Team <support at proxmox.com>
* RESTHandler.pm (cli_handler2): simplify code - allow to pass
Modified: pve-common/trunk/Makefile
===================================================================
--- pve-common/trunk/Makefile 2010-09-06 07:49:53 UTC (rev 5093)
+++ pve-common/trunk/Makefile 2010-09-07 08:33:21 UTC (rev 5094)
@@ -18,6 +18,7 @@
DEB=${PACKAGE}_${VERSION}-${PKGREL}_${ARCH}.deb
LIB_SOURCES= \
+ ProcFSTools.pm \
RPCEnvironment.pm \
CLIHandler.pm \
RESTHandler.pm \
Added: pve-common/trunk/ProcFSTools.pm
===================================================================
--- pve-common/trunk/ProcFSTools.pm (rev 0)
+++ pve-common/trunk/ProcFSTools.pm 2010-09-07 08:33:21 UTC (rev 5094)
@@ -0,0 +1,73 @@
+package PVE::ProcFSTools;
+
+use strict;
+use POSIX;
+use IO::File;
+use PVE::Tools;
+
+my $cpuinfo;
+
+# cycles_per_jiffy = frequency_of_your_cpu/jiffies_per_second
+# jiffies_per_second = 1000
+
+# frequency_of_your_cpu can be read from /proc/cpuinfo, as:
+# cpu MHz : <frequency_of_your_cpu>
+
+sub get_cpu_info {
+ my $fn = '/proc/cpuinfo';
+
+ return $cpuinfo if $cpuinfo;
+
+ my $fh = IO::File->new ($fn, "r");
+
+ my ($sysname, $nodename, $release, $version, $machine) = POSIX::uname();
+
+ my $res = {
+ model => 'unknown',
+ mhz => 0,
+ cpus => 1,
+ cpu_cycles_per_jiffy => 0,
+ kversion => "$sysname $release $version",
+ };
+
+ return $res if !$fh;
+
+ my $count = 0;
+ while (defined(my $line = <$fh>)) {
+ if ($line =~ m/^processor\s*:\s*\d+\s*$/i) {
+ $count++;
+ } elsif ($line =~ m/^model\s+name\s*:\s*(.*)\s*$/i) {
+ $res->{model} = $1 if $res->{model} eq 'unknown';
+ } elsif ($line =~ m/^cpu\s+MHz\s*:\s*(\d+\.\d+)\s*$/i) {
+ $res->{mhz} = $1 if !$res->{mhz};
+ $res->{cpu_cycles_per_jiffy} += $1 * 1000;
+ } elsif ($line =~ m/^flags\s*:.*(vmx|svm)/) {
+ $res->{hvm} = 1; # Hardware Virtual Machine (Intel VT / AMD-V)
+ }
+ }
+
+ $res->{cpus} = $count;
+
+ $fh->close;
+
+ $cpuinfo = $res;
+
+ return $res;
+}
+
+sub read_proc_uptime {
+ my $ticks = shift;
+
+ my $line = PVE::Tools::file_read_firstline("/proc/uptime");
+ if ($line && $line =~ m|^(\d+\.\d+)\s+(\d+\.\d+)\s*$|) {
+ if ($ticks) {
+ return (int($1*100), int($2*100));
+ } else {
+ return (int($1), int($2));
+ }
+ }
+
+ return (0, 0);
+}
+
+1;
More information about the pve-devel
mailing list