SeptemberHX / dde-istate-menus

a dde-dock plugin which aims to implement most features of Istat menus(macOS) for Deepin V20
GNU General Public License v3.0
40 stars 5 forks source link

It is not possible to read /proc/loadavg on Archlinux using gcc 10.1.0 and Qt 5.15.0 #7

Open JunioCalu opened 4 years ago

JunioCalu commented 4 years ago

patch:

diff --git a/utils/system_stat.cpp b/utils/system_stat.cpp
index 90395fb..00c0d24 100644
--- a/utils/system_stat.cpp
+++ b/utils/system_stat.cpp
@@ -52,25 +52,27 @@ auto print_err = [](decltype(errno) e, const QString &msg)
 bool SystemStat::readLoadAvg(qreal &loadAvg1, qreal &loadAvg5, qreal &loadAvg15) {
     bool b = false;
     FILE *fp;
-    char buf[128];
-    int rc;
-    double avg1, avg5, avg15;
-
+    char buf[256];
+    double load[3];
+    int i;
     errno = 0;
-    if ((fp = fopen(PROC_PATH_LOADAVG, "r")) == nullptr) {
-        print_err(errno, QString("open %1 failed").arg(PROC_PATH_LOADAVG));
-        return b;
-    }

-    if (fgets(buf, sizeof(buf), fp)) {
-        rc = sscanf(buf, "%lf %lf %lf", &avg1, &avg5, &avg15);
-        if (rc == 3) {
-            loadAvg1 = avg1;
-            loadAvg5 = avg5;
-            loadAvg15 = avg15;
+    if ((fp = fopen(PROC_PATH_LOADAVG, "r")) != nullptr) {
+       if((fgets(buf, sizeof(buf), fp)) != nullptr) {
+           load[0] = strtod(strtok(buf, " \n"), NULL);
+           for(i=1;i<3;i++){
+               load[i] = strtod(strtok(NULL, " \n"), NULL);
+           }
+           loadAvg1 = load[0];
+           loadAvg5 = load[1];
+           loadAvg15 = load[2];
             b = true;
         }
-    }
+        else {
+           print_err(errno, QString("open %1 failed").arg(PROC_PATH_LOADAVG));
+           return b;
+       }
+   }
     fclose(fp);
     if (!b) {
         print_err(errno, QString("read %1 failed").arg(PROC_PATH_LOADAVG));
SeptemberHX commented 4 years ago

What does the context of /proc/loadavg look like on Arch ? Does it have three lines instead of one ?

JunioCalu commented 4 years ago

are six values:

~ >>> cat /proc/loadavg
1.53 2.11 1.65 2/828 17652

JunioCalu commented 4 years ago

What does the context of /proc/loadavg look like on Arch ? Does it have three lines instead of one ?

one line

JunioCalu commented 4 years ago

I did a simple test on another file named proc.cpp to test the output with decimal places and the values were displayed correctly, but in the dde-istate-menus they are simply not displayed.

#include <cstdio>
#include <stdlib.h>
#include <string.h>
int main(){
float a, b, c;
char buf[128];
FILE *fp;
int i;
double load[3];
double loadAvg1, loadAvg5, loadAvg15;

if ((fp = fopen("/proc/loadavg", "r")) != NULL) {
    if((fgets(buf, sizeof(buf), fp)) != NULL) {
        load[0] = strtod(strtok(buf, " \n"), NULL);
        for(i=1;i<3;i++){
            load[i] = strtod(strtok(NULL, " \n"), NULL);
        }
        loadAvg1 = load[0];
        loadAvg5 = load[1];
        loadAvg15 = load[2];
       printf("%.2f %.2f %.2f", loadAvg1, loadAvg5, loadAvg15);
    }
}
}

output: g++ proc.cpp -o procbin

~ >>> ./procbin
0.94 1.80 1.75

comparison with the output of the cat:

~ >>> cat /proc/loadavg
0.94 1.80 1.75 1/815 37376

SeptemberHX commented 4 years ago

What's your OS ? I think I need to debug by installing the os in virtual machine.

JunioCalu commented 4 years ago

Qual é o seu sistema operacional? Eu acho que preciso depurar instalando o sistema operacional na máquina virtual.

I'm using Manjaro an Archlinux distribution, but both systems are the same, the packages are in the same version