[Pacemaker] SysInfo_mem_units()

Simon Horman horms at verge.net.au
Wed Jul 7 04:17:04 UTC 2010


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
    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.

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() {



More information about the Pacemaker mailing list