open() close() same file many times

Discussion in 'Perl Misc' started by alfonsobaldaserra, Oct 2, 2009.

  1. 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.
    alfonsobaldaserra, Oct 2, 2009
    #1
    1. Advertising

  2. 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
    Jürgen Exner, Oct 2, 2009
    #2
    1. Advertising

  3. alfonsobaldaserra

    Ted Zlatanov Guest

    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
    Ted Zlatanov, Oct 2, 2009
    #3
  4. 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
    Martijn Lievaart, Oct 2, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    648
  2. sfxpete

    Insert same information many times

    sfxpete, Mar 2, 2007, in forum: ASP .Net
    Replies:
    7
    Views:
    322
    sfxpete
    Mar 2, 2007
  3. Daniel Pitts
    Replies:
    0
    Views:
    492
    Daniel Pitts
    Oct 18, 2007
  4. Iñaki Baz Castillo
    Replies:
    7
    Views:
    821
    Iñaki Baz Castillo
    Jan 12, 2010
  5. Iulian Ilea
    Replies:
    1
    Views:
    292
    pcx99
    Dec 21, 2006
Loading...

Share This Page