open() close() same file many times 
Author Message
 open() close() same file many times

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;

Quote:
}

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.



Tue, 20 Mar 2012 12:41:49 GMT  
 open() close() same file many times

Quote:

>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



Tue, 20 Mar 2012 13:50:30 GMT  
 open() close() same file many times

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



Tue, 20 Mar 2012 16:04:58 GMT  
 open() close() same file many times

Quote:

> 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



Tue, 20 Mar 2012 20:18:08 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Filehandle: open(FILE) while{} close(FILE)

2. small ndbm files are not properly updated at close time

3. Question: Opening and closing files.

4. Spooling open file via http til close...

5. Is file open/closed?

6. Problem with open/closing parsing a file

7. open() not closing open filehandle before reopening

8. Opening and closing dbm files from subroutines?

9. PERLFUNC: close - close file (or pipe or socket) handle

10. PERLFUNC: close - close file (or pipe or socket) handle

11. Open file 2 times writing then append

12. open many files at the same time

 

 
Powered by phpBB® Forum Software