From: Joao Eduardo Luis Date: Wed, 27 Jan 2016 10:26:13 +0000 (+0000) Subject: common/bit_str: Helper functions to output bitmask's flags X-Git-Tag: v11.1.0~400^2~17 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=4bdb7de443cd6ca3c9dc988dd11edc4a22263e0f;p=ceph.git common/bit_str: Helper functions to output bitmask's flags These functions will take a given function 'f' as parameter, and will call 'f' for each bit that is set on a provided bitmask. The responsibility of printing the appropriate flag name falls on 'f'. Signed-off-by: Joao Eduardo Luis --- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c42af1d1208a..ce4056c9c484 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -436,6 +436,7 @@ set(libcommon_files common/TracepointProvider.cc common/Cycles.cc common/scrub_types.cc + common/bit_str.cc osdc/Striper.cc osdc/Objecter.cc common/Graylog.cc diff --git a/src/common/bit_str.cc b/src/common/bit_str.cc new file mode 100644 index 000000000000..9802f4af03f5 --- /dev/null +++ b/src/common/bit_str.cc @@ -0,0 +1,61 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2016 SUSE LINUX GmbH + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ +#include "common/bit_str.h" +#include "common/Formatter.h" +#include "include/assert.h" + +static void _dump_bit_str( + uint64_t bits, + std::ostream *out, + ceph::Formatter *f, + std::function func) +{ + uint64_t b = bits; + int cnt = 0; + bool outted = false; + + while (b && cnt < 64) { + uint64_t r = bits & (1ULL << cnt++); + if (r) { + if (out) { + if (outted) + *out << ","; + *out << func(r); + } else { + assert(f != NULL); + f->dump_stream("bit_flag") << func(r); + } + outted = true; + } + b >>= 1; + } + if (!outted && out) + *out << "none"; +} + +void print_bit_str( + uint64_t bits, + std::ostream &out, + std::function func) +{ + _dump_bit_str(bits, &out, NULL, func); +} + +void dump_bit_str( + uint64_t bits, + ceph::Formatter *f, + std::function func) +{ + _dump_bit_str(bits, NULL, f, func); +} diff --git a/src/common/bit_str.h b/src/common/bit_str.h new file mode 100644 index 000000000000..656745440ca4 --- /dev/null +++ b/src/common/bit_str.h @@ -0,0 +1,35 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2016 SUSE LINUX GmbH + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ +#ifndef CEPH_COMMON_BIT_STR_H +#define CEPH_COMMON_BIT_STR_H + +#include +#include +#include + +namespace ceph { + class Formatter; +} + +extern void print_bit_str( + uint64_t bits, + std::ostream &out, + std::function func); + +extern void dump_bit_str( + uint64_t bits, + ceph::Formatter *f, + std::function func); + +#endif /* CEPH_COMMON_BIT_STR_H */