forked from bbadaboom/lg.srv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc.c
77 lines (72 loc) · 1.41 KB
/
proc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdio.h>
#include <signal.h>
#include <stdarg.h>
#include "NetDrv.h"
int ReadProcStat( int *a_user, int *a_sys, int *a_nice, int *a_idle )
{
int fd;
char tmp[2048];
char *p;
char **av;
int ac;
int i, l;
static long ouser=0;
static long osys=0;
static long onice=0;
static long oidle=0;
long user, nice, sys, idle;
long udiff, ndiff, sdiff, idiff;
long jiffies=0;
fd=open( "/proc/stat", O_RDONLY );
if ( fd == -1 )
return -1;
*tmp=0;
if ( read( fd, tmp, 2048 ) );
close(fd);
tmp[512]=0;
p=strchr(tmp,'\n');
if ( p ) *p=0;
av=M5sStrgCut(tmp,&ac,1);
if ( !av )
return -1;
if ( ac<5 )
{
free( av );
return -1;
}
for( i=1;i<5;i++ )
{
l=strlen(av[i]);
if ( l>6 )
av[i]=av[i]+l-6;
}
user=atoi(av[1]);
nice=atoi(av[2]);
sys=atoi(av[3]);
idle=atoi(av[4]);
udiff = user < ouser ? 1000000+user-ouser : user-ouser;
sdiff = sys < osys ? 1000000+sys-osys : sys-osys;
idiff = idle < oidle ? 1000000+idle-oidle : idle-oidle;
ndiff = nice < onice ? 1000000+nice-onice : nice-onice;
jiffies = udiff+sdiff+idiff+ndiff;
if ( jiffies )
{
*a_user = udiff>jiffies?10000:udiff*10000/jiffies;
*a_sys = sdiff>jiffies?10000:sdiff*10000/jiffies;
*a_nice = ndiff>jiffies?10000:ndiff*10000/jiffies;
*a_idle = idiff>jiffies?10000:idiff*10000/jiffies;
}
else
{
*a_user = 0;
*a_sys = 0;
*a_nice = 0;
*a_idle = 0;
}
free( av );
ouser=user;
osys=sys;
onice=nice;
oidle=idle;
return 0;
}