fix CPU frequency+linux memory fallback

This commit is contained in:
deepend committed 2026-12-04 18:28:37 -06:00
1 parent 487b7d8721
commit 27f5730f50
5 files changed
+192 -51

No files matched your search

+68 -50
View File
@@ -27,6 +27,7 @@
#include "../format.h"
#include "../sysinfo-backend.h"
#include "memory.h"
#define SYSINFO_SEP " \xC2\xB7 "
@@ -702,10 +703,73 @@ sysinfo_backend_get_os (void)
return g_strdup (snapshot.os);
}
static double
read_cpu_frequency_mhz (const char **kind)
{
static const char *paths[] = {
"/sys/devices/system/cpu/cpu0/cpufreq/base_frequency",
"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"
};
guint i;
FILE *fp;
char line[1024];
for (i = 0; i < G_N_ELEMENTS (paths); i++)
{
char *text = read_trimmed_file (paths[i]);
char *end;
guint64 khz;
if (!text)
continue;
errno = 0;
khz = g_ascii_strtoull (text, &end, 10);
if (!errno && end != text && !*end && khz > 0 && khz <= 100000000)
{
g_free (text);
*kind = i == 0 ? "CPU0 base" : "CPU0 max";
return (double) khz / 1000.0;
}
g_free (text);
}
fp = fopen ("/proc/cpuinfo", "r");
if (!fp)
return 0;
while (fgets (line, sizeof (line), fp))
{
char *colon = strchr (line, ':');
if (colon)
{
char *end;
double mhz;
*colon = '\0';
if (g_ascii_strcasecmp (g_strstrip (line), "cpu MHz") != 0)
continue;
mhz = g_ascii_strtod (colon + 1, &end);
if (end != colon + 1 && mhz > 0 && mhz <= 100000)
{
fclose (fp);
*kind = "sampled";
return mhz;
}
}
}
fclose (fp);
return 0;
}
char *
sysinfo_backend_get_cpu (void)
{
const char *kind = NULL;
double mhz;
ensure_cpu_snapshot ();
if (!snapshot.cpu)
return NULL;
mhz = read_cpu_frequency_mhz (&kind);
if (mhz >= 1000)
return g_strdup_printf ("%s" SYSINFO_SEP "%s %.2f GHz", snapshot.cpu, kind, mhz / 1000.0);
if (mhz > 0)
return g_strdup_printf ("%s" SYSINFO_SEP "%s %.0f MHz", snapshot.cpu, kind, mhz);
return g_strdup (snapshot.cpu);
}
@@ -733,59 +797,13 @@ sysinfo_backend_get_sound (void)
char *
sysinfo_backend_get_memory (void)
{
FILE *fp;
char line[512];
guint64 mem_total_kib = 0;
guint64 mem_available_kib = 0;
guint64 swap_total_kib = 0;
guint64 swap_free_kib = 0;
char *contents = NULL;
char *memory;
fp = fopen ("/proc/meminfo", "r");
if (!fp)
{
if (!g_file_get_contents ("/proc/meminfo", &contents, NULL, NULL))
return NULL;
}
while (fgets (line, sizeof (line), fp) != NULL)
{
if (g_str_has_prefix (line, "MemTotal:"))
{
mem_total_kib = g_ascii_strtoull (line + strlen ("MemTotal:"), NULL, 10);
}
else if (g_str_has_prefix (line, "MemAvailable:"))
{
mem_available_kib = g_ascii_strtoull (line + strlen ("MemAvailable:"), NULL, 10);
}
else if (g_str_has_prefix (line, "SwapTotal:"))
{
swap_total_kib = g_ascii_strtoull (line + strlen ("SwapTotal:"), NULL, 10);
}
else if (g_str_has_prefix (line, "SwapFree:"))
{
swap_free_kib = g_ascii_strtoull (line + strlen ("SwapFree:"), NULL, 10);
}
}
fclose (fp);
if (mem_total_kib == 0)
{
return NULL;
}
memory = format_usage (mem_total_kib * 1024, mem_available_kib * 1024);
if (memory && swap_total_kib != 0)
{
char *swap = format_usage (swap_total_kib * 1024, swap_free_kib * 1024);
if (swap)
{
char *result = g_strdup_printf ("%s" SYSINFO_SEP "Swap: %s", memory, swap);
g_free (memory);
g_free (swap);
return result;
}
}
memory = sysinfo_linux_format_meminfo (contents);
g_free (contents);
return memory;
}