Skip to content
This repository has been archived by the owner on Jun 2, 2023. It is now read-only.

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

Open
JunioCalu opened this issue Jun 21, 2020 · 6 comments

Comments

@JunioCalu
Copy link

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));

@JunioCalu JunioCalu changed the title It is not possible to read /proc/loadavg on Archlinux using gcc 10.0.1 It is not possible to read /proc/loadavg on Archlinux using gcc 10.1.0 Jun 21, 2020
@SeptemberHX
Copy link
Owner

SeptemberHX commented Jun 21, 2020

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

@JunioCalu
Copy link
Author

are six values:

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

@JunioCalu
Copy link
Author

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

one line

@JunioCalu
Copy link
Author

JunioCalu commented Jun 21, 2020

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

@JunioCalu JunioCalu changed the title It is not possible to read /proc/loadavg on Archlinux using gcc 10.1.0 It is not possible to read /proc/loadavg on Archlinux using gcc 10.1.0 and Qt 5.15.0 Jun 21, 2020
@SeptemberHX
Copy link
Owner

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

@JunioCalu
Copy link
Author

JunioCalu commented Jun 22, 2020

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants