diff --git a/src/niceload b/src/niceload index feeb07b3..651ce0d0 100755 --- a/src/niceload +++ b/src/niceload @@ -602,6 +602,7 @@ sub nonblockGetLines { sub read_sensor { my $self = shift; + ::debug("read_sensor"); my $fh = $self->{'sensor_fh'}; if(not $fh) { # Start the sensor @@ -612,13 +613,18 @@ sub read_sensor { my ($eof,@lines) = nonblockGetLines($fh); # new load = last full line - foreach my $line ( @lines ) { + foreach my $line (@lines) { if(defined $line) { - print "Pipe saw: $eof [$line]\n"; + ::debug("Pipe saw: $eof [$line]\n"); $Global::last_sensor_reading = $line; } } - if($eof) { undef($self->{'sensor_fh'}); } + if($eof) { + # End of file => Restart the sensor + close $fh; + open($fh, "-|", $::opt_sensor) || ::die_bug("Cannot open: $::opt_sensor"); + $self->{'sensor_fh'} = $fh; + } return $Global::last_sensor_reading; } @@ -627,6 +633,7 @@ sub load_status { # Returns: # loadavg or sensor measurement my $self = shift; + if($::opt_sensor) { if(not defined $self->{'load_status'} or $self->{'load_status_cache_time'} + $self->{'recheck'} < time) { @@ -635,7 +642,7 @@ sub load_status { sleep 1; $self->{'load_status'} = $self->read_sensor(); } - $self->{'load_status_cache_time'} = time; + $self->{'load_status_cache_time'} = time - 0.001; } } else { # Normal load avg diff --git a/testsuite/tests-to-run/niceload02.sh b/testsuite/tests-to-run/niceload02.sh index 311e17ec..d0a1e5c0 100755 --- a/testsuite/tests-to-run/niceload02.sh +++ b/testsuite/tests-to-run/niceload02.sh @@ -9,5 +9,11 @@ while uptime | grep -v age:.[1-9][0-9].[0-9][0-9] >/dev/null ; do (timeout 5 nic echo '### Test -p' perl -e '$|=1;while($t++<3){sleep(1);print "."}' & -# The above should be suspended for at least 4 seconds -stdout /usr/bin/time -f %e niceload -D -l 9 -p $! | perl -ne '$_ > 6 and print "OK\n"' +# The above will normally take 3.6 sec +# It should be suspended so it at least takes 5 seconds +stdout /usr/bin/time -f %e niceload -l 8 -p $! | perl -ne '$_ >= 5 and print "OK\n"' + +echo "### Test --sensor -l negative" +timeout 10 nice nice dd iflag=fullblock if=/dev/zero of=/dev/null bs=10G & +niceload -t 1 --sensor 'free | field 3 | head -3|tail -1' -l -5000000 "free -g;echo more than 5 GB used" + diff --git a/testsuite/wanted-results/niceload02 b/testsuite/wanted-results/niceload02 index 9df911e4..869193e5 100644 --- a/testsuite/wanted-results/niceload02 +++ b/testsuite/wanted-results/niceload02 @@ -3,3 +3,9 @@ 0 eq 0 ### Test -p ...OK +### Test --sensor -l negative + total used free shared buffers cached +Mem: 7 6 1 0 0 0 +-/+ buffers/cache: 6 1 +Swap: 9 2 7 +more than 5 GB used