Skip to content

Commit

Permalink
wip: enable configure startup RAM check
Browse files Browse the repository at this point in the history
  • Loading branch information
frankiejol committed Apr 15, 2024
1 parent 690e355 commit 2462e64
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/Ravada.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2713,6 +2713,17 @@ sub _sql_insert_defaults($self){
,name => 'time'
,value => '21:00'
}
,{
id_parent => '/backend'
,name => 'limits'
,value => undef
}
,{
id_parent => '/backend/limits'
,name => 'startup_ram'
,value => 1
}


]
);
Expand Down
4 changes: 4 additions & 0 deletions lib/Ravada/Domain.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,8 @@ sub _check_has_clones {
sub _check_free_vm_memory {
my $self = shift;

return if !Ravada::Front::setting(undef,"/backend/limits/startup_ram");

my $vm_free_mem = $self->_vm->free_memory;

my $domain_memory = $self->info(Ravada::Utils::user_daemon)->{memory};
Expand Down Expand Up @@ -2967,6 +2969,7 @@ sub _copy_clone($self, %args) {
$id_owner = $user->id if (! $id_owner);
my $alias = delete $args{alias};
my $options = delete $args{options};
my $start = delete $args{start};

confess "ERROR: Unknown arguments ".join(",",sort keys %args)
if keys %args;
Expand All @@ -2978,6 +2981,7 @@ sub _copy_clone($self, %args) {
push @copy_arg, ( memory => $memory ) if $memory;
push @copy_arg, ( volatile => $volatile ) if $volatile;
push @copy_arg, ( options => $options ) if $options;
push @copy_arg, ( start => $start ) if $start;

$request->status("working","Copying domain ".$self->name
." to $name") if $request;
Expand Down
157 changes: 157 additions & 0 deletions t/mojo/80_check_resources.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
use warnings;
use strict;

use Carp qw(confess);
use Data::Dumper;
use Test::More;
use Test::Mojo;
use Mojo::File 'path';
use Mojo::JSON qw(decode_json);

use lib 't/lib';
use Test::Ravada;

no warnings "experimental::signatures";
use feature qw(signatures);

$ENV{MOJO_MODE} = 'development';
my $SCRIPT = path(__FILE__)->dirname->sibling('../script/rvd_front');

my ($USERNAME, $PASSWORD);

my $URL_LOGOUT = '/logout';

$Test::Ravada::BACKGROUND=1;
my $t;

my $BASE_NAME="zz-test-base-ubuntu";
my $BASE;
#########################################################

sub _import_base($vm_name) {
mojo_login($t,$USERNAME, $PASSWORD);
my $name = new_domain_name()."-".$vm_name."-$$";
if ($vm_name eq 'KVM') {
my $base0 = rvd_front->search_domain($BASE_NAME);
mojo_request_url_post($t,"/machine/copy",{id_base => $base0->id, new_name => $name, copy_ram => 0.128, copy_number => 1});
for ( 1 .. 90 ) {
$BASE= rvd_front->search_domain($name);
last if $BASE;
wait_request();
}

} else {
$BASE = mojo_create_domain($t, $vm_name);
}

Ravada::Request->shutdown_domain(uid => user_admin->id
,id_domain => $BASE->id);
my $req = Ravada::Request->prepare_base(uid => user_admin->id
,id_domain => $BASE->id
);
wait_request();
is($req->error,'');

$BASE->_data('shutdown_disconnected' => 1);

}

sub _remove_clones($time) {
Ravada::Request->remove_clones(
uid => user_admin->id
,id_domain => $BASE->id
,at => $time
);
}

sub _free_memory() {
open my $mem,"<","/proc/meminfo" or die $!;
my $mem_avail;
while (my $line = <$mem> ) {
($mem_avail) = $line =~ /^MemAvailable.*?(\d+)/;
return $mem_avail if $mem_avail;
}
die;
}

sub test_ram($vm_name,$enable_check, $expected=undef) {

my $free_mem = _free_memory();
my $limit = int($free_mem/1024/1024)+1 ;
_remove_clones(time+300+$limit*2);
my $count = 0;
for my $n ( 0 .. $limit*3 ) {
my $free = int(_free_memory()/1024/1024);
my $name = new_domain_name();
my $req=Ravada::Request->clone(
uid => user_admin->id
,id_domain => $BASE->id
,name => $name
,memory => 3 * 1024 * 1024
);
my $new;
for ( 1 .. 90 ) {
$new = rvd_front->search_domain($name);
last if $new;
wait_request();
}
last if !$new;
$req = Ravada::Request->start_domain( uid => user_admin->id
,id_domain => $new->id
);
for ( 1 .. 10 ) {
wait_request();
last if $req->status eq 'done';
}
if ($req->error) {
diag($req->error);
last;
}
$count++;
last if defined $expected && $count > $expected;
my $free2 = int(_free_memory()/1024/1024);
redo if $vm_name eq 'KVM' && ($free2>=$free);

}
_remove_clones(0);
wait_request();
return $count;
}

#########################################################
$ENV{MOJO_MODE} = 'development';
init('/etc/ravada.conf',0);
my $connector = rvd_back->connector;
like($connector->{driver} , qr/mysql/i) or BAIL_OUT;

if (!ping_backend()) {
diag("SKIPPED: no backend");
done_testing();
exit;
}
$Test::Ravada::BACKGROUND=1;

$t = Test::Mojo->new($SCRIPT);
$t->ua->inactivity_timeout(900);
$t->ua->connect_timeout(60);

remove_old_domains_req();

$USERNAME = user_admin->name;
$PASSWORD = "$$ $$";
for my $vm_name (reverse @{rvd_front->list_vm_types} ) {
diag("Testing RAM limit in $vm_name");

_import_base($vm_name);

rvd_back->setting("/backend/limits/startup_ram" => 1);
my $started_limit =test_ram($vm_name,1);
rvd_back->setting("/backend/limits/startup_ram" => 0);
my $started_no_limit =test_ram($vm_name,0, $started_limit);
ok($started_no_limit > $started_limit);
}

remove_old_domains_req(0); # 0=do not wait for them

end();
done_testing();

0 comments on commit 2462e64

Please sign in to comment.