Add support for MemAvailable (!10)
MemAvailable is in /proc/meminfo since Linux 3.14. This aligns the reported memory usage with the memory usage reported by xfce4-systemload-plugin. Closes !10
This commit is contained in:
@@ -219,7 +219,8 @@ gboolean get_cpu_usage (gushort *cpu_count, gfloat *cpu_user, gfloat *cpu_system
|
||||
/* vmtotal values in #pg */
|
||||
#define pagetok(nb) ((nb) * (getpagesize()))
|
||||
|
||||
gboolean get_memory_usage (guint64 *memory_total, guint64 *memory_free, guint64 *memory_cache, guint64 *memory_buffers, guint64 *swap_total, guint64 *swap_free)
|
||||
gboolean
|
||||
get_memory_usage (guint64 *memory_total, guint64 *memory_available, guint64 *memory_free, guint64 *memory_cache, guint64 *memory_buffers, guint64 *swap_total, guint64 *swap_free)
|
||||
{
|
||||
#ifdef __OpenBSD__
|
||||
int mib[] = {CTL_VM, VM_UVMEXP};
|
||||
@@ -250,6 +251,7 @@ gboolean get_memory_usage (guint64 *memory_total, guint64 *memory_free, guint64
|
||||
*memory_cache = 0;
|
||||
*memory_buffers = pagetok(vmtotal.t_rm - vmtotal.t_arm);
|
||||
#endif
|
||||
*memory_available = *memory_free + *memory_cache + *memory_buffers;
|
||||
|
||||
/* get swap stats */
|
||||
if ((nswap = swapctl(SWAP_NSWAP, 0, 0)) == 0)
|
||||
|
||||
@@ -64,7 +64,7 @@ get_mem_by_pages (const gchar *name)
|
||||
}
|
||||
|
||||
gboolean
|
||||
get_memory_usage (guint64 *memory_total, guint64 *memory_free, guint64 *memory_cache, guint64 *memory_buffers, guint64 *swap_total, guint64 *swap_free)
|
||||
get_memory_usage (guint64 *memory_total, guint64 *memory_available, guint64 *memory_free, guint64 *memory_cache, guint64 *memory_buffers, guint64 *swap_total, guint64 *swap_free)
|
||||
{
|
||||
/* Get memory usage */
|
||||
{
|
||||
@@ -72,6 +72,7 @@ get_memory_usage (guint64 *memory_total, guint64 *memory_free, guint64 *memory_c
|
||||
*memory_free = get_mem_by_pages ("vm.stats.vm.v_free_count");
|
||||
*memory_cache = get_mem_by_pages ("vm.stats.vm.v_inactive_count");
|
||||
*memory_buffers = get_mem_by_bytes ("vfs.bufspace");
|
||||
*memory_available = *memory_free + *memory_cache + *memory_buffers;
|
||||
}
|
||||
|
||||
/* Get swap usage */
|
||||
|
||||
@@ -22,42 +22,49 @@ static gushort _cpu_count = 0;
|
||||
static gulong jiffies_total_delta = 0;
|
||||
|
||||
gboolean
|
||||
get_memory_usage (guint64 *memory_total, guint64 *memory_free, guint64 *memory_cache, guint64 *memory_buffers, guint64 *swap_total, guint64 *swap_free)
|
||||
get_memory_usage (guint64 *memory_total, guint64 *memory_available, guint64 *memory_free, guint64 *memory_cache, guint64 *memory_buffers, guint64 *swap_total, guint64 *swap_free)
|
||||
{
|
||||
FILE *file;
|
||||
gchar buffer[1024];
|
||||
gchar *filename = "/proc/meminfo";
|
||||
gushort found = 0;
|
||||
gulong mem_total = 0,
|
||||
mem_free = 0,
|
||||
mem_avail = 0,
|
||||
mem_cached = 0,
|
||||
mem_buffers = 0,
|
||||
swp_total = 0,
|
||||
swp_free = 0;
|
||||
|
||||
if ((file = fopen (filename, "r")) == NULL)
|
||||
return FALSE;
|
||||
|
||||
*memory_total = 0;
|
||||
*memory_free = 0;
|
||||
*memory_cache = 0;
|
||||
*memory_buffers = 0;
|
||||
*swap_total = 0;
|
||||
*swap_free = 0;
|
||||
|
||||
while (found < 6 && fgets (buffer, sizeof(buffer), file) != NULL)
|
||||
if ((file = fopen (filename, "r")) != NULL)
|
||||
{
|
||||
found += sscanf (buffer, "MemTotal:\t%llu kB", (unsigned long long*)memory_total);
|
||||
found += sscanf (buffer, "MemFree:\t%llu kB", (unsigned long long*)memory_free);
|
||||
found += sscanf (buffer, "Cached:\t%llu kB", (unsigned long long*)memory_cache);
|
||||
found += sscanf (buffer, "Buffers:\t%llu kB", (unsigned long long*)memory_buffers);
|
||||
found += sscanf (buffer, "SwapTotal:\t%llu kB", (unsigned long long*)swap_total);
|
||||
found += sscanf (buffer, "SwapFree:\t%llu kB", (unsigned long long*)swap_free);
|
||||
gint found = 0;
|
||||
gchar buffer[256];
|
||||
while (found < 7 && fgets (buffer, sizeof(buffer), file) != NULL)
|
||||
{
|
||||
found += !mem_total ? sscanf (buffer, "MemTotal:\t%lu kB", &mem_total) : 0;
|
||||
found += !mem_free ? sscanf (buffer, "MemFree:\t%lu kB", &mem_free) : 0;
|
||||
found += !mem_avail ? sscanf (buffer, "MemAvailable:\t%lu kB", &mem_avail) : 0; /* Since Linux 3.14 */
|
||||
found += !mem_cached ? sscanf (buffer, "Cached:\t%lu kB", &mem_cached) : 0;
|
||||
found += !mem_buffers ? sscanf (buffer, "Buffers:\t%lu kB", &mem_buffers) : 0;
|
||||
found += !swp_total ? sscanf (buffer, "SwapTotal:\t%lu kB", &swp_total) : 0;
|
||||
found += !swp_free ? sscanf (buffer, "SwapFree:\t%lu kB", &swp_free) : 0;
|
||||
}
|
||||
fclose (file);
|
||||
}
|
||||
fclose (file);
|
||||
|
||||
*memory_total *= 1024;
|
||||
*memory_free *= 1024;
|
||||
*memory_cache *= 1024;
|
||||
*memory_buffers *= 1024;
|
||||
*swap_total *= 1024;
|
||||
*swap_free *= 1024;
|
||||
if (mem_avail == 0)
|
||||
{
|
||||
mem_avail = mem_free + mem_cached + mem_buffers;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
*memory_total = mem_total * 1024;
|
||||
*memory_available = mem_avail * 1024;
|
||||
*memory_free = mem_free * 1024;
|
||||
*memory_cache = mem_cached * 1024;
|
||||
*memory_buffers = mem_buffers * 1024;
|
||||
*swap_total = swp_total * 1024;
|
||||
*swap_free = swp_free * 1024;
|
||||
|
||||
return file ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
|
||||
@@ -25,12 +25,13 @@ static gushort _cpu_count = 0;
|
||||
*/
|
||||
|
||||
gboolean
|
||||
get_memory_usage (guint64 *memory_total, guint64 *memory_free, guint64 *memory_cache, guint64 *memory_buffers, guint64 *swap_total, guint64 *swap_free)
|
||||
get_memory_usage (guint64 *memory_total, guint64 *memory_available, guint64 *memory_free, guint64 *memory_cache, guint64 *memory_buffers, guint64 *swap_total, guint64 *swap_free)
|
||||
{
|
||||
*memory_total = 0;
|
||||
*memory_free = 0;
|
||||
*memory_cache = 0;
|
||||
*memory_buffers = 0;
|
||||
*memory_available = 0;
|
||||
*swap_total = 0;
|
||||
*swap_free = 0;
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ init_stats (void)
|
||||
}
|
||||
|
||||
gboolean
|
||||
get_memory_usage (guint64 *memory_total, guint64 *memory_free, guint64 *memory_cache, guint64 *memory_buffers, guint64 *swap_total, guint64 *swap_free)
|
||||
get_memory_usage (guint64 *memory_total, guint64 *memory_available, guint64 *memory_free, guint64 *memory_cache, guint64 *memory_buffers, guint64 *swap_total, guint64 *swap_free)
|
||||
{
|
||||
kstat_t *ksp;
|
||||
kstat_named_t *knp;
|
||||
@@ -52,6 +52,7 @@ get_memory_usage (guint64 *memory_total, guint64 *memory_free, guint64 *memory_c
|
||||
*memory_free = getpagesize () * knp->value.ui64;
|
||||
*memory_cache = 0;
|
||||
*memory_buffers = 0;
|
||||
*memory_available = *memory_free + *memory_cache + *memory_buffers;
|
||||
|
||||
*swap_total = *swap_free = 0;
|
||||
if ((n = swapctl (SC_GETNSWP, NULL)) > 0)
|
||||
|
||||
@@ -60,6 +60,7 @@ struct _XtmTaskManager
|
||||
gfloat cpu_user;
|
||||
gfloat cpu_system;
|
||||
guint64 memory_total;
|
||||
guint64 memory_available; /* free + cache + buffers + an-OS-specific-value */
|
||||
guint64 memory_free;
|
||||
guint64 memory_cache;
|
||||
guint64 memory_buffers;
|
||||
@@ -367,10 +368,10 @@ xtm_task_manager_get_system_info (XtmTaskManager *manager, guint *num_processes,
|
||||
*num_processes = manager->tasks->len;
|
||||
|
||||
/* Set memory and swap usage */
|
||||
get_memory_usage (&manager->memory_total, &manager->memory_free, &manager->memory_cache, &manager->memory_buffers,
|
||||
get_memory_usage (&manager->memory_total, &manager->memory_available, &manager->memory_free, &manager->memory_cache, &manager->memory_buffers,
|
||||
&manager->swap_total, &manager->swap_free);
|
||||
|
||||
*memory_used = manager->memory_total - manager->memory_free - manager->memory_cache - manager->memory_buffers;
|
||||
*memory_used = manager->memory_total - manager->memory_available;
|
||||
*memory_total = manager->memory_total;
|
||||
*swap_used = manager->swap_total - manager->swap_free;
|
||||
*swap_total = manager->swap_total;
|
||||
|
||||
@@ -39,9 +39,11 @@ struct _Task
|
||||
|
||||
/**
|
||||
* OS specific implementation.
|
||||
*
|
||||
* memory_available = free + cache + buffers + an-OS-specific-value
|
||||
*/
|
||||
|
||||
gboolean get_memory_usage (guint64 *memory_total, guint64 *memory_free, guint64 *memory_cache, guint64 *memory_buffers, guint64 *swap_total, guint64 *swap_free);
|
||||
gboolean get_memory_usage (guint64 *memory_total, guint64 *memory_available, guint64 *memory_free, guint64 *memory_cache, guint64 *memory_buffers, guint64 *swap_total, guint64 *swap_free);
|
||||
gboolean get_cpu_usage (gushort *cpu_count, gfloat *cpu_user, gfloat *cpu_system);
|
||||
gboolean get_task_list (GArray *task_list);
|
||||
gboolean pid_is_sleeping (GPid pid);
|
||||
|
||||
Reference in New Issue
Block a user