]> git.proxmox.com Git - pve-manager.git/blame - PVE/Service/pvestatd.pm
rebalance_lxc_containers: make it work with new lxc/<ID>/ns subgroup
[pve-manager.git] / PVE / Service / pvestatd.pm
CommitLineData
efd04666
DM
1package PVE::Service::pvestatd;
2
3use strict;
4use warnings;
5
6use PVE::SafeSyslog;
7use PVE::Daemon;
8
9use Time::HiRes qw (gettimeofday);
10use PVE::Tools qw(dir_glob_foreach file_read_firstline);
11use PVE::ProcFSTools;
41db757b 12use PVE::CpuSet;
efd04666
DM
13use Filesys::Df;
14use PVE::INotify;
15use PVE::Cluster qw(cfs_read_file);
16use PVE::Storage;
17use PVE::QemuServer;
18use PVE::LXC;
41db757b 19use PVE::LXC::Config;
efd04666
DM
20use PVE::RPCEnvironment;
21use PVE::API2::Subscription;
22use PVE::AutoBalloon;
23
24use PVE::Status::Plugin;
25use PVE::Status::Graphite;
58541b94 26use PVE::Status::InfluxDB;
efd04666
DM
27
28PVE::Status::Graphite->register();
58541b94 29PVE::Status::InfluxDB->register();
efd04666
DM
30PVE::Status::Plugin->init();
31
32use base qw(PVE::Daemon);
33
34my $opt_debug;
35my $restart_request;
36
37my $nodename = PVE::INotify::nodename();
38
39my $cmdline = [$0, @ARGV];
40
41my %daemon_options = (restart_on_error => 5, stop_wait_time => 5);
42my $daemon = __PACKAGE__->new('pvestatd', $cmdline, %daemon_options);
43
44sub init {
45 my ($self) = @_;
46
47 $opt_debug = $self->{debug};
48
49 PVE::Cluster::cfs_update();
50}
51
52sub shutdown {
53 my ($self) = @_;
54
55 syslog('info' , "server closing");
56
57 # wait for children
58 1 while (waitpid(-1, POSIX::WNOHANG()) > 0);
59
60 $self->exit_daemon(0);
61}
62
63sub hup {
64 my ($self) = @_;
65
66 $restart_request = 1;
67}
68
69sub update_node_status {
70 my ($status_cfg) = @_;
71
72 my ($avg1, $avg5, $avg15) = PVE::ProcFSTools::read_loadavg();
73
74 my $stat = PVE::ProcFSTools::read_proc_stat();
75
76 my $netdev = PVE::ProcFSTools::read_proc_net_dev();
77
78 my ($uptime) = PVE::ProcFSTools::read_proc_uptime();
79
80 my $cpuinfo = PVE::ProcFSTools::read_cpuinfo();
81
82 my $maxcpu = $cpuinfo->{cpus};
83
84 my $subinfo = PVE::INotify::read_file('subscription');
85 my $sublevel = $subinfo->{level} || '';
86
87 # traffic from/to physical interface cards
88 my $netin = 0;
89 my $netout = 0;
90 foreach my $dev (keys %$netdev) {
91 next if $dev !~ m/^eth\d+$/;
92 $netin += $netdev->{$dev}->{receive};
93 $netout += $netdev->{$dev}->{transmit};
94 }
95
96 my $meminfo = PVE::ProcFSTools::read_meminfo();
97
98 my $dinfo = df('/', 1); # output is bytes
99
100 my $ctime = time();
101
102 # everything not free is considered to be used
103 my $dused = $dinfo->{blocks} - $dinfo->{bfree};
104
105 my $data = "$uptime:$sublevel:$ctime:$avg1:$maxcpu:$stat->{cpu}:$stat->{wait}:" .
106 "$meminfo->{memtotal}:$meminfo->{memused}:" .
107 "$meminfo->{swaptotal}:$meminfo->{swapused}:" .
108 "$dinfo->{blocks}:$dused:$netin:$netout";
109
110 PVE::Cluster::broadcast_rrd("pve2-node/$nodename", $data);
111
112 foreach my $id (keys %{$status_cfg->{ids}}) {
113 my $plugin_config = $status_cfg->{ids}->{$id};
114 next if $plugin_config->{disable};
115 my $plugin = PVE::Status::Plugin->lookup($plugin_config->{type});
116
117 my $d = {};
118 $d->{uptime} = $uptime;
119 $d->{cpustat} = $stat;
120 $d->{cpustat}->{avg1} = $avg1;
121 $d->{cpustat}->{avg5} = $avg5;
122 $d->{cpustat}->{avg15} = $avg15;
123 $d->{cpustat}->{cpus} = $maxcpu;
124 $d->{memory} = $meminfo;
125 $d->{blockstat} = $dinfo;
126 $d->{nics} = $netdev;
127
128 $plugin->update_node_status($plugin_config, $nodename, $d, $ctime);
129 }
130}
131
132sub auto_balloning {
133 my ($vmstatus) = @_;
134
135 my $log = sub {
136 return if !$opt_debug;
137 print @_;
138 };
139
140 my $hostmeminfo = PVE::ProcFSTools::read_meminfo();
141
142 # to debug, run 'pvestatd -d' and set memtotal here
143 #$hostmeminfo->{memtotal} = int(2*1024*1024*1024/0.8); # you can set this to test
144
145 my $hostfreemem = $hostmeminfo->{memtotal} - $hostmeminfo->{memused};
146
147 # we try to use about 80% host memory
148 # goal: we want to change memory usage by this amount (positive or negative)
149 my $goal = int($hostmeminfo->{memtotal}*0.8 - $hostmeminfo->{memused});
150
151 my $maxchange = 100*1024*1024;
152 my $res = PVE::AutoBalloon::compute_alg1($vmstatus, $goal, $maxchange);
153
154 &$log("host goal: $goal free: $hostfreemem total: $hostmeminfo->{memtotal}\n");
155
156 foreach my $vmid (keys %$vmstatus) {
157 next if !$res->{$vmid};
158 my $d = $vmstatus->{$vmid};
159 my $diff = int($res->{$vmid} - $d->{balloon});
160 my $absdiff = $diff < 0 ? -$diff : $diff;
161 if ($absdiff > 0) {
162 &$log("BALLOON $vmid to $res->{$vmid} ($diff)\n");
163 eval {
164 PVE::QemuServer::vm_mon_cmd($vmid, "balloon",
165 value => int($res->{$vmid}));
166 };
167 warn $@ if $@;
168 }
169 }
170}
171
172sub update_qemu_status {
173 my ($status_cfg) = @_;
174
175 my $ctime = time();
176
177 my $vmstatus = PVE::QemuServer::vmstatus(undef, 1);
178
179 eval { auto_balloning($vmstatus); };
180 syslog('err', "auto ballooning error: $@") if $@;
181
182 foreach my $vmid (keys %$vmstatus) {
183 my $d = $vmstatus->{$vmid};
184 my $data;
185 my $status = $d->{qmpstatus} || $d->{status} || 'stopped';
186 my $template = $d->{template} ? $d->{template} : "0";
187 if ($d->{pid}) { # running
188 $data = "$d->{uptime}:$d->{name}:$status:$template:" .
189 "$ctime:$d->{cpus}:$d->{cpu}:" .
190 "$d->{maxmem}:$d->{mem}:" .
191 "$d->{maxdisk}:$d->{disk}:" .
192 "$d->{netin}:$d->{netout}:" .
193 "$d->{diskread}:$d->{diskwrite}";
194 } else {
195 $data = "0:$d->{name}:$status:$template:$ctime:$d->{cpus}::" .
196 "$d->{maxmem}::" .
197 "$d->{maxdisk}:$d->{disk}:" .
198 ":::";
199 }
200 PVE::Cluster::broadcast_rrd("pve2.3-vm/$vmid", $data);
201
202 foreach my $id (keys %{$status_cfg->{ids}}) {
203 my $plugin_config = $status_cfg->{ids}->{$id};
204 next if $plugin_config->{disable};
205 my $plugin = PVE::Status::Plugin->lookup($plugin_config->{type});
206 $plugin->update_qemu_status($plugin_config, $vmid, $d, $ctime);
207 }
208 }
209}
210
211sub remove_stale_lxc_consoles {
212
213 my $vmstatus = PVE::LXC::vmstatus();
214 my $pidhash = PVE::LXC::find_lxc_console_pids();
215
216 foreach my $vmid (keys %$pidhash) {
217 next if defined($vmstatus->{$vmid});
218 syslog('info', "remove stale lxc-console for CT $vmid");
219 foreach my $pid (@{$pidhash->{$vmid}}) {
220 kill(9, $pid);
221 }
222 }
223}
224
41db757b 225sub rebalance_lxc_containers {
41db757b
DM
226
227 return if !-d '/sys/fs/cgroup/cpuset/lxc'; # nothing to do...
228
229 my $all_cpus = PVE::CpuSet->new_from_cgroup('lxc', 'effective_cpus');
230 my @allowed_cpus = $all_cpus->members();
231 my $cpucount = scalar(@allowed_cpus);
ccfff920 232 my $max_cpuid = PVE::CpuSet::max_cpuid();
41db757b 233
ccfff920 234 my @cpu_ctcount = (0) x $max_cpuid;
41db757b
DM
235 my @balanced_cts;
236
e0dc09ad
DM
237 my $ctlist = PVE::LXC::config_list();
238
239 foreach my $vmid (sort keys %$ctlist) {
240 next if ! -d "/sys/fs/cgroup/cpuset/lxc/$vmid";
41db757b
DM
241
242 my ($conf, $cpuset);
243 eval {
244
245 $conf = PVE::LXC::Config->load_config($vmid);
246
247 $cpuset = PVE::CpuSet->new_from_cgroup("lxc/$vmid");
248 };
249 if (my $err = $@) {
250 warn $err;
251 next;
252 }
253
254 my @cpuset_members = $cpuset->members();
255
8b750abc 256 if (!PVE::LXC::Config->has_lxc_entry($conf, 'lxc.cgroup.cpuset.cpus')) {
2499255b 257
8b750abc
DM
258 my $cores = $conf->{cores} || $cpucount;
259 $cores = $cpucount if $cores > $cpucount;
41db757b 260
2499255b
DM
261 # see if the number of cores was hot-reduced or
262 # hasn't been enacted at all yet
263 my $newset = PVE::CpuSet->new();
264 if ($cores < scalar(@cpuset_members)) {
265 for (my $i = 0; $i < $cores; $i++) {
266 $newset->insert($cpuset_members[$i]);
267 }
268 } elsif ($cores > scalar(@cpuset_members)) {
269 my $count = $newset->insert(@cpuset_members);
270 foreach my $cpu (@allowed_cpus) {
271 $count += $newset->insert($cpu);
272 last if $count >= $cores;
273 }
274 } else {
275 $newset->insert(@cpuset_members);
276 }
07f9595f 277
2499255b
DM
278 # Apply hot-plugged changes if any:
279 if (!$newset->is_equal($cpuset)) {
280 @cpuset_members = $newset->members();
281 syslog('info', "detected changed cpu set for lxc/$vmid: " .
8b750abc 282 $newset->short_string());
2499255b
DM
283 $newset->write_to_cgroup("lxc/$vmid");
284 }
07f9595f 285
2499255b
DM
286 # Note: no need to rebalance if we already use all cores
287 push @balanced_cts, [$vmid, $cores, $newset]
8b750abc 288 if defined($conf->{cores}) && ($cores != $cpucount);
2499255b 289 }
07f9595f 290
2499255b 291 foreach my $cpu (@cpuset_members) {
ccfff920 292 $cpu_ctcount[$cpu]++ if $cpu <= $max_cpuid;
07f9595f 293 }
2499255b 294 }
07f9595f 295
2499255b
DM
296 my $find_best_cpu = sub {
297 my ($cpulist, $cpu) = @_;
07f9595f 298
2499255b
DM
299 my $cur_cost = $cpu_ctcount[$cpu];
300 my $cur_cpu = $cpu;
41db757b 301
2499255b
DM
302 foreach my $candidate (@$cpulist) {
303 my $cost = $cpu_ctcount[$candidate];
304 if ($cost < ($cur_cost -1)) {
305 $cur_cost = $cost;
306 $cur_cpu = $candidate;
307 }
07f9595f
DM
308 }
309
2499255b
DM
310 return $cur_cpu;
311 };
312
313 foreach my $bct (@balanced_cts) {
314 my ($vmid, $cores, $cpuset) = @$bct;
41db757b
DM
315
316 my $newset = PVE::CpuSet->new();
317
2499255b
DM
318 my $rest = [];
319 foreach my $cpu (@allowed_cpus) {
320 next if $cpuset->has($cpu);
321 push @$rest, $cpu;
322 }
323
324 my @members = $cpuset->members();
325 foreach my $cpu (@members) {
326 my $best = &$find_best_cpu($rest, $cpu);
327 if ($best != $cpu) {
328 $cpu_ctcount[$best]++;
329 $cpu_ctcount[$cpu]--;
330 }
331 $newset->insert($best);
41db757b
DM
332 }
333
334 if (!$newset->is_equal($cpuset)) {
2499255b 335 syslog('info', "modified cpu set for lxc/$vmid: " .
8b750abc 336 $newset->short_string());
193146f8
DM
337 eval {
338 # allow all, so that we can set new cpuset in /ns
339 $all_cpus->write_to_cgroup("lxc/$vmid");
340 eval {
341 $newset->write_to_cgroup("lxc/$vmid/ns");
342 };
343 if (my $err = $@) {
344 warn $err;
345 # restore original
346 $cpuset->write_to_cgroup("lxc/$vmid");
347 } else {
348 # also apply to container root cgroup
349 $newset->write_to_cgroup("lxc/$vmid");
350 }
351 };
2499255b 352 warn $@ if $@;
41db757b
DM
353 }
354 }
355}
356
efd04666
DM
357sub update_lxc_status {
358 my ($status_cfg) = @_;
359
360 my $ctime = time();
361
362 my $vmstatus = PVE::LXC::vmstatus();
363
364 foreach my $vmid (keys %$vmstatus) {
365 my $d = $vmstatus->{$vmid};
366 my $template = $d->{template} ? $d->{template} : "0";
367 my $data;
368 if ($d->{status} eq 'running') { # running
369 $data = "$d->{uptime}:$d->{name}:$d->{status}:$template:" .
370 "$ctime:$d->{cpus}:$d->{cpu}:" .
371 "$d->{maxmem}:$d->{mem}:" .
372 "$d->{maxdisk}:$d->{disk}:" .
373 "$d->{netin}:$d->{netout}:" .
374 "$d->{diskread}:$d->{diskwrite}";
375 } else {
376 $data = "0:$d->{name}:$d->{status}:$template:$ctime:$d->{cpus}::" .
377 "$d->{maxmem}::" .
378 "$d->{maxdisk}:$d->{disk}:" .
379 ":::";
380 }
381 PVE::Cluster::broadcast_rrd("pve2.3-vm/$vmid", $data);
382
383 foreach my $id (keys %{$status_cfg->{ids}}) {
384 my $plugin_config = $status_cfg->{ids}->{$id};
385 next if $plugin_config->{disable};
386 my $plugin = PVE::Status::Plugin->lookup($plugin_config->{type});
387 $plugin->update_lxc_status($plugin_config, $vmid, $d, $ctime);
388 }
389 }
390}
391
392sub update_storage_status {
393 my ($status_cfg) = @_;
394
bbcfdc08 395 my $cfg = PVE::Storage::config();
efd04666
DM
396
397 my $ctime = time();
398
399 my $info = PVE::Storage::storage_info($cfg);
400
401 foreach my $storeid (keys %$info) {
402 my $d = $info->{$storeid};
403 next if !$d->{active};
404
405 my $data = "$ctime:$d->{total}:$d->{used}";
406
407 my $key = "pve2-storage/${nodename}/$storeid";
408 PVE::Cluster::broadcast_rrd($key, $data);
409
410 foreach my $id (keys %{$status_cfg->{ids}}) {
411 my $plugin_config = $status_cfg->{ids}->{$id};
412 next if $plugin_config->{disable};
413 my $plugin = PVE::Status::Plugin->lookup($plugin_config->{type});
414 $plugin->update_storage_status($plugin_config, $nodename, $storeid, $d, $ctime);
415 }
416 }
417}
418
419sub update_status {
420
421 # update worker list. This is not really required and
422 # we just call this to make sure that we have a correct
423 # list in case of an unexpected crash.
424 eval {
425 my $tlist = PVE::RPCEnvironment::active_workers();
426 PVE::Cluster::broadcast_tasklist($tlist);
427 };
428 my $err = $@;
429 syslog('err', $err) if $err;
430
431 my $status_cfg = PVE::Cluster::cfs_read_file('status.cfg');
432
433 eval {
434 update_node_status($status_cfg);
435 };
436 $err = $@;
437 syslog('err', "node status update error: $err") if $err;
438
439 eval {
440 update_qemu_status($status_cfg);
441 };
442 $err = $@;
443 syslog('err', "qemu status update error: $err") if $err;
444
445 eval {
446 update_lxc_status($status_cfg);
447 };
448 $err = $@;
449 syslog('err', "lxc status update error: $err") if $err;
450
e0dc09ad
DM
451 eval {
452 rebalance_lxc_containers();
453 };
454 $err = $@;
455 syslog('err', "lxc cpuset rebalance error: $err") if $err;
456
efd04666
DM
457 eval {
458 update_storage_status($status_cfg);
459 };
460 $err = $@;
461 syslog('err', "storage status update error: $err") if $err;
462
463 eval {
464 remove_stale_lxc_consoles();
465 };
466 $err = $@;
467 syslog('err', "lxc console cleanup error: $err") if $err;
468}
469
470my $next_update = 0;
471
472# do not update directly after startup, because install scripts
473# have a problem with that
474my $cycle = 0;
475my $updatetime = 10;
476
477my $initial_memory_usage;
478
479sub run {
480 my ($self) = @_;
481
482 for (;;) { # forever
483
484 $next_update = time() + $updatetime;
485
486 if ($cycle) {
487 my ($ccsec, $cusec) = gettimeofday ();
488 eval {
489 # syslog('info', "start status update");
490 PVE::Cluster::cfs_update();
491 update_status();
492 };
493 my $err = $@;
494
495 if ($err) {
496 syslog('err', "status update error: $err");
497 }
498
499 my ($ccsec_end, $cusec_end) = gettimeofday ();
500 my $cptime = ($ccsec_end-$ccsec) + ($cusec_end - $cusec)/1000000;
501
502 syslog('info', sprintf("status update time (%.3f seconds)", $cptime))
503 if ($cptime > 5);
504 }
505
506 $cycle++;
507
508 my $mem = PVE::ProcFSTools::read_memory_usage();
509
510 if (!defined($initial_memory_usage) || ($cycle < 10)) {
511 $initial_memory_usage = $mem->{resident};
512 } else {
513 my $diff = $mem->{resident} - $initial_memory_usage;
514 if ($diff > 5*1024*1024) {
515 syslog ('info', "restarting server after $cycle cycles to " .
516 "reduce memory usage (free $mem->{resident} ($diff) bytes)");
517 $self->restart_daemon();
518 }
519 }
520
521 my $wcount = 0;
522 while ((time() < $next_update) &&
523 ($wcount < $updatetime) && # protect against time wrap
524 !$restart_request) { $wcount++; sleep (1); };
525
526 $self->restart_daemon() if $restart_request;
527 }
528}
529
530$daemon->register_start_command();
531$daemon->register_restart_command(1);
532$daemon->register_stop_command();
533$daemon->register_status_command();
534
535our $cmddef = {
536 start => [ __PACKAGE__, 'start', []],
537 restart => [ __PACKAGE__, 'restart', []],
538 stop => [ __PACKAGE__, 'stop', []],
539 status => [ __PACKAGE__, 'status', [], undef, sub { print shift . "\n";} ],
540};
541
542#my $cmd = shift;
543#PVE::CLIHandler::handle_cmd($cmddef, $0, $cmd, \@ARGV, undef, $0);
544#exit (0);
545
5461;
547
548__END__
549
550=head1 NAME
551
552pvestatd - PVE Status Daemon
553
554=head1 SYNOPSIS
555
556=include synopsis
557
558=head1 DESCRIPTION
559
560This daemom queries the status of VMs, storages and containers at
561regular intervals. The result is sent to all nodes in the cluster.
562
563=include pve_copyright
564
565
566
567
568