* [9fans] awk: hex numbers? (and leak(1))
@ 2006-09-20 10:33 Axel Belinfante
2006-09-20 13:34 ` Gabriel Diaz
0 siblings, 1 reply; 8+ messages in thread
From: Axel Belinfante @ 2006-09-20 10:33 UTC (permalink / raw)
To: 9fans
is there a way to use awk to sum hex values?
i.e. I want to sum values in the 3rd column of
block 0x000af9a8 0x00000040 0x0001ddf3 0x00000000
block 0x000afd88 0x00000040 0x0001ddf3 0x00000000
I have tried to make awk use an external program
to do the hex-to-dec conversion, but that was very
slow (probably due to many page faults).
(in the end I resorted to perl...)
context:
I've been fiddling with leak(1) to make it print
an overview of how much memory is allocated where,
as in:
location total calls
amount
allocated
src(0x0001d8f1); // 247648 20
src(0x000067f8); // 24256 59
src(0x0001da70); // 15760 6
src(0x000128ac); // 8256 1
src(0x000210f8); // 4128 66
src(0x00014bc2); // 3648 57
[...]
src(0x00006586); // 64 1
src(0x00011a50); // 64 1
src(0x000067c7); // 64 1
src(0x0001ca32); // 32 1
// 326544
I found it quite useful to get insight in where
the memory goes (even when it is not leaked) -
but maybe we already have this functionality
somewhere, and I missed it?
Axel.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [9fans] awk: hex numbers? (and leak(1))
2006-09-20 10:33 [9fans] awk: hex numbers? (and leak(1)) Axel Belinfante
@ 2006-09-20 13:34 ` Gabriel Diaz
2006-09-20 13:58 ` Axel Belinfante
2006-09-20 20:20 ` Russ Cox
0 siblings, 2 replies; 8+ messages in thread
From: Gabriel Diaz @ 2006-09-20 13:34 UTC (permalink / raw)
To: Fans of the OS Plan 9 from Bell Labs
hello
may be this help you:
define todec(str) {
hstr="1234567890ABDCEF";
res=0;
n=split(str,digit,"");
for(i=1;i<=n;i++) {
num=index(hstr,digit[i])-1;
res=res+(num*16^(n-i));
}
return res;
}
then just sum=todec(hex1)+todec(hex2);
if you do not go beyond int it should work.
slds.
gabi
On 9/20/06, Axel Belinfante <Axel.Belinfante@cs.utwente.nl> wrote:
> is there a way to use awk to sum hex values?
> i.e. I want to sum values in the 3rd column of
>
> block 0x000af9a8 0x00000040 0x0001ddf3 0x00000000
> block 0x000afd88 0x00000040 0x0001ddf3 0x00000000
>
> I have tried to make awk use an external program
> to do the hex-to-dec conversion, but that was very
> slow (probably due to many page faults).
> (in the end I resorted to perl...)
>
>
>
> context:
>
> I've been fiddling with leak(1) to make it print
> an overview of how much memory is allocated where,
> as in:
>
> location total calls
> amount
> allocated
>
> src(0x0001d8f1); // 247648 20
> src(0x000067f8); // 24256 59
> src(0x0001da70); // 15760 6
> src(0x000128ac); // 8256 1
> src(0x000210f8); // 4128 66
> src(0x00014bc2); // 3648 57
> [...]
> src(0x00006586); // 64 1
> src(0x00011a50); // 64 1
> src(0x000067c7); // 64 1
> src(0x0001ca32); // 32 1
> // 326544
>
>
> I found it quite useful to get insight in where
> the memory goes (even when it is not leaked) -
> but maybe we already have this functionality
> somewhere, and I missed it?
>
> Axel.
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [9fans] awk: hex numbers? (and leak(1))
2006-09-20 13:34 ` Gabriel Diaz
@ 2006-09-20 13:58 ` Axel Belinfante
2006-09-20 20:20 ` Russ Cox
1 sibling, 0 replies; 8+ messages in thread
From: Axel Belinfante @ 2006-09-20 13:58 UTC (permalink / raw)
To: Fans of the OS Plan 9 from Bell Labs
> may be this help you:
thanks! fits nicely in my 'homework assignment'. :-)
perl-less result in sources/contrib/axel/leak
Axel.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [9fans] awk: hex numbers? (and leak(1))
2006-09-20 13:34 ` Gabriel Diaz
2006-09-20 13:58 ` Axel Belinfante
@ 2006-09-20 20:20 ` Russ Cox
2006-09-20 20:29 ` Gabriel Diaz
1 sibling, 1 reply; 8+ messages in thread
From: Russ Cox @ 2006-09-20 20:20 UTC (permalink / raw)
To: Fans of the OS Plan 9 from Bell Labs
> define todec(str) {
> hstr="1234567890ABDCEF";
> res=0;
> n=split(str,digit,"");
>
> for(i=1;i<=n;i++) {
> num=index(hstr,digit[i])-1;
> res=res+(num*16^(n-i));
> }
>
> return res;
> }
Here's an alternate version.
Among other things my awk doesn't seem to know about "define".
BEGIN {
for(i=0; i<16; i++)
_unhex[sprintf("%x", i)] = _unhex[sprintf("%X", i)] = i
}
function unhex(s, i, v) {
v = 0
for (i=1; i<=length(s); i++)
v = v*16 + _unhex[substr(s,i,1)]
return v
}
{print unhex($1)}
Russ
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [9fans] awk: hex numbers? (and leak(1))
2006-09-20 20:20 ` Russ Cox
@ 2006-09-20 20:29 ` Gabriel Diaz
2006-09-21 22:41 ` Axel Belinfante
0 siblings, 1 reply; 8+ messages in thread
From: Gabriel Diaz @ 2006-09-20 20:29 UTC (permalink / raw)
To: Fans of the OS Plan 9 from Bell Labs
hello
sorry i mixed with bc syntax.
function is the right keyword.
gabi
On 9/20/06, Russ Cox <rsc@swtch.com> wrote:
> > define todec(str) {
> > hstr="1234567890ABDCEF";
> > res=0;
> > n=split(str,digit,"");
> >
> > for(i=1;i<=n;i++) {
> > num=index(hstr,digit[i])-1;
> > res=res+(num*16^(n-i));
> > }
> >
> > return res;
> > }
>
> Here's an alternate version.
> Among other things my awk doesn't seem to know about "define".
>
> BEGIN {
> for(i=0; i<16; i++)
> _unhex[sprintf("%x", i)] = _unhex[sprintf("%X", i)] = i
> }
>
> function unhex(s, i, v) {
> v = 0
> for (i=1; i<=length(s); i++)
> v = v*16 + _unhex[substr(s,i,1)]
> return v
> }
>
> {print unhex($1)}
>
> Russ
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [9fans] awk: hex numbers? (and leak(1))
2006-09-20 20:29 ` Gabriel Diaz
@ 2006-09-21 22:41 ` Axel Belinfante
2006-09-21 23:02 ` Russ Cox
0 siblings, 1 reply; 8+ messages in thread
From: Axel Belinfante @ 2006-09-21 22:41 UTC (permalink / raw)
To: Fans of the OS Plan 9 from Bell Labs
thanks all who helped.
I fiddled a bit more with my leak
(updated my sources contrib).
it helped me get more insight in
programs memory usage.
as a sidenote: one thing i learned is that
using lib9p with filetrees when having
'lots' of files 'lots' of strings get
stored (partly (largely?) duplicated)
due to the estrdup9p in createfile
(for uid, guid, muid, and name).
(I think for 25000 files this 'cost' 2 Mb or so)
would sharing those strings be an option?
(or just make things more complicated?)
(in this case I rewrote my code to no longer
use filetrees - as suggested in 9p(2) -
my file hierarchy was very regular indeed)
Axel.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [9fans] awk: hex numbers? (and leak(1))
2006-09-21 22:41 ` Axel Belinfante
@ 2006-09-21 23:02 ` Russ Cox
2006-09-22 9:57 ` Axel Belinfante
0 siblings, 1 reply; 8+ messages in thread
From: Russ Cox @ 2006-09-21 23:02 UTC (permalink / raw)
To: Fans of the OS Plan 9 from Bell Labs
if you have lots of files you shouldn't be using file trees.
russ
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [9fans] awk: hex numbers? (and leak(1))
2006-09-21 23:02 ` Russ Cox
@ 2006-09-22 9:57 ` Axel Belinfante
0 siblings, 0 replies; 8+ messages in thread
From: Axel Belinfante @ 2006-09-22 9:57 UTC (permalink / raw)
To: Fans of the OS Plan 9 from Bell Labs
> if you have lots of files you shouldn't be using file trees.
fair enough.
thanks,
Axel.
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2006-09-22 9:57 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-09-20 10:33 [9fans] awk: hex numbers? (and leak(1)) Axel Belinfante
2006-09-20 13:34 ` Gabriel Diaz
2006-09-20 13:58 ` Axel Belinfante
2006-09-20 20:20 ` Russ Cox
2006-09-20 20:29 ` Gabriel Diaz
2006-09-21 22:41 ` Axel Belinfante
2006-09-21 23:02 ` Russ Cox
2006-09-22 9:57 ` Axel Belinfante
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).