Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > open() close() same file many times

Reply
Thread Tools

open() close() same file many times

 
 
alfonsobaldaserra
Guest
Posts: n/a
 
      10-02-2009
hello list

i am writing a script to check memory usage on linux.

the script opens and closes /proc/meminfo 5 times in 5 seconds to
calculate memory usage on 5 seconds average, it goes something like

for (1..5) {
open MEM, "/proc/meminfo" or die "..."
while (<MEM>) {
...
}
close MEM;
sleep 1;
}

i was just wondering if this is advisable to open/close file that
fast? is there any better approach to do this?

looking for your valuable suggestions.

thank you.
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      10-02-2009
alfonsobaldaserra <> wrote:
>i am writing a script to check memory usage on linux.
>
>the script opens and closes /proc/meminfo 5 times in 5 seconds to
>calculate memory usage on 5 seconds average, it goes something like
>
>for (1..5) {
> open MEM, "/proc/meminfo" or die "..."
> while (<MEM>) {
> ...
> }
> close MEM;
> sleep 1;
>}
>
>i was just wondering if this is advisable to open/close file that
>fast? is there any better approach to do this?


Maybe all you need is a seek() call to reset the special file?

jue
 
Reply With Quote
 
 
 
 
Ted Zlatanov
Guest
Posts: n/a
 
      10-02-2009
On Fri, 2 Oct 2009 04:41:49 -0700 (PDT) alfonsobaldaserra <> wrote:

a> the script opens and closes /proc/meminfo 5 times in 5 seconds to
a> calculate memory usage on 5 seconds average, it goes something like

a> for (1..5) {
a> open MEM, "/proc/meminfo" or die "..."
a> while (<MEM>) {
a> ...
a> }
a> close MEM;
a> sleep 1;
a> }

a> i was just wondering if this is advisable to open/close file that
a> fast? is there any better approach to do this?

That file is on a special procfs filesystem so the usual penalties don't
apply. Even on a regular filesystem this would not be a big deal, 1 Hz
is nothing in the context of modern CPUs and disks.

By the way, if you want *precise* readings, you don't want to use
sleep(1). You're sampling every 1sec + (time to open+close file), and
sleep() itself is not very precise so you may end up sampling irregular
intervals. Doing this correctly is not easy. At the very least, look
at the documentation for sleep() in `perldoc -f sleep'.

You can, however, run a pipe on vmstat: do

open VMSTAT, "vmstat 1|";

and read from it for as long as you need. You'll get updates every
second.

You could also look at Sys::Statistics::Linux, which I found via:

http://search.cpan.org/search?query=procfs&mode=all

it seems to provide much more than just memory info, so you may find it
useful.

Ted
 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      10-02-2009
On Fri, 02 Oct 2009 04:41:49 -0700, alfonsobaldaserra wrote:

> hello list
>
> i am writing a script to check memory usage on linux.
>
> the script opens and closes /proc/meminfo 5 times in 5 seconds to
> calculate memory usage on 5 seconds average, it goes something like
>
> for (1..5) {
> open MEM, "/proc/meminfo" or die "..." while (<MEM>) {
> ...
> }
> close MEM;
> sleep 1;
> }
>
> i was just wondering if this is advisable to open/close file that fast?
> is there any better approach to do this?


Juergen already pointed out that you could use seek(), but I would add,
does it really matter? This is all in memory stuff and unless it runs on
a severely handicapped machine (embedded processors can be seriously
under powered) you probably won't even be able to measure the difference.

Just my E 0.02

M4
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
getting same e-mail many times Jose A. Vigoa Computer Support 7 11-04-2007 08:29 AM
Spring/Hibernate loadAll returning the same identity too many times... Daniel Pitts Java 0 10-18-2007 06:41 PM
Insert same information many times sfxpete ASP .Net 7 03-02-2007 08:49 PM
How many times is too many times? Jules W MCSA 3 08-30-2005 07:33 PM
call LoadControl many times for the same ascx files hohans@yahoo.com ASP .Net 2 04-21-2005 07:38 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57