]> git.proxmox.com Git - pve-common.git/blobdiff - data/PVE/ProcFSTools.pm
bump version to 1.0-39
[pve-common.git] / data / PVE / ProcFSTools.pm
index 938dae2736f945d263337505e24a97b79ed256e1..d501082f2efe07e0a5a325d01495ff17f6c8b53a 100644 (file)
@@ -10,22 +10,17 @@ my $clock_ticks = POSIX::sysconf(&POSIX::_SC_CLK_TCK);
 
 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 read_cpuinfo {
     my $fn = '/proc/cpuinfo';
 
     return $cpuinfo if $cpuinfo;
 
     my $res = {
+       user_hz => $clock_ticks,
        model => 'unknown',
        mhz => 0,
        cpus => 1,
-       cpu_cycles_per_jiffy => 0,
+       sockets => 1,
     };
 
     my $fh = IO::File->new ($fn, "r");
@@ -39,10 +34,13 @@ sub read_cpuinfo {
            $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)
+       } elsif ($line =~ m/^physical id\s*:\s*(\d+)\s*$/i) {
+           my $sid = $1 + 1;
+           $res->{sockets} = $sid if $sid > $res->{sockets};
        }
+
     }
 
     $res->{cpus} = $count;
@@ -60,7 +58,7 @@ sub read_proc_uptime {
     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));
+           return (int($1*$clock_ticks), int($2*$clock_ticks));
        } else {
            return (int($1), int($2));
        }
@@ -131,18 +129,43 @@ sub read_proc_stat {
     return $res;
 }
 
-sub read_proc_starttime {
+sub read_proc_pid_stat {
     my $pid = shift;
 
     my $statstr = PVE::Tools::file_read_firstline("/proc/$pid/stat");
 
-    if ($statstr && $statstr =~ m/^$pid \(.*\) \S (-?\d+) -?\d+ -?\d+ -?\d+ -?\d+ \d+ \d+ \d+ \d+ \d+ (\d+) (\d+) (-?\d+) (-?\d+) -?\d+ -?\d+ -?\d+ 0 (\d+) (\d+) (-?\d+) \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ -?\d+ -?\d+ \d+ \d+ \d+/) {
-       my $starttime = $6;
-
-       return $starttime;
+    if ($statstr && $statstr =~ m/^$pid \(.*\) (\S) (-?\d+) -?\d+ -?\d+ -?\d+ -?\d+ \d+ \d+ \d+ \d+ \d+ (\d+) (\d+) (-?\d+) (-?\d+) -?\d+ -?\d+ -?\d+ 0 (\d+) (\d+) (-?\d+) \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ -?\d+ -?\d+ \d+ \d+ \d+/) {
+       return {
+           status => $1,
+           utime => $3,
+           stime => $4,
+           starttime => $7,
+           vsize => $8,
+           rss => $9 * 4096,
+       };
     }
 
-    return 0;
+    return undef;
+}
+
+sub check_process_running {
+    my ($pid, $pstart) = @_;
+
+    # note: waitpid only work for child processes, but not
+    # for processes spanned by other processes.
+    # kill(0, pid) return succes for zombies.
+    # So we read the status form /proc/$pid/stat instead
+    my $info = read_proc_pid_stat($pid);
+    return $info && (!$pstart || ($info->{starttime} eq $pstart)) && ($info->{status} ne 'Z') ? $info : undef;
+}
+
+sub read_proc_starttime {
+    my $pid = shift;
+
+    my $info = read_proc_pid_stat($pid);
+    return $info ? $info->{starttime} : 0;
 }
 
 sub read_meminfo {
@@ -187,7 +210,7 @@ sub read_memory_usage {
 
     my $line = PVE::Tools::file_read_firstline("/proc/$$/statm");
 
-    if ($line =~ m/^(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+/) {
+    if ($line =~ m/^(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s*/) {
        $res->{size} = $1*$ps;
        $res->{resident} = $2*$ps;
        $res->{shared} = $3*$ps;