Skip to content

Commit

Permalink
revise gflags portability
Browse files Browse the repository at this point in the history
Summary: Instead of defining names in the upstream namespace when they are missing, always define names in the folly namespace.

Reviewed By: mzlee

Differential Revision: D38823515

fbshipit-source-id: 9355be0aaf45489f7a72affa01ba6ea7b7df1710
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Aug 20, 2022
1 parent a64dbed commit 4dadde1
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 80 deletions.
29 changes: 15 additions & 14 deletions folly/Benchmark.cpp
Expand Up @@ -39,51 +39,52 @@

using namespace std;

DEFINE_bool(benchmark, false, "Run benchmarks.");
FOLLY_GFLAGS_DEFINE_bool(benchmark, false, "Run benchmarks.");

DEFINE_bool(json, false, "Output in JSON format.");
FOLLY_GFLAGS_DEFINE_bool(json, false, "Output in JSON format.");

DEFINE_bool(bm_estimate_time, false, "Estimate running time");
FOLLY_GFLAGS_DEFINE_bool(bm_estimate_time, false, "Estimate running time");

DEFINE_bool(
FOLLY_GFLAGS_DEFINE_bool(
bm_profile, false, "Run benchmarks with constant number of iterations");

DEFINE_int64(bm_profile_iters, 1000, "Number of iterations for profiling");
FOLLY_GFLAGS_DEFINE_int64(
bm_profile_iters, 1000, "Number of iterations for profiling");

DEFINE_string(
FOLLY_GFLAGS_DEFINE_string(
bm_relative_to,
"",
"Print benchmark results relative to an earlier dump (via --bm_json_verbose)");

DEFINE_string(
FOLLY_GFLAGS_DEFINE_string(
bm_json_verbose,
"",
"File to write verbose JSON format (for BenchmarkCompare / --bm_relative_to). "
"NOTE: this is written independent of the above --json / --bm_relative_to.");

DEFINE_string(
FOLLY_GFLAGS_DEFINE_string(
bm_regex, "", "Only benchmarks whose names match this regex will be run.");

DEFINE_int64(
FOLLY_GFLAGS_DEFINE_int64(
bm_min_usec,
100,
"Minimum # of microseconds we'll accept for each benchmark.");

DEFINE_int32(
FOLLY_GFLAGS_DEFINE_int32(
bm_min_iters, 1, "Minimum # of iterations we'll try for each benchmark.");

DEFINE_int64(
FOLLY_GFLAGS_DEFINE_int64(
bm_max_iters,
1 << 30,
"Maximum # of iterations we'll try for each benchmark.");

DEFINE_int32(
FOLLY_GFLAGS_DEFINE_int32(
bm_max_secs, 1, "Maximum # of seconds we'll spend on each benchmark.");

DEFINE_uint32(
FOLLY_GFLAGS_DEFINE_uint32(
bm_result_width_chars, 76, "Width of results table in characters");

DEFINE_uint32(
FOLLY_GFLAGS_DEFINE_uint32(
bm_max_trials,
1000,
"Maximum number of trials (iterations) executed for each benchmark.");
Expand Down
4 changes: 2 additions & 2 deletions folly/Benchmark.h
Expand Up @@ -36,8 +36,8 @@
#include <boost/function_types/function_arity.hpp>
#include <glog/logging.h>

DECLARE_bool(benchmark);
DECLARE_uint32(bm_result_width_chars);
FOLLY_GFLAGS_DECLARE_bool(benchmark);
FOLLY_GFLAGS_DECLARE_uint32(bm_result_width_chars);

namespace folly {

Expand Down
7 changes: 0 additions & 7 deletions folly/Portability.h
Expand Up @@ -377,13 +377,6 @@ constexpr auto kHasWeakSymbols = false;
#endif
#endif

#if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
namespace FOLLY_GFLAGS_NAMESPACE {}
namespace gflags {
using namespace FOLLY_GFLAGS_NAMESPACE;
} // namespace gflags
#endif

// RTTI may not be enabled for this compilation unit.
#if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
(defined(_MSC_VER) && defined(_CPPRTTI))
Expand Down
2 changes: 1 addition & 1 deletion folly/detail/MemoryIdler.cpp
Expand Up @@ -32,7 +32,7 @@
#include <folly/portability/SysMman.h>
#include <folly/portability/Unistd.h>

DEFINE_bool(
FOLLY_GFLAGS_DEFINE_bool(
folly_memory_idler_purge_arenas,
true,
"if enabled, folly memory-idler purges jemalloc arenas on thread idle");
Expand Down
2 changes: 1 addition & 1 deletion folly/executors/CPUThreadPoolExecutor.cpp
Expand Up @@ -26,7 +26,7 @@
#include <folly/executors/task_queue/UnboundedBlockingQueue.h>
#include <folly/portability/GFlags.h>

DEFINE_bool(
FOLLY_GFLAGS_DEFINE_bool(
dynamic_cputhreadpoolexecutor,
true,
"CPUThreadPoolExecutor will dynamically create and destroy threads");
Expand Down
2 changes: 1 addition & 1 deletion folly/executors/CPUThreadPoolExecutor.h
Expand Up @@ -23,7 +23,7 @@
#include <folly/executors/QueueObserver.h>
#include <folly/executors/ThreadPoolExecutor.h>

DECLARE_bool(dynamic_cputhreadpoolexecutor);
FOLLY_GFLAGS_DECLARE_bool(dynamic_cputhreadpoolexecutor);

namespace folly {

Expand Down
6 changes: 4 additions & 2 deletions folly/executors/Codel.cpp
Expand Up @@ -21,8 +21,10 @@

#include <folly/portability/GFlags.h>

DEFINE_int32(codel_interval, 100, "Codel default interval time in ms");
DEFINE_int32(codel_target_delay, 5, "Target codel queueing delay in ms");
FOLLY_GFLAGS_DEFINE_int32(
codel_interval, 100, "Codel default interval time in ms");
FOLLY_GFLAGS_DEFINE_int32(
codel_target_delay, 5, "Target codel queueing delay in ms");

using namespace std::chrono;

Expand Down
4 changes: 2 additions & 2 deletions folly/executors/GlobalExecutor.cpp
Expand Up @@ -31,12 +31,12 @@

using namespace folly;

DEFINE_uint32(
FOLLY_GFLAGS_DEFINE_uint32(
folly_global_io_executor_threads,
0,
"Number of threads global IOThreadPoolExecutor will create");

DEFINE_uint32(
FOLLY_GFLAGS_DEFINE_uint32(
folly_global_cpu_executor_threads,
0,
"Number of threads global CPUThreadPoolExecutor will create");
Expand Down
2 changes: 1 addition & 1 deletion folly/executors/IOThreadPoolExecutor.cpp
Expand Up @@ -21,7 +21,7 @@
#include <folly/detail/MemoryIdler.h>
#include <folly/portability/GFlags.h>

DEFINE_bool(
FOLLY_GFLAGS_DEFINE_bool(
dynamic_iothreadpoolexecutor,
true,
"IOThreadPoolExecutor will dynamically create threads");
Expand Down
2 changes: 1 addition & 1 deletion folly/executors/ThreadPoolExecutor.cpp
Expand Up @@ -43,7 +43,7 @@ void ThreadPoolExecutor::deregisterThreadPoolExecutor(ThreadPoolExecutor* tpe) {
});
}

DEFINE_int64(
FOLLY_GFLAGS_DEFINE_int64(
threadtimeout_ms,
60000,
"Idle time before ThreadPoolExecutor threads are joined");
Expand Down
2 changes: 1 addition & 1 deletion folly/experimental/observer/detail/ObserverManager.cpp
Expand Up @@ -33,7 +33,7 @@ thread_local bool ObserverManager::inManagerThread_{false};
thread_local ObserverManager::DependencyRecorder::Dependencies*
ObserverManager::DependencyRecorder::currentDependencies_{nullptr};

DEFINE_int32(
FOLLY_GFLAGS_DEFINE_int32(
observer_manager_pool_size,
4,
"How many internal threads ObserverManager should use");
Expand Down
2 changes: 1 addition & 1 deletion folly/init/Init.cpp
Expand Up @@ -29,7 +29,7 @@
#endif
#include <folly/portability/GFlags.h>

DEFINE_string(logging, "", "Logging configuration");
FOLLY_GFLAGS_DEFINE_string(logging, "", "Logging configuration");

namespace folly {
const unsigned long kAllFatalSignals =
Expand Down
139 changes: 95 additions & 44 deletions folly/portability/GFlags.h
Expand Up @@ -18,54 +18,105 @@

#include <folly/portability/Config.h>

#if !FOLLY_HAVE_LIBGFLAGS
// glog/logging.h is dependent on this implementation detail
// being defined otherwise it undefines all of this -_-....
//
// Also, this is deliberately expanded such that places using
// it directly break loudly. (C will break louder than C++, but oh well)
#define DECLARE_VARIABLE() \
static_assert(false, "You shouldn't be using GFlags internals.");

#define FOLLY_DECLARE_FLAG(_type, _shortType, _name) \
namespace fL##_shortType { \
extern _type FLAGS_##_name; \
} \
#include <cstdint>
#include <string>

#if FOLLY_HAVE_LIBGFLAGS && __has_include(<gflags/gflags.h>)
#include <gflags/gflags.h>
#endif

#if FOLLY_HAVE_LIBGFLAGS && __has_include(<gflags/gflags.h>)

#define FOLLY_GFLAGS_DECLARE_bool(_name) DECLARE_bool(_name)
#define FOLLY_GFLAGS_DECLARE_double(_name) DECLARE_double(_name)
#define FOLLY_GFLAGS_DECLARE_int32(_name) DECLARE_int32(_name)
#define FOLLY_GFLAGS_DECLARE_int64(_name) DECLARE_int64(_name)
#define FOLLY_GFLAGS_DECLARE_uint32(_name) DECLARE_uint32(_name)
#define FOLLY_GFLAGS_DECLARE_uint64(_name) DECLARE_uint64(_name)
#define FOLLY_GFLAGS_DECLARE_string(_name) DECLARE_string(_name)

#define FOLLY_GFLAGS_DEFINE_bool(_name, _default, _description) \
DEFINE_bool(_name, _default, _description)
#define FOLLY_GFLAGS_DEFINE_double(_name, _default, _description) \
DEFINE_double(_name, _default, _description)
#define FOLLY_GFLAGS_DEFINE_int32(_name, _default, _description) \
DEFINE_int32(_name, _default, _description)
#define FOLLY_GFLAGS_DEFINE_int64(_name, _default, _description) \
DEFINE_int64(_name, _default, _description)
#define FOLLY_GFLAGS_DEFINE_uint32(_name, _default, _description) \
DEFINE_uint32(_name, _default, _description)
#define FOLLY_GFLAGS_DEFINE_uint64(_name, _default, _description) \
DEFINE_uint64(_name, _default, _description)
#define FOLLY_GFLAGS_DEFINE_string(_name, _default, _description) \
DEFINE_string(_name, _default, _description)

#else

#define FOLLY_GFLAGS_DECLARE_IMPL_(_type, _shortType, _name) \
namespace fL##_shortType { \
extern _type FLAGS_##_name; \
} \
using fL##_shortType::FLAGS_##_name

#define DECLARE_bool(_name) FOLLY_DECLARE_FLAG(bool, B, _name)
#define DECLARE_double(_name) FOLLY_DECLARE_FLAG(double, D, _name)
#define DECLARE_int32(_name) FOLLY_DECLARE_FLAG(int, I, _name)
#define DECLARE_int64(_name) FOLLY_DECLARE_FLAG(long long, I64, _name)
#define DECLARE_uint32(_name) FOLLY_DECLARE_FLAG(unsigned long, U32, _name)
#define DECLARE_uint64(_name) FOLLY_DECLARE_FLAG(unsigned long long, U64, _name)
#define DECLARE_string(_name) FOLLY_DECLARE_FLAG(std::string, S, _name)

#define FOLLY_DEFINE_FLAG(_type, _shortType, _name, _default) \
namespace fL##_shortType { \
_type FLAGS_##_name = _default; \
} \
#define FOLLY_GFLAGS_DEFINE_IMPL_( \
_type, _shortType, _name, _default, _description) \
namespace fL##_shortType { \
_type FLAGS_##_name = _default; \
} \
using fL##_shortType::FLAGS_##_name

#define DEFINE_bool(_name, _default, _description) \
FOLLY_DEFINE_FLAG(bool, B, _name, _default)
#define DEFINE_double(_name, _default, _description) \
FOLLY_DEFINE_FLAG(double, D, _name, _default)
#define DEFINE_int32(_name, _default, _description) \
FOLLY_DEFINE_FLAG(int, I, _name, _default)
#define DEFINE_int64(_name, _default, _description) \
FOLLY_DEFINE_FLAG(long long, I64, _name, _default)
#define DEFINE_uint32(_name, _default, _description) \
FOLLY_DEFINE_FLAG(unsigned long, U32, _name, _default)
#define DEFINE_uint64(_name, _default, _description) \
FOLLY_DEFINE_FLAG(unsigned long long, U64, _name, _default)
#define DEFINE_string(_name, _default, _description) \
FOLLY_DEFINE_FLAG(std::string, S, _name, _default)

namespace google {
class FlagSaver {};
} // namespace google
#define FOLLY_GFLAGS_DECLARE_bool(_name) \
FOLLY_GFLAGS_DECLARE_IMPL_(bool, B, _name)
#define FOLLY_GFLAGS_DECLARE_double(_name) \
FOLLY_GFLAGS_DECLARE_IMPL_(double, D, _name)
#define FOLLY_GFLAGS_DECLARE_int32(_name) \
FOLLY_GFLAGS_DECLARE_IMPL_(::std::int32_t, I, _name)
#define FOLLY_GFLAGS_DECLARE_int64(_name) \
FOLLY_GFLAGS_DECLARE_IMPL_(::std::int64_t, I64, _name)
#define FOLLY_GFLAGS_DECLARE_uint32(_name) \
FOLLY_GFLAGS_DECLARE_IMPL_(::std::uint32_t, U32, _name)
#define FOLLY_GFLAGS_DECLARE_uint64(_name) \
FOLLY_GFLAGS_DECLARE_IMPL_(::std::uint64_t, U64, _name)
#define FOLLY_GFLAGS_DECLARE_string(_name) \
FOLLY_GFLAGS_DECLARE_IMPL_(::std::string, S, _name)

#define FOLLY_GFLAGS_DEFINE_bool(_name, _default, _description) \
FOLLY_GFLAGS_DEFINE_IMPL_(bool, B, _name, _default, _description)
#define FOLLY_GFLAGS_DEFINE_double(_name, _default, _description) \
FOLLY_GFLAGS_DEFINE_IMPL_(double, D, _name, _default, _description)
#define FOLLY_GFLAGS_DEFINE_int32(_name, _default, _description) \
FOLLY_GFLAGS_DEFINE_IMPL_(::std::int32_t, I, _name, _default, _description)
#define FOLLY_GFLAGS_DEFINE_int64(_name, _default, _description) \
FOLLY_GFLAGS_DEFINE_IMPL_(::std::int64_t, I64, _name, _default, _description)
#define FOLLY_GFLAGS_DEFINE_uint32(_name, _default, _description) \
FOLLY_GFLAGS_DEFINE_IMPL_(::std::uint32_t, U32, _name, _default, _description)
#define FOLLY_GFLAGS_DEFINE_uint64(_name, _default, _description) \
FOLLY_GFLAGS_DEFINE_IMPL_(::std::uint64_t, U64, _name, _default, _description)
#define FOLLY_GFLAGS_DEFINE_string(_name, _default, _description) \
FOLLY_GFLAGS_DEFINE_IMPL_(::std::string, S, _name, _default, _description)

#endif

#if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
namespace FOLLY_GFLAGS_NAMESPACE {}
namespace gflags {
using namespace FOLLY_GFLAGS_NAMESPACE;
} // namespace gflags
#endif

namespace folly {

#if FOLLY_HAVE_LIBGFLAGS && __has_include(<gflags/gflags.h>)

using FlagSaver = gflags::FlagSaver;

#else
#include <gflags/gflags.h>

namespace detail {
struct GflagsFlagSaver {};
} // namespace detail
using FlagSaver = detail::GflagsFlagSaver;

#endif

} // namespace folly
2 changes: 1 addition & 1 deletion folly/synchronization/Hazptr.cpp
Expand Up @@ -20,7 +20,7 @@

#include <folly/portability/GFlags.h>

DEFINE_bool(
FOLLY_GFLAGS_DEFINE_bool(
folly_hazptr_use_executor,
true,
"Use an executor for hazptr asynchronous reclamation");
Expand Down
2 changes: 1 addition & 1 deletion folly/system/MemoryMapping.cpp
Expand Up @@ -46,7 +46,7 @@ static constexpr ssize_t kDefaultMlockChunkSize = !folly::kMscVer
// with the same address is a bad idea with the windows implementation.
: (-1);

DEFINE_int64(
FOLLY_GFLAGS_DEFINE_int64(
mlock_chunk_size,
kDefaultMlockChunkSize,
"Maximum bytes to mlock/munlock/munmap at once "
Expand Down

0 comments on commit 4dadde1

Please sign in to comment.