maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   General (https://talk.maemo.org/forumdisplay.php?f=7)
-   -   [Help] [Solution Found] Script on N900 not calculating correctly. (https://talk.maemo.org/showthread.php?t=88121)

sixwheeledbeast 2012-12-04 18:50

[Help] [Solution Found] Script on N900 not calculating correctly.
 
1 Attachment(s)
I am having a issue with a script I am trying to write and I just can't work out why it's not working.

The issue is that on my N900 the multiply command on line 47 doesn't multiply correctly.
It however seems to work fine on my Ubuntu desktop which is puzzling me.
I have tried multiple methods to calculate but it just isn't correct.
I am trying to multiply a value by 100 (to get percent) and you can clearly tell that it isn't doing so.

The script is to calculate swap de-fragmentation without needing iostat

Any help will be greatly appreciated. Thanks

juiceme 2012-12-04 19:23

Re: [Help] Require help with script on N900.
 
quickly trying this in N9 at least it works...

~ #
~ # oneh=100 ; getcurrent=123 ; timeshundred=$(($getcurrent*$oneh)) ; echo $timeshundred
12300
~ #

So it seems that N900 has different shell... What does it give as an answer there?

thedead1440 2012-12-04 19:29

Re: [Help] Require help with script on N900.
 
Quote:

Originally Posted by juiceme (Post 1301402)
quickly trying this in N9 at least it works...

~ #
~ # oneh=100 ; getcurrent=123 ; timeshundred=$(($getcurrent*$oneh)) ; echo $timeshundred
12300
~ #

So it seems that N900 has different shell... What does it give as an answer there?

works on the n900 too:
Code:

/home/user # oneh=100 ; getcurrent=123 ;timeshundred=$(($get
current*$oneh)) ; echo $timeshundred
12300
/home/user #


juiceme 2012-12-04 19:49

Re: [Help] Require help with script on N900.
 
Okaay, then there is no other possibility than for some reason $getcurrent does not evaluate to a number.

mr_pingu 2012-12-04 19:53

Re: [Help] Require help with script on N900.
 
Did you try the same script in bash? There are some minor differences between ash and bash ;)

Try to run the script from bash, need to have bash3 (packagename) installed though.

trompkins 2012-12-04 20:53

Re: [Help] Require help with script on N900.
 
No idea why it wouldn't work. Maybe try replacing timeshundred formula with $(expr $oneh \* $getcurrent) ?

sixwheeledbeast 2012-12-04 21:22

Re: [Help] Require help with script on N900.
 
Quote:

Originally Posted by thedead1440 (Post 1301403)
works on the n900 too:
Code:

/home/user # oneh=100 ; getcurrent=123 ;timeshundred=$(($get
current*$oneh)) ; echo $timeshundred
12300
/home/user #


Okay so a completely fresh script on the N900 in question with the above works.
So is there something I have done further back in the script that is effecting it?

I have tried "let" and "expr" style claculations with the same results.
I also originally tried just putting "$getcurrent*100" and got the issue.

Installing bash now to see if it makes a difference.

sixwheeledbeast 2012-12-04 21:36

Re: [Help] [Solution Found] Require help with script not working correctly on N900.
 
Quote:

Originally Posted by mr_pingu (Post 1301417)
Did you try the same script in bash? There are some minor differences between ash and bash ;)

Try to run the script from bash, need to have bash3 (packagename) installed though.

Bash3 installed.
Changed to #!/bin/bash
Works a treat, thank you.
Is this a bug in ash/busybox?
Is there another way of doing this so my application doesn't depend on bash3?

mr_pingu 2012-12-04 21:52

Re: [Help] [Solution Found] Script on N900 not calculating correctly.
 
Not a bug of busybox, more of a feature which most of us don't like. Remember busybox is written in mind to be used on embedded devices so basically it's a stripped down version of bash.

It would probably possible to rewrite without the dependence on bash but then you should knock on somebody else door. I am not a good coder/scripter.

sixwheeledbeast 2012-12-04 22:08

Re: [Help] [Solution Found] Script on N900 not calculating correctly.
 
Quote:

Originally Posted by mr_pingu (Post 1301452)
Remember busybox is written in mind to be used on embedded devices so basically it's a stripped down version of bash.

Seems odd to make it calculate a simple multiplication incorrectly. I can understand stripping commands to make a lightweight version. Making commands not work correctly is plain stupid IMO.

peterleinchen 2012-12-04 22:30

Re: [Help] [Solution Found] Script on N900 not calculating correctly.
 
What is the output of $getcurrent during execution?
I would also guess anon-numeric string?
Or what is the error message. Or full output of script.

mr_pingu 2012-12-04 22:41

Re: [Help] [Solution Found] Script on N900 not calculating correctly.
 
good point but AFAIK it doesn't support all the typical calculation shizzle :( However you could use the "dc" applet delivered with busybox

Code:

dc --help
BusyBox v1.20.2 (Debian 1.20.2power4+thumb0) multi-call binary.

Usage: dc EXPRESSION...

Tiny RPN calculator. Operations:
+, add, -, sub, *, mul, /, div, %, mod, **, exp, and, or, not, eor,
p - print top of the stack (without popping),
f - print entire stack,
o - pop the value and set output radix (must be 10, 16, 8 or 2).
Examples: 'dc 2 2 add p' -> 4, 'dc 8 8 * 2 2 + / p' -> 16


sixwheeledbeast 2012-12-04 22:51

Re: [Help] [Solution Found] Script on N900 not calculating correctly.
 
Quote:

Originally Posted by peterleinchen (Post 1301458)
Or what is the error message. Or full output of script.

BASH
Code:

/media/mmc1 $ bash swapblocks.sh
261896 ublocks
134090752 ubytes
130948 ukbytes, 127 umbytes, 0 ugbytes
838852608 totalswapbytes
1 lasttotalsb
134090751 getcurrent
13409075100 timeshundred
15 % Swap Fragmented

ASH
Code:

/media/mmc1 $ sh swapblocks.sh 
261896 ublocks
134090752 ubytes
130948 ukbytes, 127 umbytes, 0 ugbytes
838852608 totalswapbytes
1 lasttotalsb
134090751 getcurrent
524173212 timeshundred
0 % Swap Fragmented


sixwheeledbeast 2012-12-04 23:11

Re: [Help] [Solution Found] Script on N900 not calculating correctly.
 
Quote:

Originally Posted by mr_pingu (Post 1301467)
However you could use the "dc" applet delivered with busybox

Not an option, bbpower only function.

Code:

dc --help
-sh: dc: not found


peterleinchen 2012-12-05 08:01

Re: [Help] [Solution Found] Script on N900 not calculating correctly.
 
int32!

Do your calc not as byte, but as kB or MB (not that exact, but hey you also do not round the percentage ;))

reinob 2012-12-05 08:16

Re: [Help] [Solution Found] Script on N900 not calculating correctly.
 
Add.: @peterleinchen beat me!

Quote:

Originally Posted by sixwheeledbeast (Post 1301468)
BASH
Code:

/media/mmc1 $ bash swapblocks.sh
261896 ublocks
134090752 ubytes
130948 ukbytes, 127 umbytes, 0 ugbytes
838852608 totalswapbytes
1 lasttotalsb
134090751 getcurrent
13409075100 timeshundred
15 % Swap Fragmented

ASH
Code:

/media/mmc1 $ sh swapblocks.sh 
261896 ublocks
134090752 ubytes
130948 ukbytes, 127 umbytes, 0 ugbytes
838852608 totalswapbytes
1 lasttotalsb
134090751 getcurrent
524173212 timeshundred
0 % Swap Fragmented


524173212 is ((134090751 * 100) mod 2*1024**3), meaning busybox does only 32-bit signed arithmetic.

Didn't look at the script, but I guess that whenever you read values that are expected to overflow a signed 32-bit number, you should do some kind of "renormalization" (e.g. divide by 1000, or by 1000000, as appropriate), and keep track of the "multiplier" in another variable. If you really need it, that is.

Don't know at the moment if dc or bc are included in busybox(-power), but AWK is always there, and does (or should do) the job correctly.

sixwheeledbeast 2012-12-05 19:09

Re: [Help] [Solution Found] Script on N900 not calculating correctly.
 
Thank you all. Makes sense now.
Here's the fixed script and output, for anybody interested.
Should be able to use in FlopSwap now.

Code:

#!/bin/sh

# Setup notification banner
banner(){
o=org
f=freedesktop
n=Notifications
run-standalone.sh dbus-send --type=method_call \
--dest=$o.$f.$n /$o/$f/$n $o.$f.$n.SystemNoteInfoprint string:"$1"
}

# Setup notification dialog
dialog(){
o=org
f=freedesktop
n=Notifications
run-standalone.sh dbus-send --type=method_call \
--dest=$o.$f.$n /$o/$f/$n $o.$f.$n.SystemNoteDialog string:"$1" uint32: string:
}

#Get current swap from /proc/swaps
disk=$(cat /proc/swaps | awk '/dev/ {print $1}' | cut -d "/" -f 3)
#Get blocks used since boot from /proc/diskstats
ublocks=$(awk '{if ($3=="'"$disk"'") print $10}' /proc/diskstats)
echo $ublocks ublocks
#Multiply blocks by 512 to get used bytes
ubytes=$(($ublocks*512))
#Calculate used bytes into Kb, Mb and Gb
echo $ubytes ubytes
ukbytes=$(($ubytes/1024))
umbytes=$(($ubytes/1024/1024))
ugbytes=$(($ubytes/1024/1024/1024))
echo $ukbytes ukbytes, $umbytes umbytes, $ugbytes ugbytes
#Get Swap size from /proc/swaps
swapsize=$(awk '{if ($1=="'"/dev/$disk"'") print $3}' /proc/swaps)
#Divide by 1024 to get swap size in megabytes
swapsizembytes=$(($swapsize/1024))
echo $swapsizembytes swapsizembytes
#Get last megabytes value from /tmp folder
lasttotalsmb=$(cat /tmp/flopswaplast | awk '{print $1}')
echo $lasttotalsmb lasttotalsmb
#Subtract last from current value to get latest used value
getcurrent=$(($umbytes-$lasttotalsmb))
echo $getcurrent getcurrent
timeshundred=$(($getcurrent*100))
echo $timeshundred timeshundred
#Get divide to get percent
getpercent=$(($timeshundred/$swapsizembytes))
echo $getpercent % Fresh Swap Written
#Print banner
banner "$getpercent % Fresh Swap Written
$getcurrent Mb of $swapsizembytes Mb"
#If 94% show warning dialog
if [ $getpercent -ge 94 ]; then
dialog "

***Fresh Swap Recommended***
Used \"$getcurrent Mb ($getpercent)\" of \"$swapsizembytes Mb\"
Device $(hostname) on $(date)

"
fi

Code:

/media/mmc1 $ sh swapblocks.sh
262984 ublocks
134647808 ubytes
131492 ukbytes, 128 umbytes, 0 ugbytes
799 swapsizembytes
1 lasttotalsmb
127 getcurrent
12700 timeshundred
15 % Fresh Swap Written



All times are GMT. The time now is 15:46.

vBulletin® Version 3.8.8