Есть следующая функция от производителя:
Код: Выделить всё
******************************************************************************
* Cpu_getLoad
******************************************************************************/
Int Cpu_getLoad(Cpu_Handle hCpu, Int *cpuLoad)
{
int cpuLoadFound = FALSE;
unsigned long user, nice, sys, idle, total, proc;
unsigned long uTime, sTime, cuTime, csTime;
unsigned long deltaTotal, deltaIdle, deltaProc;
char textBuf[4];
FILE *fptr;
/* Read the overall system information */
fptr = fopen("/proc/stat", "r");
if (fptr == NULL) {
Dmai_err0("/proc/stat not found. Is the /proc filesystem mounted?\n");
return Dmai_EIO;
}
/* Scan the file line by line */
while (fscanf(fptr, "%4s %lu %lu %lu %lu %*[^\n]", textBuf,
&user, &nice, &sys, &idle) != EOF) {
if (strcmp(textBuf, "cpu") == 0) {
cpuLoadFound = TRUE;
break;
}
}
if (fclose(fptr) != 0) {
return Dmai_EIO;
}
if (!cpuLoadFound) {
return Dmai_EFAIL;
}
/* Read the current process information */
fptr = fopen("/proc/self/stat", "r");
if (fptr == NULL) {
Dmai_err0("/proc/self/stat not found. Is /proc filesystem mounted?\n");
return Dmai_EIO;
}
if (fscanf(fptr, "%*d %*s %*s %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %lu "
"%lu %lu %lu", &uTime, &sTime, &cuTime, &csTime) != 4) {
Dmai_err0("Failed to get process load information.\n");
fclose(fptr);
return Dmai_EIO;
}
if (fclose(fptr) != 0) {
return Dmai_EFAIL;
}
total = user + nice + sys + idle;
proc = uTime + sTime + cuTime + csTime;
/* Check if this is the first time, if so init the prev values */
if (hCpu->prevIdle == 0 && hCpu->prevTotal == 0 && hCpu->prevProc == 0) {
hCpu->prevIdle = idle;
hCpu->prevTotal = total;
hCpu->prevProc = proc;
return Dmai_EOK;
}
deltaIdle = idle - hCpu->prevIdle;
deltaTotal = total - hCpu->prevTotal;
deltaProc = proc - hCpu->prevProc;
hCpu->prevIdle = idle;
hCpu->prevTotal = total;
hCpu->prevProc = proc;
// *cpuLoad = 100 - deltaIdle * 100 / deltaTotal;
// *procLoad = deltaProc * 100 / deltaTotal;
*cpuLoad = deltaProc * 100 / deltaTotal;
return Dmai_EOK;
}Проблема в том, что она выдаёт неверные значения, очень часто я получаю значения больше 100%.
Используемый процессор:
Код: Выделить всё
Processor : ARM926EJ-S rev 5 (v5l)
BogoMIPS : 33.48
Features : swp half thumb fastmult edsp java
CPU implementer : 0x41
CPU architecture: 5TEJ
CPU variant : 0x0
CPU part : 0x926
CPU revision : 5
Cache type : write-back
Cache clean : cp15 c7 ops
Cache lockdown : format C
Cache format : Harvard
I size : 16384
I assoc : 4
I line length : 32
I sets : 128
D size : 8192
D assoc : 4
D line length : 32
D sets : 64
Hardware : DaVinci DM355 EVM
Revision : 3500000
Serial : 0000000000000000Я прочитал доступную информацию по /proc, но не нашёл расшифровки строк в /proc/self/stat.
Можете подсказать?
Спасибо.