summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--procps/free.c30
1 files changed, 17 insertions, 13 deletions
diff --git a/procps/free.c b/procps/free.c
index 3acfc40..1e5d367 100644
--- a/procps/free.c
+++ b/procps/free.c
@@ -44,6 +44,7 @@ struct globals {
#else
# define G_unit_steps 10
#endif
+ unsigned long cached_kb, available_kb, reclaimable_kb;
};
/* Because of NOFORK, "globals" are not in global data */
@@ -53,27 +54,30 @@ static unsigned long long scale(struct globals *g, unsigned long d)
}
/* NOINLINE reduces main() stack usage, which makes code smaller (on x86 at least) */
-static NOINLINE unsigned int parse_meminfo(unsigned long *cached_kb, unsigned long *available_kb)
+static NOINLINE unsigned int parse_meminfo(struct globals *g)
{
char buf[60]; /* actual lines we expect are ~30 chars or less */
FILE *fp;
- int seen_cached_and_available;
+ int seen_cached_and_available_and_reclaimable;
fp = xfopen_for_read("/proc/meminfo");
- *cached_kb = *available_kb = 0;
- seen_cached_and_available = 2;
+ g->cached_kb = g->available_kb = g->reclaimable_kb = 0;
+ seen_cached_and_available_and_reclaimable = 3;
while (fgets(buf, sizeof(buf), fp)) {
- if (sscanf(buf, "Cached: %lu %*s\n", cached_kb) == 1)
- if (--seen_cached_and_available == 0)
+ if (sscanf(buf, "Cached: %lu %*s\n", &g->cached_kb) == 1)
+ if (--seen_cached_and_available_and_reclaimable == 0)
break;
- if (sscanf(buf, "MemAvailable: %lu %*s\n", available_kb) == 1)
- if (--seen_cached_and_available == 0)
+ if (sscanf(buf, "MemAvailable: %lu %*s\n", &g->available_kb) == 1)
+ if (--seen_cached_and_available_and_reclaimable == 0)
+ break;
+ if (sscanf(buf, "SReclaimable: %lu %*s\n", &g->reclaimable_kb) == 1)
+ if (--seen_cached_and_available_and_reclaimable == 0)
break;
}
/* Have to close because of NOFORK */
fclose(fp);
- return seen_cached_and_available == 0;
+ return seen_cached_and_available_and_reclaimable == 0;
}
int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
@@ -82,7 +86,6 @@ int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
struct globals G;
struct sysinfo info;
unsigned long long cached, cached_plus_free, available;
- unsigned long cached_kb, available_kb;
int seen_available;
#if ENABLE_DESKTOP
@@ -118,10 +121,11 @@ int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
/* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
/* Extract cached and memavailable from /proc/meminfo and convert to mem_units */
- seen_available = parse_meminfo(&cached_kb, &available_kb);
- available = ((unsigned long long) available_kb * 1024) / G.mem_unit;
- cached = ((unsigned long long) cached_kb * 1024) / G.mem_unit;
+ seen_available = parse_meminfo(&G);
+ available = ((unsigned long long) G.available_kb * 1024) / G.mem_unit;
+ cached = ((unsigned long long) G.cached_kb * 1024) / G.mem_unit;
cached += info.bufferram;
+ cached += ((unsigned long long) G.reclaimable_kb * 1024) / G.mem_unit;
cached_plus_free = cached + info.freeram;
#define FIELDS_6 "%12llu %11llu %11llu %11llu %11llu %11llu\n"