[Pacemaker] SysInfo_mem_units()

Andrew Beekhof andrew at beekhof.net
Wed Jul 7 02:27:17 EDT 2010


On Wed, Jul 7, 2010 at 6:17 AM, Simon Horman <horms at verge.net.au> wrote:
> Hi,
>
> I am currently pondering the SysInfo_mem_units() function
> of sources/SysInfo (with a view to turning it into awk to
> avoid bashisms[1]).
>
> Basically the function seems to look at the unit of the input,
> leave it as if if the unit is G, divide it by 1024 if its M[bB]?
> or divide it by 1024^2 if the unit is k[B]?. It then does some rounding.
>
> On the unit handling side of things I am puzzled by the following code,
> particularly the inner if [ $mem != ${mem/./} ] portion. It seems to
> handle the x.yG case. But the logic seems to lead to the following result:
>
> xG => x
> x.yG => 1024x + w*y
>
> Surely the latter should be x.yG => x + (w*y/1024).
> Also, surely this logic also applies equally to other units.
>
>    if [ ${mem:$memlen:1} = "G" ]; then
>        mem="${mem:0:$memlen}"
>        if [ $mem != ${mem/./} ]; then
>            mem_before=${mem/.*/}
>            mem_after=${mem/*./}
>            mem=$[mem_before*1024]
>            if [ ${#mem_after} = 0 ]; then
>                :
>            elif [ ${#mem_after} = 1 ]; then
>                mem=$[mem+100*$mem_after]
>            elif [ ${#mem_after} = 2 ]; then
>                mem=$[mem+10*$mem_after]
>            elif [ ${#mem_after} = 3 ]; then
>                mem=$[mem+$mem_after]
>            else
>                mem_after=${mem_after:0:3}
>                mem=$[mem+$mem_after]
>            fi
>        fi
>    elif [ ${mem:$memlen:1} = "M" ]; then
>
>
> [1] I also struggle to care about this bashism crusade. But its

yeah, me too. but it also means that when people send patches I don't
care enough to argue against them.

>    something that Debian has decided matters, so I'm doing
>    this with my debian-ha-maintainer member hat on.
>
>    The following hunk illustrates how I think SysInfo_hdd_units() can
>    be handled. The awk version seems rather nicer or at least rather
>    shorter than the original.

agreed.  do you have a tree i can pull this from?
that or can you resend as a hg export so you can get the proper attribution :-)

> Index: stable-1.0.wip/extra/resources/SysInfo
> ===================================================================
> --- stable-1.0.wip.orig/extra/resources/SysInfo 2010-07-07 12:59:31.000000000 +0900
> +++ stable-1.0.wip/extra/resources/SysInfo      2010-07-07 13:15:32.000000000 +0900
> @@ -251,33 +247,13 @@ SysInfo_mem_units() {
>  }
>
>  SysInfo_hdd_units() {
> -    disk=$1
> -    disklen=`expr ${#disk} - 1`
> -    disklen_alt=`expr ${#disk} - 2`
> -    if [ ${disk:$disklen:1} = "G" ]; then
> -       disk="${disk:0:$disklen}"
> -    elif [ ${disk:$disklen:1} = "M" ]; then
> -       disk="${disk:0:$disklen}"
> -       disk=${disk/.*/}
> -       disk=`expr $disk / 1024`
> -    elif [ ${disk:$disklen:1} = "k" ]; then
> -       disk="${disk:0:$disklen}"
> -       disk=${disk/.*/}
> -       disk=`expr $disk / 1048576`
> -    elif [ ${disk:$disklen_alt:2} = "kB" ]; then
> -       disk="${disk:0:$disklen_alt}"
> -       disk=${disk/.*/}
> -       disk=`expr $disk / 1048576`
> -    elif [ ${disk:$disklen_alt:2} = "Mb" ]; then
> -       disk="${disk:0:$disklen_alt}"
> -       disk=${disk/.*/}
> -       disk=`expr $disk / 1024`
> -    elif [ ${disk:$disklen_alt:2} = "MB" ]; then
> -       disk="${disk:0:$disklen_alt}"
> -       disk=${disk/.*/}
> -       disk=`expr $disk / 1024`
> -    fi
> -    echo $disk
> +    # Size in gigabytes
> +    echo $1 | awk '{ split($0, a, /[^0-9]/);
> +                    n=a[1];
> +                    sub(n, "");
> +                    if (/^M[Bb]?$/) { n /= 1024 };
> +                    if (/^kB?/) { n /= 1048576 };
> +                    print n }'
>  }
>
>  SysInfo_usage() {
>
> _______________________________________________
> Pacemaker mailing list: Pacemaker at oss.clusterlabs.org
> http://oss.clusterlabs.org/mailman/listinfo/pacemaker
>
> Project Home: http://www.clusterlabs.org
> Getting started: http://www.clusterlabs.org/doc/Cluster_from_Scratch.pdf
> Bugs: http://developerbugs.linux-foundation.org/enter_bug.cgi?product=Pacemaker
>




More information about the Pacemaker mailing list