#define PROC_MEM_INFO_MEM_AVAILABLE "MemAvailable"
#define PROC_MEM_INFO_MEM_FREE "MemFree"
-static inline int memory_read_val_from_proc_node(uint32_t val_id)
+static inline int memory_read_val_from_proc_node(uint32_t val_id, u_int64_t *val)
{
+ int ret;
+
switch (val_id) {
case MEMORY_TOTAL:
- return kernel_get_memory_total();
+ ret = kernel_get_memory_total(val);
+ break;
case MEMORY_AVAILABLE:
- return kernel_get_memory_info(PROC_MEM_INFO_MEM_AVAILABLE);
+ ret = kernel_get_memory_info(PROC_MEM_INFO_MEM_AVAILABLE, val);
+ break;
case MEMORY_FREE:
- return kernel_get_memory_info(PROC_MEM_INFO_MEM_FREE);
+ ret = kernel_get_memory_info(PROC_MEM_INFO_MEM_FREE, val);
+ break;
+ default:
+ _E("wrong memory resource attribute\n");
+ ret = -EINVAL;
}
- return -EINVAL;
+
+ return ret;
}
static int memory_get_total_memory(const struct resource *res,
const struct resource_attribute *attr,
void **data)
{
- int val;
+ u_int64_t val;
+ int ret;
if (!res || !attr || !data)
return -EINVAL;
- val = memory_read_val_from_proc_node(attr->id);
- if (val < 0)
+ ret = memory_read_val_from_proc_node(attr->id, &val);
+ if (ret < 0)
return -EINVAL;
*data = (void *)(intptr_t)val;
const struct resource_attribute *attr,
void **data)
{
- int val;
+ u_int64_t val;
+ int ret;
if (!res || !attr || !data)
return -EINVAL;
- val = memory_read_val_from_proc_node(attr->id);
- if (val < 0)
+ ret = memory_read_val_from_proc_node(attr->id, &val);
+ if (ret < 0)
return -EINVAL;
*data = (void *)(intptr_t)val;
const struct resource_attribute *attr,
void **data)
{
- int val;
+ u_int64_t val;
+ int ret;
if (!res || !attr || !data)
return -EINVAL;
- val = memory_read_val_from_proc_node(attr->id);
- if (val < 0)
+ ret = memory_read_val_from_proc_node(attr->id, &val);
+ if (ret < 0)
return -EINVAL;
*data = (void *)(intptr_t)val;
{
.name = "MEMORY_TOTAL",
.id = MEMORY_TOTAL,
- .type = DATA_TYPE_INT,
+ .type = DATA_TYPE_UINT64,
.ops = {
.get = memory_get_total_memory,
},
}, {
.name = "MEMORY_AVAILABLE",
.id = MEMORY_AVAILABLE,
- .type = DATA_TYPE_INT,
+ .type = DATA_TYPE_UINT64,
.ops = {
.get = memory_get_available_memory,
},
}, {
.name = "MEMORY_FREE",
.id = MEMORY_FREE,
- .type = DATA_TYPE_INT,
+ .type = DATA_TYPE_UINT64,
.ops = {
.get = memory_get_free_memory,
}
ctx->prev_total_time = get_total_cpu_time();
ctx->pid = (pid_t)(intptr_t)res->user_data;
+ ret = kernel_get_memory_total(&ctx->total_memory);
+ if (ret < 0) {
+ free(ctx);
+ return -EINVAL;
+ }
+
/* update initial status */
ret = update_taskstats(ctx);
if (ret < 0) {
}
memcpy(ctx->comm, stats.ac_comm, TS_COMM_LEN);
- ctx->total_memory = kernel_get_memory_total() * 1024ULL;
-
res->priv = ctx;
return 0;
#include <glib.h>
#include <stdio.h>
+#include <string.h>
#include <util/common.h>
#include <util/log.h>
return ret;
}
-int kernel_get_memory_info(const char *key)
+int kernel_get_memory_info(const char *key, u_int64_t *val)
{
- int ret = -EINVAL;
char buf[BUFF_MAX];
- char str[BUFF_MAX];
FILE *fp = NULL;
fp = fopen("/proc/meminfo", "r");
while (fgets(buf, BUFF_MAX, fp)) {
if (!strncmp(buf, key, strlen(key))) {
- sscanf(buf, "%s %d", str, &ret);
+ sscanf(buf, "%*s %"PRIu64, val);
+ if (strstr(buf, "kB"))
+ *val *= 1024;
break;
}
}
fclose(fp);
- return ret;
+ return 0;
}
-int kernel_get_memory_total(void)
+int kernel_get_memory_total(u_int64_t *val)
{
static u_int64_t mem_total = 0;
+ int ret;
- if (!mem_total)
- mem_total = kernel_get_memory_info("MemTotal");
+ if (!mem_total) {
+ ret = kernel_get_memory_info("MemTotal", &mem_total);
+ if (ret < 0) {
+ _E("failed to get system total memory\n");
+ return -EINVAL;
+ }
+ }
+
+ *val = mem_total;
- return mem_total;
+ return 0;
}