| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| #include "stdlib.h" |
| 5 |
|
| #include "string.h" |
| 6 |
|
| #include "sys/types.h" |
| 7 |
|
| #include "sys/sysinfo.h" |
| 8 |
|
| #include <stdio.h> |
| 9 |
|
| |
| 10 |
0 |
| double cpuPercent (){ |
| 11 |
|
| |
| 12 |
|
| static unsigned long long lastTotalUser, lastTotalUserLow, lastTotalSys, lastTotalIdle; |
| 13 |
|
| double percent; |
| 14 |
|
| |
| 15 |
|
| FILE* file; |
| 16 |
|
| unsigned long long totalUser, totalUserLow, totalSys, totalIdle, total; |
| 17 |
|
| |
| 18 |
0 |
| file = fopen("/proc/stat", "r"); |
| 19 |
|
| |
| 20 |
0 |
▶ | if (file == NULL){ |
Branch: 1 branch
| Branch | True | False | Total |
| if (file == NULL){ |
0 |
0 |
0 |
|
| 21 |
0 |
| return -1.0; |
| 22 |
|
| } |
| 23 |
|
| |
| 24 |
0 |
▶ | if (fscanf(file, "cpu %llu %llu %llu %llu", &totalUser, &totalUserLow, |
Branch: 1 branch
| Branch | True | False | Total |
| if (fscanf(file, "cpu %llu %llu %llu %llu", &totalUser, &totalUserLow, |
0 |
0 |
0 |
|
| 25 |
|
| &totalSys, &totalIdle) != 4){ |
| 26 |
0 |
| fclose(file); |
| 27 |
0 |
| return -1.0; |
| 28 |
|
| } |
| 29 |
|
| |
| 30 |
0 |
| fclose(file); |
| 31 |
|
| |
| 32 |
0 |
▶ | if (totalUser < lastTotalUser || totalUserLow < lastTotalUserLow || |
Branch: 2 branches
| Branch | True | False | Total |
| if (totalUser < lastTotalUser || totalUserLow < lastTotalUserLow || |
0 |
0 |
0 |
| if (totalUser < lastTotalUser || totalUserLow < lastTotalUserLow || |
0 |
0 |
0 |
|
| 33 |
0 |
▶ | totalSys < lastTotalSys || totalIdle < lastTotalIdle){ |
Branch: 2 branches
| Branch | True | False | Total |
| totalSys < lastTotalSys || totalIdle < lastTotalIdle){ |
0 |
0 |
0 |
| totalSys < lastTotalSys || totalIdle < lastTotalIdle){ |
0 |
0 |
0 |
|
| 34 |
|
| //Overflow detection. Just skip this value. |
| 35 |
0 |
| percent = -1.0; |
| 36 |
|
| } |
| 37 |
|
| else{ |
| 38 |
0 |
| total = (totalUser - lastTotalUser) + (totalUserLow - lastTotalUserLow) + |
| 39 |
0 |
| (totalSys - lastTotalSys); |
| 40 |
0 |
| percent = total; |
| 41 |
0 |
| total += (totalIdle - lastTotalIdle); |
| 42 |
0 |
| percent /= total; |
| 43 |
0 |
| percent *= 100; |
| 44 |
|
| } |
| 45 |
|
| |
| 46 |
0 |
| lastTotalUser = totalUser; |
| 47 |
0 |
| lastTotalUserLow = totalUserLow; |
| 48 |
0 |
| lastTotalSys = totalSys; |
| 49 |
0 |
| lastTotalIdle = totalIdle; |
| 50 |
|
| |
| 51 |
0 |
| return percent; |
| 52 |
|
| } |
| 53 |
|
| |
| 54 |
0 |
| double memPercent (){ |
| 55 |
|
| struct sysinfo memInfo; |
| 56 |
|
| |
| 57 |
0 |
| sysinfo (&memInfo); |
| 58 |
|
| |
| 59 |
0 |
| long long totalPhysMem = memInfo.totalram; |
| 60 |
0 |
| totalPhysMem *= memInfo.mem_unit; |
| 61 |
|
| |
| 62 |
0 |
| long long physMemUsed = memInfo.totalram - memInfo.freeram; |
| 63 |
0 |
| physMemUsed *= memInfo.mem_unit; |
| 64 |
|
| |
| 65 |
0 |
| long long physMemFree = totalPhysMem - physMemUsed; |
| 66 |
|
| |
| 67 |
0 |
| double percent = 100.0 * physMemUsed / totalPhysMem; |
| 68 |
|
| |
| 69 |
0 |
| return percent; |
| 70 |
|
| } |
| 71 |
|
| |
| 72 |
|
| MODULE = RPi::SysInfo PACKAGE = RPi::SysInfo |
| 73 |
|
| |
| 74 |
|
| PROTOTYPES: DISABLE |
| 75 |
|
| |
| 76 |
|
| |
| 77 |
|
| double |
| 78 |
|
| cpuPercent () |
| 79 |
|
| |
| 80 |
|
| double |
| 81 |
|
| memPercent () |
| 82 |
|
| |