From bd3d91617c9448074267869d6ec6f21df2ba2d1f Mon Sep 17 00:00:00 2001 From: Sung-hun Kim Date: Mon, 30 May 2022 15:59:21 +0900 Subject: [PATCH] resource: memory: Add MEMORY_ATTR_SWAP_TOTAL and MEMORY_ATTR_SWAP_FREE attributes Change-Id: I8f6e3ff274c429fe2e5d2f9378ea4e8ae18dccaf Signed-off-by: Sung-hun Kim --- lib/resource-monitor/resource-monitor.h | 2 ++ src/resource/resource-memory.c | 39 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/lib/resource-monitor/resource-monitor.h b/lib/resource-monitor/resource-monitor.h index 4644d96..5bfbb3f 100644 --- a/lib/resource-monitor/resource-monitor.h +++ b/lib/resource-monitor/resource-monitor.h @@ -86,6 +86,8 @@ extern "C" { #define MEMORY_ATTR_CACHED BIT(4) /* DATA_TYPE_UINT64 */ #define MEMORY_ATTR_CMA_TOTAL BIT(5) /* DATA_TYPE_UINT64 */ #define MEMORY_ATTR_CMA_FREE BIT(6) /* DATA_TYPE_UINT64 */ +#define MEMORY_ATTR_SWAP_TOTAL BIT(7) /* DATA_TYPE_UINT64 */ +#define MEMORY_ATTR_SWAP_FREE BIT(8) /* DATA_TYPE_UINT64 */ /* Battery Resource */ #define BATTERY_ATTR_CAPACITY BIT(0) /* DATA_TYPE_INT */ diff --git a/src/resource/resource-memory.c b/src/resource/resource-memory.c index f0b8782..48c627d 100644 --- a/src/resource/resource-memory.c +++ b/src/resource/resource-memory.c @@ -38,6 +38,7 @@ #define PROC_MEM_INFO_BUFFER "Buffers" #define PROC_MEM_INFO_CACHED "Cached" #define PROC_MEM_INFO_CMA_FREE "CmaFree" +#define PROC_MEM_INFO_SWAP_FREE "SwapFree" static int memory_get_cma_total(u_int64_t *val) { @@ -57,6 +58,24 @@ static int memory_get_cma_total(u_int64_t *val) return 0; } +static int memory_get_swap_total(u_int64_t *val) +{ + static u_int64_t swap_total = 0; + int ret; + + if (!swap_total) { + ret = kernel_get_memory_info("SwapTotal", &swap_total); + if (ret < 0) { + _E("failed to get system SWAP total memory\n"); + return -EINVAL; + } + } + + *val = swap_total; + + return 0; +} + static int memory_get_memory_info(const struct resource *res, const struct resource_attribute *attr, void *data) @@ -89,6 +108,12 @@ static int memory_get_memory_info(const struct resource *res, case MEMORY_ATTR_CMA_FREE: ret = kernel_get_memory_info(PROC_MEM_INFO_CMA_FREE, val); break; + case MEMORY_ATTR_SWAP_TOTAL: + ret = memory_get_swap_total(val); + break; + case MEMORY_ATTR_SWAP_FREE: + ret = kernel_get_memory_info(PROC_MEM_INFO_SWAP_FREE, val); + break; default: _E("wrong memory resource attribute\n"); ret = -EINVAL; @@ -149,6 +174,20 @@ static const struct resource_attribute memory_attrs[] = { .ops = { .get = memory_get_memory_info, } + }, { + .name = "MEMORY_ATTR_SWAP_TOTAL", + .id = MEMORY_ATTR_SWAP_TOTAL, + .type = DATA_TYPE_UINT64, + .ops = { + .get = memory_get_memory_info, + } + }, { + .name = "MEMORY_ATTR_SWAP_FREE", + .id = MEMORY_ATTR_SWAP_FREE, + .type = DATA_TYPE_UINT64, + .ops = { + .get = memory_get_memory_info, + } }, }; -- 2.7.4