mirror of
https://github.com/ZoiteChat/zoitechat.git
synced 2026-12-05 22:42:06 +00:00
104 lines
2.5 KiB
C
104 lines
2.5 KiB
C
/* 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;
|
||
|
|
}
|