CPU: Wrong CPU Load %.
[tomato.git] / release / src / router / httpd / cpuinfo.c
blob5ceafb79c214b50d47e2e20cf2912a063d0439a8
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include "tomato.h"
7 struct occupy
9 char name[20];
10 unsigned int user;
11 unsigned int nice;
12 unsigned int system;
13 unsigned int idle;
14 unsigned int io;
15 unsigned int irq;
16 unsigned int sirq;
19 void trim( char *str)
21 char *copied, *tail = NULL;
22 if ( str == NULL )
23 return ;
25 for( copied = str; *str; str++ )
27 if ( (unsigned char)*str > 0x20 )
29 *copied++ = *str;
30 tail = copied;
32 else {
33 if ( tail )
34 *copied++ = *str;
38 if ( tail )
39 *tail = 0;
40 else
41 *copied = 0;
42 return ;
45 static float g_cpu_used;
46 static int cpu_num;
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
54 processor : 0
55 cpu model : BCM3302 V2.9
56 BogoMIPS : 238.38
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)
66 FILE *fd;
67 char *next;
68 char buff[1024];
69 char title[128], value[512];
70 int okcount=0;
72 fd = fopen ("/proc/cpuinfo", "r");
73 while (fgets(buff, sizeof(buff), fd)) {
74 next = buff;
75 strcpy(title, strsep(&next, ":"));
76 if (next == NULL) continue;
77 strcpy(value, next);
78 trim(value);
79 if (strncmp_ex(title, "system type")==0) {
80 okcount++;
81 //printf("system type: %s\n", value);
82 if (strncmp_ex(value, "Broadcom BCM5354")==0) {
83 strcpy(system_type, "Broadcom BCM5354");
85 else {
86 strcpy(system_type, value);
89 if (strncmp_ex(title, "cpu model")==0) {
90 okcount++;
91 //printf("cpu model: %s\n", value);
92 strcpy(cpu_model, value);
94 if (strncmp_ex(title, "BogoMIPS")==0) {
95 okcount++;
96 //printf("bogomips: %s\n", value);
97 strcpy(bogomips, value);
99 if (strncmp_ex(title, "cpu MHz")==0) {
100 okcount++;
101 //printf("cpuclk: %s\n", value);
102 strcpy(cpuclk, value);
105 //fprintf (stderr, "%s - %s", title, value);
107 fclose(fd);
108 return (okcount==3);
112 int main()
114 char system_type[64];
115 char cpu_model[64];
116 char bogomips[64];
117 char cpuclk[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];
128 int i;
130 // cpu_num = sysconf(_SC_NPROCESSORS_ONLN);
131 cpu_num = 1;
132 get_occupy(ocpu);
133 sleep(1);
134 get_occupy(ncpu);
135 for (i=0; i<cpu_num; i++)
137 cal_occupy(&ocpu[i], &ncpu[i]);
138 //printf("%f \n", g_cpu_used);
140 return g_cpu_used;
143 static void cal_occupy (struct occupy *o, struct occupy *n)
145 double od, nd;
146 double id, sd;
147 double scale;
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)
159 FILE *fd;
160 int n;
161 char buff[1024];
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);
171 fclose(fd);