]> git.proxmox.com Git - pve-common.git/blob - src/PVE/ProcFSTools.pm
read_proc_stat: substract guest && guest_nice from user && nice time
[pve-common.git] / src / PVE / ProcFSTools.pm
1 package PVE::ProcFSTools;
2
3 use strict;
4 use warnings;
5 use POSIX;
6 use Time::HiRes qw (gettimeofday);
7 use IO::File;
8 use List::Util qw(sum);
9 use PVE::Tools;
10 use Cwd qw();
11
12 use Socket qw(PF_INET PF_INET6 SOCK_DGRAM IPPROTO_IP);
13
14 use constant IFF_UP => 1;
15 use constant IFNAMSIZ => 16;
16 use constant SIOCGIFFLAGS => 0x8913;
17
18 my $clock_ticks = POSIX::sysconf(&POSIX::_SC_CLK_TCK);
19
20 my $cpuinfo;
21
22 sub read_cpuinfo {
23 my $fn = '/proc/cpuinfo';
24
25 return $cpuinfo if $cpuinfo;
26
27 my $res = {
28 user_hz => $clock_ticks,
29 model => 'unknown',
30 mhz => 0,
31 cpus => 1,
32 sockets => 1,
33 flags => '',
34 };
35
36 my $fh = IO::File->new ($fn, "r");
37 return $res if !$fh;
38
39 my $cpuid = 0;
40 my $idhash = {};
41 my $count = 0;
42 while (defined(my $line = <$fh>)) {
43 if ($line =~ m/^processor\s*:\s*\d+\s*$/i) {
44 $count++;
45 } elsif ($line =~ m/^model\s+name\s*:\s*(.*)\s*$/i) {
46 $res->{model} = $1 if $res->{model} eq 'unknown';
47 } elsif ($line =~ m/^cpu\s+MHz\s*:\s*(\d+\.\d+)\s*$/i) {
48 $res->{mhz} = $1 if !$res->{mhz};
49 } elsif ($line =~ m/^flags\s*:\s*(.*)$/) {
50 $res->{flags} = $1 if !length $res->{flags};
51 } elsif ($line =~ m/^physical id\s*:\s*(\d+)\s*$/i) {
52 $cpuid = $1;
53 $idhash->{$1} = 1 if not defined($idhash->{$1});
54 } elsif ($line =~ m/^cpu cores\s*:\s*(\d+)\s*$/i) {
55 $idhash->{$cpuid} = $1 if defined($idhash->{$cpuid});
56 }
57 }
58
59 # Hardware Virtual Machine (Intel VT / AMD-V)
60 $res->{hvm} = $res->{flags} =~ m/\s(vmx|svm)\s/;
61
62 $res->{sockets} = scalar(keys %$idhash) || 1;
63
64 $res->{cores} = sum(values %$idhash) || 1;
65
66 $res->{cpus} = $count;
67
68 $fh->close;
69
70 $cpuinfo = $res;
71
72 return $res;
73 }
74
75 sub read_proc_uptime {
76 my $ticks = shift;
77
78 my $line = PVE::Tools::file_read_firstline("/proc/uptime");
79 if ($line && $line =~ m|^(\d+\.\d+)\s+(\d+\.\d+)\s*$|) {
80 if ($ticks) {
81 return (int($1*$clock_ticks), int($2*$clock_ticks));
82 } else {
83 return (int($1), int($2));
84 }
85 }
86
87 return (0, 0);
88 }
89
90 sub kernel_version {
91 my $line = PVE::Tools::file_read_firstline("/proc/version");
92
93 if ($line && $line =~ m|^Linux\sversion\s((\d+(?:\.\d+)+)-?(\S+)?)|) {
94 my ($fullversion, $version_numbers, $extra) = ($1, $2, $3);
95
96 # variable names are the one from the Linux kernel Makefile
97 my ($version, $patchlevel, $sublevel) = split(/\./, $version_numbers);
98
99 return wantarray
100 ? (int($version), int($patchlevel), int($sublevel), $extra, $fullversion)
101 : $fullversion;
102 }
103
104 return (0, 0, 0, '', '');
105 }
106
107 # Check if the kernel is at least $major.$minor. Return either just a boolean,
108 # or a boolean and the kernel version's major.minor string from /proc/version
109 sub check_kernel_release {
110 my ($major, $minor) = @_;
111
112 my ($k_major, $k_minor) = kernel_version();
113
114 my $ok;
115 if (defined($minor)) {
116 $ok = $k_major > $major || ($k_major == $major && $k_minor >= $minor);
117 } else {
118 $ok = $k_major >= $major;
119 }
120
121 return wantarray ? ($ok, "$k_major.$k_minor") : $ok;
122 }
123
124 sub read_loadavg {
125
126 my $line = PVE::Tools::file_read_firstline('/proc/loadavg');
127
128 if ($line =~ m|^(\d+\.\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)\s+\d+/\d+\s+\d+\s*$|) {
129 return wantarray ? ($1, $2, $3) : $1;
130 }
131
132 return wantarray ? (0, 0, 0) : 0;
133 }
134
135 sub parse_pressure {
136 my ($path) = @_;
137
138 my $res = {};
139 my $v = qr/\d+\.\d+/;
140 my $fh = IO::File->new($path, "r") or return undef;
141 while (defined (my $line = <$fh>)) {
142 if ($line =~ /^(some|full)\s+avg10\=($v)\s+avg60\=($v)\s+avg300\=($v)\s+total\=(\d+)/) {
143 $res->{$1}->{avg10} = $2;
144 $res->{$1}->{avg60} = $3;
145 $res->{$1}->{avg300} = $4;
146 $res->{$1}->{total} = $4;
147 }
148 }
149 $fh->close;
150 return $res;
151 }
152
153 sub read_pressure {
154 my $res = {};
155 foreach my $type (qw(cpu memory io)) {
156 my $stats = parse_pressure("/proc/pressure/$type");
157 $res->{$type} = $stats if $stats;
158 }
159 return $res;
160 }
161
162 my $last_proc_stat;
163
164 sub read_proc_stat {
165 my $res = { user => 0, nice => 0, system => 0, idle => 0 , iowait => 0, irq => 0, softirq => 0, steal => 0, guest => 0, guest_nice => 0, sum => 0};
166
167 my $cpucount = 0;
168
169 if (my $fh = IO::File->new ("/proc/stat", "r")) {
170 while (defined (my $line = <$fh>)) {
171 if ($line =~ m|^cpu\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)|) {
172 $res->{user} = $1 - $9;
173 $res->{nice} = $2 - $10;
174 $res->{system} = $3;
175 $res->{idle} = $4;
176 $res->{used} = $1+$2+$3;
177 $res->{iowait} = $5;
178 $res->{irq} = $6;
179 $res->{softirq} = $7;
180 $res->{steal} = $8;
181 $res->{guest} = $9;
182 $res->{guest_nice} = $10;
183 } elsif ($line =~ m|^cpu\d+\s|) {
184 $cpucount++;
185 }
186 }
187 $fh->close;
188 }
189
190 $cpucount = 1 if !$cpucount;
191
192 my $ctime = gettimeofday; # floating point time in seconds
193
194 # the sum of all fields
195 $res->{total} = $res->{user}
196 + $res->{nice}
197 + $res->{system}
198 + $res->{iowait}
199 + $res->{irq}
200 + $res->{softirq}
201 + $res->{steal}
202 + $res->{idle}
203 + $res->{guest}
204 + $res->{guest_nice};
205
206 $res->{ctime} = $ctime;
207 $res->{cpu} = 0;
208 $res->{wait} = 0;
209
210 $last_proc_stat = $res if !$last_proc_stat;
211
212 my $diff = ($ctime - $last_proc_stat->{ctime}) * $clock_ticks * $cpucount;
213
214 if ($diff > 1000) { # don't update too often
215 my $useddiff = $res->{used} - $last_proc_stat->{used};
216 $useddiff = $diff if $useddiff > $diff;
217 $res->{cpu} = $useddiff/$diff;
218
219 my $waitdiff = $res->{iowait} - $last_proc_stat->{iowait};
220 $waitdiff = $diff if $waitdiff > $diff;
221 $res->{wait} = $waitdiff/$diff;
222
223 $last_proc_stat = $res;
224 } else {
225 $res->{cpu} = $last_proc_stat->{cpu};
226 $res->{wait} = $last_proc_stat->{wait};
227 }
228
229 return $res;
230 }
231
232 sub read_proc_pid_stat {
233 my $pid = shift;
234
235 my $statstr = PVE::Tools::file_read_firstline("/proc/$pid/stat");
236
237 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+/) {
238 return {
239 status => $1,
240 ppid => $2,
241 utime => $3,
242 stime => $4,
243 starttime => $7,
244 vsize => $8,
245 rss => $9 * 4096,
246 };
247 }
248
249 return undef;
250 }
251
252 sub check_process_running {
253 my ($pid, $pstart) = @_;
254
255 # note: waitpid only work for child processes, but not
256 # for processes spanned by other processes.
257 # kill(0, pid) return succes for zombies.
258 # So we read the status form /proc/$pid/stat instead
259
260 my $info = read_proc_pid_stat($pid);
261
262 return $info && (!$pstart || ($info->{starttime} eq $pstart)) && ($info->{status} ne 'Z') ? $info : undef;
263 }
264
265 sub read_proc_starttime {
266 my $pid = shift;
267
268 my $info = read_proc_pid_stat($pid);
269 return $info ? $info->{starttime} : 0;
270 }
271
272 sub read_meminfo {
273
274 my $res = {
275 memtotal => 0,
276 memfree => 0,
277 memused => 0,
278 memshared => 0,
279 swaptotal => 0,
280 swapfree => 0,
281 swapused => 0,
282 };
283
284 my $fh = IO::File->new ("/proc/meminfo", "r");
285 return $res if !$fh;
286
287 my $d = {};
288 while (my $line = <$fh>) {
289 if ($line =~ m/^(\S+):\s+(\d+)\s*kB/i) {
290 $d->{lc ($1)} = $2 * 1024;
291 }
292 }
293 close($fh);
294
295 $res->{memtotal} = $d->{memtotal};
296 $res->{memfree} = $d->{memfree} + $d->{buffers} + $d->{cached};
297 $res->{memused} = $res->{memtotal} - $res->{memfree};
298
299 $res->{swaptotal} = $d->{swaptotal};
300 $res->{swapfree} = $d->{swapfree};
301 $res->{swapused} = $res->{swaptotal} - $res->{swapfree};
302
303 my $spages = PVE::Tools::file_read_firstline("/sys/kernel/mm/ksm/pages_sharing") // 0 ;
304 $res->{memshared} = int($spages) * 4096;
305
306 return $res;
307 }
308
309 # memory usage of current process
310 sub read_memory_usage {
311
312 my $res = { size => 0, resident => 0, shared => 0 };
313
314 my $ps = 4096;
315
316 my $line = PVE::Tools::file_read_firstline("/proc/$$/statm");
317
318 if ($line =~ m/^(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s*/) {
319 $res->{size} = $1*$ps;
320 $res->{resident} = $2*$ps;
321 $res->{shared} = $3*$ps;
322 }
323
324 return $res;
325 }
326
327 sub read_proc_net_dev {
328
329 my $res = {};
330
331 my $fh = IO::File->new ("/proc/net/dev", "r");
332 return $res if !$fh;
333
334 while (defined (my $line = <$fh>)) {
335 if ($line =~ m/^\s*(.*):\s*(\d+)\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+(\d+)\s+/) {
336 $res->{$1} = {
337 receive => $2,
338 transmit => $3,
339 };
340 }
341 }
342
343 close($fh);
344
345 return $res;
346 }
347
348 sub write_proc_entry {
349 my ($filename, $data) = @_;#
350
351 my $fh = IO::File->new($filename, O_WRONLY);
352 die "unable to open file '$filename' - $!\n" if !$fh;
353 die "unable to write '$filename' - $!\n" unless print $fh $data;
354 die "closing file '$filename' failed - $!\n" unless close $fh;
355 $fh->close();
356 }
357
358 sub read_proc_net_route {
359 my $filename = "/proc/net/route";
360
361 my $res = [];
362
363 my $fh = IO::File->new ($filename, "r");
364 return $res if !$fh;
365
366 my $int_to_quad = sub {
367 return join '.' => map { ($_[0] >> 8*(3-$_)) % 256 } (3, 2, 1, 0);
368 };
369
370 while (defined(my $line = <$fh>)) {
371 next if $line =~/^Iface\s+Destination/; # skip head
372 my ($iface, $dest, $gateway, $metric, $mask, $mtu) = (split(/\s+/, $line))[0,1,2,6,7,8];
373 push @$res, {
374 dest => &$int_to_quad(hex($dest)),
375 gateway => &$int_to_quad(hex($gateway)),
376 mask => &$int_to_quad(hex($mask)),
377 metric => $metric,
378 mtu => $mtu,
379 iface => $iface,
380 };
381 }
382
383 return $res;
384 }
385
386 sub read_proc_mounts {
387 return PVE::Tools::file_get_contents("/proc/mounts", 512*1024);
388 }
389
390 # mounts encode spaces (\040), tabs (\011), newlines (\012), backslashes (\\ or \134)
391 sub decode_mount {
392 my ($str) = @_;
393 return $str =~ s/\\(?:040|01[12]|134|\\)/"\"$&\""/geer;
394 }
395
396 sub parse_mounts {
397 my ($mounts) = @_;
398
399 my $mntent = [];
400 while ($mounts =~ /^\s*([^#].*)$/gm) {
401 # lines from the file are encoded so we can just split at spaces
402 my ($what, $dir, $fstype, $opts) = split(/[ \t]/, $1, 4);
403 my ($freq, $passno) = (0, 0);
404 # in glibc's parser frequency and pass seem to be optional
405 $freq = $1 if $opts =~ s/\s+(\d+)$//;
406 $passno = $1 if $opts =~ s/\s+(\d+)$//;
407 push @$mntent, [
408 decode_mount($what),
409 decode_mount($dir),
410 decode_mount($fstype),
411 decode_mount($opts),
412 $freq,
413 $passno,
414 ];
415 }
416 return $mntent;
417 }
418
419 sub parse_proc_mounts {
420 return parse_mounts(read_proc_mounts());
421 }
422
423 sub is_mounted {
424 my ($mountpoint) = @_;
425
426 $mountpoint = Cwd::realpath($mountpoint);
427
428 return 0 if !defined($mountpoint); # path does not exist
429
430 my $mounts = parse_proc_mounts();
431 return (grep { $_->[1] eq $mountpoint } @$mounts) ? 1 : 0;
432 }
433
434 sub read_proc_net_ipv6_route {
435 my $filename = "/proc/net/ipv6_route";
436
437 my $res = [];
438
439 my $fh = IO::File->new ($filename, "r");
440 return $res if !$fh;
441
442 my $read_v6addr = sub { $_[0] =~ s/....(?!$)/$&:/gr };
443
444 # ipv6_route has no header
445 while (defined(my $line = <$fh>)) {
446 my ($dest, $prefix, $nexthop, $metric, $iface) = (split(/\s+/, $line))[0,1,4,5,9];
447 push @$res, {
448 dest => &$read_v6addr($dest),
449 prefix => hex("$prefix"),
450 gateway => &$read_v6addr($nexthop),
451 metric => hex("$metric"),
452 iface => $iface
453 };
454 }
455
456 return $res;
457 }
458
459 sub upid_wait {
460 my ($upid, $waitfunc, $sleep_intervall) = @_;
461
462 my $task = PVE::Tools::upid_decode($upid);
463
464 $sleep_intervall = $sleep_intervall ? $sleep_intervall : 1;
465
466 my $next_time = time + $sleep_intervall;
467
468 while (check_process_running($task->{pid}, $task->{pstart})) {
469
470 if (time >= $next_time && $waitfunc && ref($waitfunc) eq 'CODE'){
471 &$waitfunc($task);
472 $next_time = time + $sleep_intervall;
473 }
474
475 CORE::sleep(1);
476 }
477 }
478
479 # struct ifreq { // FOR SIOCGIFFLAGS:
480 # char ifrn_name[IFNAMSIZ]
481 # short ifru_flags
482 # };
483 my $STRUCT_IFREQ_SIOCGIFFLAGS = 'Z' . IFNAMSIZ . 's1';
484 sub get_active_network_interfaces {
485 # Use the interface name list from /proc/net/dev
486 open my $fh, '<', '/proc/net/dev'
487 or die "failed to open /proc/net/dev: $!\n";
488 # And filter by IFF_UP flag fetched via a PF_INET6 socket ioctl:
489 my $sock;
490 socket($sock, PF_INET6, SOCK_DGRAM, &IPPROTO_IP)
491 or socket($sock, PF_INET, SOCK_DGRAM, &IPPROTO_IP)
492 or return [];
493
494 my $ifaces = [];
495 while(defined(my $line = <$fh>)) {
496 next if $line !~ /^\s*([^:\s]+):/;
497 my $ifname = $1;
498 my $ifreq = pack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifname, 0);
499 if (!defined(ioctl($sock, SIOCGIFFLAGS, $ifreq))) {
500 warn "failed to get interface flags for: $ifname\n";
501 next;
502 }
503 my ($name, $flags) = unpack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifreq);
504 push @$ifaces, $ifname if ($flags & IFF_UP);
505 }
506 close $fh;
507 close $sock;
508 return $ifaces;
509 }
510
511 1;