oomd: switch system context parsing to use /proc/meminfo
Makes it easier in the next commits to unify on one way to read swap and memory info.
This commit is contained in:
+39
-21
@@ -201,6 +201,41 @@ int procfs_cpu_get_usage(nsec_t *ret) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int convert_meminfo_value_to_uint64_bytes(char *word, uint64_t *ret) {
|
||||
char *digits, *e;
|
||||
uint64_t v;
|
||||
size_t n;
|
||||
int r;
|
||||
|
||||
assert(word);
|
||||
assert(ret);
|
||||
|
||||
/* Determine length of numeric value */
|
||||
n = strspn(word, WHITESPACE);
|
||||
digits = word + n;
|
||||
n = strspn(digits, DIGITS);
|
||||
if (n == 0)
|
||||
return -EINVAL;
|
||||
e = digits + n;
|
||||
|
||||
/* Ensure the line ends in " kB" */
|
||||
n = strspn(e, WHITESPACE);
|
||||
if (n == 0)
|
||||
return -EINVAL;
|
||||
if (!streq(e + n, "kB"))
|
||||
return -EINVAL;
|
||||
|
||||
*e = 0;
|
||||
r = safe_atou64(digits, &v);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (v == UINT64_MAX)
|
||||
return -EINVAL;
|
||||
|
||||
*ret = v * 1024U;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int procfs_memory_get(uint64_t *ret_total, uint64_t *ret_used) {
|
||||
uint64_t mem_total = UINT64_MAX, mem_free = UINT64_MAX;
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
@@ -213,8 +248,7 @@ int procfs_memory_get(uint64_t *ret_total, uint64_t *ret_used) {
|
||||
for (;;) {
|
||||
_cleanup_free_ char *line = NULL;
|
||||
uint64_t *v;
|
||||
char *p, *e;
|
||||
size_t n;
|
||||
char *p;
|
||||
|
||||
r = read_line(f, LONG_LINE_MAX, &line);
|
||||
if (r < 0)
|
||||
@@ -233,25 +267,9 @@ int procfs_memory_get(uint64_t *ret_total, uint64_t *ret_used) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Determine length of numeric value */
|
||||
n = strspn(p, DIGITS);
|
||||
if (n == 0)
|
||||
return -EINVAL;
|
||||
e = p + n;
|
||||
|
||||
/* Ensure the line ends in " kB" */
|
||||
n = strspn(e, WHITESPACE);
|
||||
if (n == 0)
|
||||
return -EINVAL;
|
||||
if (!streq(e + n, "kB"))
|
||||
return -EINVAL;
|
||||
|
||||
*e = 0;
|
||||
r = safe_atou64(p, v);
|
||||
r = convert_meminfo_value_to_uint64_bytes(p, v);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (*v == UINT64_MAX)
|
||||
return -EINVAL;
|
||||
|
||||
if (mem_total != UINT64_MAX && mem_free != UINT64_MAX)
|
||||
break;
|
||||
@@ -261,8 +279,8 @@ int procfs_memory_get(uint64_t *ret_total, uint64_t *ret_used) {
|
||||
return -EINVAL;
|
||||
|
||||
if (ret_total)
|
||||
*ret_total = mem_total * 1024U;
|
||||
*ret_total = mem_total;
|
||||
if (ret_used)
|
||||
*ret_used = (mem_total - mem_free) * 1024U;
|
||||
*ret_used = mem_total - mem_free;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -15,3 +15,6 @@ int procfs_memory_get(uint64_t *ret_total, uint64_t *ret_used);
|
||||
static inline int procfs_memory_get_used(uint64_t *ret) {
|
||||
return procfs_memory_get(NULL, ret);
|
||||
}
|
||||
|
||||
/* This function destroys "word" (it'll be truncated to perform conversion) */
|
||||
int convert_meminfo_value_to_uint64_bytes(char *word, uint64_t *ret);
|
||||
|
||||
Reference in New Issue
Block a user