1 |
On Wed, 2007-11-07 at 13:38 -0500, James wrote: |
2 |
> All, |
3 |
> |
4 |
> I'm writing a piece of code that requires I gather the following |
5 |
> statistics every second: |
6 |
> |
7 |
> - CPU Usage (as a percentage) |
8 |
|
9 |
this is an interesting one. I had to do this recently, and the only |
10 |
place I could get the data from is /proc/stat |
11 |
|
12 |
Except that the data is not in % and reading the top source code made me |
13 |
go crosseyed! They make a simple task so complicated! |
14 |
|
15 |
I ended up with this ugly ... 6 liner! |
16 |
|
17 |
ifsProcStat.open ("/proc/stat"); |
18 |
... |
19 |
// in a loop: |
20 |
ifsProcStat.seekg (0, std::ios::beg); |
21 |
ifsProcStat >> strIgnore >> iUser >> iNice >> iSys >> iIdle >> iIoWait >> iIrq >> iSoftIrq; |
22 |
iTotal = iUser + iNice + iSys + iIdle + iIoWait + iIrq + iSoftIrq; |
23 |
|
24 |
fCpuIdleAct = ((float)(iIdle - iOldIdle) / (iTotal - iOldTotal)) * 100.0F; |
25 |
|
26 |
iOldIdle = iIdle; |
27 |
iOldTotal = iTotal; |
28 |
// end of loop |
29 |
|
30 |
Of course, I was looking at idle time as a % - you would be interested |
31 |
in User time perhaps. |
32 |
|
33 |
> - NIC I/O |
34 |
> - Hard Drive I/O |
35 |
|
36 |
I'll leave these up to you! |
37 |
|
38 |
And of course this could be complete wrong! I would welcome any |
39 |
comments on my bad coding :) |
40 |
|
41 |
HTH, |
42 |
-- |
43 |
Iain Buchanan <iaindb at netspace dot net dot au> |
44 |
|
45 |
Most people can do without the essentials, but not without the luxuries. |
46 |
|
47 |
-- |
48 |
gentoo-user@g.o mailing list |