view probes/memory.c @ 9:6f2e13f5fcfd

Add the memory probe
author Louis Opter <louis@dotcloud.com>
date Sun, 02 Jan 2011 01:50:27 +0100
parents
children 3a1977ecccc7
line wrap: on
line source

#include <sys/types.h>

#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>

#include "lxcstats.h"
#include "_lxcstats.h"

static int
read_stat(struct lxcst *c)
{
    char    path[PATH_MAX];
    char    *values;
    char    *fields[12];
    int     ret;

    _lxcst_join_path(path, sizeof(path), c->cgroup_dir, "memory.stat");
    ret = _lxcst_read_file(path, &values);
    if (ret > 0) {
        values[ret - 1] = '\0'; /* Replace the last \n by a \0 */
        if (_lxcst_strsplit(values, fields, ARRAY_SIZE(fields)) == ARRAY_SIZE(fields)) {
            errno = 0;
            c->memory.cached = strtoull(fields[1], NULL, 10);
            c->memory.used = strtoull(fields[3], NULL, 10);
            c->memory.mapped = strtoull(fields[5], NULL, 10);
            c->memory.swapped = strtoull(fields[11], NULL, 10);
            if (!errno) {
                free(values);
                return (0);
            }
        }
        free(values);
    }

    memset(&c->memory, 0, sizeof(c->memory));
    return (-1);
}

int
_lxcst_probe_memory(struct lxcst *c)
{
    assert(c);

    return (read_stat(c));
}