On Wed, 2007-11-07 at 13:38 -0500, James wrote:
> All,
> 
> I'm writing a piece of code that requires I gather the following  
> statistics every second:
> 
> - CPU Usage (as a percentage)

this is an interesting one.  I had to do this recently, and the only
place I could get the data from is /proc/stat

Except that the data is not in % and reading the top source code made me
go crosseyed!  They make a simple task so complicated!

I ended up with this ugly ... 6 liner!

ifsProcStat.open ("/proc/stat");
...
// in a loop:
   ifsProcStat.seekg (0, std::ios::beg);
   ifsProcStat >> strIgnore >> iUser >> iNice >> iSys >> iIdle >> iIoWait >> 
iIrq >> iSoftIrq;
   iTotal = iUser + iNice + iSys + iIdle + iIoWait + iIrq + iSoftIrq;

   fCpuIdleAct = ((float)(iIdle - iOldIdle) / (iTotal - iOldTotal)) * 100.0F;

   iOldIdle = iIdle;
   iOldTotal = iTotal;
// end of loop

Of course, I was looking at idle time as a % - you would be interested
in User time perhaps.

> - NIC I/O
> - Hard Drive I/O

I'll leave these up to you!

And of course this could be complete wrong!  I would welcome any
comments on my bad coding :)

HTH,
-- 
Iain Buchanan <iaindb at netspace dot net dot au>

Most people can do without the essentials, but not without the luxuries.

-- 
[EMAIL PROTECTED] mailing list

Reply via email to