open() close() same file many times

  • Thread starter alfonsobaldaserra
  • Start date
A

alfonsobaldaserra

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.
 
J

Jürgen Exner

alfonsobaldaserra said:
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
 
T

Ted Zlatanov

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
 
M

Martijn Lievaart

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top