Make swap usage match figures reported by swapon -s (#742647)

GParted reports used figure of active swap space from 4 KiB upwards,
actually 1 page, where as 'swapon -s' reports figures from 0 upwards.
This is because GParted is counting the 1 page swap space overhead as
used space.

    # sfdisk -s /dev/sdb1
    262144
    # mkswap /dev/sdb1
    # swapon /dev/sdb1
    # swapon -s
    Filename                Type            Size    Used    Priority
    /dev/sdb1               partition       262140  0       -1

For this case GParted reports used as 4 KiB.
(ptn size - swap size = 262144 - 262140 = 4)

Instead make GParted report used figure without any overhead from 0
upwards, matching what 'swapon -s' reports.

Bug 742647 - Swap usage doesn't match figures reported by swapon -s
This commit is contained in:
Mike Fleetwood 2015-01-09 14:07:25 +00:00 committed by Curtis Gedak
parent d916cee115
commit 8b45219a2d
1 changed files with 8 additions and 4 deletions

View File

@ -74,7 +74,7 @@ void linux_swap::set_used_sectors( Partition & partition )
{
if ( partition .busy )
{
T = -1 ; N = -1 ;
N = -1;
std::string line ;
std::ifstream input( "/proc/swaps" ) ;
if ( input )
@ -85,7 +85,7 @@ void linux_swap::set_used_sectors( Partition & partition )
{
if ( line .substr( 0, path_len ) == path )
{
sscanf( line .substr( path_len ) .c_str(), " %*s %Ld %Ld", &T, &N ) ;
sscanf( line.substr( path_len ).c_str(), " %*s %*d %Ld", &N );
break ;
}
}
@ -95,9 +95,13 @@ void linux_swap::set_used_sectors( Partition & partition )
{
partition .messages .push_back( "open(\"/proc/swaps\", O_RDONLY): " + Glib::strerror( errno ) ) ;
}
if ( T > -1 && N > -1 )
if ( N > -1 )
{
T = Utils::round( T * ( KIBIBYTE / double(partition .sector_size) ) ) ;
// Ignore swap space reported size to ignore 1 page format
// overhead. Instead use partition size as sectors_fs_size so
// reported used figure for active swap space starts from 0
// upwards, matching what 'swapon -s' reports.
T = partition.get_sector_length();
N = Utils::round( N * ( KIBIBYTE / double(partition .sector_size) ) ) ;
partition .set_sector_usage( T, T - N ) ;
}