On Fri, 24 Aug 2012 16:05:28 -0500, Dmitry Olshansky <dmitry.o...@gmail.com> wrote:

On 25-Aug-12 00:52, 1100110 wrote:
On Fri, 24 Aug 2012 15:42:21 -0500, Dmitry Olshansky
<dmitry.o...@gmail.com> wrote:

On 24-Aug-12 07:03, 1100110 wrote:

On linux this is not so difficult to do.

Those values are generally in /proc, and it seems to be portable across
pretty much every distro with a relatively recent kernel.

I have an extremely half-assed bit of code that prints the load average
and the totaly % of mem used to my tmux session.
It gives the exact same values that are seen in top, or htop.(without
the overhead of parsing their output, cause that takes ~500ms, way too
slow.)

If parsing takes 500ms then something is seriously wrong. What is size
of the input to parse and the machine specs?


No, you misunderstand.

Parsing the output of the `top` command takes ~500ms.

Then it's not parsing but the it's time to spawn process that does a fuckton of syscalls and read its output through the pipe :)
I just cringed at:
"the overhead of parsing their output, cause that takes ~500ms"

The snippet of code that reads /proc directly takes an order of
magnitude less time.

I assume that top reads several times, and also reads the percentages of
each process that is running, while I only
read the load average and total memory consumption.

The vast majority of the time spent parsing `top` was spent waiting for
it to initialize, and print the values to be read.

TL;DR
Parsing was probably the wrong word to use.


Yes, sorry for being nit-picky.

Reading from /proc directly will take an order of magnitude less time.


And yes.

    size_t memTotal, memUsed;
    Stream memInfo  = new BufferedFile("/proc/meminfo");
    auto t          = memInfo.readLine();
    memTotal        = to!size_t(strip(t[9..$-3]));
    memUsed         = memTotal;

    auto f  = memInfo.readLine();
    memUsed -= to!size_t(strip(f[8..$-3]));

    auto c = memInfo.readLine();
    memUsed -= to!size_t(strip(c[8..$-3]));

    auto b = memInfo.readLine();
    memUsed -= to!size_t(strip(b[8..$-3]));

    memInfo.close();

Fair warning, I wrote this over the course of 5 minutes by directly translating some public domain code.
It works, so I've never bothered to clean it up.

But that'll give you the total memory used, and the total amount of memory.
--
Using Opera's revolutionary email client: http://www.opera.com/mail/

Reply via email to