- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Calculating Memory Use with Awk
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-11-2008 06:05 AM
тАО09-11-2008 06:05 AM
Calculating Memory Use with Awk
If you to a ps -efl it outputs everything including the "SZ" column which is the size of the memory being used by that process. Do you agree?
First, I'd like to know when i do a ps -efl, what are the units of the SZ column? Is it in KB, MB?? it doesn't say. Remember at school, whenever we display information, we have to give the units for all information. We go into industry and produce products, and we forget these very simple rules. And then the man pages on the ps command don't mention them either.
Well. If i do a:
ps -ef | awk '{totalsize+=$10};END{print totalsize}'
it prints the total size it added up from column 10 (the SZ column).
But...
when i do a top command, the total memory being used HAS NOTHING in common with what my awk script produced.
Is it a problem with units? Am I not comparing oranges with oranges??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-11-2008 06:36 AM
тАО09-11-2008 06:36 AM
Re: Calculating Memory Use with Awk
man ps tells you: "sz - The size in physical pages of the core image of the process, including text, data, and stack space. Physical page size is defined by _SC_PAGE_SIZE in the header file
Considering I see that in the docs.hp.com version going back to 10.20 -- I don't think this is a recent addition.
So... your awk script is adding up pages. Top by default (and again, the man page tells you besides the units in the output) outputs in Kb. So you need to multiply the result of your awk by the result of getconf _SC_PAGE_SIZE [to get bytes] / 1024 [to get Kb].
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-11-2008 07:27 AM
тАО09-11-2008 07:27 AM
Re: Calculating Memory Use with Awk
So, what do I need to add in my awk script to compute the fact that it is the number of pages being given. I want to transfer the answer to Megabytes.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-11-2008 07:37 AM - edited тАО09-06-2011 07:26 AM
тАО09-11-2008 07:37 AM - edited тАО09-06-2011 07:26 AM
Re: Calculating Memory Use with awk
>what do I need to add in my awk script to compute the fact that it is the number of pages being given. I want to transfer the answer to Megabytes.
print totalsize * 4 * 1024 / (1024 * 1024)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-11-2008 02:12 PM
тАО09-11-2008 02:12 PM
Re: Calculating Memory Use with Awk
An alternative to adding with awk would be to use bc or dc. I did it using a "while read" loop, I don't know how to do it with inline bc commands. Maybe a bc expert can offer a quicker solution.
I also added a comment to your other posting about "ls -l | awk"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-11-2008 02:48 PM
тАО09-11-2008 02:48 PM
Re: Calculating Memory Use with Awk
As TTr wrote, "The awk program is using 32-bit arithmetic so the suming in awk overflows at 2147483647. (2^31 - 1)."
...and for that reason, you could use Perl instead of 'awk':
# awk 'BEGIN{printf "%d %d\n",(2**31-1),((2**31-1)+10)}'
2147483647 2147483647
# perl -e 'BEGIN{printf "%d %d\n",(2**31-1),((2**31-1)+10)}'
2147483647 2147483657
For this posting:
# ps -ef | perl -nale '$totalsize+=$F[9];END{printf "%10d\n",($totalsize*4*1024/(1024*1024))}'
Notice that Perl counts from zero whereas 'awk' counts from one. Hence the 9th field in Perl is the tenth in 'awk'.
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-11-2008 03:14 PM
тАО09-11-2008 03:14 PM
Re: Calculating Memory Use with Awk
By the way. I think you meant to add the 'l' switch to your 'ps':
ps -elf | perl -nale '$totalsize+=$F[9];END{printf "%10d\n",($totalsize*4*1024/(1024*1024))}'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-11-2008 08:29 PM - edited тАО09-06-2011 07:23 AM
тАО09-11-2008 08:29 PM - edited тАО09-06-2011 07:23 AM
Re: Calculating Memory Use with awk
>TTr: The awk program is using 32-bit arithmetic so the summing in awk overflows at 2147483647.
Where did you get this idea? It uses a C double.
But you could just use:
print totalsize * 4 / 1024
Or you may have to use printf with a %f format.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-11-2008 10:59 PM
тАО09-11-2008 10:59 PM
Re: Calculating Memory Use with Awk
only a negligible "improvement" which could ease your parsing and arithmetics.
As you are only interested in the SZ column why not simplifying the ps part of the pipe like
$ UNIX95= ps -e -o sz= | awk ...
or even
$ UNIX95= ps -e -o vsz= | awk ...
which already outputs memory in KB rather than pages (see man ps).
- Tags:
- UNIX95
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-12-2008 04:23 AM
тАО09-12-2008 04:23 AM
Re: Calculating Memory Use with Awk
It was not an idea. I used the awk statement from above code and it stopped counting at the above number. Maybe I should have said "It appears that awk is using 32-bit arithmetic...". As I said in my posting it was on 11.11. I just did a "what" on awk and I have PHCO_33916 on it.
I also found this in PHCO_36161 which is exactly what I experienced and is caused by INT_MAX.
<--snip-->
( SR:8606454418 CR:JAGag11066 )
According to UNIX95 specification, awk(1) should use %d as the internal conversion format for a numeric value that is exactly equal to the value of an integer. Any other numeric value should be converted to a string with the value of the variable CONVFMT as the fmt argument. The defect can be seen as:
$ print 2147483648 | UNIX95=1 awk '{ print $1-0 }'
O/p : 2147483647 (This is wrong o/p)
Here %d is used as the format specifier even if the number to be converted is non-integer (greater than INT_MAX).
Resolution: awk(1) is modified to comply with the UNIX95 standards. The above example gives the following output with fix:
$ print 2147483648 | UNIX95=1 awk '{ print $1-0 }'
2.14748e+09
<---end snip--->
- Tags:
- UNIX95