[pve-devel] r5519 - in pve-common/trunk/data: . PVE

svn-commits at proxmox.com svn-commits at proxmox.com
Mon Feb 14 08:36:33 CET 2011


Author: dietmar
Date: 2011-02-14 08:36:33 +0100 (Mon, 14 Feb 2011)
New Revision: 5519

Modified:
   pve-common/trunk/data/ChangeLog
   pve-common/trunk/data/PVE/ProcFSTools.pm
Log:
impl. new helpers read_loadavg(),
	read_meminfo() and read_proc_stat().



Modified: pve-common/trunk/data/ChangeLog
===================================================================
--- pve-common/trunk/data/ChangeLog	2011-02-14 06:15:34 UTC (rev 5518)
+++ pve-common/trunk/data/ChangeLog	2011-02-14 07:36:33 UTC (rev 5519)
@@ -1,3 +1,8 @@
+2011-02-14  Proxmox Support Team  <support at proxmox.com>
+
+	* PVE/ProcFSTools.pm: impl. new helpers read_loadavg(),
+	read_meminfo() and read_proc_stat().
+
 2011-02-08  Proxmox Support Team  <support at proxmox.com>
 
 	* PVE/INotify.pm (update_file): use PVE::Tools, changed interface

Modified: pve-common/trunk/data/PVE/ProcFSTools.pm
===================================================================
--- pve-common/trunk/data/PVE/ProcFSTools.pm	2011-02-14 06:15:34 UTC (rev 5518)
+++ pve-common/trunk/data/PVE/ProcFSTools.pm	2011-02-14 07:36:33 UTC (rev 5519)
@@ -2,9 +2,12 @@
 
 use strict;
 use POSIX;
+use Time::HiRes qw (gettimeofday);
 use IO::File;
 use PVE::Tools;
 
+my $clock_ticks = POSIX::sysconf(&POSIX::_SC_CLK_TCK);
+
 my $cpuinfo;
 
 # cycles_per_jiffy = frequency_of_your_cpu/jiffies_per_second
@@ -13,23 +16,19 @@
 # frequency_of_your_cpu can be read from /proc/cpuinfo, as:
 # cpu MHz : <frequency_of_your_cpu>
 
-sub get_cpu_info {
+sub read_cpuinfo {
     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",
     };
 
+    my $fh = IO::File->new ($fn, "r");
     return $res if !$fh;
 
     my $count = 0;
@@ -70,6 +69,68 @@
     return (0, 0);
 }
 
+sub read_loadavg {
+
+    my $line = PVE::Tools::file_read_firstline('/proc/loadavg');
+
+    if ($line =~ m|^(\d+\.\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)\s+\d+/\d+\s+\d+\s*$|) {
+	return wantarray ? ($1, $2, $3) : $1;
+    }
+
+    return wantarray ? (0, 0, 0) : 0;
+}
+
+my $last_proc_stat;
+
+sub read_proc_stat {
+    my $res = { user => 0, nice => 0, system => 0, idle => 0 , sum => 0};
+
+    my $cpucount = 0;
+
+    if (my $fh = IO::File->new ("/proc/stat", "r")) {
+	while (defined (my $line = <$fh>)) {
+	    if ($line =~ m|^cpu\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s|) {
+		$res->{user} = $1;
+		$res->{nice} = $2;
+		$res->{system} = $3;
+		$res->{idle} = $4;
+		$res->{used} = $1+$2+$3;
+		$res->{iowait} = $5;
+	    } elsif ($line =~ m|^cpu\d+\s|) {
+		$cpucount++;
+	    }
+	}
+	$fh->close;
+    }
+
+    $cpucount = 1 if !$cpucount;
+
+    my $ctime = gettimeofday; # floating point time in seconds
+
+    $res->{ctime} = $ctime;
+    $res->{cpu} = 0;
+    $res->{wait} = 0;
+
+    $last_proc_stat = $res if !$last_proc_stat;
+
+    my $diff = ($ctime - $last_proc_stat->{ctime}) * $clock_ticks * $cpucount;
+
+    if ($diff > 1000) { # don't update too often
+	my $useddiff =  $res->{used} - $last_proc_stat->{used};
+	$useddiff = $diff if $useddiff > $diff;
+	$res->{cpu} = $useddiff/$diff;
+	my $waitdiff =  $res->{iowait} - $last_proc_stat->{iowait};
+	$waitdiff = $diff if $waitdiff > $diff;
+	$res->{wait} = $waitdiff/$diff;
+	$last_proc_stat = $res;
+    } else {
+	$res->{cpu} = $last_proc_stat->{cpu};
+	$res->{wait} = $last_proc_stat->{wait};
+    }
+
+    return $res;
+}
+
 sub read_proc_starttime {
     my $pid = shift;
 
@@ -84,4 +145,37 @@
     return 0;
 }
 
+sub read_meminfo {
+
+    my $res = {
+	memtotal => 0,
+	memfree => 0,
+	memused => 0,
+	swaptotal => 0,
+	swapfree => 0,
+	swapused => 0,
+    };
+
+    my $fh = IO::File->new ("/proc/meminfo", "r");
+    return $res if !$fh;
+
+    my $d = {};
+    while (my $line = <$fh>) {
+	if ($line =~ m/^(\S+):\s+(\d+)\s*kB/i) {
+	    $d->{lc ($1)} = $2 * 1024;
+	} 
+    }
+    close($fh);
+
+    $res->{memtotal} = $d->{memtotal};
+    $res->{memfree} =  $d->{memfree} + $d->{buffers} + $d->{cached};
+    $res->{memused} = $res->{memtotal} - $res->{memfree};
+
+    $res->{swaptotal} = $d->{swaptotal};
+    $res->{swapfree} = $d->{swapfree};
+    $res->{swapused} = $res->{swaptotal} - $res->{swapfree};
+
+    return $res;
+}
+
 1;



More information about the pve-devel mailing list