in reply to get system info using perl

If you want to save the spawning time of a new shell, you can keep a shell open in a pipe, using IPC::Open3, and send your commands to it. Put the command to-check in a while loop
#!/usr/bin/perl use warnings; use strict; use IPC::Open3; $|=1; #my $pid=open3(\*IN,\*OUT,\*ERR,'/bin/bash'); my $pid=open3(\*IN,\*OUT,0,'/bin/bash'); # set \*ERR to 0 to send STDERR to STDOUT my $cmd = 'date'; #send cmd to bash print IN "$cmd\n"; my $result = <OUT>; print $result;
like you I thought about making it more efficient by reading the file driectly. What I did, was find an assembly version of "cat", and did something like
sub refresh { my $pid = shift; #asmutils version of cat my @size = split "\n", `/home/zentara/perl5lib/cat /proc/$pid/stat +us`; #my @size = split "\n", `cat /proc/$pid/status`; (my $vmsize) = grep { /VmSize/ } @size; my (undef, $size) = split ' ', $vmsize; $t->configure(-text => "PID: $pid -> $size"); if ($size eq '') { exit } } }
I never really accurately tested which one is fastest, but the assembly cat version made me feel fast. :-)

I'm not really a human, but I play one on earth Remember How Lucky You Are

Replies are listed 'Best First'.
Re^2: get system info using perl
by saurabh.hirani (Beadle) on Feb 05, 2009 at 16:00 UTC

    Thanks guys for your replies. I got a reply from comp.lang.perl.misc which pointed out the following 2 modules:
    1.Filesys::Df
    2. Linux::Sysinfo
    They are pretty easy to use, fast, and provide many values. I know that my daemon is not going to bomb the other one once in a second. But I just wanted to know.

    These modules really do pump up the speed from okay to burn-the-road. Just to compare system call and Filesys::Df, I wrote a small perl program which calls getting free disk using system call to df and using the module. I profiled them by running them around 1000 times and this was the output of dprofpp:


    Forking df and getting the time:
    Total Elapsed Time = 42.04899 Seconds
    User+System Time = 5.668994 Seconds
    Inclusive Times
    %Time ExclSec CumulS #Calls sec/call Csec/c Name
    96.7 5.485 5.485 1000 0.0055 0.0055 main::get_freequeue


    Using df from the module
    Total Elapsed Time = 0.618107 Seconds
    User+System Time = 0.768107 Seconds
    Inclusive Times
    %Time ExclSec CumulS #Calls sec/call Csec/c Name
    92.1 0.013 0.708 1000 0.0000 0.0007 main::get_freequeue
    90.4 0.013 0.695 1000 0.0000 0.0007 Filesys::Df::df
    88.7 0.682 0.682 1000 0.0007 0.0007
    Filesys::Df::_df

    The difference is staggering.

    I will also play around with the IPC mod as pointed ou by zentara. Looks interesting