21 char *copied
, *tail
= NULL
;
25 for( copied
= str
; *str
; str
++ )
27 if ( (unsigned char)*str
> 0x20 )
45 static float g_cpu_used
;
48 static void cal_occupy(struct occupy
*, struct occupy
*);
49 static void get_occupy(struct occupy
*);
53 system type : Broadcom BCM5354 chip rev 2 pkg 0
55 cpu model : BCM3302 V2.9
58 int strncmp_ex(char *str1
, char *str2
)
60 return strncmp(str1
, str2
, strlen(str2
));
63 int get_cpuinfo(char *system_type
, char *cpu_model
, char *bogomips
, char *cpuclk
)
69 char title
[128], value
[512];
72 fd
= fopen ("/proc/cpuinfo", "r");
73 while (fgets(buff
, sizeof(buff
), fd
)) {
75 strcpy(title
, strsep(&next
, ":"));
76 if (next
== NULL
) continue;
79 if (strncmp_ex(title
, "system type")==0) {
81 //printf("system type: %s\n", value);
82 if (strncmp_ex(value
, "Broadcom BCM5354")==0) {
83 strcpy(system_type
, "Broadcom BCM5354");
86 strcpy(system_type
, value
);
89 if (strncmp_ex(title
, "cpu model")==0) {
91 //printf("cpu model: %s\n", value);
92 strcpy(cpu_model
, value
);
94 if (strncmp_ex(title
, "BogoMIPS")==0) {
96 //printf("bogomips: %s\n", value);
97 strcpy(bogomips
, value
);
99 if (strncmp_ex(title
, "cpu MHz")==0) {
101 //printf("cpuclk: %s\n", value);
102 strcpy(cpuclk
, value
);
105 //fprintf (stderr, "%s - %s", title, value);
114 char system_type[64];
118 if get_cpuinfo(system_type, cpu_model, bogomips, cpuclk) {
119 printf("system type: %s, cpu model: %s, bogomips: %s, cpuclk: %s\n", system_type, cpu_model, bogomips, cpuclk);
124 float get_cpupercent()
126 struct occupy ocpu
[10];
127 struct occupy ncpu
[10];
130 // cpu_num = sysconf(_SC_NPROCESSORS_ONLN);
135 for (i
=0; i
<cpu_num
; i
++)
137 cal_occupy(&ocpu
[i
], &ncpu
[i
]);
138 //printf("%f \n", g_cpu_used);
143 static void cal_occupy (struct occupy
*o
, struct occupy
*n
)
149 od
= (double) (o
->user
+ o
->nice
+ o
->system
+ o
->idle
+ o
->io
+ o
->irq
+ o
->sirq
);
150 nd
= (double) (n
->user
+ n
->nice
+ n
->system
+ n
->idle
+ n
->io
+ n
->irq
+ n
->sirq
);
151 scale
= 100.0 / (float)(nd
-od
);
152 id
= (double) (n
->user
- o
->user
);
153 sd
= (double) (n
->system
- o
->system
);
154 g_cpu_used
= ((sd
+id
)*100.0)/(nd
-od
);
157 static void get_occupy (struct occupy
*o
)
163 fd
= fopen ("/proc/stat", "r");
164 fgets (buff
, sizeof(buff
), fd
);
165 for(n
=0;n
<cpu_num
;n
++)
167 fgets (buff
, sizeof(buff
),fd
);
168 sscanf (buff
, "%s %u %u %u %u %u %u %u", o
[n
].name
, &o
[n
].user
, &o
[n
].nice
, &o
[n
].system
, &o
[n
].idle
, &o
[n
].io
, &o
[n
].irq
, &o
[n
].sirq
);
169 //fprintf (stderr, "%s %u %u %u %u %u %u %u\n", o[n].name, o[n].user, o[n].nice, o[n].system, o[n].idle, o[n].io, o[n].irq, o[n].sirq);