procfs-util: expose functionality to query total memory
procfs_memory_get_current is renamed to procfs_memory_get_used, because "current" can mean anything, including total memory, used memory, and free memory, as long as the value is up to date. No functional change.
This commit is contained in:
@@ -201,13 +201,11 @@ int procfs_cpu_get_usage(nsec_t *ret) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int procfs_memory_get_current(uint64_t *ret) {
|
||||
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;
|
||||
int r;
|
||||
|
||||
assert(ret);
|
||||
|
||||
f = fopen("/proc/meminfo", "re");
|
||||
if (!f)
|
||||
return -errno;
|
||||
@@ -262,6 +260,9 @@ int procfs_memory_get_current(uint64_t *ret) {
|
||||
if (mem_free > mem_total)
|
||||
return -EINVAL;
|
||||
|
||||
*ret = (mem_total - mem_free) * 1024U;
|
||||
if (ret_total)
|
||||
*ret_total = mem_total * 1024U;
|
||||
if (ret_used)
|
||||
*ret_used = (mem_total - mem_free) * 1024U;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -11,4 +11,7 @@ int procfs_tasks_get_current(uint64_t *ret);
|
||||
|
||||
int procfs_cpu_get_usage(nsec_t *ret);
|
||||
|
||||
int procfs_memory_get_current(uint64_t *ret);
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user