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

+1 -1
View File
@@ -24,7 +24,7 @@ if system == 'linux'
endif
sysinfo_includes += 'unix'
sysinfo_sources += ['unix/modern-backend.c', 'unix/storage.c']
sysinfo_sources += ['unix/modern-backend.c', 'unix/storage.c', 'unix/memory.c']
elif system == 'freebsd' or system == 'gnu' or system.startswith('gnu/')
sysinfo_includes += ['shared', 'unix']
+104
View File
@@ -0,0 +1,104 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
#include <errno.h>
#include <string.h>
#include <glib.h>
#include "../format.h"
#include "memory.h"
enum
{
MEM_TOTAL, MEM_AVAILABLE, MEM_FREE, BUFFERS, CACHED,
S_RECLAIMABLE, SHMEM, SWAP_TOTAL, SWAP_FREE, FIELD_COUNT
};
static gboolean
parse_kib (const char *text, guint64 *value)
{
char *end;
guint64 parsed;
while (g_ascii_isspace (*text))
text++;
if (!g_ascii_isdigit (*text))
return FALSE;
errno = 0;
parsed = g_ascii_strtoull (text, &end, 10);
if (errno || parsed > G_MAXUINT64 / 1024)
return FALSE;
while (g_ascii_isspace (*end))
end++;
if (strncmp (end, "kB", 2) != 0)
return FALSE;
end += 2;
while (g_ascii_isspace (*end))
end++;
if (*end)
return FALSE;
*value = parsed;
return TRUE;
}
char *
sysinfo_linux_format_meminfo (const char *contents)
{
static const char *keys[FIELD_COUNT] = {
"MemTotal:", "MemAvailable:", "MemFree:", "Buffers:", "Cached:",
"SReclaimable:", "Shmem:", "SwapTotal:", "SwapFree:"
};
guint64 values[FIELD_COUNT] = {0};
gboolean present[FIELD_COUNT] = {FALSE};
char **lines;
guint i, j;
guint64 available;
char *memory;
gboolean estimated = FALSE;
if (!contents)
return NULL;
lines = g_strsplit (contents, "\n", -1);
for (i = 0; lines[i]; i++)
{
for (j = 0; j < FIELD_COUNT; j++)
{
if (g_str_has_prefix (lines[i], keys[j]))
{
present[j] = parse_kib (lines[i] + strlen (keys[j]), &values[j]);
break;
}
}
}
g_strfreev (lines);
if (!present[MEM_TOTAL] || !values[MEM_TOTAL])
return NULL;
if (present[MEM_AVAILABLE])
available = values[MEM_AVAILABLE];
else if (present[MEM_FREE])
{
/* Older kernels lack MemAvailable. This estimate excludes shared
* memory from the reclaimable-cache sum and is labelled as such. */
available = values[MEM_FREE] + values[BUFFERS] + values[CACHED] + values[S_RECLAIMABLE];
available -= MIN (available, values[SHMEM]);
estimated = TRUE;
}
else
return NULL;
available = MIN (available, values[MEM_TOTAL]);
memory = sysinfo_format_usage (values[MEM_TOTAL] * 1024, available * 1024);
if (memory && estimated)
{
char *labelled = g_strdup_printf ("%s (estimated available)", memory);
g_free (memory);
memory = labelled;
}
if (memory && present[SWAP_TOTAL] && present[SWAP_FREE] && values[SWAP_TOTAL])
{
char *swap = sysinfo_format_usage (values[SWAP_TOTAL] * 1024, values[SWAP_FREE] * 1024);
if (swap)
{
char *result = g_strdup_printf ("%s \xC2\xB7 Swap: %s", memory, swap);
g_free (memory);
g_free (swap);
return result;
}
}
return memory;
}
+8
View File
@@ -0,0 +1,8 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
#ifndef SYSINFO_LINUX_MEMORY_H
#define SYSINFO_LINUX_MEMORY_H
/* Parse a /proc/meminfo snapshot; the caller owns the returned string. */
char *sysinfo_linux_format_meminfo (const char *contents);
#endif
+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;
}
+11
View File
@@ -505,7 +505,18 @@ sysinfo_backend_get_os (void)
char *
sysinfo_backend_get_cpu (void)
{
static const WCHAR cpu_key[] = L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0";
DWORD mhz = 0;
ensure_cpu_snapshot ();
if (!snapshot.cpu)
return NULL;
/* The registry's reported MHz is not a guaranteed live or turbo clock. */
if (read_registry_dword (HKEY_LOCAL_MACHINE, cpu_key, L"~MHz", &mhz) && mhz && mhz <= 100000)
{
if (mhz >= 1000)
return g_strdup_printf ("%s" SYSINFO_SEP "reported %.2f GHz", snapshot.cpu, (double) mhz / 1000.0);
return g_strdup_printf ("%s" SYSINFO_SEP "reported %lu MHz", snapshot.cpu, (unsigned long) mhz);
}
return g_strdup (snapshot.cpu);
}