From 3d74230d1c0fbfa15487e2a90ac60b883476e840 Mon Sep 17 00:00:00 2001 From: Joao Eduardo Luis Date: Thu, 18 Sep 2014 16:32:20 +0100 Subject: [PATCH] common: util: add get_fs_stats() function simplifies the task of obtaining available/used disk space, as well as used available percentage. Signed-off-by: Joao Eduardo Luis --- src/common/util.cc | 22 ++++++++++++++++++++++ src/include/util.h | 21 +++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/common/util.cc b/src/common/util.cc index ab417befef691..212384b51942b 100644 --- a/src/common/util.cc +++ b/src/common/util.cc @@ -18,6 +18,10 @@ #include "common/errno.h" #include "common/strtol.h" +#ifdef HAVE_SYS_VFS_H +#include +#endif + // test if an entire buf is zero in 8-byte chunks bool buf_is_zero(const char *buf, size_t len) { @@ -104,3 +108,21 @@ int64_t unit_to_bytesize(string val, ostream *pss) } return (r * (1LL << modifier)); } + +int get_fs_stats(ceph_data_stats_t &stats, const char *path) +{ + if (!path) + return -EINVAL; + + struct statfs stbuf; + int err = ::statfs(path, &stbuf); + if (err < 0) { + return -errno; + } + + stats.byte_total = stbuf.f_blocks * stbuf.f_bsize; + stats.byte_used = (stbuf.f_blocks - stbuf.f_bfree) * stbuf.f_bsize; + stats.byte_avail = stbuf.f_bavail * stbuf.f_bsize; + stats.avail_percent = (((float)stats.byte_avail/stats.byte_total)*100); + return 0; +} diff --git a/src/include/util.h b/src/include/util.h index a08bb459e4a84..4e4476ad644c9 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -4,6 +4,7 @@ * Ceph - scalable distributed file system * * Copyright (C) 2012 Inktank Storage, Inc. + * Copyright (C) 2014 Red Hat * * This is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -15,9 +16,29 @@ // is buf~len completely zero (in 8-byte chunks) +#include "common/Formatter.h" #include "include/types.h" bool buf_is_zero(const char *buf, size_t len); int64_t unit_to_bytesize(string val, ostream *pss); + +struct ceph_data_stats +{ + uint64_t byte_total; + uint64_t byte_used; + uint64_t byte_avail; + int avail_percent; + + void dump(Formatter *f) const { + assert(f != NULL); + f->dump_int("total", byte_total); + f->dump_int("used", byte_used); + f->dump_int("avail", byte_avail); + f->dump_int("avail_percent", avail_percent); + } +}; +typedef struct ceph_data_stats ceph_data_stats_t; + +int get_fs_stats(ceph_data_stats_t &stats, const char *path); #endif /* CEPH_UTIL_H */ -- 2.39.5